2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/socket.h>
28 #define SD_JOURNAL_SUPPRESS_LOCATION
30 #include "sd-journal.h"
32 #include "alloc-util.h"
36 #include "memfd-util.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-util.h"
42 #define SNDBUF_SIZE (8*1024*1024)
44 #define ALLOCA_CODE_FUNC(f, func) \
47 const char *_func = (func); \
49 _fl = strlen(_func) + 1; \
50 *_f = alloca(_fl + 10); \
51 memcpy(*_f, "CODE_FUNC=", 10); \
52 memcpy(*_f + 10, _func, _fl); \
55 /* We open a single fd, and we'll share it with the current process,
56 * all its threads, and all its subprocesses. This means we need to
57 * initialize it atomically, and need to operate on it atomically
58 * never assuming we are the only user */
60 static int journal_fd(void) {
62 static int fd_plus_one
= 0;
66 return fd_plus_one
- 1;
68 fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0);
72 fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
74 if (!__sync_bool_compare_and_swap(&fd_plus_one
, 0, fd
+1)) {
82 _public_
int sd_journal_print(int priority
, const char *format
, ...) {
87 r
= sd_journal_printv(priority
, format
, ap
);
93 _public_
int sd_journal_printv(int priority
, const char *format
, va_list ap
) {
95 /* FIXME: Instead of limiting things to LINE_MAX we could do a
96 C99 variable-length array on the stack here in a loop. */
98 char buffer
[8 + LINE_MAX
], p
[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
101 assert_return(priority
>= 0, -EINVAL
);
102 assert_return(priority
<= 7, -EINVAL
);
103 assert_return(format
, -EINVAL
);
105 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
107 memcpy(buffer
, "MESSAGE=", 8);
108 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
110 /* Strip trailing whitespace, keep prefix whitespace. */
111 (void) strstrip(buffer
);
113 /* Suppress empty lines */
114 if (isempty(buffer
+8))
117 iov
[0] = IOVEC_MAKE_STRING(buffer
);
118 iov
[1] = IOVEC_MAKE_STRING(p
);
120 return sd_journal_sendv(iov
, 2);
123 _printf_(1, 0) static int fill_iovec_sprintf(const char *format
, va_list ap
, int extra
, struct iovec
**_iov
) {
125 int r
, n
= 0, i
= 0, j
;
126 struct iovec
*iov
= NULL
;
131 n
= MAX(extra
* 2, extra
+ 4);
132 iov
= malloc0(n
* sizeof(struct iovec
));
148 c
= realloc(iov
, n
* sizeof(struct iovec
));
158 if (vasprintf(&buffer
, format
, aq
) < 0) {
165 VA_FORMAT_ADVANCE(format
, ap
);
167 (void) strstrip(buffer
); /* strip trailing whitespace, keep prefixing whitespace */
169 iov
[i
++] = IOVEC_MAKE_STRING(buffer
);
171 format
= va_arg(ap
, char *);
179 for (j
= 0; j
< i
; j
++)
180 free(iov
[j
].iov_base
);
187 _public_
int sd_journal_send(const char *format
, ...) {
190 struct iovec
*iov
= NULL
;
192 va_start(ap
, format
);
193 i
= fill_iovec_sprintf(format
, ap
, 0, &iov
);
196 if (_unlikely_(i
< 0)) {
201 r
= sd_journal_sendv(iov
, i
);
204 for (j
= 0; j
< i
; j
++)
205 free(iov
[j
].iov_base
);
212 _public_
int sd_journal_sendv(const struct iovec
*iov
, int n
) {
215 _cleanup_close_
int buffer_fd
= -1;
219 static const union sockaddr_union sa
= {
220 .un
.sun_family
= AF_UNIX
,
221 .un
.sun_path
= "/run/systemd/journal/socket",
224 .msg_name
= (struct sockaddr
*) &sa
.sa
,
225 .msg_namelen
= SOCKADDR_UN_LEN(sa
.un
),
228 bool have_syslog_identifier
= false;
231 assert_return(iov
, -EINVAL
);
232 assert_return(n
> 0, -EINVAL
);
234 w
= newa(struct iovec
, n
* 5 + 3);
235 l
= newa(uint64_t, n
);
237 for (i
= 0; i
< n
; i
++) {
240 if (_unlikely_(!iov
[i
].iov_base
|| iov
[i
].iov_len
<= 1))
243 c
= memchr(iov
[i
].iov_base
, '=', iov
[i
].iov_len
);
244 if (_unlikely_(!c
|| c
== iov
[i
].iov_base
))
247 have_syslog_identifier
= have_syslog_identifier
||
248 (c
== (char *) iov
[i
].iov_base
+ 17 &&
249 startswith(iov
[i
].iov_base
, "SYSLOG_IDENTIFIER"));
251 nl
= memchr(iov
[i
].iov_base
, '\n', iov
[i
].iov_len
);
253 if (_unlikely_(nl
< c
))
256 /* Already includes a newline? Bummer, then
257 * let's write the variable name, then a
258 * newline, then the size (64bit LE), followed
259 * by the data and a final newline */
261 w
[j
++] = IOVEC_MAKE(iov
[i
].iov_base
, c
- (char*) iov
[i
].iov_base
);
262 w
[j
++] = IOVEC_MAKE_STRING("\n");
264 l
[i
] = htole64(iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
265 w
[j
++] = IOVEC_MAKE(&l
[i
], sizeof(uint64_t));
267 w
[j
++] = IOVEC_MAKE(c
+ 1, iov
[i
].iov_len
- (c
- (char*) iov
[i
].iov_base
) - 1);
269 /* Nothing special? Then just add the line and
270 * append a newline */
273 w
[j
++] = IOVEC_MAKE_STRING("\n");
276 if (!have_syslog_identifier
&&
277 string_is_safe(program_invocation_short_name
)) {
279 /* Implicitly add program_invocation_short_name, if it
280 * is not set explicitly. We only do this for
281 * program_invocation_short_name, and nothing else
282 * since everything else is much nicer to retrieve
283 * from the outside. */
285 w
[j
++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
286 w
[j
++] = IOVEC_MAKE_STRING(program_invocation_short_name
);
287 w
[j
++] = IOVEC_MAKE_STRING("\n");
291 if (_unlikely_(fd
< 0))
297 k
= sendmsg(fd
, &mh
, MSG_NOSIGNAL
);
301 /* Fail silently if the journal is not available */
305 if (errno
!= EMSGSIZE
&& errno
!= ENOBUFS
)
308 /* Message doesn't fit... Let's dump the data in a memfd or
309 * temporary file and just pass a file descriptor of it to the
312 * For the temporary files we use /dev/shm instead of /tmp
313 * here, since we want this to be a tmpfs, and one that is
314 * available from early boot on and where unprivileged users
315 * can create files. */
316 buffer_fd
= memfd_new(NULL
);
318 if (buffer_fd
== -ENOSYS
) {
319 buffer_fd
= open_tmpfile_unlinkable("/dev/shm", O_RDWR
| O_CLOEXEC
);
328 n
= writev(buffer_fd
, w
, j
);
333 r
= memfd_set_sealed(buffer_fd
);
338 r
= send_one_fd_sa(fd
, buffer_fd
, mh
.msg_name
, mh
.msg_namelen
, 0);
340 /* Fail silently if the journal is not available */
345 static int fill_iovec_perror_and_send(const char *message
, int skip
, struct iovec iov
[]) {
349 k
= isempty(message
) ? 0 : strlen(message
) + 2;
357 j
= strerror_r(_saved_errno_
, buffer
+ 8 + k
, n
- 8 - k
);
359 char error
[sizeof("ERRNO=")-1 + DECIMAL_STR_MAX(int) + 1];
361 if (j
!= buffer
+ 8 + k
)
362 memmove(buffer
+ 8 + k
, j
, strlen(j
)+1);
364 memcpy(buffer
, "MESSAGE=", 8);
367 memcpy(buffer
+ 8, message
, k
- 2);
368 memcpy(buffer
+ 8 + k
- 2, ": ", 2);
371 xsprintf(error
, "ERRNO=%i", _saved_errno_
);
373 assert_cc(3 == LOG_ERR
);
374 iov
[skip
+0] = IOVEC_MAKE_STRING("PRIORITY=3");
375 iov
[skip
+1] = IOVEC_MAKE_STRING(buffer
);
376 iov
[skip
+2] = IOVEC_MAKE_STRING(error
);
378 return sd_journal_sendv(iov
, skip
+ 3);
388 _public_
int sd_journal_perror(const char *message
) {
389 struct iovec iovec
[3];
391 return fill_iovec_perror_and_send(message
, 0, iovec
);
394 _public_
int sd_journal_stream_fd(const char *identifier
, int priority
, int level_prefix
) {
395 static const union sockaddr_union sa
= {
396 .un
.sun_family
= AF_UNIX
,
397 .un
.sun_path
= "/run/systemd/journal/stdout",
399 _cleanup_close_
int fd
= -1;
404 assert_return(priority
>= 0, -EINVAL
);
405 assert_return(priority
<= 7, -EINVAL
);
407 fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
, 0);
411 r
= connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
));
415 if (shutdown(fd
, SHUT_RD
) < 0)
418 fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
423 l
= strlen(identifier
);
424 header
= alloca(l
+ 1 + 1 + 2 + 2 + 2 + 2 + 2);
426 memcpy(header
, identifier
, l
);
428 header
[l
++] = '\n'; /* unit id */
429 header
[l
++] = '0' + priority
;
431 header
[l
++] = '0' + !!level_prefix
;
440 r
= loop_write(fd
, header
, l
, false);
449 _public_
int sd_journal_print_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, ...) {
453 va_start(ap
, format
);
454 r
= sd_journal_printv_with_location(priority
, file
, line
, func
, format
, ap
);
460 _public_
int sd_journal_printv_with_location(int priority
, const char *file
, const char *line
, const char *func
, const char *format
, va_list ap
) {
461 char buffer
[8 + LINE_MAX
], p
[sizeof("PRIORITY=")-1 + DECIMAL_STR_MAX(int) + 1];
465 assert_return(priority
>= 0, -EINVAL
);
466 assert_return(priority
<= 7, -EINVAL
);
467 assert_return(format
, -EINVAL
);
469 xsprintf(p
, "PRIORITY=%i", priority
& LOG_PRIMASK
);
471 memcpy(buffer
, "MESSAGE=", 8);
472 vsnprintf(buffer
+8, sizeof(buffer
) - 8, format
, ap
);
474 /* Strip trailing whitespace, keep prefixing whitespace */
475 (void) strstrip(buffer
);
477 /* Suppress empty lines */
478 if (isempty(buffer
+8))
481 /* func is initialized from __func__ which is not a macro, but
482 * a static const char[], hence cannot easily be prefixed with
483 * CODE_FUNC=, hence let's do it manually here. */
484 ALLOCA_CODE_FUNC(f
, func
);
486 iov
[0] = IOVEC_MAKE_STRING(buffer
);
487 iov
[1] = IOVEC_MAKE_STRING(p
);
488 iov
[2] = IOVEC_MAKE_STRING(file
);
489 iov
[3] = IOVEC_MAKE_STRING(line
);
490 iov
[4] = IOVEC_MAKE_STRING(f
);
492 return sd_journal_sendv(iov
, ELEMENTSOF(iov
));
495 _public_
int sd_journal_send_with_location(const char *file
, const char *line
, const char *func
, const char *format
, ...) {
496 _cleanup_free_
struct iovec
*iov
= NULL
;
501 va_start(ap
, format
);
502 i
= fill_iovec_sprintf(format
, ap
, 3, &iov
);
505 if (_unlikely_(i
< 0)) {
510 ALLOCA_CODE_FUNC(f
, func
);
512 iov
[0] = IOVEC_MAKE_STRING(file
);
513 iov
[1] = IOVEC_MAKE_STRING(line
);
514 iov
[2] = IOVEC_MAKE_STRING(f
);
516 r
= sd_journal_sendv(iov
, i
);
519 for (j
= 3; j
< i
; j
++)
520 free(iov
[j
].iov_base
);
525 _public_
int sd_journal_sendv_with_location(
526 const char *file
, const char *line
,
528 const struct iovec
*iov
, int n
) {
533 assert_return(iov
, -EINVAL
);
534 assert_return(n
> 0, -EINVAL
);
536 niov
= alloca(sizeof(struct iovec
) * (n
+ 3));
537 memcpy(niov
, iov
, sizeof(struct iovec
) * n
);
539 ALLOCA_CODE_FUNC(f
, func
);
541 niov
[n
++] = IOVEC_MAKE_STRING(file
);
542 niov
[n
++] = IOVEC_MAKE_STRING(line
);
543 niov
[n
++] = IOVEC_MAKE_STRING(f
);
545 return sd_journal_sendv(niov
, n
);
548 _public_
int sd_journal_perror_with_location(
549 const char *file
, const char *line
,
551 const char *message
) {
556 ALLOCA_CODE_FUNC(f
, func
);
558 iov
[0] = IOVEC_MAKE_STRING(file
);
559 iov
[1] = IOVEC_MAKE_STRING(line
);
560 iov
[2] = IOVEC_MAKE_STRING(f
);
562 return fill_iovec_perror_and_send(message
, 3, iov
);