1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
12 #include <sys/socket.h>
16 #define SD_JOURNAL_SUPPRESS_LOCATION
18 #include "sd-journal.h"
20 #include "alloc-util.h"
24 #include "memfd-util.h"
25 #include "socket-util.h"
26 #include "stdio-util.h"
27 #include "string-util.h"
30 #define SNDBUF_SIZE (8*1024*1024)
32 #define ALLOCA_CODE_FUNC(f, func) \
35 const char *_func = (func); \
37 _fl = strlen(_func) + 1; \
38 *_f = alloca(_fl + 10); \
39 memcpy(*_f, "CODE_FUNC=", 10); \
40 memcpy(*_f + 10, _func, _fl); \
43 /* We open a single fd, and we'll share it with the current process,
44 * all its threads, and all its subprocesses. This means we need to
45 * initialize it atomically, and need to operate on it atomically
46 * never assuming we are the only user */
48 static int journal_fd(void) {
50 static int fd_plus_one
= 0;
54 return fd_plus_one
- 1;
56 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
60 fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
62 if (!__sync_bool_compare_and_swap(&fd_plus_one
, 0, fd
+1)) {
70 _public_
int sd_journal_print(int priority
, const char *format
, ...) {
75 r
= sd_journal_printv(priority
, format
, ap
);
81 _public_
int sd_journal_printv(int priority
, const char *format
, va_list ap
) {
83 /* FIXME: Instead of limiting things to LINE_MAX we could do a
84 C99 variable-length array on the stack here in a loop. */
86 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
89 assert_return(priority
>= 0, -EINVAL
);
90 assert_return(priority
<= 7, -EINVAL
);
91 assert_return(format
, -EINVAL
);
93 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
95 memcpy(buffer
, "MESSAGE=", 8);
96 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
98 /* Strip trailing whitespace, keep prefix whitespace. */
99 (void) strstrip(buffer
);
101 /* Suppress empty lines */
102 if (isempty(buffer
+8))
105 iov
[0] = IOVEC_MAKE_STRING(buffer
);
106 iov
[1] = IOVEC_MAKE_STRING(p
);
108 return sd_journal_sendv(iov
, 2);
111 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
113 int r
, n
= 0, i
= 0, j
;
114 struct iovec
*iov
= NULL
;
119 n
= MAX(extra
* 2, extra
+ 4);
120 iov
= malloc0(n
* sizeof(struct iovec
));
136 c
= realloc(iov
, n
* sizeof(struct iovec
));
146 if (vasprintf(&buffer
, format
, aq
) < 0) {
153 VA_FORMAT_ADVANCE(format
, ap
);
155 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
157 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
159 format
= va_arg(ap
, char *);
167 for (j
= 0; j
< i
; j
++)
168 free(iov
[j
].iov_base
);
175 _public_
int sd_journal_send(const char *format
, ...) {
178 struct iovec
*iov
= NULL
;
180 va_start(ap
, format
);
181 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
184 if (_unlikely_(i
< 0)) {
189 r
= sd_journal_sendv(iov
, i
);
192 for (j
= 0; j
< i
; j
++)
193 free(iov
[j
].iov_base
);
200 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
203 _cleanup_close_
int buffer_fd
= -1;
207 static const union sockaddr_union sa
= {
208 .un
.sun_family
= AF_UNIX
,
209 .un
.sun_path
= "/run/systemd/journal/socket",
212 .msg_name
= (struct sockaddr
*) &sa
.sa
,
213 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
216 bool have_syslog_identifier
= false;
219 assert_return(iov
, -EINVAL
);
220 assert_return(n
> 0, -EINVAL
);
222 w
= newa(struct iovec
, n
* 5 + 3);
223 l
= newa(uint64_t, n
);
225 for (i
= 0; i
< n
; i
++) {
228 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
231 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
232 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
235 have_syslog_identifier
= have_syslog_identifier
||
236 (c
== (char *) iov
[i
].iov_base
+ 17 &&
237 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
239 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
241 if (_unlikely_(nl
< c
))
244 /* Already includes a newline? Bummer, then
245 * let's write the variable name, then a
246 * newline, then the size (64bit LE), followed
247 * by the data and a final newline */
249 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
250 w
[j
++] = IOVEC_MAKE_STRING("\n");
252 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
253 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
255 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
257 /* Nothing special? Then just add the line and
258 * append a newline */
261 w
[j
++] = IOVEC_MAKE_STRING("\n");
264 if (!have_syslog_identifier
&&
265 string_is_safe(program_invocation_short_name
)) {
267 /* Implicitly add program_invocation_short_name, if it
268 * is not set explicitly. We only do this for
269 * program_invocation_short_name, and nothing else
270 * since everything else is much nicer to retrieve
271 * from the outside. */
273 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
274 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
275 w
[j
++] = IOVEC_MAKE_STRING("\n");
279 if (_unlikely_(fd
< 0))
285 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
289 /* Fail silently if the journal is not available */
293 if (!IN_SET(errno
, EMSGSIZE
, ENOBUFS
))
296 /* Message doesn't fit... Let's dump the data in a memfd or
297 * temporary file and just pass a file descriptor of it to the
300 * For the temporary files we use /dev/shm instead of /tmp
301 * here, since we want this to be a tmpfs, and one that is
302 * available from early boot on and where unprivileged users
303 * can create files. */
304 buffer_fd
= memfd_new(NULL
);
306 if (buffer_fd
== -ENOSYS
) {
307 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
316 n
= writev(buffer_fd
, w
, j
);
321 r
= memfd_set_sealed(buffer_fd
);
326 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
328 /* Fail silently if the journal is not available */
333 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
337 k
= isempty(message
) ? 0 : strlen(message
) + 2;
345 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
347 char error
[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
349 if (j
!= buffer
+ 8 + k
)
350 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
352 memcpy(buffer
, "MESSAGE=", 8);
355 memcpy(buffer
+ 8, message
, k
- 2);
356 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
359 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
361 assert_cc(3 == LOG_ERR
);
362 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
363 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
364 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
366 return sd_journal_sendv(iov
, skip
+ 3);
376 _public_
int sd_journal_perror(const char *message
) {
377 struct iovec iovec
[3];
379 return fill_iovec_perror_and_send(message
, 0, iovec
);
382 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
383 static const union sockaddr_union sa
= {
384 .un
.sun_family
= AF_UNIX
,
385 .un
.sun_path
= "/run/systemd/journal/stdout",
387 _cleanup_close_
int fd
= -1;
392 assert_return(priority
>= 0, -EINVAL
);
393 assert_return(priority
<= 7, -EINVAL
);
395 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
399 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
403 if (shutdown(fd
, SHUT_RD
) < 0)
406 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
408 identifier
= strempty(identifier
);
410 l
= strlen(identifier
);
411 header
= alloca(l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
413 memcpy(header
, identifier
, l
);
415 header
[l
++] = '\n'; /* unit id */
416 header
[l
++] = '0' + priority
;
418 header
[l
++] = '0' + !!level_prefix
;
427 r
= loop_write(fd
, header
, l
, false);
434 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
438 va_start(ap
, format
);
439 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
445 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
446 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
450 assert_return(priority
>= 0, -EINVAL
);
451 assert_return(priority
<= 7, -EINVAL
);
452 assert_return(format
, -EINVAL
);
454 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
456 memcpy(buffer
, "MESSAGE=", 8);
457 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
459 /* Strip trailing whitespace, keep prefixing whitespace */
460 (void) strstrip(buffer
);
462 /* Suppress empty lines */
463 if (isempty(buffer
+8))
466 /* func is initialized from __func__ which is not a macro, but
467 * a static const char[], hence cannot easily be prefixed with
468 * CODE_FUNC=, hence let's do it manually here. */
469 ALLOCA_CODE_FUNC(f
, func
);
471 iov
[0] = IOVEC_MAKE_STRING(buffer
);
472 iov
[1] = IOVEC_MAKE_STRING(p
);
473 iov
[2] = IOVEC_MAKE_STRING(file
);
474 iov
[3] = IOVEC_MAKE_STRING(line
);
475 iov
[4] = IOVEC_MAKE_STRING(f
);
477 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
480 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
481 _cleanup_free_
struct iovec
*iov
= NULL
;
486 va_start(ap
, format
);
487 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
490 if (_unlikely_(i
< 0)) {
495 ALLOCA_CODE_FUNC(f
, func
);
497 iov
[0] = IOVEC_MAKE_STRING(file
);
498 iov
[1] = IOVEC_MAKE_STRING(line
);
499 iov
[2] = IOVEC_MAKE_STRING(f
);
501 r
= sd_journal_sendv(iov
, i
);
504 for (j
= 3; j
< i
; j
++)
505 free(iov
[j
].iov_base
);
510 _public_
int sd_journal_sendv_with_location(
511 const char *file
, const char *line
,
513 const struct iovec
*iov
, int n
) {
518 assert_return(iov
, -EINVAL
);
519 assert_return(n
> 0, -EINVAL
);
521 niov
= newa(struct iovec
, n
+ 3);
522 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
524 ALLOCA_CODE_FUNC(f
, func
);
526 niov
[n
++] = IOVEC_MAKE_STRING(file
);
527 niov
[n
++] = IOVEC_MAKE_STRING(line
);
528 niov
[n
++] = IOVEC_MAKE_STRING(f
);
530 return sd_journal_sendv(niov
, n
);
533 _public_
int sd_journal_perror_with_location(
534 const char *file
, const char *line
,
536 const char *message
) {
541 ALLOCA_CODE_FUNC(f
, func
);
543 iov
[0] = IOVEC_MAKE_STRING(file
);
544 iov
[1] = IOVEC_MAKE_STRING(line
);
545 iov
[2] = IOVEC_MAKE_STRING(f
);
547 return fill_iovec_perror_and_send(message
, 3, iov
);