Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Michael Chapman
On Sun, 29 Aug 2021, Nishant Nayan wrote:
> Awesome, thanks!
> 
> Also, where can I find the code section where services And kernel logs to
> journald?
> After tracing from 'main' at journald.c I came across the part where
> various sockets are opened and validated (/run/systemd/journal/stdout,
> /dev/kmsg, /dev/log ) for journald to listen to logs of systemd services
> and kernel. That is the server side part.
> 
> Where can I find the client side journald code where services and kernel
> sends their logs to journal.

If programs are using sd-journal, they are using the code 
from src/libsystemd/sd-journal/.

Programs can also implement the journal protocol themselves, using the 
documentation at:

https://systemd.io/JOURNAL_NATIVE_PROTOCOL/

The kernel doesn't do any of this, of course. journald *reads* from the 
kernel ring buffer.


Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Nishant Nayan
Awesome, thanks!

Also, where can I find the code section where services And kernel logs to
journald?
After tracing from 'main' at journald.c I came across the part where
various sockets are opened and validated (/run/systemd/journal/stdout,
/dev/kmsg, /dev/log ) for journald to listen to logs of systemd services
and kernel. That is the server side part.

Where can I find the client side journald code where services and kernel
sends their logs to journal.

Nishant

On Sun, 29 Aug 2021, 18:39 Michael Chapman,  wrote:

> On Sun, 29 Aug 2021, Nishant Nayan wrote:
> > Also I was wondering where in the code does journald.config file changes
> > are parsed?
> > For example in the above code , the line :-
> >   if (s->storage == STORAGE_PERSISTENT)
> > Here, s->storage corresponds to 'Storage' option of conf file right?
> > How is it getting set when we edit the conf file?
>
> The configuration files are loaded in server_parse_config_file(). The
> actual code that maps keys in the files to variables in the program is
> generated by gperf [1] from the journald-gperf.gperf source file.
>
> [1] https://www.gnu.org/software/gperf/manual/html_node/index.html
>
> > Also, on doing "ls /run/log/journal/machine_id/"
> > I can see output as following
> > .journal
> > .journal
> > .
> > .
> > .
> > system.journal
> >
> > Is 'system.journal' is the currently active journal and rest are archived
> > journals?
>
> That's correct.
>


Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Michael Chapman
On Sun, 29 Aug 2021, Nishant Nayan wrote:
> Also I was wondering where in the code does journald.config file changes
> are parsed?
> For example in the above code , the line :-
>   if (s->storage == STORAGE_PERSISTENT)
> Here, s->storage corresponds to 'Storage' option of conf file right?
> How is it getting set when we edit the conf file?

The configuration files are loaded in server_parse_config_file(). The 
actual code that maps keys in the files to variables in the program is 
generated by gperf [1] from the journald-gperf.gperf source file.

[1] https://www.gnu.org/software/gperf/manual/html_node/index.html

> Also, on doing "ls /run/log/journal/machine_id/"
> I can see output as following
> .journal
> .journal
> .
> .
> .
> system.journal
> 
> Is 'system.journal' is the currently active journal and rest are archived
> journals?

That's correct.


Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Nishant Nayan
Also I was wondering where in the code does journald.config file changes
are parsed?
For example in the above code , the line :-
  if (s->storage == STORAGE_PERSISTENT)
Here, s->storage corresponds to 'Storage' option of conf file right?
How is it getting set when we edit the conf file?

Also, on doing "ls /run/log/journal/machine_id/"
I can see output as following
.journal
.journal
.
.
.
system.journal

Is 'system.journal' is the currently active journal and rest are archived
journals?


Nishant

On Sun, 29 Aug 2021 at 17:56, Michael Chapman 
wrote:

> On Sun, 29 Aug 2021, Nishant Nayan wrote:
> > 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");
> [...]
> >
> > 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 .
>
> Sorry, I actually misread your question here.
>
> In Storage=auto mode, no attempt to create the /var/log/journal directory
> is made. If it doesn't exist, then the persistent journal simply fails to
> be opened.
>


Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Michael Chapman
On Sun, 29 Aug 2021, Nishant Nayan wrote:
> 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");
[...]
> 
> 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 .

Sorry, I actually misread your question here.

In Storage=auto mode, no attempt to create the /var/log/journal directory 
is made. If it doesn't exist, then the persistent journal simply fails to 
be opened.


Re: [systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Michael Chapman
On Sun, 29 Aug 2021, Nishant Nayan wrote:
> 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?

All of this is guarded with:

flush_requested || flushed_flag_is_set()

In other words, this code path is used if a request to flush the journal 
data from /run into /var had been received, or the runtime journal had 
already been flushed in the past.

> 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 .

mkdir_p() creates parent directories, like `mkdir -p` in your shell.


[systemd-devel] Why systemd-journald is creating '/var/log/journal/machine_id" directory when Storage=auto

2021-08-29 Thread Nishant Nayan
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 .