1 |
#include "../config.h" |
2 |
#include "rxvtdaemon.h" |
3 |
|
4 |
#include <cstdio> |
5 |
#include <cstdlib> |
6 |
|
7 |
#include <unistd.h> |
8 |
#include <signal.h> |
9 |
#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 |
perror ("unable to create communications socket"); |
21 |
exit (EXIT_FAILURE); |
22 |
} |
23 |
|
24 |
char *sockname = rxvt_connection::unix_sockname (); |
25 |
sockaddr_un sa; |
26 |
sa.sun_family = AF_UNIX; |
27 |
strcpy (sa.sun_path, sockname); |
28 |
free (sockname); |
29 |
|
30 |
if (connect (fd, (sockaddr *)&sa, sizeof (sa))) |
31 |
{ |
32 |
perror ("unable to connect to rxvtd"); |
33 |
exit (EXIT_FAILURE); |
34 |
} |
35 |
} |
36 |
|
37 |
extern char **environ; |
38 |
|
39 |
int |
40 |
main (int argc, const char *const *argv) |
41 |
{ |
42 |
client c; |
43 |
char buf[PATH_MAX]; |
44 |
|
45 |
{ |
46 |
sigset_t ss; |
47 |
|
48 |
sigemptyset (&ss); |
49 |
sigaddset (&ss, SIGHUP); |
50 |
sigprocmask (SIG_BLOCK, &ss, 0); |
51 |
} |
52 |
|
53 |
c.send ("NEW"); |
54 |
// instead of getcwd we could opendir (".") and pass the fd for fchdir *g* |
55 |
c.send ("CWD"), c.send (getcwd (buf, sizeof (buf))); |
56 |
|
57 |
for (char **var = environ; *environ; environ++) |
58 |
c.send ("ENV"), c.send (*environ); |
59 |
|
60 |
for (int i = 0; i < argc; i++) |
61 |
c.send ("ARG"), c.send (argv[i]); |
62 |
|
63 |
c.send ("END"); |
64 |
|
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 |
} |
89 |
|