]>
Commit | Line | Data |
---|---|---|
5899f3b7 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
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 | ||
5899f3b7 LP |
22 | #include <stdarg.h> |
23 | #include <stdio.h> | |
c9b97d2a | 24 | #include <errno.h> |
16801e90 LP |
25 | #include <unistd.h> |
26 | #include <fcntl.h> | |
27 | #include <sys/socket.h> | |
28 | #include <sys/un.h> | |
5899f3b7 LP |
29 | |
30 | #include "log.h" | |
16801e90 LP |
31 | #include "util.h" |
32 | #include "macro.h" | |
5899f3b7 | 33 | |
16801e90 LP |
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 syslog_fd = -1; | |
41 | static int kmsg_fd = -1; | |
42 | ||
185986c6 LP |
43 | /* Akin to glibc's __abort_msg; which is private and we hance cannot |
44 | * use here. */ | |
45 | static char *log_abort_msg = NULL; | |
46 | ||
16801e90 LP |
47 | void log_close_kmsg(void) { |
48 | ||
49 | if (kmsg_fd >= 0) { | |
a16e1123 | 50 | close_nointr_nofail(kmsg_fd); |
16801e90 LP |
51 | kmsg_fd = -1; |
52 | } | |
53 | } | |
54 | ||
55 | int log_open_kmsg(void) { | |
56 | ||
57 | if (log_target != LOG_TARGET_KMSG) { | |
58 | log_close_kmsg(); | |
59 | return 0; | |
60 | } | |
61 | ||
62 | if (kmsg_fd >= 0) | |
63 | return 0; | |
64 | ||
c9b80453 LP |
65 | if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) { |
66 | log_info("Failed to open syslog for logging: %s", strerror(errno)); | |
16801e90 | 67 | return -errno; |
c9b80453 | 68 | } |
16801e90 | 69 | |
0dae83f9 LP |
70 | log_info("Succesfully opened /dev/kmsg for logging."); |
71 | ||
16801e90 LP |
72 | return 0; |
73 | } | |
74 | ||
75 | void log_close_syslog(void) { | |
76 | ||
77 | if (syslog_fd >= 0) { | |
a16e1123 | 78 | close_nointr_nofail(syslog_fd); |
16801e90 LP |
79 | syslog_fd = -1; |
80 | } | |
81 | } | |
82 | ||
83 | int log_open_syslog(void) { | |
84 | union { | |
85 | struct sockaddr sa; | |
86 | struct sockaddr_un un; | |
87 | } sa; | |
88 | struct timeval tv; | |
89 | int r; | |
90 | ||
91 | if (log_target != LOG_TARGET_SYSLOG) { | |
92 | log_close_syslog(); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | if (syslog_fd >= 0) | |
97 | return 0; | |
98 | ||
99 | if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) | |
100 | return -errno; | |
101 | ||
102 | /* Make sure we don't block for more than 5s when talking to | |
103 | * syslog */ | |
104 | timeval_store(&tv, SYSLOG_TIMEOUT_USEC); | |
105 | if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) { | |
106 | r = -errno; | |
107 | log_close_syslog(); | |
108 | return r; | |
109 | } | |
110 | ||
111 | zero(sa); | |
112 | sa.un.sun_family = AF_UNIX; | |
113 | strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path)); | |
114 | ||
115 | if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) { | |
116 | r = -errno; | |
117 | log_close_syslog(); | |
c9b80453 LP |
118 | |
119 | log_info("Failed to open syslog for logging: %s", strerror(-r)); | |
120 | return r; | |
16801e90 LP |
121 | } |
122 | ||
0dae83f9 LP |
123 | log_info("Succesfully opened syslog for logging."); |
124 | ||
16801e90 LP |
125 | return 0; |
126 | } | |
127 | ||
128 | void log_set_target(LogTarget target) { | |
129 | assert(target >= 0); | |
130 | assert(target < _LOG_TARGET_MAX); | |
131 | ||
132 | log_target = target; | |
133 | } | |
134 | ||
135 | void log_set_max_level(int level) { | |
136 | assert((level & LOG_PRIMASK) == level); | |
137 | ||
138 | log_max_level = level; | |
139 | } | |
140 | ||
141 | static void write_to_console( | |
5899f3b7 LP |
142 | int level, |
143 | const char*file, | |
144 | int line, | |
145 | const char *func, | |
16801e90 LP |
146 | const char *format, |
147 | va_list ap) { | |
5899f3b7 LP |
148 | |
149 | const char *prefix, *suffix; | |
5899f3b7 LP |
150 | |
151 | if (LOG_PRI(level) <= LOG_ERR) { | |
152 | prefix = "\x1B[1;31m"; | |
153 | suffix = "\x1B[0m"; | |
154 | } else { | |
155 | prefix = ""; | |
156 | suffix = ""; | |
157 | } | |
158 | ||
5899f3b7 LP |
159 | fprintf(stderr, "(%s:%u) %s", file, line, prefix); |
160 | vfprintf(stderr, format, ap); | |
161 | fprintf(stderr, "%s\n", suffix); | |
16801e90 | 162 | } |
5899f3b7 | 163 | |
16801e90 LP |
164 | static int write_to_syslog( |
165 | int level, | |
166 | const char*file, | |
167 | int line, | |
168 | const char *func, | |
169 | const char *format, | |
170 | va_list ap) { | |
171 | ||
172 | char header_priority[16], header_time[64], header_pid[16]; | |
173 | char buffer[LOG_BUFFER_MAX]; | |
174 | struct iovec iovec[5]; | |
175 | struct msghdr msghdr; | |
176 | time_t t; | |
177 | struct tm *tm; | |
178 | ||
179 | if (syslog_fd < 0) | |
180 | return -EIO; | |
181 | ||
182 | snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level))); | |
183 | char_array_0(header_priority); | |
184 | ||
185 | t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC); | |
186 | if (!(tm = localtime(&t))) | |
187 | return -EINVAL; | |
188 | ||
189 | if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0) | |
190 | return -EINVAL; | |
191 | ||
192 | snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid()); | |
193 | char_array_0(header_pid); | |
194 | ||
195 | vsnprintf(buffer, sizeof(buffer), format, ap); | |
196 | char_array_0(buffer); | |
197 | ||
198 | zero(iovec); | |
199 | IOVEC_SET_STRING(iovec[0], header_priority); | |
200 | IOVEC_SET_STRING(iovec[1], header_time); | |
2d368c14 | 201 | IOVEC_SET_STRING(iovec[2], __progname); |
16801e90 LP |
202 | IOVEC_SET_STRING(iovec[3], header_pid); |
203 | IOVEC_SET_STRING(iovec[4], buffer); | |
204 | ||
205 | zero(msghdr); | |
206 | msghdr.msg_iov = iovec; | |
207 | msghdr.msg_iovlen = ELEMENTSOF(iovec); | |
208 | ||
209 | if (sendmsg(syslog_fd, &msghdr, 0) < 0) | |
210 | return -errno; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | static int write_to_kmsg( | |
216 | int level, | |
217 | const char*file, | |
218 | int line, | |
219 | const char *func, | |
220 | const char *format, | |
221 | va_list ap) { | |
222 | ||
223 | char header_priority[16], header_pid[16]; | |
224 | char buffer[LOG_BUFFER_MAX]; | |
225 | struct iovec iovec[5]; | |
226 | ||
227 | if (kmsg_fd < 0) | |
228 | return -EIO; | |
229 | ||
230 | snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level)); | |
231 | char_array_0(header_priority); | |
232 | ||
233 | snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid()); | |
234 | char_array_0(header_pid); | |
235 | ||
236 | vsnprintf(buffer, sizeof(buffer), format, ap); | |
237 | char_array_0(buffer); | |
238 | ||
239 | zero(iovec); | |
240 | IOVEC_SET_STRING(iovec[0], header_priority); | |
2d368c14 | 241 | IOVEC_SET_STRING(iovec[1], __progname); |
16801e90 LP |
242 | IOVEC_SET_STRING(iovec[2], header_pid); |
243 | IOVEC_SET_STRING(iovec[3], buffer); | |
244 | IOVEC_SET_STRING(iovec[4], (char*) "\n"); | |
245 | ||
246 | if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0) | |
247 | return -errno; | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
185986c6 LP |
252 | #define LOG_DISPATCH(level,file,line,func,format) \ |
253 | do { \ | |
254 | va_list _ap; \ | |
255 | bool written = false; \ | |
256 | if (log_target == LOG_TARGET_KMSG) { \ | |
257 | va_start(_ap, format); \ | |
258 | written = write_to_kmsg(level, file, line, func, format, _ap) >= 0; \ | |
259 | va_end(_ap); \ | |
260 | } else if (log_target == LOG_TARGET_SYSLOG) { \ | |
261 | va_start(_ap, format); \ | |
262 | written = write_to_syslog(level, file, line, func, format, _ap) >= 0; \ | |
263 | va_end(_ap); \ | |
264 | } \ | |
265 | if (!written) { \ | |
266 | va_start(_ap, format); \ | |
267 | write_to_console(level, file, line, func, format, _ap); \ | |
268 | va_end(_ap); \ | |
269 | } \ | |
270 | } while (false) | |
271 | ||
16801e90 LP |
272 | void log_meta( |
273 | int level, | |
274 | const char*file, | |
275 | int line, | |
276 | const char *func, | |
277 | const char *format, ...) { | |
278 | ||
16801e90 LP |
279 | int saved_errno; |
280 | ||
281 | if (LOG_PRI(level) > log_max_level) | |
282 | return; | |
283 | ||
284 | saved_errno = errno; | |
185986c6 LP |
285 | LOG_DISPATCH(level, file, line, func, format); |
286 | errno = saved_errno; | |
287 | } | |
16801e90 | 288 | |
185986c6 LP |
289 | void log_assert( |
290 | const char*file, | |
291 | int line, | |
292 | const char *func, | |
293 | const char *format, ...) { | |
294 | ||
295 | static char buffer[LOG_BUFFER_MAX]; | |
296 | va_list ap; | |
297 | int saved_errno = errno; | |
298 | ||
299 | va_start(ap, format); | |
300 | vsnprintf(buffer, sizeof(buffer), format, ap); | |
301 | va_end(ap); | |
302 | ||
303 | char_array_0(buffer); | |
304 | log_abort_msg = buffer; | |
305 | ||
306 | LOG_DISPATCH(LOG_CRIT, file, line, func, format); | |
307 | abort(); | |
5899f3b7 | 308 | |
185986c6 | 309 | /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */ |
c9b97d2a | 310 | errno = saved_errno; |
5899f3b7 | 311 | } |
34f0e866 LP |
312 | |
313 | int log_set_target_from_string(const char *e) { | |
314 | LogTarget t; | |
315 | ||
316 | if ((t = log_target_from_string(e)) < 0) | |
317 | return -EINVAL; | |
318 | ||
319 | log_set_target(t); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | int log_set_max_level_from_string(const char *e) { | |
324 | int t; | |
325 | ||
326 | if ((t = log_level_from_string(e)) < 0) | |
327 | return -EINVAL; | |
328 | ||
329 | log_set_max_level(t); | |
330 | return 0; | |
331 | } | |
332 | ||
333 | void log_parse_environment(void) { | |
334 | const char *e; | |
335 | ||
336 | if ((e = getenv("SYSTEMD_LOG_TARGET"))) | |
337 | if (log_set_target_from_string(e) < 0) | |
338 | log_warning("Failed to parse log target %s. Ignoring.", e); | |
339 | ||
340 | if ((e = getenv("SYSTEMD_LOG_LEVEL"))) | |
341 | if (log_set_max_level_from_string(e) < 0) | |
342 | log_warning("Failed to parse log level %s. Ignoring.", e); | |
343 | } | |
344 | ||
1adf1049 LP |
345 | LogTarget log_get_target(void) { |
346 | return log_target; | |
347 | } | |
348 | ||
349 | int log_get_max_level(void) { | |
350 | return log_max_level; | |
351 | } | |
352 | ||
34f0e866 LP |
353 | static const char *const log_target_table[] = { |
354 | [LOG_TARGET_CONSOLE] = "console", | |
355 | [LOG_TARGET_SYSLOG] = "syslog", | |
356 | [LOG_TARGET_KMSG] = "kmsg", | |
357 | }; | |
358 | ||
359 | DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget); |