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" |
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), |
51 | : read_ev (this, &server::read_cb), |
55 | log_cb (this, &server::log_msg), |
52 | log_cb (this, &server::log_msg), |
56 | getfd_cb (this, &server::getfd) |
53 | getfd_cb (this, &server::getfd) |
57 | { |
54 | { |
58 | this->fd = fd; |
55 | this->fd = fd; |
|
|
56 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
57 | fcntl (fd, F_SETFL, 0); |
59 | read_ev.start (fd, EVENT_READ); |
58 | read_ev.start (fd, ev::READ); |
60 | } |
59 | } |
61 | |
60 | |
62 | void err (const char *format = 0, ...); |
61 | void err (const char *format = 0, ...); |
63 | }; |
62 | }; |
64 | |
63 | |
65 | struct unix_listener { |
64 | struct unix_listener { |
66 | int fd; |
65 | int fd; |
67 | |
66 | |
68 | void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
67 | void accept_cb (ev::io &w, int revents); ev::io accept_ev; |
69 | |
68 | |
70 | unix_listener (const char *sockname); |
69 | unix_listener (const char *sockname); |
71 | }; |
70 | }; |
72 | |
71 | |
73 | unix_listener::unix_listener (const char *sockname) |
72 | unix_listener::unix_listener (const char *sockname) |
74 | : accept_ev (this, &unix_listener::accept_cb) |
73 | : accept_ev (this, &unix_listener::accept_cb) |
75 | { |
74 | { |
|
|
75 | sockaddr_un sa; |
|
|
76 | |
|
|
77 | if (strlen (sockname) >= sizeof(sa.sun_path)) |
|
|
78 | { |
|
|
79 | fputs ("socket name too long, aborting.\n", stderr); |
|
|
80 | exit (EXIT_FAILURE); |
|
|
81 | } |
|
|
82 | |
76 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
83 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
77 | { |
84 | { |
78 | perror ("unable to create listening socket"); |
85 | perror ("unable to create listening socket"); |
79 | exit (EXIT_FAILURE); |
86 | exit (EXIT_FAILURE); |
80 | } |
87 | } |
81 | |
88 | |
82 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
89 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
83 | |
90 | fcntl (fd, F_SETFL, O_NONBLOCK); |
84 | sockaddr_un sa; |
|
|
85 | |
91 | |
86 | sa.sun_family = AF_UNIX; |
92 | sa.sun_family = AF_UNIX; |
87 | strcpy (sa.sun_path, sockname); |
93 | strcpy (sa.sun_path, sockname); |
88 | |
94 | |
89 | unlink (rxvt_connection::unix_sockname ()); |
95 | unlink (rxvt_connection::unix_sockname ()); |
… | |
… | |
102 | { |
108 | { |
103 | perror ("unable to listen on socket"); |
109 | perror ("unable to listen on socket"); |
104 | exit (EXIT_FAILURE); |
110 | exit (EXIT_FAILURE); |
105 | } |
111 | } |
106 | |
112 | |
107 | accept_ev.start (fd, EVENT_READ); |
113 | accept_ev.start (fd, ev::READ); |
108 | } |
114 | } |
109 | |
115 | |
110 | void unix_listener::accept_cb (io_watcher &w, short revents) |
116 | void unix_listener::accept_cb (ev::io &w, int revents) |
111 | { |
117 | { |
112 | int fd2 = accept (fd, 0, 0); |
118 | int fd2 = accept (fd, 0, 0); |
113 | |
119 | |
114 | if (fd2 >= 0) |
120 | if (fd2 >= 0) |
115 | { |
|
|
116 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
|
|
117 | new server (fd2); |
121 | new server (fd2); |
118 | } |
|
|
119 | } |
122 | } |
120 | |
123 | |
121 | int server::getfd (int remote_fd) |
124 | int server::getfd (int remote_fd) |
122 | { |
125 | { |
123 | send ("GETFD"); |
126 | send ("GETFD"); |
124 | send (remote_fd); |
127 | send (remote_fd); |
125 | return rxvt_recv_fd (fd); |
128 | return ptytty::recv_fd (fd); |
126 | } |
129 | } |
127 | |
130 | |
128 | void server::log_msg (const char *msg) |
131 | void server::log_msg (const char *msg) |
129 | { |
132 | { |
130 | send ("MSG"), send (msg); |
133 | send ("MSG"), send (msg); |
… | |
… | |
139 | va_list ap; |
142 | va_list ap; |
140 | va_start (ap, format); |
143 | va_start (ap, format); |
141 | vsnprintf (err, 1024, format, ap); |
144 | vsnprintf (err, 1024, format, ap); |
142 | va_end (ap); |
145 | va_end (ap); |
143 | |
146 | |
144 | send ("MSG"), send (err); |
147 | log_msg (err); |
145 | } |
148 | } |
146 | |
149 | |
147 | send ("END", 0); |
150 | send ("END"), send (0); |
148 | close (fd); |
151 | close (fd); |
149 | delete this; |
152 | delete this; |
150 | } |
153 | } |
151 | |
154 | |
152 | void server::read_cb (io_watcher &w, short revents) |
155 | void server::read_cb (ev::io &w, int revents) |
153 | { |
156 | { |
154 | auto_str tok; |
157 | auto_str tok; |
155 | |
158 | |
156 | if (recv (tok)) |
159 | if (recv (tok)) |
157 | { |
160 | { |
158 | if (!strcmp (tok, "NEW")) |
161 | if (!strcmp (tok, "NEW")) |
159 | { |
162 | { |
160 | stringvec *argv = new stringvec; |
163 | stringvec *argv = new stringvec; |
161 | stringvec *envv = new stringvec; |
164 | stringvec *envv = new stringvec; |
162 | |
165 | |
163 | for (;;) |
166 | for (;;) |
164 | { |
167 | { |
165 | if (!recv (tok)) |
168 | if (!recv (tok)) |
166 | return err (); |
169 | return err (); |
167 | |
170 | |
168 | if (!strcmp (tok, "END")) |
171 | if (!strcmp (tok, "END")) |
169 | break; |
172 | break; |
170 | else if (!strcmp (tok, "ENV") && recv (tok)) |
173 | else if (!strcmp (tok, "ENV") && recv (tok)) |
171 | envv->push_back (tok.get ()); |
174 | envv->push_back (strdup (tok)); |
172 | else if (!strcmp (tok, "CWD") && recv (tok)) |
175 | else if (!strcmp (tok, "CWD") && recv (tok)) |
173 | { |
176 | { |
174 | if (chdir (tok)) |
177 | if (chdir (tok)) |
|
|
178 | { |
|
|
179 | delete envv; |
|
|
180 | delete argv; |
175 | err ("unable to change to working directory to '%s': %s", |
181 | return err ("unable to change to working directory to '%s', aborting: %s.\n", |
176 | (char *)tok, strerror (errno)); |
182 | (char *)tok, strerror (errno)); |
|
|
183 | } |
177 | } |
184 | } |
178 | else if (!strcmp (tok, "ARG") && recv (tok)) |
185 | else if (!strcmp (tok, "ARG") && recv (tok)) |
179 | argv->push_back (tok.get ()); |
186 | argv->push_back (strdup (tok)); |
180 | else |
187 | else |
181 | return err ("protocol error: unexpected NEW token"); |
188 | return err ("protocol error: unexpected NEW token.\n"); |
182 | } |
189 | } |
183 | |
190 | |
184 | envv->push_back (0); |
191 | envv->push_back (0); |
185 | |
192 | |
186 | { |
193 | { |
187 | rxvt_term *term = new rxvt_term; |
194 | rxvt_term *term = new rxvt_term; |
188 | |
195 | |
189 | term->log_hook = &log_cb; |
196 | term->log_hook = &log_cb; |
190 | term->getfd_hook = &getfd_cb; |
197 | term->getfd_hook = &getfd_cb; |
191 | term->argv = argv; |
|
|
192 | term->envv = envv; |
|
|
193 | |
198 | |
194 | bool success; |
199 | bool success; |
195 | |
200 | |
196 | try |
201 | try |
197 | { |
202 | { |
198 | success = term->init (argv->size (), argv->begin ()); |
203 | success = term->init (argv, envv); |
199 | } |
204 | } |
200 | catch (const class rxvt_failure_exception &e) |
205 | catch (const class rxvt_failure_exception &e) |
201 | { |
206 | { |
202 | success = false; |
207 | success = false; |
203 | } |
208 | } |
… | |
… | |
211 | |
216 | |
212 | send ("END"); send (success ? 1 : 0); |
217 | send ("END"); send (success ? 1 : 0); |
213 | } |
218 | } |
214 | } |
219 | } |
215 | else |
220 | else |
216 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
221 | return err ("protocol error: request '%s' unsupported.\n", (char *)tok); |
217 | } |
222 | } |
218 | else |
223 | else |
219 | return err (); |
224 | return err (); |
220 | } |
225 | } |
221 | |
226 | |
222 | int opt_fork, opt_opendisplay, opt_quiet; |
227 | int opt_fork, opt_opendisplay, opt_quiet; |
223 | |
228 | |
224 | int |
229 | int |
225 | main (int argc, const char *const *argv) |
230 | main (int argc, const char *const *argv) |
226 | { |
231 | { |
|
|
232 | rxvt_init (); |
|
|
233 | |
227 | for (int i = 1; i < argc; i++) |
234 | for (int i = 1; i < argc; i++) |
228 | { |
235 | { |
229 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
236 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
230 | opt_fork = 1; |
237 | opt_fork = 1; |
231 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
238 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
… | |
… | |
236 | { |
243 | { |
237 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
244 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
238 | return EXIT_FAILURE; |
245 | return EXIT_FAILURE; |
239 | } |
246 | } |
240 | } |
247 | } |
241 | |
|
|
242 | rxvt_init (); |
|
|
243 | |
248 | |
244 | chdir ("/"); |
249 | chdir ("/"); |
245 | |
250 | |
246 | if (opt_opendisplay) |
251 | if (opt_opendisplay) |
247 | displays.get (getenv ("DISPLAY")); // open display and never release it |
252 | displays.get (getenv ("DISPLAY")); // open display and never release it |
… | |
… | |
268 | } |
273 | } |
269 | else if (pid > 0) |
274 | else if (pid > 0) |
270 | _exit (EXIT_SUCCESS); |
275 | _exit (EXIT_SUCCESS); |
271 | } |
276 | } |
272 | |
277 | |
273 | io_manager::loop (); |
278 | ev::ev_loop (0); |
274 | |
279 | |
275 | return EXIT_SUCCESS; |
280 | return EXIT_SUCCESS; |
276 | } |
281 | } |
277 | |
282 | |