1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #define SD_JOURNAL_SUPPRESS_LOCATION
12 #include "sd-journal.h"
14 #include "alloc-util.h"
15 #include "errno-util.h"
19 #include "memfd-util.h"
20 #include "socket-util.h"
21 #include "stdio-util.h"
22 #include "string-util.h"
23 #include "tmpfile-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 = newa(char, _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
) {
77 char p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
78 char sbuf
[LINE_MAX
+ 8] = "MESSAGE=";
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
);
91 len
= vsnprintf(buffer
+ 8, LINE_MAX
, format
, aq
);
94 if (len
>= (int)LONG_LINE_MAX
- 8)
97 /* Allocate large buffer to accomodate big message */
98 if (len
>= LINE_MAX
) {
100 buffer
= alloca(len
+ 9);
101 memcpy(buffer
, "MESSAGE=", 8);
102 rlen
= vsnprintf(buffer
+ 8, len
+ 1, format
, ap
);
106 /* Strip trailing whitespace, keep prefix whitespace. */
107 (void) strstrip(buffer
);
109 /* Suppress empty lines */
110 if (isempty(buffer
+ 8))
113 iov
[0] = IOVEC_MAKE_STRING(buffer
);
114 iov
[1] = IOVEC_MAKE_STRING(p
);
116 return sd_journal_sendv(iov
, 2);
119 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
121 int r
, n
= 0, i
= 0, j
;
122 struct iovec
*iov
= NULL
;
127 n
= MAX(extra
* 2, extra
+ 4);
128 iov
= malloc0(n
* sizeof(struct iovec
));
144 c
= realloc(iov
, n
* sizeof(struct iovec
));
154 if (vasprintf(&buffer
, format
, aq
) < 0) {
161 VA_FORMAT_ADVANCE(format
, ap
);
163 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
165 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
167 format
= va_arg(ap
, char *);
175 for (j
= 0; j
< i
; j
++)
176 free(iov
[j
].iov_base
);
183 _public_
int sd_journal_send(const char *format
, ...) {
186 struct iovec
*iov
= NULL
;
188 va_start(ap
, format
);
189 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
192 if (_unlikely_(i
< 0)) {
197 r
= sd_journal_sendv(iov
, i
);
200 for (j
= 0; j
< i
; j
++)
201 free(iov
[j
].iov_base
);
208 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
211 _cleanup_close_
int buffer_fd
= -1;
215 static const union sockaddr_union sa
= {
216 .un
.sun_family
= AF_UNIX
,
217 .un
.sun_path
= "/run/systemd/journal/socket",
220 .msg_name
= (struct sockaddr
*) &sa
.sa
,
221 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
224 bool have_syslog_identifier
= false;
227 assert_return(iov
, -EINVAL
);
228 assert_return(n
> 0, -EINVAL
);
230 w
= newa(struct iovec
, n
* 5 + 3);
231 l
= newa(uint64_t, n
);
233 for (i
= 0; i
< n
; i
++) {
236 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
239 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
240 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
243 have_syslog_identifier
= have_syslog_identifier
||
244 (c
== (char *) iov
[i
].iov_base
+ 17 &&
245 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
247 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
249 if (_unlikely_(nl
< c
))
252 /* Already includes a newline? Bummer, then
253 * let's write the variable name, then a
254 * newline, then the size (64bit LE), followed
255 * by the data and a final newline */
257 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
258 w
[j
++] = IOVEC_MAKE_STRING("\n");
260 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
261 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
263 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
265 /* Nothing special? Then just add the line and
266 * append a newline */
269 w
[j
++] = IOVEC_MAKE_STRING("\n");
272 if (!have_syslog_identifier
&&
273 string_is_safe(program_invocation_short_name
)) {
275 /* Implicitly add program_invocation_short_name, if it
276 * is not set explicitly. We only do this for
277 * program_invocation_short_name, and nothing else
278 * since everything else is much nicer to retrieve
279 * from the outside. */
281 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
282 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
283 w
[j
++] = IOVEC_MAKE_STRING("\n");
287 if (_unlikely_(fd
< 0))
293 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
297 /* Fail silently if the journal is not available */
301 if (!IN_SET(errno
, EMSGSIZE
, ENOBUFS
))
304 /* Message doesn't fit... Let's dump the data in a memfd or
305 * temporary file and just pass a file descriptor of it to the
308 * For the temporary files we use /dev/shm instead of /tmp
309 * here, since we want this to be a tmpfs, and one that is
310 * available from early boot on and where unprivileged users
311 * can create files. */
312 buffer_fd
= memfd_new(NULL
);
314 if (buffer_fd
== -ENOSYS
) {
315 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
324 n
= writev(buffer_fd
, w
, j
);
329 r
= memfd_set_sealed(buffer_fd
);
334 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
336 /* Fail silently if the journal is not available */
341 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
345 k
= isempty(message
) ? 0 : strlen(message
) + 2;
353 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
355 char error
[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
357 if (j
!= buffer
+ 8 + k
)
358 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
360 memcpy(buffer
, "MESSAGE=", 8);
363 memcpy(buffer
+ 8, message
, k
- 2);
364 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
367 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
369 assert_cc(3 == LOG_ERR
);
370 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
371 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
372 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
374 return sd_journal_sendv(iov
, skip
+ 3);
384 _public_
int sd_journal_perror(const char *message
) {
385 struct iovec iovec
[3];
387 return fill_iovec_perror_and_send(message
, 0, iovec
);
390 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
391 static const union sockaddr_union sa
= {
392 .un
.sun_family
= AF_UNIX
,
393 .un
.sun_path
= "/run/systemd/journal/stdout",
395 _cleanup_close_
int fd
= -1;
400 assert_return(priority
>= 0, -EINVAL
);
401 assert_return(priority
<= 7, -EINVAL
);
403 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
407 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
411 if (shutdown(fd
, SHUT_RD
) < 0)
414 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
416 identifier
= strempty(identifier
);
418 l
= strlen(identifier
);
419 header
= newa(char, l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
421 memcpy(header
, identifier
, l
);
423 header
[l
++] = '\n'; /* unit id */
424 header
[l
++] = '0' + priority
;
426 header
[l
++] = '0' + !!level_prefix
;
435 r
= loop_write(fd
, header
, l
, false);
442 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
446 va_start(ap
, format
);
447 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
453 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
454 char p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
455 char sbuf
[LINE_MAX
+ 8] = "MESSAGE=";
462 assert_return(priority
>= 0, -EINVAL
);
463 assert_return(priority
<= 7, -EINVAL
);
464 assert_return(format
, -EINVAL
);
466 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
469 len
= vsnprintf(buffer
+ 8, LINE_MAX
, format
, aq
);
472 if (len
>= (int)LONG_LINE_MAX
- 8)
475 /* Allocate large buffer to accomodate big message */
476 if (len
>= LINE_MAX
) {
478 buffer
= alloca(len
+ 9);
479 memcpy(buffer
, "MESSAGE=", 8);
480 rlen
= vsnprintf(buffer
+ 8, len
+ 1, format
, ap
);
484 /* Strip trailing whitespace, keep prefixing whitespace */
485 (void) strstrip(buffer
);
487 /* Suppress empty lines */
488 if (isempty(buffer
+ 8))
491 /* func is initialized from __func__ which is not a macro, but
492 * a static const char[], hence cannot easily be prefixed with
493 * CODE_FUNC=, hence let's do it manually here. */
494 ALLOCA_CODE_FUNC(f
, func
);
496 iov
[0] = IOVEC_MAKE_STRING(buffer
);
497 iov
[1] = IOVEC_MAKE_STRING(p
);
498 iov
[2] = IOVEC_MAKE_STRING(file
);
499 iov
[3] = IOVEC_MAKE_STRING(line
);
500 iov
[4] = IOVEC_MAKE_STRING(f
);
502 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
505 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
506 _cleanup_free_
struct iovec
*iov
= NULL
;
511 va_start(ap
, format
);
512 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
515 if (_unlikely_(i
< 0)) {
520 ALLOCA_CODE_FUNC(f
, func
);
522 iov
[0] = IOVEC_MAKE_STRING(file
);
523 iov
[1] = IOVEC_MAKE_STRING(line
);
524 iov
[2] = IOVEC_MAKE_STRING(f
);
526 r
= sd_journal_sendv(iov
, i
);
529 for (j
= 3; j
< i
; j
++)
530 free(iov
[j
].iov_base
);
535 _public_
int sd_journal_sendv_with_location(
536 const char *file
, const char *line
,
538 const struct iovec
*iov
, int n
) {
543 assert_return(iov
, -EINVAL
);
544 assert_return(n
> 0, -EINVAL
);
546 niov
= newa(struct iovec
, n
+ 3);
547 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
549 ALLOCA_CODE_FUNC(f
, func
);
551 niov
[n
++] = IOVEC_MAKE_STRING(file
);
552 niov
[n
++] = IOVEC_MAKE_STRING(line
);
553 niov
[n
++] = IOVEC_MAKE_STRING(f
);
555 return sd_journal_sendv(niov
, n
);
558 _public_
int sd_journal_perror_with_location(
559 const char *file
, const char *line
,
561 const char *message
) {
566 ALLOCA_CODE_FUNC(f
, func
);
568 iov
[0] = IOVEC_MAKE_STRING(file
);
569 iov
[1] = IOVEC_MAKE_STRING(line
);
570 iov
[2] = IOVEC_MAKE_STRING(f
);
572 return fill_iovec_perror_and_send(message
, 3, iov
);