chiark / gitweb /
Assorted format fixes
[elogind.git] / src / shared / 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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser 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 #include <stddef.h>
30 #include <printf.h>
31
32 #include "log.h"
33 #include "util.h"
34 #include "missing.h"
35 #include "macro.h"
36 #include "socket-util.h"
37
38 #define SNDBUF_SIZE (8*1024*1024)
39
40 static LogTarget log_target = LOG_TARGET_CONSOLE;
41 static int log_max_level = LOG_INFO;
42 static int log_facility = LOG_DAEMON;
43
44 static int console_fd = STDERR_FILENO;
45 static int syslog_fd = -1;
46 static int kmsg_fd = -1;
47 static int journal_fd = -1;
48
49 static bool syslog_is_stream = false;
50
51 static bool show_color = false;
52 static bool show_location = false;
53
54 static bool upgrade_syslog_to_journal = false;
55
56 /* Akin to glibc's __abort_msg; which is private and we hence cannot
57  * use here. */
58 static char *log_abort_msg = NULL;
59
60 void log_close_console(void) {
61
62         if (console_fd < 0)
63                 return;
64
65         if (getpid() == 1) {
66                 if (console_fd >= 3)
67                         safe_close(console_fd);
68
69                 console_fd = -1;
70         }
71 }
72
73 static int log_open_console(void) {
74
75         if (console_fd >= 0)
76                 return 0;
77
78         if (getpid() == 1) {
79                 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
80                 if (console_fd < 0)
81                         return console_fd;
82         } else
83                 console_fd = STDERR_FILENO;
84
85         return 0;
86 }
87
88 void log_close_kmsg(void) {
89         kmsg_fd = safe_close(kmsg_fd);
90 }
91
92 static int log_open_kmsg(void) {
93
94         if (kmsg_fd >= 0)
95                 return 0;
96
97         kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
98         if (kmsg_fd < 0)
99                 return -errno;
100
101         return 0;
102 }
103
104 void log_close_syslog(void) {
105         syslog_fd = safe_close(syslog_fd);
106 }
107
108 static int create_log_socket(int type) {
109         struct timeval tv;
110         int fd;
111
112         fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
113         if (fd < 0)
114                 return -errno;
115
116         fd_inc_sndbuf(fd, SNDBUF_SIZE);
117
118         /* We need a blocking fd here since we'd otherwise lose
119         messages way too early. However, let's not hang forever in the
120         unlikely case of a deadlock. */
121         if (getpid() == 1)
122                 timeval_store(&tv, 10 * USEC_PER_MSEC);
123         else
124                 timeval_store(&tv, 10 * USEC_PER_SEC);
125         (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
126
127         return fd;
128 }
129
130 static int log_open_syslog(void) {
131
132         static const union sockaddr_union sa = {
133                 .un.sun_family = AF_UNIX,
134                 .un.sun_path = "/dev/log",
135         };
136
137         int r;
138
139         if (syslog_fd >= 0)
140                 return 0;
141
142         syslog_fd = create_log_socket(SOCK_DGRAM);
143         if (syslog_fd < 0) {
144                 r = syslog_fd;
145                 goto fail;
146         }
147
148         if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
149                 safe_close(syslog_fd);
150
151                 /* Some legacy syslog systems still use stream
152                  * sockets. They really shouldn't. But what can we
153                  * do... */
154                 syslog_fd = create_log_socket(SOCK_STREAM);
155                 if (syslog_fd < 0) {
156                         r = syslog_fd;
157                         goto fail;
158                 }
159
160                 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
161                         r = -errno;
162                         goto fail;
163                 }
164
165                 syslog_is_stream = true;
166         } else
167                 syslog_is_stream = false;
168
169         return 0;
170
171 fail:
172         log_close_syslog();
173         return r;
174 }
175
176 void log_close_journal(void) {
177         journal_fd = safe_close(journal_fd);
178 }
179
180 static int log_open_journal(void) {
181
182         static const union sockaddr_union sa = {
183                 .un.sun_family = AF_UNIX,
184                 .un.sun_path = "/run/systemd/journal/socket",
185         };
186
187         int r;
188
189         if (journal_fd >= 0)
190                 return 0;
191
192         journal_fd = create_log_socket(SOCK_DGRAM);
193         if (journal_fd < 0) {
194                 r = journal_fd;
195                 goto fail;
196         }
197
198         if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
199                 r = -errno;
200                 goto fail;
201         }
202
203         return 0;
204
205 fail:
206         log_close_journal();
207         return r;
208 }
209
210 int log_open(void) {
211         int r;
212
213         /* If we don't use the console we close it here, to not get
214          * killed by SAK. If we don't use syslog we close it here so
215          * that we are not confused by somebody deleting the socket in
216          * the fs. If we don't use /dev/kmsg we still keep it open,
217          * because there is no reason to close it. */
218
219         if (log_target == LOG_TARGET_NULL) {
220                 log_close_journal();
221                 log_close_syslog();
222                 log_close_console();
223                 return 0;
224         }
225
226         if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
227             getpid() == 1 ||
228             isatty(STDERR_FILENO) <= 0) {
229
230                 if (log_target == LOG_TARGET_AUTO ||
231                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
232                     log_target == LOG_TARGET_JOURNAL) {
233                         r = log_open_journal();
234                         if (r >= 0) {
235                                 log_close_syslog();
236                                 log_close_console();
237                                 return r;
238                         }
239                 }
240
241                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
242                     log_target == LOG_TARGET_SYSLOG) {
243                         r = log_open_syslog();
244                         if (r >= 0) {
245                                 log_close_journal();
246                                 log_close_console();
247                                 return r;
248                         }
249                 }
250
251                 if (log_target == LOG_TARGET_AUTO ||
252                     log_target == LOG_TARGET_SAFE ||
253                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
254                     log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
255                     log_target == LOG_TARGET_KMSG) {
256                         r = log_open_kmsg();
257                         if (r >= 0) {
258                                 log_close_journal();
259                                 log_close_syslog();
260                                 log_close_console();
261                                 return r;
262                         }
263                 }
264         }
265
266         log_close_journal();
267         log_close_syslog();
268
269         return log_open_console();
270 }
271
272 void log_set_target(LogTarget target) {
273         assert(target >= 0);
274         assert(target < _LOG_TARGET_MAX);
275
276         if (upgrade_syslog_to_journal) {
277                 if (target == LOG_TARGET_SYSLOG)
278                         target = LOG_TARGET_JOURNAL;
279                 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
280                         target = LOG_TARGET_JOURNAL_OR_KMSG;
281         }
282
283         log_target = target;
284 }
285
286 void log_close(void) {
287         log_close_journal();
288         log_close_syslog();
289         log_close_kmsg();
290         log_close_console();
291 }
292
293 void log_forget_fds(void) {
294         console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
295 }
296
297 void log_set_max_level(int level) {
298         assert((level & LOG_PRIMASK) == level);
299
300         log_max_level = level;
301 }
302
303 void log_set_facility(int facility) {
304         log_facility = facility;
305 }
306
307 static int write_to_console(
308                 int level,
309                 int error,
310                 const char *file,
311                 int line,
312                 const char *func,
313                 const char *object_field,
314                 const char *object,
315                 const char *buffer) {
316
317         char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2];
318         struct iovec iovec[6] = {};
319         unsigned n = 0;
320         bool highlight;
321
322         if (console_fd < 0)
323                 return 0;
324
325         if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
326                 sprintf(prefix, "<%i>", level);
327                 IOVEC_SET_STRING(iovec[n++], prefix);
328         }
329
330         highlight = LOG_PRI(level) <= LOG_ERR && show_color;
331
332         if (show_location) {
333                 snprintf(location, sizeof(location), "(%s:%i) ", file, line);
334                 char_array_0(location);
335                 IOVEC_SET_STRING(iovec[n++], location);
336         }
337
338         if (highlight)
339                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
340         IOVEC_SET_STRING(iovec[n++], buffer);
341         if (highlight)
342                 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
343         IOVEC_SET_STRING(iovec[n++], "\n");
344
345         if (writev(console_fd, iovec, n) < 0) {
346
347                 if (errno == EIO && getpid() == 1) {
348
349                         /* If somebody tried to kick us from our
350                          * console tty (via vhangup() or suchlike),
351                          * try to reconnect */
352
353                         log_close_console();
354                         log_open_console();
355
356                         if (console_fd < 0)
357                                 return 0;
358
359                         if (writev(console_fd, iovec, n) < 0)
360                                 return -errno;
361                 } else
362                         return -errno;
363         }
364
365         return 1;
366 }
367
368 static int write_to_syslog(
369                 int level,
370                 int error,
371                 const char *file,
372                 int line,
373                 const char *func,
374                 const char *object_field,
375                 const char *object,
376                 const char *buffer) {
377
378         char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_time[64], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
379         struct iovec iovec[5] = {};
380         struct msghdr msghdr = {
381                 .msg_iov = iovec,
382                 .msg_iovlen = ELEMENTSOF(iovec),
383         };
384         time_t t;
385         struct tm *tm;
386
387         if (syslog_fd < 0)
388                 return 0;
389
390         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
391         char_array_0(header_priority);
392
393         t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
394         tm = localtime(&t);
395         if (!tm)
396                 return -EINVAL;
397
398         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
399                 return -EINVAL;
400
401         snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
402         char_array_0(header_pid);
403
404         IOVEC_SET_STRING(iovec[0], header_priority);
405         IOVEC_SET_STRING(iovec[1], header_time);
406         IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
407         IOVEC_SET_STRING(iovec[3], header_pid);
408         IOVEC_SET_STRING(iovec[4], buffer);
409
410         /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
411         if (syslog_is_stream)
412                 iovec[4].iov_len++;
413
414         for (;;) {
415                 ssize_t n;
416
417                 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
418                 if (n < 0)
419                         return -errno;
420
421                 if (!syslog_is_stream ||
422                     (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
423                         break;
424
425                 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
426         }
427
428         return 1;
429 }
430
431 static int write_to_kmsg(
432                 int level,
433                 int error,
434                 const char*file,
435                 int line,
436                 const char *func,
437                 const char *object_field,
438                 const char *object,
439                 const char *buffer) {
440
441         char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
442         struct iovec iovec[5] = {};
443
444         if (kmsg_fd < 0)
445                 return 0;
446
447         snprintf(header_priority, sizeof(header_priority), "<%i>", level);
448         char_array_0(header_priority);
449
450         snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
451         char_array_0(header_pid);
452
453         IOVEC_SET_STRING(iovec[0], header_priority);
454         IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
455         IOVEC_SET_STRING(iovec[2], header_pid);
456         IOVEC_SET_STRING(iovec[3], buffer);
457         IOVEC_SET_STRING(iovec[4], "\n");
458
459         if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
460                 return -errno;
461
462         return 1;
463 }
464
465 static int log_do_header(
466                 char *header,
467                 size_t size,
468                 int level,
469                 int error,
470                 const char *file, int line, const char *func,
471                 const char *object_field, const char *object) {
472
473         snprintf(header, size,
474                  "PRIORITY=%i\n"
475                  "SYSLOG_FACILITY=%i\n"
476                  "%s%s%s"
477                  "%s%.*i%s"
478                  "%s%s%s"
479                  "%s%.*i%s"
480                  "%s%s%s"
481                  "SYSLOG_IDENTIFIER=%s\n",
482                  LOG_PRI(level),
483                  LOG_FAC(level),
484                  isempty(file) ? "" : "CODE_FILE=",
485                  isempty(file) ? "" : file,
486                  isempty(file) ? "" : "\n",
487                  line ? "CODE_LINE=" : "",
488                  line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
489                  line ? "\n" : "",
490                  isempty(func) ? "" : "CODE_FUNCTION=",
491                  isempty(func) ? "" : func,
492                  isempty(func) ? "" : "\n",
493                  error ? "ERRNO=" : "",
494                  error ? 1 : 0, error,
495                  error ? "\n" : "",
496                  isempty(object) ? "" : object_field,
497                  isempty(object) ? "" : object,
498                  isempty(object) ? "" : "\n",
499                  program_invocation_short_name);
500         header[size - 1] = '\0';
501
502         return 0;
503 }
504
505 static int write_to_journal(
506                 int level,
507                 int error,
508                 const char*file,
509                 int line,
510                 const char *func,
511                 const char *object_field,
512                 const char *object,
513                 const char *buffer) {
514
515         char header[LINE_MAX];
516         struct iovec iovec[4] = {};
517         struct msghdr mh = {};
518
519         if (journal_fd < 0)
520                 return 0;
521
522         log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object);
523
524         IOVEC_SET_STRING(iovec[0], header);
525         IOVEC_SET_STRING(iovec[1], "MESSAGE=");
526         IOVEC_SET_STRING(iovec[2], buffer);
527         IOVEC_SET_STRING(iovec[3], "\n");
528
529         mh.msg_iov = iovec;
530         mh.msg_iovlen = ELEMENTSOF(iovec);
531
532         if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
533                 return -errno;
534
535         return 1;
536 }
537
538 static int log_dispatch(
539                 int level,
540                 int error,
541                 const char *file,
542                 int line,
543                 const char *func,
544                 const char *object_field,
545                 const char *object,
546                 char *buffer) {
547
548         assert(buffer);
549
550         if (log_target == LOG_TARGET_NULL)
551                 return -error;
552
553         /* Patch in LOG_DAEMON facility if necessary */
554         if ((level & LOG_FACMASK) == 0)
555                 level = log_facility | LOG_PRI(level);
556
557         if (error < 0)
558                 error = -error;
559
560         do {
561                 char *e;
562                 int k = 0;
563
564                 buffer += strspn(buffer, NEWLINE);
565
566                 if (buffer[0] == 0)
567                         break;
568
569                 if ((e = strpbrk(buffer, NEWLINE)))
570                         *(e++) = 0;
571
572                 if (log_target == LOG_TARGET_AUTO ||
573                     log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
574                     log_target == LOG_TARGET_JOURNAL) {
575
576                         k = write_to_journal(level, error, file, line, func, object_field, object, buffer);
577                         if (k < 0) {
578                                 if (k != -EAGAIN)
579                                         log_close_journal();
580                                 log_open_kmsg();
581                         }
582                 }
583
584                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
585                     log_target == LOG_TARGET_SYSLOG) {
586
587                         k = write_to_syslog(level, error, file, line, func, object_field, object, buffer);
588                         if (k < 0) {
589                                 if (k != -EAGAIN)
590                                         log_close_syslog();
591                                 log_open_kmsg();
592                         }
593                 }
594
595                 if (k <= 0 &&
596                     (log_target == LOG_TARGET_AUTO ||
597                      log_target == LOG_TARGET_SAFE ||
598                      log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
599                      log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
600                      log_target == LOG_TARGET_KMSG)) {
601
602                         k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
603                         if (k < 0) {
604                                 log_close_kmsg();
605                                 log_open_console();
606                         }
607                 }
608
609                 if (k <= 0)
610                         (void) write_to_console(level, error, file, line, func, object_field, object, buffer);
611
612                 buffer = e;
613         } while (buffer);
614
615         return -error;
616 }
617
618 int log_dump_internal(
619         int level,
620         int error,
621         const char *file,
622         int line,
623         const char *func,
624         char *buffer) {
625
626         PROTECT_ERRNO;
627
628         /* This modifies the buffer... */
629
630         if (error < 0)
631                 error = -error;
632
633         if (_likely_(LOG_PRI(level) > log_max_level))
634                 return -error;
635
636         return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
637 }
638
639 int log_internalv(
640                 int level,
641                 int error,
642                 const char*file,
643                 int line,
644                 const char *func,
645                 const char *format,
646                 va_list ap) {
647
648         PROTECT_ERRNO;
649         char buffer[LINE_MAX];
650
651         if (error < 0)
652                 error = -error;
653
654         if (_likely_(LOG_PRI(level) > log_max_level))
655                 return -error;
656
657         /* Make sure that %m maps to the specified error */
658         if (error != 0)
659                 errno = error;
660
661         vsnprintf(buffer, sizeof(buffer), format, ap);
662         char_array_0(buffer);
663
664         return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
665 }
666
667 int log_internal(
668                 int level,
669                 int error,
670                 const char*file,
671                 int line,
672                 const char *func,
673                 const char *format, ...) {
674
675         va_list ap;
676         int r;
677
678         va_start(ap, format);
679         r = log_internalv(level, error, file, line, func, format, ap);
680         va_end(ap);
681
682         return r;
683 }
684
685 int log_object_internalv(
686                 int level,
687                 int error,
688                 const char*file,
689                 int line,
690                 const char *func,
691                 const char *object_field,
692                 const char *object,
693                 const char *format,
694                 va_list ap) {
695
696         PROTECT_ERRNO;
697         char buffer[LINE_MAX];
698
699         if (error < 0)
700                 error = -error;
701
702         if (_likely_(LOG_PRI(level) > log_max_level))
703                 return -error;
704
705         /* Make sure that %m maps to the specified error */
706         if (error != 0)
707                 errno = error;
708
709         vsnprintf(buffer, sizeof(buffer), format, ap);
710         char_array_0(buffer);
711
712         return log_dispatch(level, error, file, line, func, object_field, object, buffer);
713 }
714
715 int log_object_internal(
716                 int level,
717                 int error,
718                 const char*file,
719                 int line,
720                 const char *func,
721                 const char *object_field,
722                 const char *object,
723                 const char *format, ...) {
724
725         va_list ap;
726         int r;
727
728         va_start(ap, format);
729         r = log_object_internalv(level, error, file, line, func, object_field, object, format, ap);
730         va_end(ap);
731
732         return r;
733 }
734
735 static void log_assert(
736                 int level,
737                 const char *text,
738                 const char *file,
739                 int line,
740                 const char *func,
741                 const char *format) {
742
743         static char buffer[LINE_MAX];
744
745         if (_likely_(LOG_PRI(level) > log_max_level))
746                 return;
747
748         DISABLE_WARNING_FORMAT_NONLITERAL;
749         snprintf(buffer, sizeof(buffer), format, text, file, line, func);
750         REENABLE_WARNING;
751
752         char_array_0(buffer);
753         log_abort_msg = buffer;
754
755         log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
756 }
757
758 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
759         log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
760         abort();
761 }
762
763 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
764         log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
765         abort();
766 }
767
768 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
769         PROTECT_ERRNO;
770         log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
771 }
772
773 int log_oom_internal(const char *file, int line, const char *func) {
774         log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory.");
775         return -ENOMEM;
776 }
777
778 int log_struct_internal(
779                 int level,
780                 int error,
781                 const char *file,
782                 int line,
783                 const char *func,
784                 const char *format, ...) {
785
786         char buf[LINE_MAX];
787         bool found = false;
788         PROTECT_ERRNO;
789         va_list ap;
790
791         if (error < 0)
792                 error = -error;
793
794         if (_likely_(LOG_PRI(level) > log_max_level))
795                 return -error;
796
797         if (log_target == LOG_TARGET_NULL)
798                 return -error;
799
800         if ((level & LOG_FACMASK) == 0)
801                 level = log_facility | LOG_PRI(level);
802
803         if ((log_target == LOG_TARGET_AUTO ||
804              log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
805              log_target == LOG_TARGET_JOURNAL) &&
806             journal_fd >= 0) {
807                 char header[LINE_MAX];
808                 struct iovec iovec[17] = {};
809                 unsigned n = 0, i;
810                 struct msghdr mh = {
811                         .msg_iov = iovec,
812                 };
813                 static const char nl = '\n';
814                 bool fallback = false;
815
816                 /* If the journal is available do structured logging */
817                 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
818                 IOVEC_SET_STRING(iovec[n++], header);
819
820                 va_start(ap, format);
821                 while (format && n + 1 < ELEMENTSOF(iovec)) {
822                         va_list aq;
823                         char *m;
824
825                         /* We need to copy the va_list structure,
826                          * since vasprintf() leaves it afterwards at
827                          * an undefined location */
828
829                         if (error != 0)
830                                 errno = error;
831
832                         va_copy(aq, ap);
833                         if (vasprintf(&m, format, aq) < 0) {
834                                 va_end(aq);
835                                 fallback = true;
836                                 goto finish;
837                         }
838                         va_end(aq);
839
840                         /* Now, jump enough ahead, so that we point to
841                          * the next format string */
842                         VA_FORMAT_ADVANCE(format, ap);
843
844                         IOVEC_SET_STRING(iovec[n++], m);
845
846                         iovec[n].iov_base = (char*) &nl;
847                         iovec[n].iov_len = 1;
848                         n++;
849
850                         format = va_arg(ap, char *);
851                 }
852
853                 mh.msg_iovlen = n;
854
855                 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
856
857         finish:
858                 va_end(ap);
859                 for (i = 1; i < n; i += 2)
860                         free(iovec[i].iov_base);
861
862                 if (!fallback)
863                         return -error;
864         }
865
866         /* Fallback if journal logging is not available or didn't work. */
867
868         va_start(ap, format);
869         while (format) {
870                 va_list aq;
871
872                 if (error != 0)
873                         errno = error;
874
875                 va_copy(aq, ap);
876                 vsnprintf(buf, sizeof(buf), format, aq);
877                 va_end(aq);
878                 char_array_0(buf);
879
880                 if (startswith(buf, "MESSAGE=")) {
881                         found = true;
882                         break;
883                 }
884
885                 VA_FORMAT_ADVANCE(format, ap);
886
887                 format = va_arg(ap, char *);
888         }
889         va_end(ap);
890
891         if (!found)
892                 return -error;
893
894         return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
895 }
896
897 int log_set_target_from_string(const char *e) {
898         LogTarget t;
899
900         t = log_target_from_string(e);
901         if (t < 0)
902                 return -EINVAL;
903
904         log_set_target(t);
905         return 0;
906 }
907
908 int log_set_max_level_from_string(const char *e) {
909         int t;
910
911         t = log_level_from_string(e);
912         if (t < 0)
913                 return t;
914
915         log_set_max_level(t);
916         return 0;
917 }
918
919 static int parse_proc_cmdline_item(const char *key, const char *value) {
920
921         /*
922          * The systemd.log_xyz= settings are parsed by all tools, and
923          * so is "debug".
924          *
925          * However, "quiet" is only parsed by PID 1!
926          */
927
928         if (streq(key, "debug") && !value)
929                 log_set_max_level(LOG_DEBUG);
930
931         else if (streq(key, "systemd.log_target") && value) {
932
933                 if (log_set_target_from_string(value) < 0)
934                         log_warning("Failed to parse log target '%s'. Ignoring.", value);
935
936         } else if (streq(key, "systemd.log_level") && value) {
937
938                 if (log_set_max_level_from_string(value) < 0)
939                         log_warning("Failed to parse log level '%s'. Ignoring.", value);
940
941         } else if (streq(key, "systemd.log_color") && value) {
942
943                 if (log_show_color_from_string(value) < 0)
944                         log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
945
946         } else if (streq(key, "systemd.log_location") && value) {
947
948                 if (log_show_location_from_string(value) < 0)
949                         log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
950         }
951
952         return 0;
953 }
954
955 void log_parse_environment(void) {
956         const char *e;
957
958         (void) parse_proc_cmdline(parse_proc_cmdline_item);
959
960         e = secure_getenv("SYSTEMD_LOG_TARGET");
961         if (e && log_set_target_from_string(e) < 0)
962                 log_warning("Failed to parse log target '%s'. Ignoring.", e);
963
964         e = secure_getenv("SYSTEMD_LOG_LEVEL");
965         if (e && log_set_max_level_from_string(e) < 0)
966                 log_warning("Failed to parse log level '%s'. Ignoring.", e);
967
968         e = secure_getenv("SYSTEMD_LOG_COLOR");
969         if (e && log_show_color_from_string(e) < 0)
970                 log_warning("Failed to parse bool '%s'. Ignoring.", e);
971
972         e = secure_getenv("SYSTEMD_LOG_LOCATION");
973         if (e && log_show_location_from_string(e) < 0)
974                 log_warning("Failed to parse bool '%s'. Ignoring.", e);
975 }
976
977 LogTarget log_get_target(void) {
978         return log_target;
979 }
980
981 int log_get_max_level(void) {
982         return log_max_level;
983 }
984
985 void log_show_color(bool b) {
986         show_color = b;
987 }
988
989 bool log_get_show_color(void) {
990         return show_color;
991 }
992
993 void log_show_location(bool b) {
994         show_location = b;
995 }
996
997 bool log_get_show_location(void) {
998         return show_location;
999 }
1000
1001 int log_show_color_from_string(const char *e) {
1002         int t;
1003
1004         t = parse_boolean(e);
1005         if (t < 0)
1006                 return t;
1007
1008         log_show_color(t);
1009         return 0;
1010 }
1011
1012 int log_show_location_from_string(const char *e) {
1013         int t;
1014
1015         t = parse_boolean(e);
1016         if (t < 0)
1017                 return t;
1018
1019         log_show_location(t);
1020         return 0;
1021 }
1022
1023 bool log_on_console(void) {
1024         if (log_target == LOG_TARGET_CONSOLE ||
1025             log_target == LOG_TARGET_CONSOLE_PREFIXED)
1026                 return true;
1027
1028         return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1029 }
1030
1031 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1032         [LOG_TARGET_CONSOLE] = "console",
1033         [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1034         [LOG_TARGET_KMSG] = "kmsg",
1035         [LOG_TARGET_JOURNAL] = "journal",
1036         [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1037         [LOG_TARGET_SYSLOG] = "syslog",
1038         [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1039         [LOG_TARGET_AUTO] = "auto",
1040         [LOG_TARGET_SAFE] = "safe",
1041         [LOG_TARGET_NULL] = "null"
1042 };
1043
1044 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1045
1046 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1047         if (si->ssi_pid > 0) {
1048                 _cleanup_free_ char *p = NULL;
1049
1050                 get_process_comm(si->ssi_pid, &p);
1051
1052                 log_full(level,
1053                          "Received SIG%s from PID %"PRIu32" (%s).",
1054                          signal_to_string(si->ssi_signo),
1055                          si->ssi_pid, strna(p));
1056         } else
1057                 log_full(level,
1058                          "Received SIG%s.",
1059                          signal_to_string(si->ssi_signo));
1060
1061 }
1062
1063 void log_set_upgrade_syslog_to_journal(bool b) {
1064         upgrade_syslog_to_journal = b;
1065 }