Skip to content

Commit 5302ebe

Browse files
committed
journal: add sd_journal_open_files
This allows the caller to explicitly specify which journal files should be opened. The same functionality could be achieved before by creating a directory and playing around with symlinks. It is useful to debug stuff and explore the journal, and has been requested before. Waiting is supported, the journal will notice modifications on the files supplied when opening the journal, but will not add any new files.
1 parent 6eb7a9a commit 5302ebe

File tree

6 files changed

+130
-22
lines changed

6 files changed

+130
-22
lines changed

Makefile-man.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ MANPAGES_ALIAS += \
148148
man/sd_journal_get_timeout.3 \
149149
man/sd_journal_next_skip.3 \
150150
man/sd_journal_open_directory.3 \
151+
man/sd_journal_open_files.3 \
151152
man/sd_journal_perror.3 \
152153
man/sd_journal_previous.3 \
153154
man/sd_journal_previous_skip.3 \
@@ -248,6 +249,7 @@ man/sd_journal_get_monotonic_usec.3: man/sd_journal_get_realtime_usec.3
248249
man/sd_journal_get_timeout.3: man/sd_journal_get_fd.3
249250
man/sd_journal_next_skip.3: man/sd_journal_next.3
250251
man/sd_journal_open_directory.3: man/sd_journal_open.3
252+
man/sd_journal_open_files.3: man/sd_journal_open.3
251253
man/sd_journal_perror.3: man/sd_journal_print.3
252254
man/sd_journal_previous.3: man/sd_journal_next.3
253255
man/sd_journal_previous_skip.3: man/sd_journal_next.3
@@ -452,6 +454,9 @@ man/sd_journal_next_skip.html: man/sd_journal_next.html
452454
man/sd_journal_open_directory.html: man/sd_journal_open.html
453455
$(html-alias)
454456

457+
man/sd_journal_open_files.html: man/sd_journal_open.html
458+
$(html-alias)
459+
455460
man/sd_journal_perror.html: man/sd_journal_print.html
456461
$(html-alias)
457462

man/sd_journal_open.xml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<refnamediv>
4646
<refname>sd_journal_open</refname>
4747
<refname>sd_journal_open_directory</refname>
48+
<refname>sd_journal_open_files</refname>
4849
<refname>sd_journal_close</refname>
4950
<refname>sd_journal</refname>
5051
<refname>SD_JOURNAL_LOCAL_ONLY</refname>
@@ -71,6 +72,13 @@
7172
<paramdef>int <parameter>flags</parameter></paramdef>
7273
</funcprototype>
7374

75+
<funcprototype>
76+
<funcdef>int <function>sd_journal_open_files</function></funcdef>
77+
<paramdef>sd_journal** <parameter>ret</parameter></paramdef>
78+
<paramdef>const char** <parameter>paths</parameter></paramdef>
79+
<paramdef>int <parameter>flags</parameter></paramdef>
80+
</funcprototype>
81+
7482
<funcprototype>
7583
<funcdef>void <function>sd_journal_close</function></funcdef>
7684
<paramdef>sd_journal* <parameter>j</parameter></paramdef>
@@ -111,6 +119,14 @@
111119
flags argument, but it must be passed as 0 as no flags
112120
are currently understood for this call.</para>
113121

122+
<para><function>sd_journal_open_files()</function>
123+
is similar to <function>sd_journal_open()</function>
124+
but takes a <literal>NULL</literal>-terminated list
125+
of file paths to open. All files will be opened and
126+
interleaved automatically. This call also takes a
127+
flags argument, but it must be passed as 0 as no flags
128+
are currently understood for this call.</para>
129+
114130
<para><function>sd_journal_close()</function> will
115131
close the journal context allocated with
116132
<function>sd_journal_open()</function> or
@@ -188,9 +204,10 @@
188204
<para><function>sd_journal_open_directory()</function>
189205
was added in systemd-187.</para>
190206

191-
<para><literal>SD_JOURNAL_SYSTEM</literal> and
192-
<literal>SD_JOURNAL_CURRENT_USER</literal> were added
193-
in systemd-205.
207+
<para><literal>SD_JOURNAL_SYSTEM</literal>,
208+
<literal>SD_JOURNAL_CURRENT_USER</literal>,
209+
and <function>sd_journal_open_files()</function>
210+
were added in systemd-205.
194211
<literal>SD_JOURNAL_SYSTEM_ONLY</literal>
195212
was deprecated.</para>
196213
</refsect1>

