libsyslog: avoid opening+connecting to syslogd when LOG_NLOG

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg 2022-08-01 11:16:55 +02:00
parent 4c39bfeccb
commit 221ae0abe0

View File

@ -221,7 +221,7 @@ vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
char dbuf[30]; char dbuf[30];
struct iovec iov[8]; /* date/time + prog + [ + pid + ]: + fmt + crlf */ struct iovec iov[8]; /* date/time + prog + [ + pid + ]: + fmt + crlf */
int iovcnt = 0; int iovcnt = 0;
int opened; int opened = 0;
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
/* Check for invalid bits. */ /* Check for invalid bits. */
@ -484,14 +484,6 @@ output:
(void)writev(STDERR_FILENO, iov, iovcnt + 1); (void)writev(STDERR_FILENO, iov, iovcnt + 1);
} }
/* Get connected, output the message to the local logger. */
if (data == &sdata)
mutex_lock(&syslog_mutex);
opened = !data->log_opened;
if (opened)
openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
connectlog_r(data);
/* Don't write to system log, instead use fd in log_file */ /* Don't write to system log, instead use fd in log_file */
if (data->log_stat & LOG_NLOG) { if (data->log_stat & LOG_NLOG) {
iov[iovcnt].iov_base = __UNCONST(CRLF + 1); iov[iovcnt].iov_base = __UNCONST(CRLF + 1);
@ -500,6 +492,14 @@ output:
goto done; goto done;
} }
/* Get connected, output the message to the local logger. */
if (data == &sdata)
mutex_lock(&syslog_mutex);
opened = !data->log_opened;
if (opened)
openlog_unlocked_r(data->log_tag, data->log_stat, 0, data);
connectlog_r(data);
/* Log to stdout, usually for debugging syslogp() API */ /* Log to stdout, usually for debugging syslogp() API */
if (data->log_stat & LOG_STDOUT) { if (data->log_stat & LOG_STDOUT) {
strlcat(tbuf, "\n", sizeof(tbuf)); strlcat(tbuf, "\n", sizeof(tbuf));