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.1 by pcg, Mon Nov 24 17:28:08 2003 UTC vs.
Revision 1.16 by root, Sun Aug 15 02:17:32 2004 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>
30#include <cstdarg>
31#include <cstring>
7 32
8#include <unistd.h> 33#include <unistd.h>
34#include <fcntl.h>
35#include <sys/types.h>
36#include <sys/stat.h>
9#include <sys/socket.h> 37#include <sys/socket.h>
10#include <sys/un.h> 38#include <sys/un.h>
11 39
40#include <cerrno>
41
42extern char **environ;
43
12struct server : rxvt_connection { 44struct server : rxvt_connection {
45 log_callback log_cb;
46
13 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);
14 49
15 server (int fd) 50 server (int fd)
16 : read_ev (this, &server::read_cb) 51 : read_ev (this, &server::read_cb),
52 log_cb (this, &server::log_msg)
17 { 53 {
18 this->fd = fd; 54 this->fd = fd;
19 read_ev.start (fd, EVENT_READ); 55 read_ev.start (fd, EVENT_READ);
20 } 56 }
21 57
22 void err (); 58 void err (const char *format = 0, ...);
23}; 59};
24 60
25struct listener { 61struct unix_listener {
26 int fd; 62 int fd;
27 63
28 void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; 64 void accept_cb (io_watcher &w, short revents); io_watcher accept_ev;
29 65
30 listener (); 66 unix_listener (const char *sockname);
31}; 67};
32 68
33listener::listener () 69unix_listener::unix_listener (const char *sockname)
34: accept_ev (this, &listener::accept_cb) 70: accept_ev (this, &unix_listener::accept_cb)
35{ 71{
36 if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) 72 if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0)
37 { 73 {
38 perror ("unable to create listening socket"); 74 perror ("unable to create listening socket");
39 exit (EXIT_FAILURE); 75 exit (EXIT_FAILURE);
40 } 76 }
41 77
78 fcntl (fd, F_SETFD, FD_CLOEXEC);
79
42 sockaddr_un sa; 80 sockaddr_un sa;
43 81
44 sa.sun_family = AF_UNIX; 82 sa.sun_family = AF_UNIX;
45 strcpy (sa.sun_path, rxvt_connection::unix_sockname ()); 83 strcpy (sa.sun_path, sockname);
46 84
47 unlink (rxvt_connection::unix_sockname ()); 85 unlink (rxvt_connection::unix_sockname ());
86
87 mode_t omask = umask (0077);
48 88
49 if (bind (fd, (sockaddr *)&sa, sizeof (sa))) 89 if (bind (fd, (sockaddr *)&sa, sizeof (sa)))
50 { 90 {
51 perror ("unable to bind listening socket"); 91 perror ("unable to bind listening socket");
52 exit (EXIT_FAILURE); 92 exit (EXIT_FAILURE);
53 } 93 }
54 94
95 umask (omask);
96
55 if (listen (fd, 5)) 97 if (listen (fd, 5))
56 { 98 {
57 perror ("unable to listen on socket"); 99 perror ("unable to listen on socket");
58 exit (EXIT_FAILURE); 100 exit (EXIT_FAILURE);
59 } 101 }
60 102
61 accept_ev.start (fd, EVENT_READ); 103 accept_ev.start (fd, EVENT_READ);
62} 104}
63 105
64void listener::accept_cb (io_watcher &w, short revents) 106void unix_listener::accept_cb (io_watcher &w, short revents)
65{ 107{
66 int fd2 = accept (fd, 0, 0); 108 int fd2 = accept (fd, 0, 0);
67 109
68 if (fd2 >= 0) 110 if (fd2 >= 0)
111 {
112 fcntl (fd2, F_SETFD, FD_CLOEXEC);
69 new server (fd2); 113 new server (fd2);
114 }
70} 115}
71 116
72void server::err () 117void server::log_msg (const char *msg)
73{ 118{
119 send ("MSG"), send (msg);
120}
121
122void server::err (const char *format, ...)
123{
124 if (format)
125 {
126 char err[1024];
127
128 va_list ap;
129 va_start (ap, format);
130 vsnprintf (err, 1024, format, ap);
131 va_end (ap);
132
133 send ("MSG"), send (err);
134 }
135
136 send ("END", 0);
74 close (fd); 137 close (fd);
75 delete this; 138 delete this;
76} 139}
77 140
78void server::read_cb (io_watcher &w, short revents) 141void server::read_cb (io_watcher &w, short revents)
79{ 142{
80 token cmd; 143 auto_str tok;
81 144
82 if (recv (cmd)) 145 if (recv (tok))
83 { 146 {
84 if (!strcmp (cmd, "NEW")) 147 if (!strcmp (tok, "NEW"))
85 { 148 {
149 stringvec *argv = new stringvec;
150 stringvec *envv = new stringvec;
151
152 for (;;)
153 {
154 if (!recv (tok))
155 return err ();
156
157 if (!strcmp (tok, "END"))
158 break;
159 else if (!strcmp (tok, "ENV") && recv (tok))
160 envv->push_back (tok.get ());
161 else if (!strcmp (tok, "CWD") && recv (tok))
162 {
163 if (chdir (tok))
164 err ("unable to change to working directory to '%s': %s",
165 (char *)tok, strerror (errno));
166 }
167 else if (!strcmp (tok, "ARG") && recv (tok))
168 argv->push_back (tok.get ());
169 else
170 return err ("protocol error: unexpected NEW token");
171 }
172
173 envv->push_back (0);
174
175 {
176 char **old_environ = environ;
177 environ = envv->begin ();
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 {
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;
197
198 environ = old_environ;
199
200 if (!success)
201 term->destroy ();
202
203 send ("END"); send (success ? 1 : 0);
204 }
86 } 205 }
87 else 206 else
88 err (); 207 return err ("protocol error: request '%s' unsupported.", (char *)tok);
89 } 208 }
90 else 209 else
91 err (); 210 return err ();
92} 211}
93 212
94int 213int
95main(int argc, const char *const *argv) 214main (int argc, const char *const *argv)
96{ 215{
97 listener l; 216 rxvt_init ();
217
218 char *sockname = rxvt_connection::unix_sockname ();
219 unix_listener l (sockname);
220 printf ("rxvtd listening on %s.\n", sockname);
221 free (sockname);
222
98 iom.loop (); 223 iom.loop ();
99 224
100#if 0
101 if (rxvt_init(argc, argv) == NULL)
102 return EXIT_FAILURE;
103
104 dR;
105 rxvt_main_loop(aR); /* main processing loop */
106#endif
107 return EXIT_SUCCESS; 225 return EXIT_SUCCESS;
108} 226}
227

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines