This prevents starting any daemons with improper logging settings. This is desirable on its own, but will be even more beneficial when more functions start reporting errors and failing on them, coming up in following patches Signed-off-by: Martin Kletzander <mkletzan@xxxxxxxxxx> --- src/locking/lock_daemon.c | 15 ++++++++------- src/logging/log_daemon.c | 15 ++++++++------- src/remote/remote_daemon.c | 15 ++++++++------- src/util/virdaemon.c | 31 ++++++++++++++++++++----------- src/util/virdaemon.h | 14 +++++++------- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index 107fb22bc2c9..ea81940a4325 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -913,13 +913,14 @@ int main(int argc, char **argv) { } VIR_FREE(remote_config_file); - virDaemonSetupLogging("virtlockd", - config->log_level, - config->log_filters, - config->log_outputs, - privileged, - verbose, - godaemon); + if (virDaemonSetupLogging("virtlockd", + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon) < 0) + exit(EXIT_FAILURE); if (!pid_file && virPidFileConstructPath(privileged, diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index de6bf082e89b..fe7fa8534aec 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -719,13 +719,14 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - virDaemonSetupLogging("virtlogd", - config->log_level, - config->log_filters, - config->log_outputs, - privileged, - verbose, - godaemon); + if (virDaemonSetupLogging("virtlogd", + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon) < 0) + exit(EXIT_FAILURE); if (!pid_file && virPidFileConstructPath(privileged, diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index 4e10f3ad2307..8a4610da83c8 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -936,13 +936,14 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } - virDaemonSetupLogging(DAEMON_NAME, - config->log_level, - config->log_filters, - config->log_outputs, - privileged, - verbose, - godaemon); + if (virDaemonSetupLogging(DAEMON_NAME, + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon) < 0) + exit(EXIT_FAILURE); /* Let's try to initialize global variable that holds the host's boot time. */ if (virHostBootTimeInit() < 0) { diff --git a/src/util/virdaemon.c b/src/util/virdaemon.c index bb2df2eb2cab..795206301227 100644 --- a/src/util/virdaemon.c +++ b/src/util/virdaemon.c @@ -151,7 +151,7 @@ virDaemonForkIntoBackground(const char *argv0) * but if verbose or error debugging is asked for then also output * informational and debug messages. Default size if 64 kB. */ -void +int virDaemonSetupLogging(const char *daemon_name, unsigned int log_level, char *log_filters, @@ -160,7 +160,8 @@ virDaemonSetupLogging(const char *daemon_name, bool verbose, bool godaemon) { - virLogReset(); + if (virLogReset() < 0) + return -1; /* * Libvirtd's order of precedence is: @@ -169,15 +170,17 @@ virDaemonSetupLogging(const char *daemon_name, * Given the precedence, we must process the variables in the opposite * order, each one overriding the previous. */ - if (log_level != 0) - virLogSetDefaultPriority(log_level); + if (log_level != 0 && + virLogSetDefaultPriority(log_level) < 0) + return -1; /* In case the config is empty, both filters and outputs will become empty, * however we can't start with empty outputs, thus we'll need to define and * setup a default one. */ - ignore_value(virLogSetFilters(log_filters)); - ignore_value(virLogSetOutputs(log_outputs)); + if (virLogSetFilters(log_filters) < 0 || + virLogSetOutputs(log_outputs) < 0) + return -1; /* If there are some environment variables defined, use those instead */ virLogSetFromEnv(); @@ -185,16 +188,22 @@ virDaemonSetupLogging(const char *daemon_name, /* * Command line override for --verbose */ - if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO)) - virLogSetDefaultPriority(VIR_LOG_INFO); + if (verbose && + virLogGetDefaultPriority() > VIR_LOG_INFO && + virLogSetDefaultPriority(VIR_LOG_INFO) < 0) + return -1; /* Define the default output. This is only applied if there was no setting * from either the config or the environment. */ - virLogSetDefaultOutput(daemon_name, godaemon, privileged); + if (virLogSetDefaultOutput(daemon_name, godaemon, privileged) < 0) + return -1; + + if (virLogGetNbOutputs() == 0 && + virLogSetOutputs(virLogGetDefaultOutput()) < 0) + return -1; - if (virLogGetNbOutputs() == 0) - virLogSetOutputs(virLogGetDefaultOutput()); + return 0; } diff --git a/src/util/virdaemon.h b/src/util/virdaemon.h index d032b8ddb3b9..9ed0942d6d0e 100644 --- a/src/util/virdaemon.h +++ b/src/util/virdaemon.h @@ -58,13 +58,13 @@ VIR_ENUM_IMPL(virDaemonErr, int virDaemonForkIntoBackground(const char *argv0); -void virDaemonSetupLogging(const char *daemon_name, - unsigned int log_level, - char *log_filters, - char *log_outputs, - bool privileged, - bool verbose, - bool godaemon); +int virDaemonSetupLogging(const char *daemon_name, + unsigned int log_level, + char *log_filters, + char *log_outputs, + bool privileged, + bool verbose, + bool godaemon); int virDaemonUnixSocketPaths(const char *sock_prefix, bool privileged, -- 2.34.1