chiark / gitweb /
logs-show: export logic to add matches for units
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Mar 2013 23:30:05 +0000 (19:30 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Mar 2013 23:53:29 +0000 (19:53 -0400)
After that functions which add matches, show_journal_by_unit
and show_journal_by_user_unit, become nearly identical, so
I merged them into one function.

src/shared/logs-show.c
src/shared/logs-show.h
src/systemctl/systemctl.c

index b909c24..4338684 100644 (file)
@@ -869,76 +869,68 @@ finish:
         return r;
 }
 
-int show_journal_by_unit(
-                FILE *f,
-                const char *unit,
-                OutputMode mode,
-                unsigned n_columns,
-                usec_t not_before,
-                unsigned how_many,
-                OutputFlags flags) {
-
-        _cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL;
-        sd_journal *j = NULL;
+int add_matches_for_unit(sd_journal *j, const char *unit) {
         int r;
+        _cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL;
 
-        assert(mode >= 0);
-        assert(mode < _OUTPUT_MODE_MAX);
+        assert(j);
         assert(unit);
 
-        if (how_many <= 0)
-                return 0;
-
         if (asprintf(&m1, "_SYSTEMD_UNIT=%s", unit) < 0 ||
             asprintf(&m2, "COREDUMP_UNIT=%s", unit) < 0 ||
-            asprintf(&m3, "UNIT=%s", unit) < 0) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
-        if (r < 0)
-                goto finish;
-
-        /* Look for messages from the service itself */
-        r = sd_journal_add_match(j, m1, 0);
-        if (r < 0)
-                goto finish;
+            asprintf(&m3, "UNIT=%s", unit) < 0)
+                return -ENOMEM;
 
-        /* Look for coredumps of the service */
-        r = sd_journal_add_disjunction(j);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m2, 0);
-        if (r < 0)
-                goto finish;
+        (void)(
+            /* Look for messages from the service itself */
+            (r = sd_journal_add_match(j, m1, 0)) ||
+
+            /* Look for coredumps of the service */
+            (r = sd_journal_add_disjunction(j)) ||
+            (r = sd_journal_add_match(j,
+                        "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
+            (r = sd_journal_add_match(j, m2, 0)) ||
+
+             /* Look for messages from PID 1 about this service */
+            (r = sd_journal_add_disjunction(j)) ||
+            (r = sd_journal_add_match(j, "_PID=1", 0)) ||
+            (r = sd_journal_add_match(j, m3, 0))
+        );
+        return r;
+}
 
-        /* Look for messages from PID 1 about this service */
-        r = sd_journal_add_disjunction(j);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, "_PID=1", 0);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m3, 0);
-        if (r < 0)
-                goto finish;
+int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
+        int r;
+        _cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL;
 
-        r = show_journal(f, j, mode, n_columns, not_before, how_many, flags);
-        if (r < 0)
-                goto finish;
+        assert(j);
+        assert(unit);
 
-finish:
-        if (j)
-                sd_journal_close(j);
+        if (asprintf(&m1, "_SYSTEMD_USER_UNIT=%s", unit) < 0 ||
+            asprintf(&m2, "USER_UNIT=%s", unit) < 0 ||
+            asprintf(&m3, "COREDUMP_USER_UNIT=%s", unit) < 0 ||
+            asprintf(&m4, "_UID=%d", uid) < 0)
+                return -ENOMEM;
 
+        (void) (
+                /* Look for messages from the user service itself */
+                (r = sd_journal_add_match(j, m1, 0)) ||
+                (r = sd_journal_add_match(j, m4, 0)) ||
+
+                /* Look for messages from systemd about this service */
+                (r = sd_journal_add_disjunction(j)) ||
+                (r = sd_journal_add_match(j, m2, 0)) ||
+                (r = sd_journal_add_match(j, m4, 0)) ||
+
+                /* Look for coredumps of the service */
+                (r = sd_journal_add_disjunction(j)) ||
+                (r = sd_journal_add_match(j, m3, 0)) ||
+                (r = sd_journal_add_match(j, m4, 0))
+        );
         return r;
 }
 
