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.15 by root, Fri Aug 13 22:38:57 2004 UTC vs.
Revision 1.30 by root, Wed Aug 9 22:01:07 2006 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 "iom.h"
27
28#include <cstdio> 24#include <cstdio>
29#include <cstdlib> 25#include <cstdlib>
30#include <cstdarg> 26#include <cstdarg>
31#include <cstring> 27#include <cstring>
32 28
37#include <sys/socket.h> 33#include <sys/socket.h>
38#include <sys/un.h> 34#include <sys/un.h>
39 35
40#include <cerrno> 36#include <cerrno>
41 37
42extern char **environ; 38#include "rxvt.h"
39#include "rxvtdaemon.h"
40#include "libptytty.h"
41#include "iom.h"
43 42
44struct server : rxvt_connection { 43struct server : rxvt_connection {
45 log_callback log_cb; 44 log_callback log_cb;
45 getfd_callback getfd_cb;
46 46
47 void read_cb (io_watcher &w, short revents); io_watcher read_ev; 47 void read_cb (io_watcher &w, short revents); io_watcher read_ev;
48 void log_msg (const char *msg); 48 void log_msg (const char *msg);
49 int getfd (int remote_fd);
49 50
50 server (int fd) 51 server (int fd)
51 : read_ev (this, &server::read_cb), 52 : read_ev (this, &server::read_cb),
52 log_cb (this, &server::log_msg) 53 log_cb (this, &server::log_msg),
54 getfd_cb (this, &server::getfd)
53 { 55 {
54 this->fd = fd; 56 this->fd = fd;
55 read_ev.start (fd, EVENT_READ); 57 read_ev.start (fd, EVENT_READ);
56 } 58 }
57 59
67}; 69};
68 70
69unix_listener::unix_listener (const char *sockname) 71unix_listener::unix_listener (const char *sockname)
70: accept_ev (this, &unix_listener::accept_cb) 72: accept_ev (this, &unix_listener::accept_cb)
71{ 73{
74 sockaddr_un sa;
75
76 if (strlen(sockname) >= sizeof(sa.sun_path))
77 rxvt_fatal ("socket name too long, aborting.\n");
78
72 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) 79 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
73 { 80 {
74 perror ("unable to create listening socket"); 81 perror ("unable to create listening socket");
75 exit (EXIT_FAILURE); 82 exit (EXIT_FAILURE);
76 } 83 }
77 84
78 fcntl (fd, F_SETFD, FD_CLOEXEC); 85 fcntl (fd, F_SETFD, FD_CLOEXEC);
79 86
80 sockaddr_un sa;
81
82 sa.sun_family = AF_UNIX; 87 sa.sun_family = AF_UNIX;
83 strcpy (sa.sun_path, sockname); 88 strcpy (sa.sun_path, sockname);
84 89
85 unlink (rxvt_connection::unix_sockname ()); 90 unlink (rxvt_connection::unix_sockname ());
86 91
110 if (fd2 >= 0) 115 if (fd2 >= 0)
111 { 116 {
112 fcntl (fd2, F_SETFD, FD_CLOEXEC); 117 fcntl (fd2, F_SETFD, FD_CLOEXEC);
113 new server (fd2); 118 new server (fd2);
114 } 119 }
120}
121
122int server::getfd (int remote_fd)
123{
124 send ("GETFD");
125 send (remote_fd);
126 return ptytty::recv_fd (fd);
115} 127}
116 128
117void server::log_msg (const char *msg) 129void server::log_msg (const char *msg)
118{ 130{
119 send ("MSG"), send (msg); 131 send ("MSG"), send (msg);
155 return err (); 167 return err ();
156 168
157 if (!strcmp (tok, "END")) 169 if (!strcmp (tok, "END"))
158 break; 170 break;
159 else if (!strcmp (tok, "ENV") && recv (tok)) 171 else if (!strcmp (tok, "ENV") && recv (tok))
160 envv->push_back (tok.get ()); 172 envv->push_back (strdup (tok));
161 else if (!strcmp (tok, "CWD") && recv (tok)) 173 else if (!strcmp (tok, "CWD") && recv (tok))
162 { 174 {
163 if (chdir (tok)) 175 if (chdir (tok))
164 err ("unable to change to working directory to '%s': %s", 176 err ("unable to change to working directory to '%s': %s",
165 (char *)tok, strerror (errno)); 177 (char *)tok, strerror (errno));
166 } 178 }
167 else if (!strcmp (tok, "ARG") && recv (tok)) 179 else if (!strcmp (tok, "ARG") && recv (tok))
168 argv->push_back (tok.get ()); 180 argv->push_back (strdup (tok));
169 else 181 else
170 return err ("protocol error: unexpected NEW token"); 182 return err ("protocol error: unexpected NEW token");
171 } 183 }
172 184
173 envv->push_back (0); 185 envv->push_back (0);
174 186
175 { 187 {
176 char **old_environ = environ;
177 environ = envv->begin ();
178
179 rxvt_term *term = new rxvt_term; 188 rxvt_term *term = new rxvt_term;
180 189
181 term->log_hook = &log_cb; 190 term->log_hook = &log_cb;
182 term->argv = argv; 191 term->getfd_hook = &getfd_cb;
183 term->envv = envv;
184 192
185 bool success; 193 bool success;
186 194
187 try 195 try
188 { 196 {
189 success = term->init (argv->size (), argv->begin ()); 197 success = term->init (argv, envv);
190 } 198 }
191 catch (const class rxvt_failure_exception &e) 199 catch (const class rxvt_failure_exception &e)
192 { 200 {
193 success = false; 201 success = false;
194 } 202 }
195 203
196 term->log_hook = 0; 204 term->log_hook = 0;
197 205
198 environ = old_environ; 206 chdir ("/");
199 207
200 if (!success) 208 if (!success)
201 term->destroy (); 209 term->destroy ();
202 210
203 send ("END"); send (success ? 1 : 0); 211 send ("END"); send (success ? 1 : 0);
208 } 216 }
209 else 217 else
210 return err (); 218 return err ();
211} 219}
212 220
221int opt_fork, opt_opendisplay, opt_quiet;
222
213int 223int
214main (int argc, const char *const *argv) 224main (int argc, const char *const *argv)
215{ 225{
216 rxvt_init_signals (); 226 rxvt_init ();
227
228 for (int i = 1; i < argc; i++)
229 {
230 if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork"))
231 opt_fork = 1;
232 else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay"))
233 opt_opendisplay = 1;
234 else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet"))
235 opt_quiet = 1;
236 else
237 {
238 rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]);
239 return EXIT_FAILURE;
240 }
241 }
242
243 chdir ("/");
244
245 if (opt_opendisplay)
246 displays.get (getenv ("DISPLAY")); // open display and never release it
217 247
218 char *sockname = rxvt_connection::unix_sockname (); 248 char *sockname = rxvt_connection::unix_sockname ();
219 unix_listener l (sockname); 249 unix_listener l (sockname);
250
251 if (!opt_quiet)
252 {
220 printf ("rxvtd listening on %s.\n", sockname); 253 printf ("rxvt-unicode daemon listening on %s.\n", sockname);
254 fflush (stdout);
255 }
256
221 free (sockname); 257 free (sockname);
222 258
223 iom.loop (); 259 if (opt_fork)
260 {
261 pid_t pid = fork ();
262
263 if (pid < 0)
264 {
265 rxvt_log ("unable to fork daemon, aborting.\n");
266 return EXIT_FAILURE;
267 }
268 else if (pid > 0)
269 _exit (EXIT_SUCCESS);
270 }
271
272 io_manager::loop ();
224 273
225 return EXIT_SUCCESS; 274 return EXIT_SUCCESS;
226} 275}
227 276

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines