]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.c
github: Update for main branch
[mirror_lxc.git] / src / lxc / log.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Cedric Le Goater <legoater@free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <inttypes.h>
31 #include <limits.h>
32 #include <pthread.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <syslog.h>
40 #include <unistd.h>
41
42 #include "caps.h"
43 #include "config.h"
44 #include "file_utils.h"
45 #include "log.h"
46 #include "lxccontainer.h"
47 #include "utils.h"
48
49 #ifndef HAVE_STRLCPY
50 #include "include/strlcpy.h"
51 #endif
52
53 #if HAVE_DLOG
54 #include <dlog.h>
55
56 #undef LOG_TAG
57 #define LOG_TAG "LXC"
58 #endif
59
60 /* We're logging in seconds and nanoseconds. Assuming that the underlying
61 * datatype is currently at maximum a 64bit integer, we have a date string that
62 * is of maximum length (2^64 - 1) * 2 = (21 + 21) = 42.
63 */
64 #define LXC_LOG_TIME_SIZE ((INTTYPE_TO_STRLEN(uint64_t)) * 2)
65
66 int lxc_log_fd = -1;
67 static int syslog_enable = 0;
68 int lxc_quiet_specified;
69 int lxc_log_use_global_fd;
70 static int lxc_loglevel_specified;
71
72 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
73 static char *log_fname = NULL;
74 static char *log_vmname = NULL;
75
76 lxc_log_define(log, lxc);
77
78 static int lxc_log_priority_to_syslog(int priority)
79 {
80 switch (priority) {
81 case LXC_LOG_LEVEL_FATAL:
82 return LOG_EMERG;
83 case LXC_LOG_LEVEL_ALERT:
84 return LOG_ALERT;
85 case LXC_LOG_LEVEL_CRIT:
86 return LOG_CRIT;
87 case LXC_LOG_LEVEL_ERROR:
88 return LOG_ERR;
89 case LXC_LOG_LEVEL_WARN:
90 return LOG_WARNING;
91 case LXC_LOG_LEVEL_NOTICE:
92 case LXC_LOG_LEVEL_NOTSET:
93 return LOG_NOTICE;
94 case LXC_LOG_LEVEL_INFO:
95 return LOG_INFO;
96 case LXC_LOG_LEVEL_TRACE:
97 case LXC_LOG_LEVEL_DEBUG:
98 return LOG_DEBUG;
99 }
100
101 /* Not reached */
102 return LOG_NOTICE;
103 }
104
105 static const char *lxc_log_get_container_name()
106 {
107 #ifndef NO_LXC_CONF
108 if (current_config && !log_vmname)
109 return current_config->name;
110 #endif
111
112 return log_vmname;
113 }
114
115 static char *lxc_log_get_va_msg(struct lxc_log_event *event)
116 {
117 char *msg;
118 int rc, len;
119 va_list args;
120
121 if (!event)
122 return NULL;
123
124 va_copy(args, *event->vap);
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
127 len = vsnprintf(NULL, 0, event->fmt, args) + 1;
128 #pragma GCC diagnostic pop
129 va_end(args);
130
131 msg = malloc(len * sizeof(char));
132 if (!msg)
133 return NULL;
134
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
137 rc = vsnprintf(msg, len, event->fmt, *event->vap);
138 #pragma GCC diagnostic pop
139 if (rc == -1 || rc >= len) {
140 free(msg);
141 return NULL;
142 }
143
144 return msg;
145 }
146
147 /*---------------------------------------------------------------------------*/
148 static int log_append_syslog(const struct lxc_log_appender *appender,
149 struct lxc_log_event *event)
150 {
151 char *msg;
152 const char *log_container_name;
153
154 if (!syslog_enable)
155 return 0;
156
157 log_container_name = lxc_log_get_container_name();
158
159 msg = lxc_log_get_va_msg(event);
160 if (!msg)
161 return 0;
162
163 syslog(lxc_log_priority_to_syslog(event->priority),
164 "%s%s %s - %s:%s:%d - %s" ,
165 log_container_name ? log_container_name : "",
166 log_container_name ? ":" : "",
167 event->category,
168 event->locinfo->file, event->locinfo->func,
169 event->locinfo->line,
170 msg);
171 free(msg);
172
173 return 0;
174 }
175
176 /*---------------------------------------------------------------------------*/
177 static int log_append_stderr(const struct lxc_log_appender *appender,
178 struct lxc_log_event *event)
179 {
180 const char *log_container_name;
181
182 if (event->priority < LXC_LOG_LEVEL_ERROR)
183 return 0;
184
185 log_container_name = lxc_log_get_container_name();
186
187 fprintf(stderr, "%s: %s%s", log_prefix,
188 log_container_name ? log_container_name : "",
189 log_container_name ? ": " : "");
190 fprintf(stderr, "%s: %s: %d ", event->locinfo->file,
191 event->locinfo->func, event->locinfo->line);
192 #pragma GCC diagnostic push
193 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
194 vfprintf(stderr, event->fmt, *event->vap);
195 #pragma GCC diagnostic pop
196 fprintf(stderr, "\n");
197
198 return 0;
199 }
200
201 /*---------------------------------------------------------------------------*/
202 static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespec *time)
203 {
204 int64_t epoch_to_days, z, era, doe, yoe, year, doy, mp, day, month,
205 d_in_s, hours, h_in_s, minutes, seconds;
206 char nanosec[INTTYPE_TO_STRLEN(int64_t)];
207 int ret;
208
209 /* See https://howardhinnant.github.io/date_algorithms.html for an
210 * explanation of the algorithm used here.
211 */
212
213 /* Convert Epoch in seconds to number of days. */
214 epoch_to_days = time->tv_sec / 86400;
215
216 /* Shift the Epoch from 1970-01-01 to 0000-03-01. */
217 z = epoch_to_days + 719468;
218
219 /* compute the era from the serial date by simply dividing by the number
220 * of days in an era (146097).
221 */
222 era = (z >= 0 ? z : z - 146096) / 146097;
223
224 /* The day-of-era (doe) can then be found by subtracting the era number
225 * times the number of days per era, from the serial date.
226 */
227 doe = (z - era * 146097);
228
229 /* From the day-of-era (doe), the year-of-era (yoe, range [0, 399]) can
230 * be computed.
231 */
232 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
233
234 /* Given year-of-era, and era, one can now compute the year. */
235 year = yoe + era * 400;
236
237 /* Also the day-of-year, again with the year beginning on Mar. 1, can be
238 * computed from the day-of-era and year-of-era.
239 */
240 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
241
242 /* Given day-of-year, find the month number. */
243 mp = (5 * doy + 2) / 153;
244
245 /* From day-of-year and month-of-year we can now easily compute
246 * day-of-month.
247 */
248 day = doy - (153 * mp + 2) / 5 + 1;
249
250 /* Transform the month number from the [0, 11] / [Mar, Feb] system to
251 * the civil system: [1, 12] to find the correct month.
252 */
253 month = mp + (mp < 10 ? 3 : -9);
254
255 /* The algorithm assumes that a year begins on 1 March, so add 1 before
256 * that. */
257 if (month < 3)
258 year++;
259
260 /* Transform days in the epoch to seconds. */
261 d_in_s = epoch_to_days * 86400;
262
263 /* To find the current hour simply substract the Epoch_to_days from the
264 * total Epoch and divide by the number of seconds in an hour.
265 */
266 hours = (time->tv_sec - d_in_s) / 3600;
267
268 /* Transform hours to seconds. */
269 h_in_s = hours * 3600;
270
271 /* Calculate minutes by subtracting the seconds for all days in the
272 * epoch and for all hours in the epoch and divide by the number of
273 * minutes in an hour.
274 */
275 minutes = (time->tv_sec - d_in_s - h_in_s) / 60;
276
277 /* Calculate the seconds by subtracting the seconds for all days in the
278 * epoch, hours in the epoch and minutes in the epoch.
279 */
280 seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60));
281
282 /* Make string from nanoseconds. */
283 ret = snprintf(nanosec, sizeof(nanosec), "%"PRId64, (int64_t)time->tv_nsec);
284 if (ret < 0 || ret >= sizeof(nanosec))
285 return -1;
286
287 /* Create final timestamp for the log and shorten nanoseconds to 3
288 * digit precision.
289 */
290 ret = snprintf(buf, bufsize,
291 "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64
292 "%02" PRId64 "%02" PRId64 ".%.3s",
293 year, month, day, hours, minutes, seconds, nanosec);
294 if (ret < 0 || (size_t)ret >= bufsize)
295 return -1;
296
297 return 0;
298 }
299
300 /* This function needs to make extra sure that it is thread-safe. We had some
301 * problems with that before. This especially involves time-conversion
302 * functions. I don't want to find any localtime() or gmtime() functions or
303 * relatives in here. Not even localtime_r() or gmtime_r() or relatives. They
304 * all fiddle with global variables and locking in various libcs. They cause
305 * deadlocks when liblxc is used multi-threaded and no matter how smart you
306 * think you are, you __will__ cause trouble using them.
307 * (As a short example how this can cause trouble: LXD uses forkstart to fork
308 * off a new process that runs the container. At the same time the go runtime
309 * LXD relies on does its own multi-threading thing which we can't control. The
310 * fork()ing + threading then seems to mess with the locking states in these
311 * time functions causing deadlocks.)
312 * The current solution is to be good old unix people and use the Epoch as our
313 * reference point and simply use the seconds and nanoseconds that have past
314 * since then. This relies on clock_gettime() which is explicitly marked MT-Safe
315 * with no restrictions! This way, anyone who is really strongly invested in
316 * getting the actual time the log entry was created, can just convert it for
317 * themselves. Our logging is mostly done for debugging purposes so don't try
318 * to make it pretty. Pretty might cost you thread-safety.
319 */
320 static int log_append_logfile(const struct lxc_log_appender *appender,
321 struct lxc_log_event *event)
322 {
323 char buffer[LXC_LOG_BUFFER_SIZE];
324 char date_time[LXC_LOG_TIME_SIZE];
325 int n;
326 ssize_t ret;
327 int fd_to_use = -1;
328 const char *log_container_name;
329
330 #ifndef NO_LXC_CONF
331 if (current_config)
332 if (!lxc_log_use_global_fd)
333 fd_to_use = current_config->logfd;
334 #endif
335
336 log_container_name = lxc_log_get_container_name();
337
338 if (fd_to_use == -1)
339 fd_to_use = lxc_log_fd;
340
341 if (fd_to_use == -1)
342 return 0;
343
344 if (lxc_unix_epoch_to_utc(date_time, LXC_LOG_TIME_SIZE, &event->timestamp) < 0)
345 return -1;
346
347 n = snprintf(buffer, sizeof(buffer),
348 "%s%s%s %s %-8s %s - %s:%s:%d - ",
349 log_prefix,
350 log_container_name ? " " : "",
351 log_container_name ? log_container_name : "",
352 date_time,
353 lxc_log_priority_to_string(event->priority),
354 event->category,
355 event->locinfo->file, event->locinfo->func,
356 event->locinfo->line);
357 if (n < 0)
358 return n;
359
360 if ((size_t)n < STRARRAYLEN(buffer)) {
361 #pragma GCC diagnostic push
362 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
363 ret = vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt, *event->vap);
364 #pragma GCC diagnostic pop
365 if (ret < 0)
366 return 0;
367
368 n += ret;
369 }
370
371 if ((size_t)n >= sizeof(buffer))
372 n = STRARRAYLEN(buffer);
373
374 buffer[n] = '\n';
375
376 return lxc_write_nointr(fd_to_use, buffer, n + 1);
377 }
378
379 #if HAVE_DLOG
380 static int log_append_dlog(const struct lxc_log_appender *appender,
381 struct lxc_log_event *event)
382 {
383 char *msg = lxc_log_get_va_msg(event);
384 const char *log_container_name = lxc_log_get_container_name();
385
386 switch (event->priority) {
387 case LXC_LOG_LEVEL_TRACE:
388 case LXC_LOG_LEVEL_DEBUG:
389 print_log(DLOG_DEBUG, LOG_TAG, "%s: %s(%d) > [%s] %s",
390 event->locinfo->file, event->locinfo->func, event->locinfo->line,
391 log_container_name ? log_container_name : "-",
392 msg ? msg : "-");
393 break;
394 case LXC_LOG_LEVEL_INFO:
395 print_log(DLOG_INFO, LOG_TAG, "%s: %s(%d) > [%s] %s",
396 event->locinfo->file, event->locinfo->func, event->locinfo->line,
397 log_container_name ? log_container_name : "-",
398 msg ? msg : "-");
399 break;
400 case LXC_LOG_LEVEL_NOTICE:
401 case LXC_LOG_LEVEL_WARN:
402 print_log(DLOG_WARN, LOG_TAG, "%s: %s(%d) > [%s] %s",
403 event->locinfo->file, event->locinfo->func, event->locinfo->line,
404 log_container_name ? log_container_name : "-",
405 msg ? msg : "-");
406 break;
407 case LXC_LOG_LEVEL_ERROR:
408 print_log(DLOG_ERROR, LOG_TAG, "%s: %s(%d) > [%s] %s",
409 event->locinfo->file, event->locinfo->func, event->locinfo->line,
410 log_container_name ? log_container_name : "-",
411 msg ? msg : "-");
412 break;
413 case LXC_LOG_LEVEL_CRIT:
414 case LXC_LOG_LEVEL_ALERT:
415 case LXC_LOG_LEVEL_FATAL:
416 print_log(DLOG_FATAL, LOG_TAG, "%s: %s(%d) > [%s] %s",
417 event->locinfo->file, event->locinfo->func, event->locinfo->line,
418 log_container_name ? log_container_name : "-",
419 msg ? msg : "-");
420 break;
421 default:
422 break;
423 }
424
425 free(msg);
426 return 0;
427 }
428 #endif
429
430 static struct lxc_log_appender log_appender_syslog = {
431 .name = "syslog",
432 .append = log_append_syslog,
433 .next = NULL,
434 };
435
436 static struct lxc_log_appender log_appender_stderr = {
437 .name = "stderr",
438 .append = log_append_stderr,
439 .next = NULL,
440 };
441
442 static struct lxc_log_appender log_appender_logfile = {
443 .name = "logfile",
444 .append = log_append_logfile,
445 .next = NULL,
446 };
447
448 #if HAVE_DLOG
449 static struct lxc_log_appender log_appender_dlog = {
450 .name = "dlog",
451 .append = log_append_dlog,
452 .next = NULL,
453 };
454 #endif
455
456 static struct lxc_log_category log_root = {
457 .name = "root",
458 .priority = LXC_LOG_LEVEL_ERROR,
459 .appender = NULL,
460 .parent = NULL,
461 };
462
463 #if HAVE_DLOG
464 struct lxc_log_category lxc_log_category_lxc = {
465 .name = "lxc",
466 .priority = LXC_LOG_LEVEL_TRACE,
467 .appender = &log_appender_dlog,
468 .parent = &log_root
469 };
470 #else
471 struct lxc_log_category lxc_log_category_lxc = {
472 .name = "lxc",
473 .priority = LXC_LOG_LEVEL_ERROR,
474 .appender = &log_appender_logfile,
475 .parent = &log_root
476 };
477 #endif
478
479 /*---------------------------------------------------------------------------*/
480 static int build_dir(const char *name)
481 {
482 char *e, *n, *p;
483
484 /* Make copy of the string since we'll be modifying it. */
485 n = strdup(name);
486 if (!n)
487 return -1;
488
489 e = &n[strlen(n)];
490 for (p = n + 1; p < e; p++) {
491 int ret;
492
493 if (*p != '/')
494 continue;
495 *p = '\0';
496
497 ret = lxc_unpriv(mkdir(n, 0755));
498 if (ret && errno != EEXIST) {
499 SYSERROR("Failed to create directory \"%s\"", n);
500 free(n);
501 return -1;
502 }
503
504 *p = '/';
505 }
506
507 free(n);
508 return 0;
509 }
510
511 /*---------------------------------------------------------------------------*/
512 static int log_open(const char *name)
513 {
514 int fd;
515 int newfd;
516
517 fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660));
518 if (fd < 0) {
519 SYSERROR("Failed to open log file \"%s\"", name);
520 return -1;
521 }
522
523 if (fd > 2)
524 return fd;
525
526 newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO);
527 if (newfd == -1)
528 SYSERROR("Failed to dup log fd %d", fd);
529
530 close(fd);
531 return newfd;
532 }
533
534 /*
535 * Build the path to the log file
536 * @name : the name of the container
537 * @lxcpath : the lxcpath to use as a basename or NULL to use LOGPATH
538 * Returns malloced path on success, or NULL on failure
539 */
540 static char *build_log_path(const char *name, const char *lxcpath)
541 {
542 char *p;
543 int ret;
544 size_t len;
545 bool use_dir;
546
547 if (!name)
548 return NULL;
549
550 #if USE_CONFIGPATH_LOGS
551 use_dir = true;
552 #else
553 use_dir = false;
554 #endif
555
556 /*
557 * If USE_CONFIGPATH_LOGS is true or lxcpath is given, the resulting
558 * path will be:
559 * '$logpath' + '/' + '$name' + '/' + '$name' + '.log' + '\0'
560 *
561 * If USE_CONFIGPATH_LOGS is false the resulting path will be:
562 * '$logpath' + '/' + '$name' + '.log' + '\0'
563 */
564 len = strlen(name) + 6; /* 6 == '/' + '.log' + '\0' */
565 if (lxcpath)
566 use_dir = true;
567 else
568 lxcpath = LOGPATH;
569
570 if (use_dir)
571 len += strlen(lxcpath) + 1 + strlen(name) + 1; /* add "/$container_name/" */
572 else
573 len += strlen(lxcpath) + 1;
574
575 p = malloc(len);
576 if (!p)
577 return p;
578
579 if (use_dir)
580 ret = snprintf(p, len, "%s/%s/%s.log", lxcpath, name, name);
581 else
582 ret = snprintf(p, len, "%s/%s.log", lxcpath, name);
583 if (ret < 0 || (size_t)ret >= len) {
584 free(p);
585 return NULL;
586 }
587
588 return p;
589 }
590
591 /*
592 * This can be called:
593 * 1. when a program calls lxc_log_init with no logfile parameter (in which
594 * case the default is used). In this case lxc.loge can override this.
595 * 2. when a program calls lxc_log_init with a logfile parameter. In this
596 * case we don't want lxc.log to override this.
597 * 3. When a lxc.log entry is found in config file.
598 */
599 static int __lxc_log_set_file(const char *fname, int create_dirs)
600 {
601 /* we are overriding the default. */
602 if (lxc_log_fd != -1)
603 lxc_log_close();
604
605 if (!fname)
606 return -1;
607
608 if (strlen(fname) == 0) {
609 log_fname = NULL;
610 return -1;
611 }
612
613 #if USE_CONFIGPATH_LOGS
614 /* We don't build_dir for the default if the default is i.e.
615 * /var/lib/lxc/$container/$container.log.
616 */
617 if (create_dirs)
618 #endif
619 if (build_dir(fname)) {
620 SYSERROR("Failed to create dir for log file \"%s\"", fname);
621 return -1;
622 }
623
624 lxc_log_fd = log_open(fname);
625 if (lxc_log_fd == -1)
626 return -1;
627
628 log_fname = strdup(fname);
629 return 0;
630 }
631
632 static int _lxc_log_set_file(const char *name, const char *lxcpath, int create_dirs)
633 {
634 char *logfile;
635 int ret;
636
637 logfile = build_log_path(name, lxcpath);
638 if (!logfile) {
639 ERROR("Could not build log path");
640 return -1;
641 }
642
643 ret = __lxc_log_set_file(logfile, create_dirs);
644 free(logfile);
645 return ret;
646 }
647
648 /*
649 * lxc_log_init:
650 * Called from lxc front-end programs (like lxc-create, lxc-start) to
651 * initialize the log defaults.
652 */
653 int lxc_log_init(struct lxc_log *log)
654 {
655 int ret;
656 int lxc_priority = LXC_LOG_LEVEL_ERROR;
657
658 if (!log)
659 return -1;
660
661 if (lxc_log_fd != -1) {
662 WARN("Log already initialized");
663 return 0;
664 }
665
666 if (log->level)
667 lxc_priority = lxc_log_priority_to_int(log->level);
668
669 if (!lxc_loglevel_specified) {
670 lxc_log_category_lxc.priority = lxc_priority;
671 lxc_loglevel_specified = 1;
672 }
673
674 if (!lxc_quiet_specified)
675 if (!log->quiet)
676 lxc_log_category_lxc.appender->next = &log_appender_stderr;
677
678 if (log->prefix)
679 lxc_log_set_prefix(log->prefix);
680
681 if (log->name)
682 log_vmname = strdup(log->name);
683
684 if (log->file) {
685 if (strcmp(log->file, "none") == 0)
686 return 0;
687
688 ret = __lxc_log_set_file(log->file, 1);
689 if (ret < 0) {
690 ERROR("Failed to enable logfile");
691 return -1;
692 }
693
694 lxc_log_use_global_fd = 1;
695 } else {
696 /* if no name was specified, there nothing to do */
697 if (!log->name)
698 return 0;
699
700 ret = -1;
701
702 if (!log->lxcpath)
703 log->lxcpath = LOGPATH;
704
705 /* try LOGPATH if lxcpath is the default for the privileged containers */
706 if (!geteuid() && strcmp(LXCPATH, log->lxcpath) == 0)
707 ret = _lxc_log_set_file(log->name, NULL, 0);
708
709 /* try in lxcpath */
710 if (ret < 0)
711 ret = _lxc_log_set_file(log->name, log->lxcpath, 1);
712
713 /* try LOGPATH in case its writable by the caller */
714 if (ret < 0)
715 ret = _lxc_log_set_file(log->name, NULL, 0);
716 }
717
718 /*
719 * If !file, that is, if the user did not request this logpath, then
720 * ignore failures and continue logging to console
721 */
722 if (!log->file && ret != 0) {
723 INFO("Ignoring failure to open default logfile");
724 ret = 0;
725 }
726
727 if (lxc_log_fd != -1) {
728 lxc_log_category_lxc.appender = &log_appender_logfile;
729 lxc_log_category_lxc.appender->next = &log_appender_stderr;
730 }
731
732 return ret;
733 }
734
735 void lxc_log_close(void)
736 {
737 closelog();
738
739 free(log_vmname);
740 log_vmname = NULL;
741
742 if (lxc_log_fd == -1)
743 return;
744
745 close(lxc_log_fd);
746 lxc_log_fd = -1;
747
748 free(log_fname);
749 log_fname = NULL;
750 }
751
752 int lxc_log_syslog(int facility)
753 {
754 struct lxc_log_appender *appender;
755
756 openlog(log_prefix, LOG_PID, facility);
757 if (!lxc_log_category_lxc.appender) {
758 lxc_log_category_lxc.appender = &log_appender_syslog;
759 return 0;
760 }
761
762 appender = lxc_log_category_lxc.appender;
763 /* Check if syslog was already added, to avoid creating a loop */
764 while (appender) {
765 if (appender == &log_appender_syslog) {
766 /* not an error: openlog re-opened the connection */
767 return 0;
768 }
769 appender = appender->next;
770 }
771
772 appender = lxc_log_category_lxc.appender;
773 while (appender->next != NULL)
774 appender = appender->next;
775 appender->next = &log_appender_syslog;
776
777 return 0;
778 }
779
780 inline void lxc_log_enable_syslog(void)
781 {
782 syslog_enable = 1;
783 }
784
785 /*
786 * This is called when we read a lxc.log.level entry in a lxc.conf file. This
787 * happens after processing command line arguments, which override the .conf
788 * settings. So only set the level if previously unset.
789 */
790 int lxc_log_set_level(int *dest, int level)
791 {
792 if (level < 0 || level >= LXC_LOG_LEVEL_NOTSET) {
793 ERROR("Invalid log priority %d", level);
794 return -1;
795 }
796
797 *dest = level;
798 return 0;
799 }
800
801 inline int lxc_log_get_level(void)
802 {
803 return lxc_log_category_lxc.priority;
804 }
805
806 bool lxc_log_has_valid_level(void)
807 {
808 int log_level;
809
810 log_level = lxc_log_get_level();
811 if (log_level < 0 || log_level >= LXC_LOG_LEVEL_NOTSET)
812 return false;
813
814 return true;
815 }
816
817 /*
818 * This is called when we read a lxc.logfile entry in a lxc.conf file. This
819 * happens after processing command line arguments, which override the .conf
820 * settings. So only set the file if previously unset.
821 */
822 int lxc_log_set_file(int *fd, const char *fname)
823 {
824 if (*fd >= 0) {
825 close(*fd);
826 *fd = -1;
827 }
828
829 if (build_dir(fname))
830 return -1;
831
832 *fd = log_open(fname);
833 if (*fd < 0)
834 return -1;
835
836 return 0;
837 }
838
839 inline const char *lxc_log_get_file(void)
840 {
841 return log_fname;
842 }
843
844 inline void lxc_log_set_prefix(const char *prefix)
845 {
846 /* We don't care if the prefix is truncated. */
847 (void)strlcpy(log_prefix, prefix, sizeof(log_prefix));
848 }
849
850 inline const char *lxc_log_get_prefix(void)
851 {
852 return log_prefix;
853 }
854
855 inline void lxc_log_options_no_override()
856 {
857 lxc_quiet_specified = 1;
858 lxc_loglevel_specified = 1;
859 }