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