1 |
pcg |
1.8 |
#include "../config.h" |
2 |
pcg |
1.1 |
#include "rxvtdaemon.h" |
3 |
|
|
|
4 |
|
|
#include <cstdio> |
5 |
|
|
#include <cstdlib> |
6 |
|
|
|
7 |
|
|
#include <unistd.h> |
8 |
pcg |
1.3 |
#include <signal.h> |
9 |
pcg |
1.1 |
#include <sys/socket.h> |
10 |
|
|
#include <sys/un.h> |
11 |
|
|
|
12 |
|
|
struct client : rxvt_connection { |
13 |
|
|
client (); |
14 |
|
|
}; |
15 |
|
|
|
16 |
|
|
client::client () |
17 |
|
|
{ |
18 |
|
|
if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
19 |
|
|
{ |
20 |
pcg |
1.5 |
perror ("unable to create communications socket"); |
21 |
pcg |
1.1 |
exit (EXIT_FAILURE); |
22 |
|
|
} |
23 |
|
|
|
24 |
pcg |
1.4 |
char *sockname = rxvt_connection::unix_sockname (); |
25 |
pcg |
1.1 |
sockaddr_un sa; |
26 |
|
|
sa.sun_family = AF_UNIX; |
27 |
pcg |
1.4 |
strcpy (sa.sun_path, sockname); |
28 |
|
|
free (sockname); |
29 |
pcg |
1.1 |
|
30 |
|
|
if (connect (fd, (sockaddr *)&sa, sizeof (sa))) |
31 |
|
|
{ |
32 |
pcg |
1.5 |
perror ("unable to connect to rxvtd"); |
33 |
pcg |
1.1 |
exit (EXIT_FAILURE); |
34 |
|
|
} |
35 |
|
|
} |
36 |
|
|
|
37 |
pcg |
1.3 |
extern char **environ; |
38 |
|
|
|
39 |
pcg |
1.1 |
int |
40 |
pcg |
1.7 |
main (int argc, const char *const *argv) |
41 |
pcg |
1.1 |
{ |
42 |
|
|
client c; |
43 |
|
|
char buf[PATH_MAX]; |
44 |
|
|
|
45 |
pcg |
1.3 |
{ |
46 |
|
|
sigset_t ss; |
47 |
|
|
|
48 |
pcg |
1.6 |
sigemptyset (&ss); |
49 |
pcg |
1.3 |
sigaddset (&ss, SIGHUP); |
50 |
|
|
sigprocmask (SIG_BLOCK, &ss, 0); |
51 |
|
|
} |
52 |
|
|
|
53 |
pcg |
1.1 |
c.send ("NEW"); |
54 |
pcg |
1.7 |
// instead of getcwd we could opendir (".") and pass the fd for fchdir *g* |
55 |
pcg |
1.2 |
c.send ("CWD"), c.send (getcwd (buf, sizeof (buf))); |
56 |
pcg |
1.3 |
|
57 |
|
|
for (char **var = environ; *environ; environ++) |
58 |
|
|
c.send ("ENV"), c.send (*environ); |
59 |
pcg |
1.1 |
|
60 |
|
|
for (int i = 0; i < argc; i++) |
61 |
pcg |
1.2 |
c.send ("ARG"), c.send (argv[i]); |
62 |
|
|
|
63 |
pcg |
1.1 |
c.send ("END"); |
64 |
pcg |
1.8 |
|
65 |
|
|
auto_str tok; |
66 |
|
|
|
67 |
|
|
for (;;) |
68 |
|
|
if (!c.recv (tok)) |
69 |
|
|
{ |
70 |
|
|
fprintf (stderr, "protocol error: unexpected eof from server.\n"); |
71 |
|
|
break; |
72 |
|
|
} |
73 |
|
|
else if (!strcmp (tok, "MSG") && c.recv (tok)) |
74 |
|
|
fprintf (stderr, "%s", (const char *)tok); |
75 |
|
|
else if (!strcmp (tok, "END")) |
76 |
|
|
{ |
77 |
|
|
int success; |
78 |
|
|
if (c.recv (success)) |
79 |
|
|
exit (success ? EXIT_SUCCESS : EXIT_FAILURE); |
80 |
|
|
} |
81 |
|
|
else |
82 |
|
|
{ |
83 |
|
|
fprintf (stderr, "protocol error: received illegal token '%s'.\n", (const char *)tok); |
84 |
|
|
break; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
return EXIT_FAILURE; |
88 |
pcg |
1.1 |
} |
89 |
|
|
|