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 accommodate big message */
98 if (len
>= LINE_MAX
) {
99 buffer
= alloca(len
+ 9);
100 memcpy(buffer
, "MESSAGE=", 8);
101 assert_se(vsnprintf(buffer
+ 8, len
+ 1, format
, ap
) == len
);
104 /* Strip trailing whitespace, keep prefix whitespace. */
105 (void) strstrip(buffer
);
107 /* Suppress empty lines */
108 if (isempty(buffer
+ 8))
111 iov
[0] = IOVEC_MAKE_STRING(buffer
);
112 iov
[1] = IOVEC_MAKE_STRING(p
);
114 return sd_journal_sendv(iov
, 2);
117 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
119 int r
, n
= 0, i
= 0, j
;
120 struct iovec
*iov
= NULL
;
125 n
= MAX(extra
* 2, extra
+ 4);
126 iov
= malloc0(n
* sizeof(struct iovec
));
142 c
= realloc(iov
, n
* sizeof(struct iovec
));
152 if (vasprintf(&buffer
, format
, aq
) < 0) {
159 VA_FORMAT_ADVANCE(format
, ap
);
161 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
163 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
165 format
= va_arg(ap
, char *);
173 for (j
= 0; j
< i
; j
++)
174 free(iov
[j
].iov_base
);
181 _public_
int sd_journal_send(const char *format
, ...) {
184 struct iovec
*iov
= NULL
;
186 va_start(ap
, format
);
187 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
190 if (_unlikely_(i
< 0)) {
195 r
= sd_journal_sendv(iov
, i
);
198 for (j
= 0; j
< i
; j
++)
199 free(iov
[j
].iov_base
);
206 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
209 _cleanup_close_
int buffer_fd
= -1;
213 static const union sockaddr_union sa
= {
214 .un
.sun_family
= AF_UNIX
,
215 .un
.sun_path
= "/run/systemd/journal/socket",
218 .msg_name
= (struct sockaddr
*) &sa
.sa
,
219 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
222 bool have_syslog_identifier
= false;
225 assert_return(iov
, -EINVAL
);
226 assert_return(n
> 0, -EINVAL
);
228 w
= newa(struct iovec
, n
* 5 + 3);
229 l
= newa(uint64_t, n
);
231 for (i
= 0; i
< n
; i
++) {
234 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
237 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
238 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
241 have_syslog_identifier
= have_syslog_identifier
||
242 (c
== (char *) iov
[i
].iov_base
+ 17 &&
243 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
245 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
247 if (_unlikely_(nl
< c
))
250 /* Already includes a newline? Bummer, then
251 * let's write the variable name, then a
252 * newline, then the size (64bit LE), followed
253 * by the data and a final newline */
255 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
256 w
[j
++] = IOVEC_MAKE_STRING("\n");
258 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
259 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
261 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
263 /* Nothing special? Then just add the line and
264 * append a newline */
267 w
[j
++] = IOVEC_MAKE_STRING("\n");
270 if (!have_syslog_identifier
&&
271 string_is_safe(program_invocation_short_name
)) {
273 /* Implicitly add program_invocation_short_name, if it
274 * is not set explicitly. We only do this for
275 * program_invocation_short_name, and nothing else
276 * since everything else is much nicer to retrieve
277 * from the outside. */
279 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
280 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
281 w
[j
++] = IOVEC_MAKE_STRING("\n");
285 if (_unlikely_(fd
< 0))
291 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
295 /* Fail silently if the journal is not available */
299 if (!IN_SET(errno
, EMSGSIZE
, ENOBUFS
))
302 /* Message doesn't fit... Let's dump the data in a memfd or
303 * temporary file and just pass a file descriptor of it to the
306 * For the temporary files we use /dev/shm instead of /tmp
307 * here, since we want this to be a tmpfs, and one that is
308 * available from early boot on and where unprivileged users
309 * can create files. */
310 buffer_fd
= memfd_new(NULL
);
312 if (buffer_fd
== -ENOSYS
) {
313 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
322 n
= writev(buffer_fd
, w
, j
);
327 r
= memfd_set_sealed(buffer_fd
);
332 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
334 /* Fail silently if the journal is not available */
339 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
343 k
= isempty(message
) ? 0 : strlen(message
) + 2;
351 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
353 char error
[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
355 if (j
!= buffer
+ 8 + k
)
356 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
358 memcpy(buffer
, "MESSAGE=", 8);
361 memcpy(buffer
+ 8, message
, k
- 2);
362 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
365 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
367 assert_cc(3 == LOG_ERR
);
368 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
369 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
370 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
372 return sd_journal_sendv(iov
, skip
+ 3);
382 _public_
int sd_journal_perror(const char *message
) {
383 struct iovec iovec
[3];
385 return fill_iovec_perror_and_send(message
, 0, iovec
);
388 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
389 static const union sockaddr_union sa
= {
390 .un
.sun_family
= AF_UNIX
,
391 .un
.sun_path
= "/run/systemd/journal/stdout",
393 _cleanup_close_
int fd
= -1;
398 assert_return(priority
>= 0, -EINVAL
);
399 assert_return(priority
<= 7, -EINVAL
);
401 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
405 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
409 if (shutdown(fd
, SHUT_RD
) < 0)
412 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
414 identifier
= strempty(identifier
);
416 l
= strlen(identifier
);
417 header
= newa(char, l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
419 memcpy(header
, identifier
, l
);
421 header
[l
++] = '\n'; /* unit id */
422 header
[l
++] = '0' + priority
;
424 header
[l
++] = '0' + !!level_prefix
;
433 r
= loop_write(fd
, header
, l
, false);
440 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
444 va_start(ap
, format
);
445 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
451 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
452 char p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
453 char sbuf
[LINE_MAX
+ 8] = "MESSAGE=";
460 assert_return(priority
>= 0, -EINVAL
);
461 assert_return(priority
<= 7, -EINVAL
);
462 assert_return(format
, -EINVAL
);
464 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
467 len
= vsnprintf(buffer
+ 8, LINE_MAX
, format
, aq
);
470 if (len
>= (int)LONG_LINE_MAX
- 8)
473 /* Allocate large buffer to accommodate big message */
474 if (len
>= LINE_MAX
) {
475 buffer
= alloca(len
+ 9);
476 memcpy(buffer
, "MESSAGE=", 8);
477 assert_se(vsnprintf(buffer
+ 8, len
+ 1, format
, ap
) == len
);
480 /* Strip trailing whitespace, keep prefixing whitespace */
481 (void) strstrip(buffer
);
483 /* Suppress empty lines */
484 if (isempty(buffer
+ 8))
487 /* func is initialized from __func__ which is not a macro, but
488 * a static const char[], hence cannot easily be prefixed with
489 * CODE_FUNC=, hence let's do it manually here. */
490 ALLOCA_CODE_FUNC(f
, func
);
492 iov
[0] = IOVEC_MAKE_STRING(buffer
);
493 iov
[1] = IOVEC_MAKE_STRING(p
);
494 iov
[2] = IOVEC_MAKE_STRING(file
);
495 iov
[3] = IOVEC_MAKE_STRING(line
);
496 iov
[4] = IOVEC_MAKE_STRING(f
);
498 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
501 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
502 _cleanup_free_
struct iovec
*iov
= NULL
;
507 va_start(ap
, format
);
508 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
511 if (_unlikely_(i
< 0)) {
516 ALLOCA_CODE_FUNC(f
, func
);
518 iov
[0] = IOVEC_MAKE_STRING(file
);
519 iov
[1] = IOVEC_MAKE_STRING(line
);
520 iov
[2] = IOVEC_MAKE_STRING(f
);
522 r
= sd_journal_sendv(iov
, i
);
525 for (j
= 3; j
< i
; j
++)
526 free(iov
[j
].iov_base
);
531 _public_
int sd_journal_sendv_with_location(
532 const char *file
, const char *line
,
534 const struct iovec
*iov
, int n
) {
539 assert_return(iov
, -EINVAL
);
540 assert_return(n
> 0, -EINVAL
);
542 niov
= newa(struct iovec
, n
+ 3);
543 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
545 ALLOCA_CODE_FUNC(f
, func
);
547 niov
[n
++] = IOVEC_MAKE_STRING(file
);
548 niov
[n
++] = IOVEC_MAKE_STRING(line
);
549 niov
[n
++] = IOVEC_MAKE_STRING(f
);
551 return sd_journal_sendv(niov
, n
);
554 _public_
int sd_journal_perror_with_location(
555 const char *file
, const char *line
,
557 const char *message
) {
562 ALLOCA_CODE_FUNC(f
, func
);
564 iov
[0] = IOVEC_MAKE_STRING(file
);
565 iov
[1] = IOVEC_MAKE_STRING(line
);
566 iov
[2] = IOVEC_MAKE_STRING(f
);
568 return fill_iovec_perror_and_send(message
, 3, iov
);