From 886a64fe6903bc1ccde5c7af0eac6786918c8e49 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Wed, 13 Mar 2013 19:30:05 -0400 Subject: 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. --- src/shared/logs-show.c | 150 +++++++++++++++++----------------------------- src/shared/logs-show.h | 19 +++--- src/systemctl/systemctl.c | 26 +++----- 3 files changed, 75 insertions(+), 120 deletions(-) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index b909c24e98..43386841ba 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -869,76 +869,68 @@ finish: 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 add_matches_for_unit(sd_journal *j, const char *unit) { int r; + _cleanup_free_ char *m1 = NULL, *m2 = NULL, *m3 = NULL; - assert(mode >= 0); - assert(mode < _OUTPUT_MODE_MAX); + assert(j); 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; + asprintf(&m3, "UNIT=%s", unit) < 0) + return -ENOMEM; - /* 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; + (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; +} - /* 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; +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; - r = show_journal(f, j, mode, n_columns, not_before, how_many, flags); - if (r < 0) - goto finish; + assert(j); + assert(unit); -finish: - if (j) - sd_journal_close(j); + 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_user_unit( +int show_journal_by_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); - 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); + r = sd_journal_open(&j, jflags); 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; diff --git a/src/shared/logs-show.h b/src/shared/logs-show.h index a835112c99..5a4c9f24d7 100644 --- a/src/shared/logs-show.h +++ b/src/shared/logs-show.h @@ -37,16 +37,16 @@ int output_journal( unsigned n_columns, OutputFlags flags); -int show_journal_by_unit( - FILE *f, +int add_matches_for_unit( + sd_journal *j, + const char *unit); + +int add_matches_for_user_unit( + sd_journal *j, const char *unit, - OutputMode mode, - unsigned n_columns, - usec_t not_before, - unsigned how_many, - OutputFlags flags); + uid_t uid); -int show_journal_by_user_unit( +int show_journal_by_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, diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 4a55c566ec..5902a3798d 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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) -- cgit v1.2.3-54-g00ecf