1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

journal: add ability to filter by current user

This is the just the library part.

SD_JOURNAL_CURRENT_USER flags is added to sd_j_open(), to open
files from current user.

SD_JOURNAL_SYSTEM_ONLY is renamed to SD_JOURNAL_SYSTEM,
and changed to mean to (also) open system files. This way various
flags can be combined, which gives them nicer semantics, especially
if other ones are added later.

Backwards compatibility is kept, because SD_JOURNAL_SYSTEM_ONLY
is equivalent to SD_JOURNAL_SYSTEM if used alone, and before there
we no other flags.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-06-04 22:31:05 -04:00
parent c5a10d9ca0
commit a688baa8b7
7 changed files with 85 additions and 14 deletions

View File

@ -107,6 +107,7 @@ MANPAGES_ALIAS += \
man/SD_ID128_MAKE.3 \ man/SD_ID128_MAKE.3 \
man/SD_INFO.3 \ man/SD_INFO.3 \
man/SD_JOURNAL_APPEND.3 \ man/SD_JOURNAL_APPEND.3 \
man/SD_JOURNAL_CURRENT_USER.3 \
man/SD_JOURNAL_FOREACH.3 \ man/SD_JOURNAL_FOREACH.3 \
man/SD_JOURNAL_FOREACH_BACKWARDS.3 \ man/SD_JOURNAL_FOREACH_BACKWARDS.3 \
man/SD_JOURNAL_FOREACH_DATA.3 \ man/SD_JOURNAL_FOREACH_DATA.3 \
@ -116,6 +117,7 @@ MANPAGES_ALIAS += \
man/SD_JOURNAL_NOP.3 \ man/SD_JOURNAL_NOP.3 \
man/SD_JOURNAL_RUNTIME_ONLY.3 \ man/SD_JOURNAL_RUNTIME_ONLY.3 \
man/SD_JOURNAL_SUPPRESS_LOCATION.3 \ man/SD_JOURNAL_SUPPRESS_LOCATION.3 \
man/SD_JOURNAL_SYSTEM.3 \
man/SD_JOURNAL_SYSTEM_ONLY.3 \ man/SD_JOURNAL_SYSTEM_ONLY.3 \
man/SD_LISTEN_FDS_START.3 \ man/SD_LISTEN_FDS_START.3 \
man/SD_NOTICE.3 \ man/SD_NOTICE.3 \
@ -205,6 +207,7 @@ man/SD_ID128_FORMAT_VAL.3: man/sd-id128.3
man/SD_ID128_MAKE.3: man/sd-id128.3 man/SD_ID128_MAKE.3: man/sd-id128.3
man/SD_INFO.3: man/sd-daemon.3 man/SD_INFO.3: man/sd-daemon.3
man/SD_JOURNAL_APPEND.3: man/sd_journal_get_fd.3 man/SD_JOURNAL_APPEND.3: man/sd_journal_get_fd.3
man/SD_JOURNAL_CURRENT_USER.3: man/sd_journal_open.3
man/SD_JOURNAL_FOREACH.3: man/sd_journal_next.3 man/SD_JOURNAL_FOREACH.3: man/sd_journal_next.3
man/SD_JOURNAL_FOREACH_BACKWARDS.3: man/sd_journal_next.3 man/SD_JOURNAL_FOREACH_BACKWARDS.3: man/sd_journal_next.3
man/SD_JOURNAL_FOREACH_DATA.3: man/sd_journal_get_data.3 man/SD_JOURNAL_FOREACH_DATA.3: man/sd_journal_get_data.3
@ -214,6 +217,7 @@ man/SD_JOURNAL_LOCAL_ONLY.3: man/sd_journal_open.3
man/SD_JOURNAL_NOP.3: man/sd_journal_get_fd.3 man/SD_JOURNAL_NOP.3: man/sd_journal_get_fd.3
man/SD_JOURNAL_RUNTIME_ONLY.3: man/sd_journal_open.3 man/SD_JOURNAL_RUNTIME_ONLY.3: man/sd_journal_open.3
man/SD_JOURNAL_SUPPRESS_LOCATION.3: man/sd_journal_print.3 man/SD_JOURNAL_SUPPRESS_LOCATION.3: man/sd_journal_print.3
man/SD_JOURNAL_SYSTEM.3: man/sd_journal_open.3
man/SD_JOURNAL_SYSTEM_ONLY.3: man/sd_journal_open.3 man/SD_JOURNAL_SYSTEM_ONLY.3: man/sd_journal_open.3
man/SD_LISTEN_FDS_START.3: man/sd_listen_fds.3 man/SD_LISTEN_FDS_START.3: man/sd_listen_fds.3
man/SD_NOTICE.3: man/sd-daemon.3 man/SD_NOTICE.3: man/sd-daemon.3
@ -325,6 +329,9 @@ man/SD_INFO.html: man/sd-daemon.html
man/SD_JOURNAL_APPEND.html: man/sd_journal_get_fd.html man/SD_JOURNAL_APPEND.html: man/sd_journal_get_fd.html
$(html-alias) $(html-alias)
man/SD_JOURNAL_CURRENT_USER.html: man/sd_journal_open.html
$(html-alias)
man/SD_JOURNAL_FOREACH.html: man/sd_journal_next.html man/SD_JOURNAL_FOREACH.html: man/sd_journal_next.html
$(html-alias) $(html-alias)
@ -352,6 +359,9 @@ man/SD_JOURNAL_RUNTIME_ONLY.html: man/sd_journal_open.html
man/SD_JOURNAL_SUPPRESS_LOCATION.html: man/sd_journal_print.html man/SD_JOURNAL_SUPPRESS_LOCATION.html: man/sd_journal_print.html
$(html-alias) $(html-alias)
man/SD_JOURNAL_SYSTEM.html: man/sd_journal_open.html
$(html-alias)
man/SD_JOURNAL_SYSTEM_ONLY.html: man/sd_journal_open.html man/SD_JOURNAL_SYSTEM_ONLY.html: man/sd_journal_open.html
$(html-alias) $(html-alias)

