chiark / gitweb /
unit: automatically connect to syslog when it becomes available
[elogind.git] / 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 extern char * __progname;
35
36 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
37 #define LOG_BUFFER_MAX 1024
38
39 static LogTarget log_target = LOG_TARGET_CONSOLE;
40 static int log_max_level = LOG_DEBUG;
41
42 static int syslog_fd = -1;
43 static int kmsg_fd = -1;
44
45 void log_close_kmsg(void) {
46
47         if (kmsg_fd >= 0) {
48                 close_nointr(kmsg_fd);
49                 kmsg_fd = -1;
50         }
51 }
52
53 int log_open_kmsg(void) {
54
55         if (log_target != LOG_TARGET_KMSG) {
56                 log_close_kmsg();
57                 return 0;
58         }
59
60         if (kmsg_fd >= 0)
61                 return 0;
62
63         if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0)
64                 return -errno;
65
66         return 0;
67 }
68
69 void log_close_syslog(void) {
70
71         if (syslog_fd >= 0) {
72                 close_nointr(syslog_fd);
73                 syslog_fd = -1;
74         }
75 }
76
77 int log_open_syslog(void) {
78         union {
79                 struct sockaddr sa;
80                 struct sockaddr_un un;
81         } sa;
82         struct timeval tv;
83         int r;
84
85         if (log_target != LOG_TARGET_SYSLOG) {
86                 log_close_syslog();
87                 return 0;
88         }
89
90         if (syslog_fd >= 0)
91                 return 0;
92
93         if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
94                 return -errno;
95
96         /* Make sure we don't block for more than 5s when talking to
97          * syslog */
98         timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
99         if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
100                 r = -errno;
101                 log_close_syslog();
102                 return r;
103         }
104
105         zero(sa);
106         sa.un.sun_family = AF_UNIX;
107         strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
108
109         if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
110                 r = -errno;
111                 log_close_syslog();
112                 return -errno;
113         }
114
115         return 0;
116 }
117
118 void log_set_target(LogTarget target) {
119         assert(target >= 0);
120         assert(target < _LOG_TARGET_MAX);
121
122         log_target = target;
123 }
124
125 void log_set_max_level(int level) {
126         assert((level & LOG_PRIMASK) == level);
127
128         log_max_level = level;
129 }
130
131 static void write_to_console(
132         int level,
133         const char*file,
134         int line,
135         const char *func,
136         const char *format,
137         va_list ap) {
138
139         const char *prefix, *suffix;
140
141         if (LOG_PRI(level) <= LOG_ERR) {
142                 prefix = "\x1B[1;31m";
143                 suffix = "\x1B[0m";
144         } else {
145                 prefix = "";
146                 suffix = "";
147         }
148
149         fprintf(stderr, "(%s:%u) %s", file, line, prefix);
150         vfprintf(stderr, format, ap);
151         fprintf(stderr, "%s\n", suffix);
152 }
153
154 static int write_to_syslog(
155         int level,
156         const char*file,
157         int line,
158         const char *func,
159         const char *format,
160         va_list ap) {
161
162         char header_priority[16], header_time[64], header_pid[16];
163         char buffer[LOG_BUFFER_MAX];
164         struct iovec iovec[5];
165         struct msghdr msghdr;
166         time_t t;
167         struct tm *tm;
168
169         if (syslog_fd < 0)
170                 return -EIO;
171
172         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
173         char_array_0(header_priority);
174
175         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
176         if (!(tm = localtime(&t)))
177                 return -EINVAL;
178
179         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
180                 return -EINVAL;
181
182         snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
183         char_array_0(header_pid);
184
185         vsnprintf(buffer, sizeof(buffer), format, ap);
186         char_array_0(buffer);
187
188         zero(iovec);
189         IOVEC_SET_STRING(iovec[0], header_priority);
190         IOVEC_SET_STRING(iovec[1], header_time);
191         IOVEC_SET_STRING(iovec[2], file_name_from_path(__progname));
192         IOVEC_SET_STRING(iovec[3], header_pid);
193         IOVEC_SET_STRING(iovec[4], buffer);
194
195         zero(msghdr);
196         msghdr.msg_iov = iovec;
197         msghdr.msg_iovlen = ELEMENTSOF(iovec);
198
199         if (sendmsg(syslog_fd, &msghdr, 0) < 0)
200                 return -errno;
201
202         return 0;
203 }
204
205 static int write_to_kmsg(
206         int level,
207         const char*file,
208         int line,
209         const char *func,
210         const char *format,
211         va_list ap) {
212
213         char header_priority[16], header_pid[16];
214         char buffer[LOG_BUFFER_MAX];
215         struct iovec iovec[5];
216
217         if (kmsg_fd < 0)
218                 return -EIO;
219
220         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
221         char_array_0(header_priority);
222
223         snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
224         char_array_0(header_pid);
225
226         vsnprintf(buffer, sizeof(buffer), format, ap);
227         char_array_0(buffer);
228
229         zero(iovec);
230         IOVEC_SET_STRING(iovec[0], header_priority);
231         IOVEC_SET_STRING(iovec[1], file_name_from_path(__progname));
232         IOVEC_SET_STRING(iovec[2], header_pid);
233         IOVEC_SET_STRING(iovec[3], buffer);
234         IOVEC_SET_STRING(iovec[4], (char*) "\n");
235
236         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
237                 return -errno;
238
239         return 0;
240 }
241
242 void log_meta(
243         int level,
244         const char*file,
245         int line,
246         const char *func,
247         const char *format, ...) {
248
249         va_list ap;
250         bool written;
251         int saved_errno;
252
253         if (LOG_PRI(level) > log_max_level)
254                 return;
255
256         saved_errno = errno;
257         written = false;
258
259         if (log_target == LOG_TARGET_KMSG) {
260                 va_start(ap, format);
261                 written = write_to_kmsg(level, file, line, func, format, ap) >= 0;
262                 va_end(ap);
263         } else if (log_target == LOG_TARGET_SYSLOG) {
264                 va_start(ap, format);
265                 written = write_to_syslog(level, file, line, func, format, ap) >= 0;
266                 va_end(ap);
267         }
268
269         if (!written) {
270                 va_start(ap, format);
271                 write_to_console(level, file, line, func, format, ap);
272                 va_end(ap);
273         }
274
275         errno = saved_errno;
276 }