|
|
1 | /*--------------------------------*-C-*---------------------------------* |
|
|
2 | * File: rxvtd.C |
|
|
3 | *----------------------------------------------------------------------* |
|
|
4 | * |
|
|
5 | * All portions of code are copyright by their respective author/s. |
|
|
6 | * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> |
|
|
7 | * |
|
|
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 |
|
|
10 | * the Free Software Foundation; either version 2 of the License, or |
|
|
11 | * (at your option) any later version. |
|
|
12 | * |
|
|
13 | * This program is distributed in the hope that it will be useful, |
|
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 | * GNU General Public License for more details. |
|
|
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program; if not, write to the Free Software |
|
|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
21 | *----------------------------------------------------------------------*/ |
|
|
22 | |
1 | #include "../config.h" |
23 | #include "../config.h" |
2 | #include "rxvt.h" |
|
|
3 | #include "rxvtdaemon.h" |
|
|
4 | #include "iom.h" |
|
|
5 | |
|
|
6 | #include <cstdio> |
24 | #include <cstdio> |
7 | #include <cstdlib> |
25 | #include <cstdlib> |
8 | #include <cstdarg> |
26 | #include <cstdarg> |
9 | #include <cstring> |
27 | #include <cstring> |
10 | |
28 | |
… | |
… | |
15 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
16 | #include <sys/un.h> |
34 | #include <sys/un.h> |
17 | |
35 | |
18 | #include <cerrno> |
36 | #include <cerrno> |
19 | |
37 | |
20 | extern char **environ; |
38 | #include "rxvt.h" |
|
|
39 | #include "rxvtdaemon.h" |
|
|
40 | #include "libptytty.h" |
|
|
41 | #include "iom.h" |
21 | |
42 | |
22 | struct server : rxvt_connection { |
43 | struct server : rxvt_connection { |
23 | log_callback log_cb; |
44 | log_callback log_cb; |
|
|
45 | getfd_callback getfd_cb; |
24 | |
46 | |
25 | 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; |
26 | void log_msg (const char *msg); |
48 | void log_msg (const char *msg); |
|
|
49 | int getfd (int remote_fd); |
27 | |
50 | |
28 | server (int fd) |
51 | server (int fd) |
29 | : read_ev (this, &server::read_cb), |
52 | : read_ev (this, &server::read_cb), |
30 | log_cb (this, &server::log_msg) |
53 | log_cb (this, &server::log_msg), |
|
|
54 | getfd_cb (this, &server::getfd) |
31 | { |
55 | { |
32 | this->fd = fd; |
56 | this->fd = fd; |
33 | read_ev.start (fd, EVENT_READ); |
57 | read_ev.start (fd, EVENT_READ); |
34 | } |
58 | } |
35 | |
59 | |
… | |
… | |
45 | }; |
69 | }; |
46 | |
70 | |
47 | unix_listener::unix_listener (const char *sockname) |
71 | unix_listener::unix_listener (const char *sockname) |
48 | : accept_ev (this, &unix_listener::accept_cb) |
72 | : accept_ev (this, &unix_listener::accept_cb) |
49 | { |
73 | { |
50 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
74 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
51 | { |
75 | { |
52 | perror ("unable to create listening socket"); |
76 | perror ("unable to create listening socket"); |
53 | exit (EXIT_FAILURE); |
77 | exit (EXIT_FAILURE); |
54 | } |
78 | } |
55 | |
79 | |
… | |
… | |
88 | if (fd2 >= 0) |
112 | if (fd2 >= 0) |
89 | { |
113 | { |
90 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
114 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
91 | new server (fd2); |
115 | new server (fd2); |
92 | } |
116 | } |
|
|
117 | } |
|
|
118 | |
|
|
119 | int server::getfd (int remote_fd) |
|
|
120 | { |
|
|
121 | send ("GETFD"); |
|
|
122 | send (remote_fd); |
|
|
123 | return ptytty::recv_fd (fd); |
93 | } |
124 | } |
94 | |
125 | |
95 | void server::log_msg (const char *msg) |
126 | void server::log_msg (const char *msg) |
96 | { |
127 | { |
97 | send ("MSG"), send (msg); |
128 | send ("MSG"), send (msg); |
… | |
… | |
133 | return err (); |
164 | return err (); |
134 | |
165 | |
135 | if (!strcmp (tok, "END")) |
166 | if (!strcmp (tok, "END")) |
136 | break; |
167 | break; |
137 | else if (!strcmp (tok, "ENV") && recv (tok)) |
168 | else if (!strcmp (tok, "ENV") && recv (tok)) |
138 | envv->push_back (tok.get ()); |
169 | envv->push_back (strdup (tok)); |
139 | else if (!strcmp (tok, "CWD") && recv (tok)) |
170 | else if (!strcmp (tok, "CWD") && recv (tok)) |
140 | { |
171 | { |
141 | if (chdir (tok)) |
172 | if (chdir (tok)) |
142 | err ("unable to change to working directory to '%s': %s", |
173 | err ("unable to change to working directory to '%s': %s", |
143 | (char *)tok, strerror (errno)); |
174 | (char *)tok, strerror (errno)); |
144 | } |
175 | } |
145 | else if (!strcmp (tok, "ARG") && recv (tok)) |
176 | else if (!strcmp (tok, "ARG") && recv (tok)) |
146 | argv->push_back (tok.get ()); |
177 | argv->push_back (strdup (tok)); |
147 | else |
178 | else |
148 | return err ("protocol error: unexpected NEW token"); |
179 | return err ("protocol error: unexpected NEW token"); |
149 | } |
180 | } |
150 | |
181 | |
151 | envv->push_back (0); |
182 | envv->push_back (0); |
152 | |
183 | |
153 | { |
184 | { |
154 | char **old_environ = environ; |
|
|
155 | environ = envv->begin (); |
|
|
156 | |
|
|
157 | rxvt_term *term = new rxvt_term; |
185 | rxvt_term *term = new rxvt_term; |
158 | |
186 | |
159 | term->log_hook = &log_cb; |
187 | term->log_hook = &log_cb; |
160 | term->argv = argv; |
188 | term->getfd_hook = &getfd_cb; |
161 | term->envv = envv; |
|
|
162 | |
189 | |
163 | bool success; |
190 | bool success; |
164 | |
191 | |
165 | try |
192 | try |
166 | { |
193 | { |
167 | success = term->init (argv->size (), argv->begin ()); |
194 | success = term->init (argv, envv); |
168 | } |
195 | } |
169 | catch (const class rxvt_failure_exception &e) |
196 | catch (const class rxvt_failure_exception &e) |
170 | { |
197 | { |
171 | success = false; |
198 | success = false; |
172 | } |
199 | } |
173 | |
200 | |
174 | environ = old_environ; |
201 | term->log_hook = 0; |
|
|
202 | |
|
|
203 | chdir ("/"); |
175 | |
204 | |
176 | if (!success) |
205 | if (!success) |
177 | term->destroy (); |
206 | term->destroy (); |
178 | |
207 | |
179 | send ("END"); send (success ? 1 : 0); |
208 | send ("END"); send (success ? 1 : 0); |
… | |
… | |
184 | } |
213 | } |
185 | else |
214 | else |
186 | return err (); |
215 | return err (); |
187 | } |
216 | } |
188 | |
217 | |
|
|
218 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
219 | |
189 | int |
220 | int |
190 | main (int argc, const char *const *argv) |
221 | main (int argc, const char *const *argv) |
191 | { |
222 | { |
192 | rxvt_init_signals (); |
223 | rxvt_init (); |
|
|
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 |
193 | |
244 | |
194 | char *sockname = rxvt_connection::unix_sockname (); |
245 | char *sockname = rxvt_connection::unix_sockname (); |
195 | unix_listener l (sockname); |
246 | unix_listener l (sockname); |
|
|
247 | |
|
|
248 | if (!opt_quiet) |
|
|
249 | { |
196 | printf ("rxvtd listening on %s.\n", sockname); |
250 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
|
|
251 | fflush (stdout); |
|
|
252 | } |
|
|
253 | |
197 | free (sockname); |
254 | free (sockname); |
198 | |
255 | |
199 | iom.loop (); |
256 | if (opt_fork) |
|
|
257 | { |
|
|
258 | pid_t pid = fork (); |
200 | |
259 | |
201 | #if 0 |
260 | if (pid < 0) |
202 | if (rxvt_init (argc, argv) == NULL) |
261 | { |
|
|
262 | rxvt_log ("unable to fork daemon, aborting.\n"); |
203 | return EXIT_FAILURE; |
263 | return EXIT_FAILURE; |
|
|
264 | } |
|
|
265 | else if (pid > 0) |
|
|
266 | _exit (EXIT_SUCCESS); |
|
|
267 | } |
204 | |
268 | |
205 | dR; |
269 | io_manager::loop (); |
206 | rxvt_main_loop (aR); /* main processing loop */ |
270 | |
207 | #endif |
|
|
208 | return EXIT_SUCCESS; |
271 | return EXIT_SUCCESS; |
209 | } |
272 | } |
210 | |
273 | |