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