1 | #include "rxvtlib.h" |
1 | /*----------------------------------------------------------------------* |
2 | #include "rxvtdaemon.h" |
2 | * File: rxvtd.C |
3 | #include "iom.h" |
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 | *----------------------------------------------------------------------*/ |
4 | |
22 | |
|
|
23 | #include "../config.h" |
5 | #include <cstdio> |
24 | #include <cstdio> |
6 | #include <cstdlib> |
25 | #include <cstdlib> |
7 | #include <cstdarg> |
26 | #include <cstdarg> |
8 | #include <cstring> |
27 | #include <cstring> |
9 | |
28 | |
10 | #include <unistd.h> |
29 | #include <unistd.h> |
|
|
30 | #include <fcntl.h> |
11 | #include <sys/types.h> |
31 | #include <sys/types.h> |
12 | #include <sys/stat.h> |
32 | #include <sys/stat.h> |
13 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
14 | #include <sys/un.h> |
34 | #include <sys/un.h> |
15 | |
35 | |
16 | #include <cerrno> |
36 | #include <cerrno> |
17 | |
37 | |
18 | extern char **environ; |
38 | #include "rxvt.h" |
|
|
39 | #include "rxvtdaemon.h" |
|
|
40 | #include "libptytty.h" |
19 | |
41 | |
20 | struct server : rxvt_connection { |
42 | struct server : rxvt_connection { |
|
|
43 | log_callback log_cb; |
|
|
44 | getfd_callback getfd_cb; |
|
|
45 | |
21 | 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; |
|
|
47 | void log_msg (const char *msg); |
|
|
48 | int getfd (int remote_fd); |
22 | |
49 | |
23 | server (int fd) |
50 | server (int fd) |
24 | : read_ev (this, &server::read_cb) |
51 | : read_ev (this, &server::read_cb), |
|
|
52 | log_cb (this, &server::log_msg), |
|
|
53 | getfd_cb (this, &server::getfd) |
25 | { |
54 | { |
26 | this->fd = fd; |
55 | this->fd = fd; |
|
|
56 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
57 | fcntl (fd, F_SETFL, 0); |
27 | read_ev.start (fd, EVENT_READ); |
58 | read_ev.start (fd, ev::READ); |
28 | } |
59 | } |
29 | |
60 | |
30 | void err (const char *format = 0, ...); |
61 | void err (const char *format = 0, ...); |
31 | }; |
62 | }; |
32 | |
63 | |
33 | struct unix_listener { |
64 | struct unix_listener { |
34 | int fd; |
65 | int fd; |
35 | |
66 | |
36 | 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; |
37 | |
68 | |
38 | unix_listener (const char *sockname); |
69 | unix_listener (const char *sockname); |
39 | }; |
70 | }; |
40 | |
71 | |
41 | unix_listener::unix_listener (const char *sockname) |
72 | unix_listener::unix_listener (const char *sockname) |
42 | : accept_ev (this, &unix_listener::accept_cb) |
73 | : accept_ev (this, &unix_listener::accept_cb) |
43 | { |
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 | |
44 | if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
83 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
45 | { |
84 | { |
46 | perror ("unable to create listening socket"); |
85 | perror ("unable to create listening socket"); |
47 | exit (EXIT_FAILURE); |
86 | exit (EXIT_FAILURE); |
48 | } |
87 | } |
49 | |
88 | |
50 | sockaddr_un sa; |
89 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
90 | fcntl (fd, F_SETFL, O_NONBLOCK); |
51 | |
91 | |
52 | sa.sun_family = AF_UNIX; |
92 | sa.sun_family = AF_UNIX; |
53 | strcpy (sa.sun_path, sockname); |
93 | strcpy (sa.sun_path, sockname); |
54 | |
94 | |
55 | unlink (rxvt_connection::unix_sockname ()); |
95 | unlink (rxvt_connection::unix_sockname ()); |
… | |
… | |
68 | { |
108 | { |
69 | perror ("unable to listen on socket"); |
109 | perror ("unable to listen on socket"); |
70 | exit (EXIT_FAILURE); |
110 | exit (EXIT_FAILURE); |
71 | } |
111 | } |
72 | |
112 | |
73 | accept_ev.start (fd, EVENT_READ); |
113 | accept_ev.start (fd, ev::READ); |
74 | } |
114 | } |
75 | |
115 | |
76 | void unix_listener::accept_cb (io_watcher &w, short revents) |
116 | void unix_listener::accept_cb (ev::io &w, int revents) |
77 | { |
117 | { |
78 | int fd2 = accept (fd, 0, 0); |
118 | int fd2 = accept (fd, 0, 0); |
79 | |
119 | |
80 | if (fd2 >= 0) |
120 | if (fd2 >= 0) |
81 | new server (fd2); |
121 | new server (fd2); |
|
|
122 | } |
|
|
123 | |
|
|
124 | int server::getfd (int remote_fd) |
|
|
125 | { |
|
|
126 | send ("GETFD"); |
|
|
127 | send (remote_fd); |
|
|
128 | return ptytty::recv_fd (fd); |
|
|
129 | } |
|
|
130 | |
|
|
131 | void server::log_msg (const char *msg) |
|
|
132 | { |
|
|
133 | send ("MSG"), send (msg); |
82 | } |
134 | } |
83 | |
135 | |
84 | void server::err (const char *format, ...) |
136 | void server::err (const char *format, ...) |
85 | { |
137 | { |
86 | if (format) |
138 | if (format) |
… | |
… | |
90 | va_list ap; |
142 | va_list ap; |
91 | va_start (ap, format); |
143 | va_start (ap, format); |
92 | vsnprintf (err, 1024, format, ap); |
144 | vsnprintf (err, 1024, format, ap); |
93 | va_end (ap); |
145 | va_end (ap); |
94 | |
146 | |
|
|
147 | log_msg (err); |
|
|
148 | } |
|
|
149 | |
95 | send ("ERR"), send (err); |
150 | send ("END"), send (0); |
96 | } |
|
|
97 | |
|
|
98 | close (fd); |
151 | close (fd); |
99 | delete this; |
152 | delete this; |
100 | } |
153 | } |
101 | |
154 | |
102 | void server::read_cb (io_watcher &w, short revents) |
155 | void server::read_cb (ev::io &w, int revents) |
103 | { |
156 | { |
104 | auto_str tok; |
157 | auto_str tok; |
105 | |
158 | |
106 | if (recv (tok)) |
159 | if (recv (tok)) |
107 | { |
160 | { |
108 | if (!strcmp (tok, "NEW")) |
161 | if (!strcmp (tok, "NEW")) |
109 | { |
162 | { |
110 | stringvec argv; |
163 | stringvec *argv = new stringvec; |
111 | stringvec envv; |
164 | stringvec *envv = new stringvec; |
112 | |
165 | |
113 | for (;;) |
166 | for (;;) |
114 | { |
167 | { |
115 | if (!recv (tok)) |
168 | if (!recv (tok)) |
116 | return err (); |
169 | return err (); |
117 | |
170 | |
118 | if (!strcmp (tok, "END")) |
171 | if (!strcmp (tok, "END")) |
119 | break; |
172 | break; |
120 | else if (!strcmp (tok, "ENV") && recv (tok)) |
173 | else if (!strcmp (tok, "ENV") && recv (tok)) |
121 | envv.push_back (tok.get ()); |
174 | envv->push_back (strdup (tok)); |
122 | else if (!strcmp (tok, "CWD") && recv (tok)) |
175 | else if (!strcmp (tok, "CWD") && recv (tok)) |
123 | { |
176 | { |
124 | if (chdir (tok)) |
177 | if (chdir (tok)) |
|
|
178 | { |
|
|
179 | delete envv; |
|
|
180 | delete argv; |
125 | err ("unable to change to working directory to '%s': %s", |
181 | return err ("unable to change to working directory to '%s', aborting: %s.\n", |
126 | (char *)tok, strerror (errno)); |
182 | (char *)tok, strerror (errno)); |
|
|
183 | } |
127 | } |
184 | } |
128 | else if (!strcmp (tok, "ARG") && recv (tok)) |
185 | else if (!strcmp (tok, "ARG") && recv (tok)) |
129 | argv.push_back (tok.get ()); |
186 | argv->push_back (strdup (tok)); |
130 | else |
187 | else |
131 | return err ("protocol error: unexpected NEW token"); |
188 | return err ("protocol error: unexpected NEW token.\n"); |
132 | } |
189 | } |
133 | |
190 | |
134 | envv.push_back (0); |
191 | envv->push_back (0); |
135 | |
192 | |
136 | { |
193 | { |
137 | char **old_environ = environ; |
194 | rxvt_term *term = new rxvt_term; |
138 | environ = envv.begin (); |
|
|
139 | |
195 | |
140 | rxvt_init (argv.size (), argv.begin ()); |
196 | term->log_hook = &log_cb; |
|
|
197 | term->getfd_hook = &getfd_cb; |
141 | |
198 | |
142 | environ = old_environ; |
199 | bool success; |
143 | envv.clear (); // can't yet save the env 'cause rxvt modifies it :( |
200 | |
|
|
201 | try |
|
|
202 | { |
|
|
203 | success = term->init (argv, envv); |
|
|
204 | } |
|
|
205 | catch (const class rxvt_failure_exception &e) |
|
|
206 | { |
|
|
207 | success = false; |
|
|
208 | } |
|
|
209 | |
|
|
210 | term->log_hook = 0; |
|
|
211 | |
|
|
212 | chdir ("/"); |
|
|
213 | |
|
|
214 | if (!success) |
|
|
215 | term->destroy (); |
|
|
216 | |
|
|
217 | send ("END"); send (success ? 1 : 0); |
144 | } |
218 | } |
145 | } |
219 | } |
146 | else |
220 | else |
147 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
221 | return err ("protocol error: request '%s' unsupported.\n", (char *)tok); |
148 | } |
222 | } |
149 | else |
223 | else |
150 | return err (); |
224 | return err (); |
151 | } |
225 | } |
152 | |
226 | |
|
|
227 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
228 | |
153 | int |
229 | int |
154 | main(int argc, const char *const *argv) |
230 | main (int argc, const char *const *argv) |
155 | { |
231 | { |
156 | rxvt_init_signals (); |
232 | rxvt_init (); |
|
|
233 | |
|
|
234 | for (int i = 1; i < argc; i++) |
|
|
235 | { |
|
|
236 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
|
|
237 | opt_fork = 1; |
|
|
238 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
|
|
239 | opt_opendisplay = 1; |
|
|
240 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
|
|
241 | opt_quiet = 1; |
|
|
242 | else |
|
|
243 | { |
|
|
244 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
|
|
245 | return EXIT_FAILURE; |
|
|
246 | } |
|
|
247 | } |
|
|
248 | |
|
|
249 | chdir ("/"); |
|
|
250 | |
|
|
251 | if (opt_opendisplay) |
|
|
252 | displays.get (getenv ("DISPLAY")); // open display and never release it |
157 | |
253 | |
158 | char *sockname = rxvt_connection::unix_sockname (); |
254 | char *sockname = rxvt_connection::unix_sockname (); |
159 | unix_listener l (sockname); |
255 | unix_listener l (sockname); |
|
|
256 | |
|
|
257 | if (!opt_quiet) |
|
|
258 | { |
160 | printf ("rxvtd listening on %s.\n", sockname); |
259 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
|
|
260 | fflush (stdout); |
|
|
261 | } |
|
|
262 | |
161 | free (sockname); |
263 | free (sockname); |
162 | |
264 | |
163 | iom.loop (); |
265 | if (opt_fork) |
|
|
266 | { |
|
|
267 | pid_t pid = fork (); |
164 | |
268 | |
165 | #if 0 |
269 | if (pid < 0) |
166 | if (rxvt_init(argc, argv) == NULL) |
270 | { |
|
|
271 | rxvt_log ("unable to fork daemon, aborting.\n"); |
167 | return EXIT_FAILURE; |
272 | return EXIT_FAILURE; |
|
|
273 | } |
|
|
274 | else if (pid > 0) |
|
|
275 | _exit (EXIT_SUCCESS); |
|
|
276 | } |
168 | |
277 | |
169 | dR; |
278 | ev::ev_loop (0); |
170 | rxvt_main_loop(aR); /* main processing loop */ |
279 | |
171 | #endif |
|
|
172 | return EXIT_SUCCESS; |
280 | return EXIT_SUCCESS; |
173 | } |
281 | } |
|
|
282 | |