Skip to content

Commit

Permalink
journalctl: introduce --current-invocation option
Browse files Browse the repository at this point in the history
This is useful when show logs from the current invocation of systemd units.
  • Loading branch information
yuwata committed Apr 24, 2024
1 parent af63eab commit f722900
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
10 changes: 10 additions & 0 deletions man/journalctl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@
<xi:include href="version-info.xml" xpointer="v198"/></listitem>
</varlistentry>

<varlistentry>
<term><option>--current-invocation</option></term>

<listitem><para>Show messages for the current or most recent invocation of specified systemd units.
This must be specified with at least one <option>-u/--unit=</option> or <option>--user-unit=</option>.
This implies <option>--boot 0</option>, and other boot offset or boot ID cannot be specified.</para>

<xi:include href="version-info.xml" xpointer="v256"/></listitem>
</varlistentry>

<varlistentry>
<term><option>-t</option></term>
<term><option>--identifier=<replaceable>SYSLOG_IDENTIFIER</replaceable></option></term>
Expand Down
2 changes: 1 addition & 1 deletion shell-completion/bash/journalctl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ _journalctl() {
--show-cursor --dmesg -k --pager-end -e -r --reverse
--utc -x --catalog --no-full --force --dump-catalog
--flush --rotate --sync --no-hostname -N --fields
--list-namespaces'
--list-namespaces --current-invocation'
[ARG]='-b --boot -D --directory --file -F --field -t --identifier
-T --exclude-identifier --facility -M --machine -o --output
-u --unit --user-unit -p --priority --root --case-sensitive
Expand Down
65 changes: 49 additions & 16 deletions src/journal/journalctl-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "sd-device.h"

#include "bus-util.h"
#include "chase.h"
#include "devnum-util.h"
#include "fileio.h"
Expand Down Expand Up @@ -143,7 +144,50 @@ static int get_possible_units(
"OBJECT_SYSTEMD_USER_UNIT\0" \
"_SYSTEMD_USER_SLICE\0"

static int acquire_bus(RuntimeScope runtime_scope, sd_bus **bus) {
BusTransport transport;
int r;

assert(bus);

if (*bus)
return 0;

transport = arg_machine ? BUS_TRANSPORT_MACHINE : BUS_TRANSPORT_LOCAL;
if (runtime_scope == RUNTIME_SCOPE_USER && transport != BUS_TRANSPORT_LOCAL)
r = bus_connect_transport(transport, arg_machine, runtime_scope, bus);
else
r = bus_connect_transport_systemd(transport, arg_machine, runtime_scope, bus);
if (r < 0)
return bus_log_connect_error(r, transport);

return 0;
}

static int add_unit_one(sd_journal *j, sd_bus **bus, const char *unit, bool system_unit) {
int r;

assert(j);
assert(bus);
assert(unit);

/* Currently, the DBus connection is required only for obtaining the current invocation ID. */
if (arg_current_invocation) {
r = acquire_bus(system_unit ? RUNTIME_SCOPE_SYSTEM : RUNTIME_SCOPE_USER, bus);
if (r < 0)
return r;
}

if (system_unit)
r = add_matches_for_unit_full(j, *bus, unit, arg_current_invocation);
else
r = add_matches_for_user_unit_full(j, *bus, unit, getuid(), arg_current_invocation);

return sd_journal_add_disjunction(j);
}

static int add_units(sd_journal *j) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_strv_free_ char **patterns = NULL;
bool added = false;
int r;
Expand All @@ -165,10 +209,7 @@ static int add_units(sd_journal *j) {
if (r < 0)
return r;
} else {
r = add_matches_for_unit(j, u);
if (r < 0)
return r;
r = sd_journal_add_disjunction(j);
r = add_unit_one(j, &bus, u, /* system_unit = */ true);
if (r < 0)
return r;
added = true;
Expand All @@ -184,16 +225,14 @@ static int add_units(sd_journal *j) {
return r;

SET_FOREACH(u, units) {
r = add_matches_for_unit(j, u);
if (r < 0)
return r;
r = sd_journal_add_disjunction(j);
r = add_unit_one(j, &bus, u, /* system_unit = */ true);
if (r < 0)
return r;
added = true;
}
}

bus = sd_bus_flush_close_unref(bus);
patterns = strv_free(patterns);

STRV_FOREACH(i, arg_user_units) {
Expand All @@ -208,10 +247,7 @@ static int add_units(sd_journal *j) {
if (r < 0)
return r;
} else {
r = add_matches_for_user_unit(j, u, getuid());
if (r < 0)
return r;
r = sd_journal_add_disjunction(j);
r = add_unit_one(j, &bus, u, /* system_unit = */ false);
if (r < 0)
return r;
added = true;
Expand All @@ -227,10 +263,7 @@ static int add_units(sd_journal *j) {
return r;

SET_FOREACH(u, units) {
r = add_matches_for_user_unit(j, u, getuid());
if (r < 0)
return r;
r = sd_journal_add_disjunction(j);
r = add_unit_one(j, &bus, u, /* system_unit = */ false);
if (r < 0)
return r;
added = true;
Expand Down
26 changes: 26 additions & 0 deletions src/journal/journalctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ char **arg_syslog_identifier = NULL;
char **arg_exclude_identifier = NULL;
char **arg_system_units = NULL;
char **arg_user_units = NULL;
bool arg_current_invocation = false;
const char *arg_field = NULL;
bool arg_catalog = false;
bool arg_reverse = false;
Expand Down Expand Up @@ -225,6 +226,7 @@ static int help(void) {
" -b --boot[=ID] Show current boot or the specified boot\n"
" -u --unit=UNIT Show logs from the specified unit\n"
" --user-unit=UNIT Show logs from the specified user unit\n"
" --current-invocation Show logs from the unit of the current invocation\n"
" -t --identifier=STRING Show entries with the specified syslog identifier\n"
" -T --exclude-identifier=STRING\n"
" Hide entries with the specified syslog identifier\n"
Expand Down Expand Up @@ -318,6 +320,7 @@ static int parse_argv(int argc, char *argv[]) {
ARG_CURSOR_FILE,
ARG_SHOW_CURSOR,
ARG_USER_UNIT,
ARG_CURRENT_INVOCATION,
ARG_LIST_CATALOG,
ARG_DUMP_CATALOG,
ARG_UPDATE_CATALOG,
Expand Down Expand Up @@ -387,6 +390,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "until", required_argument, NULL, 'U' },
{ "unit", required_argument, NULL, 'u' },
{ "user-unit", required_argument, NULL, ARG_USER_UNIT },
{ "current-invocation", no_argument, NULL, ARG_CURRENT_INVOCATION },
{ "field", required_argument, NULL, 'F' },
{ "fields", no_argument, NULL, 'N' },
{ "catalog", no_argument, NULL, 'x' },
Expand Down Expand Up @@ -832,6 +836,10 @@ static int parse_argv(int argc, char *argv[]) {
return log_oom();
break;

case ARG_CURRENT_INVOCATION:
arg_current_invocation = true;
break;

case 'F':
arg_action = ACTION_LIST_FIELDS;
arg_field = optarg;
Expand Down Expand Up @@ -942,6 +950,24 @@ static int parse_argv(int argc, char *argv[]) {
arg_boot_offset = 0;
}

if (arg_current_invocation) {
if (strv_isempty(arg_system_units) && strv_isempty(arg_user_units))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Using --current-invocation without -u/--unit=/--user-unit= is not supported.");
if (arg_directory || arg_root || arg_image || arg_file_stdin || arg_file)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Using --current-invocation with -D/--directory=/--root=/--image=/-i/--file= is not supported.");

if (arg_boot && (!sd_id128_is_null(arg_boot_id) || arg_boot_offset != 0))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Using --current-invocation with -b/--boot is not supported.");

/* --current-invocation implies --boot 0. */
arg_boot = true;
arg_boot_id = SD_ID128_NULL;
arg_boot_offset = 0;
}

if (!!arg_directory + !!arg_file + !!arg_machine + !!arg_root + !!arg_image > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Please specify at most one of -D/--directory=, --file=, -M/--machine=, --root=, --image=.");
Expand Down
1 change: 1 addition & 0 deletions src/journal/journalctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extern char **arg_syslog_identifier;
extern char **arg_exclude_identifier;
extern char **arg_system_units;
extern char **arg_user_units;
extern bool arg_current_invocation;
extern const char *arg_field;
extern bool arg_catalog;
extern bool arg_reverse;
Expand Down
5 changes: 5 additions & 0 deletions test/units/testsuite-04.journal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ journalctl -b "$(readlink -f "$0")" | grep . >/dev/null
journalctl -b "$(systemd-id128 boot-id)" | grep . >/dev/null
journalctl --since yesterday --reverse | grep . >/dev/null
journalctl --machine .host | grep . >/dev/null
journalctl --unit systemd-networkd.service | grep . >/dev/null
(! journalctl --current-invocation)
journalctl --unit systemd-networkd.service --current-invocation | grep . >/dev/null
journalctl --unit systemd-networkd.service --current-invocation -b 0 | grep . >/dev/null
(! journalctl --unit systemd-networkd.service --current-invocation -b -1)
# Log something that journald will forward to wall
echo "Oh no!" | systemd-cat -t "emerg$RANDOM" -p emerg --stderr-priority emerg

Expand Down

0 comments on commit f722900

Please sign in to comment.