]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journal-send.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / journal / journal-send.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7f3e6257 2
7f3e6257 3#include <errno.h>
0dad12c1 4#include <fcntl.h>
72f1d5a2 5#include <printf.h>
07630cea
LP
6#include <stddef.h>
7#include <sys/socket.h>
8#include <sys/un.h>
9#include <unistd.h>
7f3e6257 10
b070e7f3
LP
11#define SD_JOURNAL_SUPPRESS_LOCATION
12
7f3e6257 13#include "sd-journal.h"
07630cea 14
b5efdb8a 15#include "alloc-util.h"
3ffd4af2 16#include "fd-util.h"
c004493c 17#include "io-util.h"
a09abc4a 18#include "memfd-util.h"
07630cea 19#include "socket-util.h"
15a5e950 20#include "stdio-util.h"
07630cea 21#include "string-util.h"
e4de7287 22#include "tmpfile-util.h"
07630cea 23#include "util.h"
7f3e6257 24
bb99a35a
LP
25#define SNDBUF_SIZE (8*1024*1024)
26
3ed08c44
LP
27#define ALLOCA_CODE_FUNC(f, func) \
28 do { \
29 size_t _fl; \
30 const char *_func = (func); \
31 char **_f = &(f); \
32 _fl = strlen(_func) + 1; \
6e9417f5 33 *_f = newa(char, _fl + 10); \
3ed08c44
LP
34 memcpy(*_f, "CODE_FUNC=", 10); \
35 memcpy(*_f + 10, _func, _fl); \
9ed794a3 36 } while (false)
3ed08c44 37
7f3e6257
LP
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 */
42
43static int journal_fd(void) {
44 int fd;
45 static int fd_plus_one = 0;
46
47retry:
48 if (fd_plus_one > 0)
49 return fd_plus_one - 1;
50
51 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
52 if (fd < 0)
53 return -errno;
54
bb99a35a
LP
55 fd_inc_sndbuf(fd, SNDBUF_SIZE);
56
7f3e6257 57 if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
03e334a1 58 safe_close(fd);
7f3e6257
LP
59 goto retry;
60 }
61
62 return fd;
63}
64
a5344d2c 65_public_ int sd_journal_print(int priority, const char *format, ...) {
7f3e6257
LP
66 int r;
67 va_list ap;
68
69 va_start(ap, format);
d0bbc21c 70 r = sd_journal_printv(priority, format, ap);
7f3e6257
LP
71 va_end(ap);
72
73 return r;
74}
75
a5344d2c 76_public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
18c7ed18
LP
77
78 /* FIXME: Instead of limiting things to LINE_MAX we could do a
79 C99 variable-length array on the stack here in a loop. */
80
fbd0b64f 81 char buffer[8 + LINE_MAX], p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
5ffa8c81 82 struct iovec iov[2];
d0bbc21c 83
1ae464e0
TA
84 assert_return(priority >= 0, -EINVAL);
85 assert_return(priority <= 7, -EINVAL);
86 assert_return(format, -EINVAL);
fe652127 87
5ffa8c81 88 xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
7f3e6257
LP
89
90 memcpy(buffer, "MESSAGE=", 8);
91 vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
7f3e6257 92
c24f1f9d
LP
93 /* Strip trailing whitespace, keep prefix whitespace. */
94 (void) strstrip(buffer);
95
0d23bc57
LP
96 /* Suppress empty lines */
97 if (isempty(buffer+8))
98 return 0;
99
e6a7ec4b
LP
100 iov[0] = IOVEC_MAKE_STRING(buffer);
101 iov[1] = IOVEC_MAKE_STRING(p);
7f3e6257 102
d0bbc21c 103 return sd_journal_sendv(iov, 2);
7f3e6257
LP
104}
105
44b601bc 106_printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
5c0aa72a 107 PROTECT_ERRNO;
25d042e8 108 int r, n = 0, i = 0, j;
7f3e6257 109 struct iovec *iov = NULL;
b070e7f3
LP
110
111 assert(_iov);
b070e7f3
LP
112
113 if (extra > 0) {
114 n = MAX(extra * 2, extra + 4);
115 iov = malloc0(n * sizeof(struct iovec));
116 if (!iov) {
117 r = -ENOMEM;
118 goto fail;
119 }
120
121 i = extra;
25d042e8 122 }
7f3e6257 123
7f3e6257
LP
124 while (format) {
125 struct iovec *c;
126 char *buffer;
72f1d5a2 127 va_list aq;
7f3e6257
LP
128
129 if (i >= n) {
130 n = MAX(i*2, 4);
131 c = realloc(iov, n * sizeof(struct iovec));
132 if (!c) {
133 r = -ENOMEM;
134 goto fail;
135 }
136
137 iov = c;
138 }
139
72f1d5a2
LP
140 va_copy(aq, ap);
141 if (vasprintf(&buffer, format, aq) < 0) {
142 va_end(aq);
7f3e6257
LP
143 r = -ENOMEM;
144 goto fail;
145 }
72f1d5a2
LP
146 va_end(aq);
147
148 VA_FORMAT_ADVANCE(format, ap);
7f3e6257 149
c24f1f9d
LP
150 (void) strstrip(buffer); /* strip trailing whitespace, keep prefixing whitespace */
151
e6a7ec4b 152 iov[i++] = IOVEC_MAKE_STRING(buffer);
7f3e6257
LP
153
154 format = va_arg(ap, char *);
155 }
b070e7f3
LP
156
157 *_iov = iov;
158
b070e7f3
LP
159 return i;
160
161fail:
162 for (j = 0; j < i; j++)
163 free(iov[j].iov_base);
164
165 free(iov);
166
b070e7f3
LP
167 return r;
168}
169
170_public_ int sd_journal_send(const char *format, ...) {
171 int r, i, j;
172 va_list ap;
173 struct iovec *iov = NULL;
174
175 va_start(ap, format);
176 i = fill_iovec_sprintf(format, ap, 0, &iov);
7f3e6257
LP
177 va_end(ap);
178
b070e7f3
LP
179 if (_unlikely_(i < 0)) {
180 r = i;
181 goto finish;
182 }
183
7f3e6257
LP
184 r = sd_journal_sendv(iov, i);
185
b070e7f3 186finish:
7f3e6257
LP
187 for (j = 0; j < i; j++)
188 free(iov[j].iov_base);
189
190 free(iov);
191
192 return r;
193}
194
a5344d2c 195_public_ int sd_journal_sendv(const struct iovec *iov, int n) {
5c0aa72a 196 PROTECT_ERRNO;
c79e98ea 197 int fd, r;
61c024b3 198 _cleanup_close_ int buffer_fd = -1;
7f3e6257
LP
199 struct iovec *w;
200 uint64_t *l;
5c0aa72a 201 int i, j = 0;
fc2fffe7
LP
202 static const union sockaddr_union sa = {
203 .un.sun_family = AF_UNIX,
204 .un.sun_path = "/run/systemd/journal/socket",
6e5abe15
ZJS
205 };
206 struct msghdr mh = {
fc2fffe7
LP
207 .msg_name = (struct sockaddr*) &sa.sa,
208 .msg_namelen = SOCKADDR_UN_LEN(sa.un),
6e5abe15 209 };
0dad12c1 210 ssize_t k;
ee55db41 211 bool have_syslog_identifier = false;
c79e98ea 212 bool seal = true;
7f3e6257 213
1ae464e0
TA
214 assert_return(iov, -EINVAL);
215 assert_return(n > 0, -EINVAL);
7f3e6257 216
9a7800af
ZJS
217 w = newa(struct iovec, n * 5 + 3);
218 l = newa(uint64_t, n);
7f3e6257
LP
219
220 for (i = 0; i < n; i++) {
221 char *c, *nl;
222
5c0aa72a
LP
223 if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
224 return -EINVAL;
a5344d2c 225
7f3e6257 226 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
5c0aa72a
LP
227 if (_unlikely_(!c || c == iov[i].iov_base))
228 return -EINVAL;
7f3e6257 229
29abad10
ZJS
230 have_syslog_identifier = have_syslog_identifier ||
231 (c == (char *) iov[i].iov_base + 17 &&
2a0e0692 232 startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
ee55db41 233
7f3e6257
LP
234 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
235 if (nl) {
5c0aa72a
LP
236 if (_unlikely_(nl < c))
237 return -EINVAL;
7f3e6257
LP
238
239 /* Already includes a newline? Bummer, then
240 * let's write the variable name, then a
241 * newline, then the size (64bit LE), followed
242 * by the data and a final newline */
243
e6a7ec4b
LP
244 w[j++] = IOVEC_MAKE(iov[i].iov_base, c - (char*) iov[i].iov_base);
245 w[j++] = IOVEC_MAKE_STRING("\n");
7f3e6257
LP
246
247 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
e6a7ec4b 248 w[j++] = IOVEC_MAKE(&l[i], sizeof(uint64_t));
7f3e6257 249
e6a7ec4b 250 w[j++] = IOVEC_MAKE(c + 1, iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
7f3e6257
LP
251 } else
252 /* Nothing special? Then just add the line and
253 * append a newline */
254 w[j++] = iov[i];
255
e6a7ec4b 256 w[j++] = IOVEC_MAKE_STRING("\n");
7f3e6257
LP
257 }
258
ee55db41
LP
259 if (!have_syslog_identifier &&
260 string_is_safe(program_invocation_short_name)) {
261
262 /* Implicitly add program_invocation_short_name, if it
263 * is not set explicitly. We only do this for
264 * program_invocation_short_name, and nothing else
265 * since everything else is much nicer to retrieve
266 * from the outside. */
267
e6a7ec4b
LP
268 w[j++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
269 w[j++] = IOVEC_MAKE_STRING(program_invocation_short_name);
270 w[j++] = IOVEC_MAKE_STRING("\n");
ee55db41
LP
271 }
272
7f3e6257 273 fd = journal_fd();
5c0aa72a
LP
274 if (_unlikely_(fd < 0))
275 return fd;
7f3e6257 276
7f3e6257
LP
277 mh.msg_iov = w;
278 mh.msg_iovlen = j;
279
0dad12c1 280 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
5c0aa72a
LP
281 if (k >= 0)
282 return 0;
0dad12c1 283
6c045c0b
ZJS
284 /* Fail silently if the journal is not available */
285 if (errno == ENOENT)
286 return 0;
287
ec2ce0c5 288 if (!IN_SET(errno, EMSGSIZE, ENOBUFS))
5c0aa72a 289 return -errno;
0dad12c1 290
c79e98ea
LP
291 /* Message doesn't fit... Let's dump the data in a memfd or
292 * temporary file and just pass a file descriptor of it to the
293 * other side.
8e33886e 294 *
c79e98ea
LP
295 * For the temporary files we use /dev/shm instead of /tmp
296 * here, since we want this to be a tmpfs, and one that is
297 * available from early boot on and where unprivileged users
298 * can create files. */
73843b52 299 buffer_fd = memfd_new(NULL);
c79e98ea 300 if (buffer_fd < 0) {
73843b52 301 if (buffer_fd == -ENOSYS) {
03532f0a 302 buffer_fd = open_tmpfile_unlinkable("/dev/shm", O_RDWR | O_CLOEXEC);
c79e98ea
LP
303 if (buffer_fd < 0)
304 return buffer_fd;
305
306 seal = false;
307 } else
73843b52 308 return buffer_fd;
c79e98ea 309 }
0dad12c1 310
87b02843 311 n = writev(buffer_fd, w, j);
61c024b3 312 if (n < 0)
5c0aa72a 313 return -errno;
0dad12c1 314
c79e98ea 315 if (seal) {
73843b52 316 r = memfd_set_sealed(buffer_fd);
c79e98ea 317 if (r < 0)
73843b52 318 return r;
c79e98ea
LP
319 }
320
4941e4ac
ZJS
321 r = send_one_fd_sa(fd, buffer_fd, mh.msg_name, mh.msg_namelen, 0);
322 if (r == -ENOENT)
323 /* Fail silently if the journal is not available */
324 return 0;
325 return r;
7f3e6257 326}
fe652127 327
18c7ed18 328static int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
5c0aa72a
LP
329 PROTECT_ERRNO;
330 size_t n, k;
18c7ed18
LP
331
332 k = isempty(message) ? 0 : strlen(message) + 2;
333 n = 8 + k + 256 + 1;
334
335 for (;;) {
336 char buffer[n];
337 char* j;
338
339 errno = 0;
5c0aa72a 340 j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
18c7ed18 341 if (errno == 0) {
fbd0b64f 342 char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
18c7ed18
LP
343
344 if (j != buffer + 8 + k)
345 memmove(buffer + 8 + k, j, strlen(j)+1);
346
347 memcpy(buffer, "MESSAGE=", 8);
348
349 if (k > 0) {
350 memcpy(buffer + 8, message, k - 2);
351 memcpy(buffer + 8 + k - 2, ": ", 2);
352 }
353
5ffa8c81 354 xsprintf(error, "ERRNO=%i", _saved_errno_);
18c7ed18 355
4850d39a 356 assert_cc(3 == LOG_ERR);
e6a7ec4b
LP
357 iov[skip+0] = IOVEC_MAKE_STRING("PRIORITY=3");
358 iov[skip+1] = IOVEC_MAKE_STRING(buffer);
359 iov[skip+2] = IOVEC_MAKE_STRING(error);
18c7ed18 360
5c0aa72a 361 return sd_journal_sendv(iov, skip + 3);
18c7ed18
LP
362 }
363
5c0aa72a
LP
364 if (errno != ERANGE)
365 return -errno;
18c7ed18
LP
366
367 n *= 2;
368 }
369}
370
371_public_ int sd_journal_perror(const char *message) {
372 struct iovec iovec[3];
373
374 return fill_iovec_perror_and_send(message, 0, iovec);
375}
376
4cd9a9d9 377_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
fc2fffe7 378 static const union sockaddr_union sa = {
6e5abe15
ZJS
379 .un.sun_family = AF_UNIX,
380 .un.sun_path = "/run/systemd/journal/stdout",
381 };
61c024b3 382 _cleanup_close_ int fd = -1;
fe652127
LP
383 char *header;
384 size_t l;
61c024b3 385 int r;
fe652127 386
1ae464e0
TA
387 assert_return(priority >= 0, -EINVAL);
388 assert_return(priority <= 7, -EINVAL);
fe652127
LP
389
390 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
391 if (fd < 0)
392 return -errno;
393
fc2fffe7 394 r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
61c024b3 395 if (r < 0)
fe652127 396 return -errno;
fe652127 397
61c024b3 398 if (shutdown(fd, SHUT_RD) < 0)
86b9b8e7 399 return -errno;
86b9b8e7 400
efb8fd6e 401 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
bb99a35a 402
ad5d4b17 403 identifier = strempty(identifier);
fe652127 404
4cd9a9d9 405 l = strlen(identifier);
6e9417f5 406 header = newa(char, l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
fe652127 407
4cd9a9d9 408 memcpy(header, identifier, l);
fe652127 409 header[l++] = '\n';
a6e87e90 410 header[l++] = '\n'; /* unit id */
fe652127
LP
411 header[l++] = '0' + priority;
412 header[l++] = '\n';
4cd9a9d9 413 header[l++] = '0' + !!level_prefix;
fe652127
LP
414 header[l++] = '\n';
415 header[l++] = '0';
416 header[l++] = '\n';
224f2ee2
LP
417 header[l++] = '0';
418 header[l++] = '\n';
419 header[l++] = '0';
420 header[l++] = '\n';
fe652127 421
553acb7b 422 r = loop_write(fd, header, l, false);
61c024b3
ZJS
423 if (r < 0)
424 return r;
fe652127 425
c10d6bdb 426 return TAKE_FD(fd);
fe652127 427}
b070e7f3
LP
428
429_public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
430 int r;
431 va_list ap;
432
433 va_start(ap, format);
434 r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
435 va_end(ap);
436
437 return r;
438}
439
440_public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
fbd0b64f 441 char buffer[8 + LINE_MAX], p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
b070e7f3
LP
442 struct iovec iov[5];
443 char *f;
b070e7f3 444
1ae464e0
TA
445 assert_return(priority >= 0, -EINVAL);
446 assert_return(priority <= 7, -EINVAL);
447 assert_return(format, -EINVAL);
b070e7f3 448
5ffa8c81 449 xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
b070e7f3
LP
450
451 memcpy(buffer, "MESSAGE=", 8);
452 vsnprintf(buffer+8, sizeof(buffer) - 8, format, ap);
b070e7f3 453
0d23bc57
LP
454 /* Strip trailing whitespace, keep prefixing whitespace */
455 (void) strstrip(buffer);
456
457 /* Suppress empty lines */
458 if (isempty(buffer+8))
459 return 0;
c24f1f9d 460
b070e7f3
LP
461 /* func is initialized from __func__ which is not a macro, but
462 * a static const char[], hence cannot easily be prefixed with
463 * CODE_FUNC=, hence let's do it manually here. */
3ed08c44 464 ALLOCA_CODE_FUNC(f, func);
b070e7f3 465
e6a7ec4b
LP
466 iov[0] = IOVEC_MAKE_STRING(buffer);
467 iov[1] = IOVEC_MAKE_STRING(p);
468 iov[2] = IOVEC_MAKE_STRING(file);
469 iov[3] = IOVEC_MAKE_STRING(line);
470 iov[4] = IOVEC_MAKE_STRING(f);
b070e7f3
LP
471
472 return sd_journal_sendv(iov, ELEMENTSOF(iov));
473}
474
475_public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
e6a7ec4b 476 _cleanup_free_ struct iovec *iov = NULL;
b070e7f3
LP
477 int r, i, j;
478 va_list ap;
b070e7f3 479 char *f;
b070e7f3
LP
480
481 va_start(ap, format);
482 i = fill_iovec_sprintf(format, ap, 3, &iov);
483 va_end(ap);
484
485 if (_unlikely_(i < 0)) {
486 r = i;
487 goto finish;
488 }
489
3ed08c44 490 ALLOCA_CODE_FUNC(f, func);
b070e7f3 491
e6a7ec4b
LP
492 iov[0] = IOVEC_MAKE_STRING(file);
493 iov[1] = IOVEC_MAKE_STRING(line);
494 iov[2] = IOVEC_MAKE_STRING(f);
b070e7f3
LP
495
496 r = sd_journal_sendv(iov, i);
497
498finish:
499 for (j = 3; j < i; j++)
500 free(iov[j].iov_base);
501
b070e7f3
LP
502 return r;
503}
504
18c7ed18
LP
505_public_ int sd_journal_sendv_with_location(
506 const char *file, const char *line,
507 const char *func,
508 const struct iovec *iov, int n) {
509
b070e7f3
LP
510 struct iovec *niov;
511 char *f;
b070e7f3 512
1ae464e0
TA
513 assert_return(iov, -EINVAL);
514 assert_return(n > 0, -EINVAL);
b070e7f3 515
702bd55c 516 niov = newa(struct iovec, n + 3);
b070e7f3
LP
517 memcpy(niov, iov, sizeof(struct iovec) * n);
518
3ed08c44 519 ALLOCA_CODE_FUNC(f, func);
b070e7f3 520
e6a7ec4b
LP
521 niov[n++] = IOVEC_MAKE_STRING(file);
522 niov[n++] = IOVEC_MAKE_STRING(line);
523 niov[n++] = IOVEC_MAKE_STRING(f);
b070e7f3
LP
524
525 return sd_journal_sendv(niov, n);
526}
18c7ed18
LP
527
528_public_ int sd_journal_perror_with_location(
529 const char *file, const char *line,
530 const char *func,
531 const char *message) {
532
533 struct iovec iov[6];
18c7ed18
LP
534 char *f;
535
3ed08c44 536 ALLOCA_CODE_FUNC(f, func);
18c7ed18 537
e6a7ec4b
LP
538 iov[0] = IOVEC_MAKE_STRING(file);
539 iov[1] = IOVEC_MAKE_STRING(line);
540 iov[2] = IOVEC_MAKE_STRING(f);
18c7ed18
LP
541
542 return fill_iovec_perror_and_send(message, 3, iov);
543}