[systemd-devel] [PATCH] journalctl: add short version for _SYSTEMD_UNIT=

harald at redhat.com harald at redhat.com
Mon Mar 18 06:04:39 PDT 2013


From: Harald Hoyer <harald at redhat.com>

Instead of typing the rather unusual:
$ journalctl _SYSTEMD_UNIT=sshd.service

one can now type

$ journalctl sshd.service
-- Logs begin at So 2013-02-24 20:54:44 CET, end at Mo 2013-03-18 14:01:01 CET. --
Mär 18 07:48:26 lenovo sshd[400]: Server listening on 0.0.0.0 port 22.
Mär 18 07:48:26 lenovo sshd[400]: Server listening on :: port 22.
---
 shell-completion/bash/journalctl |  5 ++++-
 src/journal/journalctl.c         | 12 ++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/shell-completion/bash/journalctl b/shell-completion/bash/journalctl
index 1fca5ad..112a526 100644
--- a/shell-completion/bash/journalctl
+++ b/shell-completion/bash/journalctl
@@ -23,6 +23,9 @@ __contains_word () {
         return 1
 }
 
+__journal_get_all_units      () { systemctl --full --no-legend list-units --all \
+        | { while read -r a b; do printf "%s\n" "$a"; done; }; }
+
 __journal_fields=(MESSAGE{,_ID} PRIORITY CODE_{FILE,LINE,FUNC}
                   ERRNO SYSLOG_{FACILITY,IDENTIFIER,PID} COREDUMP_EXE
                   _{P,U,G}ID _COMM _EXE _CMDLINE
@@ -98,7 +101,7 @@ _journalctl() {
                 COMPREPLY=( $(compgen -W '${field_vals[*]}' -- "$cur") )
         else
                 compopt -o nospace
-                COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") )
+                COMPREPLY=( $(compgen -W '${__journal_fields[*]}' -S= -- "$cur") $(compgen -W '$(__journal_get_all_units)' -- "$cur") )
         fi
 }
 
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 65114b2..21fb149 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -511,6 +511,10 @@ static int generate_new_id128(void) {
         return 0;
 }
 
+static bool arg_is_unit(char *name) {
+        return strchr(name, '=') == NULL && unit_name_is_valid(name, true);
+}
+
 static int add_matches(sd_journal *j, char **args) {
         char **i;
         int r;
@@ -554,6 +558,14 @@ static int add_matches(sd_journal *j, char **args) {
 
                         r = sd_journal_add_match(j, t, 0);
                         free(t);
+                } else if (arg_is_unit(*i)) {
+                        char *t = NULL;
+                        t = strappend("_SYSTEMD_UNIT=", *i);
+                        if (!t)
+                                return log_oom();
+
+                        r = sd_journal_add_match(j, t, 0);
+                        free(t);
                 } else
                         r = sd_journal_add_match(j, *i, 0);
 
-- 
1.8.1



More information about the systemd-devel mailing list