1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "sd-messages.h"
9 #include "alloc-util.h"
11 #include "format-util.h"
13 #include "journald-console.h"
14 #include "journald-kmsg.h"
15 #include "journald-server.h"
16 #include "journald-syslog.h"
17 #include "journald-wall.h"
18 #include "process-util.h"
19 #include "selinux-util.h"
20 #include "socket-util.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
23 #include "syslog-util.h"
25 /* Warn once every 30s if we missed syslog message */
26 #define WARN_FORWARD_SYSLOG_MISSED_USEC (30 * USEC_PER_SEC)
28 static void forward_syslog_iovec(Server
*s
, const struct iovec
*iovec
, unsigned n_iovec
, const struct ucred
*ucred
, const struct timeval
*tv
) {
30 static const union sockaddr_union sa
= {
31 .un
.sun_family
= AF_UNIX
,
32 .un
.sun_path
= "/run/systemd/journal/syslog",
34 struct msghdr msghdr
= {
35 .msg_iov
= (struct iovec
*) iovec
,
36 .msg_iovlen
= n_iovec
,
37 .msg_name
= (struct sockaddr
*) &sa
.sa
,
38 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
42 struct cmsghdr cmsghdr
;
43 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
52 msghdr
.msg_control
= &control
;
53 msghdr
.msg_controllen
= sizeof(control
);
55 cmsg
= CMSG_FIRSTHDR(&msghdr
);
56 cmsg
->cmsg_level
= SOL_SOCKET
;
57 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
58 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
59 memcpy(CMSG_DATA(cmsg
), ucred
, sizeof(struct ucred
));
60 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
63 /* Forward the syslog message we received via /dev/log to
64 * /run/systemd/syslog. Unfortunately we currently can't set
65 * the SO_TIMESTAMP auxiliary data, and hence we don't. */
67 if (sendmsg(s
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) >= 0)
70 /* The socket is full? I guess the syslog implementation is
71 * too slow, and we shouldn't wait for that... */
72 if (errno
== EAGAIN
) {
73 s
->n_forward_syslog_missed
++;
77 if (ucred
&& IN_SET(errno
, ESRCH
, EPERM
)) {
80 /* Hmm, presumably the sender process vanished
81 * by now, or we don't have CAP_SYS_AMDIN, so
82 * let's fix it as good as we can, and retry */
85 u
.pid
= getpid_cached();
86 memcpy(CMSG_DATA(cmsg
), &u
, sizeof(struct ucred
));
88 if (sendmsg(s
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) >= 0)
91 if (errno
== EAGAIN
) {
92 s
->n_forward_syslog_missed
++;
98 log_debug_errno(errno
, "Failed to forward syslog message: %m");
101 static void forward_syslog_raw(Server
*s
, int priority
, const char *buffer
, size_t buffer_len
, const struct ucred
*ucred
, const struct timeval
*tv
) {
107 if (LOG_PRI(priority
) > s
->max_level_syslog
)
110 iovec
= IOVEC_MAKE((char *) buffer
, buffer_len
);
111 forward_syslog_iovec(s
, &iovec
, 1, ucred
, tv
);
114 void server_forward_syslog(Server
*s
, int priority
, const char *identifier
, const char *message
, const struct ucred
*ucred
, const struct timeval
*tv
) {
115 struct iovec iovec
[5];
116 char header_priority
[DECIMAL_STR_MAX(priority
) + 3], header_time
[64],
117 header_pid
[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t
) + 1];
121 _cleanup_free_
char *ident_buf
= NULL
;
124 assert(priority
>= 0);
125 assert(priority
<= 999);
128 if (LOG_PRI(priority
) > s
->max_level_syslog
)
131 /* First: priority field */
132 xsprintf(header_priority
, "<%i>", priority
);
133 iovec
[n
++] = IOVEC_MAKE_STRING(header_priority
);
135 /* Second: timestamp */
136 t
= tv
? tv
->tv_sec
: ((time_t) (now(CLOCK_REALTIME
) / USEC_PER_SEC
));
137 if (!localtime_r(&t
, &tm
))
139 if (strftime(header_time
, sizeof(header_time
), "%h %e %T ", &tm
) <= 0)
141 iovec
[n
++] = IOVEC_MAKE_STRING(header_time
);
143 /* Third: identifier and PID */
146 get_process_comm(ucred
->pid
, &ident_buf
);
147 identifier
= ident_buf
;
150 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
153 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
155 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
156 } else if (identifier
) {
157 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
158 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
161 /* Fourth: message */
162 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
164 forward_syslog_iovec(s
, iovec
, n
, ucred
, tv
);
167 int syslog_fixup_facility(int priority
) {
169 if ((priority
& LOG_FACMASK
) == 0)
170 return (priority
& LOG_PRIMASK
) | LOG_USER
;
175 size_t syslog_parse_identifier(const char **buf
, char **identifier
, char **pid
) {
186 p
+= strspn(p
, WHITESPACE
);
187 l
= strcspn(p
, WHITESPACE
);
196 if (l
> 0 && p
[l
-1] == ']') {
202 t
= strndup(p
+k
+1, l
-k
-2);
221 /* Single space is used as separator */
222 if (p
[e
] != '\0' && strchr(WHITESPACE
, p
[e
]))
230 static int syslog_skip_timestamp(const char **buf
) {
238 LETTER
, LETTER
, LETTER
,
240 SPACE_OR_NUMBER
, NUMBER
,
242 SPACE_OR_NUMBER
, NUMBER
,
244 SPACE_OR_NUMBER
, NUMBER
,
246 SPACE_OR_NUMBER
, NUMBER
,
256 for (i
= 0, p
= *buf
; i
< ELEMENTSOF(sequence
); i
++, p
++) {
260 switch (sequence
[i
]) {
267 case SPACE_OR_NUMBER
:
273 if (*p
< '0' || *p
> '9')
279 if (!(*p
>= 'A' && *p
<= 'Z') &&
280 !(*p
>= 'a' && *p
<= 'z'))
298 void server_process_syslog_message(
302 const struct ucred
*ucred
,
303 const struct timeval
*tv
,
307 char *t
, syslog_priority
[sizeof("PRIORITY=") + DECIMAL_STR_MAX(int)],
308 syslog_facility
[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
309 const char *msg
, *syslog_ts
, *a
;
310 _cleanup_free_
char *identifier
= NULL
, *pid
= NULL
,
311 *dummy
= NULL
, *msg_msg
= NULL
, *msg_raw
= NULL
;
312 int priority
= LOG_USER
| LOG_INFO
, r
;
313 ClientContext
*context
= NULL
;
315 size_t n
= 0, m
, i
, leading_ws
, syslog_ts_len
;
320 /* The message cannot be empty. */
322 /* The buffer NUL-terminated and can be used a string. raw_len is the length
323 * without the terminating NUL byte, the buffer is actually one bigger. */
324 assert(buf
[raw_len
] == '\0');
326 if (ucred
&& pid_is_valid(ucred
->pid
)) {
327 r
= client_context_get(s
, ucred
->pid
, ucred
, label
, label_len
, NULL
, &context
);
329 log_warning_errno(r
, "Failed to retrieve credentials for PID " PID_FMT
", ignoring: %m", ucred
->pid
);
332 /* We are creating a copy of the message because we want to forward the original message
333 verbatim to the legacy syslog implementation */
334 for (i
= raw_len
; i
> 0; i
--)
335 if (!strchr(WHITESPACE
, buf
[i
-1]))
338 leading_ws
= strspn(buf
, WHITESPACE
);
341 /* The message contains only whitespaces */
343 else if (i
== raw_len
)
344 /* Nice! No need to strip anything on the end, let's optimize this a bit */
345 msg
= buf
+ leading_ws
;
347 msg
= dummy
= new(char, i
- leading_ws
+ 1);
353 memcpy(dummy
, buf
+ leading_ws
, i
- leading_ws
);
354 dummy
[i
- leading_ws
] = 0;
357 /* We will add the SYSLOG_RAW= field when we stripped anything
358 * _or_ if the input message contained NUL bytes. */
359 store_raw
= msg
!= buf
|| strlen(msg
) != raw_len
;
361 syslog_parse_priority(&msg
, &priority
, true);
363 if (!client_context_test_priority(context
, priority
))
367 syslog_ts_len
= syslog_skip_timestamp(&msg
);
368 if (syslog_ts_len
== 0)
369 /* We failed to parse the full timestamp, store the raw message too */
372 syslog_parse_identifier(&msg
, &identifier
, &pid
);
374 if (s
->forward_to_syslog
)
375 forward_syslog_raw(s
, priority
, buf
, raw_len
, ucred
, tv
);
377 if (s
->forward_to_kmsg
)
378 server_forward_kmsg(s
, priority
, identifier
, msg
, ucred
);
380 if (s
->forward_to_console
)
381 server_forward_console(s
, priority
, identifier
, msg
, ucred
);
383 if (s
->forward_to_wall
)
384 server_forward_wall(s
, priority
, identifier
, msg
, ucred
);
386 m
= N_IOVEC_META_FIELDS
+ 8 + client_context_extra_fields_n_iovec(context
);
387 iovec
= newa(struct iovec
, m
);
389 iovec
[n
++] = IOVEC_MAKE_STRING("_TRANSPORT=syslog");
391 xsprintf(syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
392 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_priority
);
394 if (priority
& LOG_FACMASK
) {
395 xsprintf(syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
));
396 iovec
[n
++] = IOVEC_MAKE_STRING(syslog_facility
);
400 a
= strjoina("SYSLOG_IDENTIFIER=", identifier
);
401 iovec
[n
++] = IOVEC_MAKE_STRING(a
);
405 a
= strjoina("SYSLOG_PID=", pid
);
406 iovec
[n
++] = IOVEC_MAKE_STRING(a
);
409 if (syslog_ts_len
> 0) {
410 const size_t hlen
= strlen("SYSLOG_TIMESTAMP=");
412 t
= newa(char, hlen
+ syslog_ts_len
);
413 memcpy(t
, "SYSLOG_TIMESTAMP=", hlen
);
414 memcpy(t
+ hlen
, syslog_ts
, syslog_ts_len
);
416 iovec
[n
++] = IOVEC_MAKE(t
, hlen
+ syslog_ts_len
);
419 msg_msg
= strjoin("MESSAGE=", msg
);
424 iovec
[n
++] = IOVEC_MAKE_STRING(msg_msg
);
427 const size_t hlen
= strlen("SYSLOG_RAW=");
429 msg_raw
= new(char, hlen
+ raw_len
);
435 memcpy(msg_raw
, "SYSLOG_RAW=", hlen
);
436 memcpy(msg_raw
+ hlen
, buf
, raw_len
);
438 iovec
[n
++] = IOVEC_MAKE(msg_raw
, hlen
+ raw_len
);
441 server_dispatch_message(s
, iovec
, n
, m
, context
, tv
, priority
, 0);
444 int server_open_syslog_socket(Server
*s
) {
446 static const union sockaddr_union sa
= {
447 .un
.sun_family
= AF_UNIX
,
448 .un
.sun_path
= "/run/systemd/journal/dev-log",
454 if (s
->syslog_fd
< 0) {
455 s
->syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
456 if (s
->syslog_fd
< 0)
457 return log_error_errno(errno
, "socket() failed: %m");
459 (void) sockaddr_un_unlink(&sa
.un
);
461 r
= bind(s
->syslog_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
463 return log_error_errno(errno
, "bind(%s) failed: %m", sa
.un
.sun_path
);
465 (void) chmod(sa
.un
.sun_path
, 0666);
467 (void) fd_nonblock(s
->syslog_fd
, true);
469 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_PASSCRED
, &const_int_one
, sizeof(const_int_one
));
471 return log_error_errno(errno
, "SO_PASSCRED failed: %m");
474 if (mac_selinux_use()) {
475 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_PASSSEC
, &const_int_one
, sizeof(const_int_one
));
477 log_warning_errno(errno
, "SO_PASSSEC failed: %m");
481 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &const_int_one
, sizeof(const_int_one
));
483 return log_error_errno(errno
, "SO_TIMESTAMP failed: %m");
485 r
= sd_event_add_io(s
->event
, &s
->syslog_event_source
, s
->syslog_fd
, EPOLLIN
, server_process_datagram
, s
);
487 return log_error_errno(r
, "Failed to add syslog server fd to event loop: %m");
489 r
= sd_event_source_set_priority(s
->syslog_event_source
, SD_EVENT_PRIORITY_NORMAL
+5);
491 return log_error_errno(r
, "Failed to adjust syslog event source priority: %m");
496 void server_maybe_warn_forward_syslog_missed(Server
*s
) {
501 if (s
->n_forward_syslog_missed
<= 0)
504 n
= now(CLOCK_MONOTONIC
);
505 if (s
->last_warn_forward_syslog_missed
+ WARN_FORWARD_SYSLOG_MISSED_USEC
> n
)
508 server_driver_message(s
, 0,
509 "MESSAGE_ID=" SD_MESSAGE_FORWARD_SYSLOG_MISSED_STR
,
510 LOG_MESSAGE("Forwarding to syslog missed %u messages.",
511 s
->n_forward_syslog_missed
),
514 s
->n_forward_syslog_missed
= 0;
515 s
->last_warn_forward_syslog_missed
= n
;