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.25 by root, Tue Jan 17 15:43:49 2006 UTC vs.
Revision 1.41 by root, Tue Dec 4 16:24:00 2007 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-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
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 (AF_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 send ("GETFD"); 128 send ("GETFD");
124 send (remote_fd); 129 send (remote_fd);
125 return rxvt_recv_fd (fd); 130 return ptytty::recv_fd (fd);
126} 131}
127 132
128void server::log_msg (const char *msg) 133void server::log_msg (const char *msg)
129{ 134{
130 send ("MSG"), send (msg); 135 send ("MSG"), send (msg);
139 va_list ap; 144 va_list ap;
140 va_start (ap, format); 145 va_start (ap, format);
141 vsnprintf (err, 1024, format, ap); 146 vsnprintf (err, 1024, format, ap);
142 va_end (ap); 147 va_end (ap);
143 148
144 send ("MSG"), send (err); 149 log_msg (err);
145 } 150 }
146 151
147 send ("END", 0); 152 send ("END"), send (0);
148 close (fd); 153 close (fd);
149 delete this; 154 delete this;
150} 155}
151 156
152void server::read_cb (io_watcher &w, short revents) 157void server::read_cb (ev::io &w, int revents)
153{ 158{
154 auto_str tok; 159 auto_str tok;
155 160
156 if (recv (tok)) 161 if (recv (tok))
157 { 162 {
158 if (!strcmp (tok, "NEW")) 163 if (!strcmp (tok, "NEW"))
159 { 164 {
160 stringvec *argv = new stringvec; 165 stringvec *argv = new stringvec;
161 stringvec *envv = new stringvec; 166 stringvec *envv = new stringvec;
162 167
163 for (;;) 168 for (;;)
164 { 169 {
165 if (!recv (tok)) 170 if (!recv (tok))
166 return err (); 171 return err ();
167 172
168 if (!strcmp (tok, "END")) 173 if (!strcmp (tok, "END"))
169 break; 174 break;
170 else if (!strcmp (tok, "ENV") && recv (tok)) 175 else if (!strcmp (tok, "ENV") && recv (tok))
171 envv->push_back (tok.get ()); 176 envv->push_back (strdup (tok));
172 else if (!strcmp (tok, "CWD") && recv (tok)) 177 else if (!strcmp (tok, "CWD") && recv (tok))
173 { 178 {
174 if (chdir (tok)) 179 if (chdir (tok))
180 {
181 delete envv;
182 delete argv;
175 err ("unable to change to working directory to '%s': %s", 183 return err ("unable to change to working directory to '%s', aborting: %s.\n",
176 (char *)tok, strerror (errno)); 184 (char *)tok, strerror (errno));
185 }
177 } 186 }
178 else if (!strcmp (tok, "ARG") && recv (tok)) 187 else if (!strcmp (tok, "ARG") && recv (tok))
179 argv->push_back (tok.get ()); 188 argv->push_back (strdup (tok));
180 else 189 else
181 return err ("protocol error: unexpected NEW token"); 190 return err ("protocol error: unexpected NEW token.\n");
182 } 191 }
183 192
184 envv->push_back (0); 193 envv->push_back (0);
185 194
186 { 195 {
187 rxvt_term *term = new rxvt_term; 196 rxvt_term *term = new rxvt_term;
188 197
189 term->log_hook = &log_cb; 198 term->log_hook = &log_cb;
190 term->getfd_hook = &getfd_cb; 199 term->getfd_hook = &getfd_cb;
191 term->argv = argv;
192 term->envv = envv;
193 200
194 bool success; 201 bool success;
195 202
196 try 203 try
197 { 204 {
198 success = term->init (argv->size (), argv->begin ()); 205 success = term->init (argv, envv);
199 } 206 }
200 catch (const class rxvt_failure_exception &e) 207 catch (const class rxvt_failure_exception &e)
201 { 208 {
202 success = false; 209 success = false;
203 } 210 }
211 218
212 send ("END"); send (success ? 1 : 0); 219 send ("END"); send (success ? 1 : 0);
213 } 220 }
214 } 221 }
215 else 222 else
216 return err ("protocol error: request '%s' unsupported.", (char *)tok); 223 return err ("protocol error: request '%s' unsupported.\n", (char *)tok);
217 } 224 }
218 else 225 else
219 return err (); 226 return err ();
220} 227}
221 228
238 { 245 {
239 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]);
240 return EXIT_FAILURE; 247 return EXIT_FAILURE;
241 } 248 }
242 } 249 }
243 250
244 chdir ("/"); 251 chdir ("/");
245 252
246 if (opt_opendisplay) 253 if (opt_opendisplay)
247 displays.get (getenv ("DISPLAY")); // open display and never release it 254 displays.get (getenv ("DISPLAY")); // open display and never release it
248 255
266 rxvt_log ("unable to fork daemon, aborting.\n"); 273 rxvt_log ("unable to fork daemon, aborting.\n");
267 return EXIT_FAILURE; 274 return EXIT_FAILURE;
268 } 275 }
269 else if (pid > 0) 276 else if (pid > 0)
270 _exit (EXIT_SUCCESS); 277 _exit (EXIT_SUCCESS);
271 }
272 278
273 io_manager::loop (); 279 ev_default_fork ();
280 }
281
282 ev_loop (0);
274 283
275 return EXIT_SUCCESS; 284 return EXIT_SUCCESS;
276} 285}
277 286

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines