]>
Commit | Line | Data |
---|---|---|
5899f3b7 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
5899f3b7 LP |
22 | #include <stdarg.h> |
23 | #include <stdio.h> | |
c9b97d2a | 24 | #include <errno.h> |
16801e90 LP |
25 | #include <unistd.h> |
26 | #include <fcntl.h> | |
27 | #include <sys/socket.h> | |
28 | #include <sys/un.h> | |
5899f3b7 LP |
29 | |
30 | #include "log.h" | |
16801e90 LP |
31 | #include "util.h" |
32 | #include "macro.h" | |
5899f3b7 | 33 | |
16801e90 LP |
34 | #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC) |
35 | #define LOG_BUFFER_MAX 1024 | |
36 | ||
37 | static LogTarget log_target = LOG_TARGET_CONSOLE; | |
38 | static int log_max_level = LOG_DEBUG; | |
39 | ||
843d2643 | 40 | static int console_fd = STDERR_FILENO; |
16801e90 LP |
41 | static int syslog_fd = -1; |
42 | static int kmsg_fd = -1; | |
43 | ||
185986c6 LP |
44 | /* Akin to glibc's __abort_msg; which is private and we hance cannot |
45 | * use here. */ | |
46 | static char *log_abort_msg = NULL; | |
47 | ||
843d2643 LP |
48 | void log_close_console(void) { |
49 | ||
50 | if (console_fd < 0) | |
51 | return; | |
16801e90 | 52 | |
c8513d54 LP |
53 | if (getpid() == 1) { |
54 | if (console_fd >= 3) | |
55 | close_nointr_nofail(console_fd); | |
56 | ||
843d2643 | 57 | console_fd = -1; |
16801e90 LP |
58 | } |
59 | } | |
60 | ||
843d2643 | 61 | static int log_open_console(void) { |
16801e90 | 62 | |
843d2643 | 63 | if (console_fd >= 0) |
16801e90 | 64 | return 0; |
843d2643 LP |
65 | |
66 | if (getpid() == 1) { | |
67 | ||
68 | if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) { | |
69 | log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd)); | |
70 | return console_fd; | |
71 | } | |
72 | ||
73 | log_info("Succesfully opened /dev/console for logging."); | |
74 | } else | |
75 | console_fd = STDERR_FILENO; | |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
80 | void log_close_kmsg(void) { | |
81 | ||
82 | if (kmsg_fd < 0) | |
83 | return; | |
84 | ||
85 | close_nointr_nofail(kmsg_fd); | |
86 | kmsg_fd = -1; | |
87 | } | |
88 | ||
89 | static int log_open_kmsg(void) { | |
16801e90 LP |
90 | |
91 | if (kmsg_fd >= 0) | |
92 | return 0; | |
93 | ||
c9b80453 | 94 | if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) { |
843d2643 | 95 | log_info("Failed to open /dev/kmsg for logging: %s", strerror(errno)); |
16801e90 | 96 | return -errno; |
c9b80453 | 97 | } |
16801e90 | 98 | |
0dae83f9 LP |
99 | log_info("Succesfully opened /dev/kmsg for logging."); |
100 | ||
16801e90 LP |
101 | return 0; |
102 | } | |
103 | ||
104 | void log_close_syslog(void) { | |
105 | ||
843d2643 LP |
106 | if (syslog_fd < 0) |
107 | return; | |
108 | ||
109 | close_nointr_nofail(syslog_fd); | |
110 | syslog_fd = -1; | |
16801e90 LP |
111 | } |
112 | ||
843d2643 | 113 | static int log_open_syslog(void) { |
16801e90 LP |
114 | union { |
115 | struct sockaddr sa; | |
116 | struct sockaddr_un un; | |
117 | } sa; | |
118 | struct timeval tv; | |
119 | int r; | |
120 | ||
16801e90 LP |
121 | if (syslog_fd >= 0) |
122 | return 0; | |
123 | ||
843d2643 LP |
124 | if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) { |
125 | r = -errno; | |
126 | goto fail; | |
127 | } | |
16801e90 LP |
128 | |
129 | /* Make sure we don't block for more than 5s when talking to | |
130 | * syslog */ | |
131 | timeval_store(&tv, SYSLOG_TIMEOUT_USEC); | |
132 | if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) { | |
133 | r = -errno; | |
843d2643 | 134 | goto fail; |
16801e90 LP |
135 | } |
136 | ||
137 | zero(sa); | |
138 | sa.un.sun_family = AF_UNIX; | |
139 | strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path)); | |
140 | ||
141 | if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) { | |
142 | r = -errno; | |
843d2643 | 143 | goto fail; |
16801e90 LP |
144 | } |
145 | ||
0dae83f9 LP |
146 | log_info("Succesfully opened syslog for logging."); |
147 | ||
16801e90 | 148 | return 0; |
843d2643 LP |
149 | |
150 | fail: | |
151 | log_close_syslog(); | |
152 | log_info("Failed to open syslog for logging: %s", strerror(-r)); | |
153 | return r; | |
154 | } | |
155 | ||
156 | int log_open(void) { | |
157 | int r; | |
158 | ||
159 | /* If we don't use the console we close it here, to not get | |
160 | * killed by SAK. If we don't use syslog we close it here so | |
161 | * that we are not confused by somebody deleting the socket in | |
162 | * the fs. If we don't use /dev/kmsg we still keep it open, | |
163 | * because there is no reason to close it. */ | |
164 | ||
9fae33d2 LP |
165 | if (log_target == LOG_TARGET_NULL) { |
166 | log_close_syslog(); | |
167 | log_close_console(); | |
168 | return 0; | |
169 | } | |
170 | ||
843d2643 LP |
171 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || |
172 | log_target == LOG_TARGET_SYSLOG) | |
173 | if ((r = log_open_syslog()) >= 0) { | |
174 | log_close_console(); | |
175 | return r; | |
176 | } | |
177 | ||
178 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || | |
179 | log_target == LOG_TARGET_KMSG) | |
180 | if ((r = log_open_kmsg()) >= 0) { | |
181 | log_close_syslog(); | |
182 | log_close_console(); | |
183 | return r; | |
184 | } | |
185 | ||
186 | log_close_syslog(); | |
187 | return log_open_console(); | |
16801e90 LP |
188 | } |
189 | ||
190 | void log_set_target(LogTarget target) { | |
191 | assert(target >= 0); | |
192 | assert(target < _LOG_TARGET_MAX); | |
193 | ||
194 | log_target = target; | |
195 | } | |
196 | ||
197 | void log_set_max_level(int level) { | |
198 | assert((level & LOG_PRIMASK) == level); | |
199 | ||
200 | log_max_level = level; | |
201 | } | |
202 | ||
843d2643 | 203 | static int write_to_console( |
5899f3b7 LP |
204 | int level, |
205 | const char*file, | |
206 | int line, | |
207 | const char *func, | |
843d2643 | 208 | const char *buffer) { |
5899f3b7 | 209 | |
843d2643 LP |
210 | char location[64]; |
211 | struct iovec iovec[5]; | |
212 | unsigned n = 0; | |
213 | bool highlight; | |
5899f3b7 | 214 | |
843d2643 LP |
215 | if (console_fd < 0) |
216 | return 0; | |
217 | ||
218 | snprintf(location, sizeof(location), "(%s:%u) ", file, line); | |
219 | char_array_0(location); | |
220 | ||
221 | highlight = LOG_PRI(level) <= LOG_ERR; | |
222 | ||
223 | zero(iovec); | |
224 | IOVEC_SET_STRING(iovec[n++], location); | |
225 | if (highlight) | |
226 | IOVEC_SET_STRING(iovec[n++], "\x1B[1;31m"); | |
227 | IOVEC_SET_STRING(iovec[n++], buffer); | |
228 | if (highlight) | |
229 | IOVEC_SET_STRING(iovec[n++], "\x1B[0m"); | |
230 | IOVEC_SET_STRING(iovec[n++], "\n"); | |
231 | ||
232 | if (writev(console_fd, iovec, n) < 0) | |
233 | return -errno; | |
5899f3b7 | 234 | |
843d2643 | 235 | return 1; |
16801e90 | 236 | } |
5899f3b7 | 237 | |
16801e90 LP |
238 | static int write_to_syslog( |
239 | int level, | |
240 | const char*file, | |
241 | int line, | |
242 | const char *func, | |
843d2643 | 243 | const char *buffer) { |
16801e90 LP |
244 | |
245 | char header_priority[16], header_time[64], header_pid[16]; | |
16801e90 LP |
246 | struct iovec iovec[5]; |
247 | struct msghdr msghdr; | |
248 | time_t t; | |
249 | struct tm *tm; | |
250 | ||
251 | if (syslog_fd < 0) | |
843d2643 | 252 | return 0; |
16801e90 LP |
253 | |
254 | snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level))); | |
255 | char_array_0(header_priority); | |
256 | ||
257 | t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC); | |
258 | if (!(tm = localtime(&t))) | |
259 | return -EINVAL; | |
260 | ||
261 | if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0) | |
262 | return -EINVAL; | |
263 | ||
264 | snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid()); | |
265 | char_array_0(header_pid); | |
266 | ||
16801e90 LP |
267 | zero(iovec); |
268 | IOVEC_SET_STRING(iovec[0], header_priority); | |
269 | IOVEC_SET_STRING(iovec[1], header_time); | |
5b6319dc | 270 | IOVEC_SET_STRING(iovec[2], program_invocation_short_name); |
16801e90 LP |
271 | IOVEC_SET_STRING(iovec[3], header_pid); |
272 | IOVEC_SET_STRING(iovec[4], buffer); | |
273 | ||
274 | zero(msghdr); | |
275 | msghdr.msg_iov = iovec; | |
276 | msghdr.msg_iovlen = ELEMENTSOF(iovec); | |
277 | ||
278 | if (sendmsg(syslog_fd, &msghdr, 0) < 0) | |
279 | return -errno; | |
280 | ||
843d2643 | 281 | return 1; |
16801e90 LP |
282 | } |
283 | ||
284 | static int write_to_kmsg( | |
285 | int level, | |
286 | const char*file, | |
287 | int line, | |
288 | const char *func, | |
843d2643 | 289 | const char *buffer) { |
16801e90 LP |
290 | |
291 | char header_priority[16], header_pid[16]; | |
16801e90 LP |
292 | struct iovec iovec[5]; |
293 | ||
294 | if (kmsg_fd < 0) | |
843d2643 | 295 | return 0; |
16801e90 LP |
296 | |
297 | snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level)); | |
298 | char_array_0(header_priority); | |
299 | ||
300 | snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid()); | |
301 | char_array_0(header_pid); | |
302 | ||
16801e90 LP |
303 | zero(iovec); |
304 | IOVEC_SET_STRING(iovec[0], header_priority); | |
5b6319dc | 305 | IOVEC_SET_STRING(iovec[1], program_invocation_short_name); |
16801e90 LP |
306 | IOVEC_SET_STRING(iovec[2], header_pid); |
307 | IOVEC_SET_STRING(iovec[3], buffer); | |
843d2643 | 308 | IOVEC_SET_STRING(iovec[4], "\n"); |
16801e90 LP |
309 | |
310 | if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) | |
311 | return -errno; | |
312 | ||
843d2643 | 313 | return 1; |
16801e90 LP |
314 | } |
315 | ||
843d2643 LP |
316 | static int log_dispatch( |
317 | int level, | |
318 | const char*file, | |
319 | int line, | |
320 | const char *func, | |
9726b29e | 321 | char *buffer) { |
843d2643 | 322 | |
9726b29e | 323 | int r = 0; |
843d2643 | 324 | |
9fae33d2 LP |
325 | if (log_target == LOG_TARGET_NULL) |
326 | return 0; | |
327 | ||
9726b29e LP |
328 | do { |
329 | char *e; | |
330 | int k; | |
843d2643 | 331 | |
9726b29e | 332 | buffer += strspn(buffer, NEWLINE); |
843d2643 | 333 | |
9726b29e LP |
334 | if (buffer[0] == 0) |
335 | break; | |
843d2643 | 336 | |
9726b29e LP |
337 | if ((e = strpbrk(buffer, NEWLINE))) |
338 | *(e++) = 0; | |
843d2643 | 339 | |
9726b29e LP |
340 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || |
341 | log_target == LOG_TARGET_SYSLOG) { | |
342 | ||
343 | if ((r = write_to_syslog(level, file, line, func, buffer)) < 0) { | |
344 | log_close_syslog(); | |
345 | log_open_kmsg(); | |
346 | } else if (r > 0) | |
347 | r++; | |
348 | } | |
349 | ||
350 | if (log_target == LOG_TARGET_SYSLOG_OR_KMSG || | |
351 | log_target == LOG_TARGET_KMSG) { | |
352 | ||
353 | if ((r = write_to_kmsg(level, file, line, func, buffer)) < 0) { | |
354 | log_close_kmsg(); | |
355 | log_open_console(); | |
356 | } else if (r > 0) | |
357 | r++; | |
358 | } | |
359 | ||
360 | if ((k = write_to_console(level, file, line, func, buffer)) < 0) | |
361 | return k; | |
362 | ||
363 | buffer = e; | |
364 | } while (buffer); | |
365 | ||
366 | return r; | |
843d2643 LP |
367 | } |
368 | ||
2149e37c LP |
369 | int log_dump_internal( |
370 | int level, | |
371 | const char*file, | |
372 | int line, | |
373 | const char *func, | |
374 | char *buffer) { | |
375 | ||
376 | int saved_errno, r; | |
377 | ||
378 | /* This modifies the buffer... */ | |
379 | ||
380 | if (_likely_(LOG_PRI(level) > log_max_level)) | |
381 | return 0; | |
382 | ||
383 | saved_errno = errno; | |
384 | r = log_dispatch(level, file, line, func, buffer); | |
385 | errno = saved_errno; | |
386 | ||
387 | return r; | |
388 | } | |
389 | ||
843d2643 | 390 | int log_meta( |
16801e90 LP |
391 | int level, |
392 | const char*file, | |
393 | int line, | |
394 | const char *func, | |
395 | const char *format, ...) { | |
396 | ||
843d2643 LP |
397 | char buffer[LOG_BUFFER_MAX]; |
398 | int saved_errno, r; | |
399 | va_list ap; | |
16801e90 | 400 | |
93a46b0b | 401 | if (_likely_(LOG_PRI(level) > log_max_level)) |
843d2643 | 402 | return 0; |
16801e90 LP |
403 | |
404 | saved_errno = errno; | |
843d2643 LP |
405 | |
406 | va_start(ap, format); | |
407 | vsnprintf(buffer, sizeof(buffer), format, ap); | |
408 | va_end(ap); | |
409 | ||
410 | char_array_0(buffer); | |
411 | ||
412 | r = log_dispatch(level, file, line, func, buffer); | |
185986c6 | 413 | errno = saved_errno; |
843d2643 LP |
414 | |
415 | return r; | |
185986c6 | 416 | } |
16801e90 | 417 | |
185986c6 LP |
418 | void log_assert( |
419 | const char*file, | |
420 | int line, | |
421 | const char *func, | |
422 | const char *format, ...) { | |
423 | ||
424 | static char buffer[LOG_BUFFER_MAX]; | |
185986c6 | 425 | int saved_errno = errno; |
843d2643 | 426 | va_list ap; |
185986c6 LP |
427 | |
428 | va_start(ap, format); | |
429 | vsnprintf(buffer, sizeof(buffer), format, ap); | |
430 | va_end(ap); | |
431 | ||
432 | char_array_0(buffer); | |
433 | log_abort_msg = buffer; | |
434 | ||
843d2643 | 435 | log_dispatch(LOG_CRIT, file, line, func, buffer); |
185986c6 | 436 | abort(); |
5899f3b7 | 437 | |
185986c6 | 438 | /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */ |
c9b97d2a | 439 | errno = saved_errno; |
5899f3b7 | 440 | } |
34f0e866 LP |
441 | |
442 | int log_set_target_from_string(const char *e) { | |
443 | LogTarget t; | |
444 | ||
445 | if ((t = log_target_from_string(e)) < 0) | |
446 | return -EINVAL; | |
447 | ||
448 | log_set_target(t); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | int log_set_max_level_from_string(const char *e) { | |
453 | int t; | |
454 | ||
455 | if ((t = log_level_from_string(e)) < 0) | |
456 | return -EINVAL; | |
457 | ||
458 | log_set_max_level(t); | |
459 | return 0; | |
460 | } | |
461 | ||
462 | void log_parse_environment(void) { | |
463 | const char *e; | |
464 | ||
465 | if ((e = getenv("SYSTEMD_LOG_TARGET"))) | |
466 | if (log_set_target_from_string(e) < 0) | |
467 | log_warning("Failed to parse log target %s. Ignoring.", e); | |
468 | ||
469 | if ((e = getenv("SYSTEMD_LOG_LEVEL"))) | |
470 | if (log_set_max_level_from_string(e) < 0) | |
471 | log_warning("Failed to parse log level %s. Ignoring.", e); | |
472 | } | |
473 | ||
1adf1049 LP |
474 | LogTarget log_get_target(void) { |
475 | return log_target; | |
476 | } | |
477 | ||
478 | int log_get_max_level(void) { | |
479 | return log_max_level; | |
480 | } | |
481 | ||
34f0e866 LP |
482 | static const char *const log_target_table[] = { |
483 | [LOG_TARGET_CONSOLE] = "console", | |
484 | [LOG_TARGET_SYSLOG] = "syslog", | |
485 | [LOG_TARGET_KMSG] = "kmsg", | |
843d2643 | 486 | [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg", |
9fae33d2 | 487 | [LOG_TARGET_NULL] = "null" |
34f0e866 LP |
488 | }; |
489 | ||
490 | DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); |