From nobody Sun Feb 8 16:49:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.74 as permitted sender) client-ip=216.205.24.74; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-74.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.74 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1585235962; cv=none; d=zohomail.com; s=zohoarc; b=jb+1xTl8pZewjnWSJMhRpxK6LNNbVRS0OlWHwuBdsjNiSQwWbJFpNmI/LeOKPnAEoLl5S2uyCka6UKLVAe581E15WiiNN86KylKLYQjek8s9tWvefQLOZNL7X9FJZpa1Z5stBocghtsUkfSOFf+/623YB/KO/iQv73Kt23lN65w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585235962; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CLnAf1qCszzUj37sMoN8drQSUSe7aTQGYuG5+4nEN4k=; b=jiJf9EgIQQBccesPZRvSEBk/vNwcolkvGpX9+saieswYhMhBkbdG1hE7aOCGJ0BxajFwIZAkvBl0sEGhikyoPv+DNo1M5upv6wtpvc7rXjONQgMcRmUpWzp1GX0F5HPOcGwhJ+KXKc4bWG+nWLtcxaZ6PU13DykvYTiRR+SBrOw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.74 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) by mx.zohomail.com with SMTPS id 1585235962650203.03483708679414; Thu, 26 Mar 2020 08:19:22 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-473-2DUAe6ndMHW_cCRbhxUSBA-1; Thu, 26 Mar 2020 11:19:18 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9D2FD107ACC4; Thu, 26 Mar 2020 15:19:12 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C0CF19C70; Thu, 26 Mar 2020 15:19:12 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1D36C86380; Thu, 26 Mar 2020 15:19:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 02QFJ9xG029275 for ; Thu, 26 Mar 2020 11:19:09 -0400 Received: by smtp.corp.redhat.com (Postfix) id BBABD114CFD; Thu, 26 Mar 2020 15:19:09 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B6545114D0D for ; Thu, 26 Mar 2020 15:19:06 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F13EF8F7840 for ; Thu, 26 Mar 2020 15:19:05 +0000 (UTC) Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-489-TI9Mlqp9MNygAr0Mp7yOxg-1; Thu, 26 Mar 2020 11:19:03 -0400 Received: by mail-wm1-f67.google.com with SMTP id f6so861299wmj.3 for ; Thu, 26 Mar 2020 08:19:02 -0700 (PDT) Received: from localhost.localdomain (public-gprs375156.centertel.pl. [37.47.96.181]) by smtp.gmail.com with ESMTPSA id t2sm3754729wml.30.2020.03.26.08.18.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 08:18:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585235961; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=CLnAf1qCszzUj37sMoN8drQSUSe7aTQGYuG5+4nEN4k=; b=JFzvp1wpAPLPsasPyPr2XFce0L6GjlypLBg3zXgPm12VYrXkqWjSywvv3JKnwM0XaTix/M f6jfW3NDHe/H9XNe2MPtE6l/T7YcYy1KBs8yVNfGfkD4u1jFaO4aULFTtaSuM2lacBx4LU v+1ySczPJjHGAWar/KJQ0f5UB0sKDLg= X-MC-Unique: 2DUAe6ndMHW_cCRbhxUSBA-1 X-MC-Unique: TI9Mlqp9MNygAr0Mp7yOxg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QsgfDkbHXozTqignRFAKKaiyndXxSk5uCTX3T3WS2BI=; b=SY9WaldfmypKJOdy/BAo1GzrMutZJ0LvA/IPM+kE2fcNTLryYYugb2s+xbTqE9BiD6 N/pu9EeOt8eryc4vdzCOwCWu+OK5cEcv72zMI3TfMak+WZqskRrX9YhLVbIU21ZnpnZ6 kEIBe/q2dDGSYUZKRCU3L5dnhVP9F3zEIY4gX2wgufpKadbmN/jOZBRIxX0QcQ9ZWw6X ENFEEpklIUxsXc0NYFkjEYAIHtTbujRhWvHp6C48qcWLwcPexMWZQTMlr9KBJyiWHmOo oz7G3qUguLgGxPt+42FNwe79y1ZcwApFe6d6jK8bhysb2jITSCKvl3RfYgoA8szZ1yQN 3o9w== X-Gm-Message-State: ANhLgQ26/FTYrhFWqiteYI20+8+az4JHRvhWtfyrpTAvIRaIQvMRwsl6 WMVDoXBHI+AzNgrOUZG1dsJhRUHFE/w= X-Google-Smtp-Source: ADFU+vuZ2lyt405+FpYr0piCL4FCK6w6xuNCyuxqvpPcjcc8gfqvBFvotA6mJIeXtKRc7cPj1CWBAA== X-Received: by 2002:a1c:bb86:: with SMTP id l128mr420460wmf.41.1585235940412; Thu, 26 Mar 2020 08:19:00 -0700 (PDT) From: Rafael Fonseca To: libvir-list@redhat.com Subject: [PATCH 2/4] locking: use shared daemon startup code Date: Thu, 26 Mar 2020 16:18:01 +0100 Message-Id: <20200326151803.2349357-3-r4f4rfs@gmail.com> In-Reply-To: <20200326151803.2349357-1-r4f4rfs@gmail.com> References: <20200326151803.2349357-1-r4f4rfs@gmail.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 02QFJ9xG029275 X-loop: libvir-list@redhat.com Cc: Rafael Fonseca X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Rafael Fonseca --- src/locking/lock_daemon.c | 251 +++++--------------------------------- 1 file changed, 29 insertions(+), 222 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index a258f7e6f1..3d33995beb 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -45,7 +45,7 @@ #include "viruuid.h" #include "virstring.h" #include "virgettext.h" -#include "virenum.h" +#include "virdaemon.h" =20 #include "locking/lock_daemon_dispatch.h" #include "locking/lock_protocol.h" @@ -69,36 +69,6 @@ virLockDaemonPtr lockDaemon =3D NULL; =20 static bool execRestart; =20 -enum { - VIR_LOCK_DAEMON_ERR_NONE =3D 0, - VIR_LOCK_DAEMON_ERR_PIDFILE, - VIR_LOCK_DAEMON_ERR_RUNDIR, - VIR_LOCK_DAEMON_ERR_INIT, - VIR_LOCK_DAEMON_ERR_SIGNAL, - VIR_LOCK_DAEMON_ERR_PRIVS, - VIR_LOCK_DAEMON_ERR_NETWORK, - VIR_LOCK_DAEMON_ERR_CONFIG, - VIR_LOCK_DAEMON_ERR_HOOKS, - VIR_LOCK_DAEMON_ERR_REEXEC, - - VIR_LOCK_DAEMON_ERR_LAST -}; - -VIR_ENUM_DECL(virDaemonErr); -VIR_ENUM_IMPL(virDaemonErr, - VIR_LOCK_DAEMON_ERR_LAST, - "Initialization successful", - "Unable to obtain pidfile", - "Unable to create rundir", - "Unable to initialize libvirt", - "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", - "Unable to re-execute daemon", -); - static void * virLockDaemonClientNew(virNetServerClientPtr client, void *opaque); @@ -352,122 +322,6 @@ virLockSpacePtr virLockDaemonFindLockSpace(virLockDae= monPtr lockd, } =20 =20 -static int -virLockDaemonForkIntoBackground(const char *argv0) -{ - int statuspipe[2]; - if (virPipeQuiet(statuspipe) < 0) - return -1; - - pid_t pid =3D fork(); - switch (pid) { - case 0: - { - int stdinfd =3D -1; - int stdoutfd =3D -1; - int nextpid; - - VIR_FORCE_CLOSE(statuspipe[0]); - - if ((stdinfd =3D open("/dev/null", O_RDONLY)) < 0) - goto cleanup; - if ((stdoutfd =3D open("/dev/null", O_WRONLY)) < 0) - goto cleanup; - if (dup2(stdinfd, STDIN_FILENO) !=3D STDIN_FILENO) - goto cleanup; - if (dup2(stdoutfd, STDOUT_FILENO) !=3D STDOUT_FILENO) - goto cleanup; - if (dup2(stdoutfd, STDERR_FILENO) !=3D STDERR_FILENO) - goto cleanup; - if (VIR_CLOSE(stdinfd) < 0) - goto cleanup; - if (VIR_CLOSE(stdoutfd) < 0) - goto cleanup; - - if (setsid() < 0) - goto cleanup; - - nextpid =3D 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 =3D 0; - int ret; - char status; - - VIR_FORCE_CLOSE(statuspipe[1]); - - /* We wait to make sure the first child forked successfully */ - if ((got =3D waitpid(pid, &exitstatus, 0)) < 0 || - got !=3D pid || - exitstatus !=3D 0) { - return -1; - } - - /* Now block until the second child initializes successfully */ - again: - ret =3D read(statuspipe[0], &status, 1); - if (ret =3D=3D -1 && errno =3D=3D EINTR) - goto again; - - if (ret =3D=3D 1 && status !=3D 0) { - fprintf(stderr, - _("%s: error: %s. Check /var/log/messages or run w= ithout " - "--daemon for more info.\n"), argv0, - virDaemonErrTypeToString(status)); - } - _exit(ret =3D=3D 1 && status =3D=3D 0 ? 0 : 1); - } - } -} - - -static int -virLockDaemonUnixSocketPaths(bool privileged, - char **sockfile, - char **adminSockfile) -{ - if (privileged) { - *sockfile =3D g_strdup(RUNSTATEDIR "/libvirt/virtlockd-sock"); - *adminSockfile =3D g_strdup(RUNSTATEDIR "/libvirt/virtlockd-admin-= sock"); - } else { - g_autofree char *rundir =3D NULL; - mode_t old_umask; - - rundir =3D virGetUserRuntimeDirectory(); - - old_umask =3D umask(077); - if (virFileMakePath(rundir) < 0) { - umask(old_umask); - return -1; - } - umask(old_umask); - - *sockfile =3D g_strdup_printf("%s/virtlockd-sock", rundir); - *adminSockfile =3D g_strdup_printf("%s/virtlockd-admin-sock", rund= ir); - } - return 0; -} - - static void virLockDaemonErrorHandler(void *opaque G_GNUC_UNUSED, virErrorPtr err G_GNUC_UNUSED) @@ -477,59 +331,6 @@ virLockDaemonErrorHandler(void *opaque G_GNUC_UNUSED, } =20 =20 -/* - * Set up the logging environment - * By default if daemonized all errors go to the logfile libvirtd.log, - * but if verbose or error debugging is asked for then also output - * informational and debug messages. Default size if 64 kB. - */ -static int -virLockDaemonSetupLogging(virLockDaemonConfigPtr config, - bool privileged, - bool verbose, - bool godaemon) -{ - virLogReset(); - - /* - * Libvirtd's order of precedence is: - * cmdline > environment > config - * - * Given the precedence, we must process the variables in the opposite - * order, each one overriding the previous. - */ - if (config->log_level !=3D 0) - virLogSetDefaultPriority(config->log_level); - - /* In case the config is empty, both filters and outputs will become e= mpty, - * however we can't start with empty outputs, thus we'll need to defin= e and - * setup a default one. - */ - ignore_value(virLogSetFilters(config->log_filters)); - ignore_value(virLogSetOutputs(config->log_outputs)); - - /* If there are some environment variables defined, use those instead = */ - virLogSetFromEnv(); - - /* - * Command line override for --verbose - */ - if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO)) - virLogSetDefaultPriority(VIR_LOG_INFO); - - /* Define the default output. This is only applied if there was no set= ting - * from either the config or the environment. - */ - virLogSetDefaultOutput("virtlockd", godaemon, privileged); - - if (virLogGetNbOutputs() =3D=3D 0) - virLogSetOutputs(virLogGetDefaultOutput()); - - return 0; -} - - - /* Display version information. */ static void virLockDaemonVersion(const char *argv0) @@ -1180,10 +981,13 @@ int main(int argc, char **argv) { } VIR_FREE(remote_config_file); =20 - if (virLockDaemonSetupLogging(config, privileged, verbose, godaemon) <= 0) { - VIR_ERROR(_("Can't initialize logging")); - exit(EXIT_FAILURE); - } + virDaemonSetupLogging("virtlockd", + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon); =20 if (!pid_file && virPidFileConstructPath(privileged, @@ -1195,9 +999,12 @@ int main(int argc, char **argv) { } VIR_DEBUG("Decided on pid file path '%s'", NULLSTR(pid_file)); =20 - if (virLockDaemonUnixSocketPaths(privileged, - &sock_file, - &admin_sock_file) < 0) { + if (virDaemonUnixSocketPaths("virtlockd", + privileged, + NULL, + &sock_file, + NULL, + &admin_sock_file) < 0) { VIR_ERROR(_("Can't determine socket paths")); exit(EXIT_FAILURE); } @@ -1227,7 +1034,7 @@ int main(int argc, char **argv) { if (virFileMakePath(run_dir) < 0) { VIR_ERROR(_("unable to create rundir %s: %s"), run_dir, g_strerror(errno)); - ret =3D VIR_LOCK_DAEMON_ERR_RUNDIR; + ret =3D VIR_DAEMON_ERR_RUNDIR; umask(old_umask); goto cleanup; } @@ -1237,7 +1044,7 @@ int main(int argc, char **argv) { pid_file, &pid_file_fd, privileged)) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 @@ -1258,7 +1065,7 @@ int main(int argc, char **argv) { goto cleanup; } =20 - if ((statuswrite =3D virLockDaemonForkIntoBackground(argv[0]))= < 0) { + if ((statuswrite =3D virDaemonForkIntoBackground(argv[0])) < 0= ) { VIR_ERROR(_("Failed to fork as daemon: %s"), g_strerror(errno)); goto cleanup; @@ -1267,19 +1074,19 @@ int main(int argc, char **argv) { =20 /* If we have a pidfile set, claim it now, exiting if already take= n */ if ((pid_file_fd =3D virPidFileAcquirePath(pid_file, false, getpid= ())) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_PIDFILE; + ret =3D VIR_DAEMON_ERR_PIDFILE; goto cleanup; } =20 if (!(lockDaemon =3D virLockDaemonNew(config, privileged))) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 if (virSystemdGetActivation(actmap, G_N_ELEMENTS(actmap), &act) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } =20 @@ -1291,7 +1098,7 @@ int main(int argc, char **argv) { sock_file, 0700, 0, 0, NULL, false, 0, 1) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } if (virNetServerAddServiceUNIX(adminSrv, @@ -1299,13 +1106,13 @@ int main(int argc, char **argv) { admin_sock_file, 0700, 0, 0, NULL, false, 0, 1) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } =20 if (act && virSystemdActivationComplete(act) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } } else { @@ -1323,7 +1130,7 @@ int main(int argc, char **argv) { } =20 if ((virLockDaemonSetupSignals(lockDaemon->dmn)) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_SIGNAL; + ret =3D VIR_DAEMON_ERR_SIGNAL; goto cleanup; } =20 @@ -1331,12 +1138,12 @@ int main(int argc, char **argv) { VIR_LOCK_SPACE_PROTOCOL_PRO= GRAM_VERSION, virLockSpaceProtocolProcs, virLockSpaceProtocolNProcs)= )) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 if (virNetServerAddProgram(lockSrv, lockProgram) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 @@ -1345,11 +1152,11 @@ int main(int argc, char **argv) { ADMIN_PROTOCOL_VERSION, adminProcs, adminNProcs))) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } if (virNetServerAddProgram(adminSrv, adminProgram) < 0) { - ret =3D VIR_LOCK_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } } @@ -1379,7 +1186,7 @@ int main(int argc, char **argv) { virLockDaemonPreExecRestart(state_file, lockDaemon->dmn, argv) < 0) - ret =3D VIR_LOCK_DAEMON_ERR_REEXEC; + ret =3D VIR_DAEMON_ERR_REEXEC; else ret =3D 0; =20 --=20 2.25.1