1 | /*----------------------------------------------------------------------* |
1 | /*----------------------------------------------------------------------* |
2 | * File: rxvtd.C |
2 | * File: rxvtd.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * |
4 | * |
5 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
6 | * Copyright (c) 2003-2007 Marc Lehmann <pcg@goof.com> |
6 | * Copyright (c) 2003-2007 Marc Lehmann <schmorp@schmorp.de> |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
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 |
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 |
10 | * the Free Software Foundation; either version 2 of the License, or |
11 | * (at your option) any later version. |
11 | * (at your option) any later version. |
… | |
… | |
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" |
… | |
… | |
92 | fcntl (fd, F_SETFL, O_NONBLOCK); |
100 | fcntl (fd, F_SETFL, O_NONBLOCK); |
93 | |
101 | |
94 | sa.sun_family = AF_UNIX; |
102 | sa.sun_family = AF_UNIX; |
95 | strcpy (sa.sun_path, sockname); |
103 | strcpy (sa.sun_path, sockname); |
96 | |
104 | |
97 | unlink (rxvt_connection::unix_sockname ()); |
105 | unlink (sockname); |
98 | |
106 | |
99 | mode_t omask = umask (0077); |
107 | mode_t omask = umask (0077); |
100 | |
108 | |
101 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
109 | if (bind (fd, (sockaddr *)&sa, sizeof (sa))) |
102 | { |
110 | { |
… | |
… | |
214 | } |
222 | } |
215 | else |
223 | else |
216 | return err (); |
224 | return err (); |
217 | } |
225 | } |
218 | |
226 | |
219 | int opt_fork, opt_opendisplay, opt_quiet; |
|
|
220 | |
|
|
221 | int |
227 | int |
222 | main (int argc, const char *const *argv) |
228 | main (int argc, const char *const *argv) |
223 | { |
229 | { |
224 | rxvt_init (); |
230 | ptytty::init (); |
|
|
231 | |
|
|
232 | static char opt_fork, opt_opendisplay, opt_quiet; |
|
|
233 | #if ENABLE_MLOCK |
|
|
234 | static char opt_lock; |
|
|
235 | #endif |
225 | |
236 | |
226 | for (int i = 1; i < argc; i++) |
237 | for (int i = 1; i < argc; i++) |
227 | { |
238 | { |
228 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
239 | if (!strcmp (argv [i], "-f") || !strcmp (argv [i], "--fork")) |
229 | opt_fork = 1; |
240 | opt_fork = 1; |
230 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
241 | else if (!strcmp (argv [i], "-o") || !strcmp (argv [i], "--opendisplay")) |
231 | opt_opendisplay = 1; |
242 | opt_opendisplay = 1; |
232 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
243 | else if (!strcmp (argv [i], "-q") || !strcmp (argv [i], "--quiet")) |
233 | 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 |
234 | else |
249 | else |
235 | { |
250 | { |
236 | 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]); |
237 | return EXIT_FAILURE; |
252 | return EXIT_FAILURE; |
238 | } |
253 | } |
239 | } |
254 | } |
240 | |
255 | |
|
|
256 | rxvt_init (); |
|
|
257 | |
241 | // optionally open display and never release it. |
258 | // optionally open display and never release it. |
242 | if (opt_opendisplay) |
259 | if (opt_opendisplay) |
243 | if (const char *dpy = getenv ("DISPLAY")) |
260 | if (const char *dpy = getenv ("DISPLAY")) |
244 | displays.get (dpy ? dpy : ":0"); // move string logic into rxvt_display maybe? |
261 | displays.get (dpy ? dpy : ":0"); // move string logic into rxvt_display maybe? |
245 | |
262 | |
… | |
… | |
254 | fflush (stdout); |
271 | fflush (stdout); |
255 | } |
272 | } |
256 | |
273 | |
257 | free (sockname); |
274 | free (sockname); |
258 | |
275 | |
|
|
276 | pid_t pid = 0; |
259 | if (opt_fork) |
277 | if (opt_fork) |
260 | { |
278 | { |
261 | pid_t pid = fork (); |
279 | pid = fork (); |
|
|
280 | } |
262 | |
281 | |
|
|
282 | #if ENABLE_MLOCK |
|
|
283 | // Optionally perform an mlockall so this process does not get swapped out. |
|
|
284 | if (opt_lock && !pid) |
|
|
285 | if (mlockall (MCL_CURRENT | MCL_FUTURE) < 0) |
|
|
286 | perror ("unable to lock into ram"); |
|
|
287 | #endif |
|
|
288 | |
|
|
289 | if (opt_fork) |
|
|
290 | { |
263 | if (pid < 0) |
291 | if (pid < 0) |
264 | { |
292 | { |
265 | rxvt_log ("unable to fork daemon, aborting.\n"); |
293 | rxvt_log ("unable to fork daemon, aborting.\n"); |
266 | return EXIT_FAILURE; |
294 | return EXIT_FAILURE; |
267 | } |
295 | } |
268 | else if (pid > 0) |
296 | else if (pid > 0) |
269 | _exit (EXIT_SUCCESS); |
297 | _exit (EXIT_SUCCESS); |
270 | |
298 | |
271 | ev_default_fork (); |
299 | ev_loop_fork (EV_DEFAULT_UC); |
272 | } |
300 | } |
273 | |
301 | |
274 | ev_loop (0); |
302 | ev_run (); |
275 | |
303 | |
276 | return EXIT_SUCCESS; |
304 | return EXIT_SUCCESS; |
277 | } |
305 | } |
278 | |
306 | |