… | |
… | |
21 | *----------------------------------------------------------------------*/ |
21 | *----------------------------------------------------------------------*/ |
22 | |
22 | |
23 | #include "../config.h" |
23 | #include "../config.h" |
24 | #include "rxvt.h" |
24 | #include "rxvt.h" |
25 | #include "rxvtdaemon.h" |
25 | #include "rxvtdaemon.h" |
|
|
26 | #include "fdpass.h" |
26 | #include "iom.h" |
27 | #include "iom.h" |
27 | |
28 | |
28 | #include <cstdio> |
29 | #include <cstdio> |
29 | #include <cstdlib> |
30 | #include <cstdlib> |
30 | #include <cstdarg> |
31 | #include <cstdarg> |
… | |
… | |
41 | |
42 | |
42 | extern char **environ; |
43 | extern char **environ; |
43 | |
44 | |
44 | struct server : rxvt_connection { |
45 | struct server : rxvt_connection { |
45 | log_callback log_cb; |
46 | log_callback log_cb; |
|
|
47 | getfd_callback getfd_cb; |
46 | |
48 | |
47 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
49 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
48 | void log_msg (const char *msg); |
50 | void log_msg (const char *msg); |
|
|
51 | int getfd (int remote_fd); |
49 | |
52 | |
50 | server (int fd) |
53 | server (int fd) |
51 | : read_ev (this, &server::read_cb), |
54 | : read_ev (this, &server::read_cb), |
52 | log_cb (this, &server::log_msg) |
55 | log_cb (this, &server::log_msg), |
|
|
56 | getfd_cb (this, &server::getfd) |
53 | { |
57 | { |
54 | this->fd = fd; |
58 | this->fd = fd; |
55 | read_ev.start (fd, EVENT_READ); |
59 | read_ev.start (fd, EVENT_READ); |
56 | } |
60 | } |
57 | |
61 | |
… | |
… | |
110 | if (fd2 >= 0) |
114 | if (fd2 >= 0) |
111 | { |
115 | { |
112 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
116 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
113 | new server (fd2); |
117 | new server (fd2); |
114 | } |
118 | } |
|
|
119 | } |
|
|
120 | |
|
|
121 | int server::getfd (int remote_fd) |
|
|
122 | { |
|
|
123 | #if ENABLE_FRILLS && HAVE_UNIX_FDPASS |
|
|
124 | send ("GETFD"); |
|
|
125 | send (remote_fd); |
|
|
126 | return rxvt_recv_fd (fd); |
|
|
127 | #else |
|
|
128 | return -1; |
|
|
129 | #endif |
115 | } |
130 | } |
116 | |
131 | |
117 | void server::log_msg (const char *msg) |
132 | void server::log_msg (const char *msg) |
118 | { |
133 | { |
119 | send ("MSG"), send (msg); |
134 | send ("MSG"), send (msg); |
… | |
… | |
171 | } |
186 | } |
172 | |
187 | |
173 | envv->push_back (0); |
188 | envv->push_back (0); |
174 | |
189 | |
175 | { |
190 | { |
176 | char **old_environ = environ; |
|
|
177 | environ = envv->begin (); |
|
|
178 | |
|
|
179 | rxvt_term *term = new rxvt_term; |
191 | rxvt_term *term = new rxvt_term; |
180 | |
192 | |
181 | term->log_hook = &log_cb; |
193 | term->log_hook = &log_cb; |
|
|
194 | term->getfd_hook = &getfd_cb; |
182 | term->argv = argv; |
195 | term->argv = argv; |
183 | term->envv = envv; |
196 | term->envv = envv; |
184 | |
197 | |
185 | bool success; |
198 | bool success; |
186 | |
199 | |
… | |
… | |
193 | success = false; |
206 | success = false; |
194 | } |
207 | } |
195 | |
208 | |
196 | term->log_hook = 0; |
209 | term->log_hook = 0; |
197 | |
210 | |
198 | environ = old_environ; |
211 | chdir ("/"); |
199 | |
212 | |
200 | if (!success) |
213 | if (!success) |
201 | term->destroy (); |
214 | term->destroy (); |
202 | |
215 | |
203 | send ("END"); send (success ? 1 : 0); |
216 | send ("END"); send (success ? 1 : 0); |
… | |
… | |
208 | } |
221 | } |
209 | else |
222 | else |
210 | return err (); |
223 | return err (); |
211 | } |
224 | } |
212 | |
225 | |
|
|
226 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
227 | |
213 | int |
228 | int |
214 | main (int argc, const char *const *argv) |
229 | main (int argc, const char *const *argv) |
215 | { |
230 | { |
|
|
231 | for (int i = 1; i < argc; i++) |
|
|
232 | { |
|
|
233 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
|
|
234 | opt_fork = 1; |
|
|
235 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
|
|
236 | opt_opendisplay = 1; |
|
|
237 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
|
|
238 | opt_quiet = 1; |
|
|
239 | else |
|
|
240 | { |
|
|
241 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
|
|
242 | return EXIT_FAILURE; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
216 | rxvt_init (); |
246 | rxvt_init (); |
|
|
247 | |
|
|
248 | chdir ("/"); |
|
|
249 | |
|
|
250 | if (opt_opendisplay) |
|
|
251 | displays.get (getenv ("DISPLAY")); // open display and never release it |
217 | |
252 | |
218 | char *sockname = rxvt_connection::unix_sockname (); |
253 | char *sockname = rxvt_connection::unix_sockname (); |
219 | unix_listener l (sockname); |
254 | unix_listener l (sockname); |
|
|
255 | |
|
|
256 | if (!opt_quiet) |
|
|
257 | { |
220 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
258 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
|
|
259 | fflush (stdout); |
|
|
260 | } |
|
|
261 | |
221 | free (sockname); |
262 | free (sockname); |
222 | |
263 | |
223 | iom.loop (); |
264 | if (opt_fork) |
|
|
265 | { |
|
|
266 | pid_t pid = fork (); |
|
|
267 | |
|
|
268 | if (pid < 0) |
|
|
269 | { |
|
|
270 | rxvt_log ("unable to fork daemon, aborting.\n"); |
|
|
271 | return EXIT_FAILURE; |
|
|
272 | } |
|
|
273 | else if (pid > 0) |
|
|
274 | _exit (EXIT_SUCCESS); |
|
|
275 | } |
|
|
276 | |
|
|
277 | io_manager::loop (); |
224 | |
278 | |
225 | return EXIT_SUCCESS; |
279 | return EXIT_SUCCESS; |
226 | } |
280 | } |
227 | |
281 | |