Skip to content

Commit 7396b0e

Browse files
committed
journalctl: introduce --current-invocation option
This may be useful when show logs from the current invocation of systemd units.
1 parent 4344396 commit 7396b0e

File tree

6 files changed

+92
-17
lines changed

6 files changed

+92
-17
lines changed

man/journalctl.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,16 @@
350350
<xi:include href="version-info.xml" xpointer="v198"/></listitem>
351351
</varlistentry>
352352

353+
<varlistentry>
354+
<term><option>--current-invocation</option></term>
355+
356+
<listitem><para>Show messages for the current or most recent invocation of specified systemd units.
357+
This must be specified with at least one <option>-u/--unit=</option> or <option>--user-unit=</option>.
358+
This implies <option>--boot 0</option>, and other boot offset or boot ID cannot be specified.</para>
359+
360+
<xi:include href="version-info.xml" xpointer="v256"/></listitem>
361+
</varlistentry>
362+
353363
<varlistentry>
354364
<term><option>-t</option></term>
355365
<term><option>--identifier=<replaceable>SYSLOG_IDENTIFIER</replaceable></option></term>

shell-completion/bash/journalctl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ _journalctl() {
4747
--show-cursor --dmesg -k --pager-end -e -r --reverse
4848
--utc -x --catalog --no-full --force --dump-catalog
4949
--flush --rotate --sync --no-hostname -N --fields
50-
--list-namespaces'
50+
--list-namespaces --current-invocation'
5151
[ARG]='-b --boot -D --directory --file -F --field -t --identifier
5252
-T --exclude-identifier --facility -M --machine -o --output
5353
-u --unit --user-unit -p --priority --root --case-sensitive

src/journal/journalctl-filter.c

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "sd-device.h"
44

5+
#include "bus-util.h"
56
#include "chase.h"
67
#include "devnum-util.h"
78
#include "fileio.h"
@@ -143,7 +144,50 @@ static int get_possible_units(
143144
"OBJECT_SYSTEMD_USER_UNIT\0" \
144145
"_SYSTEMD_USER_SLICE\0"
145146

147+
static int acquire_bus(RuntimeScope runtime_scope, sd_bus **bus) {
148+
BusTransport transport;
149+
int r;
150+
151+
assert(bus);
152+
153+
if (*bus)
154+
return 0;
155+
156+
transport = arg_machine ? BUS_TRANSPORT_MACHINE : BUS_TRANSPORT_LOCAL;
157+
if (runtime_scope == RUNTIME_SCOPE_USER && transport != BUS_TRANSPORT_LOCAL)
158+
r = bus_connect_transport(transport, arg_machine, runtime_scope, bus);
159+
else
160+
r = bus_connect_transport_systemd(transport, arg_machine, runtime_scope, bus);
161+
if (r < 0)
162+
return bus_log_connect_error(r, transport);
163+
164+
return 0;
165+
}
166+
167+
static int add_unit_one(sd_journal *j, sd_bus **bus, const char *unit, RuntimeScope runtime_scope) {
168+
int r;
169+
170+
assert(j);
171+
assert(bus);
172+
assert(unit);
173+
174+
/* Currently, the DBus connection is required only for obtaining the current invocation ID. */
175+
if (arg_current_invocation) {
176+
r = acquire_bus(runtime_scope, bus);
177+
if (r < 0)
178+
return r;
179+
}
180+
181+
if (runtime_scope == RUNTIME_SCOPE_SYSTEM)
182+
r = add_matches_for_unit_full(j, *bus, unit, arg_current_invocation);
183+
else
184+
r = add_matches_for_user_unit_full(j, *bus, unit, getuid(), arg_current_invocation);
185+
186+
return sd_journal_add_disjunction(j);
187+
}
188+
146189
static int add_units(sd_journal *j) {
190+
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
147191
_cleanup_strv_free_ char **patterns = NULL;
148192
bool added = false;
149193
int r;
@@ -165,10 +209,7 @@ static int add_units(sd_journal *j) {
165209
if (r < 0)
166210
return r;
167211
} else {
168-
r = add_matches_for_unit(j, u);
169-
if (r < 0)
170-
return r;
171-
r = sd_journal_add_disjunction(j);
212+
r = add_unit_one(j, &bus, u, RUNTIME_SCOPE_SYSTEM);
172213
if (r < 0)
173214
return r;
174215
added = true;
@@ -184,16 +225,14 @@ static int add_units(sd_journal *j) {
184225
return r;
185226

186227
SET_FOREACH(u, units) {
187-
r = add_matches_for_unit(j, u);
188-
if (r < 0)
189-
return r;
190-
r = sd_journal_add_disjunction(j);
228+
r = add_unit_one(j, &bus, u, RUNTIME_SCOPE_SYSTEM);
191229
if (r < 0)
192230
return r;
193231
added = true;
194232
}
195233
}
196234

235+
bus = sd_bus_flush_close_unref(bus);
197236
patterns = strv_free(patterns);
198237

199238
STRV_FOREACH(i, arg_user_units) {
@@ -208,10 +247,7 @@ static int add_units(sd_journal *j) {
208247
if (r < 0)
209248
return r;
210249
} else {
211-
r = add_matches_for_user_unit(j, u, getuid());
212-
if (r < 0)
213-
return r;
214-
r = sd_journal_add_disjunction(j);
250+
r = add_unit_one(j, &bus, u, RUNTIME_SCOPE_USER);
215251
if (r < 0)
216252
return r;
217253
added = true;
@@ -227,10 +263,7 @@ static int add_units(sd_journal *j) {
227263
return r;
228264

229265
SET_FOREACH(u, units) {
230-
r = add_matches_for_user_unit(j, u, getuid());
231-
if (r < 0)
232-
return r;
233-
r = sd_journal_add_disjunction(j);
266+
r = add_unit_one(j, &bus, u, RUNTIME_SCOPE_USER);
234267
if (r < 0)
235268
return r;
236269
added = true;

src/journal/journalctl.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ char **arg_syslog_identifier = NULL;
7272
char **arg_exclude_identifier = NULL;
7373
char **arg_system_units = NULL;
7474
char **arg_user_units = NULL;
75+
bool arg_current_invocation = false;
7576
const char *arg_field = NULL;
7677
bool arg_catalog = false;
7778
bool arg_reverse = false;
@@ -225,6 +226,7 @@ static int help(void) {
225226
" -b --boot[=ID] Show current boot or the specified boot\n"
226227
" -u --unit=UNIT Show logs from the specified unit\n"
227228
" --user-unit=UNIT Show logs from the specified user unit\n"
229+
" --current-invocation Show logs from the unit of the current invocation\n"
228230
" -t --identifier=STRING Show entries with the specified syslog identifier\n"
229231
" -T --exclude-identifier=STRING\n"
230232
" Hide entries with the specified syslog identifier\n"
@@ -318,6 +320,7 @@ static int parse_argv(int argc, char *argv[]) {
318320
ARG_CURSOR_FILE,
319321
ARG_SHOW_CURSOR,
320322
ARG_USER_UNIT,
323+
ARG_CURRENT_INVOCATION,
321324
ARG_LIST_CATALOG,
322325
ARG_DUMP_CATALOG,
323326
ARG_UPDATE_CATALOG,
@@ -387,6 +390,7 @@ static int parse_argv(int argc, char *argv[]) {
387390
{ "until", required_argument, NULL, 'U' },
388391
{ "unit", required_argument, NULL, 'u' },
389392
{ "user-unit", required_argument, NULL, ARG_USER_UNIT },
393+
{ "current-invocation", no_argument, NULL, ARG_CURRENT_INVOCATION },
390394
{ "field", required_argument, NULL, 'F' },
391395
{ "fields", no_argument, NULL, 'N' },
392396
{ "catalog", no_argument, NULL, 'x' },
@@ -832,6 +836,10 @@ static int parse_argv(int argc, char *argv[]) {
832836
return log_oom();
833837
break;
834838

839+
case ARG_CURRENT_INVOCATION:
840+
arg_current_invocation = true;
841+
break;
842+
835843
case 'F':
836844
arg_action = ACTION_LIST_FIELDS;
837845
arg_field = optarg;
@@ -942,6 +950,24 @@ static int parse_argv(int argc, char *argv[]) {
942950
arg_boot_offset = 0;
943951
}
944952

953+
if (arg_current_invocation) {
954+
if (strv_isempty(arg_system_units) && strv_isempty(arg_user_units))
955+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
956+
"Using --current-invocation without -u/--unit=/--user-unit= is not supported.");
957+
if (arg_directory || arg_root || arg_image || arg_file_stdin || arg_file)
958+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
959+
"Using --current-invocation with -D/--directory=/--root=/--image=/-i/--file= is not supported.");
960+
961+
if (arg_boot && (!sd_id128_is_null(arg_boot_id) || arg_boot_offset != 0))
962+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
963+
"Using --current-invocation with a different boot is not supported.");
964+
965+
/* --current-invocation implies --boot 0. */
966+
arg_boot = true;
967+
arg_boot_id = SD_ID128_NULL;
968+
arg_boot_offset = 0;
969+
}
970+
945971
if (!!arg_directory + !!arg_file + !!arg_machine + !!arg_root + !!arg_image > 1)
946972
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
947973
"Please specify at most one of -D/--directory=, --file=, -M/--machine=, --root=, --image=.");

src/journal/journalctl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern char **arg_syslog_identifier;
7676
extern char **arg_exclude_identifier;
7777
extern char **arg_system_units;
7878
extern char **arg_user_units;
79+
extern bool arg_current_invocation;
7980
extern const char *arg_field;
8081
extern bool arg_catalog;
8182
extern bool arg_reverse;

test/units/testsuite-04.journal.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ journalctl -b "$(readlink -f "$0")" | grep . >/dev/null
166166
journalctl -b "$(systemd-id128 boot-id)" | grep . >/dev/null
167167
journalctl --since yesterday --reverse | grep . >/dev/null
168168
journalctl --machine .host | grep . >/dev/null
169+
journalctl --unit systemd-networkd.service | grep . >/dev/null
170+
(! journalctl --current-invocation)
171+
journalctl --unit systemd-networkd.service --current-invocation | grep . >/dev/null
172+
journalctl --unit systemd-networkd.service --current-invocation -b 0 | grep . >/dev/null
173+
(! journalctl --unit systemd-networkd.service --current-invocation -b -1)
169174
# Log something that journald will forward to wall
170175
echo "Oh no!" | systemd-cat -t "emerg$RANDOM" -p emerg --stderr-priority emerg
171176

0 commit comments

Comments
 (0)