Debian Codegraph @

src/libsystemd/sd-journal/journal-send.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4 #if HAVE_VALGRIND_VALGRIND_H
5 # include <valgrind/valgrind.h>
6 #endif
7
8 #define SD_JOURNAL_SUPPRESS_LOCATION
9
10 #include "sd-journal.h"
11
12 #include "alloc-util.h"
13 #include "errno-util.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "io-util.h"
17 #include "iovec-util.h"
18 #include "journal-send.h"
19 #include "memfd-util.h"
20 #include "process-util.h"
21 #include "socket-util.h"
22 #include "stdio-util.h"
23 #include "string-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 static int fd_plus_one = 0;
43
44 static int journal_fd(void) {
45 int fd;
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 (!__atomic_compare_exchange_n(&fd_plus_one, &(int){0}, fd+1,
58 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
59 safe_close(fd);
60 goto retry;
61 }
62
63 return fd;
64 }
65
66 int journal_fd_nonblock(bool nonblock) {
67 int r;
68
69 r = journal_fd();
70 if (r < 0)
71 return r;
72
73 return fd_nonblock(r, nonblock);
74 }
75
76 void close_journal_fd(void) {
77 #if HAVE_VALGRIND_VALGRIND_H
78 /* Be nice to valgrind. This is not atomic, so it is useful mainly for debugging. */
79
80 if (!RUNNING_ON_VALGRIND)
81 return;
82
83 if (!is_main_thread())
84 return;
85
86 if (fd_plus_one <= 0)
87 return;
88
89 safe_close(fd_plus_one - 1);
90 fd_plus_one = 0;
91 #endif
92 }
93
94 _public_ int sd_journal_print(int priority, const char *format, ...) {
95 int r;
96 va_list ap;
97
98 va_start(ap, format);
99 r = sd_journal_printv(priority, format, ap);
100 va_end(ap);
101
102 return r;
103 }
104
105 _public_ int sd_journal_printv(int priority, const char *format, va_list ap) {
106 char p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
107 char sbuf[LINE_MAX + 8] = "MESSAGE=";
108 struct iovec iov[2];
109 int len;
110 va_list aq;
111 char *buffer = sbuf;
112
113 assert_return(priority >= 0, -EINVAL);
114 assert_return(priority <= 7, -EINVAL);
115 assert_return(format, -EINVAL);
116
117 xsprintf(p, "PRIORITY=%i", LOG_PRI(priority));
118
119 va_copy(aq, ap);
120 len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
121 va_end(aq);
122
123 if (len >= (int)LONG_LINE_MAX - 8)
124 return -ENOBUFS;
125
126 /* Allocate large buffer to accommodate big message */
127 if (len >= LINE_MAX) {
128 buffer = alloca_safe(len + 9);
129 memcpy(buffer, "MESSAGE=", 8);
130 assert_se(vsnprintf(buffer + 8, len + 1, format, ap) == len);
131 }
132
133 /* Strip trailing whitespace, keep prefix whitespace. */
134 (void) strstrip(buffer);
135
136 /* Suppress empty lines */
137 if (isempty(buffer + 8))
138 return 0;
139
140 iov[0] = IOVEC_MAKE_STRING(buffer);
141 iov[1] = IOVEC_MAKE_STRING(p);
142
143 return sd_journal_sendv(iov, 2);
144 }
145
146 _printf_(1, 0) static int fill_iovec_sprintf(
147 const char *format,
148 va_list ap,
149 size_t extra,
150 struct iovec **ret_iov,
151 size_t *ret_n_iov) {
152
153 PROTECT_ERRNO;
154 struct iovec *iov = NULL;
155 size_t n = 0;
156
157 assert(ret_iov);
158 assert(ret_n_iov);
159
160 if (extra > 0) {
161 if (!GREEDY_REALLOC0(iov, extra))
162 return -ENOMEM;
163
164 n = extra;
165 }
166
167 CLEANUP_ARRAY(iov, n, iovec_array_free);
168
169 while (format) {
170 _cleanup_free_ char *buffer = NULL;
171 va_list aq;
172
173 va_copy(aq, ap);
174 if (vasprintf(&buffer, format, aq) < 0) {
175 va_end(aq);
176 return -ENOMEM;
177 }
178 va_end(aq);
179
180 VA_FORMAT_ADVANCE(format, ap);
181 format = va_arg(ap, char *);
182
183 if (!GREEDY_REALLOC(iov, n + 1))
184 return -ENOMEM;
185
186 /* strip trailing whitespace, keep prefixing whitespace */
187 iov[n++] = IOVEC_MAKE_STRING(delete_trailing_chars(TAKE_PTR(buffer), NULL));
188 }
189
190 *ret_iov = TAKE_PTR(iov);
191 *ret_n_iov = n;
192 return 0;
193 }
194
195 _public_ int sd_journal_send(const char *format, ...) {
196 struct iovec *iov = NULL;
197 size_t n_iov = 0;
198 va_list ap;
199 int r;
200
201 CLEANUP_ARRAY(iov, n_iov, iovec_array_free);
202
203 va_start(ap, format);
204 r = fill_iovec_sprintf(format, ap, 0, &iov, &n_iov);
205 va_end(ap);
206 if (r < 0)
207 return r;
208
209 return sd_journal_sendv(iov, n_iov);
210 }
211
212 _public_ int sd_journal_sendv(const struct iovec *iov, int n) {
213 PROTECT_ERRNO;
214 int fd, r;
215 _cleanup_close_ int buffer_fd = -EBADF;
216 struct iovec *w;
217 uint64_t *l;
218 int i, j = 0;
219 static const union sockaddr_union sa = {
220 .un.sun_family = AF_UNIX,
221 .un.sun_path = "/run/systemd/journal/socket",
222 };
223 struct msghdr mh = {
224 .msg_name = (struct sockaddr*) &sa.sa,
225 .msg_namelen = sockaddr_un_len(&sa.un),
226 };
227 ssize_t k;
228 bool have_syslog_identifier = false;
229
230 assert_return(iov, -EINVAL);
231 assert_return(n > 0, -EINVAL);
232
233 w = newa(struct iovec, n * 5 + 3);
234 l = newa(uint64_t, n);
235
236 for (i = 0; i < n; i++) {
237 char *c, *nl;
238
239 if (_unlikely_(!iov[i].iov_base || iov[i].iov_len <= 1))
240 return -EINVAL;
241
242 c = memchr(iov[i].iov_base, '=', iov[i].iov_len);
243 if (_unlikely_(!c || c == iov[i].iov_base))
244 return -EINVAL;
245
246 have_syslog_identifier = have_syslog_identifier ||
247 (c == (char *) iov[i].iov_base + 17 &&
248 startswith(iov[i].iov_base, "SYSLOG_IDENTIFIER"));
249
250 nl = memchr(iov[i].iov_base, '\n', iov[i].iov_len);
251 if (nl) {
252 if (_unlikely_(nl < c))
253 return -EINVAL;
254
255 /* Already includes a newline? Bummer, then
256 * let's write the variable name, then a
257 * newline, then the size (64-bit LE), followed
258 * by the data and a final newline */
259
260 w[j++] = IOVEC_MAKE(iov[i].iov_base, c - (char*) iov[i].iov_base);
261 w[j++] = IOVEC_MAKE_STRING("\n");
262
263 l[i] = htole64(iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
264 w[j++] = IOVEC_MAKE(&l[i], sizeof(uint64_t));
265
266 w[j++] = IOVEC_MAKE(c + 1, iov[i].iov_len - (c - (char*) iov[i].iov_base) - 1);
267 } else
268 /* Nothing special? Then just add the line and
269 * append a newline */
270 w[j++] = iov[i];
271
272 w[j++] = IOVEC_MAKE_STRING("\n");
273 }
274
275 if (!have_syslog_identifier &&
276 string_is_safe(program_invocation_short_name, /* flags= */ 0)) {
277
278 /* Implicitly add program_invocation_short_name, if it is not set explicitly. We only do this
279 * for program_invocation_short_name, and nothing else since everything else is much nicer to
280 * retrieve from the outside. */
281
282 w[j++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=");
283 w[j++] = IOVEC_MAKE_STRING(program_invocation_short_name);
284 w[j++] = IOVEC_MAKE_STRING("\n");
285 }
286
287 fd = journal_fd();
288 if (_unlikely_(fd < 0))
289 return fd;
290
291 mh.msg_iov = w;
292 mh.msg_iovlen = j;
293
294 k = sendmsg(fd, &mh, MSG_NOSIGNAL);
295 if (k >= 0)
296 return 0;
297
298 /* Fail silently if the journal is not available */
299 if (errno == ENOENT)
300 return 0;
301
302 if (!IN_SET(errno, EMSGSIZE, ENOBUFS, EAGAIN))
303 return -errno;
304
305 /* Message doesn't fit... Let's dump the data in a memfd or temporary file and just pass a file
306 * descriptor of it to the other side. */
307 buffer_fd = memfd_new_full("journal-data", MFD_ALLOW_SEALING);
308 if (buffer_fd < 0)
309 return buffer_fd;
310
311 n = writev(buffer_fd, w, j);
312 if (n < 0)
313 return -errno;
314
315 r = memfd_set_sealed(buffer_fd);
316 if (r < 0)
317 return r;
318
319 r = send_one_fd_sa(fd, buffer_fd, mh.msg_name, mh.msg_namelen, 0);
320 if (r == -ENOENT)
321 /* Fail silently if the journal is not available */
322 return 0;
323 return r;
324 }
325
326 static int fill_iovec_perror_and_send(const char *message, int skip, struct iovec iov[]) {
327 PROTECT_ERRNO;
328 size_t n, k;
329
330 k = isempty(message) ? 0 : strlen(message) + 2;
331 n = 8 + k + 256 + 1;
332
333 for (;;) {
334 char buffer[n];
335 char* j;
336
337 errno = 0;
338 j = strerror_r(_saved_errno_, buffer + 8 + k, n - 8 - k);
339 if (errno == 0) {
340 char error[STRLEN("ERRNO=") + DECIMAL_STR_MAX(int) + 1];
341
342 if (j != buffer + 8 + k)
343 memmove(buffer + 8 + k, j, strlen(j)+1);
344
345 memcpy(buffer, "MESSAGE=", 8);
346
347 if (k > 0) {
348 memcpy(buffer + 8, message, k - 2);
349 memcpy(buffer + 8 + k - 2, ": ", 2);
350 }
351
352 xsprintf(error, "ERRNO=%i", _saved_errno_);
353
354 assert_cc(3 == LOG_ERR);
355 iov[skip+0] = IOVEC_MAKE_STRING("PRIORITY=3");
356 iov[skip+1] = IOVEC_MAKE_STRING(buffer);
357 iov[skip+2] = IOVEC_MAKE_STRING(error);
358
359 return sd_journal_sendv(iov, skip + 3);
360 }
361
362 if (errno != ERANGE)
363 return -errno;
364
365 n *= 2;
366 }
367 }
368
369 _public_ int sd_journal_perror(const char *message) {
370 struct iovec iovec[3];
371
372 return fill_iovec_perror_and_send(message, 0, iovec);
373 }
374
375 _public_ int sd_journal_stream_fd_with_namespace(
376 const char *name_space,
377 const char *identifier,
378 int priority,
379 int level_prefix) {
380
381 _cleanup_close_ int fd = -EBADF;
382 const char *path;
383 int r;
384
385 assert_return(priority >= 0, -EINVAL);
386 assert_return(priority <= 7, -EINVAL);
387
388 if (name_space) {
389 /* If $LOG_NAMESPACE is set, we're already placed in a mountns with /run/systemd/journal/
390 * being a bind mount for the journald namespace instance, in which case we shall go by
391 * the standard journal socket path. */
392 const char *env = secure_getenv("LOG_NAMESPACE");
393 if (env) {
394 if (!streq(name_space, env))
395 return -EREMOTE;
396
397 name_space = NULL;
398 }
399 }
400
401 path = journal_stream_path(name_space);
402 if (!path)
403 return -EINVAL;
404
405 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
406 if (fd < 0)
407 return -errno;
408
409 r = connect_unix_path(fd, AT_FDCWD, path);
410 if (r < 0)
411 return r;
412
413 if (shutdown(fd, SHUT_RD) < 0)
414 return -errno;
415
416 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
417
418 identifier = strempty(identifier);
419
420 char *header;
421 size_t l;
422
423 l = strlen(identifier);
424 header = newa(char, l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
425
426 memcpy(header, identifier, l);
427 header[l++] = '\n';
428 header[l++] = '\n'; /* unit id */
429 header[l++] = '0' + priority;
430 header[l++] = '\n';
431 header[l++] = '0' + !!level_prefix;
432 header[l++] = '\n';
433 header[l++] = '0';
434 header[l++] = '\n';
435 header[l++] = '0';
436 header[l++] = '\n';
437 header[l++] = '0';
438 header[l++] = '\n';
439
440 r = loop_write(fd, header, l);
441 if (r < 0)
442 return r;
443
444 return TAKE_FD(fd);
445 }
446
447 _public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
448 return sd_journal_stream_fd_with_namespace(NULL, identifier, priority, level_prefix);
449 }
450
451 _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
452 int r;
453 va_list ap;
454
455 va_start(ap, format);
456 r = sd_journal_printv_with_location(priority, file, line, func, format, ap);
457 va_end(ap);
458
459 return r;
460 }
461
462 _public_ int sd_journal_printv_with_location(int priority, const char *file, const char *line, const char *func, const char *format, va_list ap) {
463 char p[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int) + 1];
464 char sbuf[LINE_MAX + 8] = "MESSAGE=";
465 struct iovec iov[5];
466 char *f;
467 int len;
468 char *buffer = sbuf;
469 va_list aq;
470
471 assert_return(priority >= 0, -EINVAL);
472 assert_return(priority <= 7, -EINVAL);
473 assert_return(format, -EINVAL);
474
475 xsprintf(p, "PRIORITY=%i", LOG_PRI(priority));
476
477 va_copy(aq, ap);
478 len = vsnprintf(buffer + 8, LINE_MAX, format, aq);
479 va_end(aq);
480
481 if (len >= (int)LONG_LINE_MAX - 8)
482 return -ENOBUFS;
483
484 /* Allocate large buffer to accommodate big message */
485 if (len >= LINE_MAX) {
486 buffer = alloca_safe(len + 9);
487 memcpy(buffer, "MESSAGE=", 8);
488 assert_se(vsnprintf(buffer + 8, len + 1, format, ap) == len);
489 }
490
491 /* Strip trailing whitespace, keep prefixing whitespace */
492 (void) strstrip(buffer);
493
494 /* Suppress empty lines */
495 if (isempty(buffer + 8))
496 return 0;
497
498 /* func is initialized from __func__ which is not a macro, but
499 * a static const char[], hence cannot easily be prefixed with
500 * CODE_FUNC=, hence let's do it manually here. */
501 ALLOCA_CODE_FUNC(f, func);
502
503 iov[0] = IOVEC_MAKE_STRING(buffer);
504 iov[1] = IOVEC_MAKE_STRING(p);
505 iov[2] = IOVEC_MAKE_STRING(file);
506 iov[3] = IOVEC_MAKE_STRING(line);
507 iov[4] = IOVEC_MAKE_STRING(f);
508
509 return sd_journal_sendv(iov, ELEMENTSOF(iov));
510 }
511
512 _public_ int sd_journal_send_with_location(const char *file, const char *line, const char *func, const char *format, ...) {
513 struct iovec *iov = NULL;
514 size_t n_iov = 0;
515 va_list ap;
516 char *f;
517 int r;
518
519 CLEANUP_ARRAY(iov, n_iov, iovec_array_free);
520
521 va_start(ap, format);
522 r = fill_iovec_sprintf(format, ap, 3, &iov, &n_iov);
523 va_end(ap);
524 if (r < 0)
525 return r;
526
527 ALLOCA_CODE_FUNC(f, func);
528
529 iov[0] = IOVEC_MAKE_STRING(file);
530 iov[1] = IOVEC_MAKE_STRING(line);
531 iov[2] = IOVEC_MAKE_STRING(f);
532
533 r = sd_journal_sendv(iov, n_iov);
534
535 iov[0] = iov[1] = iov[2] = (struct iovec) {};
536
537 return r;
538 }
539
540 _public_ int sd_journal_sendv_with_location(
541 const char *file, const char *line,
542 const char *func,
543 const struct iovec *iov, int n) {
544
545 struct iovec *niov;
546 char *f;
547
548 assert_return(iov, -EINVAL);
549 assert_return(n > 0, -EINVAL);
550
551 niov = newa(struct iovec, n + 3);
552 memcpy(niov, iov, sizeof(struct iovec) * n);
553
554 ALLOCA_CODE_FUNC(f, func);
555
556 niov[n++] = IOVEC_MAKE_STRING(file);
557 niov[n++] = IOVEC_MAKE_STRING(line);
558 niov[n++] = IOVEC_MAKE_STRING(f);
559
560 return sd_journal_sendv(niov, n);
561 }
562
563 _public_ int sd_journal_perror_with_location(
564 const char *file, const char *line,
565 const char *func,
566 const char *message) {
567
568 struct iovec iov[6];
569 char *f;
570
571 ALLOCA_CODE_FUNC(f, func);
572
573 iov[0] = IOVEC_MAKE_STRING(file);
574 iov[1] = IOVEC_MAKE_STRING(line);
575 iov[2] = IOVEC_MAKE_STRING(f);
576
577 return fill_iovec_perror_and_send(message, 3, iov);
578 }