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