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