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