12 #include <sys/socket.h>
18 #include <sys/prctl.h>
20 #include <sys/reboot.h>
22 #if defined(__FreeBSD__) || defined(__DragonFly__)
23 #include <sys/procctl.h>
30 #include "dinit-log.h"
31 #include "dinit-socket.h"
32 #include "static-string.h"
33 #include "dinit-utmp.h"
38 * When running as the system init process, Dinit processes the following signals:
40 * SIGTERM - roll back services and then fork/exec /sbin/halt
41 * SIGINT - roll back services and then fork/exec /sbin/reboot
42 * SIGQUIT - exec() /sbin/shutdown without rolling back services
44 * It's an open question about whether Dinit should roll back services *before*
45 * running halt/reboot, since those commands should prompt rollback of services
46 * anyway. But it seems safe to do so, and it means the user can at least stop
47 * services even if the halt/reboot commands are unavailable for some reason.
52 using eventloop_t = dasynq::event_loop<dasynq::null_mutex>;
54 eventloop_t event_loop;
56 static void sigint_reboot_cb(eventloop_t &eloop) noexcept;
57 static void sigquit_cb(eventloop_t &eloop) noexcept;
58 static void sigterm_cb(eventloop_t &eloop) noexcept;
59 static void open_control_socket(bool report_ro_failure = true) noexcept;
60 static void close_control_socket() noexcept;
61 static void confirm_restart_boot() noexcept;
62 static void read_env_file(const char *);
64 static void control_socket_cb(eventloop_t *loop, int fd);
69 static dirload_service_set *services;
71 static bool am_pid_one = false; // true if we are PID 1
72 static bool am_system_init = false; // true if we are the system init process
74 static bool did_log_boot = false;
75 static bool control_socket_open = false;
76 static bool external_log_open = false;
77 int active_control_conns = 0;
79 // Control socket path. We maintain a string (control_socket_str) in case we need
80 // to allocate storage, but control_socket_path is the authoritative value.
81 static const char *control_socket_path = SYSCONTROLSOCKET;
82 static std::string control_socket_str;
84 static const char *env_file_path = "/etc/dinit/environment";
86 static const char *log_path = "/dev/log";
87 static bool log_is_syslog = true; // if false, log is a file
89 static const char *user_home_path = nullptr;
91 // Set to true (when console_input_watcher is active) if console input becomes available
92 static bool console_input_ready = false;
94 // Get user home (and set user_home_path). (The return may become invalid after
95 // changing the evironment (HOME variable) or using the getpwuid() function).
96 static const char * get_user_home()
98 if (user_home_path == nullptr) {
99 user_home_path = getenv("HOME");
100 if (user_home_path == nullptr) {
101 struct passwd * pwuid_p = getpwuid(getuid());
102 if (pwuid_p != nullptr) {
103 user_home_path = pwuid_p->pw_dir;
107 return user_home_path;
112 // Event-loop handler for a signal, which just delegates to a function (pointer).
113 class callback_signal_handler : public eventloop_t::signal_watcher_impl<callback_signal_handler>
115 using rearm = dasynq::rearm;
118 typedef void (*cb_func_t)(eventloop_t &);
124 callback_signal_handler() : cb_func(nullptr) { }
125 callback_signal_handler(cb_func_t pcb_func) : cb_func(pcb_func) { }
127 void set_cb_func(cb_func_t cb_func)
129 this->cb_func = cb_func;
132 rearm received(eventloop_t &eloop, int signo, siginfo_p siginfo)
139 // Event-loop handler for when a connection is made to the control socket.
140 class control_socket_watcher : public eventloop_t::fd_watcher_impl<control_socket_watcher>
142 using rearm = dasynq::rearm;
145 rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept
147 control_socket_cb(&loop, fd);
152 // Watch for console input and set a flag when it is available.
153 class console_input_watcher : public eventloop_t::fd_watcher_impl<console_input_watcher>
155 using rearm = dasynq::rearm;
158 rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept
160 console_input_ready = true;
161 return rearm::DISARM;
165 // Simple timer used to limit the amount of time waiting for the log flush to complete (at shutdown)
166 class log_flush_timer_t : public eventloop_t::timer_impl<log_flush_timer_t>
168 using rearm = dasynq::rearm;
173 rearm timer_expiry(eventloop_t &, int expiry_count)
176 return rearm::DISARM;
190 control_socket_watcher control_socket_io;
191 console_input_watcher console_input_io;
192 log_flush_timer_t log_flush_timer;
195 int dinit_main(int argc, char **argv)
199 am_pid_one = (getpid() == 1);
200 am_system_init = (getuid() == 0);
201 const char * service_dir = nullptr;
202 bool service_dir_dynamic = false; // service_dir dynamically allocated?
203 const char * env_file = nullptr;
204 bool control_socket_path_set = false;
205 bool env_file_set = false;
207 // list of services to start
208 list<const char *> services_to_start;
210 // Arguments, if given, specify a list of services to start.
211 // If we are running as init (PID=1), the Linux kernel gives us any command line arguments it was given
212 // but didn't recognize, including "single" (usually for "boot to single user mode" aka just start the
213 // shell). We can treat them as service names. In the worst case we can't find any of the named
214 // services, and so we'll start the "boot" service by default.
216 for (int i = 1; i < argc; i++) {
217 if (argv[i][0] == '-') {
219 if (strcmp(argv[i], "--env-file") == 0 || strcmp(argv[i], "-e") == 0) {
225 cerr << "dinit: '--env-file' (-e) requires an argument" << endl;
228 else if (strcmp(argv[i], "--services-dir") == 0 || strcmp(argv[i], "-d") == 0) {
230 service_dir = argv[i];
233 cerr << "dinit: '--services-dir' (-d) requires an argument" << endl;
237 else if (strcmp(argv[i], "--system") == 0 || strcmp(argv[i], "-s") == 0) {
238 am_system_init = true;
240 else if (strcmp(argv[i], "--user") == 0 || strcmp(argv[i], "-u") == 0) {
241 am_system_init = false;
243 else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
245 control_socket_path = argv[i];
246 control_socket_path_set = true;
249 cerr << "dinit: '--socket-path' (-p) requires an argument" << endl;
253 else if (strcmp(argv[i], "--log-file") == 0 || strcmp(argv[i], "-l") == 0) {
256 log_is_syslog = false;
259 cerr << "dinit: '--log-file' (-l) requires an argument" << endl;
263 else if (strcmp(argv[i], "--quiet") == 0 || strcmp(argv[i], "-q") == 0) {
264 console_service_status = false;
265 log_level[DLOG_CONS] = loglevel_t::ZERO;
267 else if (strcmp(argv[i], "--help") == 0) {
268 cout << "dinit, an init with dependency management\n"
269 " --help display help\n"
270 " --env-file <file>, -e <file>\n"
271 " environment variable initialisation file\n"
272 " --services-dir <dir>, -d <dir>\n"
273 " set base directory for service description\n"
274 " files (-d <dir>)\n"
275 " --system, -s run as the system service manager\n"
276 " --user, -u run as a user service manager\n"
277 " --socket-path <path>, -p <path>\n"
278 " path to control socket\n"
279 " --quiet, -q disable output to standard output\n"
280 " <service-name> start service with name <service-name>\n";
285 if (! am_system_init) {
286 cerr << "dinit: Unrecognized option: " << argv[i] << endl;
293 // LILO puts "auto" on the kernel command line for unattended boots; we'll filter it.
294 if (! am_pid_one || strcmp(argv[i], "auto") != 0) {
295 services_to_start.push_back(argv[i]);
298 services_to_start.push_back(argv[i]);
304 if (am_system_init) {
305 // setup STDIN, STDOUT, STDERR so that we can use them
306 int onefd = open("/dev/console", O_RDONLY, 0);
308 int twofd = open("/dev/console", O_RDWR, 0);
312 if (onefd > 2) close(onefd);
313 if (twofd > 2) close(twofd);
315 if (! env_file_set) {
316 env_file = env_file_path;
320 /* Set up signal handlers etc */
321 /* SIG_CHILD is ignored by default: good */
322 sigset_t sigwait_set;
323 sigemptyset(&sigwait_set);
324 sigaddset(&sigwait_set, SIGCHLD);
325 sigaddset(&sigwait_set, SIGINT);
326 sigaddset(&sigwait_set, SIGTERM);
327 if (am_pid_one) sigaddset(&sigwait_set, SIGQUIT);
328 sigprocmask(SIG_BLOCK, &sigwait_set, NULL);
330 // Terminal access control signals - we block these so that dinit can't be
331 // suspended if it writes to the terminal after some other process has claimed
333 signal(SIGTSTP, SIG_IGN);
334 signal(SIGTTIN, SIG_IGN);
335 signal(SIGTTOU, SIG_IGN);
337 signal(SIGPIPE, SIG_IGN);
339 if (! am_system_init && ! control_socket_path_set) {
340 const char * userhome = get_user_home();
341 if (userhome != nullptr) {
342 control_socket_str = userhome;
343 control_socket_str += "/.dinitctl";
344 control_socket_path = control_socket_str.c_str();
348 /* service directory name */
349 if (service_dir == nullptr && ! am_system_init) {
350 const char * userhome = get_user_home();
351 if (userhome != nullptr) {
352 const char * user_home = get_user_home();
353 size_t user_home_len = strlen(user_home);
354 size_t dinit_d_len = strlen("/dinit.d");
355 size_t full_len = user_home_len + dinit_d_len + 1;
356 char *service_dir_w = new char[full_len];
357 std::memcpy(service_dir_w, user_home, user_home_len);
358 std::memcpy(service_dir_w + user_home_len, "/dinit.d", dinit_d_len);
359 service_dir_w[full_len - 1] = 0;
361 service_dir = service_dir_w;
362 service_dir_dynamic = true;
366 if (services_to_start.empty()) {
367 services_to_start.push_back("boot");
370 // Set up signal handlers
371 callback_signal_handler sigterm_watcher {sigterm_cb};
372 callback_signal_handler sigint_watcher;
373 callback_signal_handler sigquit_watcher;
376 sigint_watcher.set_cb_func(sigint_reboot_cb);
377 sigquit_watcher.set_cb_func(sigquit_cb);
380 sigint_watcher.set_cb_func(sigterm_cb);
383 sigint_watcher.add_watch(event_loop, SIGINT);
384 sigterm_watcher.add_watch(event_loop, SIGTERM);
385 console_input_io.add_watch(event_loop, STDIN_FILENO, dasynq::IN_EVENTS, false);
388 // PID 1: SIGQUIT exec's shutdown
389 sigquit_watcher.add_watch(event_loop, SIGQUIT);
390 // As a user process, we instead just let SIGQUIT perform the default action.
393 // Try to open control socket (may fail due to readonly filesystem)
394 open_control_socket(false);
397 if (am_system_init) {
398 // Disable non-critical kernel output to console
399 klogctl(6 /* SYSLOG_ACTION_CONSOLE_OFF */, nullptr, 0);
400 // Make ctrl+alt+del combination send SIGINT to PID 1 (this process)
401 reboot(RB_DISABLE_CAD);
404 // Mark ourselves as a subreaper. This means that if a process we start double-forks, the
405 // orphaned child will re-parent to us rather than to PID 1 (although that could be us too).
406 prctl(PR_SET_CHILD_SUBREAPER, 1);
407 #elif defined(__FreeBSD__) || defined(__DragonFly__)
408 // Documentation (man page) for this kind of sucks. PROC_REAP_ACQUIRE "acquires the reaper status for
409 // the current process" but does that mean the first two arguments still need valid values to be
410 // supplied? We'll play it safe and explicitly target our own process:
411 procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL);
414 log_flush_timer.add_timer(event_loop, dasynq::clock_type::MONOTONIC);
416 bool add_all_service_dirs = false;
417 if (service_dir == nullptr) {
418 service_dir = "/etc/dinit.d";
419 add_all_service_dirs = true;
422 /* start requested services */
423 services = new dirload_service_set(service_dir, service_dir_dynamic);
424 if (add_all_service_dirs) {
425 services->add_service_dir("/usr/local/lib/dinit.d", false);
426 services->add_service_dir("/lib/dinit.d", false);
429 init_log(services, log_is_syslog);
430 if (am_system_init) {
431 log(loglevel_t::INFO, false, "starting system");
434 // Only try to set up the external log now if we aren't the system init. (If we are the
435 // system init, wait until the log service starts).
436 if (! am_system_init) setup_external_log();
438 if (env_file != nullptr) {
439 read_env_file(env_file);
442 for (auto svc : services_to_start) {
444 services->start_service(svc);
445 // Note in general if we fail to start a service we don't need any special error handling,
446 // since we either leave other services running or, if it was the only service, then no
447 // services will be running and we will process normally (reboot if system process,
448 // exit if user process).
450 catch (service_not_found &snf) {
451 log(loglevel_t::ERROR, snf.service_name, ": Could not find service description.");
453 catch (service_load_exc &sle) {
454 log(loglevel_t::ERROR, sle.service_name, ": ", sle.exc_description);
456 catch (std::bad_alloc &badalloce) {
457 log(loglevel_t::ERROR, "Out of memory when trying to start service: ", svc, ".");
464 // Process events until all services have terminated.
465 while (services->count_active_services() != 0) {
469 shutdown_type_t shutdown_type = services->get_shutdown_type();
470 if (shutdown_type == shutdown_type_t::REMAIN) {
475 log_msg_begin(loglevel_t::INFO, "No more active services.");
477 if (shutdown_type == shutdown_type_t::REBOOT) {
478 log_msg_end(" Will reboot.");
480 else if (shutdown_type == shutdown_type_t::HALT) {
481 log_msg_end(" Will halt.");
483 else if (shutdown_type == shutdown_type_t::POWEROFF) {
484 log_msg_end(" Will power down.");
488 log_flush_timer.reset();
489 log_flush_timer.arm_timer_rel(event_loop, timespec{5,0}); // 5 seconds
490 while (! is_log_flushed() && ! log_flush_timer.has_expired()) {
494 close_control_socket();
497 if (shutdown_type == shutdown_type_t::NONE) {
498 // Services all stopped but there was no shutdown issued. Inform user, wait for ack, and
499 // re-start boot sequence.
500 sync(); // Sync to minimise data loss if user elects to power off / hard reset
501 confirm_restart_boot();
502 if (services->count_active_services() != 0) {
503 // Recovery service started
506 shutdown_type = services->get_shutdown_type();
507 if (shutdown_type == shutdown_type_t::NONE) {
509 services->start_service("boot");
510 goto run_event_loop; // yes, the "evil" goto
513 // Now what do we do? try to reboot, but wait for user ack to avoid boot loop.
514 log(loglevel_t::ERROR, "Could not start 'boot' service. Will attempt reboot.");
515 shutdown_type = shutdown_type_t::REBOOT;
520 const char * cmd_arg;
521 if (shutdown_type == shutdown_type_t::HALT) {
524 else if (shutdown_type == shutdown_type_t::REBOOT) {
532 // Fork and execute dinit-reboot.
533 constexpr auto shutdown_exec = literal(SBINDIR) + "/shutdown";
534 execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", cmd_arg, nullptr);
535 log(loglevel_t::ERROR, (literal("Could not execute ") + SBINDIR + "/shutdown: ").c_str(),
538 // PID 1 must not actually exit, although we should never reach this point:
543 else if (shutdown_type == shutdown_type_t::REBOOT) {
544 // Non-system-process. If we got SIGINT, let's die due to it:
545 sigset_t sigwait_set_int;
546 sigemptyset(&sigwait_set_int);
547 sigaddset(&sigwait_set_int, SIGINT);
549 sigprocmask(SIG_UNBLOCK, &sigwait_set_int, NULL);
555 // Log a parse error when reading the environment file.
556 static void log_bad_env(int linenum)
558 log(loglevel_t::ERROR, "invalid environment variable setting in environment file (line ", linenum, ")");
561 // Read and set environment variables from a file.
562 static void read_env_file(const char *env_file_path)
564 // Note that we can't use the log in this function; it hasn't been initialised yet.
566 std::ifstream env_file(env_file_path);
567 if (! env_file) return;
569 env_file.exceptions(std::ios::badbit);
571 auto &clocale = std::locale::classic();
575 while (std::getline(env_file, line)) {
577 auto lpos = line.begin();
578 auto lend = line.end();
579 while (lpos != lend && std::isspace(*lpos, clocale)) {
586 log_bad_env(linenum);
589 auto name_begin = lpos++;
590 // skip until '=' or whitespace:
591 while (lpos != lend && *lpos != '=' && ! std::isspace(*lpos, clocale)) ++lpos;
592 auto name_end = lpos;
594 while (lpos != lend && std::isspace(*lpos, clocale)) ++lpos;
596 log_bad_env(linenum);
601 auto val_begin = lpos;
602 while (lpos != lend && *lpos != '\n') ++lpos;
605 std::string name = line.substr(name_begin - line.begin(), name_end - name_begin);
606 std::string value = line.substr(val_begin - line.begin(), val_end - val_begin);
607 if (setenv(name.c_str(), value.c_str(), true) == -1) {
608 throw std::system_error(errno, std::system_category());
615 // Get user confirmation before proceeding with restarting boot sequence.
616 // Returns after confirmation, possibly with shutdown type altered.
617 static void confirm_restart_boot() noexcept
619 // Bypass log; we want to make certain the message is seen:
620 std::cout << "All services have stopped with no shutdown issued; boot failure?\n";
622 // Drain input, set non-canonical input mode (receive characters as they are typed)
623 struct termios term_attr;
624 if (tcgetattr(STDIN_FILENO, &term_attr) != 0) {
626 std::cout << "Halting." << std::endl;
627 services->stop_all_services(shutdown_type_t::HALT);
630 term_attr.c_lflag &= ~ICANON;
631 tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr);
633 // Set non-blocking mode
634 int origFlags = fcntl(STDIN_FILENO, F_GETFL);
635 fcntl(STDIN_FILENO, F_SETFL, origFlags | O_NONBLOCK);
638 std::cout << "Choose: (r)eboot, r(e)covery, re(s)tart boot sequence, (p)ower off? " << std::flush;
640 console_input_io.set_enabled(event_loop, true);
643 } while (! console_input_ready && services->get_shutdown_type() == shutdown_type_t::NONE);
644 console_input_io.set_enabled(event_loop, false);
646 // We either have input, or shutdown type has been set, or both.
647 if (console_input_ready) {
648 console_input_ready = false;
650 int r = read(STDIN_FILENO, buf, 1); // read a single character, to make sure we wait for input
652 std::cout << "\n"; // force new line after input
653 if (buf[0] == 'r' || buf[0] == 'R') {
654 services->stop_all_services(shutdown_type_t::REBOOT);
656 else if (buf[0] == 'e' || buf[0] == 'E') {
658 services->start_service("recovery");
661 std::cout << "Unable to start recovery service.\n";
665 else if (buf[0] == 's' || buf[0] == 'S') {
666 // nothing - leave no shutdown type
668 else if (buf[0] == 'p' || buf[0] == 'P') {
669 services->stop_all_services(shutdown_type_t::POWEROFF);
675 tcflush(STDIN_FILENO, TCIFLUSH); // discard the rest of input
678 term_attr.c_lflag |= ICANON;
679 tcsetattr(STDIN_FILENO, TCSANOW, &term_attr);
680 fcntl(STDIN_FILENO, F_SETFL, origFlags);
683 // Callback for control socket
684 static void control_socket_cb(eventloop_t *loop, int sockfd)
686 // Considered keeping a limit the number of active connections, however, there doesn't
687 // seem much to be gained from that. Only root can create connections and not being
688 // able to establish a control connection is as much a denial-of-service as is not being
689 // able to start a service due to lack of fd's.
691 // Accept a connection
692 int newfd = dinit_accept4(sockfd, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC);
696 new control_conn_t(*loop, services, newfd); // will delete itself when it's finished
698 catch (std::exception &exc) {
699 log(loglevel_t::ERROR, "Accepting control connection: ", exc.what());
705 // Callback when the root filesystem is read/write:
706 void rootfs_is_rw() noexcept
708 open_control_socket(true);
709 if (! did_log_boot) {
710 did_log_boot = log_boot();
714 // Open/create the control socket, normally /dev/dinitctl, used to allow client programs to connect
715 // and issue service orders and shutdown commands etc. This can safely be called multiple times;
716 // once the socket has been successfully opened, further calls have no effect.
717 static void open_control_socket(bool report_ro_failure) noexcept
719 if (control_socket_open) {
720 struct stat stat_buf;
721 if (stat(control_socket_path, &stat_buf) != 0 && errno == ENOENT) {
722 // Looks like our control socket has disappeared from the filesystem. Close our control
723 // socket and re-create it:
724 control_socket_io.deregister(event_loop);
725 close(control_socket_io.get_watched_fd());
726 control_socket_open = false; // now re-open below
730 if (! control_socket_open) {
731 const char * saddrname = control_socket_path;
732 size_t saddrname_len = strlen(saddrname);
733 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
735 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
736 if (name == nullptr) {
737 log(loglevel_t::ERROR, "Opening control socket: out of memory");
741 if (am_system_init) {
742 // Unlink any stale control socket file, but only if we are system init, since otherwise
743 // the 'stale' file may not be stale at all:
747 name->sun_family = AF_UNIX;
748 memcpy(name->sun_path, saddrname, saddrname_len + 1);
750 int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
752 log(loglevel_t::ERROR, "Error creating control socket: ", strerror(errno));
757 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
758 if (errno != EROFS || report_ro_failure) {
759 log(loglevel_t::ERROR, "Error binding control socket: ", strerror(errno));
768 // No connections can be made until we listen, so it is fine to change the permissions now
769 // (and anyway there is no way to atomically create the socket and set permissions):
770 if (chmod(saddrname, S_IRUSR | S_IWUSR) == -1) {
771 log(loglevel_t::ERROR, "Error setting control socket permissions: ", strerror(errno));
776 if (listen(sockfd, 10) == -1) {
777 log(loglevel_t::ERROR, "Error listening on control socket: ", strerror(errno));
783 control_socket_io.add_watch(event_loop, sockfd, dasynq::IN_EVENTS);
784 control_socket_open = true;
786 catch (std::exception &e)
788 log(loglevel_t::ERROR, "Could not setup I/O on control socket: ", e.what());
794 static void close_control_socket() noexcept
796 if (control_socket_open) {
797 int fd = control_socket_io.get_watched_fd();
798 control_socket_io.deregister(event_loop);
801 // Unlink the socket:
802 unlink(control_socket_path);
804 control_socket_open = false;
808 void setup_external_log() noexcept
810 if (! external_log_open) {
812 const char * saddrname = log_path;
813 size_t saddrname_len = strlen(saddrname);
814 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
816 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
817 if (name == nullptr) {
818 log(loglevel_t::ERROR, "Connecting to log socket: out of memory");
822 name->sun_family = AF_UNIX;
823 memcpy(name->sun_path, saddrname, saddrname_len + 1);
825 int sockfd = dinit_socket(AF_UNIX, SOCK_DGRAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
827 log(loglevel_t::ERROR, "Error creating log socket: ", strerror(errno));
832 if (connect(sockfd, (struct sockaddr *) name, sockaddr_size) == 0 || errno == EINPROGRESS) {
833 // For EINPROGRESS, connection is still being established; however, we can select on
834 // the file descriptor so we will be notified when it's ready. In other words we can
835 // basically use it anyway.
837 setup_main_log(sockfd);
838 external_log_open = true;
840 catch (std::exception &e) {
841 log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
846 // Note if connect fails, we haven't warned at all, because the syslog server might not
855 int log_fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK | O_CLOEXEC, 0644);
858 setup_main_log(log_fd);
859 external_log_open = true;
861 catch (std::exception &e) {
862 log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
867 // log failure to log? It makes more sense than first appears, because we also log
869 log(loglevel_t::ERROR, "Setting up log failed: ", strerror(errno));
875 /* handle SIGINT signal (generated by Linux kernel when ctrl+alt+del pressed) */
876 static void sigint_reboot_cb(eventloop_t &eloop) noexcept
878 services->stop_all_services(shutdown_type_t::REBOOT);
881 /* handle SIGQUIT (if we are system init) */
882 static void sigquit_cb(eventloop_t &eloop) noexcept
884 // This performs an immediate shutdown, without service rollback.
885 close_control_socket();
886 constexpr auto shutdown_exec = literal(SBINDIR) + "/shutdown";
887 execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", (char *) 0);
888 log(loglevel_t::ERROR, literal("Error executing ") + SBINDIR + "/sbin/shutdown: ", strerror(errno));
889 sync(); // since a hard poweroff might be required at this point...
892 /* handle SIGTERM/SIGQUIT(non-system-daemon) - stop all services and shut down */
893 static void sigterm_cb(eventloop_t &eloop) noexcept
895 services->stop_all_services();