1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
10 #include <sys/socket.h>
14 #define SD_JOURNAL_SUPPRESS_LOCATION
16 #include "sd-journal.h"
18 #include "alloc-util.h"
22 #include "memfd-util.h"
23 #include "socket-util.h"
24 #include "stdio-util.h"
25 #include "string-util.h"
28 #define SNDBUF_SIZE (8*1024*1024)
30 #define ALLOCA_CODE_FUNC(f, func) \
33 const char *_func = (func); \
35 _fl = strlen(_func) + 1; \
36 *_f = alloca(_fl + 10); \
37 memcpy(*_f, "CODE_FUNC=", 10); \
38 memcpy(*_f + 10, _func, _fl); \
41 /* We open a single fd, and we'll share it with the current process,
42 * all its threads, and all its subprocesses. This means we need to
43 * initialize it atomically, and need to operate on it atomically
44 * never assuming we are the only user */
46 static int journal_fd(void) {
48 static int fd_plus_one
= 0;
52 return fd_plus_one
- 1;
54 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
58 fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
60 if (!__sync_bool_compare_and_swap(&fd_plus_one
, 0, fd
+1)) {
68 _public_
int sd_journal_print(int priority
, const char *format
, ...) {
73 r
= sd_journal_printv(priority
, format
, ap
);
79 _public_
int sd_journal_printv(int priority
, const char *format
, va_list ap
) {
81 /* FIXME: Instead of limiting things to LINE_MAX we could do a
82 C99 variable-length array on the stack here in a loop. */
84 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
87 assert_return(priority
>= 0, -EINVAL
);
88 assert_return(priority
<= 7, -EINVAL
);
89 assert_return(format
, -EINVAL
);
91 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
93 memcpy(buffer
, "MESSAGE=", 8);
94 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
96 /* Strip trailing whitespace, keep prefix whitespace. */
97 (void) strstrip(buffer
);
99 /* Suppress empty lines */
100 if (isempty(buffer
+8))
103 iov
[0] = IOVEC_MAKE_STRING(buffer
);
104 iov
[1] = IOVEC_MAKE_STRING(p
);
106 return sd_journal_sendv(iov
, 2);
109 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
111 int r
, n
= 0, i
= 0, j
;
112 struct iovec
*iov
= NULL
;
117 n
= MAX(extra
* 2, extra
+ 4);
118 iov
= malloc0(n
* sizeof(struct iovec
));
134 c
= realloc(iov
, n
* sizeof(struct iovec
));
144 if (vasprintf(&buffer
, format
, aq
) < 0) {
151 VA_FORMAT_ADVANCE(format
, ap
);
153 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
155 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
157 format
= va_arg(ap
, char *);
165 for (j
= 0; j
< i
; j
++)
166 free(iov
[j
].iov_base
);
173 _public_
int sd_journal_send(const char *format
, ...) {
176 struct iovec
*iov
= NULL
;
178 va_start(ap
, format
);
179 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
182 if (_unlikely_(i
< 0)) {
187 r
= sd_journal_sendv(iov
, i
);
190 for (j
= 0; j
< i
; j
++)
191 free(iov
[j
].iov_base
);
198 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
201 _cleanup_close_
int buffer_fd
= -1;
205 static const union sockaddr_union sa
= {
206 .un
.sun_family
= AF_UNIX
,
207 .un
.sun_path
= "/run/systemd/journal/socket",
210 .msg_name
= (struct sockaddr
*) &sa
.sa
,
211 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
214 bool have_syslog_identifier
= false;
217 assert_return(iov
, -EINVAL
);
218 assert_return(n
> 0, -EINVAL
);
220 w
= newa(struct iovec
, n
* 5 + 3);
221 l
= newa(uint64_t, n
);
223 for (i
= 0; i
< n
; i
++) {
226 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
229 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
230 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
233 have_syslog_identifier
= have_syslog_identifier
||
234 (c
== (char *) iov
[i
].iov_base
+ 17 &&
235 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
237 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
239 if (_unlikely_(nl
< c
))
242 /* Already includes a newline? Bummer, then
243 * let's write the variable name, then a
244 * newline, then the size (64bit LE), followed
245 * by the data and a final newline */
247 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
248 w
[j
++] = IOVEC_MAKE_STRING("\n");
250 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
251 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
253 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
255 /* Nothing special? Then just add the line and
256 * append a newline */
259 w
[j
++] = IOVEC_MAKE_STRING("\n");
262 if (!have_syslog_identifier
&&
263 string_is_safe(program_invocation_short_name
)) {
265 /* Implicitly add program_invocation_short_name, if it
266 * is not set explicitly. We only do this for
267 * program_invocation_short_name, and nothing else
268 * since everything else is much nicer to retrieve
269 * from the outside. */
271 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
272 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
273 w
[j
++] = IOVEC_MAKE_STRING("\n");
277 if (_unlikely_(fd
< 0))
283 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
287 /* Fail silently if the journal is not available */
291 if (!IN_SET(errno
, EMSGSIZE
, ENOBUFS
))
294 /* Message doesn't fit... Let's dump the data in a memfd or
295 * temporary file and just pass a file descriptor of it to the
298 * For the temporary files we use /dev/shm instead of /tmp
299 * here, since we want this to be a tmpfs, and one that is
300 * available from early boot on and where unprivileged users
301 * can create files. */
302 buffer_fd
= memfd_new(NULL
);
304 if (buffer_fd
== -ENOSYS
) {
305 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
314 n
= writev(buffer_fd
, w
, j
);
319 r
= memfd_set_sealed(buffer_fd
);
324 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
326 /* Fail silently if the journal is not available */
331 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
335 k
= isempty(message
) ? 0 : strlen(message
) + 2;
343 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
345 char error
[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
347 if (j
!= buffer
+ 8 + k
)
348 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
350 memcpy(buffer
, "MESSAGE=", 8);
353 memcpy(buffer
+ 8, message
, k
- 2);
354 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
357 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
359 assert_cc(3 == LOG_ERR
);
360 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
361 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
362 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
364 return sd_journal_sendv(iov
, skip
+ 3);
374 _public_
int sd_journal_perror(const char *message
) {
375 struct iovec iovec
[3];
377 return fill_iovec_perror_and_send(message
, 0, iovec
);
380 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
381 static const union sockaddr_union sa
= {
382 .un
.sun_family
= AF_UNIX
,
383 .un
.sun_path
= "/run/systemd/journal/stdout",
385 _cleanup_close_
int fd
= -1;
390 assert_return(priority
>= 0, -EINVAL
);
391 assert_return(priority
<= 7, -EINVAL
);
393 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
397 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
401 if (shutdown(fd
, SHUT_RD
) < 0)
404 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
406 identifier
= strempty(identifier
);
408 l
= strlen(identifier
);
409 header
= alloca(l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
411 memcpy(header
, identifier
, l
);
413 header
[l
++] = '\n'; /* unit id */
414 header
[l
++] = '0' + priority
;
416 header
[l
++] = '0' + !!level_prefix
;
425 r
= loop_write(fd
, header
, l
, false);
432 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
436 va_start(ap
, format
);
437 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
443 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
444 char buffer
[8 + LINE_MAX
], p
[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
448 assert_return(priority
>= 0, -EINVAL
);
449 assert_return(priority
<= 7, -EINVAL
);
450 assert_return(format
, -EINVAL
);
452 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
454 memcpy(buffer
, "MESSAGE=", 8);
455 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
457 /* Strip trailing whitespace, keep prefixing whitespace */
458 (void) strstrip(buffer
);
460 /* Suppress empty lines */
461 if (isempty(buffer
+8))
464 /* func is initialized from __func__ which is not a macro, but
465 * a static const char[], hence cannot easily be prefixed with
466 * CODE_FUNC=, hence let's do it manually here. */
467 ALLOCA_CODE_FUNC(f
, func
);
469 iov
[0] = IOVEC_MAKE_STRING(buffer
);
470 iov
[1] = IOVEC_MAKE_STRING(p
);
471 iov
[2] = IOVEC_MAKE_STRING(file
);
472 iov
[3] = IOVEC_MAKE_STRING(line
);
473 iov
[4] = IOVEC_MAKE_STRING(f
);
475 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
478 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
479 _cleanup_free_
struct iovec
*iov
= NULL
;
484 va_start(ap
, format
);
485 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
488 if (_unlikely_(i
< 0)) {
493 ALLOCA_CODE_FUNC(f
, func
);
495 iov
[0] = IOVEC_MAKE_STRING(file
);
496 iov
[1] = IOVEC_MAKE_STRING(line
);
497 iov
[2] = IOVEC_MAKE_STRING(f
);
499 r
= sd_journal_sendv(iov
, i
);
502 for (j
= 3; j
< i
; j
++)
503 free(iov
[j
].iov_base
);
508 _public_
int sd_journal_sendv_with_location(
509 const char *file
, const char *line
,
511 const struct iovec
*iov
, int n
) {
516 assert_return(iov
, -EINVAL
);
517 assert_return(n
> 0, -EINVAL
);
519 niov
= newa(struct iovec
, n
+ 3);
520 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
522 ALLOCA_CODE_FUNC(f
, func
);
524 niov
[n
++] = IOVEC_MAKE_STRING(file
);
525 niov
[n
++] = IOVEC_MAKE_STRING(line
);
526 niov
[n
++] = IOVEC_MAKE_STRING(f
);
528 return sd_journal_sendv(niov
, n
);
531 _public_
int sd_journal_perror_with_location(
532 const char *file
, const char *line
,
534 const char *message
) {
539 ALLOCA_CODE_FUNC(f
, func
);
541 iov
[0] = IOVEC_MAKE_STRING(file
);
542 iov
[1] = IOVEC_MAKE_STRING(line
);
543 iov
[2] = IOVEC_MAKE_STRING(f
);
545 return fill_iovec_perror_and_send(message
, 3, iov
);