SD_JOURNAL_OPEN(3)
π NAME π
okay so bestie, this whole page is about the squad of functions that open the system journal for reading β and uh, that's kind of a big deal π
The full roster (yes, all of them, no skipping π ) is:
sd_journal_opensd_journal_open_directorysd_journal_open_directory_fdsd_journal_open_filessd_journal_open_files_fdsd_journal_open_namespacesd_journal_closesd_journalSD_JOURNAL_LOCAL_ONLYSD_JOURNAL_RUNTIME_ONLYSD_JOURNAL_SYSTEMSD_JOURNAL_CURRENT_USERSD_JOURNAL_OS_ROOTSD_JOURNAL_ALL_NAMESPACESSD_JOURNAL_INCLUDE_DEFAULT_NAMESPACESD_JOURNAL_TAKE_DIRECTORY_FD
and what do they all do? they open the system journal for reading. iconic. β¨
π¦ SYNOPSIS π¦
like, here's the exact shape of everything, fr fr:
#include <systemd/sd-journal.h>
int sd_journal_open(sd_journal **ret, int flags);
int sd_journal_open_namespace(sd_journal **ret, const char *namespace,
int flags);
int sd_journal_open_directory(sd_journal **ret, const char *path,
int flags);
int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags);
int sd_journal_open_files(sd_journal **ret, const char **paths,
int flags);
int sd_journal_open_files_fd(sd_journal **ret, int fds[],
unsigned n_fds, int flags);
void sd_journal_close(sd_journal *j);
πΈ DESCRIPTION πΈ
sd_journal_open() β the main character π
sd_journal_open() opens the log journal for reading. it will find all journal files automatically and interleave them automatically when reading. so you don't gotta do the legwork, it just does it π
first argument is a pointer to a sd_journal pointer, which on success will contain a journal context object (that's your new bestie). second argument is a flags field, which may consist of the following flags ORed together:
SD_JOURNAL_LOCAL_ONLYβ makes sure only journal files generated on the local machine get opened πSD_JOURNAL_RUNTIME_ONLYβ makes sure only volatile journal files get opened, excluding the ones stored on persistent storage π«§SD_JOURNAL_SYSTEMβ causes journal files of system services and the kernel to be opened (as opposed to user session processes) π₯οΈSD_JOURNAL_CURRENT_USERβ causes journal files of the current user to be opened π
and if neither SD_JOURNAL_SYSTEM nor SD_JOURNAL_CURRENT_USER are specified, all journal file types will be opened. everything. the whole buffet π
sd_journal_open_namespace() β same vibe, but with a namespace π§
sd_journal_open_namespace() is similar to sd_journal_open() but takes an additional namespace parameter that specifies which journal namespace to operate on.
- if specified as
NULL, the call is identical tosd_journal_open()πͺ - if non-
NULL, only data from the namespace identified by the specified parameter is accessed
this call understands two additional flags:
SD_JOURNAL_ALL_NAMESPACESβ thenamespaceparameter is ignored and all defined namespaces are accessed simultaneously π€―SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACEβ the specified namespace and the default namespace are accessed, but no others. heads up: this flag has no effect whennamespaceis passed asNULLβ οΈ
for details about journal namespaces, go peek at systemd-journald.service(8) π
sd_journal_open_directory() β pick a directory, any directory π
sd_journal_open_directory() is similar to sd_journal_open() but takes an absolute directory path as argument. all journal files in this directory will be opened and interleaved automatically. slay.
this call also takes a flags argument, and the flags parameters accepted by this call are:
SD_JOURNAL_OS_ROOTSD_JOURNAL_SYSTEMSD_JOURNAL_CURRENT_USER
if SD_JOURNAL_OS_ROOT is specified, journal files are searched for below the usual /var/log/journal and /run/log/journal relative to the specified path, instead of directly beneath it. the other two flags limit which files are opened, the same as for sd_journal_open() π
sd_journal_open_directory_fd() β same but with a file descriptor π₯
sd_journal_open_directory_fd() is similar to sd_journal_open_directory(), but takes a file descriptor referencing a directory in the file system instead of an absolute file system path.
in addition to the flags accepted by sd_journal_open_directory(), this function also accepts SD_JOURNAL_TAKE_DIRECTORY_FD. here's the tea:
- if
SD_JOURNAL_TAKE_DIRECTORY_FDis specified, the function will take ownership of the specified file descriptor on success, and it will be closed bysd_journal_close()β so like, the caller of the function must not close the file descriptor π - when the flag is not specified,
sd_journal_close()will not close the file descriptor, so the caller should close it aftersd_journal_close()π
sd_journal_open_files() β you hand it the paths π
sd_journal_open_files() is similar to sd_journal_open() but takes a NULL-terminated list of file paths to open. all files will be opened and interleaved automatically.
this call also takes a flags argument, but it must be passed as 0 as no flags are currently understood for this call. zero. nothing. that's the whole vibe π«Ά
β οΈ please note: in the case of a live journal, this function is only useful for debugging, because individual journal files can be rotated at any moment, and the opening of specific files is inherently racy. like, genuinely racy. be careful bestie π
sd_journal_open_files_fd() β you hand it the fds π
sd_journal_open_files_fd() is similar to sd_journal_open_files() but takes an array of open file descriptors that must reference journal files, instead of an array of file system paths.
- pass the array of file descriptors as second argument
- and the number of array entries in the third
the flags parameter must be passed as 0. no cap. π
fork() warning β read this one fr fr π¨
sd_journal objects cannot be used in the child after a fork. functions which take a journal object as an argument (sd_journal_next() and others) will return -ECHILD after a fork. so likeβ¦ don't do that π
sd_journal_close() β the goodbye π₯Ί
sd_journal_close() will close the journal context allocated with sd_journal_open() or sd_journal_open_directory() and free its resources. clean up after yourself, king π
permissions, silently β¨
when opening the journal, only journal files accessible to the calling user will be opened. if journal files are not accessible to the caller, this will be silently ignored. no error, no drama, just vibes π€«
how to actually iterate π
see sd_journal_next(3) for an example of how to iterate through the journal after opening it with sd_journal_open().
the current entry + cursor lore π
a journal context object returned by sd_journal_open() references a specific journal entry as current entry, similar to a file seek index in a classic file system file β but without absolute positions. it may be altered with sd_journal_next(3) and sd_journal_seek_head(3) and related calls.
the current entry position may be exported in cursor strings, as accessible via sd_journal_get_cursor(3). cursor strings may be used to:
- globally identify a specific journal entry in a stable way π―
- and then later seek to it via
sd_journal_seek_cursor(3)β or, if the specific entry is not available locally, to its closest entry in time β³
notification of journal changes is available via sd_journal_get_fd() and related calls. yup, you can get notified π
β RETURN VALUE β
the sd_journal_open(), sd_journal_open_directory(), and sd_journal_open_files() calls return 0 on success or a negative errno-style error code. sd_journal_close() returns nothing. that's it, that's the whole thing π
π NOTES π
all functions listed here are thread-agnostic and only a single thread may operate on a given object at any given time. different threads may access the same object at different times. multiple independent objects may be used from different threads in parallel. so like, share responsibly π«Ά
functions described here are available as a shared library, which can be compiled against and linked to with the libsystemd pkg-config(1) file π
π HISTORY π
the timeline, bestie π°οΈ:
sd_journal_open(),sd_journal_open_directory(), andsd_journal_close()were added in version 187sd_journal_open_files()was added in version 205sd_journal_open_directory_fd()andsd_journal_open_files_fd()were added in version 230sd_journal_open_namespace()was added in version 245
glow-up after glow-up, honestly β¨
π§ SEE ALSO π§
go read these next, iyklyk π:
and that's the whole page, bestie π this lil' beauty ships as part of systemd 261.3 β go open some journals, you absolute icon π πβ¨
π¦ where this page comes from
- ubuntu stonking: libsystemd-dev 261.3-0ubuntu3 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms