Skip to content

Commit

Permalink
sd-journal: when SD_JOURNAL_CURRENT_USER is set, and called from syst…
Browse files Browse the repository at this point in the history
…em UID, imply SD_JOURNAL_SYSTEM

Fixes: #26742 #23679
(cherry picked from commit 97c621b)

Resolves: RHEL-31070
  • Loading branch information
poettering authored and dtardon committed Apr 9, 2024
1 parent b7072a9 commit 620b730
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/libsystemd/sd-journal/sd-journal.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "string-util.h"
#include "strv.h"
#include "syslog-util.h"
#include "uid-alloc-range.h"

#define JOURNAL_FILES_MAX 7168

Expand Down Expand Up @@ -1217,25 +1218,32 @@ static bool file_has_type_prefix(const char *prefix, const char *filename) {
static bool file_type_wanted(int flags, const char *filename) {
assert(filename);

if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
if (!ENDSWITH_SET(filename, ".journal", ".journal~"))
return false;

/* no flags set → every type is OK */
if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
return true;

if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
return true;

if (flags & SD_JOURNAL_CURRENT_USER) {
if (FLAGS_SET(flags, SD_JOURNAL_CURRENT_USER)) {
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];

xsprintf(prefix, "user-"UID_FMT, getuid());
xsprintf(prefix, "user-" UID_FMT, getuid());

if (file_has_type_prefix(prefix, filename))
return true;

/* If SD_JOURNAL_CURRENT_USER is specified and we are invoked under a system UID, then
* automatically enable SD_JOURNAL_SYSTEM too, because journald will actually put system user
* data into the system journal. */

if (uid_for_system_journal(getuid()))
flags |= SD_JOURNAL_SYSTEM;
}

if (FLAGS_SET(flags, SD_JOURNAL_SYSTEM) && file_has_type_prefix("system", filename))
return true;

return false;
}

Expand Down

0 comments on commit 620b730

Please sign in to comment.