1 |
#include "../config.h" |
2 |
#include "rxvt.h" |
3 |
#include "rxvtdaemon.h" |
4 |
#include "iom.h" |
5 |
|
6 |
#include <cstdio> |
7 |
#include <cstdlib> |
8 |
#include <cstdarg> |
9 |
#include <cstring> |
10 |
|
11 |
#include <unistd.h> |
12 |
#include <sys/types.h> |
13 |
#include <sys/stat.h> |
14 |
#include <sys/socket.h> |
15 |
#include <sys/un.h> |
16 |
|
17 |
#include <cerrno> |
18 |
|
19 |
extern char **environ; |
20 |
|
21 |
struct server : rxvt_connection { |
22 |
log_callback log_cb; |
23 |
|
24 |
void read_cb (io_watcher &w, short revents); io_watcher read_ev; |
25 |
void log_msg (const char *msg); |
26 |
|
27 |
server (int fd) |
28 |
: read_ev (this, &server::read_cb), |
29 |
log_cb (this, &server::log_msg) |
30 |
{ |
31 |
this->fd = fd; |
32 |
read_ev.start (fd, EVENT_READ); |
33 |
} |
34 |
|
35 |
void err (const char *format = 0, ...); |
36 |
}; |
37 |
|
38 |
struct unix_listener { |
39 |
int fd; |
40 |
|
41 |
void accept_cb (io_watcher &w, short revents); io_watcher accept_ev; |
42 |
|
43 |
unix_listener (const char *sockname); |
44 |
}; |
45 |
|
46 |
unix_listener::unix_listener (const char *sockname) |
47 |
: accept_ev (this, &unix_listener::accept_cb) |
48 |
{ |
49 |
if ((fd = socket (PF_LOCAL, SOCK_STREAM, 0)) < 0) |
50 |
{ |
51 |
perror ("unable to create listening socket"); |
52 |
exit (EXIT_FAILURE); |
53 |
} |
54 |
|
55 |
sockaddr_un sa; |
56 |
|
57 |
sa.sun_family = AF_UNIX; |
58 |
strcpy (sa.sun_path, sockname); |
59 |
|
60 |
unlink (rxvt_connection::unix_sockname ()); |
61 |
|
62 |
mode_t omask = umask (0077); |
63 |
|
64 |
if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
65 |
{ |
66 |
perror ("unable to bind listening socket"); |
67 |
exit (EXIT_FAILURE); |
68 |
} |
69 |
|
70 |
umask (omask); |
71 |
|
72 |
if (listen (fd, 5)) |
73 |
{ |
74 |
perror ("unable to listen on socket"); |
75 |
exit (EXIT_FAILURE); |
76 |
} |
77 |
|
78 |
accept_ev.start (fd, EVENT_READ); |
79 |
} |
80 |
|
81 |
void unix_listener::accept_cb (io_watcher &w, short revents) |
82 |
{ |
83 |
int fd2 = accept (fd, 0, 0); |
84 |
|
85 |
if (fd2 >= 0) |
86 |
new server (fd2); |
87 |
} |
88 |
|
89 |
void server::log_msg (const char *msg) |
90 |
{ |
91 |
send ("MSG"), send (msg); |
92 |
} |
93 |
|
94 |
void server::err (const char *format, ...) |
95 |
{ |
96 |
if (format) |
97 |
{ |
98 |
char err[1024]; |
99 |
|
100 |
va_list ap; |
101 |
va_start (ap, format); |
102 |
vsnprintf (err, 1024, format, ap); |
103 |
va_end (ap); |
104 |
|
105 |
send ("MSG"), send (err); |
106 |
} |
107 |
|
108 |
send ("END", 0); |
109 |
close (fd); |
110 |
delete this; |
111 |
} |
112 |
|
113 |
void server::read_cb (io_watcher &w, short revents) |
114 |
{ |
115 |
auto_str tok; |
116 |
|
117 |
if (recv (tok)) |
118 |
{ |
119 |
if (!strcmp (tok, "NEW")) |
120 |
{ |
121 |
stringvec *argv = new stringvec; |
122 |
stringvec *envv = new stringvec; |
123 |
|
124 |
for (;;) |
125 |
{ |
126 |
if (!recv (tok)) |
127 |
return err (); |
128 |
|
129 |
if (!strcmp (tok, "END")) |
130 |
break; |
131 |
else if (!strcmp (tok, "ENV") && recv (tok)) |
132 |
envv->push_back (tok.get ()); |
133 |
else if (!strcmp (tok, "CWD") && recv (tok)) |
134 |
{ |
135 |
if (chdir (tok)) |
136 |
err ("unable to change to working directory to '%s': %s", |
137 |
(char *)tok, strerror (errno)); |
138 |
} |
139 |
else if (!strcmp (tok, "ARG") && recv (tok)) |
140 |
argv->push_back (tok.get ()); |
141 |
else |
142 |
return err ("protocol error: unexpected NEW token"); |
143 |
} |
144 |
|
145 |
envv->push_back (0); |
146 |
|
147 |
{ |
148 |
char **old_environ = environ; |
149 |
environ = envv->begin (); |
150 |
|
151 |
rxvt_term *term = new rxvt_term; |
152 |
|
153 |
term->log_hook = &log_cb; |
154 |
term->argv = argv; |
155 |
term->envv = envv; |
156 |
|
157 |
bool success; |
158 |
|
159 |
try |
160 |
{ |
161 |
success = term->init (argv->size (), argv->begin ()); |
162 |
} |
163 |
catch (const class rxvt_failure_exception &e) |
164 |
{ |
165 |
success = false; |
166 |
} |
167 |
|
168 |
environ = old_environ; |
169 |
|
170 |
if (!success) |
171 |
term->destroy (); |
172 |
|
173 |
send ("END"); send (success ? 1 : 0); |
174 |
} |
175 |
} |
176 |
else |
177 |
return err ("protocol error: request '%s' unsupported.", (char *)tok); |
178 |
} |
179 |
else |
180 |
return err (); |
181 |
} |
182 |
|
183 |
int |
184 |
main (int argc, const char *const *argv) |
185 |
{ |
186 |
rxvt_init_signals (); |
187 |
|
188 |
char *sockname = rxvt_connection::unix_sockname (); |
189 |
unix_listener l (sockname); |
190 |
printf ("rxvtd listening on %s.\n", sockname); |
191 |
free (sockname); |
192 |
|
193 |
iom.loop (); |
194 |
|
195 |
#if 0 |
196 |
if (rxvt_init (argc, argv) == NULL) |
197 |
return EXIT_FAILURE; |
198 |
|
199 |
dR; |
200 |
rxvt_main_loop (aR); /* main processing loop */ |
201 |
#endif |
202 |
return EXIT_SUCCESS; |
203 |
} |
204 |
|