]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
c9b97d2a | 3 | #include <errno.h> |
16801e90 | 4 | #include <fcntl.h> |
11c3a366 TA |
5 | #include <inttypes.h> |
6 | #include <limits.h> | |
07630cea LP |
7 | #include <stdarg.h> |
8 | #include <stddef.h> | |
9 | #include <stdio.h> | |
11c3a366 TA |
10 | #include <string.h> |
11 | #include <sys/signalfd.h> | |
16801e90 | 12 | #include <sys/socket.h> |
11c3a366 TA |
13 | #include <sys/time.h> |
14 | #include <sys/uio.h> | |
16801e90 | 15 | #include <sys/un.h> |
11c3a366 | 16 | #include <time.h> |
07630cea | 17 | #include <unistd.h> |
5899f3b7 | 18 | |
158350e8 | 19 | #include "sd-messages.h" |
07630cea | 20 | |
b5efdb8a | 21 | #include "alloc-util.h" |
3ffd4af2 | 22 | #include "fd-util.h" |
f97b34a6 | 23 | #include "format-util.h" |
afc5dbf3 | 24 | #include "io-util.h" |
3ffd4af2 | 25 | #include "log.h" |
07630cea LP |
26 | #include "macro.h" |
27 | #include "missing.h" | |
4e731273 LP |
28 | #include "parse-util.h" |
29 | #include "proc-cmdline.h" | |
0b452006 | 30 | #include "process-util.h" |
24882e06 | 31 | #include "signal-util.h" |
07630cea | 32 | #include "socket-util.h" |
15a5e950 | 33 | #include "stdio-util.h" |
8b43440b | 34 | #include "string-table.h" |
07630cea | 35 | #include "string-util.h" |
7ccbd1ae | 36 | #include "syslog-util.h" |
07630cea | 37 | #include "terminal-util.h" |
93cc7779 | 38 | #include "time-util.h" |
d04ce5a9 | 39 | #include "utf8.h" |
07630cea | 40 | #include "util.h" |
5899f3b7 | 41 | |
bb99a35a LP |
42 | #define SNDBUF_SIZE (8*1024*1024) |
43 | ||
16801e90 | 44 | static LogTarget log_target = LOG_TARGET_CONSOLE; |
ff524019 ZJS |
45 | static int log_max_level[] = {LOG_INFO, LOG_INFO}; |
46 | assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX); | |
3eff4208 | 47 | static int log_facility = LOG_DAEMON; |
16801e90 | 48 | |
843d2643 | 49 | static int console_fd = STDERR_FILENO; |
16801e90 LP |
50 | static int syslog_fd = -1; |
51 | static int kmsg_fd = -1; | |
5ba081b0 | 52 | static int journal_fd = -1; |
16801e90 | 53 | |
c31e1495 LP |
54 | static bool syslog_is_stream = false; |
55 | ||
bbe63281 LP |
56 | static bool show_color = false; |
57 | static bool show_location = false; | |
58 | ||
c1dc6153 | 59 | static bool upgrade_syslog_to_journal = false; |
48a601fe | 60 | static bool always_reopen_console = false; |
16e4fd87 | 61 | static bool open_when_needed = false; |
adf47c91 | 62 | static bool prohibit_ipc = false; |
c1dc6153 | 63 | |
35b8ca3a | 64 | /* Akin to glibc's __abort_msg; which is private and we hence cannot |
185986c6 LP |
65 | * use here. */ |
66 | static char *log_abort_msg = NULL; | |
67 | ||
780747da ZJS |
68 | /* An assert to use in logging functions that does not call recursively |
69 | * into our logging functions (since that might lead to a loop). */ | |
70 | #define assert_raw(expr) \ | |
71 | do { \ | |
72 | if (_unlikely_(!(expr))) { \ | |
73 | fputs(#expr "\n", stderr); \ | |
74 | abort(); \ | |
75 | } \ | |
76 | } while (false) | |
77 | ||
cc2b9e6b | 78 | static void log_close_console(void) { |
e7685a77 | 79 | console_fd = safe_close_above_stdio(console_fd); |
16801e90 LP |
80 | } |
81 | ||
843d2643 | 82 | static int log_open_console(void) { |
16801e90 | 83 | |
cc2b9e6b AJ |
84 | if (!always_reopen_console) { |
85 | console_fd = STDERR_FILENO; | |
16801e90 | 86 | return 0; |
cc2b9e6b | 87 | } |
843d2643 | 88 | |
cc2b9e6b | 89 | if (console_fd < 3) { |
5ba081b0 | 90 | console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
cd15c418 | 91 | if (console_fd < 0) |
843d2643 | 92 | return console_fd; |
7fe2903c LP |
93 | |
94 | console_fd = fd_move_above_stdio(console_fd); | |
cc2b9e6b | 95 | } |
843d2643 LP |
96 | |
97 | return 0; | |
98 | } | |
99 | ||
cc2b9e6b | 100 | static void log_close_kmsg(void) { |
03e334a1 | 101 | kmsg_fd = safe_close(kmsg_fd); |
843d2643 LP |
102 | } |
103 | ||
104 | static int log_open_kmsg(void) { | |
16801e90 LP |
105 | |
106 | if (kmsg_fd >= 0) | |
107 | return 0; | |
108 | ||
674f8283 | 109 | kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
cd15c418 | 110 | if (kmsg_fd < 0) |
16801e90 | 111 | return -errno; |
0dae83f9 | 112 | |
7fe2903c | 113 | kmsg_fd = fd_move_above_stdio(kmsg_fd); |
16801e90 LP |
114 | return 0; |
115 | } | |
116 | ||
cc2b9e6b | 117 | static void log_close_syslog(void) { |
03e334a1 | 118 | syslog_fd = safe_close(syslog_fd); |
16801e90 LP |
119 | } |
120 | ||
c31e1495 | 121 | static int create_log_socket(int type) { |
8b18fdc1 | 122 | struct timeval tv; |
95066a90 | 123 | int fd; |
c31e1495 | 124 | |
8b18fdc1 | 125 | fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0); |
5ba081b0 | 126 | if (fd < 0) |
c31e1495 LP |
127 | return -errno; |
128 | ||
7fe2903c | 129 | fd = fd_move_above_stdio(fd); |
0474ef7b | 130 | (void) fd_inc_sndbuf(fd, SNDBUF_SIZE); |
bb99a35a | 131 | |
7fe2903c LP |
132 | /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever |
133 | * in the unlikely case of a deadlock. */ | |
df0ff127 | 134 | if (getpid_cached() == 1) |
4d89874a ZJS |
135 | timeval_store(&tv, 10 * USEC_PER_MSEC); |
136 | else | |
137 | timeval_store(&tv, 10 * USEC_PER_SEC); | |
086891e5 | 138 | (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
8b18fdc1 | 139 | |
c31e1495 LP |
140 | return fd; |
141 | } | |
142 | ||
843d2643 | 143 | static int log_open_syslog(void) { |
95066a90 LP |
144 | |
145 | static const union sockaddr_union sa = { | |
b92bea5d ZJS |
146 | .un.sun_family = AF_UNIX, |
147 | .un.sun_path = "/dev/log", | |
148 | }; | |
16801e90 | 149 | |
95066a90 LP |
150 | int r; |
151 | ||
16801e90 LP |
152 | if (syslog_fd >= 0) |
153 | return 0; | |
154 | ||
5ba081b0 LP |
155 | syslog_fd = create_log_socket(SOCK_DGRAM); |
156 | if (syslog_fd < 0) { | |
157 | r = syslog_fd; | |
843d2643 | 158 | goto fail; |
16801e90 LP |
159 | } |
160 | ||
fc2fffe7 | 161 | if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { |
03e334a1 | 162 | safe_close(syslog_fd); |
c31e1495 LP |
163 | |
164 | /* Some legacy syslog systems still use stream | |
165 | * sockets. They really shouldn't. But what can we | |
166 | * do... */ | |
5ba081b0 LP |
167 | syslog_fd = create_log_socket(SOCK_STREAM); |
168 | if (syslog_fd < 0) { | |
169 | r = syslog_fd; | |
c31e1495 LP |
170 | goto fail; |
171 | } | |
172 | ||
fc2fffe7 | 173 | if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { |
c31e1495 LP |
174 | r = -errno; |
175 | goto fail; | |
176 | } | |
177 | ||
178 | syslog_is_stream = true; | |
179 | } else | |
180 | syslog_is_stream = false; | |
181 | ||
16801e90 | 182 | return 0; |
843d2643 LP |
183 | |
184 | fail: | |
185 | log_close_syslog(); | |
843d2643 LP |
186 | return r; |
187 | } | |
188 | ||
cc2b9e6b | 189 | static void log_close_journal(void) { |
03e334a1 | 190 | journal_fd = safe_close(journal_fd); |
5ba081b0 LP |
191 | } |
192 | ||
193 | static int log_open_journal(void) { | |
95066a90 LP |
194 | |
195 | static const union sockaddr_union sa = { | |
b92bea5d ZJS |
196 | .un.sun_family = AF_UNIX, |
197 | .un.sun_path = "/run/systemd/journal/socket", | |
198 | }; | |
95066a90 | 199 | |
5ba081b0 LP |
200 | int r; |
201 | ||
202 | if (journal_fd >= 0) | |
203 | return 0; | |
204 | ||
205 | journal_fd = create_log_socket(SOCK_DGRAM); | |
206 | if (journal_fd < 0) { | |
207 | r = journal_fd; | |
208 | goto fail; | |
209 | } | |
210 | ||
fc2fffe7 | 211 | if (connect(journal_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) { |
5ba081b0 LP |
212 | r = -errno; |
213 | goto fail; | |
214 | } | |
215 | ||
5ba081b0 LP |
216 | return 0; |
217 | ||
218 | fail: | |
219 | log_close_journal(); | |
5ba081b0 LP |
220 | return r; |
221 | } | |
222 | ||
843d2643 LP |
223 | int log_open(void) { |
224 | int r; | |
225 | ||
e3e42fc2 ZJS |
226 | /* Do not call from library code. */ |
227 | ||
843d2643 LP |
228 | /* If we don't use the console we close it here, to not get |
229 | * killed by SAK. If we don't use syslog we close it here so | |
230 | * that we are not confused by somebody deleting the socket in | |
cc2b9e6b AJ |
231 | * the fs, and to make sure we don't use it if prohibit_ipc is |
232 | * set. If we don't use /dev/kmsg we still keep it open, | |
843d2643 LP |
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 | ||
6c347d50 | 242 | if (log_target != LOG_TARGET_AUTO || |
df0ff127 | 243 | getpid_cached() == 1 || |
f41c094c | 244 | isatty(STDERR_FILENO) <= 0) { |
bb7df0da | 245 | |
adf47c91 LP |
246 | if (!prohibit_ipc && |
247 | IN_SET(log_target, LOG_TARGET_AUTO, | |
5b5688af ZJS |
248 | LOG_TARGET_JOURNAL_OR_KMSG, |
249 | LOG_TARGET_JOURNAL)) { | |
5ba081b0 LP |
250 | r = log_open_journal(); |
251 | if (r >= 0) { | |
252 | log_close_syslog(); | |
bb7df0da LP |
253 | log_close_console(); |
254 | return r; | |
255 | } | |
5ba081b0 LP |
256 | } |
257 | ||
adf47c91 LP |
258 | if (!prohibit_ipc && |
259 | IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG, | |
5b5688af | 260 | LOG_TARGET_SYSLOG)) { |
5ba081b0 LP |
261 | r = log_open_syslog(); |
262 | if (r >= 0) { | |
263 | log_close_journal(); | |
264 | log_close_console(); | |
265 | return r; | |
266 | } | |
267 | } | |
268 | ||
5b5688af | 269 | if (IN_SET(log_target, LOG_TARGET_AUTO, |
5b5688af ZJS |
270 | LOG_TARGET_JOURNAL_OR_KMSG, |
271 | LOG_TARGET_SYSLOG_OR_KMSG, | |
272 | LOG_TARGET_KMSG)) { | |
5ba081b0 LP |
273 | r = log_open_kmsg(); |
274 | if (r >= 0) { | |
275 | log_close_journal(); | |
bb7df0da LP |
276 | log_close_syslog(); |
277 | log_close_console(); | |
278 | return r; | |
279 | } | |
5ba081b0 | 280 | } |
bb7df0da | 281 | } |
843d2643 | 282 | |
5ba081b0 | 283 | log_close_journal(); |
843d2643 | 284 | log_close_syslog(); |
dcdf86bb | 285 | |
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 | ||
c1dc6153 LP |
293 | if (upgrade_syslog_to_journal) { |
294 | if (target == LOG_TARGET_SYSLOG) | |
295 | target = LOG_TARGET_JOURNAL; | |
296 | else if (target == LOG_TARGET_SYSLOG_OR_KMSG) | |
297 | target = LOG_TARGET_JOURNAL_OR_KMSG; | |
298 | } | |
299 | ||
16801e90 LP |
300 | log_target = target; |
301 | } | |
302 | ||
871e5809 | 303 | void log_close(void) { |
e3e42fc2 ZJS |
304 | /* Do not call from library code. */ |
305 | ||
5ba081b0 | 306 | log_close_journal(); |
871e5809 | 307 | log_close_syslog(); |
5ba081b0 LP |
308 | log_close_kmsg(); |
309 | log_close_console(); | |
871e5809 LP |
310 | } |
311 | ||
4d8a7798 | 312 | void log_forget_fds(void) { |
e3e42fc2 ZJS |
313 | /* Do not call from library code. */ |
314 | ||
5ba081b0 | 315 | console_fd = kmsg_fd = syslog_fd = journal_fd = -1; |
4d8a7798 MS |
316 | } |
317 | ||
ff524019 | 318 | void log_set_max_level_realm(LogRealm realm, int level) { |
16801e90 | 319 | assert((level & LOG_PRIMASK) == level); |
ff524019 | 320 | assert(realm < ELEMENTSOF(log_max_level)); |
16801e90 | 321 | |
ff524019 | 322 | log_max_level[realm] = level; |
16801e90 LP |
323 | } |
324 | ||
3eff4208 LP |
325 | void log_set_facility(int facility) { |
326 | log_facility = facility; | |
327 | } | |
328 | ||
843d2643 | 329 | static int write_to_console( |
20c03b7b | 330 | int level, |
086891e5 | 331 | int error, |
95066a90 | 332 | const char *file, |
20c03b7b LP |
333 | int line, |
334 | const char *func, | |
335 | const char *buffer) { | |
5899f3b7 | 336 | |
79c95440 | 337 | char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2]; |
aca83a53 | 338 | struct iovec iovec[6] = {}; |
843d2643 | 339 | bool highlight; |
da6053d0 | 340 | size_t n = 0; |
5899f3b7 | 341 | |
843d2643 LP |
342 | if (console_fd < 0) |
343 | return 0; | |
344 | ||
aca83a53 | 345 | if (log_target == LOG_TARGET_CONSOLE_PREFIXED) { |
0474ef7b | 346 | xsprintf(prefix, "<%i>", level); |
e6a7ec4b | 347 | iovec[n++] = IOVEC_MAKE_STRING(prefix); |
aca83a53 LP |
348 | } |
349 | ||
bbe63281 | 350 | highlight = LOG_PRI(level) <= LOG_ERR && show_color; |
843d2643 | 351 | |
674f8283 | 352 | if (show_location) { |
4ad2b562 | 353 | (void) snprintf(location, sizeof location, "(%s:%i) ", file, line); |
e6a7ec4b | 354 | iovec[n++] = IOVEC_MAKE_STRING(location); |
674f8283 LP |
355 | } |
356 | ||
843d2643 | 357 | if (highlight) |
e6a7ec4b LP |
358 | iovec[n++] = IOVEC_MAKE_STRING(ANSI_HIGHLIGHT_RED); |
359 | iovec[n++] = IOVEC_MAKE_STRING(buffer); | |
843d2643 | 360 | if (highlight) |
e6a7ec4b LP |
361 | iovec[n++] = IOVEC_MAKE_STRING(ANSI_NORMAL); |
362 | iovec[n++] = IOVEC_MAKE_STRING("\n"); | |
843d2643 | 363 | |
0e6eaa2d LP |
364 | if (writev(console_fd, iovec, n) < 0) { |
365 | ||
df0ff127 | 366 | if (errno == EIO && getpid_cached() == 1) { |
0e6eaa2d LP |
367 | |
368 | /* If somebody tried to kick us from our | |
369 | * console tty (via vhangup() or suchlike), | |
370 | * try to reconnect */ | |
371 | ||
372 | log_close_console(); | |
373 | log_open_console(); | |
374 | ||
375 | if (console_fd < 0) | |
376 | return 0; | |
377 | ||
378 | if (writev(console_fd, iovec, n) < 0) | |
379 | return -errno; | |
380 | } else | |
381 | return -errno; | |
382 | } | |
5899f3b7 | 383 | |
843d2643 | 384 | return 1; |
16801e90 | 385 | } |
5899f3b7 | 386 | |
16801e90 | 387 | static int write_to_syslog( |
086891e5 LP |
388 | int level, |
389 | int error, | |
95066a90 | 390 | const char *file, |
086891e5 LP |
391 | int line, |
392 | const char *func, | |
086891e5 | 393 | const char *buffer) { |
16801e90 | 394 | |
5ffa8c81 ZJS |
395 | char header_priority[2 + DECIMAL_STR_MAX(int) + 1], |
396 | header_time[64], | |
397 | header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; | |
b92bea5d ZJS |
398 | struct iovec iovec[5] = {}; |
399 | struct msghdr msghdr = { | |
400 | .msg_iov = iovec, | |
401 | .msg_iovlen = ELEMENTSOF(iovec), | |
402 | }; | |
16801e90 LP |
403 | time_t t; |
404 | struct tm *tm; | |
405 | ||
406 | if (syslog_fd < 0) | |
843d2643 | 407 | return 0; |
16801e90 | 408 | |
5ffa8c81 | 409 | xsprintf(header_priority, "<%i>", level); |
16801e90 LP |
410 | |
411 | t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC); | |
fdf9f9bb ZJS |
412 | tm = localtime(&t); |
413 | if (!tm) | |
16801e90 LP |
414 | return -EINVAL; |
415 | ||
416 | if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0) | |
417 | return -EINVAL; | |
418 | ||
df0ff127 | 419 | xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); |
16801e90 | 420 | |
e6a7ec4b LP |
421 | iovec[0] = IOVEC_MAKE_STRING(header_priority); |
422 | iovec[1] = IOVEC_MAKE_STRING(header_time); | |
423 | iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name); | |
424 | iovec[3] = IOVEC_MAKE_STRING(header_pid); | |
425 | iovec[4] = IOVEC_MAKE_STRING(buffer); | |
16801e90 | 426 | |
c899f8c6 | 427 | /* When using syslog via SOCK_STREAM separate the messages by NUL chars */ |
c31e1495 LP |
428 | if (syslog_is_stream) |
429 | iovec[4].iov_len++; | |
430 | ||
c31e1495 LP |
431 | for (;;) { |
432 | ssize_t n; | |
433 | ||
8f7f7a1b MS |
434 | n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL); |
435 | if (n < 0) | |
c31e1495 LP |
436 | return -errno; |
437 | ||
438 | if (!syslog_is_stream || | |
439 | (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec))) | |
440 | break; | |
441 | ||
442 | IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n); | |
443 | } | |
16801e90 | 444 | |
843d2643 | 445 | return 1; |
16801e90 LP |
446 | } |
447 | ||
448 | static int write_to_kmsg( | |
086891e5 LP |
449 | int level, |
450 | int error, | |
bcf5c276 | 451 | const char *file, |
086891e5 LP |
452 | int line, |
453 | const char *func, | |
086891e5 | 454 | const char *buffer) { |
16801e90 | 455 | |
5ffa8c81 ZJS |
456 | char header_priority[2 + DECIMAL_STR_MAX(int) + 1], |
457 | header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; | |
b92bea5d | 458 | struct iovec iovec[5] = {}; |
16801e90 LP |
459 | |
460 | if (kmsg_fd < 0) | |
843d2643 | 461 | return 0; |
16801e90 | 462 | |
5ffa8c81 | 463 | xsprintf(header_priority, "<%i>", level); |
df0ff127 | 464 | xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); |
16801e90 | 465 | |
e6a7ec4b LP |
466 | iovec[0] = IOVEC_MAKE_STRING(header_priority); |
467 | iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name); | |
468 | iovec[2] = IOVEC_MAKE_STRING(header_pid); | |
469 | iovec[3] = IOVEC_MAKE_STRING(buffer); | |
470 | iovec[4] = IOVEC_MAKE_STRING("\n"); | |
16801e90 LP |
471 | |
472 | if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) | |
473 | return -errno; | |
474 | ||
843d2643 | 475 | return 1; |
16801e90 LP |
476 | } |
477 | ||
086891e5 LP |
478 | static int log_do_header( |
479 | char *header, | |
480 | size_t size, | |
481 | int level, | |
482 | int error, | |
483 | const char *file, int line, const char *func, | |
4b58153d LP |
484 | const char *object_field, const char *object, |
485 | const char *extra_field, const char *extra) { | |
f8e6f4aa | 486 | int r; |
086891e5 | 487 | |
f8e6f4aa ZJS |
488 | r = snprintf(header, size, |
489 | "PRIORITY=%i\n" | |
490 | "SYSLOG_FACILITY=%i\n" | |
491 | "%s%.256s%s" /* CODE_FILE */ | |
492 | "%s%.*i%s" /* CODE_LINE */ | |
493 | "%s%.256s%s" /* CODE_FUNC */ | |
494 | "%s%.*i%s" /* ERRNO */ | |
495 | "%s%.256s%s" /* object */ | |
496 | "%s%.256s%s" /* extra */ | |
497 | "SYSLOG_IDENTIFIER=%.256s\n", | |
498 | LOG_PRI(level), | |
499 | LOG_FAC(level), | |
500 | isempty(file) ? "" : "CODE_FILE=", | |
501 | isempty(file) ? "" : file, | |
502 | isempty(file) ? "" : "\n", | |
503 | line ? "CODE_LINE=" : "", | |
504 | line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */ | |
505 | line ? "\n" : "", | |
506 | isempty(func) ? "" : "CODE_FUNC=", | |
507 | isempty(func) ? "" : func, | |
508 | isempty(func) ? "" : "\n", | |
509 | error ? "ERRNO=" : "", | |
510 | error ? 1 : 0, error, | |
511 | error ? "\n" : "", | |
512 | isempty(object) ? "" : object_field, | |
513 | isempty(object) ? "" : object, | |
514 | isempty(object) ? "" : "\n", | |
515 | isempty(extra) ? "" : extra_field, | |
516 | isempty(extra) ? "" : extra, | |
517 | isempty(extra) ? "" : "\n", | |
518 | program_invocation_short_name); | |
780747da | 519 | assert_raw((size_t) r < size); |
086891e5 | 520 | |
41a79f10 ZJS |
521 | return 0; |
522 | } | |
5ba081b0 | 523 | |
41a79f10 | 524 | static int write_to_journal( |
086891e5 LP |
525 | int level, |
526 | int error, | |
bcf5c276 | 527 | const char *file, |
086891e5 LP |
528 | int line, |
529 | const char *func, | |
530 | const char *object_field, | |
531 | const char *object, | |
4b58153d LP |
532 | const char *extra_field, |
533 | const char *extra, | |
086891e5 | 534 | const char *buffer) { |
41a79f10 ZJS |
535 | |
536 | char header[LINE_MAX]; | |
b92bea5d ZJS |
537 | struct iovec iovec[4] = {}; |
538 | struct msghdr mh = {}; | |
41a79f10 ZJS |
539 | |
540 | if (journal_fd < 0) | |
541 | return 0; | |
542 | ||
4b58153d | 543 | log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra); |
5ba081b0 | 544 | |
e6a7ec4b LP |
545 | iovec[0] = IOVEC_MAKE_STRING(header); |
546 | iovec[1] = IOVEC_MAKE_STRING("MESSAGE="); | |
547 | iovec[2] = IOVEC_MAKE_STRING(buffer); | |
548 | iovec[3] = IOVEC_MAKE_STRING("\n"); | |
5ba081b0 | 549 | |
5ba081b0 LP |
550 | mh.msg_iov = iovec; |
551 | mh.msg_iovlen = ELEMENTSOF(iovec); | |
552 | ||
553 | if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0) | |
554 | return -errno; | |
555 | ||
556 | return 1; | |
557 | } | |
558 | ||
93484b47 | 559 | int log_dispatch_internal( |
086891e5 LP |
560 | int level, |
561 | int error, | |
95066a90 | 562 | const char *file, |
086891e5 LP |
563 | int line, |
564 | const char *func, | |
565 | const char *object_field, | |
566 | const char *object, | |
4b58153d | 567 | const char *extra_field, |
4dd09c6a | 568 | const char *extra, |
086891e5 | 569 | char *buffer) { |
843d2643 | 570 | |
780747da | 571 | assert_raw(buffer); |
843d2643 | 572 | |
0474ef7b LP |
573 | if (error < 0) |
574 | error = -error; | |
575 | ||
9fae33d2 | 576 | if (log_target == LOG_TARGET_NULL) |
bf371116 | 577 | return -error; |
9fae33d2 | 578 | |
29db5834 | 579 | /* Patch in LOG_DAEMON facility if necessary */ |
7d76f312 | 580 | if ((level & LOG_FACMASK) == 0) |
3eff4208 | 581 | level = log_facility | LOG_PRI(level); |
29db5834 | 582 | |
16e4fd87 LP |
583 | if (open_when_needed) |
584 | log_open(); | |
585 | ||
9726b29e LP |
586 | do { |
587 | char *e; | |
9499b235 | 588 | int k = 0; |
843d2643 | 589 | |
9726b29e | 590 | buffer += strspn(buffer, NEWLINE); |
843d2643 | 591 | |
9726b29e LP |
592 | if (buffer[0] == 0) |
593 | break; | |
843d2643 | 594 | |
9726b29e LP |
595 | if ((e = strpbrk(buffer, NEWLINE))) |
596 | *(e++) = 0; | |
843d2643 | 597 | |
5b5688af ZJS |
598 | if (IN_SET(log_target, LOG_TARGET_AUTO, |
599 | LOG_TARGET_JOURNAL_OR_KMSG, | |
600 | LOG_TARGET_JOURNAL)) { | |
5ba081b0 | 601 | |
4b58153d | 602 | k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer); |
e38b8a40 LP |
603 | if (k < 0 && k != -EAGAIN) |
604 | log_close_journal(); | |
5ba081b0 LP |
605 | } |
606 | ||
5b5688af ZJS |
607 | if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG, |
608 | LOG_TARGET_SYSLOG)) { | |
9726b29e | 609 | |
4b58153d | 610 | k = write_to_syslog(level, error, file, line, func, buffer); |
e38b8a40 LP |
611 | if (k < 0 && k != -EAGAIN) |
612 | log_close_syslog(); | |
9726b29e LP |
613 | } |
614 | ||
9499b235 | 615 | if (k <= 0 && |
5b5688af | 616 | IN_SET(log_target, LOG_TARGET_AUTO, |
5b5688af ZJS |
617 | LOG_TARGET_SYSLOG_OR_KMSG, |
618 | LOG_TARGET_JOURNAL_OR_KMSG, | |
619 | LOG_TARGET_KMSG)) { | |
9726b29e | 620 | |
e38b8a40 LP |
621 | if (k < 0) |
622 | log_open_kmsg(); | |
623 | ||
4b58153d | 624 | k = write_to_kmsg(level, error, file, line, func, buffer); |
4e7bc3f3 LP |
625 | if (k < 0) { |
626 | log_close_kmsg(); | |
9726b29e | 627 | log_open_console(); |
bf371116 | 628 | } |
9726b29e LP |
629 | } |
630 | ||
bf371116 | 631 | if (k <= 0) |
4b58153d | 632 | (void) write_to_console(level, error, file, line, func, buffer); |
9726b29e LP |
633 | |
634 | buffer = e; | |
635 | } while (buffer); | |
636 | ||
16e4fd87 LP |
637 | if (open_when_needed) |
638 | log_close(); | |
639 | ||
bf371116 | 640 | return -error; |
843d2643 LP |
641 | } |
642 | ||
2149e37c | 643 | int log_dump_internal( |
ff524019 ZJS |
644 | int level, |
645 | int error, | |
646 | const char *file, | |
647 | int line, | |
648 | const char *func, | |
649 | char *buffer) { | |
2149e37c | 650 | |
ff524019 | 651 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
5c0aa72a | 652 | PROTECT_ERRNO; |
2149e37c LP |
653 | |
654 | /* This modifies the buffer... */ | |
655 | ||
bf371116 LP |
656 | if (error < 0) |
657 | error = -error; | |
658 | ||
ff524019 | 659 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
bf371116 | 660 | return -error; |
2149e37c | 661 | |
93484b47 | 662 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); |
2149e37c LP |
663 | } |
664 | ||
ff524019 | 665 | int log_internalv_realm( |
086891e5 LP |
666 | int level, |
667 | int error, | |
bcf5c276 | 668 | const char *file, |
086891e5 LP |
669 | int line, |
670 | const char *func, | |
671 | const char *format, | |
672 | va_list ap) { | |
16801e90 | 673 | |
ff524019 | 674 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
addab137 | 675 | char buffer[LINE_MAX]; |
ff524019 | 676 | PROTECT_ERRNO; |
16801e90 | 677 | |
bf371116 LP |
678 | if (error < 0) |
679 | error = -error; | |
680 | ||
ff524019 | 681 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
bf371116 | 682 | return -error; |
16801e90 | 683 | |
b29f6480 ZJS |
684 | /* Make sure that %m maps to the specified error (or "Success"). */ |
685 | errno = error; | |
086891e5 | 686 | |
4ad2b562 | 687 | (void) vsnprintf(buffer, sizeof buffer, format, ap); |
843d2643 | 688 | |
93484b47 | 689 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); |
185986c6 | 690 | } |
16801e90 | 691 | |
ff524019 | 692 | int log_internal_realm( |
086891e5 LP |
693 | int level, |
694 | int error, | |
bcf5c276 | 695 | const char *file, |
086891e5 LP |
696 | int line, |
697 | const char *func, | |
698 | const char *format, ...) { | |
17a94911 | 699 | |
17a94911 | 700 | va_list ap; |
bf371116 | 701 | int r; |
17a94911 LP |
702 | |
703 | va_start(ap, format); | |
ff524019 | 704 | r = log_internalv_realm(level, error, file, line, func, format, ap); |
17a94911 LP |
705 | va_end(ap); |
706 | ||
707 | return r; | |
708 | } | |
709 | ||
a3bf7137 ZJS |
710 | _printf_(10,0) |
711 | static int log_object_internalv( | |
086891e5 LP |
712 | int level, |
713 | int error, | |
bcf5c276 | 714 | const char *file, |
086891e5 LP |
715 | int line, |
716 | const char *func, | |
717 | const char *object_field, | |
718 | const char *object, | |
4b58153d LP |
719 | const char *extra_field, |
720 | const char *extra, | |
086891e5 LP |
721 | const char *format, |
722 | va_list ap) { | |
fdf9f9bb | 723 | |
5c0aa72a | 724 | PROTECT_ERRNO; |
f2341e0a | 725 | char *buffer, *b; |
fdf9f9bb | 726 | |
bf371116 LP |
727 | if (error < 0) |
728 | error = -error; | |
729 | ||
ff524019 | 730 | if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD])) |
bf371116 | 731 | return -error; |
fdf9f9bb | 732 | |
d1a1f0aa ZJS |
733 | /* Make sure that %m maps to the specified error (or "Success"). */ |
734 | errno = error; | |
086891e5 | 735 | |
f2341e0a LP |
736 | /* Prepend the object name before the message */ |
737 | if (object) { | |
738 | size_t n; | |
739 | ||
740 | n = strlen(object); | |
1741b25c | 741 | buffer = newa(char, n + 2 + LINE_MAX); |
f2341e0a | 742 | b = stpcpy(stpcpy(buffer, object), ": "); |
1741b25c EV |
743 | } else |
744 | b = buffer = newa(char, LINE_MAX); | |
f2341e0a | 745 | |
4ad2b562 | 746 | (void) vsnprintf(b, LINE_MAX, format, ap); |
fdf9f9bb | 747 | |
93484b47 ZJS |
748 | return log_dispatch_internal(level, error, file, line, func, |
749 | object_field, object, extra_field, extra, buffer); | |
fdf9f9bb ZJS |
750 | } |
751 | ||
79008bdd | 752 | int log_object_internal( |
086891e5 LP |
753 | int level, |
754 | int error, | |
bcf5c276 | 755 | const char *file, |
086891e5 LP |
756 | int line, |
757 | const char *func, | |
758 | const char *object_field, | |
759 | const char *object, | |
4b58153d LP |
760 | const char *extra_field, |
761 | const char *extra, | |
086891e5 | 762 | const char *format, ...) { |
fdf9f9bb | 763 | |
fdf9f9bb | 764 | va_list ap; |
bf371116 | 765 | int r; |
fdf9f9bb ZJS |
766 | |
767 | va_start(ap, format); | |
4b58153d | 768 | r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap); |
fdf9f9bb ZJS |
769 | va_end(ap); |
770 | ||
771 | return r; | |
772 | } | |
773 | ||
086891e5 LP |
774 | static void log_assert( |
775 | int level, | |
776 | const char *text, | |
777 | const char *file, | |
778 | int line, | |
779 | const char *func, | |
780 | const char *format) { | |
781 | ||
addab137 | 782 | static char buffer[LINE_MAX]; |
ff524019 | 783 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
185986c6 | 784 | |
ff524019 | 785 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
50f72bca ZJS |
786 | return; |
787 | ||
bcfce235 | 788 | DISABLE_WARNING_FORMAT_NONLITERAL; |
4ad2b562 | 789 | (void) snprintf(buffer, sizeof buffer, format, text, file, line, func); |
bcfce235 | 790 | REENABLE_WARNING; |
185986c6 | 791 | |
185986c6 LP |
792 | log_abort_msg = buffer; |
793 | ||
93484b47 | 794 | log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer); |
5899f3b7 | 795 | } |
34f0e866 | 796 | |
848e863a | 797 | _noreturn_ void log_assert_failed_realm( |
ff524019 ZJS |
798 | LogRealm realm, |
799 | const char *text, | |
800 | const char *file, | |
801 | int line, | |
802 | const char *func) { | |
ea89a119 | 803 | log_open(); |
ff524019 ZJS |
804 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, |
805 | "Assertion '%s' failed at %s:%u, function %s(). Aborting."); | |
80514f9c | 806 | abort(); |
b7f33638 MS |
807 | } |
808 | ||
848e863a | 809 | _noreturn_ void log_assert_failed_unreachable_realm( |
ff524019 ZJS |
810 | LogRealm realm, |
811 | const char *text, | |
812 | const char *file, | |
813 | int line, | |
814 | const char *func) { | |
ea89a119 | 815 | log_open(); |
ff524019 ZJS |
816 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, |
817 | "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); | |
80514f9c LP |
818 | abort(); |
819 | } | |
820 | ||
ff524019 ZJS |
821 | void log_assert_failed_return_realm( |
822 | LogRealm realm, | |
823 | const char *text, | |
824 | const char *file, | |
825 | int line, | |
826 | const char *func) { | |
e5ca092c | 827 | PROTECT_ERRNO; |
ff524019 ZJS |
828 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func, |
829 | "Assertion '%s' failed at %s:%u, function %s(). Ignoring."); | |
b7f33638 MS |
830 | } |
831 | ||
ff524019 | 832 | int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) { |
7558e10c LP |
833 | return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR), |
834 | ENOMEM, file, line, func, "Out of memory."); | |
6dc1e7e0 MS |
835 | } |
836 | ||
8a03c9ef ZJS |
837 | int log_format_iovec( |
838 | struct iovec *iovec, | |
d3070fbd LP |
839 | size_t iovec_len, |
840 | size_t *n, | |
8a03c9ef ZJS |
841 | bool newline_separator, |
842 | int error, | |
843 | const char *format, | |
844 | va_list ap) { | |
845 | ||
846 | static const char nl = '\n'; | |
847 | ||
848 | while (format && *n + 1 < iovec_len) { | |
849 | va_list aq; | |
850 | char *m; | |
851 | int r; | |
852 | ||
853 | /* We need to copy the va_list structure, | |
854 | * since vasprintf() leaves it afterwards at | |
855 | * an undefined location */ | |
856 | ||
d1a1f0aa | 857 | errno = error; |
8a03c9ef ZJS |
858 | |
859 | va_copy(aq, ap); | |
860 | r = vasprintf(&m, format, aq); | |
861 | va_end(aq); | |
862 | if (r < 0) | |
863 | return -EINVAL; | |
864 | ||
865 | /* Now, jump enough ahead, so that we point to | |
866 | * the next format string */ | |
867 | VA_FORMAT_ADVANCE(format, ap); | |
868 | ||
e6a7ec4b | 869 | iovec[(*n)++] = IOVEC_MAKE_STRING(m); |
8a03c9ef ZJS |
870 | |
871 | if (newline_separator) { | |
872 | iovec[*n].iov_base = (char*) &nl; | |
873 | iovec[*n].iov_len = 1; | |
874 | (*n)++; | |
875 | } | |
876 | ||
877 | format = va_arg(ap, char *); | |
878 | } | |
879 | return 0; | |
880 | } | |
881 | ||
877d54e9 LP |
882 | int log_struct_internal( |
883 | int level, | |
086891e5 | 884 | int error, |
877d54e9 LP |
885 | const char *file, |
886 | int line, | |
887 | const char *func, | |
888 | const char *format, ...) { | |
889 | ||
e6a7ec4b | 890 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
bf371116 LP |
891 | char buf[LINE_MAX]; |
892 | bool found = false; | |
5c0aa72a | 893 | PROTECT_ERRNO; |
877d54e9 | 894 | va_list ap; |
bf371116 LP |
895 | |
896 | if (error < 0) | |
897 | error = -error; | |
877d54e9 | 898 | |
ff524019 | 899 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
bf371116 | 900 | return -error; |
877d54e9 LP |
901 | |
902 | if (log_target == LOG_TARGET_NULL) | |
bf371116 | 903 | return -error; |
877d54e9 LP |
904 | |
905 | if ((level & LOG_FACMASK) == 0) | |
906 | level = log_facility | LOG_PRI(level); | |
907 | ||
16e4fd87 LP |
908 | if (IN_SET(log_target, |
909 | LOG_TARGET_AUTO, | |
910 | LOG_TARGET_JOURNAL_OR_KMSG, | |
911 | LOG_TARGET_JOURNAL)) { | |
912 | ||
913 | if (open_when_needed) | |
914 | log_open_journal(); | |
915 | ||
916 | if (journal_fd >= 0) { | |
917 | char header[LINE_MAX]; | |
918 | struct iovec iovec[17] = {}; | |
d3070fbd | 919 | size_t n = 0, i; |
16e4fd87 LP |
920 | int r; |
921 | struct msghdr mh = { | |
922 | .msg_iov = iovec, | |
923 | }; | |
924 | bool fallback = false; | |
925 | ||
926 | /* If the journal is available do structured logging */ | |
927 | log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL); | |
928 | iovec[n++] = IOVEC_MAKE_STRING(header); | |
929 | ||
930 | va_start(ap, format); | |
931 | r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap); | |
932 | if (r < 0) | |
933 | fallback = true; | |
934 | else { | |
935 | mh.msg_iovlen = n; | |
936 | (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL); | |
937 | } | |
877d54e9 | 938 | |
16e4fd87 LP |
939 | va_end(ap); |
940 | for (i = 1; i < n; i += 2) | |
941 | free(iovec[i].iov_base); | |
877d54e9 | 942 | |
16e4fd87 LP |
943 | if (!fallback) { |
944 | if (open_when_needed) | |
945 | log_close(); | |
877d54e9 | 946 | |
16e4fd87 LP |
947 | return -error; |
948 | } | |
949 | } | |
bf371116 | 950 | } |
877d54e9 | 951 | |
bf371116 | 952 | /* Fallback if journal logging is not available or didn't work. */ |
6357ac66 | 953 | |
bf371116 LP |
954 | va_start(ap, format); |
955 | while (format) { | |
956 | va_list aq; | |
877d54e9 | 957 | |
d1a1f0aa | 958 | errno = error; |
877d54e9 | 959 | |
bf371116 | 960 | va_copy(aq, ap); |
4ad2b562 | 961 | (void) vsnprintf(buf, sizeof buf, format, aq); |
bf371116 | 962 | va_end(aq); |
963ddb91 | 963 | |
bf371116 LP |
964 | if (startswith(buf, "MESSAGE=")) { |
965 | found = true; | |
966 | break; | |
877d54e9 | 967 | } |
877d54e9 | 968 | |
bf371116 LP |
969 | VA_FORMAT_ADVANCE(format, ap); |
970 | ||
971 | format = va_arg(ap, char *); | |
877d54e9 | 972 | } |
bf371116 | 973 | va_end(ap); |
877d54e9 | 974 | |
16e4fd87 LP |
975 | if (!found) { |
976 | if (open_when_needed) | |
977 | log_close(); | |
978 | ||
bf371116 | 979 | return -error; |
16e4fd87 | 980 | } |
bf371116 | 981 | |
93484b47 | 982 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8); |
877d54e9 LP |
983 | } |
984 | ||
915b1d01 LP |
985 | int log_struct_iovec_internal( |
986 | int level, | |
987 | int error, | |
988 | const char *file, | |
989 | int line, | |
990 | const char *func, | |
991 | const struct iovec input_iovec[], | |
992 | size_t n_input_iovec) { | |
993 | ||
994 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); | |
995 | PROTECT_ERRNO; | |
996 | size_t i; | |
997 | char *m; | |
998 | ||
999 | if (error < 0) | |
1000 | error = -error; | |
1001 | ||
1002 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) | |
1003 | return -error; | |
1004 | ||
1005 | if (log_target == LOG_TARGET_NULL) | |
1006 | return -error; | |
1007 | ||
1008 | if ((level & LOG_FACMASK) == 0) | |
1009 | level = log_facility | LOG_PRI(level); | |
1010 | ||
1011 | if (IN_SET(log_target, LOG_TARGET_AUTO, | |
1012 | LOG_TARGET_JOURNAL_OR_KMSG, | |
1013 | LOG_TARGET_JOURNAL) && | |
1014 | journal_fd >= 0) { | |
1015 | ||
1016 | struct iovec iovec[1 + n_input_iovec*2]; | |
1017 | char header[LINE_MAX]; | |
1018 | struct msghdr mh = { | |
1019 | .msg_iov = iovec, | |
1020 | .msg_iovlen = 1 + n_input_iovec*2, | |
1021 | }; | |
1022 | ||
1023 | log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL); | |
1024 | iovec[0] = IOVEC_MAKE_STRING(header); | |
1025 | ||
1026 | for (i = 0; i < n_input_iovec; i++) { | |
1027 | iovec[1+i*2] = input_iovec[i]; | |
1028 | iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n"); | |
1029 | } | |
1030 | ||
1031 | if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0) | |
40056777 | 1032 | return -error; |
915b1d01 LP |
1033 | } |
1034 | ||
d27b725a LP |
1035 | for (i = 0; i < n_input_iovec; i++) |
1036 | if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) | |
915b1d01 | 1037 | break; |
915b1d01 LP |
1038 | |
1039 | if (_unlikely_(i >= n_input_iovec)) /* Couldn't find MESSAGE=? */ | |
1040 | return -error; | |
1041 | ||
fbd0b64f LP |
1042 | m = strndupa(input_iovec[i].iov_base + STRLEN("MESSAGE="), |
1043 | input_iovec[i].iov_len - STRLEN("MESSAGE=")); | |
915b1d01 LP |
1044 | |
1045 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m); | |
1046 | } | |
1047 | ||
34f0e866 LP |
1048 | int log_set_target_from_string(const char *e) { |
1049 | LogTarget t; | |
1050 | ||
5ba081b0 LP |
1051 | t = log_target_from_string(e); |
1052 | if (t < 0) | |
34f0e866 LP |
1053 | return -EINVAL; |
1054 | ||
1055 | log_set_target(t); | |
1056 | return 0; | |
1057 | } | |
1058 | ||
ff524019 | 1059 | int log_set_max_level_from_string_realm(LogRealm realm, const char *e) { |
34f0e866 LP |
1060 | int t; |
1061 | ||
5ba081b0 LP |
1062 | t = log_level_from_string(e); |
1063 | if (t < 0) | |
737af734 | 1064 | return -EINVAL; |
34f0e866 | 1065 | |
ff524019 | 1066 | log_set_max_level_realm(realm, t); |
34f0e866 LP |
1067 | return 0; |
1068 | } | |
1069 | ||
96287a49 | 1070 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
1de1c9c3 LP |
1071 | |
1072 | /* | |
1073 | * The systemd.log_xyz= settings are parsed by all tools, and | |
1074 | * so is "debug". | |
1075 | * | |
5e07a79e LP |
1076 | * However, "quiet" is only parsed by PID 1, and only turns of |
1077 | * status output to /dev/console, but does not alter the log | |
1078 | * level. | |
1de1c9c3 LP |
1079 | */ |
1080 | ||
1081 | if (streq(key, "debug") && !value) | |
1082 | log_set_max_level(LOG_DEBUG); | |
1083 | ||
1d84ad94 LP |
1084 | else if (proc_cmdline_key_streq(key, "systemd.log_target")) { |
1085 | ||
1086 | if (proc_cmdline_value_missing(key, value)) | |
1087 | return 0; | |
1de1c9c3 LP |
1088 | |
1089 | if (log_set_target_from_string(value) < 0) | |
1090 | log_warning("Failed to parse log target '%s'. Ignoring.", value); | |
1091 | ||
1d84ad94 LP |
1092 | } else if (proc_cmdline_key_streq(key, "systemd.log_level")) { |
1093 | ||
1094 | if (proc_cmdline_value_missing(key, value)) | |
1095 | return 0; | |
1de1c9c3 LP |
1096 | |
1097 | if (log_set_max_level_from_string(value) < 0) | |
1098 | log_warning("Failed to parse log level '%s'. Ignoring.", value); | |
1099 | ||
1d84ad94 | 1100 | } else if (proc_cmdline_key_streq(key, "systemd.log_color")) { |
1de1c9c3 | 1101 | |
1d84ad94 | 1102 | if (log_show_color_from_string(value ?: "1") < 0) |
1de1c9c3 LP |
1103 | log_warning("Failed to parse log color setting '%s'. Ignoring.", value); |
1104 | ||
1d84ad94 | 1105 | } else if (proc_cmdline_key_streq(key, "systemd.log_location")) { |
1de1c9c3 | 1106 | |
1d84ad94 | 1107 | if (log_show_location_from_string(value ?: "1") < 0) |
1de1c9c3 LP |
1108 | log_warning("Failed to parse log location setting '%s'. Ignoring.", value); |
1109 | } | |
1110 | ||
1111 | return 0; | |
1112 | } | |
1113 | ||
ff524019 | 1114 | void log_parse_environment_realm(LogRealm realm) { |
e3e42fc2 ZJS |
1115 | /* Do not call from library code. */ |
1116 | ||
34f0e866 | 1117 | const char *e; |
b8d0ffc2 | 1118 | |
ee46e555 | 1119 | if (get_ctty_devnr(0, NULL) < 0) |
1d84ad94 LP |
1120 | /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is |
1121 | user stuff. */ | |
1122 | (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); | |
34f0e866 | 1123 | |
a99e002c | 1124 | e = getenv("SYSTEMD_LOG_TARGET"); |
88fae6e0 | 1125 | if (e && log_set_target_from_string(e) < 0) |
f0ea29ea | 1126 | log_warning("Failed to parse log target '%s'. Ignoring.", e); |
34f0e866 | 1127 | |
a99e002c | 1128 | e = getenv("SYSTEMD_LOG_LEVEL"); |
ff524019 | 1129 | if (e && log_set_max_level_from_string_realm(realm, e) < 0) |
f0ea29ea | 1130 | log_warning("Failed to parse log level '%s'. Ignoring.", e); |
bbe63281 | 1131 | |
a99e002c | 1132 | e = getenv("SYSTEMD_LOG_COLOR"); |
88fae6e0 | 1133 | if (e && log_show_color_from_string(e) < 0) |
f0ea29ea | 1134 | log_warning("Failed to parse bool '%s'. Ignoring.", e); |
bbe63281 | 1135 | |
a99e002c | 1136 | e = getenv("SYSTEMD_LOG_LOCATION"); |
88fae6e0 | 1137 | if (e && log_show_location_from_string(e) < 0) |
f0ea29ea | 1138 | log_warning("Failed to parse bool '%s'. Ignoring.", e); |
34f0e866 LP |
1139 | } |
1140 | ||
1adf1049 LP |
1141 | LogTarget log_get_target(void) { |
1142 | return log_target; | |
1143 | } | |
1144 | ||
ff524019 ZJS |
1145 | int log_get_max_level_realm(LogRealm realm) { |
1146 | return log_max_level[realm]; | |
1adf1049 LP |
1147 | } |
1148 | ||
bbe63281 LP |
1149 | void log_show_color(bool b) { |
1150 | show_color = b; | |
1151 | } | |
1152 | ||
b1e90ec5 ZJS |
1153 | bool log_get_show_color(void) { |
1154 | return show_color; | |
1155 | } | |
1156 | ||
bbe63281 LP |
1157 | void log_show_location(bool b) { |
1158 | show_location = b; | |
1159 | } | |
1160 | ||
b1e90ec5 ZJS |
1161 | bool log_get_show_location(void) { |
1162 | return show_location; | |
1163 | } | |
1164 | ||
bbe63281 LP |
1165 | int log_show_color_from_string(const char *e) { |
1166 | int t; | |
1167 | ||
5ba081b0 LP |
1168 | t = parse_boolean(e); |
1169 | if (t < 0) | |
1170 | return t; | |
bbe63281 LP |
1171 | |
1172 | log_show_color(t); | |
1173 | return 0; | |
1174 | } | |
1175 | ||
1176 | int log_show_location_from_string(const char *e) { | |
1177 | int t; | |
1178 | ||
5ba081b0 LP |
1179 | t = parse_boolean(e); |
1180 | if (t < 0) | |
1181 | return t; | |
bbe63281 LP |
1182 | |
1183 | log_show_location(t); | |
1184 | return 0; | |
1185 | } | |
1186 | ||
81270860 | 1187 | bool log_on_console(void) { |
5b5688af ZJS |
1188 | if (IN_SET(log_target, LOG_TARGET_CONSOLE, |
1189 | LOG_TARGET_CONSOLE_PREFIXED)) | |
81270860 LP |
1190 | return true; |
1191 | ||
1192 | return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0; | |
1193 | } | |
1194 | ||
2c5859af | 1195 | static const char *const log_target_table[_LOG_TARGET_MAX] = { |
34f0e866 | 1196 | [LOG_TARGET_CONSOLE] = "console", |
aca83a53 | 1197 | [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed", |
34f0e866 | 1198 | [LOG_TARGET_KMSG] = "kmsg", |
5ba081b0 LP |
1199 | [LOG_TARGET_JOURNAL] = "journal", |
1200 | [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg", | |
1201 | [LOG_TARGET_SYSLOG] = "syslog", | |
843d2643 | 1202 | [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg", |
5ba081b0 | 1203 | [LOG_TARGET_AUTO] = "auto", |
6c347d50 | 1204 | [LOG_TARGET_NULL] = "null", |
34f0e866 LP |
1205 | }; |
1206 | ||
1207 | DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); | |
4daf54a8 ZJS |
1208 | |
1209 | void log_received_signal(int level, const struct signalfd_siginfo *si) { | |
a6ab2365 LP |
1210 | assert(si); |
1211 | ||
1212 | if (pid_is_valid(si->ssi_pid)) { | |
4daf54a8 ZJS |
1213 | _cleanup_free_ char *p = NULL; |
1214 | ||
a6ab2365 | 1215 | (void) get_process_comm(si->ssi_pid, &p); |
4daf54a8 ZJS |
1216 | |
1217 | log_full(level, | |
1fa2f38f | 1218 | "Received SIG%s from PID %"PRIu32" (%s).", |
4daf54a8 ZJS |
1219 | signal_to_string(si->ssi_signo), |
1220 | si->ssi_pid, strna(p)); | |
1221 | } else | |
1222 | log_full(level, | |
1223 | "Received SIG%s.", | |
1224 | signal_to_string(si->ssi_signo)); | |
4daf54a8 | 1225 | } |
c1dc6153 | 1226 | |
158350e8 LP |
1227 | int log_syntax_internal( |
1228 | const char *unit, | |
1229 | int level, | |
1230 | const char *config_file, | |
1231 | unsigned config_line, | |
1232 | int error, | |
1233 | const char *file, | |
1234 | int line, | |
1235 | const char *func, | |
1236 | const char *format, ...) { | |
1237 | ||
1238 | PROTECT_ERRNO; | |
1239 | char buffer[LINE_MAX]; | |
158350e8 | 1240 | va_list ap; |
c2dec702 | 1241 | const char *unit_fmt = NULL; |
158350e8 LP |
1242 | |
1243 | if (error < 0) | |
1244 | error = -error; | |
1245 | ||
ff524019 | 1246 | if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD])) |
158350e8 LP |
1247 | return -error; |
1248 | ||
1249 | if (log_target == LOG_TARGET_NULL) | |
1250 | return -error; | |
1251 | ||
d1a1f0aa | 1252 | errno = error; |
158350e8 LP |
1253 | |
1254 | va_start(ap, format); | |
4ad2b562 | 1255 | (void) vsnprintf(buffer, sizeof buffer, format, ap); |
158350e8 LP |
1256 | va_end(ap); |
1257 | ||
1258 | if (unit) | |
df0ff127 | 1259 | unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s"; |
c2dec702 ZJS |
1260 | |
1261 | return log_struct_internal( | |
ff524019 ZJS |
1262 | LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level), |
1263 | error, | |
c2dec702 | 1264 | file, line, func, |
2b044526 | 1265 | "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR, |
c2dec702 ZJS |
1266 | "CONFIG_FILE=%s", config_file, |
1267 | "CONFIG_LINE=%u", config_line, | |
e697dfef | 1268 | LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer), |
c2dec702 ZJS |
1269 | unit_fmt, unit, |
1270 | NULL); | |
158350e8 | 1271 | } |
48a601fe | 1272 | |
d04ce5a9 LP |
1273 | int log_syntax_invalid_utf8_internal( |
1274 | const char *unit, | |
1275 | int level, | |
1276 | const char *config_file, | |
1277 | unsigned config_line, | |
1278 | const char *file, | |
1279 | int line, | |
1280 | const char *func, | |
1281 | const char *rvalue) { | |
1282 | ||
1283 | _cleanup_free_ char *p = NULL; | |
1284 | ||
1285 | if (rvalue) | |
1286 | p = utf8_escape_invalid(rvalue); | |
1287 | ||
1288 | log_syntax_internal(unit, level, config_file, config_line, 0, file, line, func, | |
1289 | "String is not UTF-8 clean, ignoring assignment: %s", strna(p)); | |
1290 | ||
1291 | return -EINVAL; | |
1292 | } | |
1293 | ||
16e4fd87 LP |
1294 | void log_set_upgrade_syslog_to_journal(bool b) { |
1295 | upgrade_syslog_to_journal = b; | |
6fdb8de4 LP |
1296 | |
1297 | /* Make the change effective immediately */ | |
1298 | if (b) { | |
1299 | if (log_target == LOG_TARGET_SYSLOG) | |
1300 | log_target = LOG_TARGET_JOURNAL; | |
1301 | else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG) | |
1302 | log_target = LOG_TARGET_JOURNAL_OR_KMSG; | |
1303 | } | |
16e4fd87 LP |
1304 | } |
1305 | ||
48a601fe LP |
1306 | void log_set_always_reopen_console(bool b) { |
1307 | always_reopen_console = b; | |
1308 | } | |
16e4fd87 LP |
1309 | |
1310 | void log_set_open_when_needed(bool b) { | |
1311 | open_when_needed = b; | |
1312 | } | |
dccca82b | 1313 | |
adf47c91 LP |
1314 | void log_set_prohibit_ipc(bool b) { |
1315 | prohibit_ipc = b; | |
1316 | } | |
1317 | ||
dccca82b LP |
1318 | int log_emergency_level(void) { |
1319 | /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only | |
1320 | * then the system of the whole system is obviously affected. */ | |
1321 | ||
1322 | return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR; | |
1323 | } | |
17cac366 LP |
1324 | |
1325 | int log_dup_console(void) { | |
1326 | int copy; | |
1327 | ||
1328 | /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful | |
1329 | * whenever we want to continue logging through the original fd, but want to rearrange stderr. */ | |
1330 | ||
1331 | if (console_fd >= 3) | |
1332 | return 0; | |
1333 | ||
1334 | copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3); | |
1335 | if (copy < 0) | |
1336 | return -errno; | |
1337 | ||
1338 | console_fd = copy; | |
1339 | return 0; | |
1340 | } |