chiark / gitweb /
log: never delay logging if the log server is stuck, always drop messages quickly
[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 #include <stddef.h>
30
31 #include "log.h"
32 #include "util.h"
33 #include "macro.h"
34 #include "socket-util.h"
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 static int journal_fd = -1;
43
44 static bool syslog_is_stream = false;
45
46 static bool show_color = false;
47 static bool show_location = false;
48
49 /* Akin to glibc's __abort_msg; which is private and we hence cannot
50  * use here. */
51 static char *log_abort_msg = NULL;
52
53 void log_close_console(void) {
54
55         if (console_fd < 0)
56                 return;
57
58         if (getpid() == 1) {
59                 if (console_fd >= 3)
60                         close_nointr_nofail(console_fd);
61
62                 console_fd = -1;
63         }
64 }
65
66 static int log_open_console(void) {
67
68         if (console_fd >= 0)
69                 return 0;
70
71         if (getpid() == 1) {
72
73                 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
74                 if (console_fd < 0) {
75                         log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
76                         return console_fd;
77                 }
78
79                 log_debug("Successfully opened /dev/console for logging.");
80         } else
81                 console_fd = STDERR_FILENO;
82
83         return 0;
84 }
85
86 void log_close_kmsg(void) {
87
88         if (kmsg_fd < 0)
89                 return;
90
91         close_nointr_nofail(kmsg_fd);
92         kmsg_fd = -1;
93 }
94
95 static int log_open_kmsg(void) {
96
97         if (kmsg_fd >= 0)
98                 return 0;
99
100         kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
101         if (kmsg_fd < 0) {
102                 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
103                 return -errno;
104         }
105
106         log_debug("Successfully opened /dev/kmsg for logging.");
107
108         return 0;
109 }
110
111 void log_close_syslog(void) {
112
113         if (syslog_fd < 0)
114                 return;
115
116         close_nointr_nofail(syslog_fd);
117         syslog_fd = -1;
118 }
119
120 static int create_log_socket(int type) {
121         int fd;
122
123         /* All output to the syslog/journal fds we do asynchronously,
124          * and if the buffers are full we just drop the messages */
125
126         fd = socket(AF_UNIX, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
127         if (fd < 0)
128                 return -errno;
129
130         return fd;
131 }
132
133 static int log_open_syslog(void) {
134         union sockaddr_union sa;
135         int r;
136
137         if (syslog_fd >= 0)
138                 return 0;
139
140         zero(sa);
141         sa.un.sun_family = AF_UNIX;
142         strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
143
144         syslog_fd = create_log_socket(SOCK_DGRAM);
145         if (syslog_fd < 0) {
146                 r = syslog_fd;
147                 goto fail;
148         }
149
150         if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
151                 close_nointr_nofail(syslog_fd);
152
153                 /* Some legacy syslog systems still use stream
154                  * sockets. They really shouldn't. But what can we
155                  * do... */
156                 syslog_fd = create_log_socket(SOCK_STREAM);
157                 if (syslog_fd < 0) {
158                         r = syslog_fd;
159                         goto fail;
160                 }
161
162                 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
163                         r = -errno;
164                         goto fail;
165                 }
166
167                 syslog_is_stream = true;
168         } else
169                 syslog_is_stream = false;
170
171         log_debug("Successfully opened syslog for logging.");
172
173         return 0;
174
175 fail:
176         log_close_syslog();
177         log_debug("Failed to open syslog for logging: %s", strerror(-r));
178         return r;
179 }
180
181 void log_close_journal(void) {
182
183         if (journal_fd < 0)
184                 return;
185
186         close_nointr_nofail(journal_fd);
187         journal_fd = -1;
188 }
189
190 static int log_open_journal(void) {
191         union sockaddr_union sa;
192         int r;
193
194         if (journal_fd >= 0)
195                 return 0;
196
197         journal_fd = create_log_socket(SOCK_DGRAM);
198         if (journal_fd < 0) {
199                 r = journal_fd;
200                 goto fail;
201         }
202
203         zero(sa);
204         sa.un.sun_family = AF_UNIX;
205         strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
206
207         if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
208                 r = -errno;
209                 goto fail;
210         }
211
212         log_debug("Successfully opened journal for logging.");
213
214         return 0;
215
216 fail:
217         log_close_journal();
218         log_debug("Failed to open journal for logging: %s", strerror(-r));
219         return r;
220 }
221
222 int log_open(void) {
223         int r;
224
225         /* If we don't use the console we close it here, to not get
226          * killed by SAK. If we don't use syslog we close it here so
227          * that we are not confused by somebody deleting the socket in
228          * the fs. If we don't use /dev/kmsg we still keep it open,
229          * because there is no reason to close it. */
230
231         if (log_target == LOG_TARGET_NULL) {
232                 log_close_journal();
233                 log_close_syslog();
234                 log_close_console();
235                 return 0;
236         }
237
238         if (log_target != LOG_TARGET_AUTO ||
239             getpid() == 1 ||
240             isatty(STDERR_FILENO) <= 0) {
241
242                 if (log_target == LOG_TARGET_AUTO ||
243                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
244                     log_target == LOG_TARGET_JOURNAL) {
245                         r = log_open_journal();
246                         if (r >= 0) {
247                                 log_close_syslog();
248                                 log_close_console();
249                                 return r;
250                         }
251                 }
252
253                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
254                     log_target == LOG_TARGET_SYSLOG) {
255                         r = log_open_syslog();
256                         if (r >= 0) {
257                                 log_close_journal();
258                                 log_close_console();
259                                 return r;
260                         }
261                 }
262
263                 if (log_target == LOG_TARGET_AUTO ||
264                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
265                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
266                     log_target == LOG_TARGET_KMSG) {
267                         r = log_open_kmsg();
268                         if (r >= 0) {
269                                 log_close_journal();
270                                 log_close_syslog();
271                                 log_close_console();
272                                 return r;
273                         }
274                 }
275         }
276
277         log_close_journal();
278         log_close_syslog();
279
280         /* Get the real /dev/console if we are PID=1, hence reopen */
281         log_close_console();
282         return log_open_console();
283 }
284
285 void log_set_target(LogTarget target) {
286         assert(target >= 0);
287         assert(target < _LOG_TARGET_MAX);
288
289         log_target = target;
290 }
291
292 void log_close(void) {
293         log_close_journal();
294         log_close_syslog();
295         log_close_kmsg();
296         log_close_console();
297 }
298
299 void log_forget_fds(void) {
300         console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
301 }
302
303 void log_set_max_level(int level) {
304         assert((level & LOG_PRIMASK) == level);
305
306         log_max_level = level;
307 }
308
309 static int write_to_console(
310                 int level,
311                 const char*file,
312                 int line,
313                 const char *func,
314                 const char *buffer) {
315
316         char location[64];
317         struct iovec iovec[5];
318         unsigned n = 0;
319         bool highlight;
320
321         if (console_fd < 0)
322                 return 0;
323
324         highlight = LOG_PRI(level) <= LOG_ERR && show_color;
325
326         zero(iovec);
327
328         if (show_location) {
329                 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
330                 char_array_0(location);
331                 IOVEC_SET_STRING(iovec[n++], location);
332         }
333
334         if (highlight)
335                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
336         IOVEC_SET_STRING(iovec[n++], buffer);
337         if (highlight)
338                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
339         IOVEC_SET_STRING(iovec[n++], "\n");
340
341         if (writev(console_fd, iovec, n) < 0)
342                 return -errno;
343
344         return 1;
345 }
346
347 static int write_to_syslog(
348         int level,
349         const char*file,
350         int line,
351         const char *func,
352         const char *buffer) {
353
354         char header_priority[16], header_time[64], header_pid[16];
355         struct iovec iovec[5];
356         struct msghdr msghdr;
357         time_t t;
358         struct tm *tm;
359
360         if (syslog_fd < 0)
361                 return 0;
362
363         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
364         char_array_0(header_priority);
365
366         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
367         if (!(tm = localtime(&t)))
368                 return -EINVAL;
369
370         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
371                 return -EINVAL;
372
373         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
374         char_array_0(header_pid);
375
376         zero(iovec);
377         IOVEC_SET_STRING(iovec[0], header_priority);
378         IOVEC_SET_STRING(iovec[1], header_time);
379         IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
380         IOVEC_SET_STRING(iovec[3], header_pid);
381         IOVEC_SET_STRING(iovec[4], buffer);
382
383         /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
384         if (syslog_is_stream)
385                 iovec[4].iov_len++;
386
387         zero(msghdr);
388         msghdr.msg_iov = iovec;
389         msghdr.msg_iovlen = ELEMENTSOF(iovec);
390
391         for (;;) {
392                 ssize_t n;
393
394                 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
395                 if (n < 0)
396                         return -errno;
397
398                 if (!syslog_is_stream ||
399                     (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
400                         break;
401
402                 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
403         }
404
405         return 1;
406 }
407
408 static int write_to_kmsg(
409         int level,
410         const char*file,
411         int line,
412         const char *func,
413         const char *buffer) {
414
415         char header_priority[16], header_pid[16];
416         struct iovec iovec[5];
417
418         if (kmsg_fd < 0)
419                 return 0;
420
421         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
422         char_array_0(header_priority);
423
424         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
425         char_array_0(header_pid);
426
427         zero(iovec);
428         IOVEC_SET_STRING(iovec[0], header_priority);
429         IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
430         IOVEC_SET_STRING(iovec[2], header_pid);
431         IOVEC_SET_STRING(iovec[3], buffer);
432         IOVEC_SET_STRING(iovec[4], "\n");
433
434         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
435                 return -errno;
436
437         return 1;
438 }
439
440 static int write_to_journal(
441         int level,
442         const char*file,
443         int line,
444         const char *func,
445         const char *buffer) {
446
447         char header[LINE_MAX];
448         struct iovec iovec[3];
449         struct msghdr mh;
450
451         if (journal_fd < 0)
452                 return 0;
453
454         snprintf(header, sizeof(header),
455                  "PRIORITY=%i\n"
456                  "CODE_FILE=%s\n"
457                  "CODE_LINE=%i\n"
458                  "CODE_FUNCTION=%s\n"
459                  "MESSAGE=",
460                  LOG_PRI(level),
461                  file,
462                  line,
463                  func);
464
465         char_array_0(header);
466
467         zero(iovec);
468         IOVEC_SET_STRING(iovec[0], header);
469         IOVEC_SET_STRING(iovec[1], buffer);
470         IOVEC_SET_STRING(iovec[2], "\n");
471
472         zero(mh);
473         mh.msg_iov = iovec;
474         mh.msg_iovlen = ELEMENTSOF(iovec);
475
476         if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
477                 return -errno;
478
479         return 1;
480 }
481
482 static int log_dispatch(
483         int level,
484         const char*file,
485         int line,
486         const char *func,
487         char *buffer) {
488
489         int r = 0;
490
491         if (log_target == LOG_TARGET_NULL)
492                 return 0;
493
494         /* Patch in LOG_DAEMON facility if necessary */
495         if ((level & LOG_FACMASK) == 0)
496                 level = LOG_DAEMON | LOG_PRI(level);
497
498         do {
499                 char *e;
500                 int k = 0;
501
502                 buffer += strspn(buffer, NEWLINE);
503
504                 if (buffer[0] == 0)
505                         break;
506
507                 if ((e = strpbrk(buffer, NEWLINE)))
508                         *(e++) = 0;
509
510                 if (log_target == LOG_TARGET_AUTO ||
511                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
512                     log_target == LOG_TARGET_JOURNAL) {
513
514                         k = write_to_journal(level, file, line, func, buffer);
515                         if (k < 0) {
516                                 if (k != -EAGAIN)
517                                         log_close_journal();
518                                 log_open_kmsg();
519                         } else if (k > 0)
520                                 r++;
521                 }
522
523                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
524                     log_target == LOG_TARGET_SYSLOG) {
525
526                         k = write_to_syslog(level, file, line, func, buffer);
527                         if (k < 0) {
528                                 if (k != -EAGAIN)
529                                         log_close_syslog();
530                                 log_open_kmsg();
531                         } else if (k > 0)
532                                 r++;
533                 }
534
535                 if (k <= 0 &&
536                     (log_target == LOG_TARGET_AUTO ||
537                      log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
538                      log_target == LOG_TARGET_KMSG)) {
539
540                         k = write_to_kmsg(level, file, line, func, buffer);
541                         if (k < 0) {
542                                 log_close_kmsg();
543                                 log_open_console();
544                         } else if (k > 0)
545                                 r++;
546                 }
547
548                 if (k <= 0) {
549                         k = write_to_console(level, file, line, func, buffer);
550                         if (k < 0)
551                                 return k;
552                 }
553
554                 buffer = e;
555         } while (buffer);
556
557         return r;
558 }
559
560 int log_dump_internal(
561         int level,
562         const char*file,
563         int line,
564         const char *func,
565         char *buffer) {
566
567         int saved_errno, r;
568
569         /* This modifies the buffer... */
570
571         if (_likely_(LOG_PRI(level) > log_max_level))
572                 return 0;
573
574         saved_errno = errno;
575         r = log_dispatch(level, file, line, func, buffer);
576         errno = saved_errno;
577
578         return r;
579 }
580
581 int log_meta(
582         int level,
583         const char*file,
584         int line,
585         const char *func,
586         const char *format, ...) {
587
588         char buffer[LINE_MAX];
589         int saved_errno, r;
590         va_list ap;
591
592         if (_likely_(LOG_PRI(level) > log_max_level))
593                 return 0;
594
595         saved_errno = errno;
596
597         va_start(ap, format);
598         vsnprintf(buffer, sizeof(buffer), format, ap);
599         va_end(ap);
600
601         char_array_0(buffer);
602
603         r = log_dispatch(level, file, line, func, buffer);
604         errno = saved_errno;
605
606         return r;
607 }
608
609 #pragma GCC diagnostic push
610 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
611 _noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) {
612         static char buffer[LINE_MAX];
613
614         snprintf(buffer, sizeof(buffer), format, text, file, line, func);
615
616         char_array_0(buffer);
617         log_abort_msg = buffer;
618
619         log_dispatch(LOG_CRIT, file, line, func, buffer);
620         abort();
621 }
622 #pragma GCC diagnostic pop
623
624 void log_assert_failed(const char *text, const char *file, int line, const char *func) {
625         log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
626 }
627
628 void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
629         log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
630 }
631
632 int log_set_target_from_string(const char *e) {
633         LogTarget t;
634
635         t = log_target_from_string(e);
636         if (t < 0)
637                 return -EINVAL;
638
639         log_set_target(t);
640         return 0;
641 }
642
643 int log_set_max_level_from_string(const char *e) {
644         int t;
645
646         t = log_level_from_string(e);
647         if (t < 0)
648                 return t;
649
650         log_set_max_level(t);
651         return 0;
652 }
653
654 void log_parse_environment(void) {
655         const char *e;
656
657         if ((e = getenv("SYSTEMD_LOG_TARGET")))
658                 if (log_set_target_from_string(e) < 0)
659                         log_warning("Failed to parse log target %s. Ignoring.", e);
660
661         if ((e = getenv("SYSTEMD_LOG_LEVEL")))
662                 if (log_set_max_level_from_string(e) < 0)
663                         log_warning("Failed to parse log level %s. Ignoring.", e);
664
665         if ((e = getenv("SYSTEMD_LOG_COLOR")))
666                 if (log_show_color_from_string(e) < 0)
667                         log_warning("Failed to parse bool %s. Ignoring.", e);
668
669         if ((e = getenv("SYSTEMD_LOG_LOCATION")))
670                 if (log_show_location_from_string(e) < 0)
671                         log_warning("Failed to parse bool %s. Ignoring.", e);
672 }
673
674 LogTarget log_get_target(void) {
675         return log_target;
676 }
677
678 int log_get_max_level(void) {
679         return log_max_level;
680 }
681
682 void log_show_color(bool b) {
683         show_color = b;
684 }
685
686 void log_show_location(bool b) {
687         show_location = b;
688 }
689
690 int log_show_color_from_string(const char *e) {
691         int t;
692
693         t = parse_boolean(e);
694         if (t < 0)
695                 return t;
696
697         log_show_color(t);
698         return 0;
699 }
700
701 int log_show_location_from_string(const char *e) {
702         int t;
703
704         t = parse_boolean(e);
705         if (t < 0)
706                 return t;
707
708         log_show_location(t);
709         return 0;
710 }
711
712 static const char *const log_target_table[] = {
713         [LOG_TARGET_CONSOLE] = "console",
714         [LOG_TARGET_KMSG] = "kmsg",
715         [LOG_TARGET_JOURNAL] = "journal",
716         [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
717         [LOG_TARGET_SYSLOG] = "syslog",
718         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
719         [LOG_TARGET_AUTO] = "auto",
720         [LOG_TARGET_NULL] = "null"
721 };
722
723 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);