mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-11 05:17:44 +03:00
logs-show: export logic to add matches for units
After that functions which add matches, show_journal_by_unit and show_journal_by_user_unit, become nearly identical, so I merged them into one function.
This commit is contained in:
parent
7d7681f70b
commit
886a64fe69
@ -869,76 +869,68 @@ finish:
|
||||
return r;
|
||||
}
|
||||
|
||||
int add_matches_for_unit(sd_journal *j, const char *unit) {
|
||||
int r;
|
||||
_cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL;
|
||||
|
||||
assert(j);
|
||||
assert(unit);
|
||||
|
||||
if (asprintf(&m1, "_SYSTEMD_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m2, "COREDUMP_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m3, "UNIT=%s", unit) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
(void)(
|
||||
/* Look for messages from the service itself */
|
||||
(r = sd_journal_add_match(j, m1, 0)) ||
|
||||
|
||||
/* Look for coredumps of the service */
|
||||
(r = sd_journal_add_disjunction(j)) ||
|
||||
(r = sd_journal_add_match(j,
|
||||
"MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
|
||||
(r = sd_journal_add_match(j, m2, 0)) ||
|
||||
|
||||
/* Look for messages from PID 1 about this service */
|
||||
(r = sd_journal_add_disjunction(j)) ||
|
||||
(r = sd_journal_add_match(j, "_PID=1", 0)) ||
|
||||
(r = sd_journal_add_match(j, m3, 0))
|
||||
);
|
||||
return r;
|
||||
}
|
||||
|
||||
int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
|
||||
int r;
|
||||
_cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL;
|
||||
|
||||
assert(j);
|
||||
assert(unit);
|
||||
|
||||
if (asprintf(&m1, "_SYSTEMD_USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m2, "USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m3, "COREDUMP_USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m4, "_UID=%d", uid) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
(void) (
|
||||
/* Look for messages from the user service itself */
|
||||
(r = sd_journal_add_match(j, m1, 0)) ||
|
||||
(r = sd_journal_add_match(j, m4, 0)) ||
|
||||
|
||||
/* Look for messages from systemd about this service */
|
||||
(r = sd_journal_add_disjunction(j)) ||
|
||||
(r = sd_journal_add_match(j, m2, 0)) ||
|
||||
(r = sd_journal_add_match(j, m4, 0)) ||
|
||||
|
||||
/* Look for coredumps of the service */
|
||||
(r = sd_journal_add_disjunction(j)) ||
|
||||
(r = sd_journal_add_match(j, m3, 0)) ||
|
||||
(r = sd_journal_add_match(j, m4, 0))
|
||||
);
|
||||
return r;
|
||||
}
|
||||
|
||||
int show_journal_by_unit(
|
||||
FILE *f,
|
||||
const char *unit,
|
||||
OutputMode mode,
|
||||
unsigned n_columns,
|
||||
usec_t not_before,
|
||||
unsigned how_many,
|
||||
OutputFlags flags) {
|
||||
|
||||
_cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL;
|
||||
sd_journal *j = NULL;
|
||||
int r;
|
||||
|
||||
assert(mode >= 0);
|
||||
assert(mode < _OUTPUT_MODE_MAX);
|
||||
assert(unit);
|
||||
|
||||
if (how_many <= 0)
|
||||
return 0;
|
||||
|
||||
if (asprintf(&m1, "_SYSTEMD_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m2, "COREDUMP_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m3, "UNIT=%s", unit) < 0) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for messages from the service itself */
|
||||
r = sd_journal_add_match(j, m1, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for coredumps of the service */
|
||||
r = sd_journal_add_disjunction(j);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m2, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for messages from PID 1 about this service */
|
||||
r = sd_journal_add_disjunction(j);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, "_PID=1", 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m3, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = show_journal(f, j, mode, n_columns, not_before, how_many, flags);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
finish:
|
||||
if (j)
|
||||
sd_journal_close(j);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int show_journal_by_user_unit(
|
||||
FILE *f,
|
||||
const char *unit,
|
||||
OutputMode mode,
|
||||
@ -946,11 +938,12 @@ int show_journal_by_user_unit(
|
||||
usec_t not_before,
|
||||
unsigned how_many,
|
||||
uid_t uid,
|
||||
OutputFlags flags) {
|
||||
OutputFlags flags,
|
||||
bool system) {
|
||||
|
||||
_cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL;
|
||||
sd_journal *j = NULL;
|
||||
int r;
|
||||
int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM_ONLY;
|
||||
|
||||
assert(mode >= 0);
|
||||
assert(mode < _OUTPUT_MODE_MAX);
|
||||
@ -959,45 +952,14 @@ int show_journal_by_user_unit(
|
||||
if (how_many <= 0)
|
||||
return 0;
|
||||
|
||||
if (asprintf(&m1, "_SYSTEMD_USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m2, "USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m3, "COREDUMP_USER_UNIT=%s", unit) < 0 ||
|
||||
asprintf(&m4, "_UID=%d", uid) < 0) {
|
||||
r = -ENOMEM;
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
|
||||
r = sd_journal_open(&j, jflags);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for messages from the user service itself */
|
||||
r = sd_journal_add_match(j, m1, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m4, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for messages from systemd about this service */
|
||||
r = sd_journal_add_disjunction(j);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m2, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m4, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
/* Look for coredumps of the service */
|
||||
r = sd_journal_add_disjunction(j);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m3, 0);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
r = sd_journal_add_match(j, m4, 0);
|
||||
if (system)
|
||||
r = add_matches_for_unit(j, unit);
|
||||
else
|
||||
r = add_matches_for_user_unit(j, unit, uid);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
|
@ -37,16 +37,16 @@ int output_journal(
|
||||
unsigned n_columns,
|
||||
OutputFlags flags);
|
||||
|
||||
int add_matches_for_unit(
|
||||
sd_journal *j,
|
||||
const char *unit);
|
||||
|
||||
int add_matches_for_user_unit(
|
||||
sd_journal *j,
|
||||
const char *unit,
|
||||
uid_t uid);
|
||||
|
||||
int show_journal_by_unit(
|
||||
FILE *f,
|
||||
const char *unit,
|
||||
OutputMode mode,
|
||||
unsigned n_columns,
|
||||
usec_t not_before,
|
||||
unsigned how_many,
|
||||
OutputFlags flags);
|
||||
|
||||
int show_journal_by_user_unit(
|
||||
FILE *f,
|
||||
const char *unit,
|
||||
OutputMode mode,
|
||||
@ -54,7 +54,8 @@ int show_journal_by_user_unit(
|
||||
usec_t not_before,
|
||||
unsigned how_many,
|
||||
uid_t uid,
|
||||
OutputFlags flags);
|
||||
OutputFlags flags,
|
||||
bool system);
|
||||
|
||||
void json_escape(
|
||||
FILE *f,
|
||||
|
@ -2509,23 +2509,15 @@ static void print_status_info(UnitStatusInfo *i) {
|
||||
|
||||
if (i->id && arg_transport != TRANSPORT_SSH) {
|
||||
printf("\n");
|
||||
if(arg_scope == UNIT_FILE_SYSTEM)
|
||||
show_journal_by_unit(stdout,
|
||||
i->id,
|
||||
arg_output,
|
||||
0,
|
||||
i->inactive_exit_timestamp_monotonic,
|
||||
arg_lines,
|
||||
flags);
|
||||
else
|
||||
show_journal_by_user_unit(stdout,
|
||||
i->id,
|
||||
arg_output,
|
||||
0,
|
||||
i->inactive_exit_timestamp_monotonic,
|
||||
arg_lines,
|
||||
getuid(),
|
||||
flags);
|
||||
show_journal_by_unit(stdout,
|
||||
i->id,
|
||||
arg_output,
|
||||
0,
|
||||
i->inactive_exit_timestamp_monotonic,
|
||||
arg_lines,
|
||||
getuid(),
|
||||
flags,
|
||||
arg_scope == UNIT_FILE_SYSTEM);
|
||||
}
|
||||
|
||||
if (i->need_daemon_reload)
|
||||
|
Loading…
Reference in New Issue
Block a user