src/journal/journal-internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct sd_journal {
123123
uint64_t unique_offset;
124124

125125
bool on_network;
126+
bool no_new_files;
126127

127128
size_t data_threshold;
128129

src/journal/libsystemd-journal.sym

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ LIBSYSTEMD_JOURNAL_202 {
104104
global:
105105
sd_journal_add_conjunction;
106106
} LIBSYSTEMD_JOURNAL_201;
107+
108+
LIBSYSTEMD_JOURNAL_205 {
109+
global:
110+
sd_journal_open_files;
111+
} LIBSYSTEMD_JOURNAL_202;

src/journal/sd-journal.c

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "journal-file.h"
3434
#include "hashmap.h"
3535
#include "list.h"
36+
#include "strv.h"
3637
#include "path-util.h"
3738
#include "lookup3.h"
3839
#include "compress.h"
@@ -1278,37 +1279,24 @@ static bool file_type_wanted(int flags, const char *filename) {
12781279
return false;
12791280
}
12801281

1281-
static int add_file(sd_journal *j, const char *prefix, const char *filename) {
1282-
_cleanup_free_ char *path = NULL;
1283-
int r;
1282+
static int add_any_file(sd_journal *j, const char *path) {
12841283
JournalFile *f;
1284+
int r;
12851285

12861286
assert(j);
1287-
assert(prefix);
1288-
assert(filename);
1289-
1290-
if (!file_type_wanted(j->flags, filename))
1291-
return 0;
1292-
1293-
path = strjoin(prefix, "/", filename, NULL);
1294-
if (!path)
1295-
return -ENOMEM;
1287+
assert(path);
12961288

12971289
if (hashmap_get(j->files, path))
12981290
return 0;
12991291

13001292
if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
1301-
log_debug("Too many open journal files, not adding %s, ignoring.", path);
1293+
log_warning("Too many open journal files, not adding %s.", path);
13021294
return set_put_error(j, -ETOOMANYREFS);
13031295
}
13041296

13051297
r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
1306-
if (r < 0) {
1307-
if (errno == ENOENT)
1308-
return 0;
1309-
1298+
if (r < 0)
13101299
return r;
1311-
}
13121300

13131301
/* journal_file_dump(f); */
13141302

@@ -1327,6 +1315,28 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
13271315
return 0;
13281316
}
13291317

1318+
static int add_file(sd_journal *j, const char *prefix, const char *filename) {
1319+
_cleanup_free_ char *path = NULL;
1320+
int r;
1321+
1322+
assert(j);
1323+
assert(prefix);
1324+
assert(filename);
1325+
1326+
if (j->no_new_files ||
1327+
!file_type_wanted(j->flags, filename))
1328+
return 0;
1329+
1330+
path = strjoin(prefix, "/", filename, NULL);
1331+
if (!path)
1332+
return -ENOMEM;
1333+
1334+
r = add_any_file(j, path);
1335+
if (r == -ENOENT)
1336+
return 0;
1337+
return 0;
1338+
}
1339+
13301340
static int remove_file(sd_journal *j, const char *prefix, const char *filename) {
13311341
char *path;
13321342
JournalFile *f;
@@ -1507,6 +1517,9 @@ static int add_root_directory(sd_journal *j, const char *p) {
15071517
inotify_rm_watch(j->inotify_fd, m->wd);
15081518
}
15091519

1520+
if (j->no_new_files)
1521+
return 0;
1522+
15101523
for (;;) {
15111524
struct dirent *de;
15121525
union dirent_storage buf;
@@ -1587,6 +1600,36 @@ static int add_search_paths(sd_journal *j) {
15871600
return 0;
15881601
}
15891602

1603+
static int add_current_paths(sd_journal *j) {
1604+
Iterator i;
1605+
JournalFile *f;
1606+
1607+
assert(j);
1608+
assert(j->no_new_files);
1609+
1610+
/* Simply adds all directories for files we have open as
1611+
* "root" directories. We don't expect errors here, so we
1612+
* treat them as fatal. */
1613+
1614+
HASHMAP_FOREACH(f, j->files, i) {
1615+
int r;
1616+
_cleanup_free_ char *dir;
1617+
1618+
dir = dirname_malloc(f->path);
1619+
if (!dir)
1620+
return -ENOMEM;
1621+
1622+
r = add_root_directory(j, dir);
1623+
if (r < 0) {
1624+
set_put_error(j, r);
1625+
return r;
1626+
}
1627+
}
1628+
1629+
return 0;
1630+
}
1631+
1632+
15901633
static int allocate_inotify(sd_journal *j) {
15911634
assert(j);
15921635

@@ -1697,6 +1740,40 @@ _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int f
16971740
return r;
16981741
}
16991742

1743+
_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
1744+
sd_journal *j;
1745+
const char **path;
1746+
int r;
1747+
1748+
if (!ret)
1749+
return -EINVAL;
1750+
1751+
if (flags != 0)
1752+
return -EINVAL;
1753+
1754+
j = journal_new(flags, NULL);
1755+
if (!j)
1756+
return -ENOMEM;
1757+
1758+
STRV_FOREACH(path, paths) {
1759+
r = add_any_file(j, *path);
1760+
if (r < 0) {
1761+
log_error("Failed to open %s: %s", *path, strerror(-r));
1762+
goto fail;
1763+
}
1764+
}
1765+
1766+
j->no_new_files = true;
1767+
1768+
*ret = j;
1769+
return 0;
1770+
1771+
fail:
1772+
sd_journal_close(j);
1773+
1774+
return r;
1775+
}
1776+
17001777
_public_ void sd_journal_close(sd_journal *j) {
17011778
Directory *d;
17021779
JournalFile *f;
@@ -2017,7 +2094,9 @@ _public_ int sd_journal_get_fd(sd_journal *j) {
20172094

20182095
/* Iterate through all dirs again, to add them to the
20192096
* inotify */
2020-
if (j->path)
2097+
if (j->no_new_files)
2098+
r = add_current_paths(j);
2099+
else if (j->path)
20212100
r = add_root_directory(j, j->path);
20222101
else
20232102
r = add_search_paths(j);

src/systemd/sd-journal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ enum {
100100

101101
int sd_journal_open(sd_journal **ret, int flags);
102102
int sd_journal_open_directory(sd_journal **ret, const char *path, int flags);
103+
int sd_journal_open_files(sd_journal **ret, const char **paths, int flags);
103104
void sd_journal_close(sd_journal *j);
104105

105106
int sd_journal_previous(sd_journal *j);

0 commit comments

Comments
 (0)