… | |
… | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | *----------------------------------------------------------------------*/ |
21 | *----------------------------------------------------------------------*/ |
22 | |
22 | |
23 | #include "../config.h" |
23 | #include "../config.h" |
24 | #include "rxvtdaemon.h" |
24 | #include "rxvtdaemon.h" |
|
|
25 | #include "fdpass.h" |
25 | |
26 | |
26 | #include "rxvt.h" |
27 | #include "rxvt.h" |
27 | |
28 | |
28 | #include <cstdio> |
29 | #include <cstdio> |
29 | #include <cstdlib> |
30 | #include <cstdlib> |
|
|
31 | #include <cstring> |
|
|
32 | #include <csignal> |
30 | |
33 | |
31 | #include <unistd.h> |
34 | #include <unistd.h> |
32 | #include <signal.h> |
|
|
33 | #include <sys/socket.h> |
35 | #include <sys/socket.h> |
34 | #include <sys/un.h> |
36 | #include <sys/un.h> |
35 | |
37 | |
36 | struct client : rxvt_connection { |
38 | struct client : rxvt_connection { |
37 | client (); |
39 | client (); |
38 | }; |
40 | }; |
39 | |
41 | |
40 | client::client () |
42 | client::client () |
41 | { |
43 | { |
42 | if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) |
44 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
43 | { |
45 | { |
44 | perror ("unable to create communications socket"); |
46 | perror ("unable to create communications socket"); |
45 | exit (EXIT_FAILURE); |
47 | exit (EXIT_FAILURE); |
46 | } |
48 | } |
47 | |
49 | |
… | |
… | |
51 | strcpy (sa.sun_path, sockname); |
53 | strcpy (sa.sun_path, sockname); |
52 | free (sockname); |
54 | free (sockname); |
53 | |
55 | |
54 | if (connect (fd, (sockaddr *)&sa, sizeof (sa))) |
56 | if (connect (fd, (sockaddr *)&sa, sizeof (sa))) |
55 | { |
57 | { |
56 | perror ("unable to connect to rxvtd"); |
58 | perror ("unable to connect to the rxvt-unicode daemon"); |
57 | exit (EXIT_FAILURE); |
59 | exit (EXIT_FAILURE); |
58 | } |
60 | } |
59 | } |
61 | } |
60 | |
62 | |
61 | extern char **environ; |
63 | extern char **environ; |
62 | |
64 | |
63 | int |
65 | int |
64 | main (int argc, const char *const *argv) |
66 | main (int argc, const char *const *argv) |
65 | { |
67 | { |
66 | client c; |
68 | client c; |
67 | char buf[PATH_MAX]; |
|
|
68 | |
|
|
69 | { |
69 | { |
70 | sigset_t ss; |
70 | sigset_t ss; |
71 | |
71 | |
72 | sigemptyset (&ss); |
72 | sigemptyset (&ss); |
73 | sigaddset (&ss, SIGHUP); |
73 | sigaddset (&ss, SIGHUP); |
74 | sigprocmask (SIG_BLOCK, &ss, 0); |
74 | sigprocmask (SIG_BLOCK, &ss, 0); |
75 | } |
75 | } |
76 | |
76 | |
77 | c.send ("NEW"); |
77 | c.send ("NEW"); |
|
|
78 | |
78 | // instead of getcwd we could opendir (".") and pass the fd for fchdir *g* |
79 | // instead of getcwd we could opendir (".") and pass the fd for fchdir *g* |
79 | c.send ("CWD"), c.send (getcwd (buf, sizeof (buf))); |
80 | char cwd[PATH_MAX]; |
80 | |
81 | |
81 | for (char **var = environ; *environ; environ++) |
82 | if (!getcwd (cwd, sizeof (cwd))) |
82 | c.send ("ENV"), c.send (*environ); |
83 | { |
|
|
84 | perror ("unable to determine current working directory"); |
|
|
85 | exit (EXIT_FAILURE); |
|
|
86 | } |
83 | |
87 | |
|
|
88 | c.send ("CWD"), c.send (cwd); |
|
|
89 | |
|
|
90 | for (char **var = environ; *var; var++) |
|
|
91 | c.send ("ENV"), c.send (*var); |
|
|
92 | |
|
|
93 | const char *base = strrchr (argv[0], '/'); |
|
|
94 | base = base ? base + 1 : argv[0]; |
|
|
95 | c.send ("ARG"), c.send (strcmp (base, RXVTNAME "c") ? base : RXVTNAME); |
|
|
96 | |
84 | for (int i = 0; i < argc; i++) |
97 | for (int i = 1; i < argc; i++) |
85 | c.send ("ARG"), c.send (argv[i]); |
98 | c.send ("ARG"), c.send (argv[i]); |
86 | |
99 | |
87 | c.send ("END"); |
100 | c.send ("END"); |
88 | |
101 | |
89 | auto_str tok; |
102 | auto_str tok; |
|
|
103 | int cint; |
90 | |
104 | |
91 | for (;;) |
105 | for (;;) |
92 | if (!c.recv (tok)) |
106 | if (!c.recv (tok)) |
93 | { |
107 | { |
94 | fprintf (stderr, "protocol error: unexpected eof from server.\n"); |
108 | fprintf (stderr, "protocol error: unexpected eof from server.\n"); |
95 | break; |
109 | break; |
96 | } |
110 | } |
97 | else if (!strcmp (tok, "MSG") && c.recv (tok)) |
111 | else if (!strcmp (tok, "MSG") && c.recv (tok)) |
98 | fprintf (stderr, "%s", (const char *)tok); |
112 | fprintf (stderr, "%s", (const char *)tok); |
|
|
113 | else if (!strcmp (tok, "GETFD") && c.recv (cint)) |
|
|
114 | { |
|
|
115 | if (rxvt_send_fd (c.fd, cint) < 0) |
|
|
116 | { |
|
|
117 | fprintf (stderr, "unable to send fd %d: ", cint); perror (0); |
|
|
118 | exit (EXIT_FAILURE); |
|
|
119 | } |
|
|
120 | } |
99 | else if (!strcmp (tok, "END")) |
121 | else if (!strcmp (tok, "END")) |
100 | { |
122 | { |
101 | int success; |
123 | int success; |
|
|
124 | |
102 | if (c.recv (success)) |
125 | if (c.recv (success)) |
103 | exit (success ? EXIT_SUCCESS : EXIT_FAILURE); |
126 | exit (success ? EXIT_SUCCESS : EXIT_FAILURE); |
104 | } |
127 | } |
105 | else |
128 | else |
106 | { |
129 | { |
107 | fprintf (stderr, "protocol error: received illegal token '%s'.\n", (const char *)tok); |
130 | fprintf (stderr, "protocol error: received unsupported token '%s'.\n", (const char *)tok); |
108 | break; |
131 | break; |
109 | } |
132 | } |
110 | |
133 | |
111 | return EXIT_FAILURE; |
134 | return EXIT_FAILURE; |
112 | } |
135 | } |