diff options
author | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-03-20 13:26:57 +0100 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-03-22 13:20:08 +0100 |
commit | a3b00f91bb985fa10bc33db5c7883e33dbdf83d0 (patch) | |
tree | 6b55bc4b285cfd4c927d6cd11b3d54bc7214ff0c /src/basic/log.c | |
parent | 8d0747abb732a5038a1cead30f32d6917f3f702a (diff) | |
download | systemd-a3b00f91bb985fa10bc33db5c7883e33dbdf83d0.tar.gz |
core: Settle log target if we're going to be closing all fds
Whenever we're going to close all file descriptors, we tend to close
the log and set it into open when needed mode. When this is done with
the logging target set to LOG_TARGET_AUTO, we run into issues because
for every logging call, we'll check if stderr is connected to the
journal to determine where to send the logging message. This check
obviously stops working when we close stderr, so we settle the log
target before we do that so that we keep using the same logging
target even after stderr is closed.
Diffstat (limited to 'src/basic/log.c')
-rw-r--r-- | src/basic/log.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/basic/log.c b/src/basic/log.c index 8b973f9e0a..21c6f2a685 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1264,6 +1264,24 @@ LogTarget log_get_target(void) { return log_target; } +void log_settle_target(void) { + + /* If we're using LOG_TARGET_AUTO and opening the log again on every single log call, we'll check if + * stderr is attached to the journal every single log call. However, if we then close all file + * descriptors later, that will stop working because stderr will be closed as well. To avoid that + * problem, this function is used to permanently change the log target depending on whether stderr is + * connected to the journal or not. */ + + LogTarget t = log_get_target(); + + if (t != LOG_TARGET_AUTO) + return; + + t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG) + : LOG_TARGET_CONSOLE; + log_set_target(t); +} + int log_get_max_level(void) { return log_max_level; } |