chiark / gitweb /
journal: generate structured journal messages for a number of events
[elogind.git] / src / core / manager.c
index c0cc1b3f37d1889f30162d02ff6d1f4a1dfd8873..da766e6d58e0e1de72030a23c14af20ef451f893 100644 (file)
@@ -41,7 +41,9 @@
 #include <libaudit.h>
 #endif
 
-#include <systemd/sd-daemon.h>
+#include "systemd/sd-daemon.h"
+#include "systemd/sd-id128.h"
+#include "systemd/sd-messages.h"
 
 #include "manager.h"
 #include "transaction.h"
@@ -74,8 +76,7 @@
 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
 
 /* Where clients shall send notification messages to */
-#define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify"
-#define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
+#define NOTIFY_SOCKET "@/org/freedesktop/systemd1/notify"
 
 static int manager_setup_notify(Manager *m) {
         union {
@@ -83,13 +84,13 @@ static int manager_setup_notify(Manager *m) {
                 struct sockaddr_un un;
         } sa;
         struct epoll_event ev;
-        int one = 1, r;
-        mode_t u;
+        int one = 1;
 
         assert(m);
 
         m->notify_watch.type = WATCH_NOTIFY;
-        if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
+        m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
+        if (m->notify_watch.fd < 0) {
                 log_error("Failed to allocate notification socket: %m");
                 return -errno;
         }
@@ -97,21 +98,14 @@ static int manager_setup_notify(Manager *m) {
         zero(sa);
         sa.sa.sa_family = AF_UNIX;
 
-        if (getpid() != 1)
-                snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
-        else {
-                unlink(NOTIFY_SOCKET_SYSTEM);
-                strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
-        }
-
-        if (sa.un.sun_path[0] == '@')
-                sa.un.sun_path[0] = 0;
+        if (getpid() != 1 || detect_container(NULL) > 0)
+                snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET "/%llu", random_ull());
+        else
+                strncpy(sa.un.sun_path, NOTIFY_SOCKET, sizeof(sa.un.sun_path));
 
-        u = umask(0111);
-        r = bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1));
-        umask(u);
+        sa.un.sun_path[0] = 0;
 
-        if (r < 0) {
+        if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
                 log_error("bind() failed: %m");
                 return -errno;
         }
@@ -128,10 +122,9 @@ static int manager_setup_notify(Manager *m) {
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
                 return -errno;
 
-        if (sa.un.sun_path[0] == 0)
-                sa.un.sun_path[0] = '@';
-
-        if (!(m->notify_socket = strdup(sa.un.sun_path)))
+        sa.un.sun_path[0] = '@';
+        m->notify_socket = strdup(sa.un.sun_path);
+        if (!m->notify_socket)
                 return -ENOMEM;
 
         log_debug("Using notification socket %s", m->notify_socket);
@@ -599,7 +592,7 @@ static void manager_build_unit_path_cache(Manager *m) {
                         if (ignore_file(de->d_name))
                                 continue;
 
-                        p = join(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
+                        p = strjoin(streq(*i, "/") ? "" : *i, "/", de->d_name, NULL);
                         if (!p) {
                                 r = -ENOMEM;
                                 goto fail;
@@ -805,7 +798,7 @@ int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DB
 
         t = unit_name_to_type(name);
 
-        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid_no_type(name, false)) {
+        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, false)) {
                 dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
                 return -EINVAL;
         }
@@ -1634,7 +1627,7 @@ void manager_send_unit_plymouth(Manager *m, Unit *u) {
         }
 
         if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->id) + 1), u->id, &n) < 0) {
-                log_error("Out of memory");
+                log_oom();
                 goto finish;
         }
 
@@ -1731,7 +1724,7 @@ int manager_open_serialization(Manager *m, FILE **_f) {
         return 0;
 }
 
-int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
+int manager_serialize(Manager *m, FILE *f, FDSet *fds, bool serialize_jobs) {
         Iterator i;
         Unit *u;
         const char *t;
@@ -1750,7 +1743,7 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
 
         dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp);
 
-        if (! in_initrd()) {
+        if (!in_initrd()) {
                 dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp);
                 dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp);
         }
