1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
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-2006 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 "iom.h" |
|
|
27 | |
|
|
28 | #include <cstdio> |
24 | #include <cstdio> |
29 | #include <cstdlib> |
25 | #include <cstdlib> |
30 | #include <cstdarg> |
26 | #include <cstdarg> |
31 | #include <cstring> |
27 | #include <cstring> |
32 | |
28 | |
… | |
… | |
37 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
38 | #include <sys/un.h> |
34 | #include <sys/un.h> |
39 | |
35 | |
40 | #include <cerrno> |
36 | #include <cerrno> |
41 | |
37 | |
42 | extern char **environ; |
38 | #include "rxvt.h" |
|
|
39 | #include "rxvtdaemon.h" |
|
|
40 | #include "libptytty.h" |
|
|
41 | #include "iom.h" |
43 | |
42 | |
44 | struct server : rxvt_connection { |
43 | struct server : rxvt_connection { |
45 | log_callback log_cb; |
44 | log_callback log_cb; |
|
|
45 | getfd_callback getfd_cb; |
46 | |
46 | |
47 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
47 | void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
48 | void log_msg (const char *msg); |
48 | void log_msg (const char *msg); |
|
|
49 | int getfd (int remote_fd); |
49 | |
50 | |
50 | server (int fd) |
51 | server (int fd) |
51 | : read_ev (this, &server::read_cb), |
52 | : read_ev (this, &server::read_cb), |
52 | log_cb (this, &server::log_msg) |
53 | log_cb (this, &server::log_msg), |
|
|
54 | getfd_cb (this, &server::getfd) |
53 | { |
55 | { |
54 | this->fd = fd; |
56 | this->fd = fd; |
55 | read_ev.start (fd, EVENT_READ); |
57 | read_ev.start (fd, EVENT_READ); |
56 | } |
58 | } |
57 | |
59 | |
… | |
… | |
67 | }; |
69 | }; |
68 | |
70 | |
69 | unix_listener::unix_listener (const char *sockname) |
71 | unix_listener::unix_listener (const char *sockname) |
70 | : accept_ev (this, &unix_listener::accept_cb) |
72 | : accept_ev (this, &unix_listener::accept_cb) |
71 | { |
73 | { |
72 | if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) |
74 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
73 | { |
75 | { |
74 | perror ("unable to create listening socket"); |
76 | perror ("unable to create listening socket"); |
75 | exit (EXIT_FAILURE); |
77 | exit (EXIT_FAILURE); |
76 | } |
78 | } |
77 | |
79 | |
… | |
… | |
110 | if (fd2 >= 0) |
112 | if (fd2 >= 0) |
111 | { |
113 | { |
112 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
114 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
113 | new server (fd2); |
115 | new server (fd2); |
114 | } |
116 | } |
|
|
117 | } |
|
|
118 | |
|
|
119 | int server::getfd (int remote_fd) |
|
|
120 | { |
|
|
121 | send ("GETFD"); |
|
|
122 | send (remote_fd); |
|
|
123 | return ptytty::recv_fd (fd); |
115 | } |
124 | } |
116 | |
125 | |
117 | void server::log_msg (const char *msg) |
126 | void server::log_msg (const char *msg) |
118 | { |
127 | { |
119 | send ("MSG"), send (msg); |
128 | send ("MSG"), send (msg); |
… | |
… | |
155 | return err (); |
164 | return err (); |
156 | |
165 | |
157 | if (!strcmp (tok, "END")) |
166 | if (!strcmp (tok, "END")) |
158 | break; |
167 | break; |
159 | else if (!strcmp (tok, "ENV") && recv (tok)) |
168 | else if (!strcmp (tok, "ENV") && recv (tok)) |
160 | envv->push_back (tok.get ()); |
169 | envv->push_back (strdup (tok)); |
161 | else if (!strcmp (tok, "CWD") && recv (tok)) |
170 | else if (!strcmp (tok, "CWD") && recv (tok)) |
162 | { |
171 | { |
163 | if (chdir (tok)) |
172 | if (chdir (tok)) |
164 | err ("unable to change to working directory to '%s': %s", |
173 | err ("unable to change to working directory to '%s': %s", |
165 | (char *)tok, strerror (errno)); |
174 | (char *)tok, strerror (errno)); |
166 | } |
175 | } |
167 | else if (!strcmp (tok, "ARG") && recv (tok)) |
176 | else if (!strcmp (tok, "ARG") && recv (tok)) |
168 | argv->push_back (tok.get ()); |
177 | argv->push_back (strdup (tok)); |
169 | else |
178 | else |
170 | return err ("protocol error: unexpected NEW token"); |
179 | return err ("protocol error: unexpected NEW token"); |
171 | } |
180 | } |
172 | |
181 | |
173 | envv->push_back (0); |
182 | envv->push_back (0); |
174 | |
183 | |
175 | { |
184 | { |
176 | char **old_environ = environ; |
|
|
177 | environ = envv->begin (); |
|
|
178 | |
|
|
179 | rxvt_term *term = new rxvt_term; |
185 | rxvt_term *term = new rxvt_term; |
180 | |
186 | |
181 | term->log_hook = &log_cb; |
187 | term->log_hook = &log_cb; |
182 | term->argv = argv; |
188 | term->getfd_hook = &getfd_cb; |
183 | term->envv = envv; |
|
|
184 | |
189 | |
185 | bool success; |
190 | bool success; |
186 | |
191 | |
187 | try |
192 | try |
188 | { |
193 | { |
189 | success = term->init (argv->size (), argv->begin ()); |
194 | success = term->init (argv, envv); |
190 | } |
195 | } |
191 | catch (const class rxvt_failure_exception &e) |
196 | catch (const class rxvt_failure_exception &e) |
192 | { |
197 | { |
193 | success = false; |
198 | success = false; |
194 | } |
199 | } |
195 | |
200 | |
196 | term->log_hook = 0; |
201 | term->log_hook = 0; |
197 | |
202 | |
198 | environ = old_environ; |
203 | chdir ("/"); |
199 | |
204 | |
200 | if (!success) |
205 | if (!success) |
201 | term->destroy (); |
206 | term->destroy (); |
202 | |
207 | |
203 | send ("END"); send (success ? 1 : 0); |
208 | send ("END"); send (success ? 1 : 0); |
… | |
… | |
208 | } |
213 | } |
209 | else |
214 | else |
210 | return err (); |
215 | return err (); |
211 | } |
216 | } |
212 | |
217 | |
|
|
218 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
219 | |
213 | int |
220 | int |
214 | main (int argc, const char *const *argv) |
221 | main (int argc, const char *const *argv) |
215 | { |
222 | { |
216 | rxvt_init (); |
223 | rxvt_init (); |
217 | |
224 | |
|
|
225 | for (int i = 1; i < argc; i++) |
|
|
226 | { |
|
|
227 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
|
|
228 | opt_fork = 1; |
|
|
229 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
|
|
230 | opt_opendisplay = 1; |
|
|
231 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
|
|
232 | opt_quiet = 1; |
|
|
233 | else |
|
|
234 | { |
|
|
235 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
|
|
236 | return EXIT_FAILURE; |
|
|
237 | } |
|
|
238 | } |
|
|
239 | |
|
|
240 | chdir ("/"); |
|
|
241 | |
|
|
242 | if (opt_opendisplay) |
|
|
243 | displays.get (getenv ("DISPLAY")); // open display and never release it |
|
|
244 | |
218 | char *sockname = rxvt_connection::unix_sockname (); |
245 | char *sockname = rxvt_connection::unix_sockname (); |
219 | unix_listener l (sockname); |
246 | unix_listener l (sockname); |
|
|
247 | |
|
|
248 | if (!opt_quiet) |
|
|
249 | { |
220 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
250 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
221 | fflush (stdout); |
251 | fflush (stdout); |
|
|
252 | } |
|
|
253 | |
222 | free (sockname); |
254 | free (sockname); |
223 | |
255 | |
|
|
256 | if (opt_fork) |
|
|
257 | { |
|
|
258 | pid_t pid = fork (); |
|
|
259 | |
|
|
260 | if (pid < 0) |
|
|
261 | { |
|
|
262 | rxvt_log ("unable to fork daemon, aborting.\n"); |
|
|
263 | return EXIT_FAILURE; |
|
|
264 | } |
|
|
265 | else if (pid > 0) |
|
|
266 | _exit (EXIT_SUCCESS); |
|
|
267 | } |
|
|
268 | |
224 | io_manager::loop (); |
269 | io_manager::loop (); |
225 | |
270 | |
226 | return EXIT_SUCCESS; |
271 | return EXIT_SUCCESS; |
227 | } |
272 | } |
228 | |
273 | |