Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 1 | /* upstart |
| 2 | * |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 3 | * Copyright © 2010 Canonical Ltd. |
Scott James Remnant | 7d5b2ea | 2009-05-22 15:20:12 +0200 | [diff] [blame] | 4 | * Author: Scott James Remnant <scott@netsplit.com>. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 5 | * |
Scott James Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2, as |
Scott James Remnant | 7512302 | 2009-05-22 13:27:56 +0200 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
Scott James Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | # include <config.h> |
| 22 | #endif /* HAVE_CONFIG_H */ |
| 23 | |
| 24 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 25 | #include <sys/types.h> |
Scott James Remnant | ea806b7 | 2006-10-18 15:01:00 +0100 | [diff] [blame] | 26 | #include <sys/time.h> |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 27 | #include <sys/wait.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/reboot.h> |
| 30 | #include <sys/resource.h> |
| 31 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <stdio.h> |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 34 | #include <signal.h> |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 35 | #include <stdlib.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 36 | #include <string.h> |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 37 | #include <syslog.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 38 | #include <unistd.h> |
| 39 | |
Scott James Remnant | 097b2a9 | 2006-09-01 19:30:37 +0100 | [diff] [blame] | 40 | #include <linux/kd.h> |
| 41 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 42 | #include <nih/macros.h> |
| 43 | #include <nih/alloc.h> |
| 44 | #include <nih/list.h> |
| 45 | #include <nih/timer.h> |
| 46 | #include <nih/signal.h> |
| 47 | #include <nih/child.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 48 | #include <nih/option.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 49 | #include <nih/main.h> |
Scott James Remnant | 28fcc92 | 2006-09-01 04:15:57 +0100 | [diff] [blame] | 50 | #include <nih/error.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 51 | #include <nih/logging.h> |
| 52 | |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 53 | #include "paths.h" |
Scott James Remnant | 5d82d90 | 2008-04-30 00:03:29 +0100 | [diff] [blame] | 54 | #include "events.h" |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 55 | #include "system.h" |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 56 | #include "job_process.h" |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 57 | #include "event.h" |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 58 | #include "conf.h" |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 59 | #include "control.h" |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 60 | |
| 61 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 62 | /* Prototypes for static functions */ |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 63 | #ifndef DEBUG |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 64 | static void crash_handler (int signum); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 65 | static void cad_handler (void *data, NihSignal *signal); |
| 66 | static void kbd_handler (void *data, NihSignal *signal); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 67 | static void pwr_handler (void *data, NihSignal *signal); |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 68 | static void hup_handler (void *data, NihSignal *signal); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 69 | static void usr1_handler (void *data, NihSignal *signal); |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 70 | #endif /* DEBUG */ |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 71 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 72 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 73 | /** |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 74 | * argv0: |
| 75 | * |
| 76 | * Path to program executed, used for re-executing the init binary from the |
| 77 | * same location we were executed from. |
| 78 | **/ |
| 79 | static const char *argv0 = NULL; |
| 80 | |
| 81 | /** |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 82 | * restart: |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 83 | * |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 84 | * This is set to TRUE if we're being re-exec'd by an existing init |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 85 | * process. |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 86 | **/ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 87 | static int restart = FALSE; |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 88 | |
| 89 | |
| 90 | /** |
| 91 | * options: |
| 92 | * |
| 93 | * Command-line options we accept. |
| 94 | **/ |
| 95 | static NihOption options[] = { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 96 | { 0, "restart", NULL, NULL, NULL, &restart, NULL }, |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 97 | |
| 98 | /* Ignore invalid options */ |
| 99 | { '-', "--", NULL, NULL, NULL, NULL, NULL }, |
| 100 | |
| 101 | NIH_OPTION_LAST |
| 102 | }; |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame] | 103 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 104 | |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 105 | int |
| 106 | main (int argc, |
| 107 | char *argv[]) |
| 108 | { |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 109 | char **args; |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 110 | int ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 111 | |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 112 | argv0 = argv[0]; |
| 113 | nih_main_init (argv0); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 114 | |
Scott James Remnant | 930e25a | 2006-10-13 12:28:05 +0100 | [diff] [blame] | 115 | nih_option_set_synopsis (_("Process management daemon.")); |
Scott James Remnant | 462734c | 2006-10-13 13:36:00 +0100 | [diff] [blame] | 116 | nih_option_set_help ( |
| 117 | _("This daemon is normally executed by the kernel and given " |
| 118 | "process id 1 to denote its special status. When executed " |
| 119 | "by a user process, it will actually run /sbin/telinit.")); |
Scott James Remnant | a6ed7eb | 2006-10-13 12:14:45 +0100 | [diff] [blame] | 120 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 121 | args = nih_option_parser (NULL, argc, argv, options, FALSE); |
| 122 | if (! args) |
| 123 | exit (1); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 124 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 125 | #ifndef DEBUG |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 126 | /* Check we're root */ |
| 127 | if (getuid ()) { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 128 | nih_fatal (_("Need to be root")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 129 | exit (1); |
| 130 | } |
| 131 | |
| 132 | /* Check we're process #1 */ |
| 133 | if (getpid () > 1) { |
Scott James Remnant | e0d0dd1 | 2006-09-14 10:51:05 +0100 | [diff] [blame] | 134 | execv (TELINIT, argv); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 135 | /* Ignore failure, probably just that telinit doesn't exist */ |
| 136 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 137 | nih_fatal (_("Not being executed as init")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 138 | exit (1); |
| 139 | } |
| 140 | |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 141 | /* Clear our arguments from the command-line, so that we show up in |
| 142 | * ps or top output as /sbin/init, with no extra flags. |
| 143 | * |
| 144 | * This is a very Linux-specific trick; by deleting the NULL |
| 145 | * terminator at the end of the last argument, we fool the kernel |
| 146 | * into believing we used a setproctitle()-a-like to extend the |
| 147 | * argument space into the environment space, and thus make it use |
| 148 | * strlen() instead of its own assumed length. In fact, we've done |
| 149 | * the exact opposite, and shrunk the command line length to just that |
| 150 | * of whatever is in argv[0]. |
| 151 | * |
| 152 | * If we don't do this, and just write \0 over the rest of argv, for |
| 153 | * example; the command-line length still includes those \0s, and ps |
| 154 | * will show whitespace in their place. |
| 155 | */ |
| 156 | if (argc > 1) { |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 157 | char *arg_end; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 158 | |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 159 | arg_end = argv[argc-1] + strlen (argv[argc-1]); |
| 160 | *arg_end = ' '; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 161 | } |
Scott James Remnant | 7f4db42 | 2007-01-09 20:51:08 +0000 | [diff] [blame] | 162 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 163 | |
| 164 | /* Become the leader of a new session and process group, shedding |
| 165 | * any controlling tty (which we shouldn't have had anyway - but |
| 166 | * you never know what initramfs did). |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 167 | */ |
| 168 | setsid (); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 169 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 170 | /* Set the standard file descriptors to the ordinary console device, |
| 171 | * resetting it to sane defaults unless we're inheriting from another |
| 172 | * init process which we know left it in a sane state. |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 173 | */ |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 174 | if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 175 | nih_free (nih_error_get ()); |
Scott James Remnant | f99ea0c | 2010-02-04 11:14:30 -0800 | [diff] [blame] | 176 | if (system_setup_console (CONSOLE_NONE, FALSE) < 0) |
| 177 | nih_free (nih_error_get ()); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 178 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 179 | /* Set the PATH environment variable */ |
| 180 | setenv ("PATH", PATH, TRUE); |
| 181 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 182 | /* Switch to the root directory in case we were started from some |
| 183 | * strange place, or worse, some directory in the initramfs that's |
| 184 | * going to go away soon. |
| 185 | */ |
Scott James Remnant | 5635e07 | 2008-05-06 23:22:58 +0100 | [diff] [blame] | 186 | if (chdir ("/")) |
| 187 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 188 | strerror (errno)); |
Scott James Remnant | d69c1da | 2010-02-26 15:29:07 +0000 | [diff] [blame^] | 189 | |
| 190 | /* Mount the /proc and /sys filesystems, which are pretty much |
| 191 | * essential for any Linux system; not to mention used by |
| 192 | * ourselves. |
| 193 | */ |
| 194 | if (system_mount ("proc", "/proc") < 0) { |
| 195 | NihError *err; |
| 196 | |
| 197 | err = nih_error_get (); |
| 198 | nih_warn ("%s: %s", _("Unable to mount /proc filesystem"), |
| 199 | err->message); |
| 200 | nih_free (err); |
| 201 | } |
| 202 | |
| 203 | if (system_mount ("sysfs", "/sys") < 0) { |
| 204 | NihError *err; |
| 205 | |
| 206 | err = nih_error_get (); |
| 207 | nih_warn ("%s: %s", _("Unable to mount /sys filesystem"), |
| 208 | err->message); |
| 209 | nih_free (err); |
| 210 | } |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 211 | #else /* DEBUG */ |
| 212 | nih_log_set_priority (NIH_LOG_DEBUG); |
| 213 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 214 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 215 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 216 | /* Reset the signal state and install the signal handler for those |
| 217 | * signals we actually want to catch; this also sets those that |
| 218 | * can be sent to us, because we're special |
| 219 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 220 | if (! restart) |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 221 | nih_signal_reset (); |
| 222 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 223 | #ifndef DEBUG |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 224 | /* Catch fatal errors immediately rather than waiting for a new |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 225 | * iteration through the main loop. |
| 226 | */ |
| 227 | nih_signal_set_handler (SIGSEGV, crash_handler); |
| 228 | nih_signal_set_handler (SIGABRT, crash_handler); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 229 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 230 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 231 | /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them |
| 232 | * directly; it's enough that they interrupt the main loop and |
| 233 | * get dealt with during it. |
| 234 | */ |
| 235 | nih_signal_set_handler (SIGCHLD, nih_signal_handler); |
| 236 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 237 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 238 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 239 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 240 | * pressed; generate an event with the same name. |
| 241 | */ |
| 242 | reboot (RB_DISABLE_CAD); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 243 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 244 | NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 245 | |
| 246 | /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed; |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 247 | * generate a keyboard-request event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 248 | */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 249 | if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) { |
| 250 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 251 | NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH, |
| 252 | kbd_handler, NULL)); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 253 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 254 | |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 255 | /* powstatd sends us SIGPWR when it changes /etc/powerstatus */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 256 | nih_signal_set_handler (SIGPWR, nih_signal_handler); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 257 | NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL)); |
| 258 | |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 259 | /* SIGHUP instructs us to re-load our configuration */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 260 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 261 | NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL)); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 262 | |
| 263 | /* SIGUSR1 instructs us to reconnect to D-Bus */ |
| 264 | nih_signal_set_handler (SIGUSR1, nih_signal_handler); |
| 265 | NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL)); |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 266 | #endif /* DEBUG */ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 267 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 268 | |
Scott James Remnant | 0eade49 | 2007-11-15 05:48:07 +0000 | [diff] [blame] | 269 | /* Watch children for events */ |
Scott James Remnant | 5ebc6c6 | 2007-12-06 16:01:13 +0000 | [diff] [blame] | 270 | NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL, |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 271 | job_process_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 272 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 273 | /* Process the event queue each time through the main loop */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 274 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 275 | NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 276 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 277 | |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 278 | /* Read configuration */ |
Scott James Remnant | 854fda2 | 2009-06-23 01:21:55 +0100 | [diff] [blame] | 279 | NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE)); |
| 280 | NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR)); |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 281 | |
| 282 | conf_reload (); |
Scott James Remnant | d03c53c | 2007-03-13 19:13:19 +0000 | [diff] [blame] | 283 | |
Scott James Remnant | 1ba7f7a | 2008-05-07 23:52:11 +0100 | [diff] [blame] | 284 | /* Create a listening server for private connections. */ |
| 285 | while (control_server_open () < 0) { |
| 286 | NihError *err; |
| 287 | |
| 288 | err = nih_error_get (); |
| 289 | if (err->number != ENOMEM) { |
| 290 | nih_warn ("%s: %s", _("Unable to listen for private connections"), |
| 291 | err->message); |
| 292 | nih_free (err); |
| 293 | break; |
| 294 | } |
| 295 | nih_free (err); |
| 296 | } |
| 297 | |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 298 | /* Open connection to the system bus; we normally expect this to |
| 299 | * fail and will try again later - don't let ENOMEM stop us though. |
| 300 | */ |
| 301 | while (control_bus_open () < 0) { |
| 302 | NihError *err; |
| 303 | int number; |
| 304 | |
| 305 | err = nih_error_get (); |
| 306 | number = err->number; |
| 307 | nih_free (err); |
| 308 | |
| 309 | if (number != ENOMEM) |
| 310 | break; |
| 311 | } |
| 312 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 313 | #ifndef DEBUG |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 314 | /* Now that the startup is complete, send all further logging output |
| 315 | * to syslog instead of to the console. |
| 316 | */ |
| 317 | openlog (program_name, LOG_CONS, LOG_DAEMON); |
| 318 | nih_log_set_logger (nih_logger_syslog); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 319 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 320 | |
| 321 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 322 | /* Generate and run the startup event or read the state from the |
| 323 | * init daemon that exec'd us |
| 324 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 325 | if (! restart) { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 326 | NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL)); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 327 | } else { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 328 | sigset_t mask; |
| 329 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 330 | /* We're ok to receive signals again */ |
| 331 | sigemptyset (&mask); |
| 332 | sigprocmask (SIG_SETMASK, &mask, NULL); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 333 | } |
| 334 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 335 | /* Run through the loop at least once to deal with signals that were |
| 336 | * delivered to the previous process while the mask was set or to |
| 337 | * process the startup event we emitted. |
| 338 | */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 339 | nih_main_loop_interrupt (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 340 | ret = nih_main_loop (); |
| 341 | |
| 342 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 343 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 344 | |
| 345 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 346 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 347 | /** |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 348 | * crash_handler: |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 349 | * @signum: signal number received. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 350 | * |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 351 | * Handle receiving the SEGV or ABRT signal, usually caused by one of |
| 352 | * our own mistakes. We deal with it by dumping core in a child process |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 353 | * and then killing the parent. |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 354 | * |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 355 | * Sadly there's no real alternative to the ensuing kernel panic. Our |
| 356 | * state is likely in tatters, so we can't sigjmp() anywhere "safe" or |
| 357 | * re-exec since the system will be suddenly lobotomised. We definitely |
| 358 | * don't want to start a root shell or anything like that. Best thing is |
| 359 | * to just stop the whole thing and hope that bug report comes quickly. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 360 | **/ |
| 361 | static void |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 362 | crash_handler (int signum) |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 363 | { |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 364 | pid_t pid; |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 365 | |
| 366 | nih_assert (argv0 != NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 367 | |
| 368 | pid = fork (); |
| 369 | if (pid == 0) { |
| 370 | struct sigaction act; |
| 371 | struct rlimit limit; |
| 372 | sigset_t mask; |
| 373 | |
| 374 | /* Mask out all signals */ |
| 375 | sigfillset (&mask); |
| 376 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 377 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 378 | /* Set the handler to the default so core is dumped */ |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 379 | act.sa_handler = SIG_DFL; |
| 380 | act.sa_flags = 0; |
| 381 | sigemptyset (&act.sa_mask); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 382 | sigaction (signum, &act, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 383 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 384 | /* Don't limit the core dump size */ |
| 385 | limit.rlim_cur = RLIM_INFINITY; |
| 386 | limit.rlim_max = RLIM_INFINITY; |
| 387 | setrlimit (RLIMIT_CORE, &limit); |
| 388 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 389 | /* Dump in the root directory */ |
Scott James Remnant | 9d736bb | 2009-07-21 18:15:20 +0100 | [diff] [blame] | 390 | if (chdir ("/")) |
| 391 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 392 | strerror (errno)); |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 393 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 394 | /* Raise the signal again */ |
| 395 | raise (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 396 | |
| 397 | /* Unmask so that we receive it */ |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 398 | sigdelset (&mask, signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 399 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 400 | |
| 401 | /* Wait for death */ |
| 402 | pause (); |
| 403 | exit (0); |
| 404 | } else if (pid > 0) { |
| 405 | /* Wait for the core to be generated */ |
| 406 | waitpid (pid, NULL, 0); |
| 407 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 408 | nih_fatal (_("Caught %s, core dumped"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 409 | (signum == SIGSEGV |
| 410 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 411 | } else { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 412 | nih_fatal (_("Caught %s, unable to dump core"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 413 | (signum == SIGSEGV |
| 414 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 415 | } |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 416 | |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 417 | /* Goodbye, cruel world. */ |
| 418 | exit (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * cad_handler: |
| 423 | * @data: unused, |
| 424 | * @signal: signal that called this handler. |
| 425 | * |
| 426 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 427 | * presses Ctrl-Alt-Delete on the console. We just generate a |
Scott James Remnant | bb3cc3f | 2006-09-08 17:17:47 +0100 | [diff] [blame] | 428 | * ctrlaltdel event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 429 | **/ |
| 430 | static void |
| 431 | cad_handler (void *data, |
| 432 | NihSignal *signal) |
| 433 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 434 | NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * kbd_handler: |
| 439 | * @data: unused, |
| 440 | * @signal: signal that called this handler. |
| 441 | * |
| 442 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 443 | * presses Alt-UpArrow on the console. We just generate a |
| 444 | * kbdrequest event. |
| 445 | **/ |
| 446 | static void |
| 447 | kbd_handler (void *data, |
| 448 | NihSignal *signal) |
| 449 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 450 | NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 451 | } |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 452 | |
| 453 | /** |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 454 | * pwr_handler: |
| 455 | * @data: unused, |
| 456 | * @signal: signal that called this handler. |
| 457 | * |
| 458 | * Handle having recieved the SIGPWR signal, sent to us when powstatd |
| 459 | * changes the /etc/powerstatus file. We just generate a |
| 460 | * power-status-changed event and jobs read the file. |
| 461 | **/ |
| 462 | static void |
| 463 | pwr_handler (void *data, |
| 464 | NihSignal *signal) |
| 465 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 466 | NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL)); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 470 | * hup_handler: |
| 471 | * @data: unused, |
| 472 | * @signal: signal that called this handler. |
| 473 | * |
| 474 | * Handle having recieved the SIGHUP signal, which we use to instruct us to |
| 475 | * reload our configuration. |
| 476 | **/ |
| 477 | static void |
| 478 | hup_handler (void *data, |
| 479 | NihSignal *signal) |
| 480 | { |
| 481 | nih_info (_("Reloading configuration")); |
| 482 | conf_reload (); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 483 | } |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 484 | |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 485 | /** |
| 486 | * usr1_handler: |
| 487 | * @data: unused, |
| 488 | * @signal: signal that called this handler. |
| 489 | * |
| 490 | * Handle having recieved the SIGUSR signal, which we use to instruct us to |
| 491 | * reconnect to D-Bus. |
| 492 | **/ |
| 493 | static void |
| 494 | usr1_handler (void *data, |
| 495 | NihSignal *signal) |
| 496 | { |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 497 | if (! control_bus) { |
| 498 | nih_info (_("Reconnecting to system bus")); |
| 499 | |
| 500 | if (control_bus_open () < 0) { |
| 501 | NihError *err; |
| 502 | |
| 503 | err = nih_error_get (); |
| 504 | nih_warn ("%s: %s", _("Unable to connect to the system bus"), |
| 505 | err->message); |
| 506 | nih_free (err); |
| 507 | } |
| 508 | } |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 509 | } |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 510 | #endif /* DEBUG */ |