]>
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 | 403 | time_t t; |
e0f691e1 | 404 | struct tm tm; |
16801e90 LP |
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); | |
e0f691e1 | 412 | if (!localtime_r(&t, &tm)) |
16801e90 LP |
413 | return -EINVAL; |
414 | ||
e0f691e1 | 415 | if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0) |
16801e90 LP |
416 | return -EINVAL; |
417 | ||
df0ff127 | 418 | xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); |
16801e90 | 419 | |
e6a7ec4b LP |
420 | iovec[0] = IOVEC_MAKE_STRING(header_priority); |
421 | iovec[1] = IOVEC_MAKE_STRING(header_time); | |
422 | iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name); | |
423 | iovec[3] = IOVEC_MAKE_STRING(header_pid); | |
424 | iovec[4] = IOVEC_MAKE_STRING(buffer); | |
16801e90 | 425 | |
c899f8c6 | 426 | /* When using syslog via SOCK_STREAM separate the messages by NUL chars */ |
c31e1495 LP |
427 | if (syslog_is_stream) |
428 | iovec[4].iov_len++; | |
429 | ||
c31e1495 LP |
430 | for (;;) { |
431 | ssize_t n; | |
432 | ||
8f7f7a1b MS |
433 | n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL); |
434 | if (n < 0) | |
c31e1495 LP |
435 | return -errno; |
436 | ||
437 | if (!syslog_is_stream || | |
438 | (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec))) | |
439 | break; | |
440 | ||
441 | IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n); | |
442 | } | |
16801e90 | 443 | |
843d2643 | 444 | return 1; |
16801e90 LP |
445 | } |
446 | ||
447 | static int write_to_kmsg( | |
086891e5 LP |
448 | int level, |
449 | int error, | |
bcf5c276 | 450 | const char *file, |
086891e5 LP |
451 | int line, |
452 | const char *func, | |
086891e5 | 453 | const char *buffer) { |
16801e90 | 454 | |
5ffa8c81 ZJS |
455 | char header_priority[2 + DECIMAL_STR_MAX(int) + 1], |
456 | header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; | |
b92bea5d | 457 | struct iovec iovec[5] = {}; |
16801e90 LP |
458 | |
459 | if (kmsg_fd < 0) | |
843d2643 | 460 | return 0; |
16801e90 | 461 | |
5ffa8c81 | 462 | xsprintf(header_priority, "<%i>", level); |
df0ff127 | 463 | xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached()); |
16801e90 | 464 | |
e6a7ec4b LP |
465 | iovec[0] = IOVEC_MAKE_STRING(header_priority); |
466 | iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name); | |
467 | iovec[2] = IOVEC_MAKE_STRING(header_pid); | |
468 | iovec[3] = IOVEC_MAKE_STRING(buffer); | |
469 | iovec[4] = IOVEC_MAKE_STRING("\n"); | |
16801e90 LP |
470 | |
471 | if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) | |
472 | return -errno; | |
473 | ||
843d2643 | 474 | return 1; |
16801e90 LP |
475 | } |
476 | ||
086891e5 LP |
477 | static int log_do_header( |
478 | char *header, | |
479 | size_t size, | |
480 | int level, | |
481 | int error, | |
482 | const char *file, int line, const char *func, | |
4b58153d LP |
483 | const char *object_field, const char *object, |
484 | const char *extra_field, const char *extra) { | |
f8e6f4aa | 485 | int r; |
086891e5 | 486 | |
52d86690 ZJS |
487 | error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error); |
488 | ||
f8e6f4aa ZJS |
489 | r = snprintf(header, size, |
490 | "PRIORITY=%i\n" | |
491 | "SYSLOG_FACILITY=%i\n" | |
492 | "%s%.256s%s" /* CODE_FILE */ | |
493 | "%s%.*i%s" /* CODE_LINE */ | |
494 | "%s%.256s%s" /* CODE_FUNC */ | |
495 | "%s%.*i%s" /* ERRNO */ | |
496 | "%s%.256s%s" /* object */ | |
497 | "%s%.256s%s" /* extra */ | |
498 | "SYSLOG_IDENTIFIER=%.256s\n", | |
499 | LOG_PRI(level), | |
500 | LOG_FAC(level), | |
501 | isempty(file) ? "" : "CODE_FILE=", | |
502 | isempty(file) ? "" : file, | |
503 | isempty(file) ? "" : "\n", | |
504 | line ? "CODE_LINE=" : "", | |
505 | line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */ | |
506 | line ? "\n" : "", | |
507 | isempty(func) ? "" : "CODE_FUNC=", | |
508 | isempty(func) ? "" : func, | |
509 | isempty(func) ? "" : "\n", | |
510 | error ? "ERRNO=" : "", | |
511 | error ? 1 : 0, error, | |
512 | error ? "\n" : "", | |
513 | isempty(object) ? "" : object_field, | |
514 | isempty(object) ? "" : object, | |
515 | isempty(object) ? "" : "\n", | |
516 | isempty(extra) ? "" : extra_field, | |
517 | isempty(extra) ? "" : extra, | |
518 | isempty(extra) ? "" : "\n", | |
519 | program_invocation_short_name); | |
780747da | 520 | assert_raw((size_t) r < size); |
086891e5 | 521 | |
41a79f10 ZJS |
522 | return 0; |
523 | } | |
5ba081b0 | 524 | |
41a79f10 | 525 | static int write_to_journal( |
086891e5 LP |
526 | int level, |
527 | int error, | |
bcf5c276 | 528 | const char *file, |
086891e5 LP |
529 | int line, |
530 | const char *func, | |
531 | const char *object_field, | |
532 | const char *object, | |
4b58153d LP |
533 | const char *extra_field, |
534 | const char *extra, | |
086891e5 | 535 | const char *buffer) { |
41a79f10 ZJS |
536 | |
537 | char header[LINE_MAX]; | |
b92bea5d ZJS |
538 | struct iovec iovec[4] = {}; |
539 | struct msghdr mh = {}; | |
41a79f10 ZJS |
540 | |
541 | if (journal_fd < 0) | |
542 | return 0; | |
543 | ||
4b58153d | 544 | log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra); |
5ba081b0 | 545 | |
e6a7ec4b LP |
546 | iovec[0] = IOVEC_MAKE_STRING(header); |
547 | iovec[1] = IOVEC_MAKE_STRING("MESSAGE="); | |
548 | iovec[2] = IOVEC_MAKE_STRING(buffer); | |
549 | iovec[3] = IOVEC_MAKE_STRING("\n"); | |
5ba081b0 | 550 | |
5ba081b0 LP |
551 | mh.msg_iov = iovec; |
552 | mh.msg_iovlen = ELEMENTSOF(iovec); | |
553 | ||
554 | if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0) | |
555 | return -errno; | |
556 | ||
557 | return 1; | |
558 | } | |
559 | ||
93484b47 | 560 | int log_dispatch_internal( |
086891e5 LP |
561 | int level, |
562 | int error, | |
95066a90 | 563 | const char *file, |
086891e5 LP |
564 | int line, |
565 | const char *func, | |
566 | const char *object_field, | |
567 | const char *object, | |
4b58153d | 568 | const char *extra_field, |
4dd09c6a | 569 | const char *extra, |
086891e5 | 570 | char *buffer) { |
843d2643 | 571 | |
780747da | 572 | assert_raw(buffer); |
843d2643 | 573 | |
9fae33d2 | 574 | if (log_target == LOG_TARGET_NULL) |
52d86690 | 575 | return -ERRNO_VALUE(error); |
9fae33d2 | 576 | |
29db5834 | 577 | /* Patch in LOG_DAEMON facility if necessary */ |
7d76f312 | 578 | if ((level & LOG_FACMASK) == 0) |
52d86690 | 579 | level |= log_facility; |
29db5834 | 580 | |
16e4fd87 LP |
581 | if (open_when_needed) |
582 | log_open(); | |
583 | ||
9726b29e LP |
584 | do { |
585 | char *e; | |
9499b235 | 586 | int k = 0; |
843d2643 | 587 | |
9726b29e | 588 | buffer += strspn(buffer, NEWLINE); |
843d2643 | 589 | |
9726b29e LP |
590 | if (buffer[0] == 0) |
591 | break; | |
843d2643 | 592 | |
9726b29e LP |
593 | if ((e = strpbrk(buffer, NEWLINE))) |
594 | *(e++) = 0; | |
843d2643 | 595 | |
5b5688af ZJS |
596 | if (IN_SET(log_target, LOG_TARGET_AUTO, |
597 | LOG_TARGET_JOURNAL_OR_KMSG, | |
598 | LOG_TARGET_JOURNAL)) { | |
5ba081b0 | 599 | |
4b58153d | 600 | k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer); |
e38b8a40 LP |
601 | if (k < 0 && k != -EAGAIN) |
602 | log_close_journal(); | |
5ba081b0 LP |
603 | } |
604 | ||
5b5688af ZJS |
605 | if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG, |
606 | LOG_TARGET_SYSLOG)) { | |
9726b29e | 607 | |
4b58153d | 608 | k = write_to_syslog(level, error, file, line, func, buffer); |
e38b8a40 LP |
609 | if (k < 0 && k != -EAGAIN) |
610 | log_close_syslog(); | |
9726b29e LP |
611 | } |
612 | ||
9499b235 | 613 | if (k <= 0 && |
5b5688af | 614 | IN_SET(log_target, LOG_TARGET_AUTO, |
5b5688af ZJS |
615 | LOG_TARGET_SYSLOG_OR_KMSG, |
616 | LOG_TARGET_JOURNAL_OR_KMSG, | |
617 | LOG_TARGET_KMSG)) { | |
9726b29e | 618 | |
e38b8a40 LP |
619 | if (k < 0) |
620 | log_open_kmsg(); | |
621 | ||
4b58153d | 622 | k = write_to_kmsg(level, error, file, line, func, buffer); |
4e7bc3f3 LP |
623 | if (k < 0) { |
624 | log_close_kmsg(); | |
9726b29e | 625 | log_open_console(); |
bf371116 | 626 | } |
9726b29e LP |
627 | } |
628 | ||
bf371116 | 629 | if (k <= 0) |
4b58153d | 630 | (void) write_to_console(level, error, file, line, func, buffer); |
9726b29e LP |
631 | |
632 | buffer = e; | |
633 | } while (buffer); | |
634 | ||
16e4fd87 LP |
635 | if (open_when_needed) |
636 | log_close(); | |
637 | ||
52d86690 | 638 | return -ERRNO_VALUE(error); |
843d2643 LP |
639 | } |
640 | ||
2149e37c | 641 | int log_dump_internal( |
ff524019 ZJS |
642 | int level, |
643 | int error, | |
644 | const char *file, | |
645 | int line, | |
646 | const char *func, | |
647 | char *buffer) { | |
2149e37c | 648 | |
ff524019 | 649 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
5c0aa72a | 650 | PROTECT_ERRNO; |
2149e37c LP |
651 | |
652 | /* This modifies the buffer... */ | |
653 | ||
ff524019 | 654 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
52d86690 | 655 | return -ERRNO_VALUE(error); |
2149e37c | 656 | |
93484b47 | 657 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); |
2149e37c LP |
658 | } |
659 | ||
ff524019 | 660 | int log_internalv_realm( |
086891e5 LP |
661 | int level, |
662 | int error, | |
bcf5c276 | 663 | const char *file, |
086891e5 LP |
664 | int line, |
665 | const char *func, | |
666 | const char *format, | |
667 | va_list ap) { | |
16801e90 | 668 | |
ff524019 | 669 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
addab137 | 670 | char buffer[LINE_MAX]; |
ff524019 | 671 | PROTECT_ERRNO; |
16801e90 | 672 | |
ff524019 | 673 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
52d86690 | 674 | return -ERRNO_VALUE(error); |
16801e90 | 675 | |
b29f6480 | 676 | /* Make sure that %m maps to the specified error (or "Success"). */ |
52d86690 | 677 | errno = ERRNO_VALUE(error); |
086891e5 | 678 | |
4ad2b562 | 679 | (void) vsnprintf(buffer, sizeof buffer, format, ap); |
843d2643 | 680 | |
93484b47 | 681 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer); |
185986c6 | 682 | } |
16801e90 | 683 | |
ff524019 | 684 | int log_internal_realm( |
086891e5 LP |
685 | int level, |
686 | int error, | |
bcf5c276 | 687 | const char *file, |
086891e5 LP |
688 | int line, |
689 | const char *func, | |
690 | const char *format, ...) { | |
17a94911 | 691 | |
17a94911 | 692 | va_list ap; |
bf371116 | 693 | int r; |
17a94911 LP |
694 | |
695 | va_start(ap, format); | |
ff524019 | 696 | r = log_internalv_realm(level, error, file, line, func, format, ap); |
17a94911 LP |
697 | va_end(ap); |
698 | ||
699 | return r; | |
700 | } | |
701 | ||
a3bf7137 ZJS |
702 | _printf_(10,0) |
703 | static int log_object_internalv( | |
086891e5 LP |
704 | int level, |
705 | int error, | |
bcf5c276 | 706 | const char *file, |
086891e5 LP |
707 | int line, |
708 | const char *func, | |
709 | const char *object_field, | |
710 | const char *object, | |
4b58153d LP |
711 | const char *extra_field, |
712 | const char *extra, | |
086891e5 LP |
713 | const char *format, |
714 | va_list ap) { | |
fdf9f9bb | 715 | |
5c0aa72a | 716 | PROTECT_ERRNO; |
f2341e0a | 717 | char *buffer, *b; |
fdf9f9bb | 718 | |
ff524019 | 719 | if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD])) |
52d86690 | 720 | return -ERRNO_VALUE(error); |
fdf9f9bb | 721 | |
d1a1f0aa | 722 | /* Make sure that %m maps to the specified error (or "Success"). */ |
52d86690 | 723 | errno = ERRNO_VALUE(error); |
086891e5 | 724 | |
f2341e0a LP |
725 | /* Prepend the object name before the message */ |
726 | if (object) { | |
727 | size_t n; | |
728 | ||
729 | n = strlen(object); | |
1741b25c | 730 | buffer = newa(char, n + 2 + LINE_MAX); |
f2341e0a | 731 | b = stpcpy(stpcpy(buffer, object), ": "); |
1741b25c EV |
732 | } else |
733 | b = buffer = newa(char, LINE_MAX); | |
f2341e0a | 734 | |
4ad2b562 | 735 | (void) vsnprintf(b, LINE_MAX, format, ap); |
fdf9f9bb | 736 | |
93484b47 ZJS |
737 | return log_dispatch_internal(level, error, file, line, func, |
738 | object_field, object, extra_field, extra, buffer); | |
fdf9f9bb ZJS |
739 | } |
740 | ||
79008bdd | 741 | int log_object_internal( |
086891e5 LP |
742 | int level, |
743 | int error, | |
bcf5c276 | 744 | const char *file, |
086891e5 LP |
745 | int line, |
746 | const char *func, | |
747 | const char *object_field, | |
748 | const char *object, | |
4b58153d LP |
749 | const char *extra_field, |
750 | const char *extra, | |
086891e5 | 751 | const char *format, ...) { |
fdf9f9bb | 752 | |
fdf9f9bb | 753 | va_list ap; |
bf371116 | 754 | int r; |
fdf9f9bb ZJS |
755 | |
756 | va_start(ap, format); | |
4b58153d | 757 | r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap); |
fdf9f9bb ZJS |
758 | va_end(ap); |
759 | ||
760 | return r; | |
761 | } | |
762 | ||
086891e5 LP |
763 | static void log_assert( |
764 | int level, | |
765 | const char *text, | |
766 | const char *file, | |
767 | int line, | |
768 | const char *func, | |
769 | const char *format) { | |
770 | ||
addab137 | 771 | static char buffer[LINE_MAX]; |
ff524019 | 772 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
185986c6 | 773 | |
ff524019 | 774 | if (_likely_(LOG_PRI(level) > log_max_level[realm])) |
50f72bca ZJS |
775 | return; |
776 | ||
bcfce235 | 777 | DISABLE_WARNING_FORMAT_NONLITERAL; |
4ad2b562 | 778 | (void) snprintf(buffer, sizeof buffer, format, text, file, line, func); |
bcfce235 | 779 | REENABLE_WARNING; |
185986c6 | 780 | |
185986c6 LP |
781 | log_abort_msg = buffer; |
782 | ||
93484b47 | 783 | log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer); |
5899f3b7 | 784 | } |
34f0e866 | 785 | |
848e863a | 786 | _noreturn_ void log_assert_failed_realm( |
ff524019 ZJS |
787 | LogRealm realm, |
788 | const char *text, | |
789 | const char *file, | |
790 | int line, | |
791 | const char *func) { | |
ea89a119 | 792 | log_open(); |
ff524019 ZJS |
793 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, |
794 | "Assertion '%s' failed at %s:%u, function %s(). Aborting."); | |
80514f9c | 795 | abort(); |
b7f33638 MS |
796 | } |
797 | ||
848e863a | 798 | _noreturn_ void log_assert_failed_unreachable_realm( |
ff524019 ZJS |
799 | LogRealm realm, |
800 | const char *text, | |
801 | const char *file, | |
802 | int line, | |
803 | const char *func) { | |
ea89a119 | 804 | log_open(); |
ff524019 ZJS |
805 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func, |
806 | "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); | |
80514f9c LP |
807 | abort(); |
808 | } | |
809 | ||
ff524019 ZJS |
810 | void log_assert_failed_return_realm( |
811 | LogRealm realm, | |
812 | const char *text, | |
813 | const char *file, | |
814 | int line, | |
815 | const char *func) { | |
e5ca092c | 816 | PROTECT_ERRNO; |
ff524019 ZJS |
817 | log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func, |
818 | "Assertion '%s' failed at %s:%u, function %s(). Ignoring."); | |
b7f33638 MS |
819 | } |
820 | ||
ff524019 | 821 | int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) { |
7558e10c LP |
822 | return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR), |
823 | ENOMEM, file, line, func, "Out of memory."); | |
6dc1e7e0 MS |
824 | } |
825 | ||
8a03c9ef ZJS |
826 | int log_format_iovec( |
827 | struct iovec *iovec, | |
d3070fbd LP |
828 | size_t iovec_len, |
829 | size_t *n, | |
8a03c9ef ZJS |
830 | bool newline_separator, |
831 | int error, | |
832 | const char *format, | |
833 | va_list ap) { | |
834 | ||
835 | static const char nl = '\n'; | |
836 | ||
837 | while (format && *n + 1 < iovec_len) { | |
838 | va_list aq; | |
839 | char *m; | |
840 | int r; | |
841 | ||
842 | /* We need to copy the va_list structure, | |
843 | * since vasprintf() leaves it afterwards at | |
844 | * an undefined location */ | |
845 | ||
52d86690 | 846 | errno = ERRNO_VALUE(error); |
8a03c9ef ZJS |
847 | |
848 | va_copy(aq, ap); | |
849 | r = vasprintf(&m, format, aq); | |
850 | va_end(aq); | |
851 | if (r < 0) | |
852 | return -EINVAL; | |
853 | ||
854 | /* Now, jump enough ahead, so that we point to | |
855 | * the next format string */ | |
856 | VA_FORMAT_ADVANCE(format, ap); | |
857 | ||
e6a7ec4b | 858 | iovec[(*n)++] = IOVEC_MAKE_STRING(m); |
8a03c9ef ZJS |
859 | |
860 | if (newline_separator) { | |
861 | iovec[*n].iov_base = (char*) &nl; | |
862 | iovec[*n].iov_len = 1; | |
863 | (*n)++; | |
864 | } | |
865 | ||
866 | format = va_arg(ap, char *); | |
867 | } | |
868 | return 0; | |
869 | } | |
870 | ||
877d54e9 LP |
871 | int log_struct_internal( |
872 | int level, | |
086891e5 | 873 | int error, |
877d54e9 LP |
874 | const char *file, |
875 | int line, | |
876 | const char *func, | |
877 | const char *format, ...) { | |
878 | ||
e6a7ec4b | 879 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); |
bf371116 LP |
880 | char buf[LINE_MAX]; |
881 | bool found = false; | |
5c0aa72a | 882 | PROTECT_ERRNO; |
877d54e9 | 883 | va_list ap; |
bf371116 | 884 | |
52d86690 ZJS |
885 | if (_likely_(LOG_PRI(level) > log_max_level[realm]) || |
886 | log_target == LOG_TARGET_NULL) | |
887 | return -ERRNO_VALUE(error); | |
877d54e9 LP |
888 | |
889 | if ((level & LOG_FACMASK) == 0) | |
52d86690 | 890 | level |= log_facility; |
877d54e9 | 891 | |
16e4fd87 LP |
892 | if (IN_SET(log_target, |
893 | LOG_TARGET_AUTO, | |
894 | LOG_TARGET_JOURNAL_OR_KMSG, | |
895 | LOG_TARGET_JOURNAL)) { | |
896 | ||
897 | if (open_when_needed) | |
898 | log_open_journal(); | |
899 | ||
900 | if (journal_fd >= 0) { | |
901 | char header[LINE_MAX]; | |
902 | struct iovec iovec[17] = {}; | |
d3070fbd | 903 | size_t n = 0, i; |
16e4fd87 LP |
904 | int r; |
905 | struct msghdr mh = { | |
906 | .msg_iov = iovec, | |
907 | }; | |
908 | bool fallback = false; | |
909 | ||
52d86690 ZJS |
910 | /* If the journal is available do structured logging. |
911 | * Do not report the errno if it is synthetic. */ | |
16e4fd87 LP |
912 | log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL); |
913 | iovec[n++] = IOVEC_MAKE_STRING(header); | |
914 | ||
915 | va_start(ap, format); | |
916 | r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap); | |
917 | if (r < 0) | |
918 | fallback = true; | |
919 | else { | |
920 | mh.msg_iovlen = n; | |
921 | (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL); | |
922 | } | |
877d54e9 | 923 | |
16e4fd87 LP |
924 | va_end(ap); |
925 | for (i = 1; i < n; i += 2) | |
926 | free(iovec[i].iov_base); | |
877d54e9 | 927 | |
16e4fd87 LP |
928 | if (!fallback) { |
929 | if (open_when_needed) | |
930 | log_close(); | |
877d54e9 | 931 | |
52d86690 | 932 | return -ERRNO_VALUE(error); |
16e4fd87 LP |
933 | } |
934 | } | |
bf371116 | 935 | } |
877d54e9 | 936 | |
bf371116 | 937 | /* Fallback if journal logging is not available or didn't work. */ |
6357ac66 | 938 | |
bf371116 LP |
939 | va_start(ap, format); |
940 | while (format) { | |
941 | va_list aq; | |
877d54e9 | 942 | |
52d86690 | 943 | errno = ERRNO_VALUE(error); |
877d54e9 | 944 | |
bf371116 | 945 | va_copy(aq, ap); |
4ad2b562 | 946 | (void) vsnprintf(buf, sizeof buf, format, aq); |
bf371116 | 947 | va_end(aq); |
963ddb91 | 948 | |
bf371116 LP |
949 | if (startswith(buf, "MESSAGE=")) { |
950 | found = true; | |
951 | break; | |
877d54e9 | 952 | } |
877d54e9 | 953 | |
bf371116 LP |
954 | VA_FORMAT_ADVANCE(format, ap); |
955 | ||
956 | format = va_arg(ap, char *); | |
877d54e9 | 957 | } |
bf371116 | 958 | va_end(ap); |
877d54e9 | 959 | |
16e4fd87 LP |
960 | if (!found) { |
961 | if (open_when_needed) | |
962 | log_close(); | |
963 | ||
52d86690 | 964 | return -ERRNO_VALUE(error); |
16e4fd87 | 965 | } |
bf371116 | 966 | |
93484b47 | 967 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8); |
877d54e9 LP |
968 | } |
969 | ||
915b1d01 LP |
970 | int log_struct_iovec_internal( |
971 | int level, | |
972 | int error, | |
973 | const char *file, | |
974 | int line, | |
975 | const char *func, | |
976 | const struct iovec input_iovec[], | |
977 | size_t n_input_iovec) { | |
978 | ||
979 | LogRealm realm = LOG_REALM_REMOVE_LEVEL(level); | |
980 | PROTECT_ERRNO; | |
981 | size_t i; | |
982 | char *m; | |
983 | ||
52d86690 ZJS |
984 | if (_likely_(LOG_PRI(level) > log_max_level[realm]) || |
985 | log_target == LOG_TARGET_NULL) | |
986 | return -ERRNO_VALUE(error); | |
915b1d01 LP |
987 | |
988 | if ((level & LOG_FACMASK) == 0) | |
52d86690 | 989 | level |= log_facility; |
915b1d01 LP |
990 | |
991 | if (IN_SET(log_target, LOG_TARGET_AUTO, | |
992 | LOG_TARGET_JOURNAL_OR_KMSG, | |
993 | LOG_TARGET_JOURNAL) && | |
994 | journal_fd >= 0) { | |
995 | ||
996 | struct iovec iovec[1 + n_input_iovec*2]; | |
997 | char header[LINE_MAX]; | |
998 | struct msghdr mh = { | |
999 | .msg_iov = iovec, | |
1000 | .msg_iovlen = 1 + n_input_iovec*2, | |
1001 | }; | |
1002 | ||
1003 | log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL); | |
1004 | iovec[0] = IOVEC_MAKE_STRING(header); | |
1005 | ||
1006 | for (i = 0; i < n_input_iovec; i++) { | |
1007 | iovec[1+i*2] = input_iovec[i]; | |
1008 | iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n"); | |
1009 | } | |
1010 | ||
1011 | if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0) | |
52d86690 | 1012 | return -ERRNO_VALUE(error); |
915b1d01 LP |
1013 | } |
1014 | ||
d27b725a LP |
1015 | for (i = 0; i < n_input_iovec; i++) |
1016 | if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) | |
915b1d01 | 1017 | break; |
915b1d01 LP |
1018 | |
1019 | if (_unlikely_(i >= n_input_iovec)) /* Couldn't find MESSAGE=? */ | |
52d86690 | 1020 | return -ERRNO_VALUE(error); |
915b1d01 | 1021 | |
fbd0b64f LP |
1022 | m = strndupa(input_iovec[i].iov_base + STRLEN("MESSAGE="), |
1023 | input_iovec[i].iov_len - STRLEN("MESSAGE=")); | |
915b1d01 LP |
1024 | |
1025 | return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m); | |
1026 | } | |
1027 | ||
34f0e866 LP |
1028 | int log_set_target_from_string(const char *e) { |
1029 | LogTarget t; | |
1030 | ||
5ba081b0 LP |
1031 | t = log_target_from_string(e); |
1032 | if (t < 0) | |
34f0e866 LP |
1033 | return -EINVAL; |
1034 | ||
1035 | log_set_target(t); | |
1036 | return 0; | |
1037 | } | |
1038 | ||
ff524019 | 1039 | int log_set_max_level_from_string_realm(LogRealm realm, const char *e) { |
34f0e866 LP |
1040 | int t; |
1041 | ||
5ba081b0 LP |
1042 | t = log_level_from_string(e); |
1043 | if (t < 0) | |
737af734 | 1044 | return -EINVAL; |
34f0e866 | 1045 | |
ff524019 | 1046 | log_set_max_level_realm(realm, t); |
34f0e866 LP |
1047 | return 0; |
1048 | } | |
1049 | ||
96287a49 | 1050 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
1de1c9c3 LP |
1051 | |
1052 | /* | |
1053 | * The systemd.log_xyz= settings are parsed by all tools, and | |
1054 | * so is "debug". | |
1055 | * | |
5e07a79e LP |
1056 | * However, "quiet" is only parsed by PID 1, and only turns of |
1057 | * status output to /dev/console, but does not alter the log | |
1058 | * level. | |
1de1c9c3 LP |
1059 | */ |
1060 | ||
1061 | if (streq(key, "debug") && !value) | |
1062 | log_set_max_level(LOG_DEBUG); | |
1063 | ||
1d84ad94 LP |
1064 | else if (proc_cmdline_key_streq(key, "systemd.log_target")) { |
1065 | ||
1066 | if (proc_cmdline_value_missing(key, value)) | |
1067 | return 0; | |
1de1c9c3 LP |
1068 | |
1069 | if (log_set_target_from_string(value) < 0) | |
1070 | log_warning("Failed to parse log target '%s'. Ignoring.", value); | |
1071 | ||
1d84ad94 LP |
1072 | } else if (proc_cmdline_key_streq(key, "systemd.log_level")) { |
1073 | ||
1074 | if (proc_cmdline_value_missing(key, value)) | |
1075 | return 0; | |
1de1c9c3 LP |
1076 | |
1077 | if (log_set_max_level_from_string(value) < 0) | |
1078 | log_warning("Failed to parse log level '%s'. Ignoring.", value); | |
1079 | ||
1d84ad94 | 1080 | } else if (proc_cmdline_key_streq(key, "systemd.log_color")) { |
1de1c9c3 | 1081 | |
1d84ad94 | 1082 | if (log_show_color_from_string(value ?: "1") < 0) |
1de1c9c3 LP |
1083 | log_warning("Failed to parse log color setting '%s'. Ignoring.", value); |
1084 | ||
1d84ad94 | 1085 | } else if (proc_cmdline_key_streq(key, "systemd.log_location")) { |
1de1c9c3 | 1086 | |
1d84ad94 | 1087 | if (log_show_location_from_string(value ?: "1") < 0) |
1de1c9c3 LP |
1088 | log_warning("Failed to parse log location setting '%s'. Ignoring.", value); |
1089 | } | |
1090 | ||
1091 | return 0; | |
1092 | } | |
1093 | ||
ff524019 | 1094 | void log_parse_environment_realm(LogRealm realm) { |
e3e42fc2 ZJS |
1095 | /* Do not call from library code. */ |
1096 | ||
34f0e866 | 1097 | const char *e; |
b8d0ffc2 | 1098 | |
ee46e555 | 1099 | if (get_ctty_devnr(0, NULL) < 0) |
1d84ad94 LP |
1100 | /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is |
1101 | user stuff. */ | |
1102 | (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); | |
34f0e866 | 1103 | |
a99e002c | 1104 | e = getenv("SYSTEMD_LOG_TARGET"); |
88fae6e0 | 1105 | if (e && log_set_target_from_string(e) < 0) |
f0ea29ea | 1106 | log_warning("Failed to parse log target '%s'. Ignoring.", e); |
34f0e866 | 1107 | |
a99e002c | 1108 | e = getenv("SYSTEMD_LOG_LEVEL"); |
ff524019 | 1109 | if (e && log_set_max_level_from_string_realm(realm, e) < 0) |
f0ea29ea | 1110 | log_warning("Failed to parse log level '%s'. Ignoring.", e); |
bbe63281 | 1111 | |
a99e002c | 1112 | e = getenv("SYSTEMD_LOG_COLOR"); |
88fae6e0 | 1113 | if (e && log_show_color_from_string(e) < 0) |
f0ea29ea | 1114 | log_warning("Failed to parse bool '%s'. Ignoring.", e); |
bbe63281 | 1115 | |
a99e002c | 1116 | e = getenv("SYSTEMD_LOG_LOCATION"); |
88fae6e0 | 1117 | if (e && log_show_location_from_string(e) < 0) |
f0ea29ea | 1118 | log_warning("Failed to parse bool '%s'. Ignoring.", e); |
34f0e866 LP |
1119 | } |
1120 | ||
1adf1049 LP |
1121 | LogTarget log_get_target(void) { |
1122 | return log_target; | |
1123 | } | |
1124 | ||
ff524019 ZJS |
1125 | int log_get_max_level_realm(LogRealm realm) { |
1126 | return log_max_level[realm]; | |
1adf1049 LP |
1127 | } |
1128 | ||
bbe63281 LP |
1129 | void log_show_color(bool b) { |
1130 | show_color = b; | |
1131 | } | |
1132 | ||
b1e90ec5 ZJS |
1133 | bool log_get_show_color(void) { |
1134 | return show_color; | |
1135 | } | |
1136 | ||
bbe63281 LP |
1137 | void log_show_location(bool b) { |
1138 | show_location = b; | |
1139 | } | |
1140 | ||
b1e90ec5 ZJS |
1141 | bool log_get_show_location(void) { |
1142 | return show_location; | |
1143 | } | |
1144 | ||
bbe63281 LP |
1145 | int log_show_color_from_string(const char *e) { |
1146 | int t; | |
1147 | ||
5ba081b0 LP |
1148 | t = parse_boolean(e); |
1149 | if (t < 0) | |
1150 | return t; | |
bbe63281 LP |
1151 | |
1152 | log_show_color(t); | |
1153 | return 0; | |
1154 | } | |
1155 | ||
1156 | int log_show_location_from_string(const char *e) { | |
1157 | int t; | |
1158 | ||
5ba081b0 LP |
1159 | t = parse_boolean(e); |
1160 | if (t < 0) | |
1161 | return t; | |
bbe63281 LP |
1162 | |
1163 | log_show_location(t); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
81270860 | 1167 | bool log_on_console(void) { |
5b5688af ZJS |
1168 | if (IN_SET(log_target, LOG_TARGET_CONSOLE, |
1169 | LOG_TARGET_CONSOLE_PREFIXED)) | |
81270860 LP |
1170 | return true; |
1171 | ||
1172 | return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0; | |
1173 | } | |
1174 | ||
2c5859af | 1175 | static const char *const log_target_table[_LOG_TARGET_MAX] = { |
34f0e866 | 1176 | [LOG_TARGET_CONSOLE] = "console", |
aca83a53 | 1177 | [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed", |
34f0e866 | 1178 | [LOG_TARGET_KMSG] = "kmsg", |
5ba081b0 LP |
1179 | [LOG_TARGET_JOURNAL] = "journal", |
1180 | [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg", | |
1181 | [LOG_TARGET_SYSLOG] = "syslog", | |
843d2643 | 1182 | [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg", |
5ba081b0 | 1183 | [LOG_TARGET_AUTO] = "auto", |
6c347d50 | 1184 | [LOG_TARGET_NULL] = "null", |
34f0e866 LP |
1185 | }; |
1186 | ||
1187 | DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); | |
4daf54a8 ZJS |
1188 | |
1189 | void log_received_signal(int level, const struct signalfd_siginfo *si) { | |
a6ab2365 LP |
1190 | assert(si); |
1191 | ||
1192 | if (pid_is_valid(si->ssi_pid)) { | |
4daf54a8 ZJS |
1193 | _cleanup_free_ char *p = NULL; |
1194 | ||
a6ab2365 | 1195 | (void) get_process_comm(si->ssi_pid, &p); |
4daf54a8 ZJS |
1196 | |
1197 | log_full(level, | |
1fa2f38f | 1198 | "Received SIG%s from PID %"PRIu32" (%s).", |
4daf54a8 ZJS |
1199 | signal_to_string(si->ssi_signo), |
1200 | si->ssi_pid, strna(p)); | |
1201 | } else | |
1202 | log_full(level, | |
1203 | "Received SIG%s.", | |
1204 | signal_to_string(si->ssi_signo)); | |
4daf54a8 | 1205 | } |
c1dc6153 | 1206 | |
158350e8 LP |
1207 | int log_syntax_internal( |
1208 | const char *unit, | |
1209 | int level, | |
1210 | const char *config_file, | |
1211 | unsigned config_line, | |
1212 | int error, | |
1213 | const char *file, | |
1214 | int line, | |
1215 | const char *func, | |
1216 | const char *format, ...) { | |
1217 | ||
1218 | PROTECT_ERRNO; | |
1219 | char buffer[LINE_MAX]; | |
158350e8 | 1220 | va_list ap; |
c2dec702 | 1221 | const char *unit_fmt = NULL; |
158350e8 | 1222 | |
52d86690 ZJS |
1223 | if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]) || |
1224 | log_target == LOG_TARGET_NULL) | |
1225 | return -ERRNO_VALUE(error); | |
158350e8 | 1226 | |
d1a1f0aa | 1227 | errno = error; |
158350e8 LP |
1228 | |
1229 | va_start(ap, format); | |
4ad2b562 | 1230 | (void) vsnprintf(buffer, sizeof buffer, format, ap); |
158350e8 LP |
1231 | va_end(ap); |
1232 | ||
1233 | if (unit) | |
df0ff127 | 1234 | unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s"; |
c2dec702 ZJS |
1235 | |
1236 | return log_struct_internal( | |
ff524019 ZJS |
1237 | LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level), |
1238 | error, | |
c2dec702 | 1239 | file, line, func, |
2b044526 | 1240 | "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR, |
c2dec702 ZJS |
1241 | "CONFIG_FILE=%s", config_file, |
1242 | "CONFIG_LINE=%u", config_line, | |
e697dfef | 1243 | LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer), |
c2dec702 ZJS |
1244 | unit_fmt, unit, |
1245 | NULL); | |
158350e8 | 1246 | } |
48a601fe | 1247 | |
d04ce5a9 LP |
1248 | int log_syntax_invalid_utf8_internal( |
1249 | const char *unit, | |
1250 | int level, | |
1251 | const char *config_file, | |
1252 | unsigned config_line, | |
1253 | const char *file, | |
1254 | int line, | |
1255 | const char *func, | |
1256 | const char *rvalue) { | |
1257 | ||
1258 | _cleanup_free_ char *p = NULL; | |
1259 | ||
1260 | if (rvalue) | |
1261 | p = utf8_escape_invalid(rvalue); | |
1262 | ||
1263 | log_syntax_internal(unit, level, config_file, config_line, 0, file, line, func, | |
1264 | "String is not UTF-8 clean, ignoring assignment: %s", strna(p)); | |
1265 | ||
1266 | return -EINVAL; | |
1267 | } | |
1268 | ||
16e4fd87 LP |
1269 | void log_set_upgrade_syslog_to_journal(bool b) { |
1270 | upgrade_syslog_to_journal = b; | |
6fdb8de4 LP |
1271 | |
1272 | /* Make the change effective immediately */ | |
1273 | if (b) { | |
1274 | if (log_target == LOG_TARGET_SYSLOG) | |
1275 | log_target = LOG_TARGET_JOURNAL; | |
1276 | else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG) | |
1277 | log_target = LOG_TARGET_JOURNAL_OR_KMSG; | |
1278 | } | |
16e4fd87 LP |
1279 | } |
1280 | ||
48a601fe LP |
1281 | void log_set_always_reopen_console(bool b) { |
1282 | always_reopen_console = b; | |
1283 | } | |
16e4fd87 LP |
1284 | |
1285 | void log_set_open_when_needed(bool b) { | |
1286 | open_when_needed = b; | |
1287 | } | |
dccca82b | 1288 | |
adf47c91 LP |
1289 | void log_set_prohibit_ipc(bool b) { |
1290 | prohibit_ipc = b; | |
1291 | } | |
1292 | ||
dccca82b LP |
1293 | int log_emergency_level(void) { |
1294 | /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only | |
1295 | * then the system of the whole system is obviously affected. */ | |
1296 | ||
1297 | return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR; | |
1298 | } | |
17cac366 LP |
1299 | |
1300 | int log_dup_console(void) { | |
1301 | int copy; | |
1302 | ||
1303 | /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful | |
1304 | * whenever we want to continue logging through the original fd, but want to rearrange stderr. */ | |
1305 | ||
1306 | if (console_fd >= 3) | |
1307 | return 0; | |
1308 | ||
1309 | copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3); | |
1310 | if (copy < 0) | |
1311 | return -errno; | |
1312 | ||
1313 | console_fd = copy; | |
1314 | return 0; | |
1315 | } | |
6bf3c61c LP |
1316 | |
1317 | void log_setup_service(void) { | |
1318 | /* Sets up logging the way it is most appropriate for running a program as a service. Note that using this | |
1319 | * doesn't make the binary unsuitable for invocation on the command line, as log output will still go to the | |
1320 | * terminal if invoked interactively. */ | |
1321 | ||
1322 | log_set_target(LOG_TARGET_AUTO); | |
1323 | log_parse_environment(); | |
1324 | log_open(); | |
1325 | } |