blob: 37ef0597321b9eb15411163944b42983985c794a [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant540dcfd2011-03-16 15:54:56 -07003 * Copyright © 2011 Google Inc.
Scott James Remnante7138052010-02-04 00:41:25 -08004 * Copyright © 2010 Canonical Ltd.
Scott James Remnant7d5b2ea2009-05-22 15:20:12 +02005 * Author: Scott James Remnant <scott@netsplit.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01006 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2, as
Scott James Remnant75123022009-05-22 13:27:56 +02009 * published by the Free Software Foundation.
Scott James Remnant50748842006-05-16 21:02:31 +010010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Scott James Remnant0c0c5a52009-06-23 10:29:35 +010016 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Scott James Remnant50748842006-05-16 21:02:31 +010019 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif /* HAVE_CONFIG_H */
24
25
Scott James Remnantf43bdf32006-08-27 18:20:29 +010026#include <sys/types.h>
Scott James Remnantea806b72006-10-18 15:01:00 +010027#include <sys/time.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010028#include <sys/wait.h>
29#include <sys/ioctl.h>
30#include <sys/reboot.h>
31#include <sys/resource.h>
32
Scott James Remnant3401ab72006-09-01 02:14:47 +010033#include <errno.h>
34#include <stdio.h>
Scott James Remnant91da63a2011-08-11 13:47:00 -070035#include <limits.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010036#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020037#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010038#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020039#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020040#include <unistd.h>
41
Scott James Remnant097b2a92006-09-01 19:30:37 +010042#include <linux/kd.h>
43
Scott James Remnant77e8db32006-08-21 08:47:50 +020044#include <nih/macros.h>
45#include <nih/alloc.h>
46#include <nih/list.h>
47#include <nih/timer.h>
48#include <nih/signal.h>
49#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010050#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020051#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010052#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020053#include <nih/logging.h>
54
Scott James Remnantf2f69d02008-04-29 23:38:23 +010055#include "paths.h"
Scott James Remnant5d82d902008-04-30 00:03:29 +010056#include "events.h"
Scott James Remnantf2f69d02008-04-29 23:38:23 +010057#include "system.h"
Scott James Remnant91da63a2011-08-11 13:47:00 -070058#include "job_class.h"
Scott James Remnant63fd5c72008-04-30 21:34:31 +010059#include "job_process.h"
Scott James Remnant77e8db32006-08-21 08:47:50 +020060#include "event.h"
Scott James Remnant54b2a952007-06-10 22:15:24 +010061#include "conf.h"
Scott James Remnantf8491442008-04-18 13:19:24 +010062#include "control.h"
Scott James Remnant50748842006-05-16 21:02:31 +010063
64
Scott James Remnantf43bdf32006-08-27 18:20:29 +010065/* Prototypes for static functions */
Scott James Remnant9f62a8e2008-01-15 19:22:36 +000066#ifndef DEBUG
Scott James Remnant02977f82011-02-17 15:33:04 -080067static int logger_kmsg (NihLogLevel priority, const char *message);
Scott James Remnant16a286f2007-01-10 15:38:33 +000068static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010069static void cad_handler (void *data, NihSignal *signal);
70static void kbd_handler (void *data, NihSignal *signal);
Scott James Remnant2c950692007-02-25 09:13:38 +000071static void pwr_handler (void *data, NihSignal *signal);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +010072static void hup_handler (void *data, NihSignal *signal);
Scott James Remnante7138052010-02-04 00:41:25 -080073static void usr1_handler (void *data, NihSignal *signal);
Scott James Remnantf3ef5112008-06-05 01:26:10 +010074#endif /* DEBUG */
Scott James Remnant3401ab72006-09-01 02:14:47 +010075
Scott James Remnant1db88042006-09-01 03:14:19 +010076
Scott James Remnant3401ab72006-09-01 02:14:47 +010077/**
Scott James Remnant06abbec2007-03-09 13:02:38 +000078 * argv0:
79 *
80 * Path to program executed, used for re-executing the init binary from the
81 * same location we were executed from.
82 **/
83static const char *argv0 = NULL;
84
85/**
Scott James Remnant1db88042006-09-01 03:14:19 +010086 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010087 *
Scott James Remnant8b227402006-10-11 17:55:27 +010088 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010089 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010090 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010091static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010092
93
94/**
95 * options:
96 *
97 * Command-line options we accept.
98 **/
99static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +0100100 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +0100101
102 /* Ignore invalid options */
103 { '-', "--", NULL, NULL, NULL, NULL, NULL },
104
105 NIH_OPTION_LAST
106};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100107
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100108
Scott James Remnant50748842006-05-16 21:02:31 +0100109int
110main (int argc,
111 char *argv[])
112{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100113 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000114 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100115
Scott James Remnant06abbec2007-03-09 13:02:38 +0000116 argv0 = argv[0];
117 nih_main_init (argv0);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200118
Scott James Remnant930e25a2006-10-13 12:28:05 +0100119 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100120 nih_option_set_help (
121 _("This daemon is normally executed by the kernel and given "
122 "process id 1 to denote its special status. When executed "
123 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100124
Scott James Remnant3401ab72006-09-01 02:14:47 +0100125 args = nih_option_parser (NULL, argc, argv, options, FALSE);
126 if (! args)
127 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200128
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000129#ifndef DEBUG
Scott James Remnanta17917d2006-09-07 00:18:28 +0100130 /* Check we're root */
131 if (getuid ()) {
Scott James Remnant31421b72007-03-08 23:53:05 +0000132 nih_fatal (_("Need to be root"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100133 exit (1);
134 }
135
136 /* Check we're process #1 */
137 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100138 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100139 /* Ignore failure, probably just that telinit doesn't exist */
140
Scott James Remnant31421b72007-03-08 23:53:05 +0000141 nih_fatal (_("Not being executed as init"));
Scott James Remnanta17917d2006-09-07 00:18:28 +0100142 exit (1);
143 }
144
Scott James Remnant78626fb2007-01-10 16:48:10 +0000145 /* Clear our arguments from the command-line, so that we show up in
146 * ps or top output as /sbin/init, with no extra flags.
147 *
148 * This is a very Linux-specific trick; by deleting the NULL
149 * terminator at the end of the last argument, we fool the kernel
150 * into believing we used a setproctitle()-a-like to extend the
151 * argument space into the environment space, and thus make it use
152 * strlen() instead of its own assumed length. In fact, we've done
153 * the exact opposite, and shrunk the command line length to just that
154 * of whatever is in argv[0].
155 *
156 * If we don't do this, and just write \0 over the rest of argv, for
157 * example; the command-line length still includes those \0s, and ps
158 * will show whitespace in their place.
159 */
160 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000161 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000162
Scott James Remnant505f9282007-01-10 18:44:38 +0000163 arg_end = argv[argc-1] + strlen (argv[argc-1]);
164 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000165 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000166
Scott James Remnantfa733382008-01-15 15:33:27 +0000167
168 /* Become the leader of a new session and process group, shedding
169 * any controlling tty (which we shouldn't have had anyway - but
170 * you never know what initramfs did).
Scott James Remnant2e204b72007-02-03 23:15:28 +0000171 */
172 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100173
Scott James Remnantfa733382008-01-15 15:33:27 +0000174 /* Set the standard file descriptors to the ordinary console device,
175 * resetting it to sane defaults unless we're inheriting from another
176 * init process which we know left it in a sane state.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200177 */
Scott James Remnant93216182011-08-11 13:30:15 -0700178 if (system_setup_console (CONSOLE_OUTPUT, (! restart)) < 0) {
179 NihError *err;
180
181 err = nih_error_get ();
182 nih_warn ("%s: %s", _("Unable to initialize console, will try /dev/null"),
183 err->message);
184 nih_free (err);
185
186 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
187 err = nih_error_get ();
188 nih_fatal ("%s: %s", _("Unable to initialize console as /dev/null"),
189 err->message);
190 nih_free (err);
191
192 exit (1);
193 }
194 }
Scott James Remnant77e8db32006-08-21 08:47:50 +0200195
Scott James Remnant2e204b72007-02-03 23:15:28 +0000196 /* Set the PATH environment variable */
197 setenv ("PATH", PATH, TRUE);
198
Scott James Remnantfa733382008-01-15 15:33:27 +0000199 /* Switch to the root directory in case we were started from some
200 * strange place, or worse, some directory in the initramfs that's
201 * going to go away soon.
202 */
Scott James Remnant5635e072008-05-06 23:22:58 +0100203 if (chdir ("/"))
204 nih_warn ("%s: %s", _("Unable to set root directory"),
205 strerror (errno));
Scott James Remnantd69c1da2010-02-26 15:29:07 +0000206
207 /* Mount the /proc and /sys filesystems, which are pretty much
208 * essential for any Linux system; not to mention used by
209 * ourselves.
210 */
211 if (system_mount ("proc", "/proc") < 0) {
212 NihError *err;
213
214 err = nih_error_get ();
215 nih_warn ("%s: %s", _("Unable to mount /proc filesystem"),
216 err->message);
217 nih_free (err);
218 }
219
220 if (system_mount ("sysfs", "/sys") < 0) {
221 NihError *err;
222
223 err = nih_error_get ();
224 nih_warn ("%s: %s", _("Unable to mount /sys filesystem"),
225 err->message);
226 nih_free (err);
227 }
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000228#else /* DEBUG */
229 nih_log_set_priority (NIH_LOG_DEBUG);
James Hunt39f1c4f2010-12-13 18:15:24 +0000230 nih_debug ("Running as PID %d (PPID %d)",
231 (int)getpid (), (int)getppid ());
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000232#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000233
Scott James Remnant2e204b72007-02-03 23:15:28 +0000234
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100235 /* Reset the signal state and install the signal handler for those
236 * signals we actually want to catch; this also sets those that
237 * can be sent to us, because we're special
238 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100239 if (! restart)
Scott James Remnant2e204b72007-02-03 23:15:28 +0000240 nih_signal_reset ();
241
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100242#ifndef DEBUG
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000243 /* Catch fatal errors immediately rather than waiting for a new
Scott James Remnantfa733382008-01-15 15:33:27 +0000244 * iteration through the main loop.
245 */
246 nih_signal_set_handler (SIGSEGV, crash_handler);
247 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000248#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000249
Scott James Remnant5c408432007-11-07 21:42:25 -0500250 /* Don't ignore SIGCHLD or SIGALRM, but don't respond to them
251 * directly; it's enough that they interrupt the main loop and
252 * get dealt with during it.
253 */
254 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
255 nih_signal_set_handler (SIGALRM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100256
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000257#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100258 /* Ask the kernel to send us SIGINT when control-alt-delete is
259 * pressed; generate an event with the same name.
260 */
261 reboot (RB_DISABLE_CAD);
Scott James Remnant5c408432007-11-07 21:42:25 -0500262 nih_signal_set_handler (SIGINT, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000263 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100264
265 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100266 * generate a keyboard-request event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100267 */
Scott James Remnant5c408432007-11-07 21:42:25 -0500268 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) {
269 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant5d702952007-01-05 17:21:34 +0000270 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
271 kbd_handler, NULL));
Scott James Remnant5c408432007-11-07 21:42:25 -0500272 }
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100273
Scott James Remnant2c950692007-02-25 09:13:38 +0000274 /* powstatd sends us SIGPWR when it changes /etc/powerstatus */
Scott James Remnant5c408432007-11-07 21:42:25 -0500275 nih_signal_set_handler (SIGPWR, nih_signal_handler);
Scott James Remnant2c950692007-02-25 09:13:38 +0000276 NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL));
277
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100278 /* SIGHUP instructs us to re-load our configuration */
Scott James Remnant5c408432007-11-07 21:42:25 -0500279 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100280 NIH_MUST (nih_signal_add_handler (NULL, SIGHUP, hup_handler, NULL));
Scott James Remnante7138052010-02-04 00:41:25 -0800281
282 /* SIGUSR1 instructs us to reconnect to D-Bus */
283 nih_signal_set_handler (SIGUSR1, nih_signal_handler);
284 NIH_MUST (nih_signal_add_handler (NULL, SIGUSR1, usr1_handler, NULL));
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100285#endif /* DEBUG */
Scott James Remnant1db88042006-09-01 03:14:19 +0100286
Scott James Remnantfa733382008-01-15 15:33:27 +0000287
Scott James Remnant0eade492007-11-15 05:48:07 +0000288 /* Watch children for events */
Scott James Remnant5ebc6c62007-12-06 16:01:13 +0000289 NIH_MUST (nih_child_add_watch (NULL, -1, NIH_CHILD_ALL,
Scott James Remnant63fd5c72008-04-30 21:34:31 +0100290 job_process_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100291
Scott James Remnant5c408432007-11-07 21:42:25 -0500292 /* Process the event queue each time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000293 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000294 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100295
Scott James Remnant94d00982006-08-25 15:38:22 +0200296
Scott James Remnant91da63a2011-08-11 13:47:00 -0700297 /* Adjust our OOM priority to the default, which will be inherited
298 * by all jobs.
299 */
300 if (JOB_DEFAULT_OOM_SCORE_ADJ) {
301 char filename[PATH_MAX];
302 int oom_value;
303 FILE *fd;
304
305 snprintf (filename, sizeof (filename),
306 "/proc/%d/oom_score_adj", getpid ());
307 oom_value = JOB_DEFAULT_OOM_SCORE_ADJ;
308 fd = fopen (filename, "w");
Scott James Remnant4c0fa532011-08-11 13:52:28 -0700309 if ((! fd) && (errno == ENOENT)) {
Scott James Remnant91da63a2011-08-11 13:47:00 -0700310 snprintf (filename, sizeof (filename),
311 "/proc/%d/oom_adj", getpid ());
312 oom_value = (JOB_DEFAULT_OOM_SCORE_ADJ
313 * ((JOB_DEFAULT_OOM_SCORE_ADJ < 0) ? 17 : 15)) / 1000;
314 fd = fopen (filename, "w");
315 }
316 if (! fd) {
317 nih_warn ("%s: %s", _("Unable to set default oom score"),
318 strerror (errno));
319 } else {
320 fprintf (fd, "%d\n", oom_value);
321
322 if (fclose (fd))
323 nih_warn ("%s: %s", _("Unable to set default oom score"),
324 strerror (errno));
325 }
326 }
327
328
Scott James Remnant54b2a952007-06-10 22:15:24 +0100329 /* Read configuration */
Scott James Remnant854fda22009-06-23 01:21:55 +0100330 NIH_MUST (conf_source_new (NULL, CONFFILE, CONF_FILE));
331 NIH_MUST (conf_source_new (NULL, CONFDIR, CONF_JOB_DIR));
Scott James Remnant54b2a952007-06-10 22:15:24 +0100332
333 conf_reload ();
Scott James Remnantd03c53c2007-03-13 19:13:19 +0000334
Scott James Remnant1ba7f7a2008-05-07 23:52:11 +0100335 /* Create a listening server for private connections. */
336 while (control_server_open () < 0) {
337 NihError *err;
338
339 err = nih_error_get ();
340 if (err->number != ENOMEM) {
341 nih_warn ("%s: %s", _("Unable to listen for private connections"),
342 err->message);
343 nih_free (err);
344 break;
345 }
346 nih_free (err);
347 }
348
Scott James Remnantf8491442008-04-18 13:19:24 +0100349 /* Open connection to the system bus; we normally expect this to
350 * fail and will try again later - don't let ENOMEM stop us though.
351 */
352 while (control_bus_open () < 0) {
353 NihError *err;
354 int number;
355
356 err = nih_error_get ();
357 number = err->number;
358 nih_free (err);
359
360 if (number != ENOMEM)
361 break;
362 }
363
Scott James Remnant2c0bd592008-04-12 12:31:49 +0100364#ifndef DEBUG
Scott James Remnantfa733382008-01-15 15:33:27 +0000365 /* Now that the startup is complete, send all further logging output
Scott James Remnant02977f82011-02-17 15:33:04 -0800366 * to kmsg instead of to the console.
Scott James Remnantfa733382008-01-15 15:33:27 +0000367 */
Scott James Remnant93216182011-08-11 13:30:15 -0700368 if (system_setup_console (CONSOLE_NONE, FALSE) < 0) {
369 NihError *err;
370
371 err = nih_error_get ();
372 nih_fatal ("%s: %s", _("Unable to setup standard file descriptors"),
373 err->message);
374 nih_free (err);
375
376 exit (1);
377 }
Scott James Remnant540dcfd2011-03-16 15:54:56 -0700378
Scott James Remnant02977f82011-02-17 15:33:04 -0800379 nih_log_set_logger (logger_kmsg);
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000380#endif /* DEBUG */
Scott James Remnantfa733382008-01-15 15:33:27 +0000381
382
Scott James Remnant3401ab72006-09-01 02:14:47 +0100383 /* Generate and run the startup event or read the state from the
384 * init daemon that exec'd us
385 */
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100386 if (! restart) {
Scott James Remnantd0258e02008-06-06 02:09:38 +0100387 NIH_MUST (event_new (NULL, STARTUP_EVENT, NULL));
Scott James Remnant3401ab72006-09-01 02:14:47 +0100388 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100389 sigset_t mask;
390
Scott James Remnant1db88042006-09-01 03:14:19 +0100391 /* We're ok to receive signals again */
392 sigemptyset (&mask);
393 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100394 }
395
Scott James Remnantfa733382008-01-15 15:33:27 +0000396 /* Run through the loop at least once to deal with signals that were
397 * delivered to the previous process while the mask was set or to
398 * process the startup event we emitted.
399 */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000400 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200401 ret = nih_main_loop ();
402
403 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100404}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100405
406
Scott James Remnant9f62a8e2008-01-15 19:22:36 +0000407#ifndef DEBUG
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100408/**
Scott James Remnant02977f82011-02-17 15:33:04 -0800409 * logger_kmsg:
410 * @priority: priority of message being logged,
411 * @message: message to log.
412 *
413 * Outputs the @message to the kernel log message socket prefixed with an
414 * appropriate tag based on @priority, the program name and terminated with
415 * a new line.
416 *
417 * Returns: zero on success, negative value on error.
418 **/
419static int
420logger_kmsg (NihLogLevel priority,
421 const char *message)
422{
423 int tag;
424 FILE *kmsg;
425
426 nih_assert (message != NULL);
427
428 switch (priority) {
429 case NIH_LOG_DEBUG:
430 tag = '7';
431 break;
432 case NIH_LOG_INFO:
433 tag = '6';
434 break;
435 case NIH_LOG_MESSAGE:
436 tag = '5';
437 break;
438 case NIH_LOG_WARN:
439 tag = '4';
440 break;
441 case NIH_LOG_ERROR:
442 tag = '3';
443 break;
444 case NIH_LOG_FATAL:
445 tag = '2';
446 break;
447 default:
448 tag = 'd';
449 }
450
451 kmsg = fopen ("/dev/kmsg", "w");
452 if (! kmsg)
453 return -1;
454
455 if (fprintf (kmsg, "<%c>%s: %s\n", tag, program_name, message) < 0) {
456 int saved_errno = errno;
457 fclose (kmsg);
458 errno = saved_errno;
459 return -1;
460 }
461
462 if (fclose (kmsg) < 0)
463 return -1;
464
465 return 0;
466}
467
468
469/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000470 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100471 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100472 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000473 * Handle receiving the SEGV or ABRT signal, usually caused by one of
474 * our own mistakes. We deal with it by dumping core in a child process
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100475 * and then killing the parent.
Scott James Remnant16a286f2007-01-10 15:38:33 +0000476 *
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100477 * Sadly there's no real alternative to the ensuing kernel panic. Our
478 * state is likely in tatters, so we can't sigjmp() anywhere "safe" or
479 * re-exec since the system will be suddenly lobotomised. We definitely
480 * don't want to start a root shell or anything like that. Best thing is
481 * to just stop the whole thing and hope that bug report comes quickly.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100482 **/
483static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000484crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100485{
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100486 pid_t pid;
Scott James Remnant06abbec2007-03-09 13:02:38 +0000487
488 nih_assert (argv0 != NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100489
490 pid = fork ();
491 if (pid == 0) {
492 struct sigaction act;
493 struct rlimit limit;
494 sigset_t mask;
495
496 /* Mask out all signals */
497 sigfillset (&mask);
498 sigprocmask (SIG_SETMASK, &mask, NULL);
499
Scott James Remnant16a286f2007-01-10 15:38:33 +0000500 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100501 act.sa_handler = SIG_DFL;
502 act.sa_flags = 0;
503 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000504 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100505
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100506 /* Don't limit the core dump size */
507 limit.rlim_cur = RLIM_INFINITY;
508 limit.rlim_max = RLIM_INFINITY;
509 setrlimit (RLIMIT_CORE, &limit);
510
Scott James Remnantfa733382008-01-15 15:33:27 +0000511 /* Dump in the root directory */
Scott James Remnant9d736bb2009-07-21 18:15:20 +0100512 if (chdir ("/"))
513 nih_warn ("%s: %s", _("Unable to set root directory"),
514 strerror (errno));
Scott James Remnantfa733382008-01-15 15:33:27 +0000515
Scott James Remnant16a286f2007-01-10 15:38:33 +0000516 /* Raise the signal again */
517 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100518
519 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000520 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100521 sigprocmask (SIG_SETMASK, &mask, NULL);
522
523 /* Wait for death */
524 pause ();
525 exit (0);
526 } else if (pid > 0) {
527 /* Wait for the core to be generated */
528 waitpid (pid, NULL, 0);
529
Scott James Remnant31421b72007-03-08 23:53:05 +0000530 nih_fatal (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000531 (signum == SIGSEGV
532 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100533 } else {
Scott James Remnant31421b72007-03-08 23:53:05 +0000534 nih_fatal (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000535 (signum == SIGSEGV
536 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100537 }
Scott James Remnant502ea702007-03-05 20:47:18 +0000538
Scott James Remnant4b56d6f2008-04-12 12:23:15 +0100539 /* Goodbye, cruel world. */
540 exit (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100541}
542
543/**
544 * cad_handler:
545 * @data: unused,
546 * @signal: signal that called this handler.
547 *
548 * Handle having recieved the SIGINT signal, sent to us when somebody
549 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100550 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100551 **/
552static void
553cad_handler (void *data,
554 NihSignal *signal)
555{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100556 NIH_MUST (event_new (NULL, CTRLALTDEL_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100557}
558
559/**
560 * kbd_handler:
561 * @data: unused,
562 * @signal: signal that called this handler.
563 *
564 * Handle having recieved the SIGWINCH signal, sent to us when somebody
565 * presses Alt-UpArrow on the console. We just generate a
566 * kbdrequest event.
567 **/
568static void
569kbd_handler (void *data,
570 NihSignal *signal)
571{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100572 NIH_MUST (event_new (NULL, KBDREQUEST_EVENT, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100573}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100574
575/**
Scott James Remnant2c950692007-02-25 09:13:38 +0000576 * pwr_handler:
577 * @data: unused,
578 * @signal: signal that called this handler.
579 *
580 * Handle having recieved the SIGPWR signal, sent to us when powstatd
581 * changes the /etc/powerstatus file. We just generate a
582 * power-status-changed event and jobs read the file.
583 **/
584static void
585pwr_handler (void *data,
586 NihSignal *signal)
587{
Scott James Remnantd0258e02008-06-06 02:09:38 +0100588 NIH_MUST (event_new (NULL, PWRSTATUS_EVENT, NULL));
Scott James Remnant2c950692007-02-25 09:13:38 +0000589}
590
591/**
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100592 * hup_handler:
593 * @data: unused,
594 * @signal: signal that called this handler.
595 *
596 * Handle having recieved the SIGHUP signal, which we use to instruct us to
597 * reload our configuration.
598 **/
599static void
600hup_handler (void *data,
601 NihSignal *signal)
602{
603 nih_info (_("Reloading configuration"));
604 conf_reload ();
Scott James Remnante7138052010-02-04 00:41:25 -0800605}
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100606
Scott James Remnante7138052010-02-04 00:41:25 -0800607/**
608 * usr1_handler:
609 * @data: unused,
610 * @signal: signal that called this handler.
611 *
612 * Handle having recieved the SIGUSR signal, which we use to instruct us to
613 * reconnect to D-Bus.
614 **/
615static void
616usr1_handler (void *data,
617 NihSignal *signal)
618{
Scott James Remnant911cb2e2009-07-08 22:40:50 +0100619 if (! control_bus) {
620 nih_info (_("Reconnecting to system bus"));
621
622 if (control_bus_open () < 0) {
623 NihError *err;
624
625 err = nih_error_get ();
626 nih_warn ("%s: %s", _("Unable to connect to the system bus"),
627 err->message);
628 nih_free (err);
629 }
630 }
Scott James Remnant7ba2cf62007-06-10 22:20:38 +0100631}
Scott James Remnantf3ef5112008-06-05 01:26:10 +0100632#endif /* DEBUG */