Unverified Commit 52767e2e by Stéphane Graber Committed by GitHub

Merge pull request #3316 from brauner/2020-03-20/fixes

log: fixes
parents 92956baa 3e92b6f7
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "file_utils.h" #include "file_utils.h"
#include "log.h" #include "log.h"
#include "lxccontainer.h" #include "lxccontainer.h"
#include "memory_utils.h"
#include "utils.h" #include "utils.h"
#ifndef HAVE_STRLCPY #ifndef HAVE_STRLCPY
...@@ -42,7 +43,7 @@ ...@@ -42,7 +43,7 @@
*/ */
#define LXC_LOG_TIME_SIZE ((INTTYPE_TO_STRLEN(uint64_t)) * 2) #define LXC_LOG_TIME_SIZE ((INTTYPE_TO_STRLEN(uint64_t)) * 2)
int lxc_log_fd = -1; int lxc_log_fd = -EBADF;
static int syslog_enable = 0; static int syslog_enable = 0;
int lxc_quiet_specified; int lxc_quiet_specified;
int lxc_log_use_global_fd; int lxc_log_use_global_fd;
...@@ -93,12 +94,12 @@ static const char *lxc_log_get_container_name() ...@@ -93,12 +94,12 @@ static const char *lxc_log_get_container_name()
static char *lxc_log_get_va_msg(struct lxc_log_event *event) static char *lxc_log_get_va_msg(struct lxc_log_event *event)
{ {
char *msg; __do_free char *msg = NULL;
int rc, len; int rc, len;
va_list args; va_list args;
if (!event) if (!event)
return NULL; return ret_set_errno(NULL, EINVAL);
va_copy(args, *event->vap); va_copy(args, *event->vap);
#pragma GCC diagnostic push #pragma GCC diagnostic push
...@@ -109,25 +110,22 @@ static char *lxc_log_get_va_msg(struct lxc_log_event *event) ...@@ -109,25 +110,22 @@ static char *lxc_log_get_va_msg(struct lxc_log_event *event)
msg = malloc(len * sizeof(char)); msg = malloc(len * sizeof(char));
if (!msg) if (!msg)
return NULL; return ret_set_errno(NULL, ENOMEM);
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral" #pragma GCC diagnostic ignored "-Wformat-nonliteral"
rc = vsnprintf(msg, len, event->fmt, *event->vap); rc = vsnprintf(msg, len, event->fmt, *event->vap);
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
if (rc == -1 || rc >= len) { if (rc < 0 || rc >= len)
free(msg); return ret_set_errno(NULL, EIO);
return NULL;
}
return msg; return move_ptr(msg);
} }
/*---------------------------------------------------------------------------*/
static int log_append_syslog(const struct lxc_log_appender *appender, static int log_append_syslog(const struct lxc_log_appender *appender,
struct lxc_log_event *event) struct lxc_log_event *event)
{ {
char *msg; __do_free char *msg = NULL;
const char *log_container_name; const char *log_container_name;
if (!syslog_enable) if (!syslog_enable)
...@@ -147,12 +145,10 @@ static int log_append_syslog(const struct lxc_log_appender *appender, ...@@ -147,12 +145,10 @@ static int log_append_syslog(const struct lxc_log_appender *appender,
event->locinfo->file, event->locinfo->func, event->locinfo->file, event->locinfo->func,
event->locinfo->line, event->locinfo->line,
msg); msg);
free(msg);
return 0; return 0;
} }
/*---------------------------------------------------------------------------*/
static int log_append_stderr(const struct lxc_log_appender *appender, static int log_append_stderr(const struct lxc_log_appender *appender,
struct lxc_log_event *event) struct lxc_log_event *event)
{ {
...@@ -177,7 +173,6 @@ static int log_append_stderr(const struct lxc_log_appender *appender, ...@@ -177,7 +173,6 @@ static int log_append_stderr(const struct lxc_log_appender *appender,
return 0; return 0;
} }
/*---------------------------------------------------------------------------*/
static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespec *time) static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespec *time)
{ {
int64_t epoch_to_days, z, era, doe, yoe, year, doy, mp, day, month, int64_t epoch_to_days, z, era, doe, yoe, year, doy, mp, day, month,
...@@ -185,7 +180,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -185,7 +180,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
char nanosec[INTTYPE_TO_STRLEN(int64_t)]; char nanosec[INTTYPE_TO_STRLEN(int64_t)];
int ret; int ret;
/* See https://howardhinnant.github.io/date_algorithms.html for an /*
* See https://howardhinnant.github.io/date_algorithms.html for an
* explanation of the algorithm used here. * explanation of the algorithm used here.
*/ */
...@@ -195,17 +191,20 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -195,17 +191,20 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
/* Shift the Epoch from 1970-01-01 to 0000-03-01. */ /* Shift the Epoch from 1970-01-01 to 0000-03-01. */
z = epoch_to_days + 719468; z = epoch_to_days + 719468;
/* compute the era from the serial date by simply dividing by the number /*
* Compute the era from the serial date by simply dividing by the number
* of days in an era (146097). * of days in an era (146097).
*/ */
era = (z >= 0 ? z : z - 146096) / 146097; era = (z >= 0 ? z : z - 146096) / 146097;
/* The day-of-era (doe) can then be found by subtracting the era number /*
* The day-of-era (doe) can then be found by subtracting the era number
* times the number of days per era, from the serial date. * times the number of days per era, from the serial date.
*/ */
doe = (z - era * 146097); doe = (z - era * 146097);
/* From the day-of-era (doe), the year-of-era (yoe, range [0, 399]) can /*
* From the day-of-era (doe), the year-of-era (yoe, range [0, 399]) can
* be computed. * be computed.
*/ */
yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
...@@ -213,7 +212,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -213,7 +212,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
/* Given year-of-era, and era, one can now compute the year. */ /* Given year-of-era, and era, one can now compute the year. */
year = yoe + era * 400; year = yoe + era * 400;
/* Also the day-of-year, again with the year beginning on Mar. 1, can be /*
* Also the day-of-year, again with the year beginning on Mar. 1, can be
* computed from the day-of-era and year-of-era. * computed from the day-of-era and year-of-era.
*/ */
doy = doe - (365 * yoe + yoe / 4 - yoe / 100); doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
...@@ -221,25 +221,30 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -221,25 +221,30 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
/* Given day-of-year, find the month number. */ /* Given day-of-year, find the month number. */
mp = (5 * doy + 2) / 153; mp = (5 * doy + 2) / 153;
/* From day-of-year and month-of-year we can now easily compute /*
* From day-of-year and month-of-year we can now easily compute
* day-of-month. * day-of-month.
*/ */
day = doy - (153 * mp + 2) / 5 + 1; day = doy - (153 * mp + 2) / 5 + 1;
/* Transform the month number from the [0, 11] / [Mar, Feb] system to /*
* Transform the month number from the [0, 11] / [Mar, Feb] system to
* the civil system: [1, 12] to find the correct month. * the civil system: [1, 12] to find the correct month.
*/ */
month = mp + (mp < 10 ? 3 : -9); month = mp + (mp < 10 ? 3 : -9);
/* The algorithm assumes that a year begins on 1 March, so add 1 before /*
* that. */ * The algorithm assumes that a year begins on 1 March, so add 1 before
* that.
*/
if (month < 3) if (month < 3)
year++; year++;
/* Transform days in the epoch to seconds. */ /* Transform days in the epoch to seconds. */
d_in_s = epoch_to_days * 86400; d_in_s = epoch_to_days * 86400;
/* To find the current hour simply substract the Epoch_to_days from the /*
* To find the current hour simply substract the Epoch_to_days from the
* total Epoch and divide by the number of seconds in an hour. * total Epoch and divide by the number of seconds in an hour.
*/ */
hours = (time->tv_sec - d_in_s) / 3600; hours = (time->tv_sec - d_in_s) / 3600;
...@@ -247,23 +252,26 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -247,23 +252,26 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
/* Transform hours to seconds. */ /* Transform hours to seconds. */
h_in_s = hours * 3600; h_in_s = hours * 3600;
/* Calculate minutes by subtracting the seconds for all days in the /*
* Calculate minutes by subtracting the seconds for all days in the
* epoch and for all hours in the epoch and divide by the number of * epoch and for all hours in the epoch and divide by the number of
* minutes in an hour. * minutes in an hour.
*/ */
minutes = (time->tv_sec - d_in_s - h_in_s) / 60; minutes = (time->tv_sec - d_in_s - h_in_s) / 60;
/* Calculate the seconds by subtracting the seconds for all days in the /*
* Calculate the seconds by subtracting the seconds for all days in the
* epoch, hours in the epoch and minutes in the epoch. * epoch, hours in the epoch and minutes in the epoch.
*/ */
seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60)); seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60));
/* Make string from nanoseconds. */ /* Make string from nanoseconds. */
ret = snprintf(nanosec, sizeof(nanosec), "%"PRId64, (int64_t)time->tv_nsec); ret = snprintf(nanosec, sizeof(nanosec), "%"PRId64, (int64_t)time->tv_nsec);
if (ret < 0 || ret >= sizeof(nanosec)) if (ret < 0 || (size_t)ret >= sizeof(nanosec))
return -1; return ret_errno(EIO);
/* Create final timestamp for the log and shorten nanoseconds to 3 /*
* Create final timestamp for the log and shorten nanoseconds to 3
* digit precision. * digit precision.
*/ */
ret = snprintf(buf, bufsize, ret = snprintf(buf, bufsize,
...@@ -271,12 +279,13 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -271,12 +279,13 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
"%02" PRId64 "%02" PRId64 ".%.3s", "%02" PRId64 "%02" PRId64 ".%.3s",
year, month, day, hours, minutes, seconds, nanosec); year, month, day, hours, minutes, seconds, nanosec);
if (ret < 0 || (size_t)ret >= bufsize) if (ret < 0 || (size_t)ret >= bufsize)
return -1; return ret_errno(EIO);
return 0; return 0;
} }
/* This function needs to make extra sure that it is thread-safe. We had some /*
* This function needs to make extra sure that it is thread-safe. We had some
* problems with that before. This especially involves time-conversion * problems with that before. This especially involves time-conversion
* functions. I don't want to find any localtime() or gmtime() functions or * functions. I don't want to find any localtime() or gmtime() functions or
* relatives in here. Not even localtime_r() or gmtime_r() or relatives. They * relatives in here. Not even localtime_r() or gmtime_r() or relatives. They
...@@ -299,29 +308,29 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe ...@@ -299,29 +308,29 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
static int log_append_logfile(const struct lxc_log_appender *appender, static int log_append_logfile(const struct lxc_log_appender *appender,
struct lxc_log_event *event) struct lxc_log_event *event)
{ {
int fd_to_use = -EBADF;
char buffer[LXC_LOG_BUFFER_SIZE]; char buffer[LXC_LOG_BUFFER_SIZE];
char date_time[LXC_LOG_TIME_SIZE]; char date_time[LXC_LOG_TIME_SIZE];
int n; int n;
ssize_t ret; ssize_t ret;
int fd_to_use = -1;
const char *log_container_name; const char *log_container_name;
#ifndef NO_LXC_CONF #ifndef NO_LXC_CONF
if (current_config) if (current_config && !lxc_log_use_global_fd)
if (!lxc_log_use_global_fd) fd_to_use = current_config->logfd;
fd_to_use = current_config->logfd;
#endif #endif
log_container_name = lxc_log_get_container_name(); log_container_name = lxc_log_get_container_name();
if (fd_to_use == -1) if (fd_to_use < 0)
fd_to_use = lxc_log_fd; fd_to_use = lxc_log_fd;
if (fd_to_use == -1) if (fd_to_use < 0)
return 0; return 0;
if (lxc_unix_epoch_to_utc(date_time, LXC_LOG_TIME_SIZE, &event->timestamp) < 0) ret = lxc_unix_epoch_to_utc(date_time, LXC_LOG_TIME_SIZE, &event->timestamp);
return -1; if (ret)
return ret;
n = snprintf(buffer, sizeof(buffer), n = snprintf(buffer, sizeof(buffer),
"%s%s%s %s %-8s %s - %s:%s:%d - ", "%s%s%s %s %-8s %s - %s:%s:%d - ",
...@@ -334,7 +343,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -334,7 +343,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
event->locinfo->file, event->locinfo->func, event->locinfo->file, event->locinfo->func,
event->locinfo->line); event->locinfo->line);
if (n < 0) if (n < 0)
return n; return ret_errno(EIO);
if ((size_t)n < STRARRAYLEN(buffer)) { if ((size_t)n < STRARRAYLEN(buffer)) {
#pragma GCC diagnostic push #pragma GCC diagnostic push
...@@ -359,8 +368,11 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -359,8 +368,11 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
static int log_append_dlog(const struct lxc_log_appender *appender, static int log_append_dlog(const struct lxc_log_appender *appender,
struct lxc_log_event *event) struct lxc_log_event *event)
{ {
char *msg = lxc_log_get_va_msg(event); __do_free char *msg = NULL;
const char *log_container_name = lxc_log_get_container_name(); const char *log_container_name;
log_container_name = lxc_log_get_container_name();
msg = lxc_log_get_va_msg(event);
switch (event->priority) { switch (event->priority) {
case LXC_LOG_LEVEL_TRACE: case LXC_LOG_LEVEL_TRACE:
...@@ -401,7 +413,6 @@ static int log_append_dlog(const struct lxc_log_appender *appender, ...@@ -401,7 +413,6 @@ static int log_append_dlog(const struct lxc_log_appender *appender,
break; break;
} }
free(msg);
return 0; return 0;
} }
#endif #endif
...@@ -455,15 +466,15 @@ struct lxc_log_category lxc_log_category_lxc = { ...@@ -455,15 +466,15 @@ struct lxc_log_category lxc_log_category_lxc = {
}; };
#endif #endif
/*---------------------------------------------------------------------------*/
static int build_dir(const char *name) static int build_dir(const char *name)
{ {
char *e, *n, *p; __do_free char *n = NULL;
char *e, *p;
/* Make copy of the string since we'll be modifying it. */ /* Make copy of the string since we'll be modifying it. */
n = strdup(name); n = strdup(name);
if (!n) if (!n)
return -1; return ret_errno(ENOMEM);
e = &n[strlen(n)]; e = &n[strlen(n)];
for (p = n + 1; p < e; p++) { for (p = n + 1; p < e; p++) {
...@@ -474,39 +485,31 @@ static int build_dir(const char *name) ...@@ -474,39 +485,31 @@ static int build_dir(const char *name)
*p = '\0'; *p = '\0';
ret = lxc_unpriv(mkdir(n, 0755)); ret = lxc_unpriv(mkdir(n, 0755));
if (ret && errno != EEXIST) { if (ret && errno != EEXIST)
SYSERROR("Failed to create directory \"%s\"", n); return log_error_errno(-errno, errno, "Failed to create directory \"%s\"", n);
free(n);
return -1;
}
*p = '/'; *p = '/';
} }
free(n);
return 0; return 0;
} }
/*---------------------------------------------------------------------------*/
static int log_open(const char *name) static int log_open(const char *name)
{ {
int fd; __do_close int fd = -EBADF;
int newfd; int newfd;
fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660)); fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660));
if (fd < 0) { if (fd < 0)
SYSERROR("Failed to open log file \"%s\"", name); return log_error_errno(-errno, errno, "Failed to open log file \"%s\"", name);
return -1;
}
if (fd > 2) if (fd > 2)
return fd; return move_fd(fd);
newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO); newfd = fcntl(fd, F_DUPFD_CLOEXEC, STDERR_FILENO);
if (newfd == -1) if (newfd < 0)
SYSERROR("Failed to dup log fd %d", fd); return log_error_errno(-errno, errno, "Failed to dup log fd %d", fd);
close(fd);
return newfd; return newfd;
} }
...@@ -518,13 +521,13 @@ static int log_open(const char *name) ...@@ -518,13 +521,13 @@ static int log_open(const char *name)
*/ */
static char *build_log_path(const char *name, const char *lxcpath) static char *build_log_path(const char *name, const char *lxcpath)
{ {
char *p; __do_free char *p = NULL;
int ret; int ret;
size_t len; size_t len;
bool use_dir; bool use_dir;
if (!name) if (!name)
return NULL; return ret_set_errno(NULL, EINVAL);
#if USE_CONFIGPATH_LOGS #if USE_CONFIGPATH_LOGS
use_dir = true; use_dir = true;
...@@ -553,18 +556,16 @@ static char *build_log_path(const char *name, const char *lxcpath) ...@@ -553,18 +556,16 @@ static char *build_log_path(const char *name, const char *lxcpath)
p = malloc(len); p = malloc(len);
if (!p) if (!p)
return p; return ret_set_errno(NULL, ENOMEM);
if (use_dir) if (use_dir)
ret = snprintf(p, len, "%s/%s/%s.log", lxcpath, name, name); ret = snprintf(p, len, "%s/%s/%s.log", lxcpath, name, name);
else else
ret = snprintf(p, len, "%s/%s.log", lxcpath, name); ret = snprintf(p, len, "%s/%s.log", lxcpath, name);
if (ret < 0 || (size_t)ret >= len) { if (ret < 0 || (size_t)ret >= len)
free(p); return ret_set_errno(NULL, EIO);
return NULL;
}
return p; return move_ptr(p);
} }
/* /*
...@@ -578,15 +579,15 @@ static char *build_log_path(const char *name, const char *lxcpath) ...@@ -578,15 +579,15 @@ static char *build_log_path(const char *name, const char *lxcpath)
static int __lxc_log_set_file(const char *fname, int create_dirs) static int __lxc_log_set_file(const char *fname, int create_dirs)
{ {
/* we are overriding the default. */ /* we are overriding the default. */
if (lxc_log_fd != -1) if (lxc_log_fd >= 0)
lxc_log_close(); lxc_log_close();
if (!fname) if (!fname)
return -1; return ret_errno(EINVAL);
if (strlen(fname) == 0) { if (strlen(fname) == 0) {
log_fname = NULL; log_fname = NULL;
return -1; return ret_errno(EINVAL);
} }
#if USE_CONFIGPATH_LOGS #if USE_CONFIGPATH_LOGS
...@@ -595,14 +596,12 @@ static int __lxc_log_set_file(const char *fname, int create_dirs) ...@@ -595,14 +596,12 @@ static int __lxc_log_set_file(const char *fname, int create_dirs)
*/ */
if (create_dirs) if (create_dirs)
#endif #endif
if (build_dir(fname)) { if (build_dir(fname))
SYSERROR("Failed to create dir for log file \"%s\"", fname); return log_error_errno(-errno, errno, "Failed to create dir for log file \"%s\"", fname);
return -1;
}
lxc_log_fd = log_open(fname); lxc_log_fd = log_open(fname);
if (lxc_log_fd == -1) if (lxc_log_fd < 0)
return -1; return lxc_log_fd;
log_fname = strdup(fname); log_fname = strdup(fname);
return 0; return 0;
...@@ -610,18 +609,13 @@ static int __lxc_log_set_file(const char *fname, int create_dirs) ...@@ -610,18 +609,13 @@ static int __lxc_log_set_file(const char *fname, int create_dirs)
static int _lxc_log_set_file(const char *name, const char *lxcpath, int create_dirs) static int _lxc_log_set_file(const char *name, const char *lxcpath, int create_dirs)
{ {
char *logfile; __do_free char *logfile = NULL;
int ret;
logfile = build_log_path(name, lxcpath); logfile = build_log_path(name, lxcpath);
if (!logfile) { if (!logfile)
ERROR("Could not build log path"); return log_error_errno(-errno, errno, "Could not build log path");
return -1;
}
ret = __lxc_log_set_file(logfile, create_dirs); return __lxc_log_set_file(logfile, create_dirs);
free(logfile);
return ret;
} }
/* /*
...@@ -635,12 +629,10 @@ int lxc_log_init(struct lxc_log *log) ...@@ -635,12 +629,10 @@ int lxc_log_init(struct lxc_log *log)
int lxc_priority = LXC_LOG_LEVEL_ERROR; int lxc_priority = LXC_LOG_LEVEL_ERROR;
if (!log) if (!log)
return -1; return ret_errno(EINVAL);
if (lxc_log_fd != -1) { if (lxc_log_fd >= 0)
WARN("Log already initialized"); return log_warn_errno(0, EOPNOTSUPP, "Log already initialized");
return 0;
}
if (log->level) if (log->level)
lxc_priority = lxc_log_priority_to_int(log->level); lxc_priority = lxc_log_priority_to_int(log->level);
...@@ -665,10 +657,8 @@ int lxc_log_init(struct lxc_log *log) ...@@ -665,10 +657,8 @@ int lxc_log_init(struct lxc_log *log)
return 0; return 0;
ret = __lxc_log_set_file(log->file, 1); ret = __lxc_log_set_file(log->file, 1);
if (ret < 0) { if (ret < 0)
ERROR("Failed to enable logfile"); return log_error_errno(-1, errno, "Failed to enable logfile");
return -1;
}
lxc_log_use_global_fd = 1; lxc_log_use_global_fd = 1;
} else { } else {
...@@ -703,7 +693,7 @@ int lxc_log_init(struct lxc_log *log) ...@@ -703,7 +693,7 @@ int lxc_log_init(struct lxc_log *log)
ret = 0; ret = 0;
} }
if (lxc_log_fd != -1) { if (lxc_log_fd >= 0) {
lxc_log_category_lxc.appender = &log_appender_logfile; lxc_log_category_lxc.appender = &log_appender_logfile;
lxc_log_category_lxc.appender->next = &log_appender_stderr; lxc_log_category_lxc.appender->next = &log_appender_stderr;
} }
...@@ -715,17 +705,11 @@ void lxc_log_close(void) ...@@ -715,17 +705,11 @@ void lxc_log_close(void)
{ {
closelog(); closelog();
free(log_vmname); free_disarm(log_vmname);
log_vmname = NULL;
if (lxc_log_fd == -1)
return;
close(lxc_log_fd); close_prot_errno_disarm(lxc_log_fd);
lxc_log_fd = -1;
free(log_fname); free_disarm(log_fname);
log_fname = NULL;
} }
int lxc_log_syslog(int facility) int lxc_log_syslog(int facility)
...@@ -741,10 +725,9 @@ int lxc_log_syslog(int facility) ...@@ -741,10 +725,9 @@ int lxc_log_syslog(int facility)
appender = lxc_log_category_lxc.appender; appender = lxc_log_category_lxc.appender;
/* Check if syslog was already added, to avoid creating a loop */ /* Check if syslog was already added, to avoid creating a loop */
while (appender) { while (appender) {
if (appender == &log_appender_syslog) { /* not an error: openlog re-opened the connection */
/* not an error: openlog re-opened the connection */ if (appender == &log_appender_syslog)
return 0; return 0;
}
appender = appender->next; appender = appender->next;
} }
...@@ -768,10 +751,8 @@ inline void lxc_log_enable_syslog(void) ...@@ -768,10 +751,8 @@ inline void lxc_log_enable_syslog(void)
*/ */
int lxc_log_set_level(int *dest, int level) int lxc_log_set_level(int *dest, int level)
{ {
if (level < 0 || level >= LXC_LOG_LEVEL_NOTSET) { if (level < 0 || level >= LXC_LOG_LEVEL_NOTSET)
ERROR("Invalid log priority %d", level); return log_error_errno(-EINVAL, EINVAL, "Invalid log priority %d", level);
return -1;
}
*dest = level; *dest = level;
return 0; return 0;
...@@ -788,7 +769,7 @@ bool lxc_log_has_valid_level(void) ...@@ -788,7 +769,7 @@ bool lxc_log_has_valid_level(void)
log_level = lxc_log_get_level(); log_level = lxc_log_get_level();
if (log_level < 0 || log_level >= LXC_LOG_LEVEL_NOTSET) if (log_level < 0 || log_level >= LXC_LOG_LEVEL_NOTSET)
return false; return ret_set_errno(false, EINVAL);
return true; return true;
} }
...@@ -800,17 +781,15 @@ bool lxc_log_has_valid_level(void) ...@@ -800,17 +781,15 @@ bool lxc_log_has_valid_level(void)
*/ */
int lxc_log_set_file(int *fd, const char *fname) int lxc_log_set_file(int *fd, const char *fname)
{ {
if (*fd >= 0) { if (*fd >= 0)
close(*fd); close_prot_errno_disarm(*fd);
*fd = -1;
}
if (build_dir(fname)) if (build_dir(fname))
return -1; return -errno;
*fd = log_open(fname); *fd = log_open(fname);
if (*fd < 0) if (*fd < 0)
return -1; return -errno;
return 0; return 0;
} }
......
...@@ -464,7 +464,7 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ...@@ -464,7 +464,7 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
do { \ do { \
lxc_log_strerror_r; \ lxc_log_strerror_r; \
fprintf(stderr, "%s - %s: %d: %s: " format "\n", __FILE__, \ fprintf(stderr, "%s - %s: %d: %s: " format "\n", __FILE__, \
__LINE__, __func__, ##__VA_ARGS__); \ __LINE__, __func__, ptr, ##__VA_ARGS__); \
} while (0) } while (0)
#endif #endif
...@@ -472,10 +472,11 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \ ...@@ -472,10 +472,11 @@ __lxc_unused static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
#define CMD_SYSINFO(format, ...) \ #define CMD_SYSINFO(format, ...) \
printf("%m - " format, ##__VA_ARGS__) printf("%m - " format, ##__VA_ARGS__)
#else #else
#define CMD_SYSINFO(format, ...) \ #define CMD_SYSINFO(format, ...) \
do { \ do { \
lxc_log_strerror_r; \ lxc_log_strerror_r; \
printf("%s - " format, ptr, ##__VA_ARGS__); \ prinft("%s - %s: %d: %s: " format "\n", __FILE__, __LINE__, \
__func__, ptr, ##__VA_ARGS__); \
} while (0) } while (0)
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment