chiark / gitweb /
bash-completion: simplify udevadm completion
[elogind.git] / src / journal / journald-server.c
index 44ba916f1045d56f262f9d9385afc206a2c03ae1..821935c39048bea38e2736909fd1d1a1f46b9a72 100644 (file)
@@ -352,13 +352,12 @@ void server_rotate(Server *s) {
 }
 
 void server_sync(Server *s) {
+        static const struct itimerspec sync_timer_disable = {};
         JournalFile *f;
         void *k;
         Iterator i;
         int r;
 
-        static const struct itimerspec sync_timer_disable = {};
-
         if (s->system_journal) {
                 r = journal_file_set_offline(s->system_journal);
                 if (r < 0)
@@ -443,7 +442,7 @@ bool shall_try_append_again(JournalFile *f, int r) {
         return true;
 }
 
-static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
+static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n, int priority) {
         JournalFile *f;
         bool vacuumed = false;
         int r;
@@ -469,7 +468,7 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
 
         r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
         if (r >= 0) {
-                server_schedule_sync(s);
+                server_schedule_sync(s, priority);
                 return;
         }
 
@@ -499,7 +498,8 @@ static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned
                         size += iovec[i].iov_len;
 
                 log_error("Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %s", n, size, strerror(-r));
-        }
+        } else
+                server_schedule_sync(s, priority);
 }
 
 static void dispatch_message_real(
@@ -509,6 +509,7 @@ static void dispatch_message_real(
                 struct timeval *tv,
                 const char *label, size_t label_len,
                 const char *unit_id,
+                int priority,
                 pid_t object_pid) {
 
         char    pid[sizeof("_PID=") + DECIMAL_STR_MAX(pid_t)],
@@ -523,7 +524,6 @@ static void dispatch_message_real(
                 o_owner_uid[sizeof("OBJECT_SYSTEMD_OWNER_UID=") + DECIMAL_STR_MAX(uid_t)];
         uid_t object_uid;
         gid_t object_gid;
-
         char *x;
         sd_id128_t id;
         int r;
@@ -578,6 +578,13 @@ static void dispatch_message_real(
                         IOVEC_SET_STRING(iovec[n++], x);
                 }
 
+                r = get_process_capeff(ucred->pid, &t);
+                if (r >= 0) {
+                        x = strappenda("_CAP_EFFECTIVE=", t);
+                        free(t);
+                        IOVEC_SET_STRING(iovec[n++], x);
+                }
+
 #ifdef HAVE_AUDIT
                 r = audit_session_from_pid(ucred->pid, &audit);
                 if (r >= 0) {
@@ -616,19 +623,20 @@ static void dispatch_message_real(
                         if (cg_path_get_unit(c, &t) >= 0) {
                                 x = strappenda("_SYSTEMD_UNIT=", t);
                                 free(t);
-                        } else if (cg_path_get_user_unit(c, &t) >= 0) {
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        } else if (unit_id && !session) {
+                                x = strappenda("_SYSTEMD_UNIT=", unit_id);
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        }
+
+                        if (cg_path_get_user_unit(c, &t) >= 0) {
                                 x = strappenda("_SYSTEMD_USER_UNIT=", t);
                                 free(t);
-                        } else if (unit_id) {
-                                if (session)
-                                        x = strappenda("_SYSTEMD_USER_UNIT=", unit_id);
-                                else
-                                        x = strappenda("_SYSTEMD_UNIT=", unit_id);
-                        } else
-                                x = NULL;
-
-                        if (x)
                                 IOVEC_SET_STRING(iovec[n++], x);
+                        } else if (unit_id && session) {
+                                x = strappenda("_SYSTEMD_USER_UNIT=", unit_id);
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        }
 
                         free(c);
                 }
@@ -721,14 +729,14 @@ static void dispatch_message_real(
                         if (cg_path_get_unit(c, &t) >= 0) {
                                 x = strappenda("OBJECT_SYSTEMD_UNIT=", t);
                                 free(t);
-                        } else if (cg_path_get_user_unit(c, &t) >= 0) {
+                                IOVEC_SET_STRING(iovec[n++], x);
+                        }
+
+                        if (cg_path_get_user_unit(c, &t) >= 0) {
                                 x = strappenda("OBJECT_SYSTEMD_USER_UNIT=", t);
                                 free(t);
-                        } else
-                                x = NULL;
-
-                        if (x)
                                 IOVEC_SET_STRING(iovec[n++], x);
+                        }
 
                         free(c);
                 }
@@ -778,7 +786,7 @@ static void dispatch_message_real(
         else
                 journal_uid = 0;
 
-        write_to_journal(s, journal_uid, iovec, n);
+        write_to_journal(s, journal_uid, iovec, n, priority);
 }
 
 void server_driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
@@ -812,7 +820,7 @@ void server_driver_message(Server *s, sd_id128_t message_id, const char *format,
         ucred.uid = getuid();
         ucred.gid = getgid();
 
-        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, 0);
+        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL, LOG_INFO, 0);
 }
 
 void server_dispatch_message(
@@ -838,6 +846,11 @@ void server_dispatch_message(
         if (LOG_PRI(priority) > s->max_level_store)
                 return;
 
+        /* Stop early in case the information will not be stored
+         * in a journal. */
+        if (s->storage == STORAGE_NONE)
+                return;
+
         if (!ucred)
                 goto finish;
 
@@ -873,7 +886,7 @@ void server_dispatch_message(
                                       "Suppressed %u messages from %s", rl - 1, path);
 
 finish:
-        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, object_pid);
+        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id, priority, object_pid);
 }
 
 
@@ -910,11 +923,12 @@ static int system_journal_open(Server *s) {
 
                 if (r >= 0)
                         server_fix_perms(s, s->system_journal, 0);
-        } else if (r < 0) {
-                if (r != -ENOENT && r != -EROFS)
-                        log_warning("Failed to open system journal: %s", strerror(-r));
+                else if (r < 0) {
+                        if (r != -ENOENT && r != -EROFS)
+                                log_warning("Failed to open system journal: %s", strerror(-r));
 
-                r = 0;
+                        r = 0;
+                }
         }
 
         if (!s->runtime_journal &&
@@ -1409,11 +1423,17 @@ static int server_open_sync_timer(Server *s) {
         return 0;
 }
 
-int server_schedule_sync(Server *s) {
+int server_schedule_sync(Server *s, int priority) {
         int r;
 
         assert(s);
 
+        if (priority <= LOG_CRIT) {
+                /* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
+                server_sync(s);
+                return 0;
+        }
+
         if (s->sync_scheduled)
                 return 0;