0

I was looking into the code of systemd-journald and found this (in system_journal_open()):

if (!s->system_journal &&
    IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
    (flush_requested || flushed_flag_is_set())) {

        /* If in auto mode: first try to create the machine
         * path, but not the prefix.
         *
         * If in persistent mode: create /var/log/journal and
         * the machine path */

        if (s->storage == STORAGE_PERSISTENT)
                (void) mkdir_p("/var/log/journal/", 0755);

        (void) mkdir(s->system_storage.path, 0755);

        fn = strjoina(s->system_storage.path, "/system.journal");

Here, system_storage.path is set to strjoin("/var/log/journal/", SERVER_MACHINE_ID(s)); in previous function call.

As far as I understood its saying to create /var/log/journal directory when storage is set to persistent. But in either of the cases (persistent or auto) why is it creating /var/log/journal/machine_id directory ((void) mkdir(s->system_storage.path, 0755);)? auto will store logs persistently if /var/log/journal is created beforehand or else logs will be written in /run/log/journal. For auto it should not create /var/log/journal/machine_id directory right?

Also after reading the comment, how is it possible to create /var/log/journal/machine_id without creating the prefix? I am assuming /var/log/journal is the prefix.

2
  • I suspect system_storage.path is also set depending on persistence. systemd will not create /var/log/journal if Storage=volatile, Storage=none, or Storage=auto in /etc/systemd/journald.conf. This is the documented behavior in man journald.conf, and the observed behaviour when I test it. If this is not the behaviour you expect when reading sources, then there is probably something subtle in the source code you missed (that's very easy to do). Look at documented behaviour to figure out what programs do, not source code. If it's not documented, it could be a bug or change.
    – Stewart
    Sep 1, 2021 at 9:18
  • Hi Stewart, I have figured it out, it was just a confusion, so if set to 'persistent' the code will create '/var/log/journal' prefix and then machine_id dir, if set to 'auto' then '/var/log/journal' (prefix) is not created hence attempt to create complete path via (void) mkdir(s->system_storage.path, 0755); will simply fail because 'journal' directory is missing (assuming that most systems already have /var/log/ ) and mkdir command will fail if any of the directories is missing in the complete path (unlike mkdir -p which creates missing dirs) Sep 7, 2021 at 5:01

1 Answer 1

0

I have figured it out, it was just a confusion, so if set to persistent the code will create /var/log/journal prefix and then machine_id dir, if set to auto then /var/log/journal (prefix) is not created hence attempt to create complete path via (void) mkdir(s->system_storage.path, 0755); will simply fail because journal directory is missing (assuming that most systems already have /var/log/) and mkdir command will fail if any of the directories is missing in the complete path (unlike mkdir -p which creates missing dirs)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .