chiark / gitweb /
units: Remove the distro specific references to killall.service.
[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 hance 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("Succesfully 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_info("Failed to open /dev/kmsg for logging: %s", strerror(errno));
100                 return -errno;
101         }
102
103         log_debug("Succesfully 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("Succesfully opened syslog for logging.");
175
176         return 0;
177
178 fail:
179         log_close_syslog();
180         log_info("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_SYSLOG_OR_KMSG ||
200             log_target == LOG_TARGET_SYSLOG)
201                 if ((r = log_open_syslog()) >= 0) {
202                         log_close_console();
203                         return r;
204                 }
205
206         if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
207             log_target == LOG_TARGET_KMSG)
208                 if ((r = log_open_kmsg()) >= 0) {
209                         log_close_syslog();
210                         log_close_console();
211                         return r;
212                 }
213
214         log_close_syslog();
215
216         /* Get the real /dev/console if we are PID=1, hence reopen */
217         log_close_console();
218         return log_open_console();
219 }
220
221 void log_set_target(LogTarget target) {
222         assert(target >= 0);
223         assert(target < _LOG_TARGET_MAX);
224
225         log_target = target;
226 }
227
228 void log_set_max_level(int level) {
229         assert((level & LOG_PRIMASK) == level);
230
231         log_max_level = level;
232 }
233
234 static int write_to_console(
235         int level,
236         const char*file,
237         int line,
238         const char *func,
239         const char *buffer) {
240
241         char location[64];
242         struct iovec iovec[5];
243         unsigned n = 0;
244         bool highlight;
245
246         if (console_fd < 0)
247                 return 0;
248
249         snprintf(location, sizeof(location), "(%s:%u) ", file, line);
250         char_array_0(location);
251
252         highlight = LOG_PRI(level) <= LOG_ERR && show_color;
253
254         zero(iovec);
255         if (show_location)
256                 IOVEC_SET_STRING(iovec[n++], location);
257         if (highlight)
258                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
259         IOVEC_SET_STRING(iovec[n++], buffer);
260         if (highlight)
261                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
262         IOVEC_SET_STRING(iovec[n++], "\n");
263
264         if (writev(console_fd, iovec, n) < 0)
265                 return -errno;
266
267         return 1;
268 }
269
270 static int write_to_syslog(
271         int level,
272         const char*file,
273         int line,
274         const char *func,
275         const char *buffer) {
276
277         char header_priority[16], header_time[64], header_pid[16];
278         struct iovec iovec[5];
279         struct msghdr msghdr;
280         time_t t;
281         struct tm *tm;
282
283         if (syslog_fd < 0)
284                 return 0;
285
286         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
287         char_array_0(header_priority);
288
289         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
290         if (!(tm = localtime(&t)))
291                 return -EINVAL;
292
293         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
294                 return -EINVAL;
295
296         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
297         char_array_0(header_pid);
298
299         zero(iovec);
300         IOVEC_SET_STRING(iovec[0], header_priority);
301         IOVEC_SET_STRING(iovec[1], header_time);
302         IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
303         IOVEC_SET_STRING(iovec[3], header_pid);
304         IOVEC_SET_STRING(iovec[4], buffer);
305
306         /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
307         if (syslog_is_stream)
308                 iovec[4].iov_len++;
309
310         zero(msghdr);
311         msghdr.msg_iov = iovec;
312         msghdr.msg_iovlen = ELEMENTSOF(iovec);
313
314         for (;;) {
315                 ssize_t n;
316
317                 if ((n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0)
318                         return -errno;
319
320                 if (!syslog_is_stream ||
321                     (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
322                         break;
323
324                 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
325         }
326
327         return 1;
328 }
329
330 static int write_to_kmsg(
331         int level,
332         const char*file,
333         int line,
334         const char *func,
335         const char *buffer) {
336
337         char header_priority[16], header_pid[16];
338         struct iovec iovec[5];
339
340         if (kmsg_fd < 0)
341                 return 0;
342
343         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
344         char_array_0(header_priority);
345
346         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
347         char_array_0(header_pid);
348
349         zero(iovec);
350         IOVEC_SET_STRING(iovec[0], header_priority);
351         IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
352         IOVEC_SET_STRING(iovec[2], header_pid);
353         IOVEC_SET_STRING(iovec[3], buffer);
354         IOVEC_SET_STRING(iovec[4], "\n");
355
356         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
357                 return -errno;
358
359         return 1;
360 }
361
362 static int log_dispatch(
363         int level,
364         const char*file,
365         int line,
366         const char *func,
367         char *buffer) {
368
369         int r = 0;
370
371         if (log_target == LOG_TARGET_NULL)
372                 return 0;
373
374         do {
375                 char *e;
376                 int k = 0;
377
378                 buffer += strspn(buffer, NEWLINE);
379
380                 if (buffer[0] == 0)
381                         break;
382
383                 if ((e = strpbrk(buffer, NEWLINE)))
384                         *(e++) = 0;
385
386                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
387                     log_target == LOG_TARGET_SYSLOG) {
388
389                         if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
390                                 log_close_syslog();
391                                 log_open_kmsg();
392                         } else if (k > 0)
393                                 r++;
394                 }
395
396                 if (k <= 0 &&
397                     (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
398                      log_target == LOG_TARGET_KMSG)) {
399
400                         if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
401                                 log_close_kmsg();
402                                 log_open_console();
403                         } else if (k > 0)
404                                 r++;
405                 }
406
407                 if (k <= 0 &&
408                     (k = write_to_console(level, file, line, func, buffer)) < 0)
409                         return k;
410
411                 buffer = e;
412         } while (buffer);
413
414         return r;
415 }
416
417 int log_dump_internal(
418         int level,
419         const char*file,
420         int line,
421         const char *func,
422         char *buffer) {
423
424         int saved_errno, r;
425
426         /* This modifies the buffer... */
427
428         if (_likely_(LOG_PRI(level) > log_max_level))
429                 return 0;
430
431         saved_errno = errno;
432         r = log_dispatch(level, file, line, func, buffer);
433         errno = saved_errno;
434
435         return r;
436 }
437
438 int log_meta(
439         int level,
440         const char*file,
441         int line,
442         const char *func,
443         const char *format, ...) {
444
445         char buffer[LINE_MAX];
446         int saved_errno, r;
447         va_list ap;
448
449         if (_likely_(LOG_PRI(level) > log_max_level))
450                 return 0;
451
452         saved_errno = errno;
453
454         va_start(ap, format);
455         vsnprintf(buffer, sizeof(buffer), format, ap);
456         va_end(ap);
457
458         char_array_0(buffer);
459
460         r = log_dispatch(level, file, line, func, buffer);
461         errno = saved_errno;
462
463         return r;
464 }
465
466 void log_assert(
467         const char*file,
468         int line,
469         const char *func,
470         const char *format, ...) {
471
472         static char buffer[LINE_MAX];
473         int saved_errno = errno;
474         va_list ap;
475
476         va_start(ap, format);
477         vsnprintf(buffer, sizeof(buffer), format, ap);
478         va_end(ap);
479
480         char_array_0(buffer);
481         log_abort_msg = buffer;
482
483         log_dispatch(LOG_CRIT, file, line, func, buffer);
484         abort();
485
486         /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
487         errno = saved_errno;
488 }
489
490 int log_set_target_from_string(const char *e) {
491         LogTarget t;
492
493         if ((t = log_target_from_string(e)) < 0)
494                 return -EINVAL;
495
496         log_set_target(t);
497         return 0;
498 }
499
500 int log_set_max_level_from_string(const char *e) {
501         int t;
502
503         if ((t = log_level_from_string(e)) < 0)
504                 return -EINVAL;
505
506         log_set_max_level(t);
507         return 0;
508 }
509
510 void log_parse_environment(void) {
511         const char *e;
512
513         if ((e = getenv("SYSTEMD_LOG_TARGET")))
514                 if (log_set_target_from_string(e) < 0)
515                         log_warning("Failed to parse log target %s. Ignoring.", e);
516
517         if ((e = getenv("SYSTEMD_LOG_LEVEL")))
518                 if (log_set_max_level_from_string(e) < 0)
519                         log_warning("Failed to parse log level %s. Ignoring.", e);
520
521         if ((e = getenv("SYSTEMD_LOG_COLOR")))
522                 if (log_show_color_from_string(e) < 0)
523                         log_warning("Failed to parse bool %s. Ignoring.", e);
524
525         if ((e = getenv("SYSTEMD_LOG_LOCATION")))
526                 if (log_show_location_from_string(e) < 0)
527                         log_warning("Failed to parse bool %s. Ignoring.", e);
528 }
529
530 LogTarget log_get_target(void) {
531         return log_target;
532 }
533
534 int log_get_max_level(void) {
535         return log_max_level;
536 }
537
538 void log_show_color(bool b) {
539         show_color = b;
540 }
541
542 void log_show_location(bool b) {
543         show_location = b;
544 }
545
546 int log_show_color_from_string(const char *e) {
547         int t;
548
549         if ((t = parse_boolean(e)) < 0)
550                 return -EINVAL;
551
552         log_show_color(t);
553         return 0;
554 }
555
556 int log_show_location_from_string(const char *e) {
557         int t;
558
559         if ((t = parse_boolean(e)) < 0)
560                 return -EINVAL;
561
562         log_show_location(t);
563         return 0;
564 }
565
566 static const char *const log_target_table[] = {
567         [LOG_TARGET_CONSOLE] = "console",
568         [LOG_TARGET_SYSLOG] = "syslog",
569         [LOG_TARGET_KMSG] = "kmsg",
570         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
571         [LOG_TARGET_NULL] = "null"
572 };
573
574 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);