|
|
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 | |
18 | extern char **environ; |
42 | extern char **environ; |
19 | |
43 | |
20 | struct server : rxvt_connection { |
44 | struct 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 | |
41 | unix_listener::unix_listener (const char *sockname) |
69 | unix_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 | |
… | |
… | |
76 | void unix_listener::accept_cb (io_watcher &w, short revents) |
106 | void 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 | |
|
|
117 | void server::log_msg (const char *msg) |
|
|
118 | { |
|
|
119 | send ("MSG"), send (msg); |
82 | } |
120 | } |
83 | |
121 | |
84 | void server::err (const char *format, ...) |
122 | void 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 | |
102 | void server::read_cb (io_watcher &w, short revents) |
141 | void 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 | |
|
|
200 | if (!success) |
|
|
201 | term->destroy (); |
|
|
202 | |
|
|
203 | send ("END"); send (success ? 1 : 0); |
144 | } |
204 | } |
145 | } |
205 | } |
146 | else |
206 | else |
147 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
207 | return err ("protocol error: request '%s' unsupported.", (char *)tok); |
148 | } |
208 | } |
149 | else |
209 | else |
150 | return err (); |
210 | return err (); |
151 | } |
211 | } |
152 | |
212 | |
153 | int |
213 | int |
154 | main(int argc, const char *const *argv) |
214 | main (int argc, const char *const *argv) |
155 | { |
215 | { |
156 | { |
|
|
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 (); |
216 | rxvt_init (); |
165 | |
217 | |
166 | char *sockname = rxvt_connection::unix_sockname (); |
218 | char *sockname = rxvt_connection::unix_sockname (); |
167 | unix_listener l (sockname); |
219 | unix_listener l (sockname); |
168 | printf ("rxvtd listening on %s.\n", sockname); |
220 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
169 | free (sockname); |
221 | free (sockname); |
170 | |
222 | |
171 | iom.loop (); |
223 | iom.loop (); |
172 | |
224 | |
173 | #if 0 |
|
|
174 | if (rxvt_init(argc, argv) == NULL) |
|
|
175 | return EXIT_FAILURE; |
|
|
176 | |
|
|
177 | dR; |
|
|
178 | rxvt_main_loop(aR); /* main processing loop */ |
|
|
179 | #endif |
|
|
180 | return EXIT_SUCCESS; |
225 | return EXIT_SUCCESS; |
181 | } |
226 | } |
|
|
227 | |