[systemd-commits] 3 commits - man/journalctl.xml src/journal src/shared src/systemctl

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Wed Mar 13 16:54:19 PDT 2013


 man/journalctl.xml        |   26 ++++---
 src/journal/journalctl.c  |   27 +++-----
 src/shared/logs-show.c    |  150 +++++++++++++++++-----------------------------
 src/shared/logs-show.h    |   19 +++--
 src/systemctl/systemctl.c |   43 +++++--------
 5 files changed, 111 insertions(+), 154 deletions(-)

New commits:
commit 427b47c4abaf4b5820e3532bf6993c7abe66703c
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Mar 13 19:40:01 2013 -0400

    initctl: catch write error, use _cleanup_
    
    != operator always returns something nonnegative, so the
    error condition was not caught.

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 5902a37..84b7c8e 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -5041,21 +5041,22 @@ finish:
 }
 
 static int talk_initctl(void) {
-        struct init_request request;
-        int r, fd;
+        struct init_request request = {0};
+        int r;
+        int _cleanup_close_ fd = -1;
         char rl;
 
-        if (!(rl = action_to_runlevel()))
+        rl = action_to_runlevel();
+        if (!rl)
                 return 0;
 
-        zero(request);
         request.magic = INIT_MAGIC;
         request.sleeptime = 0;
         request.cmd = INIT_CMD_RUNLVL;
         request.runlevel = rl;
 
-        if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
-
+        fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY);
+        if (fd < 0) {
                 if (errno == ENOENT)
                         return 0;
 
@@ -5065,9 +5066,7 @@ static int talk_initctl(void) {
 
         errno = 0;
         r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
-        close_nointr_nofail(fd);
-
-        if (r < 0) {
+        if (r) {
                 log_error("Failed to write to "INIT_FIFO": %m");
                 return errno ? -errno : -EIO;
         }

commit 7199aa96ce19468d9ce4cb21a13e32a3792fde41
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Mar 13 19:30:05 2013 -0400

    journalctl: show systemd messages about unit for -u
    
    journalctl -u unit is not very useful, because it doesn't show
    systemd messages about starting, stopping, coredumps, etc,
    like systemctl status unit does. Make it show the same
    information using the same rules.
    
    If somebody really want to see just messages from by the unit,
    it is easy enough to use _SYSTEMD_UNIT=...

diff --git a/man/journalctl.xml b/man/journalctl.xml
index 4bbc288..632a813 100644
--- a/man/journalctl.xml
+++ b/man/journalctl.xml
@@ -324,22 +324,28 @@
                                 <term><option>-u</option></term>
                                 <term><option>--unit=</option></term>
 
-                                <listitem><para>Show data only of the
-                                specified unit. This will add a match
-                                for <literal>_SYSTEMD_UNIT=</literal>
-                                for the specified
-                                unit.</para></listitem>
+                                <listitem><para>Show messages for the
+                                specified systemd unit. This will add
+                                a match for messages from the unit
+                                (<literal>_SYSTEMD_UNIT=</literal>)
+                                and additional matches for messages
+                                from systemd and messages about
+                                coredumps for the specified unit.
+                                </para></listitem>
                         </varlistentry>
 
                         <varlistentry>
                                 <term><option>--user-unit=</option></term>
 
-                                <listitem><para>Show data only of the
+                                <listitem><para>Show messages for the
                                 specified user session unit. This will
-                                add a match for
-                                <literal>_SYSTEMD_USER_UNIT=</literal>
-                                for the specified
-                                unit.</para></listitem>
+                                add a match for messages from the unit
+                                (<literal>_SYSTEMD_USER_UNIT=</literal>
+                                and <literal>_UID=</literal>) and
+                                additional matches for messages from
+                                session systemd and messages about
+                                coredumps for the specified unit.
+                                </para></listitem>
                         </varlistentry>
 
                         <varlistentry>
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 3682329..65114b2 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -38,6 +38,7 @@
 #include <systemd/sd-journal.h>
 
 #include "log.h"
+#include "logs-show.h"
 #include "util.h"
 #include "path-util.h"
 #include "build.h"
@@ -76,7 +77,7 @@ static usec_t arg_interval = DEFAULT_FSS_INTERVAL_USEC;
 static usec_t arg_since, arg_until;
 static bool arg_since_set = false, arg_until_set = false;
 static const char *arg_unit = NULL;
-static const char *arg_unit_type = NULL;
+static bool arg_unit_system;
 static const char *arg_field = NULL;
 static bool arg_catalog = false;
 static bool arg_reverse = false;
@@ -187,8 +188,8 @@ static int parse_argv(int argc, char *argv[]) {
                 { "cursor",       required_argument, NULL, 'c'              },
                 { "since",        required_argument, NULL, ARG_SINCE        },
                 { "until",        required_argument, NULL, ARG_UNTIL        },
-                { "user-unit",    required_argument, NULL, ARG_USER_UNIT    },
                 { "unit",         required_argument, NULL, 'u'              },
+                { "user-unit",    required_argument, NULL, ARG_USER_UNIT    },
                 { "field",        required_argument, NULL, 'F'              },
                 { "catalog",      no_argument,       NULL, 'x'              },
                 { "list-catalog", no_argument,       NULL, ARG_LIST_CATALOG },
@@ -419,14 +420,14 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_until_set = true;
                         break;
 
-                case ARG_USER_UNIT:
+                case 'u':
                         arg_unit = optarg;
-                        arg_unit_type = "_SYSTEMD_USER_UNIT=";
+                        arg_unit_system = true;
                         break;
 
-                case 'u':
+                case ARG_USER_UNIT:
                         arg_unit = optarg;
-                        arg_unit_type = "_SYSTEMD_UNIT=";
+                        arg_unit_system = false;
                         break;
 
                 case '?':
@@ -604,16 +605,12 @@ static int add_unit(sd_journal *j) {
         if (!u)
                 return log_oom();
 
-        m = strappend(arg_unit_type, u);
-
-        if (!m)
-                return log_oom();
-
-        r = sd_journal_add_match(j, m, strlen(m));
-        if (r < 0) {
-                log_error("Failed to add match: %s", strerror(-r));
+        if (arg_unit_system)
+                r = add_matches_for_unit(j, u);
+        else
+                r = add_matches_for_user_unit(j, u, getuid());
+        if (r < 0)
                 return r;
-        }
 
         return 0;
 }

commit 886a64fe6903bc1ccde5c7af0eac6786918c8e49
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Wed Mar 13 19:30:05 2013 -0400

    logs-show: export logic to add matches for units
    
    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.

diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
index b909c24..4338684 100644
--- a/src/shared/logs-show.c
+++ b/src/shared/logs-show.c
@@ -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;
 
diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h
index a835112..5a4c9f2 100644
--- a/src/shared/logs-show.h
+++ b/src/shared/logs-show.h
@@ -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,
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 4a55c56..5902a37 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -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)



More information about the systemd-commits mailing list