chiark / gitweb /
manager: introduce watch_init() initializer for watches
[elogind.git] / src / core / manager.c
index 3cd99154e698ae7e2be533c01f0cf53bb0f64414..72ce2f25956fef35c1b1a482142d3c5355f1bc72 100644 (file)
@@ -68,6 +68,7 @@
 #include "watchdog.h"
 #include "cgroup-util.h"
 #include "path-util.h"
+#include "audit-fd.h"
 
 /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */
 #define GC_QUEUE_ENTRIES_MAX 16
@@ -198,6 +199,7 @@ static int manager_setup_signals(Manager *m) {
                         SIGRTMIN+21, /* systemd: disable status messages */
                         SIGRTMIN+22, /* systemd: set log level to LOG_DEBUG */
                         SIGRTMIN+23, /* systemd: set log level to LOG_INFO */
+                        SIGRTMIN+24, /* systemd: Immediate exit (--user only) */
                         SIGRTMIN+26, /* systemd: set log target to journal-or-kmsg */
                         SIGRTMIN+27, /* systemd: set log target to console */
                         SIGRTMIN+28, /* systemd: set log target to kmsg */
@@ -257,10 +259,6 @@ int manager_new(SystemdRunningAs running_as, Manager **_m) {
         m->pin_cgroupfs_fd = -1;
         m->idle_pipe[0] = m->idle_pipe[1] = -1;
 
-#ifdef HAVE_AUDIT
-        m->audit_fd = -1;
-#endif
-
         m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1;
         m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */
 
@@ -307,14 +305,6 @@ int manager_new(SystemdRunningAs running_as, Manager **_m) {
         if ((r = bus_init(m, running_as != SYSTEMD_SYSTEM)) < 0)
                 goto fail;
 
-#ifdef HAVE_AUDIT
-        if ((m->audit_fd = audit_open()) < 0 &&
-            /* If the kernel lacks netlink or audit support,
-             * don't worry about it. */
-            errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
-                log_error("Failed to connect to audit log: %m");
-#endif
-
         m->taint_usr = dir_is_empty("/usr") > 0;
 
         *_m = m;
@@ -498,11 +488,6 @@ void manager_free(Manager *m) {
         if (m->notify_watch.fd >= 0)
                 close_nointr_nofail(m->notify_watch.fd);
 
-#ifdef HAVE_AUDIT
-        if (m->audit_fd >= 0)
-                audit_close(m->audit_fd);
-#endif
-
         free(m->notify_socket);
 
         lookup_paths_free(&m->lookup_paths);
@@ -1288,6 +1273,15 @@ static int manager_process_signal_fd(Manager *m) {
                                 log_notice("Setting log level to info.");
                                 break;
 
+                        case 24:
+                                if (m->running_as == SYSTEMD_USER) {
+                                        m->exit_code = MANAGER_EXIT;
+                                        return 0;
+                                }
+
+                                /* This is a nop on init */
+                                break;
+
                         case 26:
                                 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
                                 log_notice("Setting log target to journal-or-kmsg.");
@@ -1369,7 +1363,8 @@ static int process_event(Manager *m, struct epoll_event *ev) {
                 ssize_t k;
 
                 /* Some timer event, to be dispatched to the units */
-                if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) {
+                k = read(w->fd, &v, sizeof(v));
+                if (k != sizeof(v)) {
 
                         if (k < 0 && (errno == EINTR || errno == EAGAIN))
                                 break;
@@ -1553,8 +1548,10 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
 
 #ifdef HAVE_AUDIT
         char *p;
+        int audit_fd;
 
-        if (m->audit_fd < 0)
+        audit_fd = get_audit_fd();
+        if (audit_fd < 0)
                 return;
 
         /* Don't generate audit events if the service was already
@@ -1573,12 +1570,11 @@ void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) {
                 return;
         }
 
-        if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
+        if (audit_log_user_comm_message(audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) {
                 if (errno == EPERM) {
                         /* We aren't allowed to send audit messages?
                          * Then let's not retry again. */
-                        audit_close(m->audit_fd);
-                        m->audit_fd = -1;
+                        close_audit_fd();
                 } else
                         log_warning("Failed to send audit message: %m");
         }
@@ -2054,7 +2050,7 @@ void manager_check_finished(Manager *m) {
 
                         if (!log_on_console())
                                 log_struct(LOG_INFO,
-                                           "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_STARTUP_FINISHED),
+                                           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,
@@ -2070,7 +2066,7 @@ void manager_check_finished(Manager *m) {
 
                         if (!log_on_console())
                                 log_struct(LOG_INFO,
-                                           "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_STARTUP_FINISHED),
+                                           MESSAGE_ID(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.",
@@ -2085,7 +2081,7 @@ void manager_check_finished(Manager *m) {
 
                 if (!log_on_console())
                         log_struct(LOG_INFO,
-                                   "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_STARTUP_FINISHED),
+                                   MESSAGE_ID(SD_MESSAGE_STARTUP_FINISHED),
                                    "USERSPACE_USEC=%llu", (unsigned long long) userspace_usec,
                                    "MESSAGE=Startup finished in %s.",
                                    format_timespan(sum, sizeof(sum), total_usec),
@@ -2312,3 +2308,10 @@ bool manager_get_show_status(Manager *m) {
 
         return plymouth_running();
 }
+
+void watch_init(Watch *w) {
+        assert(w);
+
+        w->type = WATCH_INVALID;
+        w->fd = -1;
+}