assert(arg_action == ACTION_LIST_INVOCATIONS);
- r = acquire_unit("--list-invocations", &unit, &type);
+ r = acquire_journal(&j);
if (r < 0)
return r;
- r = acquire_journal(&j);
+ r = acquire_unit(j, "--list-invocations", &unit, &type);
if (r < 0)
return r;
#include <unistd.h>
+#include "glob-util.h"
#include "id128-util.h"
#include "journal-util.h"
#include "journalctl.h"
return 0;
}
-int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_type) {
+int acquire_unit(sd_journal *j, const char *option_name, const char **ret_unit, LogIdType *ret_type) {
size_t n;
int r;
+ assert(j);
assert(option_name);
assert(ret_unit);
assert(ret_type);
}
_cleanup_free_ char *u = NULL;
- r = unit_name_mangle(units[0], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &u);
+ r = unit_name_mangle(units[0], UNIT_NAME_MANGLE_GLOB | (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);
+ if (string_is_glob(u)) {
+ _cleanup_set_free_ Set *s = NULL;
+
+ r = get_possible_units(j, type == LOG_SYSTEM_UNIT_INVOCATION_ID ? SYSTEM_UNITS : USER_UNITS,
+ STRV_MAKE(u), &s);
+ if (r < 0)
+ return log_error_errno(r, "Failed to get matching unit '%s' from journal: %m", u);
+ if (set_isempty(s))
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No matching unit found for '%s' in journal.", u);
+ if (set_size(s) > 1)
+ return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Multiple matching units found for '%s' in journal.", u);
+
+ char *found = set_steal_first(s);
+ log_debug("Found matching unit '%s' for '%s'.", found, u);
+
+ free_and_replace(units[0], found);
+ assert(set_isempty(s));
+ } else
+ free_and_replace(units[0], u);
*ret_type = type;
*ret_unit = units[0];
* system unit or user unit, and calling without unit name is allowed. Otherwise, a unit name must
* be specified. */
if (arg_invocation_offset != 0 || sd_id128_is_null(arg_invocation_id)) {
- r = acquire_unit("-I/--invocation= with an offset", &unit, &type);
+ r = acquire_unit(j, "-I/--invocation= with an offset", &unit, &type);
if (r < 0)
return r;
}
bool journal_boot_has_effect(sd_journal *j);
int journal_acquire_boot(sd_journal *j);
int get_possible_units(sd_journal *j, const char *fields, char * const *patterns, Set **ret);
-int acquire_unit(const char *option_name, const char **ret_unit, LogIdType *ret_type);
+int acquire_unit(sd_journal *j, const char *option_name, const char **ret_unit, LogIdType *ret_type);
int journal_acquire_invocation(sd_journal *j);
# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh
-SERVICE_NAME_SHORT=invocation-id-test-"$RANDOM"
+BASE=test-"$RANDOM"
+SERVICE_NAME_SHORT=invocation-id-"$BASE"
SERVICE_NAME="$SERVICE_NAME_SHORT".service
+SERVICE_NAME_GLOB="invocation-*-${BASE}.service"
TMP_DIR=$(mktemp -d)
journalctl --sync
journalctl --list-invocation -u "$SERVICE_NAME_SHORT" | tee "$TMP_DIR"/short
+journalctl --list-invocation -u "$SERVICE_NAME_GLOB" | tee "$TMP_DIR"/glob
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
[[ $(cat "$TMP_DIR"/p5-r | wc -l) == 6 ]]
diff "$TMP_DIR"/10 "$TMP_DIR"/short
+diff "$TMP_DIR"/10 "$TMP_DIR"/glob
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)
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_GLOB")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME_GLOB")"
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}")"
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_GLOB")"
+ assert_in "invocation ${i} ${invocation}" "$(journalctl --no-hostname -n 1 -t bash --invocation="${idx}" -u "$SERVICE_NAME_GLOB")"
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}")"