chiark / gitweb /
dbus: no sync D-Bus connection flushing
[elogind.git] / src / log.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
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
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "macro.h"
33
34 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
35
36 static LogTarget log_target = LOG_TARGET_CONSOLE;
37 static int log_max_level = LOG_INFO;
38
39 static int console_fd = STDERR_FILENO;
40 static int syslog_fd = -1;
41 static int kmsg_fd = -1;
42
43 static bool syslog_is_stream = false;
44
45 static bool show_color = false;
46 static bool show_location = false;
47
48 /* Akin to glibc's __abort_msg; which is private and we hence cannot
49  * use here. */
50 static char *log_abort_msg = NULL;
51
52 void log_close_console(void) {
53
54         if (console_fd < 0)
55                 return;
56
57         if (getpid() == 1) {
58                 if (console_fd >= 3)
59                         close_nointr_nofail(console_fd);
60
61                 console_fd = -1;
62         }
63 }
64
65 static int log_open_console(void) {
66
67         if (console_fd >= 0)
68                 return 0;
69
70         if (getpid() == 1) {
71
72                 if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
73                         log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
74                         return console_fd;
75                 }
76
77                 log_debug("Successfully opened /dev/console for logging.");
78         } else
79                 console_fd = STDERR_FILENO;
80
81         return 0;
82 }
83
84 void log_close_kmsg(void) {
85
86         if (kmsg_fd < 0)
87                 return;
88
89         close_nointr_nofail(kmsg_fd);
90         kmsg_fd = -1;
91 }
92
93 static int log_open_kmsg(void) {
94
95         if (kmsg_fd >= 0)
96                 return 0;
97
98         if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
99                 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
100                 return -errno;
101         }
102
103         log_debug("Successfully opened /dev/kmsg for logging.");
104
105         return 0;
106 }
107
108 void log_close_syslog(void) {
109
110         if (syslog_fd < 0)
111                 return;
112
113         close_nointr_nofail(syslog_fd);
114         syslog_fd = -1;
115 }
116
117 static int create_log_socket(int type) {
118         struct timeval tv;
119         int fd;
120
121         if ((fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0)) < 0)
122                 return -errno;
123
124         /* Make sure we don't block for more than 5s when talking to
125          * syslog */
126         timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
127         if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
128                 close_nointr_nofail(fd);
129                 return -errno;
130         }
131
132         return fd;
133 }
134
135 static int log_open_syslog(void) {
136         union {
137                 struct sockaddr sa;
138                 struct sockaddr_un un;
139         } sa;
140         int r;
141
142         if (syslog_fd >= 0)
143                 return 0;
144
145         zero(sa);
146         sa.un.sun_family = AF_UNIX;
147         strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
148
149         if ((syslog_fd = create_log_socket(SOCK_DGRAM)) < 0) {
150                 r = -errno;
151                 goto fail;
152         }
153
154         if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
155                 close_nointr_nofail(syslog_fd);
156
157                 /* Some legacy syslog systems still use stream
158                  * sockets. They really shouldn't. But what can we
159                  * do... */
160                 if ((syslog_fd = create_log_socket(SOCK_STREAM)) < 0) {
161                         r = -errno;
162                         goto fail;
163                 }
164
165                 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
166                         r = -errno;
167                         goto fail;
168                 }
169
170                 syslog_is_stream = true;
171         } else
172                 syslog_is_stream = false;
173
174         log_debug("Successfully opened syslog for logging.");
175
176         return 0;
177
178 fail:
179         log_close_syslog();
180         log_debug("Failed to open syslog for logging: %s", strerror(-r));
181         return r;
182 }
183
184 int log_open(void) {
185         int r;
186
187         /* If we don't use the console we close it here, to not get
188          * killed by SAK. If we don't use syslog we close it here so
189          * that we are not confused by somebody deleting the socket in
190          * the fs. If we don't use /dev/kmsg we still keep it open,
191          * because there is no reason to close it. */
192
193         if (log_target == LOG_TARGET_NULL) {
194                 log_close_syslog();
195                 log_close_console();
196                 return 0;
197         }
198
199         if (log_target != LOG_TARGET_AUTO ||
200             getpid() == 1 ||
201             isatty(STDERR_FILENO) <= 0) {
202
203                 if (log_target == LOG_TARGET_AUTO ||
204                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
205                     log_target == LOG_TARGET_SYSLOG)
206                         if ((r = log_open_syslog()) >= 0) {
207                                 log_close_console();
208                                 return r;
209                         }
210                 if (log_target == LOG_TARGET_AUTO ||
211                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
212                     log_target == LOG_TARGET_KMSG)
213                         if ((r = log_open_kmsg()) >= 0) {
214                                 log_close_syslog();
215                                 log_close_console();
216                                 return r;
217                         }
218         }
219
220         log_close_syslog();
221
222         /* Get the real /dev/console if we are PID=1, hence reopen */
223         log_close_console();
224         return log_open_console();
225 }
226
227 void log_set_target(LogTarget target) {
228         assert(target >= 0);
229         assert(target < _LOG_TARGET_MAX);
230
231         log_target = target;
232 }
233
234 void log_close(void) {
235         log_close_console();
236         log_close_kmsg();
237         log_close_syslog();
238 }
239
240 void log_forget_fds(void) {
241         console_fd = kmsg_fd = syslog_fd = -1;
242 }
243
244 void log_set_max_level(int level) {
245         assert((level & LOG_PRIMASK) == level);
246
247         log_max_level = level;
248 }
249
250 static int write_to_console(
251                 int level,
252                 const char*file,
253                 int line,
254                 const char *func,
255                 const char *buffer) {
256
257         char location[64];
258         struct iovec iovec[5];
259         unsigned n = 0;
260         bool highlight;
261
262         if (console_fd < 0)
263                 return 0;
264
265         snprintf(location, sizeof(location), "(%s:%u) ", file, line);
266         char_array_0(location);
267
268         highlight = LOG_PRI(level) <= LOG_ERR && show_color;
269
270         zero(iovec);
271         if (show_location)
272                 IOVEC_SET_STRING(iovec[n++], location);
273         if (highlight)
274                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
275         IOVEC_SET_STRING(iovec[n++], buffer);
276         if (highlight)
277                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
278         IOVEC_SET_STRING(iovec[n++], "\n");
279
280         if (writev(console_fd, iovec, n) < 0)
281                 return -errno;
282
283         return 1;
284 }
285
286 static int write_to_syslog(
287         int level,
288         const char*file,
289         int line,
290         const char *func,
291         const char *buffer) {
292
293         char header_priority[16], header_time[64], header_pid[16];
294         struct iovec iovec[5];
295         struct msghdr msghdr;
296         time_t t;
297         struct tm *tm;
298
299         if (syslog_fd < 0)
300                 return 0;
301
302         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
303         char_array_0(header_priority);
304
305         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
306         if (!(tm = localtime(&t)))
307                 return -EINVAL;
308
309         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
310                 return -EINVAL;
311
312         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
313         char_array_0(header_pid);
314
315         zero(iovec);
316         IOVEC_SET_STRING(iovec[0], header_priority);
317         IOVEC_SET_STRING(iovec[1], header_time);
318         IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
319         IOVEC_SET_STRING(iovec[3], header_pid);
320         IOVEC_SET_STRING(iovec[4], buffer);
321
322         /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
323         if (syslog_is_stream)
324                 iovec[4].iov_len++;
325
326         zero(msghdr);
327         msghdr.msg_iov = iovec;
328         msghdr.msg_iovlen = ELEMENTSOF(iovec);
329
330         for (;;) {
331                 ssize_t n;
332
333                 if ((n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0)
334                         return -errno;
335
336                 if (!syslog_is_stream ||
337                     (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
338                         break;
339
340                 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
341         }
342
343         return 1;
344 }
345
346 static int write_to_kmsg(
347         int level,
348         const char*file,
349         int line,
350         const char *func,
351         const char *buffer) {
352
353         char header_priority[16], header_pid[16];
354         struct iovec iovec[5];
355
356         if (kmsg_fd < 0)
357                 return 0;
358
359         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
360         char_array_0(header_priority);
361
362         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
363         char_array_0(header_pid);
364
365         zero(iovec);
366         IOVEC_SET_STRING(iovec[0], header_priority);
367         IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
368         IOVEC_SET_STRING(iovec[2], header_pid);
369         IOVEC_SET_STRING(iovec[3], buffer);
370         IOVEC_SET_STRING(iovec[4], "\n");
371
372         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
373                 return -errno;
374
375         return 1;
376 }
377
378 static int log_dispatch(
379         int level,
380         const char*file,
381         int line,
382         const char *func,
383         char *buffer) {
384
385         int r = 0;
386
387         if (log_target == LOG_TARGET_NULL)
388                 return 0;
389
390         /* Patch in LOG_DAEMON facility if necessary */
391         if ((level & LOG_FACMASK) == 0)
392                 level = LOG_DAEMON | LOG_PRI(level);
393
394         do {
395                 char *e;
396                 int k = 0;
397
398                 buffer += strspn(buffer, NEWLINE);
399
400                 if (buffer[0] == 0)
401                         break;
402
403                 if ((e = strpbrk(buffer, NEWLINE)))
404                         *(e++) = 0;
405
406                 if (log_target == LOG_TARGET_AUTO ||
407                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
408                     log_target == LOG_TARGET_SYSLOG) {
409
410                         if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
411                                 log_close_syslog();
412                                 log_open_kmsg();
413                         } else if (k > 0)
414                                 r++;
415                 }
416
417                 if (k <= 0 &&
418                     (log_target == LOG_TARGET_AUTO ||
419                      log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
420                      log_target == LOG_TARGET_KMSG)) {
421
422                         if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
423                                 log_close_kmsg();
424                                 log_open_console();
425                         } else if (k > 0)
426                                 r++;
427                 }
428
429                 if (k <= 0 &&
430                     (k = write_to_console(level, file, line, func, buffer)) < 0)
431                         return k;
432
433                 buffer = e;
434         } while (buffer);
435
436         return r;
437 }
438
439 int log_dump_internal(
440         int level,
441         const char*file,
442         int line,
443         const char *func,
444         char *buffer) {
445
446         int saved_errno, r;
447
448         /* This modifies the buffer... */
449
450         if (_likely_(LOG_PRI(level) > log_max_level))
451                 return 0;
452
453         saved_errno = errno;
454         r = log_dispatch(level, file, line, func, buffer);
455         errno = saved_errno;
456
457         return r;
458 }
459
460 int log_meta(
461         int level,
462         const char*file,
463         int line,
464         const char *func,
465         const char *format, ...) {
466
467         char buffer[LINE_MAX];
468         int saved_errno, r;
469         va_list ap;
470
471         if (_likely_(LOG_PRI(level) > log_max_level))
472                 return 0;
473
474         saved_errno = errno;
475
476         va_start(ap, format);
477         vsnprintf(buffer, sizeof(buffer), format, ap);
478         va_end(ap);
479
480         char_array_0(buffer);
481
482         r = log_dispatch(level, file, line, func, buffer);
483         errno = saved_errno;
484
485         return r;
486 }
487
488 void log_assert(
489         const char*file,
490         int line,
491         const char *func,
492         const char *format, ...) {
493
494         static char buffer[LINE_MAX];
495         int saved_errno = errno;
496         va_list ap;
497
498         va_start(ap, format);
499         vsnprintf(buffer, sizeof(buffer), format, ap);
500         va_end(ap);
501
502         char_array_0(buffer);
503         log_abort_msg = buffer;
504
505         log_dispatch(LOG_CRIT, file, line, func, buffer);
506         abort();
507
508         /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
509         errno = saved_errno;
510 }
511
512 int log_set_target_from_string(const char *e) {
513         LogTarget t;
514
515         if ((t = log_target_from_string(e)) < 0)
516                 return -EINVAL;
517
518         log_set_target(t);
519         return 0;
520 }
521
522 int log_set_max_level_from_string(const char *e) {
523         int t;
524
525         if ((t = log_level_from_string(e)) < 0)
526                 return -EINVAL;
527
528         log_set_max_level(t);
529         return 0;
530 }
531
532 void log_parse_environment(void) {
533         const char *e;
534
535         if ((e = getenv("SYSTEMD_LOG_TARGET")))
536                 if (log_set_target_from_string(e) < 0)
537                         log_warning("Failed to parse log target %s. Ignoring.", e);
538
539         if ((e = getenv("SYSTEMD_LOG_LEVEL")))
540                 if (log_set_max_level_from_string(e) < 0)
541                         log_warning("Failed to parse log level %s. Ignoring.", e);
542
543         if ((e = getenv("SYSTEMD_LOG_COLOR")))
544                 if (log_show_color_from_string(e) < 0)
545                         log_warning("Failed to parse bool %s. Ignoring.", e);
546
547         if ((e = getenv("SYSTEMD_LOG_LOCATION")))
548                 if (log_show_location_from_string(e) < 0)
549                         log_warning("Failed to parse bool %s. Ignoring.", e);
550 }
551
552 LogTarget log_get_target(void) {
553         return log_target;
554 }
555
556 int log_get_max_level(void) {
557         return log_max_level;
558 }
559
560 void log_show_color(bool b) {
561         show_color = b;
562 }
563
564 void log_show_location(bool b) {
565         show_location = b;
566 }
567
568 int log_show_color_from_string(const char *e) {
569         int t;
570
571         if ((t = parse_boolean(e)) < 0)
572                 return -EINVAL;
573
574         log_show_color(t);
575         return 0;
576 }
577
578 int log_show_location_from_string(const char *e) {
579         int t;
580
581         if ((t = parse_boolean(e)) < 0)
582                 return -EINVAL;
583
584         log_show_location(t);
585         return 0;
586 }
587
588 static const char *const log_target_table[] = {
589         [LOG_TARGET_CONSOLE] = "console",
590         [LOG_TARGET_SYSLOG] = "syslog",
591         [LOG_TARGET_KMSG] = "kmsg",
592         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
593         [LOG_TARGET_NULL] = "null",
594         [LOG_TARGET_AUTO] = "auto"
595 };
596
597 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);