]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5899f3b7 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5899f3b7 LP |
22 | #include <stdarg.h> |
23 | #include <stdio.h> | |
c9b97d2a | 24 | #include <errno.h> |
16801e90 LP |
25 | #include <unistd.h> |
26 | #include <fcntl.h> | |
27 | #include <sys/socket.h> | |
28 | #include <sys/un.h> | |
5ba081b0 | 29 | #include <stddef.h> |
5899f3b7 LP |
30 | |
31 | #include "log.h" | |
16801e90 LP |
32 | #include "util.h" |
33 | #include "macro.h" | |
5ba081b0 | 34 | #include "socket-util.h" |
5899f3b7 | 35 | |
bb99a35a LP |
36 | #define SNDBUF_SIZE (8*1024*1024) |
37 | ||
16801e90 | 38 | static LogTarget log_target = LOG_TARGET_CONSOLE; |
bbe63281 | 39 | static int log_max_level = LOG_INFO; |
16801e90 | 40 | |
843d2643 | 41 | static int console_fd = STDERR_FILENO; |
16801e90 LP |
42 | static int syslog_fd = -1; |
43 | static int kmsg_fd = -1; | |
5ba081b0 | 44 | static int journal_fd = -1; |
16801e90 | 45 | |
c31e1495 LP |
46 | static bool syslog_is_stream = false; |
47 | ||
bbe63281 LP |
48 | static bool show_color = false; |
49 | static bool show_location = false; | |
50 | ||
35b8ca3a | 51 | /* Akin to glibc's __abort_msg; which is private and we hence cannot |
185986c6 LP |
52 | * use here. */ |
53 | static char *log_abort_msg = NULL; | |
54 | ||
843d2643 LP |
55 | void log_close_console(void) { |
56 | ||
57 | if (console_fd < 0) | |
58 | return; | |
16801e90 | 59 | |
c8513d54 LP |
60 | if (getpid() == 1) { |
61 | if (console_fd >= 3) | |
62 | close_nointr_nofail(console_fd); | |
63 | ||
843d2643 | 64 | console_fd = -1; |
16801e90 LP |
65 | } |
66 | } | |
67 | ||
843d2643 | 68 | static int log_open_console(void) { |
16801e90 | 69 | |
843d2643 | 70 | if (console_fd >= 0) |
16801e90 | 71 | return 0; |
843d2643 LP |
72 | |
73 | if (getpid() == 1) { | |
74 | ||
5ba081b0 LP |
75 | console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
76 | if (console_fd < 0) { | |
843d2643 LP |
77 | log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd)); |
78 | return console_fd; | |
79 | } | |
80 | ||
35b8ca3a | 81 | log_debug("Successfully opened /dev/console for logging."); |
843d2643 LP |
82 | } else |
83 | console_fd = STDERR_FILENO; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | void log_close_kmsg(void) { | |
89 | ||
90 | if (kmsg_fd < 0) | |
91 | return; | |
92 | ||
93 | close_nointr_nofail(kmsg_fd); | |
94 | kmsg_fd = -1; | |
95 | } | |
96 | ||
97 | static int log_open_kmsg(void) { | |
16801e90 LP |
98 | |
99 | if (kmsg_fd >= 0) | |
100 | return 0; | |
101 | ||
674f8283 LP |
102 | kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
103 | if (kmsg_fd < 0) { | |
31a7034d | 104 | log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno)); |
16801e90 | 105 | return -errno; |
c9b80453 | 106 | } |
16801e90 | 107 | |
35b8ca3a | 108 | log_debug("Successfully opened /dev/kmsg for logging."); |
0dae83f9 | 109 | |
16801e90 LP |
110 | return 0; |
111 | } | |
112 | ||
113 | void log_close_syslog(void) { | |
114 | ||
843d2643 LP |
115 | if (syslog_fd < 0) |
116 | return; | |
117 | ||
118 | close_nointr_nofail(syslog_fd); | |
119 | syslog_fd = -1; | |
16801e90 LP |
120 | } |
121 | ||
c31e1495 | 122 | static int create_log_socket(int type) { |
c31e1495 LP |
123 | int fd; |
124 | ||
632117b7 LP |
125 | /* All output to the syslog/journal fds we do asynchronously, |
126 | * and if the buffers are full we just drop the messages */ | |
5ba081b0 | 127 | |
632117b7 | 128 | fd = socket(AF_UNIX, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
5ba081b0 | 129 | if (fd < 0) |
c31e1495 LP |
130 | return -errno; |
131 | ||
bb99a35a LP |
132 | fd_inc_sndbuf(fd, SNDBUF_SIZE); |
133 | ||
c31e1495 LP |
134 | return fd; |
135 | } | |
136 | ||
843d2643 | 137 | static int log_open_syslog(void) { |
5ba081b0 | 138 | union sockaddr_union sa; |
16801e90 LP |
139 | int r; |
140 | ||
16801e90 LP |
141 | if (syslog_fd >= 0) |
142 | return 0; | |
143 | ||
16801e90 LP |
144 | zero(sa); |
145 | sa.un.sun_family = AF_UNIX; | |
146 | strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path)); | |
147 | ||
5ba081b0 LP |
148 | syslog_fd = create_log_socket(SOCK_DGRAM); |
149 | if (syslog_fd < 0) { | |
150 | r = syslog_fd; | |
843d2643 | 151 | goto fail; |
16801e90 LP |
152 | } |
153 | ||
c31e1495 LP |
154 | if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) { |
155 | close_nointr_nofail(syslog_fd); | |
156 | ||
157 | /* Some legacy syslog systems still use stream | |
158 | * sockets. They really shouldn't. But what can we | |
159 | * do... */ | |
5ba081b0 LP |
160 | syslog_fd = create_log_socket(SOCK_STREAM); |
161 | if (syslog_fd < 0) { | |
162 | r = syslog_fd; | |
c31e1495 LP |
163 | goto fail; |
164 | } | |
165 | ||
166 | if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) { | |
167 | r = -errno; | |
168 | goto fail; | |
169 | } | |
170 | ||
171 | syslog_is_stream = true; | |
172 | } else | |
173 | syslog_is_stream = false; | |
174 | ||
35b8ca3a | 175 | log_debug("Successfully opened syslog for logging."); |
0dae83f9 | 176 | |
16801e90 | 177 | return 0; |
843d2643 LP |
178 | |
179 | fail: | |
180 | log_close_syslog(); | |
31a7034d | 181 | log_debug("Failed to open syslog for logging: %s", strerror(-r)); |
843d2643 LP |
182 | return r; |
183 | } | |
184 | ||
5ba081b0 LP |
185 | void log_close_journal(void) { |
186 | ||
187 | if (journal_fd < 0) | |
188 | return; | |
189 | ||
190 | close_nointr_nofail(journal_fd); | |
191 | journal_fd = -1; | |
192 | } | |
193 | ||
194 | static int log_open_journal(void) { | |
195 | union sockaddr_union sa; | |
196 | int r; | |
197 | ||
198 | if (journal_fd >= 0) | |
199 | return 0; | |
200 | ||
201 | journal_fd = create_log_socket(SOCK_DGRAM); | |
202 | if (journal_fd < 0) { | |
203 | r = journal_fd; | |
204 | goto fail; | |
205 | } | |
206 | ||
207 | zero(sa); | |
208 | sa.un.sun_family = AF_UNIX; | |
209 | strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path)); | |
210 | ||
211 | if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) { | |
212 | r = -errno; | |
213 | goto fail; | |
214 | } | |
215 | ||
216 | log_debug("Successfully opened journal for logging."); | |
217 | ||
218 | return 0; | |
219 | ||
220 | fail: | |
221 | log_close_journal(); | |
222 | log_debug("Failed to open journal for logging: %s", strerror(-r)); | |
223 | return r; | |
224 | } | |
225 | ||
843d2643 LP |
226 | int log_open(void) { |
227 | int r; | |
228 | ||
229 | /* If we don't use the console we close it here, to not get | |
230 | * killed by SAK. If we don't use syslog we close it here so | |
231 | * that we are not confused by somebody deleting the socket in | |
232 | * the fs. If we don't use /dev/kmsg we still keep it open, | |
233 | * because there is no reason to close it. */ | |
234 | ||
9fae33d2 | 235 | if (log_target == LOG_TARGET_NULL) { |
5ba081b0 | 236 | log_close_journal(); |
9fae33d2 LP |
237 | log_close_syslog(); |
238 | log_close_console(); | |
239 | return 0; | |
240 | } | |
241 | ||
bb7df0da LP |
242 | if (log_target != LOG_TARGET_AUTO || |
243 | getpid() == 1 || | |
f41c094c | 244 | isatty(STDERR_FILENO) <= 0) { |
bb7df0da LP |
245 | |
246 | if (log_target == LOG_TARGET_AUTO || | |
5ba081b0 LP |
247 | log_target == LOG_TARGET_JOURNAL_OR_KMSG || |
248 | log_target == LOG_TARGET_JOURNAL) { | |
249 | r = log_open_journal(); | |
250 | if (r >= 0) { | |
251 | log_close_syslog(); | |
bb7df0da LP |
252 | log_close_console(); |
253 | return r; | |
254 | } | |
5ba081b0 LP |
255 | } |
256 | ||
257 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || | |
258 | log_target == LOG_TARGET_SYSLOG) { | |
259 | r = log_open_syslog(); | |
260 | if (r >= 0) { | |
261 | log_close_journal(); | |
262 | log_close_console(); | |
263 | return r; | |
264 | } | |
265 | } | |
266 | ||
bb7df0da | 267 | if (log_target == LOG_TARGET_AUTO || |
5ba081b0 | 268 | log_target == LOG_TARGET_JOURNAL_OR_KMSG || |
bb7df0da | 269 | log_target == LOG_TARGET_SYSLOG_OR_KMSG || |
5ba081b0 LP |
270 | log_target == LOG_TARGET_KMSG) { |
271 | r = log_open_kmsg(); | |
272 | if (r >= 0) { | |
273 | log_close_journal(); | |
bb7df0da LP |
274 | log_close_syslog(); |
275 | log_close_console(); | |
276 | return r; | |
277 | } | |
5ba081b0 | 278 | } |
bb7df0da | 279 | } |
843d2643 | 280 | |
5ba081b0 | 281 | log_close_journal(); |
843d2643 | 282 | log_close_syslog(); |
dcdf86bb LP |
283 | |
284 | /* Get the real /dev/console if we are PID=1, hence reopen */ | |
285 | log_close_console(); | |
843d2643 | 286 | return log_open_console(); |
16801e90 LP |
287 | } |
288 | ||
289 | void log_set_target(LogTarget target) { | |
290 | assert(target >= 0); | |
291 | assert(target < _LOG_TARGET_MAX); | |
292 | ||
293 | log_target = target; | |
294 | } | |
295 | ||
871e5809 | 296 | void log_close(void) { |
5ba081b0 | 297 | log_close_journal(); |
871e5809 | 298 | log_close_syslog(); |
5ba081b0 LP |
299 | log_close_kmsg(); |
300 | log_close_console(); | |
871e5809 LP |
301 | } |
302 | ||
4d8a7798 | 303 | void log_forget_fds(void) { |
5ba081b0 | 304 | console_fd = kmsg_fd = syslog_fd = journal_fd = -1; |
4d8a7798 MS |
305 | } |
306 | ||
16801e90 LP |
307 | void log_set_max_level(int level) { |
308 | assert((level & LOG_PRIMASK) == level); | |
309 | ||
310 | log_max_level = level; | |
311 | } | |
312 | ||
843d2643 | 313 | static int write_to_console( |
20c03b7b LP |
314 | int level, |
315 | const char*file, | |
316 | int line, | |
317 | const char *func, | |
318 | const char *buffer) { | |
5899f3b7 | 319 | |
843d2643 LP |
320 | char location[64]; |
321 | struct iovec iovec[5]; | |
322 | unsigned n = 0; | |
323 | bool highlight; | |
5899f3b7 | 324 | |
843d2643 LP |
325 | if (console_fd < 0) |
326 | return 0; | |
327 | ||
bbe63281 | 328 | highlight = LOG_PRI(level) <= LOG_ERR && show_color; |
843d2643 LP |
329 | |
330 | zero(iovec); | |
674f8283 LP |
331 | |
332 | if (show_location) { | |
333 | snprintf(location, sizeof(location), "(%s:%u) ", file, line); | |
334 | char_array_0(location); | |
bbe63281 | 335 | IOVEC_SET_STRING(iovec[n++], location); |
674f8283 LP |
336 | } |
337 | ||
843d2643 | 338 | if (highlight) |
c1072ea0 | 339 | IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON); |
843d2643 LP |
340 | IOVEC_SET_STRING(iovec[n++], buffer); |
341 | if (highlight) | |
61cbdc4b | 342 | IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF); |
843d2643 LP |
343 | IOVEC_SET_STRING(iovec[n++], "\n"); |
344 | ||
345 | if (writev(console_fd, iovec, n) < 0) | |
346 | return -errno; | |
5899f3b7 | 347 | |
843d2643 | 348 | return 1; |
16801e90 | 349 | } |
5899f3b7 | 350 | |
16801e90 LP |
351 | static int write_to_syslog( |
352 | int level, | |
353 | const char*file, | |
354 | int line, | |
355 | const char *func, | |
843d2643 | 356 | const char *buffer) { |
16801e90 LP |
357 | |
358 | char header_priority[16], header_time[64], header_pid[16]; | |
16801e90 LP |
359 | struct iovec iovec[5]; |
360 | struct msghdr msghdr; | |
361 | time_t t; | |
362 | struct tm *tm; | |
363 | ||
364 | if (syslog_fd < 0) | |
843d2643 | 365 | return 0; |
16801e90 | 366 | |
29db5834 | 367 | snprintf(header_priority, sizeof(header_priority), "<%i>", level); |
16801e90 LP |
368 | char_array_0(header_priority); |
369 | ||
370 | t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC); | |
371 | if (!(tm = localtime(&t))) | |
372 | return -EINVAL; | |
373 | ||
374 | if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0) | |
375 | return -EINVAL; | |
376 | ||
bb00e604 | 377 | snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid()); |
16801e90 LP |
378 | char_array_0(header_pid); |
379 | ||
16801e90 LP |
380 | zero(iovec); |
381 | IOVEC_SET_STRING(iovec[0], header_priority); | |
382 | IOVEC_SET_STRING(iovec[1], header_time); | |
5b6319dc | 383 | IOVEC_SET_STRING(iovec[2], program_invocation_short_name); |
16801e90 LP |
384 | IOVEC_SET_STRING(iovec[3], header_pid); |
385 | IOVEC_SET_STRING(iovec[4], buffer); | |
386 | ||
c899f8c6 | 387 | /* When using syslog via SOCK_STREAM separate the messages by NUL chars */ |
c31e1495 LP |
388 | if (syslog_is_stream) |
389 | iovec[4].iov_len++; | |
390 | ||
16801e90 LP |
391 | zero(msghdr); |
392 | msghdr.msg_iov = iovec; | |
393 | msghdr.msg_iovlen = ELEMENTSOF(iovec); | |
394 | ||
c31e1495 LP |
395 | for (;;) { |
396 | ssize_t n; | |
397 | ||
8f7f7a1b MS |
398 | n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL); |
399 | if (n < 0) | |
c31e1495 LP |
400 | return -errno; |
401 | ||
402 | if (!syslog_is_stream || | |
403 | (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec))) | |
404 | break; | |
405 | ||
406 | IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n); | |
407 | } | |
16801e90 | 408 | |
843d2643 | 409 | return 1; |
16801e90 LP |
410 | } |
411 | ||
412 | static int write_to_kmsg( | |
413 | int level, | |
414 | const char*file, | |
415 | int line, | |
416 | const char *func, | |
843d2643 | 417 | const char *buffer) { |
16801e90 LP |
418 | |
419 | char header_priority[16], header_pid[16]; | |
16801e90 LP |
420 | struct iovec iovec[5]; |
421 | ||
422 | if (kmsg_fd < 0) | |
843d2643 | 423 | return 0; |
16801e90 | 424 | |
29db5834 | 425 | snprintf(header_priority, sizeof(header_priority), "<%i>", level); |
16801e90 LP |
426 | char_array_0(header_priority); |
427 | ||
bb00e604 | 428 | snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid()); |
16801e90 LP |
429 | char_array_0(header_pid); |
430 | ||
16801e90 LP |
431 | zero(iovec); |
432 | IOVEC_SET_STRING(iovec[0], header_priority); | |
5b6319dc | 433 | IOVEC_SET_STRING(iovec[1], program_invocation_short_name); |
16801e90 LP |
434 | IOVEC_SET_STRING(iovec[2], header_pid); |
435 | IOVEC_SET_STRING(iovec[3], buffer); | |
843d2643 | 436 | IOVEC_SET_STRING(iovec[4], "\n"); |
16801e90 LP |
437 | |
438 | if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) | |
439 | return -errno; | |
440 | ||
843d2643 | 441 | return 1; |
16801e90 LP |
442 | } |
443 | ||
5ba081b0 LP |
444 | static int write_to_journal( |
445 | int level, | |
446 | const char*file, | |
447 | int line, | |
448 | const char *func, | |
449 | const char *buffer) { | |
450 | ||
451 | char header[LINE_MAX]; | |
452 | struct iovec iovec[3]; | |
453 | struct msghdr mh; | |
454 | ||
455 | if (journal_fd < 0) | |
456 | return 0; | |
457 | ||
458 | snprintf(header, sizeof(header), | |
459 | "PRIORITY=%i\n" | |
460 | "CODE_FILE=%s\n" | |
461 | "CODE_LINE=%i\n" | |
462 | "CODE_FUNCTION=%s\n" | |
463 | "MESSAGE=", | |
464 | LOG_PRI(level), | |
465 | file, | |
466 | line, | |
467 | func); | |
468 | ||
469 | char_array_0(header); | |
470 | ||
471 | zero(iovec); | |
472 | IOVEC_SET_STRING(iovec[0], header); | |
473 | IOVEC_SET_STRING(iovec[1], buffer); | |
474 | IOVEC_SET_STRING(iovec[2], "\n"); | |
475 | ||
476 | zero(mh); | |
477 | mh.msg_iov = iovec; | |
478 | mh.msg_iovlen = ELEMENTSOF(iovec); | |
479 | ||
480 | if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0) | |
481 | return -errno; | |
482 | ||
483 | return 1; | |
484 | } | |
485 | ||
843d2643 LP |
486 | static int log_dispatch( |
487 | int level, | |
488 | const char*file, | |
489 | int line, | |
490 | const char *func, | |
9726b29e | 491 | char *buffer) { |
843d2643 | 492 | |
9726b29e | 493 | int r = 0; |
843d2643 | 494 | |
9fae33d2 LP |
495 | if (log_target == LOG_TARGET_NULL) |
496 | return 0; | |
497 | ||
29db5834 | 498 | /* Patch in LOG_DAEMON facility if necessary */ |
7d76f312 LP |
499 | if ((level & LOG_FACMASK) == 0) |
500 | level = LOG_DAEMON | LOG_PRI(level); | |
29db5834 | 501 | |
9726b29e LP |
502 | do { |
503 | char *e; | |
9499b235 | 504 | int k = 0; |
843d2643 | 505 | |
9726b29e | 506 | buffer += strspn(buffer, NEWLINE); |
843d2643 | 507 | |
9726b29e LP |
508 | if (buffer[0] == 0) |
509 | break; | |
843d2643 | 510 | |
9726b29e LP |
511 | if ((e = strpbrk(buffer, NEWLINE))) |
512 | *(e++) = 0; | |
843d2643 | 513 | |
bb7df0da | 514 | if (log_target == LOG_TARGET_AUTO || |
5ba081b0 LP |
515 | log_target == LOG_TARGET_JOURNAL_OR_KMSG || |
516 | log_target == LOG_TARGET_JOURNAL) { | |
517 | ||
518 | k = write_to_journal(level, file, line, func, buffer); | |
519 | if (k < 0) { | |
520 | if (k != -EAGAIN) | |
521 | log_close_journal(); | |
522 | log_open_kmsg(); | |
523 | } else if (k > 0) | |
524 | r++; | |
525 | } | |
526 | ||
527 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || | |
9726b29e LP |
528 | log_target == LOG_TARGET_SYSLOG) { |
529 | ||
8f7f7a1b MS |
530 | k = write_to_syslog(level, file, line, func, buffer); |
531 | if (k < 0) { | |
532 | if (k != -EAGAIN) | |
533 | log_close_syslog(); | |
9726b29e | 534 | log_open_kmsg(); |
9499b235 | 535 | } else if (k > 0) |
9726b29e LP |
536 | r++; |
537 | } | |
538 | ||
9499b235 | 539 | if (k <= 0 && |
bb7df0da LP |
540 | (log_target == LOG_TARGET_AUTO || |
541 | log_target == LOG_TARGET_SYSLOG_OR_KMSG || | |
9499b235 | 542 | log_target == LOG_TARGET_KMSG)) { |
9726b29e | 543 | |
8f7f7a1b MS |
544 | k = write_to_kmsg(level, file, line, func, buffer); |
545 | if (k < 0) { | |
9726b29e LP |
546 | log_close_kmsg(); |
547 | log_open_console(); | |
9499b235 | 548 | } else if (k > 0) |
9726b29e LP |
549 | r++; |
550 | } | |
551 | ||
8f7f7a1b MS |
552 | if (k <= 0) { |
553 | k = write_to_console(level, file, line, func, buffer); | |
554 | if (k < 0) | |
555 | return k; | |
556 | } | |
9726b29e LP |
557 | |
558 | buffer = e; | |
559 | } while (buffer); | |
560 | ||
561 | return r; | |
843d2643 LP |
562 | } |
563 | ||
2149e37c LP |
564 | int log_dump_internal( |
565 | int level, | |
566 | const char*file, | |
567 | int line, | |
568 | const char *func, | |
569 | char *buffer) { | |
570 | ||
571 | int saved_errno, r; | |
572 | ||
573 | /* This modifies the buffer... */ | |
574 | ||
575 | if (_likely_(LOG_PRI(level) > log_max_level)) | |
576 | return 0; | |
577 | ||
578 | saved_errno = errno; | |
579 | r = log_dispatch(level, file, line, func, buffer); | |
580 | errno = saved_errno; | |
581 | ||
582 | return r; | |
583 | } | |
584 | ||
17a94911 | 585 | int log_metav( |
16801e90 LP |
586 | int level, |
587 | const char*file, | |
588 | int line, | |
589 | const char *func, | |
17a94911 LP |
590 | const char *format, |
591 | va_list ap) { | |
16801e90 | 592 | |
addab137 | 593 | char buffer[LINE_MAX]; |
843d2643 | 594 | int saved_errno, r; |
16801e90 | 595 | |
93a46b0b | 596 | if (_likely_(LOG_PRI(level) > log_max_level)) |
843d2643 | 597 | return 0; |
16801e90 LP |
598 | |
599 | saved_errno = errno; | |
843d2643 | 600 | vsnprintf(buffer, sizeof(buffer), format, ap); |
843d2643 LP |
601 | char_array_0(buffer); |
602 | ||
603 | r = log_dispatch(level, file, line, func, buffer); | |
185986c6 | 604 | errno = saved_errno; |
843d2643 LP |
605 | |
606 | return r; | |
185986c6 | 607 | } |
16801e90 | 608 | |
17a94911 LP |
609 | int log_meta( |
610 | int level, | |
611 | const char*file, | |
612 | int line, | |
613 | const char *func, | |
614 | const char *format, ...) { | |
615 | ||
616 | int r; | |
617 | va_list ap; | |
618 | ||
619 | va_start(ap, format); | |
620 | r = log_metav(level, file, line, func, format, ap); | |
621 | va_end(ap); | |
622 | ||
623 | return r; | |
624 | } | |
625 | ||
a3f914b2 MS |
626 | #pragma GCC diagnostic push |
627 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" | |
b7f33638 | 628 | _noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) { |
addab137 | 629 | static char buffer[LINE_MAX]; |
185986c6 | 630 | |
b7f33638 | 631 | snprintf(buffer, sizeof(buffer), format, text, file, line, func); |
185986c6 LP |
632 | |
633 | char_array_0(buffer); | |
634 | log_abort_msg = buffer; | |
635 | ||
843d2643 | 636 | log_dispatch(LOG_CRIT, file, line, func, buffer); |
185986c6 | 637 | abort(); |
5899f3b7 | 638 | } |
a3f914b2 | 639 | #pragma GCC diagnostic pop |
34f0e866 | 640 | |
6a39419f | 641 | _noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) { |
b7f33638 MS |
642 | log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting."); |
643 | } | |
644 | ||
6a39419f | 645 | _noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) { |
b7f33638 MS |
646 | log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); |
647 | } | |
648 | ||
34f0e866 LP |
649 | int log_set_target_from_string(const char *e) { |
650 | LogTarget t; | |
651 | ||
5ba081b0 LP |
652 | t = log_target_from_string(e); |
653 | if (t < 0) | |
34f0e866 LP |
654 | return -EINVAL; |
655 | ||
656 | log_set_target(t); | |
657 | return 0; | |
658 | } | |
659 | ||
660 | int log_set_max_level_from_string(const char *e) { | |
661 | int t; | |
662 | ||
5ba081b0 LP |
663 | t = log_level_from_string(e); |
664 | if (t < 0) | |
665 | return t; | |
34f0e866 LP |
666 | |
667 | log_set_max_level(t); | |
668 | return 0; | |
669 | } | |
670 | ||
671 | void log_parse_environment(void) { | |
672 | const char *e; | |
673 | ||
674 | if ((e = getenv("SYSTEMD_LOG_TARGET"))) | |
675 | if (log_set_target_from_string(e) < 0) | |
676 | log_warning("Failed to parse log target %s. Ignoring.", e); | |
677 | ||
678 | if ((e = getenv("SYSTEMD_LOG_LEVEL"))) | |
679 | if (log_set_max_level_from_string(e) < 0) | |
680 | log_warning("Failed to parse log level %s. Ignoring.", e); | |
bbe63281 | 681 | |
541d6159 | 682 | if ((e = getenv("SYSTEMD_LOG_COLOR"))) |
bbe63281 LP |
683 | if (log_show_color_from_string(e) < 0) |
684 | log_warning("Failed to parse bool %s. Ignoring.", e); | |
685 | ||
dcdf86bb | 686 | if ((e = getenv("SYSTEMD_LOG_LOCATION"))) |
bbe63281 LP |
687 | if (log_show_location_from_string(e) < 0) |
688 | log_warning("Failed to parse bool %s. Ignoring.", e); | |
34f0e866 LP |
689 | } |
690 | ||
1adf1049 LP |
691 | LogTarget log_get_target(void) { |
692 | return log_target; | |
693 | } | |
694 | ||
695 | int log_get_max_level(void) { | |
696 | return log_max_level; | |
697 | } | |
698 | ||
bbe63281 LP |
699 | void log_show_color(bool b) { |
700 | show_color = b; | |
701 | } | |
702 | ||
703 | void log_show_location(bool b) { | |
704 | show_location = b; | |
705 | } | |
706 | ||
707 | int log_show_color_from_string(const char *e) { | |
708 | int t; | |
709 | ||
5ba081b0 LP |
710 | t = parse_boolean(e); |
711 | if (t < 0) | |
712 | return t; | |
bbe63281 LP |
713 | |
714 | log_show_color(t); | |
715 | return 0; | |
716 | } | |
717 | ||
718 | int log_show_location_from_string(const char *e) { | |
719 | int t; | |
720 | ||
5ba081b0 LP |
721 | t = parse_boolean(e); |
722 | if (t < 0) | |
723 | return t; | |
bbe63281 LP |
724 | |
725 | log_show_location(t); | |
726 | return 0; | |
727 | } | |
728 | ||
34f0e866 LP |
729 | static const char *const log_target_table[] = { |
730 | [LOG_TARGET_CONSOLE] = "console", | |
34f0e866 | 731 | [LOG_TARGET_KMSG] = "kmsg", |
5ba081b0 LP |
732 | [LOG_TARGET_JOURNAL] = "journal", |
733 | [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg", | |
734 | [LOG_TARGET_SYSLOG] = "syslog", | |
843d2643 | 735 | [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg", |
5ba081b0 LP |
736 | [LOG_TARGET_AUTO] = "auto", |
737 | [LOG_TARGET_NULL] = "null" | |
34f0e866 LP |
738 | }; |
739 | ||
740 | DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); |