1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <sys/socket.h>
11 #define SD_JOURNAL_SUPPRESS_LOCATION
13 #include "sd-journal.h"
15 #include "alloc-util.h"
19 #include "memfd-util.h"
20 #include "socket-util.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
25 #define SNDBUF_SIZE (8*1024*1024)
27 #define ALLOCA_CODE_FUNC(f, func) \
30 const char *_func = (func); \
32 _fl = strlen(_func) + 1; \
33 *_f = alloca(_fl + 10); \
34 memcpy(*_f, "CODE_FUNC=", 10); \
35 memcpy(*_f + 10, _func, _fl); \
38 /* We open a single fd, and we'll share it with the current process,
39 * all its threads, and all its subprocesses. This means we need to
40 * initialize it atomically, and need to operate on it atomically
41 * never assuming we are the only user */
43 static int journal_fd(void) {
45 static int fd_plus_one
= 0;
49 return fd_plus_one
- 1;
51 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
55 fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
57 if (!__sync_bool_compare_and_swap(&fd_plus_one
, 0, fd
+1)) {
65 _public_
int sd_journal_print(int priority
, const char *format
, ...) {
70 r
= sd_journal_printv(priority
, format
, ap
);
76 _public_
int sd_journal_printv(int priority
, const char *format
, va_list ap
) {
78 /* FIXME: Instead of limiting things to LINE_MAX we could do a
79 C99 variable-length array on the stack here in a loop. */
81 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
84 assert_return(priority
>= 0, -EINVAL
);
85 assert_return(priority
<= 7, -EINVAL
);
86 assert_return(format
, -EINVAL
);
88 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
90 memcpy(buffer
, "MESSAGE=", 8);
91 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
93 /* Strip trailing whitespace, keep prefix whitespace. */
94 (void) strstrip(buffer
);
96 /* Suppress empty lines */
97 if (isempty(buffer
+8))
100 iov
[0] = IOVEC_MAKE_STRING(buffer
);
101 iov
[1] = IOVEC_MAKE_STRING(p
);
103 return sd_journal_sendv(iov
, 2);
106 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
108 int r
, n
= 0, i
= 0, j
;
109 struct iovec
*iov
= NULL
;
114 n
= MAX(extra
* 2, extra
+ 4);
115 iov
= malloc0(n
* sizeof(struct iovec
));
131 c
= realloc(iov
, n
* sizeof(struct iovec
));
141 if (vasprintf(&buffer
, format
, aq
) < 0) {
148 VA_FORMAT_ADVANCE(format
, ap
);
150 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
152 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
154 format
= va_arg(ap
, char *);
162 for (j
= 0; j
< i
; j
++)
163 free(iov
[j
].iov_base
);
170 _public_
int sd_journal_send(const char *format
, ...) {
173 struct iovec
*iov
= NULL
;
175 va_start(ap
, format
);
176 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
179 if (_unlikely_(i
< 0)) {
184 r
= sd_journal_sendv(iov
, i
);
187 for (j
= 0; j
< i
; j
++)
188 free(iov
[j
].iov_base
);
195 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
198 _cleanup_close_
int buffer_fd
= -1;
202 static const union sockaddr_union sa
= {
203 .un
.sun_family
= AF_UNIX
,
204 .un
.sun_path
= "/run/systemd/journal/socket",
207 .msg_name
= (struct sockaddr
*) &sa
.sa
,
208 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
211 bool have_syslog_identifier
= false;
214 assert_return(iov
, -EINVAL
);
215 assert_return(n
> 0, -EINVAL
);
217 w
= newa(struct iovec
, n
* 5 + 3);
218 l
= newa(uint64_t, n
);
220 for (i
= 0; i
< n
; i
++) {
223 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
226 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
227 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
230 have_syslog_identifier
= have_syslog_identifier
||
231 (c
== (char *) iov
[i
].iov_base
+ 17 &&
232 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
234 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
236 if (_unlikely_(nl
< c
))
239 /* Already includes a newline? Bummer, then
240 * let's write the variable name, then a
241 * newline, then the size (64bit LE), followed
242 * by the data and a final newline */
244 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
245 w
[j
++] = IOVEC_MAKE_STRING("\n");
247 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
248 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
250 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
252 /* Nothing special? Then just add the line and
253 * append a newline */
256 w
[j
++] = IOVEC_MAKE_STRING("\n");
259 if (!have_syslog_identifier
&&
260 string_is_safe(program_invocation_short_name
)) {
262 /* Implicitly add program_invocation_short_name, if it
263 * is not set explicitly. We only do this for
264 * program_invocation_short_name, and nothing else
265 * since everything else is much nicer to retrieve
266 * from the outside. */
268 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
269 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
270 w
[j
++] = IOVEC_MAKE_STRING("\n");
274 if (_unlikely_(fd
< 0))
280 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
284 /* Fail silently if the journal is not available */
288 if (!IN_SET(errno
, EMSGSIZE
, ENOBUFS
))
291 /* Message doesn't fit... Let's dump the data in a memfd or
292 * temporary file and just pass a file descriptor of it to the
295 * For the temporary files we use /dev/shm instead of /tmp
296 * here, since we want this to be a tmpfs, and one that is
297 * available from early boot on and where unprivileged users
298 * can create files. */
299 buffer_fd
= memfd_new(NULL
);
301 if (buffer_fd
== -ENOSYS
) {
302 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
311 n
= writev(buffer_fd
, w
, j
);
316 r
= memfd_set_sealed(buffer_fd
);
321 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
323 /* Fail silently if the journal is not available */
328 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
332 k
= isempty(message
) ? 0 : strlen(message
) + 2;
340 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
342 char error
[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
344 if (j
!= buffer
+ 8 + k
)
345 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
347 memcpy(buffer
, "MESSAGE=", 8);
350 memcpy(buffer
+ 8, message
, k
- 2);
351 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
354 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
356 assert_cc(3 == LOG_ERR
);
357 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
358 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
359 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
361 return sd_journal_sendv(iov
, skip
+ 3);
371 _public_
int sd_journal_perror(const char *message
) {
372 struct iovec iovec
[3];
374 return fill_iovec_perror_and_send(message
, 0, iovec
);
377 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
378 static const union sockaddr_union sa
= {
379 .un
.sun_family
= AF_UNIX
,
380 .un
.sun_path
= "/run/systemd/journal/stdout",
382 _cleanup_close_
int fd
= -1;
387 assert_return(priority
>= 0, -EINVAL
);
388 assert_return(priority
<= 7, -EINVAL
);
390 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
394 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
398 if (shutdown(fd
, SHUT_RD
) < 0)
401 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
403 identifier
= strempty(identifier
);
405 l
= strlen(identifier
);
406 header
= alloca(l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
408 memcpy(header
, identifier
, l
);
410 header
[l
++] = '\n'; /* unit id */
411 header
[l
++] = '0' + priority
;
413 header
[l
++] = '0' + !!level_prefix
;
422 r
= loop_write(fd
, header
, l
, false);
429 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
433 va_start(ap
, format
);
434 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
440 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
441 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
445 assert_return(priority
>= 0, -EINVAL
);
446 assert_return(priority
<= 7, -EINVAL
);
447 assert_return(format
, -EINVAL
);
449 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
451 memcpy(buffer
, "MESSAGE=", 8);
452 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
454 /* Strip trailing whitespace, keep prefixing whitespace */
455 (void) strstrip(buffer
);
457 /* Suppress empty lines */
458 if (isempty(buffer
+8))
461 /* func is initialized from __func__ which is not a macro, but
462 * a static const char[], hence cannot easily be prefixed with
463 * CODE_FUNC=, hence let's do it manually here. */
464 ALLOCA_CODE_FUNC(f
, func
);
466 iov
[0] = IOVEC_MAKE_STRING(buffer
);
467 iov
[1] = IOVEC_MAKE_STRING(p
);
468 iov
[2] = IOVEC_MAKE_STRING(file
);
469 iov
[3] = IOVEC_MAKE_STRING(line
);
470 iov
[4] = IOVEC_MAKE_STRING(f
);
472 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
475 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
476 _cleanup_free_
struct iovec
*iov
= NULL
;
481 va_start(ap
, format
);
482 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
485 if (_unlikely_(i
< 0)) {
490 ALLOCA_CODE_FUNC(f
, func
);
492 iov
[0] = IOVEC_MAKE_STRING(file
);
493 iov
[1] = IOVEC_MAKE_STRING(line
);
494 iov
[2] = IOVEC_MAKE_STRING(f
);
496 r
= sd_journal_sendv(iov
, i
);
499 for (j
= 3; j
< i
; j
++)
500 free(iov
[j
].iov_base
);
505 _public_
int sd_journal_sendv_with_location(
506 const char *file
, const char *line
,
508 const struct iovec
*iov
, int n
) {
513 assert_return(iov
, -EINVAL
);
514 assert_return(n
> 0, -EINVAL
);
516 niov
= newa(struct iovec
, n
+ 3);
517 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
519 ALLOCA_CODE_FUNC(f
, func
);
521 niov
[n
++] = IOVEC_MAKE_STRING(file
);
522 niov
[n
++] = IOVEC_MAKE_STRING(line
);
523 niov
[n
++] = IOVEC_MAKE_STRING(f
);
525 return sd_journal_sendv(niov
, n
);
528 _public_
int sd_journal_perror_with_location(
529 const char *file
, const char *line
,
531 const char *message
) {
536 ALLOCA_CODE_FUNC(f
, func
);
538 iov
[0] = IOVEC_MAKE_STRING(file
);
539 iov
[1] = IOVEC_MAKE_STRING(line
);
540 iov
[2] = IOVEC_MAKE_STRING(f
);
542 return fill_iovec_perror_and_send(message
, 3, iov
);