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