@@ -1768,7 +1761,7 @@ int manager_serialize(Manager *m, FILE *f, FDSet *fds) {
                 fputs(u->id, f);
                 fputc('\n', f);
 
-                if ((r = unit_serialize(u, f, fds)) < 0) {
+                if ((r = unit_serialize(u, f, fds, serialize_jobs)) < 0) {
                         m->n_reloading --;
                         return r;
                 }
@@ -1908,7 +1901,7 @@ int manager_reload(Manager *m) {
                 goto finish;
         }
 
-        r = manager_serialize(m, f, fds);
+        r = manager_serialize(m, f, fds, true);
         if (r < 0) {
                 m->n_reloading --;
                 goto finish;
@@ -2039,26 +2032,41 @@ void manager_check_finished(Manager *m) {
                         kernel_usec = m->initrd_timestamp.monotonic;
                         initrd_usec = m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic;
 
-                        log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
-                                 format_timespan(kernel, sizeof(kernel), kernel_usec),
-                                 format_timespan(initrd, sizeof(initrd), initrd_usec),
-                                 format_timespan(userspace, sizeof(userspace), userspace_usec),
-                                 format_timespan(sum, sizeof(sum), total_usec));
+                        log_struct(LOG_INFO,
+                                   "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(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,
+                                   "MESSAGE=Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.",
+                                   format_timespan(kernel, sizeof(kernel), kernel_usec),
+                                   format_timespan(initrd, sizeof(initrd), initrd_usec),
+                                   format_timespan(userspace, sizeof(userspace), userspace_usec),
+                                   format_timespan(sum, sizeof(sum), total_usec),
+                                   NULL);
                 } else {
                         kernel_usec = m->startup_timestamp.monotonic;
                         initrd_usec = 0;
 
-                        log_info("Startup finished in %s (kernel) + %s (userspace) = %s.",
-                                 format_timespan(kernel, sizeof(kernel), kernel_usec),
-                                 format_timespan(userspace, sizeof(userspace), userspace_usec),
-                                 format_timespan(sum, sizeof(sum), total_usec));
+                        log_struct(LOG_INFO,
+                                   "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_STARTUP_FINISHED),
+                                   "KERNEL_USEC=%llu", (unsigned long long) kernel_usec,
+                                   "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
+                                   "MESSAGE=Startup finished in %s (kernel) + %s (userspace) = %s.",
+                                   format_timespan(kernel, sizeof(kernel), kernel_usec),
+                                   format_timespan(userspace, sizeof(userspace), userspace_usec),
+                                   format_timespan(sum, sizeof(sum), total_usec),
+                                   NULL);
                 }
         } else {
-                userspace_usec = initrd_usec = kernel_usec = 0;
-                total_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
-
-                log_debug("Startup finished in %s.",
-                          format_timespan(sum, sizeof(sum), total_usec));
+                initrd_usec = kernel_usec = 0;
+                total_usec = userspace_usec = m->finish_timestamp.monotonic - m->startup_timestamp.monotonic;
+
+                log_struct(LOG_INFO,
+                           "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_STARTUP_FINISHED),
+                           "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
+                           "MESSAGE=Startup finished in %s.",
+                           format_timespan(sum, sizeof(sum), total_usec),
+                           NULL);
         }
 
         bus_broadcast_finished(m, kernel_usec, initrd_usec, userspace_usec, total_usec);
@@ -2082,10 +2090,8 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
         if (m->running_as == MANAGER_SYSTEM && getpid() == 1) {
 
                 p = strappend("/run/systemd/", name);
-                if (!p) {
-                        log_error("Out of memory");
-                        return -ENOMEM;
-                }
+                if (!p)
+                        return log_oom();
 
                 r = mkdir_p_label(p, 0755);
                 if (r < 0) {
@@ -2094,11 +2100,9 @@ static int create_generator_dir(Manager *m, char **generator, const char *name)
                         return r;
                 }
         } else {
-                p = join("/tmp/systemd-", name, ".XXXXXX", NULL);
-                if (!p) {
-                        log_error("Out of memory");
-                        return -ENOMEM;
-                }
+                p = strjoin("/tmp/systemd-", name, ".XXXXXX", NULL);
+                if (!p)
+                        return log_oom();
 
                 if (!mkdtemp(p)) {
                         free(p);