11 #include <sys/socket.h>
16 #include <sys/prctl.h>
18 #include <sys/reboot.h>
25 #include "dinit-log.h"
26 #include "dinit-socket.h"
29 * When running as the system init process, Dinit processes the following signals:
31 * SIGTERM - roll back services and then fork/exec /sbin/halt
32 * SIGINT - roll back services and then fork/exec /sbin/reboot
33 * SIGQUIT - exec() /sbin/shutdown without rolling back services
35 * It's an open question about whether Dinit should roll back services *before*
36 * running halt/reboot, since those commands should prompt rollback of services
37 * anyway. But it seems safe to do so, and it means the user can at least stop
38 * services even if the halt/reboot commands are unavailable for some reason.
41 using eventloop_t = dasynq::event_loop<dasynq::null_mutex>;
43 eventloop_t event_loop;
45 static void sigint_reboot_cb(eventloop_t &eloop) noexcept;
46 static void sigquit_cb(eventloop_t &eloop) noexcept;
47 static void sigterm_cb(eventloop_t &eloop) noexcept;
48 static void close_control_socket() noexcept;
49 static void wait_for_user_input() noexcept;
51 static void control_socket_cb(eventloop_t *loop, int fd);
56 static dirload_service_set *services;
58 static bool am_system_init = false; // true if we are the system init process
60 static bool control_socket_open = false;
61 static bool external_log_open = false;
62 int active_control_conns = 0;
64 // Control socket path. We maintain a string (control_socket_str) in case we need
65 // to allocate storage, but control_socket_path is the authoritative value.
66 static const char *control_socket_path = "/dev/dinitctl";
67 static std::string control_socket_str;
69 static const char *log_socket_path = "/dev/log";
71 static const char *user_home_path = nullptr;
74 // Get user home (and set user_home_path). (The return may become invalid after
75 // changing the evironment (HOME variable) or using the getpwuid() function).
76 const char * get_user_home()
78 if (user_home_path == nullptr) {
79 user_home_path = getenv("HOME");
80 if (user_home_path == nullptr) {
81 struct passwd * pwuid_p = getpwuid(getuid());
82 if (pwuid_p != nullptr) {
83 user_home_path = pwuid_p->pw_dir;
87 return user_home_path;
92 class callback_signal_handler : public eventloop_t::signal_watcher_impl<callback_signal_handler>
94 using rearm = dasynq::rearm;
97 typedef void (*cb_func_t)(eventloop_t &);
103 callback_signal_handler() : cb_func(nullptr) { }
104 callback_signal_handler(cb_func_t pcb_func) : cb_func(pcb_func) { }
106 void setCbFunc(cb_func_t cb_func)
108 this->cb_func = cb_func;
111 rearm received(eventloop_t &eloop, int signo, siginfo_p siginfo)
118 class control_socket_watcher : public eventloop_t::fd_watcher_impl<control_socket_watcher>
120 using rearm = dasynq::rearm;
123 rearm fd_event(eventloop_t &loop, int fd, int flags) noexcept
125 control_socket_cb(&loop, fd);
130 // Simple timer used to limit the amount of time waiting for the log flush to complete (at shutdown)
131 class log_flush_timer_t : public eventloop_t::timer_impl<log_flush_timer_t>
133 using rearm = dasynq::rearm;
135 bool expired = false;
138 rearm timer_expiry(eventloop_t &, int expiry_count)
141 return rearm::DISARM;
150 control_socket_watcher control_socket_io;
151 log_flush_timer_t log_flush_timer;
154 int dinit_main(int argc, char **argv)
158 am_system_init = (getpid() == 1);
159 const char * service_dir = nullptr;
160 string service_dir_str; // to hold storage for above if necessary
161 bool control_socket_path_set = false;
163 // list of services to start
164 list<const char *> services_to_start;
166 // Arguments, if given, specify a list of services to start.
167 // If we are running as init (PID=1), the kernel gives us any command line
168 // arguments it was given but didn't recognize, including "single" (usually
169 // for "boot to single user mode" aka just start the shell). We can treat
170 // them as service names. In the worst case we can't find any of the named
171 // services, and so we'll start the "boot" service by default.
173 for (int i = 1; i < argc; i++) {
174 if (argv[i][0] == '-') {
176 if (strcmp(argv[i], "--services-dir") == 0 ||
177 strcmp(argv[i], "-d") == 0) {
179 service_dir = argv[i];
182 cerr << "dinit: '--services-dir' (-d) requires an argument" << endl;
186 else if (strcmp(argv[i], "--system") == 0 ||
187 strcmp(argv[i], "-s") == 0) {
188 am_system_init = true;
190 else if (strcmp(argv[i], "--socket-path") == 0 ||
191 strcmp(argv[i], "-p") == 0) {
193 control_socket_path = argv[i];
194 control_socket_path_set = true;
197 cerr << "dinit: '--socket-path' (-p) requires an argument" << endl;
201 else if (strcmp(argv[i], "--help") == 0) {
202 cout << "dinit, an init with dependency management" << endl;
203 cout << " --help display help" << endl;
204 cout << " --services-dir <dir>, -d <dir>" << endl;
205 cout << " set base directory for service description" << endl;
206 cout << " files (-d <dir>)" << endl;
207 cout << " --system, -s run as the system init process" << endl;
208 cout << " --socket-path <path>, -p <path>" << endl;
209 cout << " path to control socket" << endl;
210 cout << " <service-name> start service with name <service-name>" << endl;
215 if (! am_system_init) {
216 cerr << "dinit: Unrecognized option: " << argv[i] << endl;
223 // LILO puts "auto" on the kernel command line for unattended boots; we'll filter it.
224 if (! am_system_init || strcmp(argv[i], "auto") != 0) {
225 services_to_start.push_back(argv[i]);
232 if (am_system_init) {
233 // setup STDIN, STDOUT, STDERR so that we can use them
234 int onefd = open("/dev/console", O_RDONLY, 0);
236 int twofd = open("/dev/console", O_RDWR, 0);
240 if (onefd > 2) close(onefd);
241 if (twofd > 2) close(twofd);
244 /* Set up signal handlers etc */
245 /* SIG_CHILD is ignored by default: good */
246 sigset_t sigwait_set;
247 sigemptyset(&sigwait_set);
248 sigaddset(&sigwait_set, SIGCHLD);
249 sigaddset(&sigwait_set, SIGINT);
250 sigaddset(&sigwait_set, SIGTERM);
251 if (am_system_init) sigaddset(&sigwait_set, SIGQUIT);
252 sigprocmask(SIG_BLOCK, &sigwait_set, NULL);
254 // Terminal access control signals - we block these so that dinit can't be
255 // suspended if it writes to the terminal after some other process has claimed
257 signal(SIGTSTP, SIG_IGN);
258 signal(SIGTTIN, SIG_IGN);
259 signal(SIGTTOU, SIG_IGN);
261 signal(SIGPIPE, SIG_IGN);
263 if (! am_system_init && ! control_socket_path_set) {
264 const char * userhome = get_user_home();
265 if (userhome != nullptr) {
266 control_socket_str = userhome;
267 control_socket_str += "/.dinitctl";
268 control_socket_path = control_socket_str.c_str();
272 /* service directory name */
273 if (service_dir == nullptr && ! am_system_init) {
274 const char * userhome = get_user_home();
275 if (userhome != nullptr) {
276 service_dir_str = get_user_home();
277 service_dir_str += "/dinit.d";
278 service_dir = service_dir_str.c_str();
282 if (service_dir == nullptr) {
283 service_dir = "/etc/dinit.d";
286 if (services_to_start.empty()) {
287 services_to_start.push_back("boot");
290 // Set up signal handlers
291 callback_signal_handler sigterm_watcher {sigterm_cb};
292 callback_signal_handler sigint_watcher;
293 callback_signal_handler sigquit_watcher;
295 if (am_system_init) {
296 sigint_watcher.setCbFunc(sigint_reboot_cb);
297 sigquit_watcher.setCbFunc(sigquit_cb);
300 sigint_watcher.setCbFunc(sigterm_cb);
303 sigint_watcher.add_watch(event_loop, SIGINT);
304 sigterm_watcher.add_watch(event_loop, SIGTERM);
306 if (am_system_init) {
307 // PID 1: SIGQUIT exec's shutdown
308 sigquit_watcher.add_watch(event_loop, SIGQUIT);
309 // As a user process, we instead just let SIGQUIT perform the default action.
312 // Try to open control socket (may fail due to readonly filesystem)
313 open_control_socket(false);
316 if (am_system_init) {
317 // Disable non-critical kernel output to console
318 klogctl(6 /* SYSLOG_ACTION_CONSOLE_OFF */, nullptr, 0);
319 // Make ctrl+alt+del combination send SIGINT to PID 1 (this process)
320 reboot(RB_DISABLE_CAD);
323 // Mark ourselves as a subreaper. This means that if a process we start double-forks, the
324 // orphaned child will re-parent to us rather than to PID 1 (although that could be us too).
325 prctl(PR_SET_CHILD_SUBREAPER, 1);
328 log_flush_timer.add_timer(event_loop, dasynq::clock_type::MONOTONIC);
330 /* start requested services */
331 services = new dirload_service_set(service_dir);
335 for (auto svc : services_to_start) {
337 services->start_service(svc);
338 // Note in general if we fail to start a service we don't need any special error handling,
339 // since we either leave other services running or, if it was the only service, then no
340 // services will be running and we will process normally (reboot if system process,
341 // exit if user process).
343 catch (service_not_found &snf) {
344 log(loglevel_t::ERROR, snf.serviceName, ": Could not find service description.");
346 catch (service_load_exc &sle) {
347 log(loglevel_t::ERROR, sle.serviceName, ": ", sle.excDescription);
349 catch (std::bad_alloc &badalloce) {
350 log(loglevel_t::ERROR, "Out of memory when trying to start service: ", svc, ".");
357 // Process events until all services have terminated.
358 while (services->count_active_services() != 0) {
362 shutdown_type_t shutdown_type = services->get_shutdown_type();
364 if (am_system_init) {
365 log_msg_begin(loglevel_t::INFO, "No more active services.");
367 if (shutdown_type == shutdown_type_t::REBOOT) {
368 log_msg_end(" Will reboot.");
370 else if (shutdown_type == shutdown_type_t::HALT) {
371 log_msg_end(" Will halt.");
373 else if (shutdown_type == shutdown_type_t::POWEROFF) {
374 log_msg_end(" Will power down.");
377 log_msg_end(" Re-initiating boot sequence.");
381 log_flush_timer.arm_timer_rel(event_loop, timespec{5,0}); // 5 seconds
382 while (! is_log_flushed() && ! log_flush_timer.has_expired()) {
386 close_control_socket();
388 if (am_system_init) {
389 if (shutdown_type == shutdown_type_t::CONTINUE) {
390 // It could be that we started in single user mode, and the
391 // user has now exited the shell. We'll try and re-start the
394 services->start_service("boot");
395 goto event_loop; // yes, the "evil" goto
398 // Now what do we do? try to reboot, but wait for user ack to avoid boot loop.
399 log(loglevel_t::ERROR, "Could not start 'boot' service. Will attempt reboot.");
400 wait_for_user_input();
401 shutdown_type = shutdown_type_t::REBOOT;
405 const char * cmd_arg;
406 if (shutdown_type == shutdown_type_t::HALT) {
409 else if (shutdown_type == shutdown_type_t::REBOOT) {
417 // Fork and execute dinit-reboot.
418 execl("/sbin/shutdown", "/sbin/shutdown", "--system", cmd_arg, nullptr);
419 log(loglevel_t::ERROR, "Could not execute /sbin/shutdown: ", strerror(errno));
421 // PID 1 must not actually exit, although we should never reach this point:
426 else if (shutdown_type == shutdown_type_t::REBOOT) {
427 // Non-system-process. If we got SIGINT, let's die due to it:
428 sigset_t sigwait_set;
429 sigemptyset(&sigwait_set);
430 sigaddset(&sigwait_set, SIGINT);
432 sigprocmask(SIG_UNBLOCK, &sigwait_set, NULL);
438 // In exception situations we want user confirmation before proceeding (eg on critical boot failure
439 // we wait before rebooting to avoid a reboot loop).
440 static void wait_for_user_input() noexcept
442 std::cout << "Press Enter to continue." << std::endl;
444 read(STDIN_FILENO, buf, 1);
447 // Callback for control socket
448 static void control_socket_cb(eventloop_t *loop, int sockfd)
450 // Considered keeping a limit the number of active connections, however, there doesn't
451 // seem much to be gained from that. Only root can create connections and not being
452 // able to establish a control connection is as much a denial-of-service as is not being
453 // able to start a service due to lack of fd's.
455 // Accept a connection
456 int newfd = dinit_accept4(sockfd, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC);
460 new control_conn_t(*loop, services, newfd); // will delete itself when it's finished
462 catch (std::exception &exc) {
463 log(loglevel_t::ERROR, "Accepting control connection: ", exc.what());
469 void open_control_socket(bool report_ro_failure) noexcept
471 if (! control_socket_open) {
472 const char * saddrname = control_socket_path;
473 size_t saddrname_len = strlen(saddrname);
474 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
476 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
477 if (name == nullptr) {
478 log(loglevel_t::ERROR, "Opening control socket: out of memory");
482 if (am_system_init) {
483 // Unlink any stale control socket file, but only if we are system init, since otherwise
484 // the 'stale' file may not be stale at all:
488 name->sun_family = AF_UNIX;
489 memcpy(name->sun_path, saddrname, saddrname_len + 1);
491 int sockfd = dinit_socket(AF_UNIX, SOCK_STREAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
493 log(loglevel_t::ERROR, "Error creating control socket: ", strerror(errno));
498 if (bind(sockfd, (struct sockaddr *) name, sockaddr_size) == -1) {
499 if (errno != EROFS || report_ro_failure) {
500 log(loglevel_t::ERROR, "Error binding control socket: ", strerror(errno));
509 // No connections can be made until we listen, so it is fine to change the permissions now
510 // (and anyway there is no way to atomically create the socket and set permissions):
511 if (chmod(saddrname, S_IRUSR | S_IWUSR) == -1) {
512 log(loglevel_t::ERROR, "Error setting control socket permissions: ", strerror(errno));
517 if (listen(sockfd, 10) == -1) {
518 log(loglevel_t::ERROR, "Error listening on control socket: ", strerror(errno));
524 control_socket_io.add_watch(event_loop, sockfd, dasynq::IN_EVENTS);
525 control_socket_open = true;
527 catch (std::exception &e)
529 log(loglevel_t::ERROR, "Could not setup I/O on control socket: ", e.what());
535 static void close_control_socket() noexcept
537 if (control_socket_open) {
538 int fd = control_socket_io.get_watched_fd();
539 control_socket_io.deregister(event_loop);
542 // Unlink the socket:
543 unlink(control_socket_path);
547 void setup_external_log() noexcept
549 if (! external_log_open) {
551 const char * saddrname = log_socket_path;
552 size_t saddrname_len = strlen(saddrname);
553 uint sockaddr_size = offsetof(struct sockaddr_un, sun_path) + saddrname_len + 1;
555 struct sockaddr_un * name = static_cast<sockaddr_un *>(malloc(sockaddr_size));
556 if (name == nullptr) {
557 log(loglevel_t::ERROR, "Connecting to log socket: out of memory");
561 name->sun_family = AF_UNIX;
562 memcpy(name->sun_path, saddrname, saddrname_len + 1);
564 int sockfd = dinit_socket(AF_UNIX, SOCK_DGRAM, 0, SOCK_NONBLOCK | SOCK_CLOEXEC);
566 log(loglevel_t::ERROR, "Error creating log socket: ", strerror(errno));
571 if (connect(sockfd, (struct sockaddr *) name, sockaddr_size) == 0 || errno == EINPROGRESS) {
572 // For EINPROGRESS, connection is still being established; however, we can select on
573 // the file descriptor so we will be notified when it's ready. In other words we can
574 // basically use it anyway.
576 setup_main_log(sockfd);
578 catch (std::exception &e) {
579 log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
584 // Note if connect fails, we haven't warned at all, because the syslog server might not
593 /* handle SIGINT signal (generated by Linux kernel when ctrl+alt+del pressed) */
594 static void sigint_reboot_cb(eventloop_t &eloop) noexcept
596 services->stop_all_services(shutdown_type_t::REBOOT);
599 /* handle SIGQUIT (if we are system init) */
600 static void sigquit_cb(eventloop_t &eloop) noexcept
602 // This performs an immediate shutdown, without service rollback.
603 close_control_socket();
604 execl("/sbin/shutdown", "/sbin/shutdown", "--system", (char *) 0);
605 log(loglevel_t::ERROR, "Error executing /sbin/shutdown: ", strerror(errno));
606 sync(); // since a hard poweroff might be required at this point...
609 /* handle SIGTERM/SIGQUIT(non-system-daemon) - stop all services and shut down */
610 static void sigterm_cb(eventloop_t &eloop) noexcept
612 services->stop_all_services();