1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: rxvtd.C |
2 | * File: rxvtd.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com> |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
… | |
… | |
19 | * along with this program; if not, write to the Free Software |
19 | * along with this program; if not, write to the Free Software |
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 "rxvt.h" |
|
|
25 | #include "rxvtdaemon.h" |
|
|
26 | #include "fdpass.h" |
|
|
27 | #include "iom.h" |
|
|
28 | |
|
|
29 | #include <cstdio> |
24 | #include <cstdio> |
30 | #include <cstdlib> |
25 | #include <cstdlib> |
31 | #include <cstdarg> |
26 | #include <cstdarg> |
32 | #include <cstring> |
27 | #include <cstring> |
33 | |
28 | |
… | |
… | |
38 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
39 | #include <sys/un.h> |
34 | #include <sys/un.h> |
40 | |
35 | |
41 | #include <cerrno> |
36 | #include <cerrno> |
42 | |
37 | |
43 | extern char **environ; |
38 | #include "rxvt.h" |
|
|
39 | #include "rxvtdaemon.h" |
|
|
40 | #include "libptytty.h" |
44 | |
41 | |
45 | struct server : rxvt_connection { |
42 | struct server : rxvt_connection { |
46 | log_callback log_cb; |
43 | log_callback log_cb; |
47 | getfd_callback getfd_cb; |
44 | getfd_callback getfd_cb; |
48 | |
45 | |
49 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
46 | void read_cb (ev::io &w, int revents); ev::io read_ev; |
50 | void log_msg (const char *msg); |
47 | void log_msg (const char *msg); |
51 | int getfd (int remote_fd); |
48 | int getfd (int remote_fd); |
52 | |
49 | |
53 | server (int fd) |
50 | server (int fd) |
54 | : read_ev (this, &server::read_cb), |
|
|
55 | log_cb (this, &server::log_msg), |
|
|
56 | getfd_cb (this, &server::getfd) |
|
|
57 | { |
51 | { |
|
|
52 | read_ev.set <server, &server::read_cb> (this); |
|
|
53 | log_cb.set <server, &server::log_msg> (this); |
|
|
54 | getfd_cb.set<server, &server::getfd> (this); |
|
|
55 | |
58 | this->fd = fd; |
56 | this->fd = fd; |
|
|
57 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
58 | fcntl (fd, F_SETFL, 0); |
59 | read_ev.start (fd, EVENT_READ); |
59 | read_ev.start (fd, ev::READ); |
60 | } |
60 | } |
61 | |
61 | |
62 | void err (const char *format = 0, ...); |
62 | void err (const char *format = 0, ...); |
63 | }; |
63 | }; |
64 | |
64 | |
65 | struct unix_listener { |
65 | struct unix_listener { |
66 | int fd; |
66 | int fd; |
67 | |
67 | |
68 | void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
68 | void accept_cb (ev::io &w, int revents); ev::io accept_ev; |
69 | |
69 | |
70 | unix_listener (const char *sockname); |
70 | unix_listener (const char *sockname); |
71 | }; |
71 | }; |
72 | |
72 | |
73 | unix_listener::unix_listener (const char *sockname) |
73 | unix_listener::unix_listener (const char *sockname) |
74 | : accept_ev (this, &unix_listener::accept_cb) |
|
|
75 | { |
74 | { |
|
|
75 | accept_ev.set<unix_listener, &unix_listener::accept_cb> (this); |
|
|
76 | |
|
|
77 | sockaddr_un sa; |
|
|
78 | |
|
|
79 | if (strlen (sockname) >= sizeof(sa.sun_path)) |
|
|
80 | { |
|
|
81 | fputs ("socket name too long, aborting.\n", stderr); |
|
|
82 | exit (EXIT_FAILURE); |
|
|
83 | } |
|
|
84 | |
76 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
85 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
77 | { |
86 | { |
78 | perror ("unable to create listening socket"); |
87 | perror ("unable to create listening socket"); |
79 | exit (EXIT_FAILURE); |
88 | exit (EXIT_FAILURE); |
80 | } |
89 | } |
81 | |
90 | |
82 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
91 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
83 | |
92 | fcntl (fd, F_SETFL, O_NONBLOCK); |
84 | sockaddr_un sa; |
|
|
85 | |
93 | |
86 | sa.sun_family = AF_UNIX; |
94 | sa.sun_family = AF_UNIX; |
87 | strcpy (sa.sun_path, sockname); |
95 | strcpy (sa.sun_path, sockname); |
88 | |
96 | |
89 | unlink (rxvt_connection::unix_sockname ()); |
97 | unlink (rxvt_connection::unix_sockname ()); |
… | |
… | |
102 | { |
110 | { |
103 | perror ("unable to listen on socket"); |
111 | perror ("unable to listen on socket"); |
104 | exit (EXIT_FAILURE); |
112 | exit (EXIT_FAILURE); |
105 | } |
113 | } |
106 | |
114 | |
107 | accept_ev.start (fd, EVENT_READ); |
115 | accept_ev.start (fd, ev::READ); |
108 | } |
116 | } |
109 | |
117 | |
110 | void unix_listener::accept_cb (io_watcher &w, short revents) |
118 | void unix_listener::accept_cb (ev::io &w, int revents) |
111 | { |
119 | { |
112 | int fd2 = accept (fd, 0, 0); |
120 | int fd2 = accept (fd, 0, 0); |
113 | |
121 | |
114 | if (fd2 >= 0) |
122 | if (fd2 >= 0) |
115 | { |
|
|
116 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
|
|
117 | new server (fd2); |
123 | new server (fd2); |
118 | } |
|
|
119 | } |
124 | } |
120 | |
125 | |
121 | int server::getfd (int remote_fd) |
126 | int server::getfd (int remote_fd) |
122 | { |
127 | { |
123 | send ("GETFD"); |
128 | send ("GETFD"); |
124 | send (remote_fd); |
129 | send (remote_fd); |
125 | return rxvt_recv_fd (fd); |
130 | return ptytty::recv_fd (fd); |
126 | } |
131 | } |
127 | |
132 | |
128 | void server::log_msg (const char *msg) |
133 | void server::log_msg (const char *msg) |
129 | { |
134 | { |
130 | send ("MSG"), send (msg); |
135 | send ("MSG"), send (msg); |
… | |
… | |
139 | va_list ap; |
144 | va_list ap; |
140 | va_start (ap, format); |
145 | va_start (ap, format); |
141 | vsnprintf (err, 1024, format, ap); |
146 | vsnprintf (err, 1024, format, ap); |
142 | va_end (ap); |
147 | va_end (ap); |
143 | |
148 | |
144 | send ("MSG"), send (err); |
149 | log_msg (err); |
145 | } |
150 | } |
146 | |
151 | |
147 | send ("END", 0); |
152 | send ("END"), send (0); |
148 | close (fd); |
153 | close (fd); |
149 | delete this; |
154 | delete this; |
150 | } |
155 | } |
151 | |
156 | |
152 | void server::read_cb (io_watcher &w, short revents) |
157 | void server::read_cb (ev::io &w, int revents) |
153 | { |
158 | { |
154 | auto_str tok; |
159 | auto_str tok; |
155 | |
160 | |
156 | if (recv (tok)) |
161 | if (recv (tok)) |
157 | { |
162 | { |
158 | if (!strcmp (tok, "NEW")) |
163 | if (!strcmp (tok, "NEW")) |
159 | { |
164 | { |
160 | stringvec *argv = new stringvec; |
165 | stringvec *argv = new stringvec; |
161 | stringvec *envv = new stringvec; |
166 | stringvec *envv = new stringvec; |
162 | |
167 | |
163 | for (;;) |
168 | for (;;) |
164 | { |
169 | { |
165 | if (!recv (tok)) |
170 | if (!recv (tok)) |
166 | return err (); |
171 | return err (); |
167 | |
172 | |
… | |
… | |
170 | else if (!strcmp (tok, "ENV") && recv (tok)) |
175 | else if (!strcmp (tok, "ENV") && recv (tok)) |
171 | envv->push_back (strdup (tok)); |
176 | envv->push_back (strdup (tok)); |
172 | else if (!strcmp (tok, "CWD") && recv (tok)) |
177 | else if (!strcmp (tok, "CWD") && recv (tok)) |
173 | { |
178 | { |
174 | if (chdir (tok)) |
179 | if (chdir (tok)) |
|
|
180 | { |
|
|
181 | delete envv; |
|
|
182 | delete argv; |
175 | err ("unable to change to working directory to '%s': %s", |
183 | return err ("unable to change to working directory to '%s', aborting: %s.\n", |
176 | (char *)tok, strerror (errno)); |
184 | (char *)tok, strerror (errno)); |
|
|
185 | } |
177 | } |
186 | } |
178 | else if (!strcmp (tok, "ARG") && recv (tok)) |
187 | else if (!strcmp (tok, "ARG") && recv (tok)) |
179 | argv->push_back (strdup (tok)); |
188 | argv->push_back (strdup (tok)); |
180 | else |
189 | else |
181 | return err ("protocol error: unexpected NEW token"); |
190 | return err ("protocol error: unexpected NEW token.\n"); |
182 | } |
191 | } |
183 | |
192 | |
184 | envv->push_back (0); |
193 | envv->push_back (0); |
185 | |
194 | |
186 | { |
195 | { |
187 | rxvt_term *term = new rxvt_term; |
196 | rxvt_term *term = new rxvt_term; |
188 | |
197 | |
189 | term->log_hook = &log_cb; |
198 | term->log_hook = &log_cb; |
190 | term->getfd_hook = &getfd_cb; |
199 | term->getfd_hook = &getfd_cb; |
191 | |
200 | |
192 | bool success; |
201 | bool success = true; |
193 | |
202 | |
194 | try |
203 | try |
195 | { |
204 | { |
196 | success = term->init (argv, envv); |
205 | term->init (argv, envv); |
197 | } |
206 | } |
198 | catch (const class rxvt_failure_exception &e) |
207 | catch (const class rxvt_failure_exception &e) |
199 | { |
208 | { |
200 | success = false; |
209 | success = false; |
201 | } |
210 | } |
… | |
… | |
209 | |
218 | |
210 | send ("END"); send (success ? 1 : 0); |
219 | send ("END"); send (success ? 1 : 0); |
211 | } |
220 | } |
212 | } |
221 | } |
213 | else |
222 | else |
214 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
223 | return err ("protocol error: request '%s' unsupported.\n", (char *)tok); |
215 | } |
224 | } |
216 | else |
225 | else |
217 | return err (); |
226 | return err (); |
218 | } |
227 | } |
219 | |
228 | |
… | |
… | |
236 | { |
245 | { |
237 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
246 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
238 | return EXIT_FAILURE; |
247 | return EXIT_FAILURE; |
239 | } |
248 | } |
240 | } |
249 | } |
241 | |
250 | |
242 | chdir ("/"); |
251 | chdir ("/"); |
243 | |
252 | |
244 | if (opt_opendisplay) |
253 | if (opt_opendisplay) |
245 | displays.get (getenv ("DISPLAY")); // open display and never release it |
254 | displays.get (getenv ("DISPLAY")); // open display and never release it |
246 | |
255 | |
… | |
… | |
264 | rxvt_log ("unable to fork daemon, aborting.\n"); |
273 | rxvt_log ("unable to fork daemon, aborting.\n"); |
265 | return EXIT_FAILURE; |
274 | return EXIT_FAILURE; |
266 | } |
275 | } |
267 | else if (pid > 0) |
276 | else if (pid > 0) |
268 | _exit (EXIT_SUCCESS); |
277 | _exit (EXIT_SUCCESS); |
269 | } |
|
|
270 | |
278 | |
271 | io_manager::loop (); |
279 | ev_default_fork (); |
|
|
280 | } |
|
|
281 | |
|
|
282 | ev_loop (0); |
272 | |
283 | |
273 | return EXIT_SUCCESS; |
284 | return EXIT_SUCCESS; |
274 | } |
285 | } |
275 | |
286 | |