chiark / gitweb /
bus-proxyd: explicitly address messages to unique and well-known name
[elogind.git] / src / rc-local-generator / rc-local-generator.c
index 42d7ae4..01ff470 100644 (file)
 #include "util.h"
 #include "mkdir.h"
 
-#if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
-#define SCRIPT_PATH "/etc/rc.d/rc.local"
-#elif defined(TARGET_SUSE)
-#define SCRIPT_PATH "/etc/init.d/boot.local"
+#ifndef RC_LOCAL_SCRIPT_PATH_START
+#define RC_LOCAL_SCRIPT_PATH_START "/etc/rc.d/rc.local"
+#endif
+
+#ifndef RC_LOCAL_SCRIPT_PATH_STOP
+#define RC_LOCAL_SCRIPT_PATH_STOP "/sbin/halt.local"
 #endif
 
 const char *arg_dest = "/tmp";
 
-static int add_symlink(const char *service) {
-        char *from = NULL, *to = NULL;
+static int add_symlink(const char *service, const char *where) {
+        _cleanup_free_ char *from = NULL, *to = NULL;
         int r;
 
         assert(service);
+        assert(where);
 
-        asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", service);
-        asprintf(&to, "%s/multi-user.target.wants/%s", arg_dest, service);
+        from = strjoin(SYSTEM_DATA_UNIT_PATH, "/", service, NULL);
+        if (!from)
+                return log_oom();
 
-        if (!from || !to) {
-                log_error("Out of memory");
-                r = -ENOMEM;
-                goto finish;
-        }
+        to = strjoin(arg_dest, "/", where, ".wants/", service, NULL);
+        if (!to)
+                return log_oom();
 
-        mkdir_parents(to, 0755);
+        mkdir_parents_label(to, 0755);
 
         r = symlink(from, to);
         if (r < 0) {
                 if (errno == EEXIST)
-                        r = 0;
-                else {
-                        log_error("Failed to create symlink from %s to %s: %m", from, to);
-                        r = -errno;
-                }
-        }
-
-finish:
-
-        free(from);
-        free(to);
-
-        return r;
-}
-
-static bool file_is_executable(const char *f) {
-        struct stat st;
+                        return 0;
 
-        if (stat(f, &st) < 0)
-                return false;
+                log_error("Failed to create symlink %s: %m", to);
+                return -errno;
+        }
 
-        return S_ISREG(st.st_mode) && (st.st_mode & 0111);
+        return 1;
 }
 
 int main(int argc, char *argv[]) {
-
         int r = EXIT_SUCCESS;
 
-        if (argc > 2) {
-                log_error("This program takes one or no arguments.");
+        if (argc > 1 && argc != 4) {
+                log_error("This program takes three or no arguments.");
                 return EXIT_FAILURE;
         }
 
-        log_set_target(LOG_TARGET_AUTO);
+        if (argc > 1)
+                arg_dest = argv[1];
+
+        log_set_target(LOG_TARGET_SAFE);
         log_parse_environment();
         log_open();
 
-        if (argc > 1)
-                arg_dest = argv[1];
+        umask(0022);
 
-        if (file_is_executable(SCRIPT_PATH)) {
+        if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) >= 0) {
                 log_debug("Automatically adding rc-local.service.");
 
-                if (add_symlink("rc-local.service") < 0)
+                if (add_symlink("rc-local.service", "multi-user.target") < 0)
                         r = EXIT_FAILURE;
+        }
 
+        if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) >= 0) {
+                log_debug("Automatically adding halt-local.service.");
+
+                if (add_symlink("halt-local.service", "final.target") < 0)
+                        r = EXIT_FAILURE;
         }
 
         return r;