2015-02-09 16:35:05 +00:00
|
|
|
/*
|
|
|
|
* log_daemon.c: log management daemon
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2015 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "log_daemon.h"
|
|
|
|
#include "log_daemon_config.h"
|
2018-01-19 14:54:00 +00:00
|
|
|
#include "admin/admin_server_dispatch.h"
|
2015-02-09 16:35:05 +00:00
|
|
|
#include "virutil.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virpidfile.h"
|
|
|
|
#include "virprocess.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virconf.h"
|
|
|
|
#include "rpc/virnetdaemon.h"
|
|
|
|
#include "virrandom.h"
|
|
|
|
#include "virhash.h"
|
|
|
|
#include "viruuid.h"
|
|
|
|
#include "virstring.h"
|
2016-04-12 22:29:52 +00:00
|
|
|
#include "virgettext.h"
|
2019-04-01 10:14:26 +00:00
|
|
|
#include "virenum.h"
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
#include "log_daemon_dispatch.h"
|
|
|
|
#include "log_protocol.h"
|
|
|
|
|
|
|
|
#include "configmake.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_LOGGING
|
|
|
|
|
|
|
|
VIR_LOG_INIT("logging.log_daemon");
|
|
|
|
|
|
|
|
struct _virLogDaemon {
|
|
|
|
virMutex lock;
|
|
|
|
virNetDaemonPtr dmn;
|
2015-11-03 11:01:21 +00:00
|
|
|
virLogHandlerPtr handler;
|
2015-02-09 16:35:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virLogDaemonPtr logDaemon = NULL;
|
|
|
|
|
|
|
|
static bool execRestart;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
VIR_LOG_DAEMON_ERR_NONE = 0,
|
|
|
|
VIR_LOG_DAEMON_ERR_PIDFILE,
|
|
|
|
VIR_LOG_DAEMON_ERR_RUNDIR,
|
|
|
|
VIR_LOG_DAEMON_ERR_INIT,
|
|
|
|
VIR_LOG_DAEMON_ERR_SIGNAL,
|
|
|
|
VIR_LOG_DAEMON_ERR_PRIVS,
|
|
|
|
VIR_LOG_DAEMON_ERR_NETWORK,
|
|
|
|
VIR_LOG_DAEMON_ERR_CONFIG,
|
|
|
|
VIR_LOG_DAEMON_ERR_HOOKS,
|
|
|
|
VIR_LOG_DAEMON_ERR_REEXEC,
|
|
|
|
|
|
|
|
VIR_LOG_DAEMON_ERR_LAST
|
|
|
|
};
|
|
|
|
|
2019-01-20 16:04:56 +00:00
|
|
|
VIR_ENUM_DECL(virDaemonErr);
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virDaemonErr,
|
|
|
|
VIR_LOG_DAEMON_ERR_LAST,
|
2015-02-09 16:35:05 +00:00
|
|
|
"Initialization successful",
|
|
|
|
"Unable to obtain pidfile",
|
|
|
|
"Unable to create rundir",
|
|
|
|
"Unable to initialize log daemon",
|
|
|
|
"Unable to setup signal handlers",
|
|
|
|
"Unable to drop privileges",
|
|
|
|
"Unable to initialize network sockets",
|
|
|
|
"Unable to load configuration file",
|
|
|
|
"Unable to look for hook scripts",
|
2019-01-20 16:30:15 +00:00
|
|
|
"Unable to re-execute daemon",
|
|
|
|
);
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
static void *
|
|
|
|
virLogDaemonClientNew(virNetServerClientPtr client,
|
|
|
|
void *opaque);
|
|
|
|
static void
|
|
|
|
virLogDaemonClientFree(void *opaque);
|
|
|
|
|
|
|
|
static void *
|
|
|
|
virLogDaemonClientNewPostExecRestart(virNetServerClientPtr client,
|
|
|
|
virJSONValuePtr object,
|
|
|
|
void *opaque);
|
|
|
|
static virJSONValuePtr
|
|
|
|
virLogDaemonClientPreExecRestart(virNetServerClientPtr client,
|
|
|
|
void *opaque);
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLogDaemonFree(virLogDaemonPtr logd)
|
|
|
|
{
|
|
|
|
if (!logd)
|
|
|
|
return;
|
|
|
|
|
2015-11-03 11:01:21 +00:00
|
|
|
virObjectUnref(logd->handler);
|
2015-02-09 16:35:05 +00:00
|
|
|
virMutexDestroy(&logd->lock);
|
|
|
|
virObjectUnref(logd->dmn);
|
|
|
|
|
|
|
|
VIR_FREE(logd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-24 11:47:41 +00:00
|
|
|
static void
|
|
|
|
virLogDaemonInhibitor(bool inhibit, void *opaque)
|
|
|
|
{
|
2015-11-27 09:26:46 +00:00
|
|
|
virLogDaemonPtr dmn = opaque;
|
2015-11-24 11:47:41 +00:00
|
|
|
|
2018-04-26 10:17:03 +00:00
|
|
|
/* virtlogd uses inhibition only to stop session daemon being killed after
|
|
|
|
* the specified timeout, for the system daemon this is taken care of by
|
|
|
|
* libvirtd and the dependencies between the services. */
|
|
|
|
if (virNetDaemonIsPrivileged(dmn->dmn))
|
|
|
|
return;
|
|
|
|
|
2015-11-24 11:47:41 +00:00
|
|
|
if (inhibit)
|
2015-11-27 09:26:46 +00:00
|
|
|
virNetDaemonAddShutdownInhibition(dmn->dmn);
|
2015-11-24 11:47:41 +00:00
|
|
|
else
|
2015-11-27 09:26:46 +00:00
|
|
|
virNetDaemonRemoveShutdownInhibition(dmn->dmn);
|
2015-11-24 11:47:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
static virLogDaemonPtr
|
|
|
|
virLogDaemonNew(virLogDaemonConfigPtr config, bool privileged)
|
|
|
|
{
|
|
|
|
virLogDaemonPtr logd;
|
2018-01-19 14:54:00 +00:00
|
|
|
virNetServerPtr srv = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
if (VIR_ALLOC(logd) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virMutexInit(&logd->lock) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unable to initialize mutex"));
|
|
|
|
VIR_FREE(logd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:54:00 +00:00
|
|
|
if (!(logd->dmn = virNetDaemonNew()))
|
|
|
|
goto error;
|
|
|
|
|
2017-11-14 13:32:16 +00:00
|
|
|
if (!(srv = virNetServerNew("virtlogd", 1,
|
2018-03-06 17:12:20 +00:00
|
|
|
0, 0, 0, config->max_clients,
|
2017-11-14 13:32:16 +00:00
|
|
|
config->max_clients, -1, 0,
|
|
|
|
virLogDaemonClientNew,
|
|
|
|
virLogDaemonClientPreExecRestart,
|
|
|
|
virLogDaemonClientFree,
|
|
|
|
(void*)(intptr_t)(privileged ? 0x1 : 0x0))))
|
2015-02-09 16:35:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2018-01-19 14:54:00 +00:00
|
|
|
if (virNetDaemonAddServer(logd->dmn, srv) < 0)
|
|
|
|
goto error;
|
|
|
|
virObjectUnref(srv);
|
|
|
|
srv = NULL;
|
|
|
|
|
|
|
|
if (!(srv = virNetServerNew("admin", 1,
|
2018-03-06 17:12:20 +00:00
|
|
|
0, 0, 0, config->admin_max_clients,
|
2018-01-19 14:54:00 +00:00
|
|
|
config->admin_max_clients, -1, 0,
|
|
|
|
remoteAdmClientNew,
|
|
|
|
remoteAdmClientPreExecRestart,
|
|
|
|
remoteAdmClientFree,
|
|
|
|
logd->dmn)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (virNetDaemonAddServer(logd->dmn, srv) < 0)
|
2015-02-09 16:35:05 +00:00
|
|
|
goto error;
|
2017-11-14 13:32:16 +00:00
|
|
|
virObjectUnref(srv);
|
|
|
|
srv = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
|
2015-11-24 11:47:41 +00:00
|
|
|
if (!(logd->handler = virLogHandlerNew(privileged,
|
2016-07-01 16:40:55 +00:00
|
|
|
config->max_size,
|
|
|
|
config->max_backups,
|
2015-11-24 11:47:41 +00:00
|
|
|
virLogDaemonInhibitor,
|
|
|
|
logd)))
|
2015-11-03 11:01:21 +00:00
|
|
|
goto error;
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
return logd;
|
|
|
|
|
|
|
|
error:
|
2017-11-14 13:32:16 +00:00
|
|
|
virObjectUnref(srv);
|
2015-02-09 16:35:05 +00:00
|
|
|
virLogDaemonFree(logd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-03 11:01:21 +00:00
|
|
|
virLogHandlerPtr
|
2015-11-27 09:26:46 +00:00
|
|
|
virLogDaemonGetHandler(virLogDaemonPtr dmn)
|
2015-11-03 11:01:21 +00:00
|
|
|
{
|
2015-11-27 09:26:46 +00:00
|
|
|
return dmn->handler;
|
2015-11-03 11:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-22 17:38:55 +00:00
|
|
|
static virNetServerPtr
|
2018-01-19 14:54:00 +00:00
|
|
|
virLogDaemonNewServerPostExecRestart(virNetDaemonPtr dmn,
|
2018-01-22 17:38:55 +00:00
|
|
|
const char *name,
|
|
|
|
virJSONValuePtr object,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
if (STREQ(name, "virtlogd")) {
|
|
|
|
return virNetServerNewPostExecRestart(object,
|
|
|
|
name,
|
|
|
|
virLogDaemonClientNew,
|
|
|
|
virLogDaemonClientNewPostExecRestart,
|
|
|
|
virLogDaemonClientPreExecRestart,
|
|
|
|
virLogDaemonClientFree,
|
|
|
|
opaque);
|
2018-01-19 14:54:00 +00:00
|
|
|
} else if (STREQ(name, "admin")) {
|
|
|
|
return virNetServerNewPostExecRestart(object,
|
|
|
|
name,
|
|
|
|
remoteAdmClientNew,
|
|
|
|
remoteAdmClientNewPostExecRestart,
|
|
|
|
remoteAdmClientPreExecRestart,
|
|
|
|
remoteAdmClientFree,
|
|
|
|
dmn);
|
2018-01-22 17:38:55 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unexpected server name '%s' during restart"),
|
|
|
|
name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
static virLogDaemonPtr
|
2016-07-01 16:40:55 +00:00
|
|
|
virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged,
|
|
|
|
virLogDaemonConfigPtr config)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
virLogDaemonPtr logd;
|
|
|
|
virJSONValuePtr child;
|
2018-01-22 17:38:55 +00:00
|
|
|
const char *serverNames[] = { "virtlogd" };
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
if (VIR_ALLOC(logd) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virMutexInit(&logd->lock) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unable to initialize mutex"));
|
|
|
|
VIR_FREE(logd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(child = virJSONValueObjectGet(object, "daemon"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Malformed daemon data from JSON file"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2018-01-22 17:38:55 +00:00
|
|
|
if (!(logd->dmn = virNetDaemonNewPostExecRestart(child,
|
2019-10-15 11:55:26 +00:00
|
|
|
G_N_ELEMENTS(serverNames),
|
2018-01-22 17:38:55 +00:00
|
|
|
serverNames,
|
|
|
|
virLogDaemonNewServerPostExecRestart,
|
|
|
|
(void*)(intptr_t)(privileged ? 0x1 : 0x0))))
|
2015-02-09 16:35:05 +00:00
|
|
|
goto error;
|
|
|
|
|
2015-11-03 11:01:21 +00:00
|
|
|
if (!(child = virJSONValueObjectGet(object, "handler"))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Malformed daemon data from JSON file"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(logd->handler = virLogHandlerNewPostExecRestart(child,
|
2015-11-24 11:47:41 +00:00
|
|
|
privileged,
|
2016-07-01 16:40:55 +00:00
|
|
|
config->max_size,
|
|
|
|
config->max_backups,
|
2015-11-24 11:47:41 +00:00
|
|
|
virLogDaemonInhibitor,
|
|
|
|
logd)))
|
2015-11-03 11:01:21 +00:00
|
|
|
goto error;
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
return logd;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virLogDaemonFree(logd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonForkIntoBackground(const char *argv0)
|
|
|
|
{
|
|
|
|
int statuspipe[2];
|
2020-01-24 15:22:12 +00:00
|
|
|
if (virPipeQuiet(statuspipe) < 0)
|
2015-02-09 16:35:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
switch (pid) {
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
int stdinfd = -1;
|
|
|
|
int stdoutfd = -1;
|
|
|
|
int nextpid;
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(statuspipe[0]);
|
|
|
|
|
|
|
|
if ((stdinfd = open("/dev/null", O_RDONLY)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if ((stdoutfd = open("/dev/null", O_WRONLY)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (dup2(stdinfd, STDIN_FILENO) != STDIN_FILENO)
|
|
|
|
goto cleanup;
|
|
|
|
if (dup2(stdoutfd, STDOUT_FILENO) != STDOUT_FILENO)
|
|
|
|
goto cleanup;
|
|
|
|
if (dup2(stdoutfd, STDERR_FILENO) != STDERR_FILENO)
|
|
|
|
goto cleanup;
|
|
|
|
if (VIR_CLOSE(stdinfd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (VIR_CLOSE(stdoutfd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (setsid() < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
nextpid = fork();
|
|
|
|
switch (nextpid) {
|
|
|
|
case 0:
|
|
|
|
return statuspipe[1];
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_CLOSE(stdoutfd);
|
|
|
|
VIR_FORCE_CLOSE(stdinfd);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
int got, exitstatus = 0;
|
|
|
|
int ret;
|
|
|
|
char status;
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(statuspipe[1]);
|
|
|
|
|
|
|
|
/* We wait to make sure the first child forked successfully */
|
|
|
|
if ((got = waitpid(pid, &exitstatus, 0)) < 0 ||
|
|
|
|
got != pid ||
|
|
|
|
exitstatus != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now block until the second child initializes successfully */
|
|
|
|
again:
|
|
|
|
ret = read(statuspipe[0], &status, 1);
|
|
|
|
if (ret == -1 && errno == EINTR)
|
|
|
|
goto again;
|
|
|
|
|
|
|
|
if (ret == 1 && status != 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
_("%s: error: %s. Check /var/log/messages or run without "
|
|
|
|
"--daemon for more info.\n"), argv0,
|
|
|
|
virDaemonErrTypeToString(status));
|
|
|
|
}
|
|
|
|
_exit(ret == 1 && status == 0 ? 0 : 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonUnixSocketPaths(bool privileged,
|
2018-01-19 14:54:00 +00:00
|
|
|
char **sockfile,
|
|
|
|
char **adminSockfile)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
if (privileged) {
|
2019-10-20 11:49:46 +00:00
|
|
|
*sockfile = g_strdup(RUNSTATEDIR "/libvirt/virtlogd-sock");
|
|
|
|
*adminSockfile = g_strdup(RUNSTATEDIR "/libvirt/virtlogd-admin-sock");
|
2015-02-09 16:35:05 +00:00
|
|
|
} else {
|
2019-12-19 09:30:34 +00:00
|
|
|
g_autofree char *rundir = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
mode_t old_umask;
|
|
|
|
|
2019-12-19 09:29:28 +00:00
|
|
|
rundir = virGetUserRuntimeDirectory();
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
old_umask = umask(077);
|
|
|
|
if (virFileMakePath(rundir) < 0) {
|
|
|
|
umask(old_umask);
|
2019-12-19 09:30:34 +00:00
|
|
|
return -1;
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
umask(old_umask);
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
*sockfile = g_strdup_printf("%s/virtlogd-sock", rundir);
|
|
|
|
*adminSockfile = g_strdup_printf("%s/virtlogd-admin-sock", rundir);
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2019-10-14 12:45:33 +00:00
|
|
|
virLogDaemonErrorHandler(void *opaque G_GNUC_UNUSED,
|
|
|
|
virErrorPtr err G_GNUC_UNUSED)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
/* Don't do anything, since logging infrastructure already
|
|
|
|
* took care of reporting the error */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
static void
|
2015-02-09 16:35:05 +00:00
|
|
|
virLogDaemonSetupLogging(virLogDaemonConfigPtr config,
|
|
|
|
bool privileged,
|
|
|
|
bool verbose,
|
|
|
|
bool godaemon)
|
|
|
|
{
|
|
|
|
virLogReset();
|
|
|
|
|
|
|
|
/*
|
2016-10-31 11:52:51 +00:00
|
|
|
* Libvirtd's order of precedence is:
|
2015-02-09 16:35:05 +00:00
|
|
|
* cmdline > environment > config
|
|
|
|
*
|
2017-08-25 14:58:51 +00:00
|
|
|
* Given the precedence, we must process the variables in the opposite
|
2015-02-09 16:35:05 +00:00
|
|
|
* order, each one overriding the previous.
|
|
|
|
*/
|
|
|
|
if (config->log_level != 0)
|
|
|
|
virLogSetDefaultPriority(config->log_level);
|
|
|
|
|
2017-08-25 14:58:51 +00:00
|
|
|
/* 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.
|
2016-10-31 11:52:51 +00:00
|
|
|
*/
|
|
|
|
ignore_value(virLogSetFilters(config->log_filters));
|
|
|
|
ignore_value(virLogSetOutputs(config->log_outputs));
|
2015-02-09 16:35:05 +00:00
|
|
|
|
2016-10-31 11:52:51 +00:00
|
|
|
/* If there are some environment variables defined, use those instead */
|
|
|
|
virLogSetFromEnv();
|
2015-02-09 16:35:05 +00:00
|
|
|
|
2016-06-28 11:05:30 +00:00
|
|
|
/*
|
|
|
|
* Command line override for --verbose
|
|
|
|
*/
|
|
|
|
if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
|
|
|
|
virLogSetDefaultPriority(VIR_LOG_INFO);
|
|
|
|
|
2017-08-25 14:58:51 +00:00
|
|
|
/* Define the default output. This is only applied if there was no setting
|
|
|
|
* from either the config or the environment.
|
|
|
|
*/
|
2019-10-22 13:26:14 +00:00
|
|
|
virLogSetDefaultOutput("virtlogd", godaemon, privileged);
|
2017-08-25 14:58:51 +00:00
|
|
|
|
|
|
|
if (virLogGetNbOutputs() == 0)
|
|
|
|
virLogSetOutputs(virLogGetDefaultOutput());
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Display version information. */
|
|
|
|
static void
|
|
|
|
virLogDaemonVersion(const char *argv0)
|
|
|
|
{
|
|
|
|
printf("%s (%s) %s\n", argv0, PACKAGE_NAME, PACKAGE_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLogDaemonShutdownHandler(virNetDaemonPtr dmn,
|
2019-10-14 12:45:33 +00:00
|
|
|
siginfo_t *sig G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
virNetDaemonQuit(dmn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLogDaemonExecRestartHandler(virNetDaemonPtr dmn,
|
2019-10-14 12:45:33 +00:00
|
|
|
siginfo_t *sig G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
execRestart = true;
|
|
|
|
virNetDaemonQuit(dmn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonSetupSignals(virNetDaemonPtr dmn)
|
|
|
|
{
|
|
|
|
if (virNetDaemonAddSignalHandler(dmn, SIGINT, virLogDaemonShutdownHandler, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virNetDaemonAddSignalHandler(dmn, SIGQUIT, virLogDaemonShutdownHandler, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virNetDaemonAddSignalHandler(dmn, SIGTERM, virLogDaemonShutdownHandler, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
if (virNetDaemonAddSignalHandler(dmn, SIGUSR1, virLogDaemonExecRestartHandler, NULL) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLogDaemonClientFree(void *opaque)
|
|
|
|
{
|
|
|
|
virLogDaemonClientPtr priv = opaque;
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
return;
|
|
|
|
|
|
|
|
VIR_DEBUG("priv=%p client=%lld",
|
|
|
|
priv,
|
|
|
|
(unsigned long long)priv->clientPid);
|
|
|
|
|
|
|
|
virMutexDestroy(&priv->lock);
|
|
|
|
VIR_FREE(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
virLogDaemonClientNew(virNetServerClientPtr client,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virLogDaemonClientPtr priv;
|
|
|
|
uid_t clientuid;
|
|
|
|
gid_t clientgid;
|
|
|
|
unsigned long long timestamp;
|
|
|
|
bool privileged = opaque != NULL;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(priv) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (virMutexInit(&priv->lock) < 0) {
|
|
|
|
VIR_FREE(priv);
|
|
|
|
virReportSystemError(errno, "%s", _("unable to init mutex"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virNetServerClientGetUNIXIdentity(client,
|
|
|
|
&clientuid,
|
|
|
|
&clientgid,
|
|
|
|
&priv->clientPid,
|
|
|
|
×tamp) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
VIR_DEBUG("New client pid %llu uid %llu",
|
|
|
|
(unsigned long long)priv->clientPid,
|
|
|
|
(unsigned long long)clientuid);
|
|
|
|
|
|
|
|
if (!privileged) {
|
|
|
|
if (geteuid() != clientuid) {
|
|
|
|
virReportRestrictedError(_("Disallowing client %llu with uid %llu"),
|
|
|
|
(unsigned long long)priv->clientPid,
|
|
|
|
(unsigned long long)clientuid);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (clientuid != 0) {
|
|
|
|
virReportRestrictedError(_("Disallowing client %llu with uid %llu"),
|
|
|
|
(unsigned long long)priv->clientPid,
|
|
|
|
(unsigned long long)clientuid);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 15:38:47 +00:00
|
|
|
/* there's no closing handshake in the logging protocol */
|
|
|
|
virNetServerClientSetQuietEOF(client);
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
return priv;
|
|
|
|
|
|
|
|
error:
|
|
|
|
virLogDaemonClientFree(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
virLogDaemonClientNewPostExecRestart(virNetServerClientPtr client,
|
2019-10-14 12:45:33 +00:00
|
|
|
virJSONValuePtr object G_GNUC_UNUSED,
|
2015-02-09 16:35:05 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virLogDaemonClientPtr priv = virLogDaemonClientNew(client, opaque);
|
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virJSONValuePtr
|
2019-10-14 12:45:33 +00:00
|
|
|
virLogDaemonClientPreExecRestart(virNetServerClientPtr client G_GNUC_UNUSED,
|
|
|
|
void *opaque G_GNUC_UNUSED)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
virJSONValuePtr object = virJSONValueNewObject();
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonExecRestartStatePath(bool privileged,
|
|
|
|
char **state_file)
|
|
|
|
{
|
|
|
|
if (privileged) {
|
2019-10-20 11:49:46 +00:00
|
|
|
*state_file = g_strdup(RUNSTATEDIR "/virtlogd-restart-exec.json");
|
2015-02-09 16:35:05 +00:00
|
|
|
} else {
|
2019-12-19 09:31:39 +00:00
|
|
|
g_autofree char *rundir = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
mode_t old_umask;
|
|
|
|
|
2019-12-19 09:29:28 +00:00
|
|
|
rundir = virGetUserRuntimeDirectory();
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
old_umask = umask(077);
|
|
|
|
if (virFileMakePath(rundir) < 0) {
|
|
|
|
umask(old_umask);
|
2019-12-19 09:31:39 +00:00
|
|
|
return -1;
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
umask(old_umask);
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
*state_file = g_strdup_printf("%s/virtlogd-restart-exec.json", rundir);
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
virLogDaemonGetExecRestartMagic(void)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
ret = g_strdup_printf("%lld", (long long int)getpid());
|
2015-02-09 16:35:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonPostExecRestart(const char *state_file,
|
|
|
|
const char *pid_file,
|
|
|
|
int *pid_file_fd,
|
2016-07-01 16:40:55 +00:00
|
|
|
bool privileged,
|
|
|
|
virLogDaemonConfigPtr config)
|
2015-02-09 16:35:05 +00:00
|
|
|
{
|
|
|
|
const char *gotmagic;
|
|
|
|
char *wantmagic = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
char *state = NULL;
|
|
|
|
virJSONValuePtr object = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Running post-restart exec");
|
|
|
|
|
|
|
|
if (!virFileExists(state_file)) {
|
|
|
|
VIR_DEBUG("No restart state file %s present",
|
|
|
|
state_file);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileReadAll(state_file,
|
|
|
|
1024 * 1024 * 10, /* 10 MB */
|
|
|
|
&state) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_DEBUG("Loading state %s", state);
|
|
|
|
|
|
|
|
if (!(object = virJSONValueFromString(state)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
gotmagic = virJSONValueObjectGetString(object, "magic");
|
|
|
|
if (!gotmagic) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Missing magic data in JSON document"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(wantmagic = virLogDaemonGetExecRestartMagic()))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (STRNEQ(gotmagic, wantmagic)) {
|
|
|
|
VIR_WARN("Found restart exec file with old magic %s vs wanted %s",
|
|
|
|
gotmagic, wantmagic);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-claim PID file now as we will not be daemonizing */
|
|
|
|
if (pid_file &&
|
|
|
|
(*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2016-07-01 16:40:55 +00:00
|
|
|
if (!(logDaemon = virLogDaemonNewPostExecRestart(object,
|
|
|
|
privileged,
|
|
|
|
config)))
|
2015-02-09 16:35:05 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
unlink(state_file);
|
|
|
|
VIR_FREE(wantmagic);
|
|
|
|
VIR_FREE(state);
|
|
|
|
virJSONValueFree(object);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virLogDaemonPreExecRestart(const char *state_file,
|
|
|
|
virNetDaemonPtr dmn,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
virJSONValuePtr child;
|
|
|
|
char *state = NULL;
|
|
|
|
int ret = -1;
|
2020-03-04 09:04:33 +00:00
|
|
|
virJSONValuePtr object = virJSONValueNewObject();
|
2015-02-09 16:35:05 +00:00
|
|
|
char *magic;
|
|
|
|
virHashKeyValuePairPtr pairs = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Running pre-restart exec");
|
|
|
|
|
|
|
|
if (!(child = virNetDaemonPreExecRestart(dmn)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectAppend(object, "daemon", child) < 0) {
|
|
|
|
virJSONValueFree(child);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(magic = virLogDaemonGetExecRestartMagic()))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectAppendString(object, "magic", magic) < 0) {
|
|
|
|
VIR_FREE(magic);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2015-11-03 11:01:21 +00:00
|
|
|
if (!(child = virLogHandlerPreExecRestart(logDaemon->handler)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virJSONValueObjectAppend(object, "handler", child) < 0) {
|
|
|
|
virJSONValueFree(child);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
if (!(state = virJSONValueToString(object, true)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
VIR_DEBUG("Saving state %s", state);
|
|
|
|
|
|
|
|
if (virFileWriteStr(state_file,
|
|
|
|
state, 0700) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to save state file %s"),
|
|
|
|
state_file);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (execvp(argv[0], argv) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to restart self"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(); /* This should be impossible to reach */
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(pairs);
|
|
|
|
VIR_FREE(state);
|
|
|
|
virJSONValueFree(object);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
virLogDaemonUsage(const char *argv0, bool privileged)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
_("\n"
|
|
|
|
"Usage:\n"
|
|
|
|
" %s [options]\n"
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -h | --help Display program help:\n"
|
|
|
|
" -v | --verbose Verbose messages.\n"
|
|
|
|
" -d | --daemon Run as a daemon & write PID file.\n"
|
|
|
|
" -t | --timeout <secs> Exit after timeout period.\n"
|
|
|
|
" -f | --config <file> Configuration file.\n"
|
|
|
|
" -V | --version Display version information.\n"
|
|
|
|
" -p | --pid-file <file> Change name of PID file.\n"
|
|
|
|
"\n"
|
|
|
|
"libvirt log management daemon:\n"), argv0);
|
|
|
|
|
|
|
|
if (privileged) {
|
|
|
|
fprintf(stderr,
|
|
|
|
_("\n"
|
|
|
|
" Default paths:\n"
|
|
|
|
"\n"
|
|
|
|
" Configuration file (unless overridden by -f):\n"
|
|
|
|
" %s/libvirt/virtlogd.conf\n"
|
|
|
|
"\n"
|
|
|
|
" Sockets:\n"
|
2019-08-20 15:05:12 +00:00
|
|
|
" %s/libvirt/virtlogd-sock\n"
|
2015-02-09 16:35:05 +00:00
|
|
|
"\n"
|
|
|
|
" PID file (unless overridden by -p):\n"
|
2019-08-20 15:05:12 +00:00
|
|
|
" %s/virtlogd.pid\n"
|
2015-02-09 16:35:05 +00:00
|
|
|
"\n"),
|
|
|
|
SYSCONFDIR,
|
2019-08-20 15:05:12 +00:00
|
|
|
RUNSTATEDIR,
|
|
|
|
RUNSTATEDIR);
|
2015-02-09 16:35:05 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s",
|
|
|
|
_("\n"
|
|
|
|
" Default paths:\n"
|
|
|
|
"\n"
|
|
|
|
" Configuration file (unless overridden by -f):\n"
|
|
|
|
" $XDG_CONFIG_HOME/libvirt/virtlogd.conf\n"
|
|
|
|
"\n"
|
|
|
|
" Sockets:\n"
|
|
|
|
" $XDG_RUNTIME_DIR/libvirt/virtlogd-sock\n"
|
|
|
|
"\n"
|
|
|
|
" PID file:\n"
|
|
|
|
" $XDG_RUNTIME_DIR/libvirt/virtlogd.pid\n"
|
|
|
|
"\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-01-19 14:54:00 +00:00
|
|
|
virNetServerPtr logSrv = NULL;
|
|
|
|
virNetServerPtr adminSrv = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
virNetServerProgramPtr logProgram = NULL;
|
2018-01-19 14:54:00 +00:00
|
|
|
virNetServerProgramPtr adminProgram = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
char *remote_config_file = NULL;
|
|
|
|
int statuswrite = -1;
|
|
|
|
int ret = 1;
|
|
|
|
int verbose = 0;
|
|
|
|
int godaemon = 0;
|
|
|
|
char *run_dir = NULL;
|
|
|
|
char *pid_file = NULL;
|
|
|
|
int pid_file_fd = -1;
|
|
|
|
char *sock_file = NULL;
|
2018-01-19 14:54:00 +00:00
|
|
|
char *admin_sock_file = NULL;
|
2015-02-09 16:35:05 +00:00
|
|
|
int timeout = -1; /* -t: Shutdown timeout */
|
|
|
|
char *state_file = NULL;
|
|
|
|
bool implicit_conf = false;
|
|
|
|
mode_t old_umask;
|
|
|
|
bool privileged = false;
|
|
|
|
virLogDaemonConfigPtr config = NULL;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
struct option opts[] = {
|
|
|
|
{ "verbose", no_argument, &verbose, 'v'},
|
|
|
|
{ "daemon", no_argument, &godaemon, 'd'},
|
|
|
|
{ "config", required_argument, NULL, 'f'},
|
|
|
|
{ "timeout", required_argument, NULL, 't'},
|
|
|
|
{ "pid-file", required_argument, NULL, 'p'},
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
privileged = geteuid() == 0;
|
|
|
|
|
2016-04-12 22:29:52 +00:00
|
|
|
if (virGettextInitialize() < 0 ||
|
2015-02-09 16:35:05 +00:00
|
|
|
virErrorInitialize() < 0) {
|
|
|
|
fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int optidx = 0;
|
|
|
|
int c;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "df:p:t:vVh", opts, &optidx);
|
|
|
|
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 0:
|
|
|
|
/* Got one of the flags */
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
godaemon = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (virStrToLong_i(optarg, &tmp, 10, &timeout) != 0
|
|
|
|
|| timeout <= 0
|
|
|
|
/* Ensure that we can multiply by 1000 without overflowing. */
|
|
|
|
|| timeout > INT_MAX / 1000) {
|
|
|
|
VIR_ERROR(_("Invalid value for timeout"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
VIR_FREE(pid_file);
|
2019-10-20 11:49:46 +00:00
|
|
|
pid_file = g_strdup(optarg);
|
2015-02-09 16:35:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
VIR_FREE(remote_config_file);
|
2019-10-20 11:49:46 +00:00
|
|
|
remote_config_file = g_strdup(optarg);
|
2015-02-09 16:35:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
virLogDaemonVersion(argv[0]);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
virLogDaemonUsage(argv[0], privileged);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
virLogDaemonUsage(argv[0], privileged);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 10:52:08 +00:00
|
|
|
virFileActivateDirOverrideForProg(argv[0]);
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
if (!(config = virLogDaemonConfigNew(privileged))) {
|
|
|
|
VIR_ERROR(_("Can't create initial configuration"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No explicit config, so try and find a default one */
|
|
|
|
if (remote_config_file == NULL) {
|
|
|
|
implicit_conf = true;
|
|
|
|
if (virLogDaemonConfigFilePath(privileged,
|
|
|
|
&remote_config_file) < 0) {
|
|
|
|
VIR_ERROR(_("Can't determine config path"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:30:04 +00:00
|
|
|
/* Read the config file if it exists */
|
2015-02-09 16:35:05 +00:00
|
|
|
if (remote_config_file &&
|
|
|
|
virLogDaemonConfigLoadFile(config, remote_config_file, implicit_conf) < 0) {
|
2016-05-19 19:10:19 +00:00
|
|
|
VIR_ERROR(_("Can't load config file: %s: %s"),
|
|
|
|
virGetLastErrorMessage(), remote_config_file);
|
2015-02-09 16:35:05 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
virLogDaemonSetupLogging(config, privileged, verbose, godaemon);
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
if (!pid_file &&
|
|
|
|
virPidFileConstructPath(privileged,
|
2019-08-20 15:05:12 +00:00
|
|
|
RUNSTATEDIR,
|
2015-02-09 16:35:05 +00:00
|
|
|
"virtlogd",
|
|
|
|
&pid_file) < 0) {
|
|
|
|
VIR_ERROR(_("Can't determine pid file path."));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Decided on pid file path '%s'", NULLSTR(pid_file));
|
|
|
|
|
|
|
|
if (virLogDaemonUnixSocketPaths(privileged,
|
2018-01-19 14:54:00 +00:00
|
|
|
&sock_file,
|
|
|
|
&admin_sock_file) < 0) {
|
2015-02-09 16:35:05 +00:00
|
|
|
VIR_ERROR(_("Can't determine socket paths"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-01-19 14:54:00 +00:00
|
|
|
VIR_DEBUG("Decided on socket paths '%s' and '%s'",
|
|
|
|
sock_file, admin_sock_file);
|
2015-02-09 16:35:05 +00:00
|
|
|
|
|
|
|
if (virLogDaemonExecRestartStatePath(privileged,
|
|
|
|
&state_file) < 0) {
|
|
|
|
VIR_ERROR(_("Can't determine restart state file path"));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Decided on restart state file path '%s'",
|
|
|
|
state_file);
|
|
|
|
|
|
|
|
/* Ensure the rundir exists (on tmpfs on some systems) */
|
|
|
|
if (privileged) {
|
2019-10-20 11:49:46 +00:00
|
|
|
run_dir = g_strdup(RUNSTATEDIR "/libvirt");
|
2015-02-09 16:35:05 +00:00
|
|
|
} else {
|
2019-12-19 09:29:28 +00:00
|
|
|
run_dir = virGetUserRuntimeDirectory();
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (privileged)
|
|
|
|
old_umask = umask(022);
|
|
|
|
else
|
|
|
|
old_umask = umask(077);
|
|
|
|
VIR_DEBUG("Ensuring run dir '%s' exists", run_dir);
|
|
|
|
if (virFileMakePath(run_dir) < 0) {
|
|
|
|
char ebuf[1024];
|
|
|
|
VIR_ERROR(_("unable to create rundir %s: %s"), run_dir,
|
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_RUNDIR;
|
|
|
|
umask(old_umask);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
umask(old_umask);
|
|
|
|
|
|
|
|
if ((rv = virLogDaemonPostExecRestart(state_file,
|
|
|
|
pid_file,
|
|
|
|
&pid_file_fd,
|
2016-07-01 16:40:55 +00:00
|
|
|
privileged,
|
|
|
|
config)) < 0) {
|
2015-02-09 16:35:05 +00:00
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rv == 1, means we setup everything from saved state,
|
|
|
|
* so only (possibly) daemonize and setup stuff from
|
|
|
|
* scratch if rv == 0
|
|
|
|
*/
|
|
|
|
if (rv == 0) {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSystemdActivation) act = NULL;
|
2019-06-25 17:29:43 +00:00
|
|
|
virSystemdActivationMap actmap[] = {
|
|
|
|
{ .name = "virtlogd.socket", .family = AF_UNIX, .path = sock_file },
|
|
|
|
{ .name = "virtlogd-admin.socket", .family = AF_UNIX, .path = admin_sock_file },
|
|
|
|
};
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
if (godaemon) {
|
|
|
|
char ebuf[1024];
|
|
|
|
|
|
|
|
if (chdir("/") < 0) {
|
|
|
|
VIR_ERROR(_("cannot change to root directory: %s"),
|
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((statuswrite = virLogDaemonForkIntoBackground(argv[0])) < 0) {
|
|
|
|
VIR_ERROR(_("Failed to fork as daemon: %s"),
|
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have a pidfile set, claim it now, exiting if already taken */
|
|
|
|
if ((pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_PIDFILE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(logDaemon = virLogDaemonNew(config, privileged))) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:29:43 +00:00
|
|
|
if (virSystemdGetActivation(actmap,
|
2019-10-15 11:55:26 +00:00
|
|
|
G_N_ELEMENTS(actmap),
|
2019-06-25 17:29:43 +00:00
|
|
|
&act) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_NETWORK;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:54:00 +00:00
|
|
|
logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd");
|
|
|
|
adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin");
|
2019-06-25 17:29:43 +00:00
|
|
|
|
|
|
|
if (virNetServerAddServiceUNIX(logSrv,
|
|
|
|
act, "virtlogd.socket",
|
|
|
|
sock_file, 0700, 0, 0,
|
|
|
|
NULL,
|
|
|
|
false, 0, 1) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_NETWORK;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virNetServerAddServiceUNIX(adminSrv,
|
|
|
|
act, "virtlogd-admin.socket",
|
|
|
|
admin_sock_file, 0700, 0, 0,
|
|
|
|
NULL,
|
|
|
|
false, 0, 1) < 0) {
|
2015-02-09 16:35:05 +00:00
|
|
|
ret = VIR_LOG_DAEMON_ERR_NETWORK;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2019-06-25 17:29:43 +00:00
|
|
|
if (act &&
|
|
|
|
virSystemdActivationComplete(act) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_NETWORK;
|
|
|
|
goto cleanup;
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
2019-06-25 17:29:43 +00:00
|
|
|
} else {
|
|
|
|
logSrv = virNetDaemonGetServer(logDaemon->dmn, "virtlogd");
|
|
|
|
/* If exec-restarting from old virtlogd, we won't have an
|
|
|
|
* admin server present */
|
|
|
|
if (virNetDaemonHasServer(logDaemon->dmn, "admin"))
|
|
|
|
adminSrv = virNetDaemonGetServer(logDaemon->dmn, "admin");
|
2015-02-09 16:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout != -1) {
|
|
|
|
VIR_DEBUG("Registering shutdown timeout %d", timeout);
|
|
|
|
virNetDaemonAutoShutdown(logDaemon->dmn,
|
|
|
|
timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((virLogDaemonSetupSignals(logDaemon->dmn)) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_SIGNAL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(logProgram = virNetServerProgramNew(VIR_LOG_MANAGER_PROTOCOL_PROGRAM,
|
|
|
|
VIR_LOG_MANAGER_PROTOCOL_PROGRAM_VERSION,
|
|
|
|
virLogManagerProtocolProcs,
|
|
|
|
virLogManagerProtocolNProcs))) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-01-19 14:54:00 +00:00
|
|
|
if (virNetServerAddProgram(logSrv, logProgram) < 0) {
|
2015-02-09 16:35:05 +00:00
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:54:00 +00:00
|
|
|
if (adminSrv != NULL) {
|
|
|
|
if (!(adminProgram = virNetServerProgramNew(ADMIN_PROGRAM,
|
|
|
|
ADMIN_PROTOCOL_VERSION,
|
|
|
|
adminProcs,
|
|
|
|
adminNProcs))) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virNetServerAddProgram(adminSrv, adminProgram) < 0) {
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_INIT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 16:35:05 +00:00
|
|
|
/* Disable error func, now logging is setup */
|
|
|
|
virSetErrorFunc(NULL, virLogDaemonErrorHandler);
|
|
|
|
|
|
|
|
/* Tell parent of daemon that basic initialization is complete
|
|
|
|
* In particular we're ready to accept net connections & have
|
|
|
|
* written the pidfile
|
|
|
|
*/
|
|
|
|
if (statuswrite != -1) {
|
|
|
|
char status = 0;
|
|
|
|
while (write(statuswrite, &status, 1) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
VIR_FORCE_CLOSE(statuswrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start accepting new clients from network */
|
|
|
|
|
2018-01-19 14:54:00 +00:00
|
|
|
virNetDaemonUpdateServices(logDaemon->dmn, true);
|
2015-02-09 16:35:05 +00:00
|
|
|
virNetDaemonRun(logDaemon->dmn);
|
|
|
|
|
|
|
|
if (execRestart &&
|
|
|
|
virLogDaemonPreExecRestart(state_file,
|
|
|
|
logDaemon->dmn,
|
|
|
|
argv) < 0)
|
|
|
|
ret = VIR_LOG_DAEMON_ERR_REEXEC;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(logProgram);
|
2018-01-19 14:54:00 +00:00
|
|
|
virObjectUnref(adminProgram);
|
|
|
|
virObjectUnref(logSrv);
|
|
|
|
virObjectUnref(adminSrv);
|
2015-02-09 16:35:05 +00:00
|
|
|
virLogDaemonFree(logDaemon);
|
|
|
|
if (statuswrite != -1) {
|
|
|
|
if (ret != 0) {
|
|
|
|
/* Tell parent of daemon what failed */
|
|
|
|
char status = ret;
|
|
|
|
while (write(statuswrite, &status, 1) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
VIR_FORCE_CLOSE(statuswrite);
|
|
|
|
}
|
|
|
|
if (pid_file_fd != -1)
|
|
|
|
virPidFileReleasePath(pid_file, pid_file_fd);
|
|
|
|
VIR_FREE(pid_file);
|
|
|
|
VIR_FREE(sock_file);
|
2018-01-19 14:54:00 +00:00
|
|
|
VIR_FREE(admin_sock_file);
|
2015-02-09 16:35:05 +00:00
|
|
|
VIR_FREE(state_file);
|
|
|
|
VIR_FREE(run_dir);
|
2016-04-10 23:19:25 +00:00
|
|
|
VIR_FREE(remote_config_file);
|
|
|
|
virLogDaemonConfigFree(config);
|
2015-02-09 16:35:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|