Adjust dependency auto-break logic.
[oweals/dinit.git] / src / dinit.cc
index 7297f9895fcace4408323d313015be3c23797a82..b7fe727a007ecb7f2bae19fe303d160d9fddc2c9 100644 (file)
@@ -28,6 +28,9 @@
 #include "control.h"
 #include "dinit-log.h"
 #include "dinit-socket.h"
+#include "static-string.h"
+
+#include "mconfig.h"
 
 /*
  * When running as the system init process, Dinit processes the following signals:
@@ -42,6 +45,8 @@
  * services even if the halt/reboot commands are unavailable for some reason.
  */
 
+using namespace cts;
+
 using eventloop_t = dasynq::event_loop<dasynq::null_mutex>;
 
 eventloop_t event_loop;
@@ -68,7 +73,7 @@ int active_control_conns = 0;
 
 // Control socket path. We maintain a string (control_socket_str) in case we need
 // to allocate storage, but control_socket_path is the authoritative value.
-static const char *control_socket_path = "/dev/dinitctl";
+static const char *control_socket_path = SYSCONTROLSOCKET;
 static std::string control_socket_str;
 
 static const char *env_file_path = "/etc/dinit/environment";
@@ -350,10 +355,6 @@ int dinit_main(int argc, char **argv)
     // Try to open control socket (may fail due to readonly filesystem)
     open_control_socket(false);
     
-    // Only try to set up the external log now if we aren't the system init. (If we are the
-    // system init, wait until the log service starts).
-    if (! am_system_init) setup_external_log();
-
 #ifdef __linux__
     if (am_system_init) {
         // Disable non-critical kernel output to console
@@ -387,11 +388,15 @@ int dinit_main(int argc, char **argv)
         services->add_service_dir("/lib/dinit.d", false);
     }
     
-    init_log(services);
+    init_log(services, log_is_syslog);
     if (am_system_init) {
         log(loglevel_t::INFO, false, "starting system");
     }
     
+    // Only try to set up the external log now if we aren't the system init. (If we are the
+    // system init, wait until the log service starts).
+    if (! am_system_init) setup_external_log();
+
     if (env_file != nullptr) {
         read_env_file(env_file);
     }
@@ -479,8 +484,10 @@ int dinit_main(int argc, char **argv)
         }
         
         // Fork and execute dinit-reboot.
-        execl("/sbin/shutdown", "/sbin/shutdown", "--system", cmd_arg, nullptr);
-        log(loglevel_t::ERROR, "Could not execute /sbin/shutdown: ", strerror(errno));
+        constexpr auto shutdown_exec = literal(SBINDIR) + "/shutdown";
+        execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", cmd_arg, nullptr);
+        log(loglevel_t::ERROR, (literal("Could not execute ") + SBINDIR + "/shutdown: ").c_str(),
+                strerror(errno));
         
         // PID 1 must not actually exit, although we should never reach this point:
         while (true) {
@@ -696,7 +703,8 @@ void setup_external_log() noexcept
                 // the file descriptor so we will be notified when it's ready. In other words we can
                 // basically use it anyway.
                 try {
-                    setup_main_log(sockfd, true);
+                    setup_main_log(sockfd);
+                    external_log_open = true;
                 }
                 catch (std::exception &e) {
                     log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
@@ -716,7 +724,8 @@ void setup_external_log() noexcept
             int log_fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK | O_CLOEXEC, 0644);
             if (log_fd >= 0) {
                 try {
-                    setup_main_log(log_fd, false);
+                    setup_main_log(log_fd);
+                    external_log_open = true;
                 }
                 catch (std::exception &e) {
                     log(loglevel_t::ERROR, "Setting up log failed: ", e.what());
@@ -742,8 +751,9 @@ static void sigquit_cb(eventloop_t &eloop) noexcept
 {
     // This performs an immediate shutdown, without service rollback.
     close_control_socket();
-    execl("/sbin/shutdown", "/sbin/shutdown", "--system", (char *) 0);
-    log(loglevel_t::ERROR, "Error executing /sbin/shutdown: ", strerror(errno));
+    constexpr auto shutdown_exec = literal(SBINDIR) + "/shutdown";
+    execl(shutdown_exec.c_str(), shutdown_exec.c_str(), "--system", (char *) 0);
+    log(loglevel_t::ERROR, literal("Error executing ") + SBINDIR + "/sbin/shutdown: ", strerror(errno));
     sync(); // since a hard poweroff might be required at this point...
 }