View File

@ -49,7 +49,8 @@
<refname>sd_journal</refname> <refname>sd_journal</refname>
<refname>SD_JOURNAL_LOCAL_ONLY</refname> <refname>SD_JOURNAL_LOCAL_ONLY</refname>
<refname>SD_JOURNAL_RUNTIME_ONLY</refname> <refname>SD_JOURNAL_RUNTIME_ONLY</refname>
<refname>SD_JOURNAL_SYSTEM_ONLY</refname> <refname>SD_JOURNAL_SYSTEM</refname>
<refname>SD_JOURNAL_CURRENT_USER</refname>
<refpurpose>Open the system journal for reading</refpurpose> <refpurpose>Open the system journal for reading</refpurpose>
</refnamediv> </refnamediv>
@ -93,10 +94,14 @@
be opened. <literal>SD_JOURNAL_RUNTIME_ONLY</literal> be opened. <literal>SD_JOURNAL_RUNTIME_ONLY</literal>
makes sure only volatile journal files will be opened, makes sure only volatile journal files will be opened,
excluding those which are stored on persistent excluding those which are stored on persistent
storage. <literal>SD_JOURNAL_SYSTEM_ONLY</literal> storage. <literal>SD_JOURNAL_SYSTEM</literal>
will ensure that only journal files of system services will cause journal files of system services and the
and the kernel (in opposition to user session processes) will kernel (in opposition to user session processes) to
be opened.</para> be opened. <literal>SD_JOURNAL_CURRENT_USER</literal>
will cause journal files of the current user to be
opened. If neither <literal>SD_JOURNAL_SYSTEM</literal>
nor <literal>SD_JOURNAL_CURRENT_USER</literal> are
specified, all journal file types will be opened.</para>
<para><function>sd_journal_open_directory()</function> <para><function>sd_journal_open_directory()</function>
is similar to <function>sd_journal_open()</function> is similar to <function>sd_journal_open()</function>
@ -170,6 +175,26 @@
file.</para> file.</para>
</refsect1> </refsect1>
<refsect1>
<title>History</title>
<para><function>sd_journal_open()</function>,
<function>sd_journal_close()</function>,
<literal>SD_JOURNAL_LOCAL_ONLY</literal>,
<literal>SD_JOURNAL_RUNTIME_ONLY</literal>,
<literal>SD_JOURNAL_SYSTEM_ONLY</literal> were added
in systemd-38.</para>
<para><function>sd_journal_open_directory()</function>
was added in systemd-187.</para>
<para><literal>SD_JOURNAL_SYSTEM</literal> and
<literal>SD_JOURNAL_CURRENT_USER</literal> were added
in systemd-205.
<literal>SD_JOURNAL_SYSTEM_ONLY</literal>
was deprecated.</para>
</refsect1>
<refsect1> <refsect1>
<title>See Also</title> <title>See Also</title>

View File

