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" |
24 |
#include "rxvt.h" |
25 |
#include "rxvtdaemon.h" |
26 |
#include "iom.h" |
27 |
|
28 |
#include <cstdio> |
29 |
#include <cstdlib> |
30 |
#include <cstdarg> |
31 |
#include <cstring> |
32 |
|
33 |
#include <unistd.h> |
34 |
#include <fcntl.h> |
35 |
#include <sys/types.h> |
36 |
#include <sys/stat.h> |
37 |
#include <sys/socket.h> |
38 |
#include <sys/un.h> |
39 |
|
40 |
#include <cerrno> |
41 |
|
42 |
extern char **environ; |
43 |
|
44 |
struct server : rxvt_connection { |
45 |
log_callback log_cb; |
46 |
|
47 |
void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
48 |
void log_msg (const char *msg); |
49 |
|
50 |
server (int fd) |
51 |
: read_ev (this, &server::read_cb), |
52 |
log_cb (this, &server::log_msg) |
53 |
{ |
54 |
this->fd = fd; |
55 |
read_ev.start (fd, EVENT_READ); |
56 |
} |
57 |
|
58 |
void err (const char *format = 0, ...); |
59 |
}; |
60 |
|
61 |
struct unix_listener { |
62 |
int fd; |
63 |
|
64 |
void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
65 |
|
66 |
unix_listener (const char *sockname); |
67 |
}; |
68 |
|
69 |
unix_listener::unix_listener (const char *sockname) |
70 |
: accept_ev (this, &unix_listener::accept_cb) |
71 |
{ |
72 |
if ((fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0) |
73 |
{ |
74 |
perror ("unable to create listening socket"); |
75 |
exit (EXIT_FAILURE); |
76 |
} |
77 |
|
78 |
fcntl (fd, F_SETFD, FD_CLOEXEC); |
79 |
|
80 |
sockaddr_un sa; |
81 |
|
82 |
sa.sun_family = AF_UNIX; |
83 |
strcpy (sa.sun_path, sockname); |
84 |
|
85 |
unlink (rxvt_connection::unix_sockname ()); |
86 |
|
87 |
mode_t omask = umask (0077); |
88 |
|
89 |
if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
90 |
{ |
91 |
perror ("unable to bind listening socket"); |
92 |
exit (EXIT_FAILURE); |
93 |
} |
94 |
|
95 |
umask (omask); |
96 |
|
97 |
if (listen (fd, 5)) |
98 |
{ |
99 |
perror ("unable to listen on socket"); |
100 |
exit (EXIT_FAILURE); |
101 |
} |
102 |
|
103 |
accept_ev.start (fd, EVENT_READ); |
104 |
} |
105 |
|
106 |
void unix_listener::accept_cb (io_watcher &w, short revents) |
107 |
{ |
108 |
int fd2 = accept (fd, 0, 0); |
109 |
|
110 |
if (fd2 >= 0) |
111 |
{ |
112 |
fcntl (fd2, F_SETFD, FD_CLOEXEC); |
113 |
new server (fd2); |
114 |
} |
115 |
} |
116 |
|
117 |
void server::log_msg (const char *msg) |
118 |
{ |
119 |
send ("MSG"), send (msg); |
120 |
} |
121 |
|
122 |
void 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); |
137 |
close (fd); |
138 |
delete this; |
139 |
} |
140 |
|
141 |
void server::read_cb (io_watcher &w, short revents) |
142 |
{ |
143 |
auto_str tok; |
144 |
|
145 |
if (recv (tok)) |
146 |
{ |
147 |
if (!strcmp (tok, "NEW")) |
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 |
environ = old_environ; |
197 |
|
198 |
if (!success) |
199 |
term->destroy (); |
200 |
|
201 |
send ("END"); send (success ? 1 : 0); |
202 |
} |
203 |
} |
204 |
else |
205 |
return err ("protocol error: request '%s' unsupported.", (char *)tok); |
206 |
} |
207 |
else |
208 |
return err (); |
209 |
} |
210 |
|
211 |
int |
212 |
main (int argc, const char *const *argv) |
213 |
{ |
214 |
rxvt_init_signals (); |
215 |
|
216 |
char *sockname = rxvt_connection::unix_sockname (); |
217 |
unix_listener l (sockname); |
218 |
printf ("rxvtd listening on %s.\n", sockname); |
219 |
free (sockname); |
220 |
|
221 |
iom.loop (); |
222 |
|
223 |
#if 0 |
224 |
if (rxvt_init (argc, argv) == NULL) |
225 |
return EXIT_FAILURE; |
226 |
|
227 |
dR; |
228 |
rxvt_main_loop (aR); /* main processing loop */ |
229 |
#endif |
230 |
return EXIT_SUCCESS; |
231 |
} |
232 |
|