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.29 by root, Mon Feb 20 22:42:00 2006 UTC vs.
Revision 1.41 by root, Tue Dec 4 16:24:00 2007 UTC

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
43struct server : rxvt_connection { 42struct 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
63struct unix_listener { 65struct 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
71unix_listener::unix_listener (const char *sockname) 73unix_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
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
74 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 85 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
75 { 86 {
76 perror ("unable to create listening socket"); 87 perror ("unable to create listening socket");
77 exit (EXIT_FAILURE); 88 exit (EXIT_FAILURE);
78 } 89 }
79 90
80 fcntl (fd, F_SETFD, FD_CLOEXEC); 91 fcntl (fd, F_SETFD, FD_CLOEXEC);
81 92 fcntl (fd, F_SETFL, O_NONBLOCK);
82 sockaddr_un sa;
83 93
84 sa.sun_family = AF_UNIX; 94 sa.sun_family = AF_UNIX;
85 strcpy (sa.sun_path, sockname); 95 strcpy (sa.sun_path, sockname);
86 96
87 unlink (rxvt_connection::unix_sockname ()); 97 unlink (rxvt_connection::unix_sockname ());
100 { 110 {
101 perror ("unable to listen on socket"); 111 perror ("unable to listen on socket");
102 exit (EXIT_FAILURE); 112 exit (EXIT_FAILURE);
103 } 113 }
104 114
105 accept_ev.start (fd, EVENT_READ); 115 accept_ev.start (fd, ev::READ);
106} 116}
107 117
108void unix_listener::accept_cb (io_watcher &w, short revents) 118void unix_listener::accept_cb (ev::io &w, int revents)
109{ 119{
110 int fd2 = accept (fd, 0, 0); 120 int fd2 = accept (fd, 0, 0);
111 121
112 if (fd2 >= 0) 122 if (fd2 >= 0)
113 {
114 fcntl (fd2, F_SETFD, FD_CLOEXEC);
115 new server (fd2); 123 new server (fd2);
116 }
117} 124}
118 125
119int server::getfd (int remote_fd) 126int server::getfd (int remote_fd)
120{ 127{
121 send ("GETFD"); 128 send ("GETFD");
137 va_list ap; 144 va_list ap;
138 va_start (ap, format); 145 va_start (ap, format);
139 vsnprintf (err, 1024, format, ap); 146 vsnprintf (err, 1024, format, ap);
140 va_end (ap); 147 va_end (ap);
141 148
142 send ("MSG"), send (err); 149 log_msg (err);
143 } 150 }
144 151
145 send ("END", 0); 152 send ("END"), send (0);
146 close (fd); 153 close (fd);
147 delete this; 154 delete this;
148} 155}
149 156
150void server::read_cb (io_watcher &w, short revents) 157void server::read_cb (ev::io &w, int revents)
151{ 158{
152 auto_str tok; 159 auto_str tok;
153 160
154 if (recv (tok)) 161 if (recv (tok))
155 { 162 {
156 if (!strcmp (tok, "NEW")) 163 if (!strcmp (tok, "NEW"))
157 { 164 {
158 stringvec *argv = new stringvec; 165 stringvec *argv = new stringvec;
159 stringvec *envv = new stringvec; 166 stringvec *envv = new stringvec;
160 167
161 for (;;) 168 for (;;)
162 { 169 {
163 if (!recv (tok)) 170 if (!recv (tok))
164 return err (); 171 return err ();
165 172
168 else if (!strcmp (tok, "ENV") && recv (tok)) 175 else if (!strcmp (tok, "ENV") && recv (tok))
169 envv->push_back (strdup (tok)); 176 envv->push_back (strdup (tok));
170 else if (!strcmp (tok, "CWD") && recv (tok)) 177 else if (!strcmp (tok, "CWD") && recv (tok))
171 { 178 {
172 if (chdir (tok)) 179 if (chdir (tok))
180 {
181 delete envv;
182 delete argv;
173 err ("unable to change to working directory to '%s': %s", 183 return err ("unable to change to working directory to '%s', aborting: %s.\n",
174 (char *)tok, strerror (errno)); 184 (char *)tok, strerror (errno));
185 }
175 } 186 }
176 else if (!strcmp (tok, "ARG") && recv (tok)) 187 else if (!strcmp (tok, "ARG") && recv (tok))
177 argv->push_back (strdup (tok)); 188 argv->push_back (strdup (tok));
178 else 189 else
179 return err ("protocol error: unexpected NEW token"); 190 return err ("protocol error: unexpected NEW token.\n");
180 } 191 }
181 192
182 envv->push_back (0); 193 envv->push_back (0);
183 194
184 { 195 {
185 rxvt_term *term = new rxvt_term; 196 rxvt_term *term = new rxvt_term;
186 197
187 term->log_hook = &log_cb; 198 term->log_hook = &log_cb;
188 term->getfd_hook = &getfd_cb; 199 term->getfd_hook = &getfd_cb;
189 200
190 bool success; 201 bool success;
191 202
192 try 203 try
193 { 204 {
194 success = term->init (argv, envv); 205 success = term->init (argv, envv);
195 } 206 }
196 catch (const class rxvt_failure_exception &e) 207 catch (const class rxvt_failure_exception &e)
207 218
208 send ("END"); send (success ? 1 : 0); 219 send ("END"); send (success ? 1 : 0);
209 } 220 }
210 } 221 }
211 else 222 else
212 return err ("protocol error: request '%s' unsupported.", (char *)tok); 223 return err ("protocol error: request '%s' unsupported.\n", (char *)tok);
213 } 224 }
214 else 225 else
215 return err (); 226 return err ();
216} 227}
217 228
234 { 245 {
235 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]);
236 return EXIT_FAILURE; 247 return EXIT_FAILURE;
237 } 248 }
238 } 249 }
239 250
240 chdir ("/"); 251 chdir ("/");
241 252
242 if (opt_opendisplay) 253 if (opt_opendisplay)
243 displays.get (getenv ("DISPLAY")); // open display and never release it 254 displays.get (getenv ("DISPLAY")); // open display and never release it
244 255
262 rxvt_log ("unable to fork daemon, aborting.\n"); 273 rxvt_log ("unable to fork daemon, aborting.\n");
263 return EXIT_FAILURE; 274 return EXIT_FAILURE;
264 } 275 }
265 else if (pid > 0) 276 else if (pid > 0)
266 _exit (EXIT_SUCCESS); 277 _exit (EXIT_SUCCESS);
267 }
268 278
269 io_manager::loop (); 279 ev_default_fork ();
280 }
281
282 ev_loop (0);
270 283
271 return EXIT_SUCCESS; 284 return EXIT_SUCCESS;
272} 285}
273 286

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines