]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: make --invocation and --list-invocations accept unit name without suffix
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 00:05:34 +0000 (09:05 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 7 Jan 2025 20:41:10 +0000 (20:41 +0000)
Fixes #35538.

(cherry picked from commit e8823b5e352711c68ab3282697a6ab1cfb06d25a)

src/journal/journalctl-util.c
test/units/TEST-04-JOURNAL.invocation.sh

index 1996bddf605856e67688168d169141969bddf15b..62d6ed7f32943ee42ba12d1c6072286e04254db3 100644 (file)
@@ -10,6 +10,7 @@
 #include "rlimit-util.h"
 #include "strv.h"
 #include "terminal-util.h"
+#include "unit-name.h"
 
 char* format_timestamp_maybe_utc(char *buf, size_t l, usec_t t) {
         assert(buf);
@@ -117,6 +118,7 @@ int journal_acquire_boot(sd_journal *j) {
 
 int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_type) {
         size_t n;
+        int r;
 
         assert(option_name);
         assert(ret_unit);
@@ -132,15 +134,26 @@ int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_
                                        "Using %s with multiple units is not supported.",
                                        option_name);
 
+        LogIdType type;
+        char **units;
         if (!strv_isempty(arg_system_units)) {
-                *ret_type = LOG_SYSTEM_UNIT_INVOCATION_ID;
-                *ret_unit = arg_system_units[0];
+                type = LOG_SYSTEM_UNIT_INVOCATION_ID;
+                units = arg_system_units;
         } else {
                 assert(!strv_isempty(arg_user_units));
-                *ret_type = LOG_USER_UNIT_INVOCATION_ID;
-                *ret_unit = arg_user_units[0];
+                type = LOG_USER_UNIT_INVOCATION_ID;
+                units = arg_user_units;
         }
 
+        _cleanup_free_ char *u = NULL;
+        r = unit_name_mangle(units[0], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &u);
+        if (r < 0)
+                return log_error_errno(r, "Failed to mangle unit name '%s': %m", units[0]);
+
+        free_and_replace(units[0], u);
+
+        *ret_type = type;
+        *ret_unit = units[0];
         return 0;
 }
 
index e7a6f547b4885be8284da2a190f04b246cbff826..3ac931e26330ec318367465ed3125af2c024a124 100755 (executable)
@@ -7,7 +7,8 @@ set -o pipefail
 # shellcheck source=test/units/util.sh
 . "$(dirname "$0")"/util.sh
 
-SERVICE_NAME=invocation-id-test-"$RANDOM".service
+SERVICE_NAME_SHORT=invocation-id-test-"$RANDOM"
+SERVICE_NAME="$SERVICE_NAME_SHORT".service
 
 TMP_DIR=$(mktemp -d)
 
@@ -26,6 +27,7 @@ done
 # systemd[1]: invocation-id-test-26448.service: Deactivated successfully.
 journalctl --sync
 
+journalctl --list-invocation -u "$SERVICE_NAME_SHORT" | tee "$TMP_DIR"/short
 journalctl --list-invocation -u "$SERVICE_NAME" | tee "$TMP_DIR"/10
 journalctl --list-invocation -u "$SERVICE_NAME" --reverse | tee "$TMP_DIR"/10-r
 journalctl --list-invocation -u "$SERVICE_NAME" -n +10 | tee "$TMP_DIR"/p10
@@ -44,6 +46,7 @@ journalctl --list-invocation -u "$SERVICE_NAME" -n +5 --reverse | tee "$TMP_DIR"
 [[ $(cat "$TMP_DIR"/p5 | wc -l) == 6 ]]
 [[ $(cat "$TMP_DIR"/p5-r | wc -l) == 6 ]]
 
+diff "$TMP_DIR"/10 "$TMP_DIR"/short
 diff <(tail -n 10 "$TMP_DIR"/10 | tac) <(tail -n 10 "$TMP_DIR"/10-r)
 diff <(tail -n 5 "$TMP_DIR"/10) <(tail -n 5 "$TMP_DIR"/5)
 diff <(tail -n 5 "$TMP_DIR"/10 | tac) <(tail -n 5 "$TMP_DIR"/5-r)
@@ -54,6 +57,8 @@ diff <(tail -n 10 "$TMP_DIR"/p10 | head -n 5 | tac) <(tail -n 5 "$TMP_DIR"/p5-r)
 tail -n 10 "$TMP_DIR"/10 |
     while read -r idx invocation _; do
         i="$(( idx + 10 ))"
+        assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME_SHORT")"
+        assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME_SHORT")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")"
@@ -62,6 +67,8 @@ tail -n 10 "$TMP_DIR"/10 |
 tail -n 10 "$TMP_DIR"/p10 |
     while read -r i invocation _; do
         idx="$(( i - 10 ))"
+        assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME_SHORT")"
+        assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME_SHORT")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${i}" -u "$SERVICE_NAME")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME")"
         assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${invocation}")"