ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtd.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtd.C (file contents):
Revision 1.22 by root, Fri Dec 23 14:46:35 2005 UTC vs.
Revision 1.44 by root, Tue Feb 19 13:37:17 2008 UTC

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-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.
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
43extern char **environ; 38#include "rxvt.h"
39#include "rxvtdaemon.h"
40#include "libptytty.h"
44 41
45struct server : rxvt_connection { 42struct 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),
55 log_cb (this, &server::log_msg),
56 getfd_cb (this, &server::getfd)
57 { 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
58 this->fd = fd; 56 this->fd = fd;
57 fcntl (fd, F_SETFD, FD_CLOEXEC);
58 fcntl (fd, F_SETFL, 0);
59 read_ev.start (fd, EVENT_READ); 59 read_ev.start (fd, ev::READ);
60 } 60 }
61 61
62 void err (const char *format = 0, ...); 62 void err (const char *format = 0, ...);
63}; 63};
64 64
65struct unix_listener { 65struct unix_listener {
66 int fd; 66 int fd;
67 67
68 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;
69 69
70 unix_listener (const char *sockname); 70 unix_listener (const char *sockname);
71}; 71};
72 72
73unix_listener::unix_listener (const char *sockname) 73unix_listener::unix_listener (const char *sockname)
74: accept_ev (this, &unix_listener::accept_cb)
75{ 74{
75 accept_ev.set<unix_listener, &unix_listener::accept_cb> (this);
76
77 sockaddr_un sa;
78
79 if (strlen (sockname) >= sizeof(sa.sun_path))
80 {
81 fputs ("socket name too long, aborting.\n", stderr);
82 exit (EXIT_FAILURE);
83 }
84
76 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) 85 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
77 { 86 {
78 perror ("unable to create listening socket"); 87 perror ("unable to create listening socket");
79 exit (EXIT_FAILURE); 88 exit (EXIT_FAILURE);
80 } 89 }
81 90
82 fcntl (fd, F_SETFD, FD_CLOEXEC); 91 fcntl (fd, F_SETFD, FD_CLOEXEC);
83 92 fcntl (fd, F_SETFL, O_NONBLOCK);
84 sockaddr_un sa;
85 93
86 sa.sun_family = AF_UNIX; 94 sa.sun_family = AF_UNIX;
87 strcpy (sa.sun_path, sockname); 95 strcpy (sa.sun_path, sockname);
88 96
89 unlink (rxvt_connection::unix_sockname ()); 97 unlink (rxvt_connection::unix_sockname ());
102 { 110 {
103 perror ("unable to listen on socket"); 111 perror ("unable to listen on socket");
104 exit (EXIT_FAILURE); 112 exit (EXIT_FAILURE);
105 } 113 }
106 114
107 accept_ev.start (fd, EVENT_READ); 115 accept_ev.start (fd, ev::READ);
108} 116}
109 117
110void unix_listener::accept_cb (io_watcher &w, short revents) 118void unix_listener::accept_cb (ev::io &w, int revents)
111{ 119{
112 int fd2 = accept (fd, 0, 0); 120 int fd2 = accept (fd, 0, 0);
113 121
114 if (fd2 >= 0) 122 if (fd2 >= 0)
115 {
116 fcntl (fd2, F_SETFD, FD_CLOEXEC);
117 new server (fd2); 123 new server (fd2);
118 }
119} 124}
120 125
121int server::getfd (int remote_fd) 126int server::getfd (int remote_fd)
122{ 127{
123#if ENABLE_FRILLS && HAVE_UNIX_FDPASS
124 send ("GETFD"); 128 send ("GETFD");
125 send (remote_fd); 129 send (remote_fd);
126 return rxvt_recv_fd (fd); 130 return ptytty::recv_fd (fd);
127#else
128 return -1;
129#endif
130} 131}
131 132
132void server::log_msg (const char *msg) 133void server::log_msg (const char *msg)
133{ 134{
134 send ("MSG"), send (msg); 135 send ("MSG"), send (msg);
143 va_list ap; 144 va_list ap;
144 va_start (ap, format); 145 va_start (ap, format);
145 vsnprintf (err, 1024, format, ap); 146 vsnprintf (err, 1024, format, ap);
146 va_end (ap); 147 va_end (ap);
147 148
148 send ("MSG"), send (err); 149 log_msg (err);
149 } 150 }
150 151
151 send ("END", 0); 152 send ("END"), send (0);
152 close (fd); 153 close (fd);
153 delete this; 154 delete this;
154} 155}
155 156
156void server::read_cb (io_watcher &w, short revents) 157void 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
172 if (!strcmp (tok, "END")) 173 if (!strcmp (tok, "END"))
173 break; 174 break;
174 else if (!strcmp (tok, "ENV") && recv (tok)) 175 else if (!strcmp (tok, "ENV") && recv (tok))
175 envv->push_back (tok.get ()); 176 envv->push_back (strdup (tok));
176 else if (!strcmp (tok, "CWD") && recv (tok)) 177 else if (!strcmp (tok, "CWD") && recv (tok))
177 { 178 {
178 if (chdir (tok)) 179 if (chdir (tok))
180 {
181 delete envv;
182 delete argv;
179 err ("unable to change to working directory to '%s': %s", 183 return err ("unable to change to working directory to '%s', aborting: %s.\n",
180 (char *)tok, strerror (errno)); 184 (char *)tok, strerror (errno));
185 }
181 } 186 }
182 else if (!strcmp (tok, "ARG") && recv (tok)) 187 else if (!strcmp (tok, "ARG") && recv (tok))
183 argv->push_back (tok.get ()); 188 argv->push_back (strdup (tok));
184 else 189 else
185 return err ("protocol error: unexpected NEW token"); 190 return err ("protocol error: unexpected NEW token.\n");
186 } 191 }
187 192
188 envv->push_back (0); 193 envv->push_back (0);
189 194
190 { 195 {
191 char **old_environ = environ;
192 environ = envv->begin ();
193
194 rxvt_term *term = new rxvt_term; 196 rxvt_term *term = new rxvt_term;
195 197
196 term->log_hook = &log_cb; 198 term->log_hook = &log_cb;
197 term->getfd_hook = &getfd_cb; 199 term->getfd_hook = &getfd_cb;
198 term->argv = argv;
199 term->envv = envv;
200 200
201 bool success; 201 bool success = true;
202 202
203 try 203 try
204 { 204 {
205 success = term->init (argv->size (), argv->begin ()); 205 term->init (argv, envv);
206 } 206 }
207 catch (const class rxvt_failure_exception &e) 207 catch (const class rxvt_failure_exception &e)
208 { 208 {
209 success = false; 209 success = false;
210 } 210 }
211 211
212 term->log_hook = 0; 212 term->log_hook = 0;
213 213
214 environ = old_environ;
215 chdir ("/"); 214 chdir ("/");
216 215
217 if (!success) 216 if (!success)
218 term->destroy (); 217 term->destroy ();
219 218
220 send ("END"); send (success ? 1 : 0); 219 send ("END"); send (success ? 1 : 0);
221 } 220 }
222 } 221 }
223 else 222 else
224 return err ("protocol error: request '%s' unsupported.", (char *)tok); 223 return err ("protocol error: request '%s' unsupported.\n", (char *)tok);
225 } 224 }
226 else 225 else
227 return err (); 226 return err ();
228} 227}
229 228
230int opt_fork, opt_opendisplay, opt_quiet; 229int opt_fork, opt_opendisplay, opt_quiet;
231 230
232int 231int
233main (int argc, const char *const *argv) 232main (int argc, const char *const *argv)
234{ 233{
234 rxvt_init ();
235
235 for (int i = 1; i < argc; i++) 236 for (int i = 1; i < argc; i++)
236 { 237 {
237 if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) 238 if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork"))
238 opt_fork = 1; 239 opt_fork = 1;
239 else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) 240 else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay"))
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 rxvt_init ();
251
252 chdir ("/");
253 250
254 if (opt_opendisplay) 251 if (opt_opendisplay)
255 displays.get (getenv ("DISPLAY")); // open display and never release it 252 displays.get (getenv ("DISPLAY")); // open display and never release it
256 253
257 char *sockname = rxvt_connection::unix_sockname (); 254 char *sockname = rxvt_connection::unix_sockname ();
258 unix_listener l (sockname); 255 unix_listener l (sockname);
256
257 chdir ("/");
259 258
260 if (!opt_quiet) 259 if (!opt_quiet)
261 { 260 {
262 printf ("rxvt-unicode daemon listening on %s.\n", sockname); 261 printf ("rxvt-unicode daemon listening on %s.\n", sockname);
263 fflush (stdout); 262 fflush (stdout);
274 rxvt_log ("unable to fork daemon, aborting.\n"); 273 rxvt_log ("unable to fork daemon, aborting.\n");
275 return EXIT_FAILURE; 274 return EXIT_FAILURE;
276 } 275 }
277 else if (pid > 0) 276 else if (pid > 0)
278 _exit (EXIT_SUCCESS); 277 _exit (EXIT_SUCCESS);
279 }
280 278
281 io_manager::loop (); 279 ev_default_fork ();
280 }
281
282 ev_loop (0);
282 283
283 return EXIT_SUCCESS; 284 return EXIT_SUCCESS;
284} 285}
285 286

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines