… | |
… | |
31 | #include <sys/types.h> |
31 | #include <sys/types.h> |
32 | #include <sys/stat.h> |
32 | #include <sys/stat.h> |
33 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
34 | #include <sys/un.h> |
34 | #include <sys/un.h> |
35 | |
35 | |
|
|
36 | #if defined(ENABLE_FRILLS) && defined(_POSIX_MEMLOCK) && _POSIX_MEMLOCK > 0 |
|
|
37 | # define ENABLE_MLOCK 1 |
|
|
38 | #endif |
|
|
39 | |
|
|
40 | #if ENABLE_MLOCK |
|
|
41 | # include <sys/mman.h> |
|
|
42 | #endif |
|
|
43 | |
36 | #include <cerrno> |
44 | #include <cerrno> |
37 | |
45 | |
38 | #include "rxvt.h" |
46 | #include "rxvt.h" |
39 | #include "rxvtdaemon.h" |
47 | #include "rxvtdaemon.h" |
40 | #include "libptytty.h" |
48 | #include "libptytty.h" |
… | |
… | |
172 | |
180 | |
173 | if (!strcmp (tok, "END")) |
181 | if (!strcmp (tok, "END")) |
174 | break; |
182 | break; |
175 | else if (!strcmp (tok, "ENV") && recv (tok)) |
183 | else if (!strcmp (tok, "ENV") && recv (tok)) |
176 | envv->push_back (strdup (tok)); |
184 | envv->push_back (strdup (tok)); |
177 | else if (!strcmp (tok, "CWD") && recv (tok)) |
|
|
178 | { |
|
|
179 | if (chdir (tok)) |
|
|
180 | { |
|
|
181 | delete envv; |
|
|
182 | delete argv; |
|
|
183 | return err ("unable to change to working directory to '%s', aborting: %s.\n", |
|
|
184 | (char *)tok, strerror (errno)); |
|
|
185 | } |
|
|
186 | } |
|
|
187 | else if (!strcmp (tok, "ARG") && recv (tok)) |
185 | else if (!strcmp (tok, "ARG") && recv (tok)) |
188 | argv->push_back (strdup (tok)); |
186 | argv->push_back (strdup (tok)); |
189 | else |
187 | else |
190 | return err ("protocol error: unexpected NEW token.\n"); |
188 | return err ("protocol error: unexpected NEW token.\n"); |
191 | } |
189 | } |
… | |
… | |
209 | success = false; |
207 | success = false; |
210 | } |
208 | } |
211 | |
209 | |
212 | term->log_hook = 0; |
210 | term->log_hook = 0; |
213 | |
211 | |
214 | chdir ("/"); |
212 | chdir ("/"); // init might change to different working directory |
215 | |
213 | |
216 | if (!success) |
214 | if (!success) |
217 | term->destroy (); |
215 | term->destroy (); |
218 | |
216 | |
219 | send ("END"); send (success ? 1 : 0); |
217 | send ("END"); send (success ? 1 : 0); |
… | |
… | |
224 | } |
222 | } |
225 | else |
223 | else |
226 | return err (); |
224 | return err (); |
227 | } |
225 | } |
228 | |
226 | |
229 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
230 | |
|
|
231 | int |
227 | int |
232 | main (int argc, const char *const *argv) |
228 | main (int argc, const char *const *argv) |
233 | { |
229 | { |
|
|
230 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
231 | #if ENABLE_MLOCK |
|
|
232 | int opt_lock; |
|
|
233 | #endif |
|
|
234 | |
234 | rxvt_init (); |
235 | rxvt_init (); |
235 | |
236 | |
236 | for (int i = 1; i < argc; i++) |
237 | for (int i = 1; i < argc; i++) |
237 | { |
238 | { |
238 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
239 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
239 | opt_fork = 1; |
240 | opt_fork = 1; |
240 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
241 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
241 | opt_opendisplay = 1; |
242 | opt_opendisplay = 1; |
242 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
243 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
243 | opt_quiet = 1; |
244 | opt_quiet = 1; |
|
|
245 | #if ENABLE_MLOCK |
|
|
246 | else if (!strcmp (argv [i], "-m") || !strcmp (argv [i], "--mlock")) |
|
|
247 | opt_lock = 1; |
|
|
248 | #endif |
244 | else |
249 | else |
245 | { |
250 | { |
246 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
251 | rxvt_log ("%s: unknown option '%s', aborting.\n", argv [0], argv [i]); |
247 | return EXIT_FAILURE; |
252 | return EXIT_FAILURE; |
248 | } |
253 | } |
249 | } |
254 | } |
250 | |
255 | |
251 | chdir ("/"); |
256 | // optionally open display and never release it. |
252 | |
|
|
253 | if (opt_opendisplay) |
257 | if (opt_opendisplay) |
254 | displays.get (getenv ("DISPLAY")); // open display and never release it |
258 | if (const char *dpy = getenv ("DISPLAY")) |
|
|
259 | displays.get (dpy ? dpy : ":0"); // move string logic into rxvt_display maybe? |
255 | |
260 | |
256 | char *sockname = rxvt_connection::unix_sockname (); |
261 | char *sockname = rxvt_connection::unix_sockname (); |
257 | unix_listener l (sockname); |
262 | unix_listener l (sockname); |
258 | |
263 | |
|
|
264 | chdir ("/"); |
|
|
265 | |
259 | if (!opt_quiet) |
266 | if (!opt_quiet) |
260 | { |
267 | { |
261 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
268 | printf ("rxvt-unicode daemon listening on %s.\n", sockname); |
262 | fflush (stdout); |
269 | fflush (stdout); |
263 | } |
270 | } |
264 | |
271 | |
265 | free (sockname); |
272 | free (sockname); |
266 | |
273 | |
|
|
274 | pid_t pid = 0; |
267 | if (opt_fork) |
275 | if (opt_fork) |
268 | { |
276 | { |
269 | pid_t pid = fork (); |
277 | pid = fork (); |
|
|
278 | } |
270 | |
279 | |
|
|
280 | #if ENABLE_MLOCK |
|
|
281 | // Optionally perform an mlockall so this process does not get swapped out. |
|
|
282 | if (opt_lock && !pid) |
|
|
283 | if (mlockall (MCL_CURRENT | MCL_FUTURE) < 0) |
|
|
284 | perror ("unable to lock into ram"); |
|
|
285 | #endif |
|
|
286 | |
|
|
287 | if (opt_fork) |
|
|
288 | { |
271 | if (pid < 0) |
289 | if (pid < 0) |
272 | { |
290 | { |
273 | rxvt_log ("unable to fork daemon, aborting.\n"); |
291 | rxvt_log ("unable to fork daemon, aborting.\n"); |
274 | return EXIT_FAILURE; |
292 | return EXIT_FAILURE; |
275 | } |
293 | } |
276 | else if (pid > 0) |
294 | else if (pid > 0) |
277 | _exit (EXIT_SUCCESS); |
295 | _exit (EXIT_SUCCESS); |
278 | |
296 | |
279 | ev_default_fork (); |
297 | ev_loop_fork (EV_DEFAULT_UC); |
280 | } |
298 | } |
281 | |
299 | |
282 | ev_loop (0); |
300 | ev_run (); |
283 | |
301 | |
284 | return EXIT_SUCCESS; |
302 | return EXIT_SUCCESS; |
285 | } |
303 | } |
286 | |
304 | |