1 | /*----------------------------------------------------------------------* |
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-2006 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2007 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. |
… | |
… | |
36 | #include <cerrno> |
36 | #include <cerrno> |
37 | |
37 | |
38 | #include "rxvt.h" |
38 | #include "rxvt.h" |
39 | #include "rxvtdaemon.h" |
39 | #include "rxvtdaemon.h" |
40 | #include "libptytty.h" |
40 | #include "libptytty.h" |
41 | #include "iom.h" |
|
|
42 | |
41 | |
43 | struct server : rxvt_connection { |
42 | struct server : rxvt_connection { |
44 | log_callback log_cb; |
43 | log_callback log_cb; |
45 | getfd_callback getfd_cb; |
44 | getfd_callback getfd_cb; |
46 | |
45 | |
47 | 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; |
48 | void log_msg (const char *msg); |
47 | void log_msg (const char *msg); |
49 | int getfd (int remote_fd); |
48 | int getfd (int remote_fd); |
50 | |
49 | |
51 | server (int fd) |
50 | server (int fd) |
52 | : read_ev (this, &server::read_cb), |
|
|
53 | log_cb (this, &server::log_msg), |
|
|
54 | getfd_cb (this, &server::getfd) |
|
|
55 | { |
51 | { |
|
|
52 | read_ev.set <server, &server::read_cb> (this); |
|
|
53 | log_cb.set <server, &server::log_msg> (this); |
|
|
54 | getfd_cb.set<server, &server::getfd> (this); |
|
|
55 | |
56 | this->fd = fd; |
56 | this->fd = fd; |
|
|
57 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
58 | fcntl (fd, F_SETFL, 0); |
57 | read_ev.start (fd, EVENT_READ); |
59 | read_ev.start (fd, ev::READ); |
58 | } |
60 | } |
59 | |
61 | |
60 | void err (const char *format = 0, ...); |
62 | void err (const char *format = 0, ...); |
61 | }; |
63 | }; |
62 | |
64 | |
63 | struct unix_listener { |
65 | struct unix_listener { |
64 | int fd; |
66 | int fd; |
65 | |
67 | |
66 | void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
68 | void accept_cb (ev::io &w, int revents); ev::io accept_ev; |
67 | |
69 | |
68 | unix_listener (const char *sockname); |
70 | unix_listener (const char *sockname); |
69 | }; |
71 | }; |
70 | |
72 | |
71 | unix_listener::unix_listener (const char *sockname) |
73 | unix_listener::unix_listener (const char *sockname) |
72 | : accept_ev (this, &unix_listener::accept_cb) |
|
|
73 | { |
74 | { |
|
|
75 | accept_ev.set<unix_listener, &unix_listener::accept_cb> (this); |
|
|
76 | |
74 | sockaddr_un sa; |
77 | sockaddr_un sa; |
75 | |
78 | |
76 | if (strlen (sockname) >= sizeof(sa.sun_path)) |
79 | if (strlen (sockname) >= sizeof(sa.sun_path)) |
77 | { |
80 | { |
78 | fputs ("socket name too long, aborting.\n", stderr); |
81 | fputs ("socket name too long, aborting.\n", stderr); |
79 | exit (EXIT_FAILURE); |
82 | exit (EXIT_FAILURE); |
80 | } |
83 | } |
… | |
… | |
84 | perror ("unable to create listening socket"); |
87 | perror ("unable to create listening socket"); |
85 | exit (EXIT_FAILURE); |
88 | exit (EXIT_FAILURE); |
86 | } |
89 | } |
87 | |
90 | |
88 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
91 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
92 | fcntl (fd, F_SETFL, O_NONBLOCK); |
89 | |
93 | |
90 | sa.sun_family = AF_UNIX; |
94 | sa.sun_family = AF_UNIX; |
91 | strcpy (sa.sun_path, sockname); |
95 | strcpy (sa.sun_path, sockname); |
92 | |
96 | |
93 | unlink (rxvt_connection::unix_sockname ()); |
97 | unlink (rxvt_connection::unix_sockname ()); |
… | |
… | |
106 | { |
110 | { |
107 | perror ("unable to listen on socket"); |
111 | perror ("unable to listen on socket"); |
108 | exit (EXIT_FAILURE); |
112 | exit (EXIT_FAILURE); |
109 | } |
113 | } |
110 | |
114 | |
111 | accept_ev.start (fd, EVENT_READ); |
115 | accept_ev.start (fd, ev::READ); |
112 | } |
116 | } |
113 | |
117 | |
114 | void unix_listener::accept_cb (io_watcher &w, short revents) |
118 | void unix_listener::accept_cb (ev::io &w, int revents) |
115 | { |
119 | { |
116 | int fd2 = accept (fd, 0, 0); |
120 | int fd2 = accept (fd, 0, 0); |
117 | |
121 | |
118 | if (fd2 >= 0) |
122 | if (fd2 >= 0) |
119 | { |
|
|
120 | fcntl (fd2, F_SETFD, FD_CLOEXEC); |
|
|
121 | new server (fd2); |
123 | new server (fd2); |
122 | } |
|
|
123 | } |
124 | } |
124 | |
125 | |
125 | int server::getfd (int remote_fd) |
126 | int server::getfd (int remote_fd) |
126 | { |
127 | { |
127 | send ("GETFD"); |
128 | send ("GETFD"); |
… | |
… | |
151 | send ("END"), send (0); |
152 | send ("END"), send (0); |
152 | close (fd); |
153 | close (fd); |
153 | delete this; |
154 | delete this; |
154 | } |
155 | } |
155 | |
156 | |
156 | void server::read_cb (io_watcher &w, short revents) |
157 | void server::read_cb (ev::io &w, int revents) |
157 | { |
158 | { |
158 | auto_str tok; |
159 | auto_str tok; |
159 | |
160 | |
160 | if (recv (tok)) |
161 | if (recv (tok)) |
161 | { |
162 | { |
162 | if (!strcmp (tok, "NEW")) |
163 | if (!strcmp (tok, "NEW")) |
163 | { |
164 | { |
164 | stringvec *argv = new stringvec; |
165 | stringvec *argv = new stringvec; |
165 | stringvec *envv = new stringvec; |
166 | stringvec *envv = new stringvec; |
166 | |
167 | |
167 | for (;;) |
168 | for (;;) |
168 | { |
169 | { |
169 | if (!recv (tok)) |
170 | if (!recv (tok)) |
170 | return err (); |
171 | return err (); |
171 | |
172 | |
… | |
… | |
191 | |
192 | |
192 | envv->push_back (0); |
193 | envv->push_back (0); |
193 | |
194 | |
194 | { |
195 | { |
195 | rxvt_term *term = new rxvt_term; |
196 | rxvt_term *term = new rxvt_term; |
196 | |
197 | |
197 | term->log_hook = &log_cb; |
198 | term->log_hook = &log_cb; |
198 | term->getfd_hook = &getfd_cb; |
199 | term->getfd_hook = &getfd_cb; |
199 | |
200 | |
200 | bool success; |
201 | bool success = true; |
201 | |
202 | |
202 | try |
203 | try |
203 | { |
204 | { |
204 | success = term->init (argv, envv); |
205 | term->init (argv, envv); |
205 | } |
206 | } |
206 | catch (const class rxvt_failure_exception &e) |
207 | catch (const class rxvt_failure_exception &e) |
207 | { |
208 | { |
208 | success = false; |
209 | success = false; |
209 | } |
210 | } |
… | |
… | |
244 | { |
245 | { |
245 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
246 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
246 | return EXIT_FAILURE; |
247 | return EXIT_FAILURE; |
247 | } |
248 | } |
248 | } |
249 | } |
249 | |
|
|
250 | chdir ("/"); |
|
|
251 | |
250 | |
252 | if (opt_opendisplay) |
251 | if (opt_opendisplay) |
253 | displays.get (getenv ("DISPLAY")); // open display and never release it |
252 | displays.get (getenv ("DISPLAY")); // open display and never release it |
254 | |
253 | |
255 | char *sockname = rxvt_connection::unix_sockname (); |
254 | char *sockname = rxvt_connection::unix_sockname (); |
256 | unix_listener l (sockname); |
255 | unix_listener l (sockname); |
|
|
256 | |
|
|
257 | chdir ("/"); |
257 | |
258 | |
258 | if (!opt_quiet) |
259 | if (!opt_quiet) |
259 | { |
260 | { |
260 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
261 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
261 | fflush (stdout); |
262 | fflush (stdout); |
… | |
… | |
272 | rxvt_log ("unable to fork daemon, aborting.\n"); |
273 | rxvt_log ("unable to fork daemon, aborting.\n"); |
273 | return EXIT_FAILURE; |
274 | return EXIT_FAILURE; |
274 | } |
275 | } |
275 | else if (pid > 0) |
276 | else if (pid > 0) |
276 | _exit (EXIT_SUCCESS); |
277 | _exit (EXIT_SUCCESS); |
277 | } |
|
|
278 | |
278 | |
279 | io_manager::loop (); |
279 | ev_default_fork (); |
|
|
280 | } |
|
|
281 | |
|
|
282 | ev_loop (0); |
280 | |
283 | |
281 | return EXIT_SUCCESS; |
284 | return EXIT_SUCCESS; |
282 | } |
285 | } |
283 | |
286 | |