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.30 by root, Wed Aug 9 22:01:07 2006 UTC vs.
Revision 1.36 by root, Sat Nov 17 07:28: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), 51 : read_ev (this, &server::read_cb),
53 log_cb (this, &server::log_msg), 52 log_cb (this, &server::log_msg),
54 getfd_cb (this, &server::getfd) 53 getfd_cb (this, &server::getfd)
55 { 54 {
56 this->fd = fd; 55 fcntl (fd, F_SETFD, FD_CLOEXEC);
56 fcntl (fd, F_SETFL, 0);
57 read_ev.start (fd, EVENT_READ); 57 read_ev.start (fd, ev::READ);
58 } 58 }
59 59
60 void err (const char *format = 0, ...); 60 void err (const char *format = 0, ...);
61}; 61};
62 62
63struct unix_listener { 63struct unix_listener {
64 int fd; 64 int fd;
65 65
66 void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; 66 void accept_cb (ev::io &w, int revents); ev::io accept_ev;
67 67
68 unix_listener (const char *sockname); 68 unix_listener (const char *sockname);
69}; 69};
70 70
71unix_listener::unix_listener (const char *sockname) 71unix_listener::unix_listener (const char *sockname)
72: accept_ev (this, &unix_listener::accept_cb) 72: accept_ev (this, &unix_listener::accept_cb)
73{ 73{
74 sockaddr_un sa; 74 sockaddr_un sa;
75 75
76 if (strlen(sockname) >= sizeof(sa.sun_path)) 76 if (strlen (sockname) >= sizeof(sa.sun_path))
77 {
77 rxvt_fatal ("socket name too long, aborting.\n"); 78 fputs ("socket name too long, aborting.\n", stderr);
79 exit (EXIT_FAILURE);
80 }
78 81
79 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) 82 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
80 { 83 {
81 perror ("unable to create listening socket"); 84 perror ("unable to create listening socket");
82 exit (EXIT_FAILURE); 85 exit (EXIT_FAILURE);
83 } 86 }
84 87
85 fcntl (fd, F_SETFD, FD_CLOEXEC); 88 fcntl (fd, F_SETFD, FD_CLOEXEC);
89 fcntl (fd, F_SETFL, O_NONBLOCK);
86 90
87 sa.sun_family = AF_UNIX; 91 sa.sun_family = AF_UNIX;
88 strcpy (sa.sun_path, sockname); 92 strcpy (sa.sun_path, sockname);
89 93
90 unlink (rxvt_connection::unix_sockname ()); 94 unlink (rxvt_connection::unix_sockname ());
103 { 107 {
104 perror ("unable to listen on socket"); 108 perror ("unable to listen on socket");
105 exit (EXIT_FAILURE); 109 exit (EXIT_FAILURE);
106 } 110 }
107 111
108 accept_ev.start (fd, EVENT_READ); 112 accept_ev.start (fd, ev::READ);
109} 113}
110 114
111void unix_listener::accept_cb (io_watcher &w, short revents) 115void unix_listener::accept_cb (ev::io &w, int revents)
112{ 116{
113 int fd2 = accept (fd, 0, 0); 117 int fd2 = accept (fd, 0, 0);
114 118
115 if (fd2 >= 0) 119 if (fd2 >= 0)
116 {
117 fcntl (fd2, F_SETFD, FD_CLOEXEC);
118 new server (fd2); 120 new server (fd2);
119 }
120} 121}
121 122
122int server::getfd (int remote_fd) 123int server::getfd (int remote_fd)
123{ 124{
124 send ("GETFD"); 125 send ("GETFD");
140 va_list ap; 141 va_list ap;
141 va_start (ap, format); 142 va_start (ap, format);
142 vsnprintf (err, 1024, format, ap); 143 vsnprintf (err, 1024, format, ap);
143 va_end (ap); 144 va_end (ap);
144 145
145 send ("MSG"), send (err); 146 log_msg (err);
146 } 147 }
147 148
148 send ("END", 0); 149 send ("END"), send (0);
149 close (fd); 150 close (fd);
150 delete this; 151 delete this;
151} 152}
152 153
153void server::read_cb (io_watcher &w, short revents) 154void server::read_cb (ev::io &w, int revents)
154{ 155{
155 auto_str tok; 156 auto_str tok;
156 157
157 if (recv (tok)) 158 if (recv (tok))
158 { 159 {
159 if (!strcmp (tok, "NEW")) 160 if (!strcmp (tok, "NEW"))
160 { 161 {
161 stringvec *argv = new stringvec; 162 stringvec *argv = new stringvec;
162 stringvec *envv = new stringvec; 163 stringvec *envv = new stringvec;
163 164
164 for (;;) 165 for (;;)
165 { 166 {
166 if (!recv (tok)) 167 if (!recv (tok))
167 return err (); 168 return err ();
168 169
171 else if (!strcmp (tok, "ENV") && recv (tok)) 172 else if (!strcmp (tok, "ENV") && recv (tok))
172 envv->push_back (strdup (tok)); 173 envv->push_back (strdup (tok));
173 else if (!strcmp (tok, "CWD") && recv (tok)) 174 else if (!strcmp (tok, "CWD") && recv (tok))
174 { 175 {
175 if (chdir (tok)) 176 if (chdir (tok))
177 {
178 delete envv;
179 delete argv;
176 err ("unable to change to working directory to '%s': %s", 180 return err ("unable to change to working directory to '%s', aborting: %s.\n",
177 (char *)tok, strerror (errno)); 181 (char *)tok, strerror (errno));
182 }
178 } 183 }
179 else if (!strcmp (tok, "ARG") && recv (tok)) 184 else if (!strcmp (tok, "ARG") && recv (tok))
180 argv->push_back (strdup (tok)); 185 argv->push_back (strdup (tok));
181 else 186 else
182 return err ("protocol error: unexpected NEW token"); 187 return err ("protocol error: unexpected NEW token.\n");
183 } 188 }
184 189
185 envv->push_back (0); 190 envv->push_back (0);
186 191
187 { 192 {
188 rxvt_term *term = new rxvt_term; 193 rxvt_term *term = new rxvt_term;
189 194
190 term->log_hook = &log_cb; 195 term->log_hook = &log_cb;
191 term->getfd_hook = &getfd_cb; 196 term->getfd_hook = &getfd_cb;
192 197
193 bool success; 198 bool success;
194 199
195 try 200 try
196 { 201 {
197 success = term->init (argv, envv); 202 success = term->init (argv, envv);
198 } 203 }
199 catch (const class rxvt_failure_exception &e) 204 catch (const class rxvt_failure_exception &e)
210 215
211 send ("END"); send (success ? 1 : 0); 216 send ("END"); send (success ? 1 : 0);
212 } 217 }
213 } 218 }
214 else 219 else
215 return err ("protocol error: request '%s' unsupported.", (char *)tok); 220 return err ("protocol error: request '%s' unsupported.\n", (char *)tok);
216 } 221 }
217 else 222 else
218 return err (); 223 return err ();
219} 224}
220 225
237 { 242 {
238 rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); 243 rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]);
239 return EXIT_FAILURE; 244 return EXIT_FAILURE;
240 } 245 }
241 } 246 }
242 247
243 chdir ("/"); 248 chdir ("/");
244 249
245 if (opt_opendisplay) 250 if (opt_opendisplay)
246 displays.get (getenv ("DISPLAY")); // open display and never release it 251 displays.get (getenv ("DISPLAY")); // open display and never release it
247 252
267 } 272 }
268 else if (pid > 0) 273 else if (pid > 0)
269 _exit (EXIT_SUCCESS); 274 _exit (EXIT_SUCCESS);
270 } 275 }
271 276
272 io_manager::loop (); 277 ev::ev_loop (0);
273 278
274 return EXIT_SUCCESS; 279 return EXIT_SUCCESS;
275} 280}
276 281

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines