Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 1 | /* upstart |
| 2 | * |
Scott James Remnant | 540dcfd | 2011-03-16 15:54:56 -0700 | [diff] [blame] | 3 | * Copyright © 2011 Google Inc. |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 4 | * Copyright © 2010 Canonical Ltd. |
Scott James Remnant | 7d5b2ea | 2009-05-22 15:20:12 +0200 | [diff] [blame] | 5 | * Author: Scott James Remnant <scott@netsplit.com>. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 6 | * |
Scott James Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * 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] | 9 | * published by the Free Software Foundation. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 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 | * |
Scott James Remnant | 0c0c5a5 | 2009-06-23 10:29:35 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 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 | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 35 | #include <limits.h> |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 36 | #include <signal.h> |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 37 | #include <stdlib.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 38 | #include <string.h> |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 39 | #include <syslog.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 40 | #include <unistd.h> |
| 41 | |
Scott James Remnant | 097b2a9 | 2006-09-01 19:30:37 +0100 | [diff] [blame] | 42 | #include <linux/kd.h> |
| 43 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 44 | #include <nih/macros.h> |
| 45 | #include <nih/alloc.h> |
| 46 | #include <nih/list.h> |
| 47 | #include <nih/timer.h> |
| 48 | #include <nih/signal.h> |
| 49 | #include <nih/child.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 50 | #include <nih/option.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 51 | #include <nih/main.h> |
Scott James Remnant | 28fcc92 | 2006-09-01 04:15:57 +0100 | [diff] [blame] | 52 | #include <nih/error.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 53 | #include <nih/logging.h> |
| 54 | |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 55 | #include "paths.h" |
Scott James Remnant | 5d82d90 | 2008-04-30 00:03:29 +0100 | [diff] [blame] | 56 | #include "events.h" |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 57 | #include "system.h" |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 58 | #include "job_class.h" |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 59 | #include "job_process.h" |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 60 | #include "event.h" |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 61 | #include "conf.h" |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 62 | #include "control.h" |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 63 | |
| 64 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 65 | /* Prototypes for static functions */ |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 66 | #ifndef DEBUG |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 67 | static int logger_kmsg (NihLogLevel priority, const char *message); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 68 | static void crash_handler (int signum); |
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 | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 72 | static void hup_handler (void *data, NihSignal *signal); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 73 | static void usr1_handler (void *data, NihSignal *signal); |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 74 | #endif /* DEBUG */ |
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 | */ |
Scott James Remnant | 5635e07 | 2008-05-06 23:22:58 +0100 | [diff] [blame] | 188 | if (chdir ("/")) |
| 189 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 190 | strerror (errno)); |
Scott James Remnant | d69c1da | 2010-02-26 15:29:07 +0000 | [diff] [blame] | 191 | |
| 192 | /* Mount the /proc and /sys filesystems, which are pretty much |
| 193 | * essential for any Linux system; not to mention used by |
| 194 | * ourselves. |
| 195 | */ |
| 196 | if (system_mount ("proc", "/proc") < 0) { |
| 197 | NihError *err; |
| 198 | |
| 199 | err = nih_error_get (); |
| 200 | nih_warn ("%s: %s", _("Unable to mount /proc filesystem"), |
| 201 | err->message); |
| 202 | nih_free (err); |
| 203 | } |
| 204 | |
| 205 | if (system_mount ("sysfs", "/sys") < 0) { |
| 206 | NihError *err; |
| 207 | |
| 208 | err = nih_error_get (); |
| 209 | nih_warn ("%s: %s", _("Unable to mount /sys filesystem"), |
| 210 | err->message); |
| 211 | nih_free (err); |
| 212 | } |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 213 | #else /* DEBUG */ |
| 214 | nih_log_set_priority (NIH_LOG_DEBUG); |
James Hunt | 39f1c4f | 2010-12-13 18:15:24 +0000 | [diff] [blame] | 215 | nih_debug ("Running as PID %d (PPID %d)", |
| 216 | (int)getpid (), (int)getppid ()); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 217 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 218 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 219 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 220 | /* Reset the signal state and install the signal handler for those |
| 221 | * signals we actually want to catch; this also sets those that |
| 222 | * can be sent to us, because we're special |
| 223 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 224 | if (! restart) |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 225 | nih_signal_reset (); |
| 226 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 227 | #ifndef DEBUG |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 228 | /* Catch fatal errors immediately rather than waiting for a new |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 229 | * iteration through the main loop. |
| 230 | */ |
| 231 | nih_signal_set_handler (SIGSEGV, crash_handler); |
| 232 | nih_signal_set_handler (SIGABRT, crash_handler); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 233 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 234 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 235 | /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them |
| 236 | * directly; it's enough that they interrupt the main loop and |
| 237 | * get dealt with during it. |
| 238 | */ |
| 239 | nih_signal_set_handler (SIGCHLD, nih_signal_handler); |
| 240 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 241 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 242 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 243 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 244 | * pressed; generate an event with the same name. |
| 245 | */ |
| 246 | reboot (RB_DISABLE_CAD); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 247 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 248 | NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 249 | |
| 250 | /* 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] | 251 | * generate a keyboard-request event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 252 | */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 253 | if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) { |
| 254 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 255 | NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH, |
| 256 | kbd_handler, NULL)); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 257 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 258 | |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 259 | /* powstatd sends us SIGPWR when it changes /etc/powerstatus */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 260 | nih_signal_set_handler (SIGPWR, nih_signal_handler); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 261 | NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL)); |
| 262 | |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 263 | /* SIGHUP instructs us to re-load our configuration */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 264 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 265 | NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL)); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 266 | |
| 267 | /* SIGUSR1 instructs us to reconnect to D-Bus */ |
| 268 | nih_signal_set_handler (SIGUSR1, nih_signal_handler); |
| 269 | NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL)); |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 270 | #endif /* DEBUG */ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 271 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 272 | |
Scott James Remnant | 0eade49 | 2007-11-15 05:48:07 +0000 | [diff] [blame] | 273 | /* Watch children for events */ |
Scott James Remnant | 5ebc6c6 | 2007-12-06 16:01:13 +0000 | [diff] [blame] | 274 | NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL, |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 275 | job_process_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 276 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 277 | /* Process the event queue each time through the main loop */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 278 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 279 | NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 280 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 281 | |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 282 | /* Adjust our OOM priority to the default, which will be inherited |
| 283 | * by all jobs. |
| 284 | */ |
| 285 | if (JOB_DEFAULT_OOM_SCORE_ADJ) { |
| 286 | char filename[PATH_MAX]; |
| 287 | int oom_value; |
| 288 | FILE *fd; |
| 289 | |
| 290 | snprintf (filename, sizeof (filename), |
| 291 | "/proc/%d/oom_score_adj", getpid ()); |
| 292 | oom_value = JOB_DEFAULT_OOM_SCORE_ADJ; |
| 293 | fd = fopen (filename, "w"); |
Scott James Remnant | 4c0fa53 | 2011-08-11 13:52:28 -0700 | [diff] [blame^] | 294 | if ((! fd) && (errno == ENOENT)) { |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 295 | snprintf (filename, sizeof (filename), |
| 296 | "/proc/%d/oom_adj", getpid ()); |
| 297 | oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ |
| 298 | * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000; |
| 299 | fd = fopen (filename, "w"); |
| 300 | } |
| 301 | if (! fd) { |
| 302 | nih_warn ("%s: %s", _("Unable to set default oom score"), |
| 303 | strerror (errno)); |
| 304 | } else { |
| 305 | fprintf (fd, "%d\n", oom_value); |
| 306 | |
| 307 | if (fclose (fd)) |
| 308 | nih_warn ("%s: %s", _("Unable to set default oom score"), |
| 309 | strerror (errno)); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 314 | /* Read configuration */ |
Scott James Remnant | 854fda2 | 2009-06-23 01:21:55 +0100 | [diff] [blame] | 315 | NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE)); |
| 316 | NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR)); |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 317 | |
| 318 | conf_reload (); |
Scott James Remnant | d03c53c | 2007-03-13 19:13:19 +0000 | [diff] [blame] | 319 | |
Scott James Remnant | 1ba7f7a | 2008-05-07 23:52:11 +0100 | [diff] [blame] | 320 | /* Create a listening server for private connections. */ |
| 321 | while (control_server_open () < 0) { |
| 322 | NihError *err; |
| 323 | |
| 324 | err = nih_error_get (); |
| 325 | if (err->number != ENOMEM) { |
| 326 | nih_warn ("%s: %s", _("Unable to listen for private connections"), |
| 327 | err->message); |
| 328 | nih_free (err); |
| 329 | break; |
| 330 | } |
| 331 | nih_free (err); |
| 332 | } |
| 333 | |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 334 | /* Open connection to the system bus; we normally expect this to |
| 335 | * fail and will try again later - don't let ENOMEM stop us though. |
| 336 | */ |
| 337 | while (control_bus_open () < 0) { |
| 338 | NihError *err; |
| 339 | int number; |
| 340 | |
| 341 | err = nih_error_get (); |
| 342 | number = err->number; |
| 343 | nih_free (err); |
| 344 | |
| 345 | if (number != ENOMEM) |
| 346 | break; |
| 347 | } |
| 348 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 349 | #ifndef DEBUG |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 350 | /* Now that the startup is complete, send all further logging output |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 351 | * to kmsg instead of to the console. |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 352 | */ |
Scott James Remnant | 540dcfd | 2011-03-16 15:54:56 -0700 | [diff] [blame] | 353 | if (system_setup_console (CONSOLE_NONE, FALSE) < 0) |
| 354 | nih_free (nih_error_get ()); |
| 355 | |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 356 | nih_log_set_logger (logger_kmsg); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 357 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 358 | |
| 359 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 360 | /* Generate and run the startup event or read the state from the |
| 361 | * init daemon that exec'd us |
| 362 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 363 | if (! restart) { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 364 | NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL)); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 365 | } else { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 366 | sigset_t mask; |
| 367 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 368 | /* We're ok to receive signals again */ |
| 369 | sigemptyset (&mask); |
| 370 | sigprocmask (SIG_SETMASK, &mask, NULL); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 373 | /* Run through the loop at least once to deal with signals that were |
| 374 | * delivered to the previous process while the mask was set or to |
| 375 | * process the startup event we emitted. |
| 376 | */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 377 | nih_main_loop_interrupt (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 378 | ret = nih_main_loop (); |
| 379 | |
| 380 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 381 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 382 | |
| 383 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 384 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 385 | /** |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 386 | * logger_kmsg: |
| 387 | * @priority: priority of message being logged, |
| 388 | * @message: message to log. |
| 389 | * |
| 390 | * Outputs the @message to the kernel log message socket prefixed with an |
| 391 | * appropriate tag based on @priority, the program name and terminated with |
| 392 | * a new line. |
| 393 | * |
| 394 | * Returns: zero on success, negative value on error. |
| 395 | **/ |
| 396 | static int |
| 397 | logger_kmsg (NihLogLevel priority, |
| 398 | const char *message) |
| 399 | { |
| 400 | int tag; |
| 401 | FILE *kmsg; |
| 402 | |
| 403 | nih_assert (message != NULL); |
| 404 | |
| 405 | switch (priority) { |
| 406 | case NIH_LOG_DEBUG: |
| 407 | tag = '7'; |
| 408 | break; |
| 409 | case NIH_LOG_INFO: |
| 410 | tag = '6'; |
| 411 | break; |
| 412 | case NIH_LOG_MESSAGE: |
| 413 | tag = '5'; |
| 414 | break; |
| 415 | case NIH_LOG_WARN: |
| 416 | tag = '4'; |
| 417 | break; |
| 418 | case NIH_LOG_ERROR: |
| 419 | tag = '3'; |
| 420 | break; |
| 421 | case NIH_LOG_FATAL: |
| 422 | tag = '2'; |
| 423 | break; |
| 424 | default: |
| 425 | tag = 'd'; |
| 426 | } |
| 427 | |
| 428 | kmsg = fopen ("/dev/kmsg", "w"); |
| 429 | if (! kmsg) |
| 430 | return -1; |
| 431 | |
| 432 | if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) { |
| 433 | int saved_errno = errno; |
| 434 | fclose (kmsg); |
| 435 | errno = saved_errno; |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | if (fclose (kmsg) < 0) |
| 440 | return -1; |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /** |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 447 | * crash_handler: |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 448 | * @signum: signal number received. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 449 | * |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 450 | * Handle receiving the SEGV or ABRT signal, usually caused by one of |
| 451 | * 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] | 452 | * and then killing the parent. |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 453 | * |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 454 | * Sadly there's no real alternative to the ensuing kernel panic. Our |
| 455 | * state is likely in tatters, so we can't sigjmp() anywhere "safe" or |
| 456 | * re-exec since the system will be suddenly lobotomised. We definitely |
| 457 | * don't want to start a root shell or anything like that. Best thing is |
| 458 | * 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] | 459 | **/ |
| 460 | static void |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 461 | crash_handler (int signum) |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 462 | { |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 463 | pid_t pid; |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 464 | |
| 465 | nih_assert (argv0 != NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 466 | |
| 467 | pid = fork (); |
| 468 | if (pid == 0) { |
| 469 | struct sigaction act; |
| 470 | struct rlimit limit; |
| 471 | sigset_t mask; |
| 472 | |
| 473 | /* Mask out all signals */ |
| 474 | sigfillset (&mask); |
| 475 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 476 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 477 | /* Set the handler to the default so core is dumped */ |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 478 | act.sa_handler = SIG_DFL; |
| 479 | act.sa_flags = 0; |
| 480 | sigemptyset (&act.sa_mask); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 481 | sigaction (signum, &act, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 482 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 483 | /* Don't limit the core dump size */ |
| 484 | limit.rlim_cur = RLIM_INFINITY; |
| 485 | limit.rlim_max = RLIM_INFINITY; |
| 486 | setrlimit (RLIMIT_CORE, &limit); |
| 487 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 488 | /* Dump in the root directory */ |
Scott James Remnant | 9d736bb | 2009-07-21 18:15:20 +0100 | [diff] [blame] | 489 | if (chdir ("/")) |
| 490 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 491 | strerror (errno)); |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 492 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 493 | /* Raise the signal again */ |
| 494 | raise (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 495 | |
| 496 | /* Unmask so that we receive it */ |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 497 | sigdelset (&mask, signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 498 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 499 | |
| 500 | /* Wait for death */ |
| 501 | pause (); |
| 502 | exit (0); |
| 503 | } else if (pid > 0) { |
| 504 | /* Wait for the core to be generated */ |
| 505 | waitpid (pid, NULL, 0); |
| 506 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 507 | nih_fatal (_("Caught %s, core dumped"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 508 | (signum == SIGSEGV |
| 509 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 510 | } else { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 511 | nih_fatal (_("Caught %s, unable to dump core"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 512 | (signum == SIGSEGV |
| 513 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 514 | } |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 515 | |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 516 | /* Goodbye, cruel world. */ |
| 517 | exit (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /** |
| 521 | * cad_handler: |
| 522 | * @data: unused, |
| 523 | * @signal: signal that called this handler. |
| 524 | * |
| 525 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 526 | * presses Ctrl-Alt-Delete on the console. We just generate a |
Scott James Remnant | bb3cc3f | 2006-09-08 17:17:47 +0100 | [diff] [blame] | 527 | * ctrlaltdel event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 528 | **/ |
| 529 | static void |
| 530 | cad_handler (void *data, |
| 531 | NihSignal *signal) |
| 532 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 533 | NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /** |
| 537 | * kbd_handler: |
| 538 | * @data: unused, |
| 539 | * @signal: signal that called this handler. |
| 540 | * |
| 541 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 542 | * presses Alt-UpArrow on the console. We just generate a |
| 543 | * kbdrequest event. |
| 544 | **/ |
| 545 | static void |
| 546 | kbd_handler (void *data, |
| 547 | NihSignal *signal) |
| 548 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 549 | NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 550 | } |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 551 | |
| 552 | /** |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 553 | * pwr_handler: |
| 554 | * @data: unused, |
| 555 | * @signal: signal that called this handler. |
| 556 | * |
| 557 | * Handle having recieved the SIGPWR signal, sent to us when powstatd |
| 558 | * changes the /etc/powerstatus file. We just generate a |
| 559 | * power-status-changed event and jobs read the file. |
| 560 | **/ |
| 561 | static void |
| 562 | pwr_handler (void *data, |
| 563 | NihSignal *signal) |
| 564 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 565 | NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL)); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 569 | * hup_handler: |
| 570 | * @data: unused, |
| 571 | * @signal: signal that called this handler. |
| 572 | * |
| 573 | * Handle having recieved the SIGHUP signal, which we use to instruct us to |
| 574 | * reload our configuration. |
| 575 | **/ |
| 576 | static void |
| 577 | hup_handler (void *data, |
| 578 | NihSignal *signal) |
| 579 | { |
| 580 | nih_info (_("Reloading configuration")); |
| 581 | conf_reload (); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 582 | } |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 583 | |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 584 | /** |
| 585 | * usr1_handler: |
| 586 | * @data: unused, |
| 587 | * @signal: signal that called this handler. |
| 588 | * |
| 589 | * Handle having recieved the SIGUSR signal, which we use to instruct us to |
| 590 | * reconnect to D-Bus. |
| 591 | **/ |
| 592 | static void |
| 593 | usr1_handler (void *data, |
| 594 | NihSignal *signal) |
| 595 | { |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 596 | if (! control_bus) { |
| 597 | nih_info (_("Reconnecting to system bus")); |
| 598 | |
| 599 | if (control_bus_open () < 0) { |
| 600 | NihError *err; |
| 601 | |
| 602 | err = nih_error_get (); |
| 603 | nih_warn ("%s: %s", _("Unable to connect to the system bus"), |
| 604 | err->message); |
| 605 | nih_free (err); |
| 606 | } |
| 607 | } |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 608 | } |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 609 | #endif /* DEBUG */ |