]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journal-send.c
d51d03acd9de09ccecb815b6002b4224e55ed2d3
[thirdparty/systemd.git] / src / journal / journal-send.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <printf.h>
6 #include <stddef.h>
7 #include <sys/un.h>
8 #include <unistd.h>
9
10 #define SD_JOURNAL_SUPPRESS_LOCATION
11
12 #include "sd-journal.h"
13
14 #include "alloc-util.h"
15 #include "errno-util.h"
16 #include "fd-util.h"
17 #include "io-util.h"
18 #include "fileio.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"
24
25 #define SNDBUF_SIZE (8*1024*1024)
26
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; \
33 *_f = newa(char, _fl + 10); \
34 memcpy(*_f, "CODE_FUNC=", 10); \
35 memcpy(*_f + 10, _func, _fl); \
36 } while (false)
37
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
43 static int journal_fd(void) {
44 int fd;
45 static int fd_plus_one = 0;
46
47 retry:
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
55 fd_inc_sndbuf(fd, SNDBUF_SIZE);
56
57 if (!__sync_bool_compare_and_swap(&fd_plus_one, 0, fd+1)) {
58 safe_close(fd);
59 goto retry;
60 }
61
62 return fd;
63 }
64
65 _public_ int sd_journal_print(int priority, const char *format, ...) {
66 int r;
67 va_list ap;
68
69 va_start(ap, format);
70 r = sd_journal_printv(priority, format, ap);
71 va_end(ap);
72
73 return r;
74 }
75
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=";
79 struct iovec iov[2];
80 int len;
81 va_list aq;
82 char *buffer = sbuf;
83
84 assert_return(priority >= 0, -EINVAL);
85 assert_return(priority <= 7, -EINVAL);
86 assert_return(format, -EINVAL);
87
88 xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
89
90 va_copy(aq, ap);
91 len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
92 va_end(aq);
93
94 if (len >= (int)LONG_LINE_MAX - 8)
95 return -ENOBUFS;
96
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);
102 }
103
104 /* Strip trailing whitespace, keep prefix whitespace. */
105 (void) strstrip(buffer);
106
107 /* Suppress empty lines */
108 if (isempty(buffer + 8))
109 return 0;
110
111 iov[0] = IOVEC_MAKE_STRING(buffer);
112 iov[1] = IOVEC_MAKE_STRING(p);
113
114 return sd_journal_sendv(iov, 2);
115 }
116
117 _printf_(1, 0) static int fill_iovec_sprintf(const char *format, va_list ap, int extra, struct iovec **_iov) {
118 PROTECT_ERRNO;
119 int r, n = 0, i = 0, j;
120 struct iovec *iov = NULL;
121
122 assert(_iov);
123
124 if (extra > 0) {
125 n = MAX(extra * 2, extra + 4);
126 iov = malloc0(n * sizeof(struct iovec));
127 if (!iov) {
128 r = -ENOMEM;
129 goto fail;
130 }
131
132 i = extra;
133 }
134
135 while (format) {
136 struct iovec *c;
137 char *buffer;
138 va_list aq;
139
140 if (i >= n) {
141 n = MAX(i*2, 4);
142 c = realloc(iov, n * sizeof(struct iovec));
143 if (!c) {
144 r = -ENOMEM;
145 goto fail;
146 }
147
148 iov = c;
149 }
150
151 va_copy(aq, ap);
152 if (vasprintf(&buffer, format, aq) < 0) {
153 va_end(aq);
154 r = -ENOMEM;
155 goto fail;
156 }
157 va_end(aq);
158
159 VA_FORMAT_ADVANCE(format, ap);
160
161 (void) strstrip(buffer); /* strip trailing whitespace, keep prefixing whitespace */
162
163 iov[i++] = IOVEC_MAKE_STRING(buffer);
164
165 format = va_arg(ap, char *);
166 }
167
168 *_iov = iov;
169
170 return i;
171
172 fail:
173 for (j = 0; j < i; j++)
174 free(iov[j].iov_base);
175
176 free(iov);
177
178 return r;
179 }
180
181 _public_ int sd_journal_send(const char *format, ...) {
182 int r, i, j;
183 va_list ap;
184 struct iovec *iov = NULL;
185
186 va_start(ap, format);
187 i = fill_iovec_sprintf(format, ap, 0, &iov);
188 va_end(ap);
189
190 if (_unlikely_(i < 0)) {
191 r = i;
192 goto finish;
193 }
194
195 r = sd_journal_sendv(iov, i);
196
197 finish:
198 for (j = 0; j < i; j++)
199 free(iov[j].iov_base);
200
201 free(iov);
202
203 return r;
204 }
205
206 _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
207 PROTECT_ERRNO;
208 int fd, r;
209 _cleanup_close_ int buffer_fd = -1;
210 struct iovec *w;
211 uint64_t *l;
212 int i, j = 0;
213 static const union sockaddr_union sa = {
214 .un.sun_family = AF_UNIX,
215 .un.sun_path = "/run/systemd/journal/socket",
216 };
217 struct msghdr mh = {
218 .msg_name = (struct sockaddr*) &sa.sa,
219 .msg_namelen = SOCKADDR_UN_LEN(sa.un),
220 };
221 ssize_t k;
222 bool have_syslog_identifier = false;
223 bool seal = true;
224
225 assert_return(iov, -EINVAL);
226 assert_return(n > 0, -EINVAL);
227
228 w = newa(struct iovec, n * 5 + 3);
229 l = newa(uint64_t, n);
230
231 for (i = 0; i < n; i++) {
232 char *c, *nl;
233
234 if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
235 return -EINVAL;
236
237 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
238 if (_unlikely_(!c || c == iov[i].iov_base))
239 return -EINVAL;
240
241 have_syslog_identifier = have_syslog_identifier ||
242 (c == (char *) iov[i].iov_base + 17 &&
243 startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
244
245 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
246 if (nl) {
247 if (_unlikely_(nl < c))
248 return -EINVAL;
249
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 */
254
255 w[j++] = IOVEC_MAKE(iov[i].iov_base, c - (char*) iov[i].iov_base);
256 w[j++] = IOVEC_MAKE_STRING("\n");
257
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));
260
261 w[j++] = IOVEC_MAKE(c + 1, iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
262 } else
263 /* Nothing special? Then just add the line and
264 * append a newline */
265 w[j++] = iov[i];
266
267 w[j++] = IOVEC_MAKE_STRING("\n");
268 }
269
270 if (!have_syslog_identifier &&
271 string_is_safe(program_invocation_short_name)) {
272
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. */
278
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");
282 }
283
284 fd = journal_fd();
285 if (_unlikely_(fd < 0))
286 return fd;
287
288 mh.msg_iov = w;
289 mh.msg_iovlen = j;
290
291 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
292 if (k >= 0)
293 return 0;
294
295 /* Fail silently if the journal is not available */
296 if (errno == ENOENT)
297 return 0;
298
299 if (!IN_SET(errno, EMSGSIZE, ENOBUFS))
300 return -errno;
301
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
304 * other side.
305 *
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);
311 if (buffer_fd < 0) {
312 if (buffer_fd == -ENOSYS) {
313 buffer_fd = open_tmpfile_unlinkable("/dev/shm", O_RDWR | O_CLOEXEC);
314 if (buffer_fd < 0)
315 return buffer_fd;
316
317 seal = false;
318 } else
319 return buffer_fd;
320 }
321
322 n = writev(buffer_fd, w, j);
323 if (n < 0)
324 return -errno;
325
326 if (seal) {
327 r = memfd_set_sealed(buffer_fd);
328 if (r < 0)
329 return r;
330 }
331
332 r = send_one_fd_sa(fd, buffer_fd, mh.msg_name, mh.msg_namelen, 0);
333 if (r == -ENOENT)
334 /* Fail silently if the journal is not available */
335 return 0;
336 return r;
337 }
338
339 static int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
340 PROTECT_ERRNO;
341 size_t n, k;
342
343 k = isempty(message) ? 0 : strlen(message) + 2;
344 n = 8 + k + 256 + 1;
345
346 for (;;) {
347 char buffer[n];
348 char* j;
349
350 errno = 0;
351 j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
352 if (errno == 0) {
353 char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
354
355 if (j != buffer + 8 + k)
356 memmove(buffer + 8 + k, j, strlen(j)+1);
357
358 memcpy(buffer, "MESSAGE=", 8);
359
360 if (k > 0) {
361 memcpy(buffer + 8, message, k - 2);
362 memcpy(buffer + 8 + k - 2, ": ", 2);
363 }
364
365 xsprintf(error, "ERRNO=%i", _saved_errno_);
366
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);
371
372 return sd_journal_sendv(iov, skip + 3);
373 }
374
375 if (errno != ERANGE)
376 return -errno;
377
378 n *= 2;
379 }
380 }
381
382 _public_ int sd_journal_perror(const char *message) {
383 struct iovec iovec[3];
384
385 return fill_iovec_perror_and_send(message, 0, iovec);
386 }
387
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",
392 };
393 _cleanup_close_ int fd = -1;
394 char *header;
395 size_t l;
396 int r;
397
398 assert_return(priority >= 0, -EINVAL);
399 assert_return(priority <= 7, -EINVAL);
400
401 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
402 if (fd < 0)
403 return -errno;
404
405 r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un));
406 if (r < 0)
407 return -errno;
408
409 if (shutdown(fd, SHUT_RD) < 0)
410 return -errno;
411
412 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
413
414 identifier = strempty(identifier);
415
416 l = strlen(identifier);
417 header = newa(char, l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
418
419 memcpy(header, identifier, l);
420 header[l++] = '\n';
421 header[l++] = '\n'; /* unit id */
422 header[l++] = '0' + priority;
423 header[l++] = '\n';
424 header[l++] = '0' + !!level_prefix;
425 header[l++] = '\n';
426 header[l++] = '0';
427 header[l++] = '\n';
428 header[l++] = '0';
429 header[l++] = '\n';
430 header[l++] = '0';
431 header[l++] = '\n';
432
433 r = loop_write(fd, header, l, false);
434 if (r < 0)
435 return r;
436
437 return TAKE_FD(fd);
438 }
439
440 _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
441 int r;
442 va_list ap;
443
444 va_start(ap, format);
445 r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
446 va_end(ap);
447
448 return r;
449 }
450
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=";
454 struct iovec iov[5];
455 char *f;
456 int len;
457 char *buffer = sbuf;
458 va_list aq;
459
460 assert_return(priority >= 0, -EINVAL);
461 assert_return(priority <= 7, -EINVAL);
462 assert_return(format, -EINVAL);
463
464 xsprintf(p, "PRIORITY=%i", priority & LOG_PRIMASK);
465
466 va_copy(aq, ap);
467 len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
468 va_end(aq);
469
470 if (len >= (int)LONG_LINE_MAX - 8)
471 return -ENOBUFS;
472
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);
478 }
479
480 /* Strip trailing whitespace, keep prefixing whitespace */
481 (void) strstrip(buffer);
482
483 /* Suppress empty lines */
484 if (isempty(buffer + 8))
485 return 0;
486
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);
491
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);
497
498 return sd_journal_sendv(iov, ELEMENTSOF(iov));
499 }
500
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;
503 int r, i, j;
504 va_list ap;
505 char *f;
506
507 va_start(ap, format);
508 i = fill_iovec_sprintf(format, ap, 3, &iov);
509 va_end(ap);
510
511 if (_unlikely_(i < 0)) {
512 r = i;
513 goto finish;
514 }
515
516 ALLOCA_CODE_FUNC(f, func);
517
518 iov[0] = IOVEC_MAKE_STRING(file);
519 iov[1] = IOVEC_MAKE_STRING(line);
520 iov[2] = IOVEC_MAKE_STRING(f);
521
522 r = sd_journal_sendv(iov, i);
523
524 finish:
525 for (j = 3; j < i; j++)
526 free(iov[j].iov_base);
527
528 return r;
529 }
530
531 _public_ int sd_journal_sendv_with_location(
532 const char *file, const char *line,
533 const char *func,
534 const struct iovec *iov, int n) {
535
536 struct iovec *niov;
537 char *f;
538
539 assert_return(iov, -EINVAL);
540 assert_return(n > 0, -EINVAL);
541
542 niov = newa(struct iovec, n + 3);
543 memcpy(niov, iov, sizeof(struct iovec) * n);
544
545 ALLOCA_CODE_FUNC(f, func);
546
547 niov[n++] = IOVEC_MAKE_STRING(file);
548 niov[n++] = IOVEC_MAKE_STRING(line);
549 niov[n++] = IOVEC_MAKE_STRING(f);
550
551 return sd_journal_sendv(niov, n);
552 }
553
554 _public_ int sd_journal_perror_with_location(
555 const char *file, const char *line,
556 const char *func,
557 const char *message) {
558
559 struct iovec iov[6];
560 char *f;
561
562 ALLOCA_CODE_FUNC(f, func);
563
564 iov[0] = IOVEC_MAKE_STRING(file);
565 iov[1] = IOVEC_MAKE_STRING(line);
566 iov[2] = IOVEC_MAKE_STRING(f);
567
568 return fill_iovec_perror_and_send(message, 3, iov);
569 }