@ -109,7 +109,7 @@ static int open_journal(RequestMeta *m) {
if (m->journal) if (m->journal)
return 0; return 0;
return sd_journal_open(&m->journal, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM_ONLY); return sd_journal_open(&m->journal, SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM);
} }
static int respond_oom_internal(struct MHD_Connection *connection) { static int respond_oom_internal(struct MHD_Connection *connection) {

View File

@ -1249,6 +1249,42 @@ static void check_network(sd_journal *j, int fd) {
F_TYPE_CMP(sfs.f_type, SMB_SUPER_MAGIC); F_TYPE_CMP(sfs.f_type, SMB_SUPER_MAGIC);
} }
static bool file_has_type_prefix(const char *prefix, const char *filename) {
const char *full, *tilded, *atted;
full = strappend(prefix, ".journal");
tilded = strappenda(full, "~");
atted = strappenda(prefix, "@");
return streq(filename, full) ||
streq(filename, tilded) ||
startswith(filename, atted);
}
static bool file_type_wanted(int flags, const char *filename) {
if (!endswith(filename, ".journal") && !endswith(filename, ".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) {
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
assert_se(snprintf(prefix, sizeof(prefix), "user-%lu", (unsigned long) getuid())
< (int) sizeof(prefix));
if (file_has_type_prefix(prefix, filename))
return true;
}
return false;
}
static int add_file(sd_journal *j, const char *prefix, const char *filename) { static int add_file(sd_journal *j, const char *prefix, const char *filename) {
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
int r; int r;
@ -1258,11 +1294,7 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
assert(prefix); assert(prefix);
assert(filename); assert(filename);
if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) && if (!file_type_wanted(j->flags, filename))
!(streq(filename, "system.journal") ||
streq(filename, "system.journal~") ||
(startswith(filename, "system@") &&
(endswith(filename, ".journal") || endswith(filename, ".journal~")))))
return 0; return 0;
path = strjoin(prefix, "/", filename, NULL); path = strjoin(prefix, "/", filename, NULL);
@ -1619,7 +1651,8 @@ _public_ int sd_journal_open(sd_journal **ret, int flags) {
if (flags & ~(SD_JOURNAL_LOCAL_ONLY| if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
SD_JOURNAL_RUNTIME_ONLY| SD_JOURNAL_RUNTIME_ONLY|
SD_JOURNAL_SYSTEM_ONLY)) SD_JOURNAL_SYSTEM|
SD_JOURNAL_CURRENT_USER))
return -EINVAL; return -EINVAL;
j = journal_new(flags, NULL); j = journal_new(flags, NULL);

View File

@ -1097,6 +1097,7 @@ init_reader(void)
PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE) || PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE) ||
PyModule_AddIntConstant(m, "LOCAL_ONLY", SD_JOURNAL_LOCAL_ONLY) || PyModule_AddIntConstant(m, "LOCAL_ONLY", SD_JOURNAL_LOCAL_ONLY) ||
PyModule_AddIntConstant(m, "RUNTIME_ONLY", SD_JOURNAL_RUNTIME_ONLY) || PyModule_AddIntConstant(m, "RUNTIME_ONLY", SD_JOURNAL_RUNTIME_ONLY) ||
PyModule_AddIntConstant(m, "SYSTEM", SD_JOURNAL_SYSTEM) ||
PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY) || PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY) ||
PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) { PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
#if PY_MAJOR_VERSION >= 3 #if PY_MAJOR_VERSION >= 3

View File

@ -1010,7 +1010,7 @@ int show_journal_by_unit(
_cleanup_journal_close_ sd_journal*j = NULL; _cleanup_journal_close_ sd_journal*j = NULL;
int r; int r;
int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM_ONLY; int jflags = SD_JOURNAL_LOCAL_ONLY | system * SD_JOURNAL_SYSTEM;
assert(mode >= 0); assert(mode >= 0);
assert(mode < _OUTPUT_MODE_MAX); assert(mode < _OUTPUT_MODE_MAX);

View File

@ -86,7 +86,9 @@ typedef struct sd_journal sd_journal;
enum { enum {
SD_JOURNAL_LOCAL_ONLY = 1, SD_JOURNAL_LOCAL_ONLY = 1,
SD_JOURNAL_RUNTIME_ONLY = 2, SD_JOURNAL_RUNTIME_ONLY = 2,
SD_JOURNAL_SYSTEM_ONLY = 4 SD_JOURNAL_SYSTEM = 4,
SD_JOURNAL_SYSTEM_ONLY = SD_JOURNAL_SYSTEM, /* deprecated */
SD_JOURNAL_CURRENT_USER = 8,
}; };
/* Wakeup event types */ /* Wakeup event types */