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 | |
Luigi Semenzato | 9496496 | 2016-01-29 13:59:20 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | #include <stdio.h> |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 37 | #include <limits.h> |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 38 | #include <signal.h> |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 39 | #include <stdlib.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 40 | #include <string.h> |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 41 | #include <syslog.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
| 43 | |
Gwendal Grignou | 8c5b529 | 2016-03-07 11:51:37 -0800 | [diff] [blame^] | 44 | #ifdef ADD_DIRCRYPTO_RING |
| 45 | #include <ext2fs/ext2_fs.h> |
| 46 | #include <keyutils.h> |
| 47 | #endif |
| 48 | |
Luigi Semenzato | 9496496 | 2016-01-29 13:59:20 -0800 | [diff] [blame] | 49 | #ifdef HAVE_SELINUX |
| 50 | #include <selinux/selinux.h> |
| 51 | #endif |
| 52 | |
Scott James Remnant | 097b2a9 | 2006-09-01 19:30:37 +0100 | [diff] [blame] | 53 | #include <linux/kd.h> |
| 54 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 55 | #include <nih/macros.h> |
| 56 | #include <nih/alloc.h> |
| 57 | #include <nih/list.h> |
| 58 | #include <nih/timer.h> |
| 59 | #include <nih/signal.h> |
| 60 | #include <nih/child.h> |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 61 | #include <nih/option.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 62 | #include <nih/main.h> |
Scott James Remnant | 28fcc92 | 2006-09-01 04:15:57 +0100 | [diff] [blame] | 63 | #include <nih/error.h> |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 64 | #include <nih/logging.h> |
| 65 | |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 66 | #include "paths.h" |
Scott James Remnant | 5d82d90 | 2008-04-30 00:03:29 +0100 | [diff] [blame] | 67 | #include "events.h" |
Scott James Remnant | f2f69d0 | 2008-04-29 23:38:23 +0100 | [diff] [blame] | 68 | #include "system.h" |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 69 | #include "job_class.h" |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 70 | #include "job_process.h" |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 71 | #include "event.h" |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 72 | #include "conf.h" |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 73 | #include "control.h" |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 74 | |
| 75 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 76 | /* Prototypes for static functions */ |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 77 | #ifndef DEBUG |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 78 | static int logger_kmsg (NihLogLevel priority, const char *message); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 79 | static void crash_handler (int signum); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 80 | static void cad_handler (void *data, NihSignal *signal); |
| 81 | static void kbd_handler (void *data, NihSignal *signal); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 82 | static void pwr_handler (void *data, NihSignal *signal); |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 83 | static void hup_handler (void *data, NihSignal *signal); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 84 | static void usr1_handler (void *data, NihSignal *signal); |
Ricky Zhou | bedce60 | 2016-02-16 23:45:19 -0800 | [diff] [blame] | 85 | #else |
| 86 | static int logger_kmsg (NihLogLevel priority, const char *message) {} |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 87 | #endif /* DEBUG */ |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 88 | |
Ricky Zhou | bedce60 | 2016-02-16 23:45:19 -0800 | [diff] [blame] | 89 | #ifdef HAVE_SELINUX |
| 90 | #define CHECKREQPROT_PATH "/sys/fs/selinux/checkreqprot" |
| 91 | static void initialize_selinux (char **argv); |
| 92 | #endif |
| 93 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 94 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 95 | /** |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 96 | * argv0: |
| 97 | * |
| 98 | * Path to program executed, used for re-executing the init binary from the |
| 99 | * same location we were executed from. |
| 100 | **/ |
| 101 | static const char *argv0 = NULL; |
| 102 | |
| 103 | /** |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 104 | * restart: |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 105 | * |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 106 | * 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] | 107 | * process. |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 108 | **/ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 109 | static int restart = FALSE; |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /** |
| 113 | * options: |
| 114 | * |
| 115 | * Command-line options we accept. |
| 116 | **/ |
| 117 | static NihOption options[] = { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 118 | { 0, "restart", NULL, NULL, NULL, &restart, NULL }, |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 119 | |
| 120 | /* Ignore invalid options */ |
| 121 | { '-', "--", NULL, NULL, NULL, NULL, NULL }, |
| 122 | |
| 123 | NIH_OPTION_LAST |
| 124 | }; |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame] | 125 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 126 | |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 127 | int |
| 128 | main (int argc, |
| 129 | char *argv[]) |
| 130 | { |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 131 | char **args; |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 132 | int ret; |
Gwendal Grignou | 8c5b529 | 2016-03-07 11:51:37 -0800 | [diff] [blame^] | 133 | #ifdef ADD_DIRCRYPTO_RING |
| 134 | int root_fd; |
| 135 | struct ext4_encryption_policy policy; |
| 136 | key_serial_t keyring_id; |
| 137 | #endif |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 138 | |
Luigi Semenzato | 9496496 | 2016-01-29 13:59:20 -0800 | [diff] [blame] | 139 | #ifdef HAVE_SELINUX |
Ricky Zhou | bedce60 | 2016-02-16 23:45:19 -0800 | [diff] [blame] | 140 | if (getpid () == 1 && getenv ("SELINUX_INIT") == NULL) { |
| 141 | initialize_selinux (argv); |
Luigi Semenzato | 9496496 | 2016-01-29 13:59:20 -0800 | [diff] [blame] | 142 | } |
| 143 | #endif |
| 144 | |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 145 | argv0 = argv[0]; |
| 146 | nih_main_init (argv0); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 147 | |
Scott James Remnant | 930e25a | 2006-10-13 12:28:05 +0100 | [diff] [blame] | 148 | nih_option_set_synopsis (_("Process management daemon.")); |
Scott James Remnant | 462734c | 2006-10-13 13:36:00 +0100 | [diff] [blame] | 149 | nih_option_set_help ( |
| 150 | _("This daemon is normally executed by the kernel and given " |
| 151 | "process id 1 to denote its special status. When executed " |
| 152 | "by a user process, it will actually run /sbin/telinit.")); |
Scott James Remnant | a6ed7eb | 2006-10-13 12:14:45 +0100 | [diff] [blame] | 153 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 154 | args = nih_option_parser (NULL, argc, argv, options, FALSE); |
| 155 | if (! args) |
| 156 | exit (1); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 157 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 158 | #ifndef DEBUG |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 159 | /* Check we're root */ |
| 160 | if (getuid ()) { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 161 | nih_fatal (_("Need to be root")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 162 | exit (1); |
| 163 | } |
| 164 | |
| 165 | /* Check we're process #1 */ |
| 166 | if (getpid () > 1) { |
Scott James Remnant | e0d0dd1 | 2006-09-14 10:51:05 +0100 | [diff] [blame] | 167 | execv (TELINIT, argv); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 168 | /* Ignore failure, probably just that telinit doesn't exist */ |
| 169 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 170 | nih_fatal (_("Not being executed as init")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 171 | exit (1); |
| 172 | } |
| 173 | |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 174 | /* Clear our arguments from the command-line, so that we show up in |
| 175 | * ps or top output as /sbin/init, with no extra flags. |
| 176 | * |
| 177 | * This is a very Linux-specific trick; by deleting the NULL |
| 178 | * terminator at the end of the last argument, we fool the kernel |
| 179 | * into believing we used a setproctitle()-a-like to extend the |
| 180 | * argument space into the environment space, and thus make it use |
| 181 | * strlen() instead of its own assumed length. In fact, we've done |
| 182 | * the exact opposite, and shrunk the command line length to just that |
| 183 | * of whatever is in argv[0]. |
| 184 | * |
| 185 | * If we don't do this, and just write \0 over the rest of argv, for |
| 186 | * example; the command-line length still includes those \0s, and ps |
| 187 | * will show whitespace in their place. |
| 188 | */ |
| 189 | if (argc > 1) { |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 190 | char *arg_end; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 191 | |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 192 | arg_end = argv[argc-1] + strlen (argv[argc-1]); |
| 193 | *arg_end = ' '; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 194 | } |
Scott James Remnant | 7f4db42 | 2007-01-09 20:51:08 +0000 | [diff] [blame] | 195 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 196 | |
| 197 | /* Become the leader of a new session and process group, shedding |
| 198 | * any controlling tty (which we shouldn't have had anyway - but |
| 199 | * you never know what initramfs did). |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 200 | */ |
| 201 | setsid (); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 202 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 203 | /* Set the standard file descriptors to the ordinary console device, |
| 204 | * resetting it to sane defaults unless we're inheriting from another |
| 205 | * init process which we know left it in a sane state. |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 206 | */ |
Scott James Remnant | 9321618 | 2011-08-11 13:30:15 -0700 | [diff] [blame] | 207 | if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) { |
| 208 | NihError *err; |
| 209 | |
| 210 | err = nih_error_get (); |
| 211 | nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"), |
| 212 | err->message); |
| 213 | nih_free (err); |
| 214 | |
| 215 | if (system_setup_console (CONSOLE_NONE, FALSE) < 0) { |
| 216 | err = nih_error_get (); |
| 217 | nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"), |
| 218 | err->message); |
| 219 | nih_free (err); |
| 220 | |
| 221 | exit (1); |
| 222 | } |
| 223 | } |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 224 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 225 | /* Set the PATH environment variable */ |
| 226 | setenv ("PATH", PATH, TRUE); |
| 227 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 228 | /* Switch to the root directory in case we were started from some |
| 229 | * strange place, or worse, some directory in the initramfs that's |
| 230 | * going to go away soon. |
| 231 | */ |
Scott James Remnant | 5635e07 | 2008-05-06 23:22:58 +0100 | [diff] [blame] | 232 | if (chdir ("/")) |
| 233 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 234 | strerror (errno)); |
Scott James Remnant | d69c1da | 2010-02-26 15:29:07 +0000 | [diff] [blame] | 235 | |
| 236 | /* Mount the /proc and /sys filesystems, which are pretty much |
| 237 | * essential for any Linux system; not to mention used by |
| 238 | * ourselves. |
| 239 | */ |
| 240 | if (system_mount ("proc", "/proc") < 0) { |
| 241 | NihError *err; |
| 242 | |
| 243 | err = nih_error_get (); |
| 244 | nih_warn ("%s: %s", _("Unable to mount /proc filesystem"), |
| 245 | err->message); |
| 246 | nih_free (err); |
| 247 | } |
| 248 | |
| 249 | if (system_mount ("sysfs", "/sys") < 0) { |
| 250 | NihError *err; |
| 251 | |
| 252 | err = nih_error_get (); |
| 253 | nih_warn ("%s: %s", _("Unable to mount /sys filesystem"), |
| 254 | err->message); |
| 255 | nih_free (err); |
| 256 | } |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 257 | #else /* DEBUG */ |
| 258 | nih_log_set_priority (NIH_LOG_DEBUG); |
James Hunt | 39f1c4f | 2010-12-13 18:15:24 +0000 | [diff] [blame] | 259 | nih_debug ("Running as PID %d (PPID %d)", |
| 260 | (int)getpid (), (int)getppid ()); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 261 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 262 | |
Gwendal Grignou | 8c5b529 | 2016-03-07 11:51:37 -0800 | [diff] [blame^] | 263 | #ifdef ADD_DIRCRYPTO_RING |
| 264 | #define EXT4_IOC_GET_ENCRYPTION_POLICY \ |
| 265 | _IOW('f', 21, struct ext4_encryption_policy) |
| 266 | /* |
| 267 | * Set a keyring for the session to hold ext4 crypto keys. |
| 268 | * The session is at the root of all processes, so any users who wish |
| 269 | * to access a directory protected by ext4 crypto can access the key. |
| 270 | * |
| 271 | * Set only a session keyring when needed. |
| 272 | * A kernel patch is needed (see crbug/593893). |
| 273 | * Upstream kernel does not have the patch yet |
| 274 | * (See https://lkml.org/lkml/2016/3/17/491). |
| 275 | */ |
| 276 | int fd = open("/", O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 277 | if (fd == -1) { |
| 278 | nih_warn("%s: %s", _("Unable to open / directory: %s"), |
| 279 | strerror (errno)); |
| 280 | ret = EINVAL; |
| 281 | } else { |
| 282 | ret = ioctl(fd, EXT4_IOC_GET_ENCRYPTION_POLICY, &policy); |
| 283 | if (ret) |
| 284 | ret = errno; |
| 285 | close(fd); |
| 286 | } |
| 287 | if (ret != EINVAL && ret != EOPNOTSUPP && ret != ENOTTY) { |
| 288 | keyring_id = add_key ("keyring", "dircrypt", 0, 0, |
| 289 | KEY_SPEC_SESSION_KEYRING); |
| 290 | if (keyring_id == -1) |
| 291 | nih_warn ("%s: %s", |
| 292 | _("Unable to create dircrypt keyring: %s"), |
| 293 | strerror (errno)); |
| 294 | else |
| 295 | keyctl_setperm(keyring_id, |
| 296 | KEY_POS_VIEW | KEY_POS_SEARCH | |
| 297 | KEY_POS_LINK | KEY_POS_READ | |
| 298 | KEY_USR_ALL); |
| 299 | } |
| 300 | #endif |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 301 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 302 | /* Reset the signal state and install the signal handler for those |
| 303 | * signals we actually want to catch; this also sets those that |
| 304 | * can be sent to us, because we're special |
| 305 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 306 | if (! restart) |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 307 | nih_signal_reset (); |
| 308 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 309 | #ifndef DEBUG |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 310 | /* Catch fatal errors immediately rather than waiting for a new |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 311 | * iteration through the main loop. |
| 312 | */ |
| 313 | nih_signal_set_handler (SIGSEGV, crash_handler); |
| 314 | nih_signal_set_handler (SIGABRT, crash_handler); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 315 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 316 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 317 | /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them |
| 318 | * directly; it's enough that they interrupt the main loop and |
| 319 | * get dealt with during it. |
| 320 | */ |
| 321 | nih_signal_set_handler (SIGCHLD, nih_signal_handler); |
| 322 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 323 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 324 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 325 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 326 | * pressed; generate an event with the same name. |
| 327 | */ |
| 328 | reboot (RB_DISABLE_CAD); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 329 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 330 | NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 331 | |
| 332 | /* 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] | 333 | * generate a keyboard-request event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 334 | */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 335 | if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) { |
| 336 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 337 | NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH, |
| 338 | kbd_handler, NULL)); |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 339 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 340 | |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 341 | /* powstatd sends us SIGPWR when it changes /etc/powerstatus */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 342 | nih_signal_set_handler (SIGPWR, nih_signal_handler); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 343 | NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL)); |
| 344 | |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 345 | /* SIGHUP instructs us to re-load our configuration */ |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 346 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 347 | NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL)); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 348 | |
| 349 | /* SIGUSR1 instructs us to reconnect to D-Bus */ |
| 350 | nih_signal_set_handler (SIGUSR1, nih_signal_handler); |
| 351 | NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL)); |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 352 | #endif /* DEBUG */ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 353 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 354 | |
Scott James Remnant | 0eade49 | 2007-11-15 05:48:07 +0000 | [diff] [blame] | 355 | /* Watch children for events */ |
Scott James Remnant | 5ebc6c6 | 2007-12-06 16:01:13 +0000 | [diff] [blame] | 356 | NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL, |
Scott James Remnant | 63fd5c7 | 2008-04-30 21:34:31 +0100 | [diff] [blame] | 357 | job_process_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 358 | |
Scott James Remnant | 5c40843 | 2007-11-07 21:42:25 -0500 | [diff] [blame] | 359 | /* Process the event queue each time through the main loop */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 360 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 361 | NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 362 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 363 | |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 364 | /* Adjust our OOM priority to the default, which will be inherited |
| 365 | * by all jobs. |
| 366 | */ |
| 367 | if (JOB_DEFAULT_OOM_SCORE_ADJ) { |
| 368 | char filename[PATH_MAX]; |
| 369 | int oom_value; |
| 370 | FILE *fd; |
| 371 | |
| 372 | snprintf (filename, sizeof (filename), |
| 373 | "/proc/%d/oom_score_adj", getpid ()); |
| 374 | oom_value = JOB_DEFAULT_OOM_SCORE_ADJ; |
| 375 | fd = fopen (filename, "w"); |
Scott James Remnant | 4c0fa53 | 2011-08-11 13:52:28 -0700 | [diff] [blame] | 376 | if ((! fd) && (errno == ENOENT)) { |
Scott James Remnant | 91da63a | 2011-08-11 13:47:00 -0700 | [diff] [blame] | 377 | snprintf (filename, sizeof (filename), |
| 378 | "/proc/%d/oom_adj", getpid ()); |
| 379 | oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ |
| 380 | * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000; |
| 381 | fd = fopen (filename, "w"); |
| 382 | } |
| 383 | if (! fd) { |
| 384 | nih_warn ("%s: %s", _("Unable to set default oom score"), |
| 385 | strerror (errno)); |
| 386 | } else { |
| 387 | fprintf (fd, "%d\n", oom_value); |
| 388 | |
| 389 | if (fclose (fd)) |
| 390 | nih_warn ("%s: %s", _("Unable to set default oom score"), |
| 391 | strerror (errno)); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 396 | /* Read configuration */ |
Scott James Remnant | 854fda2 | 2009-06-23 01:21:55 +0100 | [diff] [blame] | 397 | NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE)); |
| 398 | NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR)); |
Scott James Remnant | 54b2a95 | 2007-06-10 22:15:24 +0100 | [diff] [blame] | 399 | |
| 400 | conf_reload (); |
Scott James Remnant | d03c53c | 2007-03-13 19:13:19 +0000 | [diff] [blame] | 401 | |
Scott James Remnant | 1ba7f7a | 2008-05-07 23:52:11 +0100 | [diff] [blame] | 402 | /* Create a listening server for private connections. */ |
| 403 | while (control_server_open () < 0) { |
| 404 | NihError *err; |
| 405 | |
| 406 | err = nih_error_get (); |
| 407 | if (err->number != ENOMEM) { |
| 408 | nih_warn ("%s: %s", _("Unable to listen for private connections"), |
| 409 | err->message); |
| 410 | nih_free (err); |
| 411 | break; |
| 412 | } |
| 413 | nih_free (err); |
| 414 | } |
| 415 | |
Scott James Remnant | f849144 | 2008-04-18 13:19:24 +0100 | [diff] [blame] | 416 | /* Open connection to the system bus; we normally expect this to |
| 417 | * fail and will try again later - don't let ENOMEM stop us though. |
| 418 | */ |
| 419 | while (control_bus_open () < 0) { |
| 420 | NihError *err; |
| 421 | int number; |
| 422 | |
| 423 | err = nih_error_get (); |
| 424 | number = err->number; |
| 425 | nih_free (err); |
| 426 | |
| 427 | if (number != ENOMEM) |
| 428 | break; |
| 429 | } |
| 430 | |
Scott James Remnant | 2c0bd59 | 2008-04-12 12:31:49 +0100 | [diff] [blame] | 431 | #ifndef DEBUG |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 432 | /* Now that the startup is complete, send all further logging output |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 433 | * to kmsg instead of to the console. |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 434 | */ |
Scott James Remnant | 9321618 | 2011-08-11 13:30:15 -0700 | [diff] [blame] | 435 | if (system_setup_console (CONSOLE_NONE, FALSE) < 0) { |
| 436 | NihError *err; |
| 437 | |
| 438 | err = nih_error_get (); |
| 439 | nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"), |
| 440 | err->message); |
| 441 | nih_free (err); |
| 442 | |
| 443 | exit (1); |
| 444 | } |
Scott James Remnant | 540dcfd | 2011-03-16 15:54:56 -0700 | [diff] [blame] | 445 | |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 446 | nih_log_set_logger (logger_kmsg); |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 447 | #endif /* DEBUG */ |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 448 | |
| 449 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 450 | /* Generate and run the startup event or read the state from the |
| 451 | * init daemon that exec'd us |
| 452 | */ |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 453 | if (! restart) { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 454 | NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL)); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 455 | } else { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 456 | sigset_t mask; |
| 457 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 458 | /* We're ok to receive signals again */ |
| 459 | sigemptyset (&mask); |
| 460 | sigprocmask (SIG_SETMASK, &mask, NULL); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 463 | /* Run through the loop at least once to deal with signals that were |
| 464 | * delivered to the previous process while the mask was set or to |
| 465 | * process the startup event we emitted. |
| 466 | */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 467 | nih_main_loop_interrupt (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 468 | ret = nih_main_loop (); |
| 469 | |
| 470 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 471 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 472 | |
| 473 | |
Scott James Remnant | 9f62a8e | 2008-01-15 19:22:36 +0000 | [diff] [blame] | 474 | #ifndef DEBUG |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 475 | /** |
Scott James Remnant | 02977f8 | 2011-02-17 15:33:04 -0800 | [diff] [blame] | 476 | * logger_kmsg: |
| 477 | * @priority: priority of message being logged, |
| 478 | * @message: message to log. |
| 479 | * |
| 480 | * Outputs the @message to the kernel log message socket prefixed with an |
| 481 | * appropriate tag based on @priority, the program name and terminated with |
| 482 | * a new line. |
| 483 | * |
| 484 | * Returns: zero on success, negative value on error. |
| 485 | **/ |
| 486 | static int |
| 487 | logger_kmsg (NihLogLevel priority, |
| 488 | const char *message) |
| 489 | { |
| 490 | int tag; |
| 491 | FILE *kmsg; |
| 492 | |
| 493 | nih_assert (message != NULL); |
| 494 | |
| 495 | switch (priority) { |
| 496 | case NIH_LOG_DEBUG: |
| 497 | tag = '7'; |
| 498 | break; |
| 499 | case NIH_LOG_INFO: |
| 500 | tag = '6'; |
| 501 | break; |
| 502 | case NIH_LOG_MESSAGE: |
| 503 | tag = '5'; |
| 504 | break; |
| 505 | case NIH_LOG_WARN: |
| 506 | tag = '4'; |
| 507 | break; |
| 508 | case NIH_LOG_ERROR: |
| 509 | tag = '3'; |
| 510 | break; |
| 511 | case NIH_LOG_FATAL: |
| 512 | tag = '2'; |
| 513 | break; |
| 514 | default: |
| 515 | tag = 'd'; |
| 516 | } |
| 517 | |
| 518 | kmsg = fopen ("/dev/kmsg", "w"); |
| 519 | if (! kmsg) |
| 520 | return -1; |
| 521 | |
| 522 | if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) { |
| 523 | int saved_errno = errno; |
| 524 | fclose (kmsg); |
| 525 | errno = saved_errno; |
| 526 | return -1; |
| 527 | } |
| 528 | |
| 529 | if (fclose (kmsg) < 0) |
| 530 | return -1; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 537 | * crash_handler: |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 538 | * @signum: signal number received. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 539 | * |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 540 | * Handle receiving the SEGV or ABRT signal, usually caused by one of |
| 541 | * 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] | 542 | * and then killing the parent. |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 543 | * |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 544 | * Sadly there's no real alternative to the ensuing kernel panic. Our |
| 545 | * state is likely in tatters, so we can't sigjmp() anywhere "safe" or |
| 546 | * re-exec since the system will be suddenly lobotomised. We definitely |
| 547 | * don't want to start a root shell or anything like that. Best thing is |
| 548 | * 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] | 549 | **/ |
| 550 | static void |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 551 | crash_handler (int signum) |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 552 | { |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 553 | pid_t pid; |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame] | 554 | |
| 555 | nih_assert (argv0 != NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 556 | |
| 557 | pid = fork (); |
| 558 | if (pid == 0) { |
| 559 | struct sigaction act; |
| 560 | struct rlimit limit; |
| 561 | sigset_t mask; |
| 562 | |
| 563 | /* Mask out all signals */ |
| 564 | sigfillset (&mask); |
| 565 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 566 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 567 | /* Set the handler to the default so core is dumped */ |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 568 | act.sa_handler = SIG_DFL; |
| 569 | act.sa_flags = 0; |
| 570 | sigemptyset (&act.sa_mask); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 571 | sigaction (signum, &act, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 572 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 573 | /* Don't limit the core dump size */ |
| 574 | limit.rlim_cur = RLIM_INFINITY; |
| 575 | limit.rlim_max = RLIM_INFINITY; |
| 576 | setrlimit (RLIMIT_CORE, &limit); |
| 577 | |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 578 | /* Dump in the root directory */ |
Scott James Remnant | 9d736bb | 2009-07-21 18:15:20 +0100 | [diff] [blame] | 579 | if (chdir ("/")) |
| 580 | nih_warn ("%s: %s", _("Unable to set root directory"), |
| 581 | strerror (errno)); |
Scott James Remnant | fa73338 | 2008-01-15 15:33:27 +0000 | [diff] [blame] | 582 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 583 | /* Raise the signal again */ |
| 584 | raise (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 585 | |
| 586 | /* Unmask so that we receive it */ |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 587 | sigdelset (&mask, signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 588 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 589 | |
| 590 | /* Wait for death */ |
| 591 | pause (); |
| 592 | exit (0); |
| 593 | } else if (pid > 0) { |
| 594 | /* Wait for the core to be generated */ |
| 595 | waitpid (pid, NULL, 0); |
| 596 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 597 | nih_fatal (_("Caught %s, core dumped"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 598 | (signum == SIGSEGV |
| 599 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 600 | } else { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 601 | nih_fatal (_("Caught %s, unable to dump core"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 602 | (signum == SIGSEGV |
| 603 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 604 | } |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 605 | |
Scott James Remnant | 4b56d6f | 2008-04-12 12:23:15 +0100 | [diff] [blame] | 606 | /* Goodbye, cruel world. */ |
| 607 | exit (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
| 611 | * cad_handler: |
| 612 | * @data: unused, |
| 613 | * @signal: signal that called this handler. |
| 614 | * |
| 615 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 616 | * presses Ctrl-Alt-Delete on the console. We just generate a |
Scott James Remnant | bb3cc3f | 2006-09-08 17:17:47 +0100 | [diff] [blame] | 617 | * ctrlaltdel event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 618 | **/ |
| 619 | static void |
| 620 | cad_handler (void *data, |
| 621 | NihSignal *signal) |
| 622 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 623 | NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /** |
| 627 | * kbd_handler: |
| 628 | * @data: unused, |
| 629 | * @signal: signal that called this handler. |
| 630 | * |
| 631 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 632 | * presses Alt-UpArrow on the console. We just generate a |
| 633 | * kbdrequest event. |
| 634 | **/ |
| 635 | static void |
| 636 | kbd_handler (void *data, |
| 637 | NihSignal *signal) |
| 638 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 639 | NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 640 | } |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 641 | |
| 642 | /** |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 643 | * pwr_handler: |
| 644 | * @data: unused, |
| 645 | * @signal: signal that called this handler. |
| 646 | * |
| 647 | * Handle having recieved the SIGPWR signal, sent to us when powstatd |
| 648 | * changes the /etc/powerstatus file. We just generate a |
| 649 | * power-status-changed event and jobs read the file. |
| 650 | **/ |
| 651 | static void |
| 652 | pwr_handler (void *data, |
| 653 | NihSignal *signal) |
| 654 | { |
Scott James Remnant | d0258e0 | 2008-06-06 02:09:38 +0100 | [diff] [blame] | 655 | NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL)); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /** |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 659 | * hup_handler: |
| 660 | * @data: unused, |
| 661 | * @signal: signal that called this handler. |
| 662 | * |
| 663 | * Handle having recieved the SIGHUP signal, which we use to instruct us to |
| 664 | * reload our configuration. |
| 665 | **/ |
| 666 | static void |
| 667 | hup_handler (void *data, |
| 668 | NihSignal *signal) |
| 669 | { |
| 670 | nih_info (_("Reloading configuration")); |
| 671 | conf_reload (); |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 672 | } |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 673 | |
Scott James Remnant | e713805 | 2010-02-04 00:41:25 -0800 | [diff] [blame] | 674 | /** |
| 675 | * usr1_handler: |
| 676 | * @data: unused, |
| 677 | * @signal: signal that called this handler. |
| 678 | * |
| 679 | * Handle having recieved the SIGUSR signal, which we use to instruct us to |
| 680 | * reconnect to D-Bus. |
| 681 | **/ |
| 682 | static void |
| 683 | usr1_handler (void *data, |
| 684 | NihSignal *signal) |
| 685 | { |
Scott James Remnant | 911cb2e | 2009-07-08 22:40:50 +0100 | [diff] [blame] | 686 | if (! control_bus) { |
| 687 | nih_info (_("Reconnecting to system bus")); |
| 688 | |
| 689 | if (control_bus_open () < 0) { |
| 690 | NihError *err; |
| 691 | |
| 692 | err = nih_error_get (); |
| 693 | nih_warn ("%s: %s", _("Unable to connect to the system bus"), |
| 694 | err->message); |
| 695 | nih_free (err); |
| 696 | } |
| 697 | } |
Scott James Remnant | 7ba2cf6 | 2007-06-10 22:20:38 +0100 | [diff] [blame] | 698 | } |
Scott James Remnant | f3ef511 | 2008-06-05 01:26:10 +0100 | [diff] [blame] | 699 | #endif /* DEBUG */ |
Ricky Zhou | bedce60 | 2016-02-16 23:45:19 -0800 | [diff] [blame] | 700 | |
| 701 | #ifdef HAVE_SELINUX |
| 702 | /** |
| 703 | * initialize_selinux: |
| 704 | * |
| 705 | * Loads an SELinux policy and reexecs init to enter the the proper SELinux |
| 706 | * context. |
| 707 | **/ |
| 708 | void initialize_selinux (char **argv) |
| 709 | { |
| 710 | int enforce = 0; |
| 711 | FILE *checkreqprot_file; |
| 712 | const char *errstr; |
| 713 | |
| 714 | program_name = argv[0]; /* for logger_kmsg before NIH init */ |
| 715 | putenv ("SELINUX_INIT=YES"); |
| 716 | if (selinux_init_load_policy (&enforce) != 0) { |
| 717 | logger_kmsg (NIH_LOG_WARN, "SELinux policy failed to load"); |
| 718 | if (enforce > 0) { |
| 719 | /* Enforcing mode, must quit. */ |
| 720 | logger_kmsg (NIH_LOG_FATAL, |
| 721 | "no SELinux policy in enforcing mode: quit"); |
| 722 | exit (1); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | checkreqprot_file = fopen (CHECKREQPROT_PATH, "w"); |
| 727 | if (checkreqprot_file == NULL) { |
| 728 | errstr = strerror(errno); |
| 729 | logger_kmsg (NIH_LOG_FATAL, |
| 730 | "Failed to open " CHECKREQPROT_PATH); |
| 731 | logger_kmsg (NIH_LOG_FATAL, errstr); |
| 732 | exit (1); |
| 733 | } |
| 734 | if (fputc ('0', checkreqprot_file) == EOF) { |
| 735 | errstr = strerror(errno); |
| 736 | logger_kmsg (NIH_LOG_FATAL, |
| 737 | "Failed to write " CHECKREQPROT_PATH); |
| 738 | logger_kmsg (NIH_LOG_FATAL, errstr); |
| 739 | exit (1); |
| 740 | } |
| 741 | if (fclose (checkreqprot_file) != 0) { |
| 742 | errstr = strerror(errno); |
| 743 | logger_kmsg (NIH_LOG_FATAL, |
| 744 | "Failed to close " CHECKREQPROT_PATH); |
| 745 | logger_kmsg (NIH_LOG_FATAL, errstr); |
| 746 | exit (1); |
| 747 | } |
| 748 | |
| 749 | logger_kmsg (NIH_LOG_MESSAGE, "SELinux policy loaded, doing self-exec"); |
| 750 | execv (argv[0], argv); |
| 751 | errstr = strerror(errno); |
| 752 | |
| 753 | logger_kmsg (NIH_LOG_FATAL, "Failed to re-exec init."); |
| 754 | logger_kmsg (NIH_LOG_FATAL, errstr); |
| 755 | exit (1); |
| 756 | } |
| 757 | #endif /* HAVE_SELINUX */ |