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