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.5 by pcg, Thu Nov 27 10:12:10 2003 UTC vs.
Revision 1.21 by root, Thu Jul 7 19:37:47 2005 UTC

1/*--------------------------------*-C-*---------------------------------*
2 * File: rxvtd.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7 *
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
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
22
23#include "../config.h"
1#include "rxvtlib.h" 24#include "rxvt.h"
2#include "rxvtdaemon.h" 25#include "rxvtdaemon.h"
3#include "iom.h" 26#include "iom.h"
4 27
5#include <cstdio> 28#include <cstdio>
6#include <cstdlib> 29#include <cstdlib>
7#include <cstdarg> 30#include <cstdarg>
8#include <cstring> 31#include <cstring>
9 32
10#include <unistd.h> 33#include <unistd.h>
34#include <fcntl.h>
11#include <sys/types.h> 35#include <sys/types.h>
12#include <sys/stat.h> 36#include <sys/stat.h>
13#include <sys/socket.h> 37#include <sys/socket.h>
14#include <sys/un.h> 38#include <sys/un.h>
15 39
16#include <cerrno> 40#include <cerrno>
17 41
18extern char **environ; 42extern char **environ;
19 43
20struct server : rxvt_connection { 44struct server : rxvt_connection {
45 log_callback log_cb;
46
21 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);
22 49
23 server (int fd) 50 server (int fd)
24 : read_ev (this, &server::read_cb) 51 : read_ev (this, &server::read_cb),
52 log_cb (this, &server::log_msg)
25 { 53 {
26 this->fd = fd; 54 this->fd = fd;
27 read_ev.start (fd, EVENT_READ); 55 read_ev.start (fd, EVENT_READ);
28 } 56 }
29 57
39}; 67};
40 68
41unix_listener::unix_listener (const char *sockname) 69unix_listener::unix_listener (const char *sockname)
42: accept_ev (this, &unix_listener::accept_cb) 70: accept_ev (this, &unix_listener::accept_cb)
43{ 71{
44 if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) 72 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0)
45 { 73 {
46 perror ("unable to create listening socket"); 74 perror ("unable to create listening socket");
47 exit (EXIT_FAILURE); 75 exit (EXIT_FAILURE);
48 } 76 }
49 77
78 fcntl (fd, F_SETFD, FD_CLOEXEC);
79
50 sockaddr_un sa; 80 sockaddr_un sa;
51 81
52 sa.sun_family = AF_UNIX; 82 sa.sun_family = AF_UNIX;
53 strcpy (sa.sun_path, sockname); 83 strcpy (sa.sun_path, sockname);
54 84
76void unix_listener::accept_cb (io_watcher &w, short revents) 106void unix_listener::accept_cb (io_watcher &w, short revents)
77{ 107{
78 int fd2 = accept (fd, 0, 0); 108 int fd2 = accept (fd, 0, 0);
79 109
80 if (fd2 >= 0) 110 if (fd2 >= 0)
111 {
112 fcntl (fd2, F_SETFD, FD_CLOEXEC);
81 new server (fd2); 113 new server (fd2);
114 }
115}
116
117void server::log_msg (const char *msg)
118{
119 send ("MSG"), send (msg);
82} 120}
83 121
84void server::err (const char *format, ...) 122void server::err (const char *format, ...)
85{ 123{
86 if (format) 124 if (format)
90 va_list ap; 128 va_list ap;
91 va_start (ap, format); 129 va_start (ap, format);
92 vsnprintf (err, 1024, format, ap); 130 vsnprintf (err, 1024, format, ap);
93 va_end (ap); 131 va_end (ap);
94 132
95 send ("ERR"), send (err); 133 send ("MSG"), send (err);
96 } 134 }
97 135
136 send ("END", 0);
98 close (fd); 137 close (fd);
99 delete this; 138 delete this;
100} 139}
101 140
102void server::read_cb (io_watcher &w, short revents) 141void server::read_cb (io_watcher &w, short revents)
105 144
106 if (recv (tok)) 145 if (recv (tok))
107 { 146 {
108 if (!strcmp (tok, "NEW")) 147 if (!strcmp (tok, "NEW"))
109 { 148 {
110 stringvec argv; 149 stringvec *argv = new stringvec;
111 stringvec envv; 150 stringvec *envv = new stringvec;
112 151
113 for (;;) 152 for (;;)
114 { 153 {
115 if (!recv (tok)) 154 if (!recv (tok))
116 return err (); 155 return err ();
117 156
118 if (!strcmp (tok, "END")) 157 if (!strcmp (tok, "END"))
119 break; 158 break;
120 else if (!strcmp (tok, "ENV") && recv (tok)) 159 else if (!strcmp (tok, "ENV") && recv (tok))
121 envv.push_back (tok.get ()); 160 envv->push_back (tok.get ());
122 else if (!strcmp (tok, "CWD") && recv (tok)) 161 else if (!strcmp (tok, "CWD") && recv (tok))
123 { 162 {
124 if (chdir (tok)) 163 if (chdir (tok))
125 err ("unable to change to working directory to '%s': %s", 164 err ("unable to change to working directory to '%s': %s",
126 (char *)tok, strerror (errno)); 165 (char *)tok, strerror (errno));
127 } 166 }
128 else if (!strcmp (tok, "ARG") && recv (tok)) 167 else if (!strcmp (tok, "ARG") && recv (tok))
129 argv.push_back (tok.get ()); 168 argv->push_back (tok.get ());
130 else 169 else
131 return err ("protocol error: unexpected NEW token"); 170 return err ("protocol error: unexpected NEW token");
132 } 171 }
133 172
134 envv.push_back (0); 173 envv->push_back (0);
135 174
136 { 175 {
137 char **old_environ = environ; 176 char **old_environ = environ;
138 environ = envv.begin (); 177 environ = envv->begin ();
139 178
179 rxvt_term *term = new rxvt_term;
180
181 term->log_hook = &log_cb;
182 term->argv = argv;
183 term->envv = envv;
184
185 bool success;
186
187 try
188 {
140 rxvt_init (argv.size (), argv.begin ()); 189 success = term->init (argv->size (), argv->begin ());
190 }
191 catch (const class rxvt_failure_exception &e)
192 {
193 success = false;
194 }
195
196 term->log_hook = 0;
141 197
142 environ = old_environ; 198 environ = old_environ;
143 envv.clear (); // can't yet save the env 'cause rxvt modifies it :( 199 chdir ("/");
200
201 if (!success)
202 term->destroy ();
203
204 send ("END"); send (success ? 1 : 0);
144 } 205 }
145 } 206 }
146 else 207 else
147 return err ("protocol error: request '%s' unsupported.", (char *)tok); 208 return err ("protocol error: request '%s' unsupported.", (char *)tok);
148 } 209 }
149 else 210 else
150 return err (); 211 return err ();
151} 212}
152 213
214int opt_fork, opt_opendisplay, opt_quiet;
215
153int 216int
154main(int argc, const char *const *argv) 217main (int argc, const char *const *argv)
155{ 218{
219 for (int i = 1; i < argc; i++)
220 {
221 if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork"))
222 opt_fork = 1;
223 else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay"))
224 opt_opendisplay = 1;
225 else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet"))
226 opt_quiet = 1;
227 else
228 {
229 rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]);
230 return EXIT_FAILURE;
231 }
232 }
156 { 233
157 sigset_t ss;
158
159 sigaddset (&ss, SIGHUP);
160 sigaddset (&ss, SIGPIPE);
161 sigprocmask (SIG_BLOCK, &ss, 0);
162 }
163
164 rxvt_init_signals (); 234 rxvt_init ();
235
236 chdir ("/");
237
238 if (opt_opendisplay)
239 displays.get (getenv ("DISPLAY")); // open display and never release it
165 240
166 char *sockname = rxvt_connection::unix_sockname (); 241 char *sockname = rxvt_connection::unix_sockname ();
167 unix_listener l (sockname); 242 unix_listener l (sockname);
243
244 if (!opt_quiet)
245 {
168 printf ("rxvtd listening on %s.\n", sockname); 246 printf ("rxvt-unicode daemon listening on %s.\n", sockname);
247 fflush (stdout);
248 }
249
169 free (sockname); 250 free (sockname);
170 251
171 iom.loop (); 252 if (opt_fork)
253 {
254 pid_t pid = fork ();
172 255
173#if 0 256 if (pid < 0)
174 if (rxvt_init(argc, argv) == NULL) 257 {
258 rxvt_log ("unable to fork daemon, aborting.\n");
175 return EXIT_FAILURE; 259 return EXIT_FAILURE;
260 }
261 else if (pid > 0)
262 _exit (EXIT_SUCCESS);
263 }
176 264
177 dR; 265 io_manager::loop ();
178 rxvt_main_loop(aR); /* main processing loop */ 266
179#endif
180 return EXIT_SUCCESS; 267 return EXIT_SUCCESS;
181} 268}
269

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines