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