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