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-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 "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" |
|
|
41 | #include "iom.h" |
44 | |
42 | |
45 | struct server : rxvt_connection { |
43 | struct server : rxvt_connection { |
46 | log_callback log_cb; |
44 | log_callback log_cb; |
47 | getfd_callback getfd_cb; |
45 | getfd_callback getfd_cb; |
48 | |
46 | |
… | |
… | |
71 | }; |
69 | }; |
72 | |
70 | |
73 | unix_listener::unix_listener (const char *sockname) |
71 | unix_listener::unix_listener (const char *sockname) |
74 | : accept_ev (this, &unix_listener::accept_cb) |
72 | : accept_ev (this, &unix_listener::accept_cb) |
75 | { |
73 | { |
|
|
74 | sockaddr_un sa; |
|
|
75 | |
|
|
76 | if (strlen (sockname) >= sizeof(sa.sun_path)) |
|
|
77 | { |
|
|
78 | fputs ("socket name too long, aborting.\n", stderr); |
|
|
79 | exit (EXIT_FAILURE); |
|
|
80 | } |
|
|
81 | |
76 | if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) |
82 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
77 | { |
83 | { |
78 | perror ("unable to create listening socket"); |
84 | perror ("unable to create listening socket"); |
79 | exit (EXIT_FAILURE); |
85 | exit (EXIT_FAILURE); |
80 | } |
86 | } |
81 | |
87 | |
82 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
88 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
83 | |
|
|
84 | sockaddr_un sa; |
|
|
85 | |
89 | |
86 | sa.sun_family = AF_UNIX; |
90 | sa.sun_family = AF_UNIX; |
87 | strcpy (sa.sun_path, sockname); |
91 | strcpy (sa.sun_path, sockname); |
88 | |
92 | |
89 | unlink (rxvt_connection::unix_sockname ()); |
93 | unlink (rxvt_connection::unix_sockname ()); |
… | |
… | |
118 | } |
122 | } |
119 | } |
123 | } |
120 | |
124 | |
121 | int server::getfd (int remote_fd) |
125 | int server::getfd (int remote_fd) |
122 | { |
126 | { |
123 | #if ENABLE_FRILLS && HAVE_UNIX_FDPASS |
|
|
124 | send ("GETFD"); |
127 | send ("GETFD"); |
125 | send (remote_fd); |
128 | send (remote_fd); |
126 | return rxvt_recv_fd (fd); |
129 | return ptytty::recv_fd (fd); |
127 | #else |
|
|
128 | return -1; |
|
|
129 | #endif |
|
|
130 | } |
130 | } |
131 | |
131 | |
132 | void server::log_msg (const char *msg) |
132 | void server::log_msg (const char *msg) |
133 | { |
133 | { |
134 | send ("MSG"), send (msg); |
134 | send ("MSG"), send (msg); |
… | |
… | |
143 | va_list ap; |
143 | va_list ap; |
144 | va_start (ap, format); |
144 | va_start (ap, format); |
145 | vsnprintf (err, 1024, format, ap); |
145 | vsnprintf (err, 1024, format, ap); |
146 | va_end (ap); |
146 | va_end (ap); |
147 | |
147 | |
148 | send ("MSG"), send (err); |
148 | log_msg (err); |
149 | } |
149 | } |
150 | |
150 | |
151 | send ("END", 0); |
151 | send ("END"), send (0); |
152 | close (fd); |
152 | close (fd); |
153 | delete this; |
153 | delete this; |
154 | } |
154 | } |
155 | |
155 | |
156 | void server::read_cb (io_watcher &w, short revents) |
156 | void server::read_cb (io_watcher &w, short revents) |
… | |
… | |
161 | { |
161 | { |
162 | if (!strcmp (tok, "NEW")) |
162 | if (!strcmp (tok, "NEW")) |
163 | { |
163 | { |
164 | stringvec *argv = new stringvec; |
164 | stringvec *argv = new stringvec; |
165 | stringvec *envv = new stringvec; |
165 | stringvec *envv = new stringvec; |
166 | |
166 | |
167 | for (;;) |
167 | for (;;) |
168 | { |
168 | { |
169 | if (!recv (tok)) |
169 | if (!recv (tok)) |
170 | return err (); |
170 | return err (); |
171 | |
171 | |
172 | if (!strcmp (tok, "END")) |
172 | if (!strcmp (tok, "END")) |
173 | break; |
173 | break; |
174 | else if (!strcmp (tok, "ENV") && recv (tok)) |
174 | else if (!strcmp (tok, "ENV") && recv (tok)) |
175 | envv->push_back (tok.get ()); |
175 | envv->push_back (strdup (tok)); |
176 | else if (!strcmp (tok, "CWD") && recv (tok)) |
176 | else if (!strcmp (tok, "CWD") && recv (tok)) |
177 | { |
177 | { |
178 | if (chdir (tok)) |
178 | if (chdir (tok)) |
|
|
179 | { |
|
|
180 | delete envv; |
|
|
181 | delete argv; |
179 | err ("unable to change to working directory to '%s': %s", |
182 | return err ("unable to change to working directory to '%s', aborting: %s.\n", |
180 | (char *)tok, strerror (errno)); |
183 | (char *)tok, strerror (errno)); |
|
|
184 | } |
181 | } |
185 | } |
182 | else if (!strcmp (tok, "ARG") && recv (tok)) |
186 | else if (!strcmp (tok, "ARG") && recv (tok)) |
183 | argv->push_back (tok.get ()); |
187 | argv->push_back (strdup (tok)); |
184 | else |
188 | else |
185 | return err ("protocol error: unexpected NEW token"); |
189 | return err ("protocol error: unexpected NEW token.\n"); |
186 | } |
190 | } |
187 | |
191 | |
188 | envv->push_back (0); |
192 | envv->push_back (0); |
189 | |
193 | |
190 | { |
194 | { |
191 | char **old_environ = environ; |
|
|
192 | environ = envv->begin (); |
|
|
193 | |
|
|
194 | rxvt_term *term = new rxvt_term; |
195 | rxvt_term *term = new rxvt_term; |
195 | |
196 | |
196 | term->log_hook = &log_cb; |
197 | term->log_hook = &log_cb; |
197 | term->getfd_hook = &getfd_cb; |
198 | term->getfd_hook = &getfd_cb; |
198 | term->argv = argv; |
|
|
199 | term->envv = envv; |
|
|
200 | |
199 | |
201 | bool success; |
200 | bool success; |
202 | |
201 | |
203 | try |
202 | try |
204 | { |
203 | { |
205 | success = term->init (argv->size (), argv->begin ()); |
204 | success = term->init (argv, envv); |
206 | } |
205 | } |
207 | catch (const class rxvt_failure_exception &e) |
206 | catch (const class rxvt_failure_exception &e) |
208 | { |
207 | { |
209 | success = false; |
208 | success = false; |
210 | } |
209 | } |
211 | |
210 | |
212 | term->log_hook = 0; |
211 | term->log_hook = 0; |
213 | |
212 | |
214 | environ = old_environ; |
|
|
215 | chdir ("/"); |
213 | chdir ("/"); |
216 | |
214 | |
217 | if (!success) |
215 | if (!success) |
218 | term->destroy (); |
216 | term->destroy (); |
219 | |
217 | |
220 | send ("END"); send (success ? 1 : 0); |
218 | send ("END"); send (success ? 1 : 0); |
221 | } |
219 | } |
222 | } |
220 | } |
223 | else |
221 | else |
224 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
222 | return err ("protocol error: request '%s' unsupported.\n", (char *)tok); |
225 | } |
223 | } |
226 | else |
224 | else |
227 | return err (); |
225 | return err (); |
228 | } |
226 | } |
229 | |
227 | |
230 | int opt_fork, opt_opendisplay, opt_quiet; |
228 | int opt_fork, opt_opendisplay, opt_quiet; |
231 | |
229 | |
232 | int |
230 | int |
233 | main (int argc, const char *const *argv) |
231 | main (int argc, const char *const *argv) |
234 | { |
232 | { |
|
|
233 | rxvt_init (); |
|
|
234 | |
235 | for (int i = 1; i < argc; i++) |
235 | for (int i = 1; i < argc; i++) |
236 | { |
236 | { |
237 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
237 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
238 | opt_fork = 1; |
238 | opt_fork = 1; |
239 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
239 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
… | |
… | |
245 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
245 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
246 | return EXIT_FAILURE; |
246 | return EXIT_FAILURE; |
247 | } |
247 | } |
248 | } |
248 | } |
249 | |
249 | |
250 | rxvt_init (); |
|
|
251 | |
|
|
252 | chdir ("/"); |
250 | chdir ("/"); |
253 | |
251 | |
254 | if (opt_opendisplay) |
252 | if (opt_opendisplay) |
255 | displays.get (getenv ("DISPLAY")); // open display and never release it |
253 | displays.get (getenv ("DISPLAY")); // open display and never release it |
256 | |
254 | |