chiark / gitweb /
core: --user -- move generator directories from /tmp to $XDG_RUNTIME_DIR
[elogind.git] / src / core / manager.c
index b9aa6dcfd56fa957485485603707b10c2d8fc776..0e35ba5cab9134d3364bbdf30e00a2bff88a3b21 100644 (file)
@@ -482,7 +482,7 @@ static int manager_setup_notify(Manager *m) {
                 }
 
                 if (getpid() != 1 || detect_container(NULL) > 0)
-                        snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%llu", random_ull());
+                        snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%" PRIx64, random_u64());
                 else
                         strncpy(sa.un.sun_path, NOTIFY_SOCKET, sizeof(sa.un.sun_path));
                 sa.un.sun_path[0] = 0;
@@ -538,11 +538,7 @@ static int manager_setup_kdbus(Manager *m) {
         if (m->kdbus_fd >= 0)
                 return 0;
 
-        /* If there's already a bus address set, don't set up kdbus */
-        if (m->running_as == SYSTEMD_USER && getenv("DBUS_SESSION_BUS_ADDRESS"))
-                return 0;
-
-        m->kdbus_fd = bus_kernel_create_bus(m->running_as == SYSTEMD_SYSTEM ? "system" : "user", &p);
+        m->kdbus_fd = bus_kernel_create_bus(m->running_as == SYSTEMD_SYSTEM ? "system" : "user", m->running_as == SYSTEMD_SYSTEM, &p);
         if (m->kdbus_fd < 0) {
                 log_debug("Failed to set up kdbus: %s", strerror(-m->kdbus_fd));
                 return m->kdbus_fd;
@@ -554,7 +550,8 @@ static int manager_setup_kdbus(Manager *m) {
          * of that directory is not visible to non-root users. This is
          * necessary to ensure that users cannot get access to busses
          * of virtualized users when no UID namespacing is used. */
-        mkdir_p_label("/dev/kdbus/ns", 0700);
+        if (m->running_as == SYSTEMD_SYSTEM)
+                mkdir_p_label("/dev/kdbus/ns", 0700);
 #endif
 
         return 0;
@@ -1115,7 +1112,7 @@ int manager_load_unit_prepare(
 
         t = unit_name_to_type(name);
 
-        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, false))
+        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
                 return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
 
         ret = manager_get_unit(m, name);
@@ -1334,7 +1331,7 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
                 if (!u) {
                         u = manager_get_unit_by_pid(m, ucred->pid);
                         if (!u) {
-                                log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid);
+                                log_warning("Cannot find unit for notify message of PID "PID_FMT".", ucred->pid);
                                 continue;
                         }
                 }
@@ -1382,7 +1379,7 @@ static int manager_dispatch_sigchld(Manager *m) {
                         _cleanup_free_ char *name = NULL;
 
                         get_process_comm(si.si_pid, &name);
-                        log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name));
+                        log_debug("Got SIGCHLD for process "PID_FMT" (%s)", si.si_pid, strna(name));
                 }
 
                 /* And now figure out the unit this belongs to */
