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