chiark / gitweb /
manager: reword some error messages regarding "logs"
[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         time_t t;
252         struct tm *tm;
253
254         if (syslog_fd < 0)
255                 return 0;
256
257         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
258         char_array_0(header_priority);
259
260         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
261         if (!(tm = localtime(&t)))
262                 return -EINVAL;
263
264         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
265                 return -EINVAL;
266
267         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
268         char_array_0(header_pid);
269
270         zero(iovec);
271         IOVEC_SET_STRING(iovec[0], header_priority);
272         IOVEC_SET_STRING(iovec[1], header_time);
273         IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
274         IOVEC_SET_STRING(iovec[3], header_pid);
275         IOVEC_SET_STRING(iovec[4], buffer);
276
277         zero(msghdr);
278         msghdr.msg_iov = iovec;
279         msghdr.msg_iovlen = ELEMENTSOF(iovec);
280
281         if (sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
282                 return -errno;
283
284         return 1;
285 }
286
287 static int write_to_kmsg(
288         int level,
289         const char*file,
290         int line,
291         const char *func,
292         const char *buffer) {
293
294         char header_priority[16], header_pid[16];
295         struct iovec iovec[5];
296
297         if (kmsg_fd < 0)
298                 return 0;
299
300         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
301         char_array_0(header_priority);
302
303         snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
304         char_array_0(header_pid);
305
306         zero(iovec);
307         IOVEC_SET_STRING(iovec[0], header_priority);
308         IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
309         IOVEC_SET_STRING(iovec[2], header_pid);
310         IOVEC_SET_STRING(iovec[3], buffer);
311         IOVEC_SET_STRING(iovec[4], "\n");
312
313         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
314                 return -errno;
315
316         return 1;
317 }
318
319 static int log_dispatch(
320         int level,
321         const char*file,
322         int line,
323         const char *func,
324         char *buffer) {
325
326         int r = 0;
327
328         if (log_target == LOG_TARGET_NULL)
329                 return 0;
330
331         do {
332                 char *e;
333                 int k = 0;
334
335                 buffer += strspn(buffer, NEWLINE);
336
337                 if (buffer[0] == 0)
338                         break;
339
340                 if ((e = strpbrk(buffer, NEWLINE)))
341                         *(e++) = 0;
342
343                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
344                     log_target == LOG_TARGET_SYSLOG) {
345
346                         if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
347                                 log_close_syslog();
348                                 log_open_kmsg();
349                         } else if (k > 0)
350                                 r++;
351                 }
352
353                 if (k <= 0 &&
354                     (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
355                      log_target == LOG_TARGET_KMSG)) {
356
357                         if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
358                                 log_close_kmsg();
359                                 log_open_console();
360                         } else if (k > 0)
361                                 r++;
362                 }
363
364                 if (k <= 0 &&
365                     (k = write_to_console(level, file, line, func, buffer)) < 0)
366                         return k;
367
368                 buffer = e;
369         } while (buffer);
370
371         return r;
372 }
373
374 int log_dump_internal(
375         int level,
376         const char*file,
377         int line,
378         const char *func,
379         char *buffer) {
380
381         int saved_errno, r;
382
383         /* This modifies the buffer... */
384
385         if (_likely_(LOG_PRI(level) > log_max_level))
386                 return 0;
387
388         saved_errno = errno;
389         r = log_dispatch(level, file, line, func, buffer);
390         errno = saved_errno;
391
392         return r;
393 }
394
395 int log_meta(
396         int level,
397         const char*file,
398         int line,
399         const char *func,
400         const char *format, ...) {
401
402         char buffer[LINE_MAX];
403         int saved_errno, r;
404         va_list ap;
405
406         if (_likely_(LOG_PRI(level) > log_max_level))
407                 return 0;
408
409         saved_errno = errno;
410
411         va_start(ap, format);
412         vsnprintf(buffer, sizeof(buffer), format, ap);
413         va_end(ap);
414
415         char_array_0(buffer);
416
417         r = log_dispatch(level, file, line, func, buffer);
418         errno = saved_errno;
419
420         return r;
421 }
422
423 void log_assert(
424         const char*file,
425         int line,
426         const char *func,
427         const char *format, ...) {
428
429         static char buffer[LINE_MAX];
430         int saved_errno = errno;
431         va_list ap;
432
433         va_start(ap, format);
434         vsnprintf(buffer, sizeof(buffer), format, ap);
435         va_end(ap);
436
437         char_array_0(buffer);
438         log_abort_msg = buffer;
439
440         log_dispatch(LOG_CRIT, file, line, func, buffer);
441         abort();
442
443         /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
444         errno = saved_errno;
445 }
446
447 int log_set_target_from_string(const char *e) {
448         LogTarget t;
449
450         if ((t = log_target_from_string(e)) < 0)
451                 return -EINVAL;
452
453         log_set_target(t);
454         return 0;
455 }
456
457 int log_set_max_level_from_string(const char *e) {
458         int t;
459
460         if ((t = log_level_from_string(e)) < 0)
461                 return -EINVAL;
462
463         log_set_max_level(t);
464         return 0;
465 }
466
467 void log_parse_environment(void) {
468         const char *e;
469
470         if ((e = getenv("SYSTEMD_LOG_TARGET")))
471                 if (log_set_target_from_string(e) < 0)
472                         log_warning("Failed to parse log target %s. Ignoring.", e);
473
474         if ((e = getenv("SYSTEMD_LOG_LEVEL")))
475                 if (log_set_max_level_from_string(e) < 0)
476                         log_warning("Failed to parse log level %s. Ignoring.", e);
477
478         if ((e = getenv("SYSTEMD_LOG_COLOR")))
479                 if (log_show_color_from_string(e) < 0)
480                         log_warning("Failed to parse bool %s. Ignoring.", e);
481
482         if ((e = getenv("SYSTEMD_LOG_LOCATION"))) {
483                 if (log_show_location_from_string(e) < 0)
484                         log_warning("Failed to parse bool %s. Ignoring.", e);
485         }
486 }
487
488 LogTarget log_get_target(void) {
489         return log_target;
490 }
491
492 int log_get_max_level(void) {
493         return log_max_level;
494 }
495
496 void log_show_color(bool b) {
497         show_color = b;
498 }
499
500 void log_show_location(bool b) {
501         show_location = b;
502 }
503
504 int log_show_color_from_string(const char *e) {
505         int t;
506
507         if ((t = parse_boolean(e)) < 0)
508                 return -EINVAL;
509
510         log_show_color(t);
511         return 0;
512 }
513
514 int log_show_location_from_string(const char *e) {
515         int t;
516
517         if ((t = parse_boolean(e)) < 0)
518                 return -EINVAL;
519
520         log_show_location(t);
521         return 0;
522 }
523
524 static const char *const log_target_table[] = {
525         [LOG_TARGET_CONSOLE] = "console",
526         [LOG_TARGET_SYSLOG] = "syslog",
527         [LOG_TARGET_KMSG] = "kmsg",
528         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
529         [LOG_TARGET_NULL] = "null"
530 };
531
532 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);