@@ -1463,16 +1460,22 @@ static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t
                 }
 
                 if (sfsi.ssi_pid > 0) {
-                        char *p = NULL;
+                        _cleanup_free_ char *p = NULL;
 
                         get_process_comm(sfsi.ssi_pid, &p);
 
-                        log_debug("Received SIG%s from PID %lu (%s).",
-                                  signal_to_string(sfsi.ssi_signo),
-                                  (unsigned long) sfsi.ssi_pid, strna(p));
-                        free(p);
+                        log_full(sfsi.ssi_signo == SIGCHLD ||
+                                 (sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
+                                 ? LOG_DEBUG : LOG_INFO,
+                                 "Received SIG%s from PID "PID_FMT" (%s).",
+                                 signal_to_string(sfsi.ssi_signo),
+                                 sfsi.ssi_pid, strna(p));
                 } else
-                        log_debug("Received SIG%s.", signal_to_string(sfsi.ssi_signo));
+                        log_full(sfsi.ssi_signo == SIGCHLD ||
+                                 (sfsi.ssi_signo == SIGTERM && m->running_as == SYSTEMD_USER)
+                                 ? LOG_DEBUG : LOG_INFO,
+                                 "Received SIG%s.",
+                                 signal_to_string(sfsi.ssi_signo));
 
                 switch (sfsi.ssi_signo) {
 
@@ -1968,9 +1971,9 @@ int manager_open_serialization(Manager *m, FILE **_f) {
         assert(_f);
 
         if (m->running_as == SYSTEMD_SYSTEM)
-                asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid());
+                asprintf(&path, "/run/systemd/dump-"PID_FMT"-XXXXXX", getpid());
         else
-                asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid());
+                asprintf(&path, "/tmp/systemd-dump-"PID_FMT"-XXXXXX", getpid());
 
         if (!path)
                 return -ENOMEM;
@@ -2208,8 +2211,10 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
                         if (safe_atoi(l + 10, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
                                 log_debug("Failed to parse notify fd: %s", l + 10);
                         else {
-                                if (m->notify_fd >= 0)
+                                if (m->notify_fd >= 0) {
+                                        m->notify_event_source = sd_event_source_unref(m->notify_event_source);
                                         close_nointr_nofail(m->notify_fd);
+                                }
 
                                 m->notify_fd = fdset_remove(fds, fd);
                         }
@@ -2446,9 +2451,9 @@ void manager_check_finished(Manager *m) {
                         if (!log_on_console())
                                 log_struct(LOG_INFO,
                                            MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
-                                           "KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
-                                           "INITRD_USEC=%llu", (unsigned long long) initrd_usec,
-                                           "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
+                                           "KERNEL_USEC="USEC_FMT, kernel_usec,
+                                           "INITRD_USEC="USEC_FMT, initrd_usec,
+                                           "USERSPACE_USEC="USEC_FMT, userspace_usec,
                                            "MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
                                            format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
                                            format_timespan(initrd, sizeof(initrd), initrd_usec, USEC_PER_MSEC),
@@ -2462,8 +2467,8 @@ void manager_check_finished(Manager *m) {
                         if (!log_on_console())
                                 log_struct(LOG_INFO,
                                            MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
-                                           "KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
-                                           "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
+                                           "KERNEL_USEC="USEC_FMT, kernel_usec,
+                                           "USERSPACE_USEC="USEC_FMT, userspace_usec,
                                            "MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
                                            format_timespan(kernel, sizeof(kernel), kernel_usec, USEC_PER_MSEC),
                                            format_timespan(userspace, sizeof(userspace), userspace_usec, USEC_PER_MSEC),
@@ -2477,7 +2482,7 @@ void manager_check_finished(Manager *m) {
                 if (!log_on_console())
                         log_struct(LOG_INFO,
                                    MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
-                                   "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
+                                   "USERSPACE_USEC="USEC_FMT, userspace_usec,
                                    "MESSAGE=Startup finished in %s.",
                                    format_timespan(sum, sizeof(sum), total_usec, USEC_PER_MSEC),
                                    NULL);
@@ -2502,11 +2507,29 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
                 return 0;
 
         if (m->running_as == SYSTEMD_SYSTEM && getpid() == 1) {
+                /* systemd --system, not running --test */
 
                 p = strappend("/run/systemd/", name);
                 if (!p)
                         return log_oom();
 
+                r = mkdir_p_label(p, 0755);
+                if (r < 0) {
+                        log_error("Failed to create generator directory %s: %s",
+                                  p, strerror(-r));
+                        free(p);
+                        return r;
+                }
+        } else if (m->running_as == SYSTEMD_USER) {
+                const char *s = NULL;
+
+                s = getenv("XDG_RUNTIME_DIR");
+                if (!s)
+                        return -EINVAL;
+                p = strjoin(s, "/systemd/", name, NULL);
+                if (!p)
+                        return log_oom();
+
                 r = mkdir_p_label(p, 0755);
                 if (r < 0) {
                         log_error("Failed to create generator directory %s: %s",
@@ -2515,6 +2538,8 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
                         return r;
                 }
         } else {
+                /* systemd --system --test */
+
                 p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL);
                 if (!p)
                         return log_oom();