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