-int show_journal_by_user_unit(
+int show_journal_by_unit(
                 FILE *f,
                 const char *unit,
                 OutputMode mode,
@@ -946,11 +938,12 @@ int show_journal_by_user_unit(
                 usec_t not_before,
                 unsigned how_many,
                 uid_t uid,
-                OutputFlags flags) {
+                OutputFlags flags,
+                bool system) {
 
-        _cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL;
         sd_journal *j = NULL;
         int r;
+        int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM_ONLY;
 
         assert(mode >= 0);
         assert(mode < _OUTPUT_MODE_MAX);
@@ -959,45 +952,14 @@ int show_journal_by_user_unit(
         if (how_many <= 0)
                 return 0;
 
-        if (asprintf(&m1, "_SYSTEMD_USER_UNIT=%s", unit) < 0 ||
-            asprintf(&m2, "USER_UNIT=%s", unit) < 0 ||
-            asprintf(&m3, "COREDUMP_USER_UNIT=%s", unit) < 0 ||
-            asprintf(&m4, "_UID=%d", uid) < 0) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
-        if (r < 0)
-                goto finish;
-
-        /* Look for messages from the user service itself */
-        r = sd_journal_add_match(j, m1, 0);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m4, 0);
-        if (r < 0)
-                goto finish;
-
-        /* Look for messages from systemd about this service */
-        r = sd_journal_add_disjunction(j);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m2, 0);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m4, 0);
+        r = sd_journal_open(&j, jflags);
         if (r < 0)
                 goto finish;
 
-        /* Look for coredumps of the service */
-        r = sd_journal_add_disjunction(j);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m3, 0);
-        if (r < 0)
-                goto finish;
-        r = sd_journal_add_match(j, m4, 0);
+        if (system)
+                r = add_matches_for_unit(j, unit);
+        else
+                r = add_matches_for_user_unit(j, unit, uid);
         if (r < 0)
                 goto finish;
 
index a835112..5a4c9f2 100644 (file)
@@ -37,16 +37,16 @@ int output_journal(
                 unsigned n_columns,
                 OutputFlags flags);
 
-int show_journal_by_unit(
-                FILE *f,
+int add_matches_for_unit(
+                sd_journal *j,
+                const char *unit);
+
+int add_matches_for_user_unit(
+                sd_journal *j,
                 const char *unit,
-                OutputMode mode,
-                unsigned n_columns,
-                usec_t not_before,
-                unsigned how_many,
-                OutputFlags flags);
+                uid_t uid);
 
-int show_journal_by_user_unit(
+int show_journal_by_unit(
                 FILE *f,
                 const char *unit,
                 OutputMode mode,
@@ -54,7 +54,8 @@ int show_journal_by_user_unit(
                 usec_t not_before,
                 unsigned how_many,
                 uid_t uid,
-                OutputFlags flags);
+                OutputFlags flags,
+                bool system);
 
 void json_escape(
                 FILE *f,
index 4a55c56..5902a37 100644 (file)
@@ -2509,23 +2509,15 @@ static void print_status_info(UnitStatusInfo *i) {
 
         if (i->id && arg_transport != TRANSPORT_SSH) {
                 printf("\n");
-                if(arg_scope == UNIT_FILE_SYSTEM)
-                        show_journal_by_unit(stdout,
-                                             i->id,
-                                             arg_output,
-                                             0,
-                                             i->inactive_exit_timestamp_monotonic,
-                                             arg_lines,
-                                             flags);
-                else
-                        show_journal_by_user_unit(stdout,
-                                                  i->id,
-                                                  arg_output,
-                                                  0,
-                                                  i->inactive_exit_timestamp_monotonic,
-                                                  arg_lines,
-                                                  getuid(),
-                                                  flags);
+                show_journal_by_unit(stdout,
+                                     i->id,
+                                     arg_output,
+                                     0,
+                                     i->inactive_exit_timestamp_monotonic,
+                                     arg_lines,
+                                     getuid(),
+                                     flags,
+                                     arg_scope == UNIT_FILE_SYSTEM);
         }
 
         if (i->need_daemon_reload)