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