From nobody Sun Feb 8 16:49:45 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.74 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1585235970; cv=none; d=zohomail.com; s=zohoarc; b=oFoxXgEq7YNKZ2YkPvcMMiJkfJQzwoqWTkCkE5DPoayRmyzaX02Pbq/u+9ZWylIkNHmemUbx90G3QbB0X3jzzm5zt+KItI0NPtrKII93uERo0V6io0LCFe1MnuJ4XeyUszLK10YjRXadOuAYggaxlYrNnLAXPxL4wyh6prfBb8Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585235970; 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=QoTzPo1iYkFJBUZzqNRKMN/Wr0kAPv5hu8cRLFxTcrY=; b=R0bWxv424VI7bjfPEqBfN+G3Y/z46DuGsPNh3zRWfOtdYjEvduWf0NRRu3r5e+LfURKfW3TqOaajiaAsV6MyHREFO510kf4/D/L66OaKmx0280UsKpRdGgp3WOHopw/dvvN8Ptd4O+BoKdOFhucEn/YT7PYe31lB9FqTFN4ZoYM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.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 [63.128.21.74]) by mx.zohomail.com with SMTPS id 1585235970392693.3945923498053; Thu, 26 Mar 2020 08:19:30 -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-108-pxXdf03XPueYBnWpcctrSw-1; Thu, 26 Mar 2020 11:19:26 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5B4351937FC0; Thu, 26 Mar 2020 15:19:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 31D125D9CD; Thu, 26 Mar 2020 15:19:21 +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 CA901180880D; Thu, 26 Mar 2020 15:19:20 +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 02QFJJ3t029291 for ; Thu, 26 Mar 2020 11:19:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 05AFAAF79D; Thu, 26 Mar 2020 15:19:19 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 01A14114D0A for ; Thu, 26 Mar 2020 15:19:18 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (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 D88358FF666 for ; Thu, 26 Mar 2020 15:19:18 +0000 (UTC) Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-154-qnsqS7QEMVOOKl0dd1OkHw-1; Thu, 26 Mar 2020 11:19:15 -0400 Received: by mail-wr1-f49.google.com with SMTP id h15so8252610wrx.9 for ; Thu, 26 Mar 2020 08:19:14 -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.19.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 08:19:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585235969; 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=QoTzPo1iYkFJBUZzqNRKMN/Wr0kAPv5hu8cRLFxTcrY=; b=ggSnwGg4AmBiqJAQ6Pg2gQtsSmfCiYafnwiag1slx8G9tYDemME0erWmCOd02hbbvCyAay HRi5TKYGKwmyhwcwx87yO+XMEbyBNJ2GFdGj6+Iyss3b8MzP59p/dUEYlwKYTCqsdPzW6v 210ecliEWiPN/+yNp0T4WouTcsVmDDU= X-MC-Unique: pxXdf03XPueYBnWpcctrSw-1 X-MC-Unique: qnsqS7QEMVOOKl0dd1OkHw-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=3XqbjMQUVx8p9VZB3X1W1k6HANyGVXq0UpoWAMqv6+w=; b=kdFwyUvbRmibAQAwVV2Do0b4kghzwHNgDTyCT+cFeFpUqq+GpTXtQ0h8ldp8uWlNJP kJe2XGlWWutPMtIz0L0CkgXObRSGaX9T56eEEl5r1x2YT1n+viWw51QZlhIKvxHRT3AV 1XDcfrBUETyhr9IVMOAdm88hHMVLHfdLCy2SLNiUJRt9cuPyBLW7UdZVpAj/ayKCr9uA g2WrhbHcQWPDgs/uyC94mPgPRS+4KSNORudu1ZQnfsV300UCaW8SKItFJKaZZLFcOn0N hX4qto5viCwyHw2w45/rNzZQrjvDIQXdb1E2qYxwPjjsajZqJM5ZIJGk8vkxZRO4wUAP I63g== X-Gm-Message-State: ANhLgQ3P9/II1m3qvwgdiEZo+lIi5w8Rvnn480Qk/f3gx+072fOWaBzu bqMMwZPWJoBwUU8sdtp/09UDrKprnLQ= X-Google-Smtp-Source: ADFU+vviLYonjWdrUQQ+jLPAKwZPSRONxR8kXmmPRpimpS2l/6fvDpFv6e7uAOcPCyt2lDFEnTI6eg== X-Received: by 2002:a5d:63c5:: with SMTP id c5mr9955107wrw.331.1585235953301; Thu, 26 Mar 2020 08:19:13 -0700 (PDT) From: Rafael Fonseca To: libvir-list@redhat.com Subject: [PATCH 4/4] remote: use shared daemon startup code Date: Thu, 26 Mar 2020 16:18:03 +0100 Message-Id: <20200326151803.2349357-5-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 02QFJJ3t029291 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.79 on 10.5.11.14 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/remote/remote_daemon.c | 263 +++---------------------------------- 1 file changed, 15 insertions(+), 248 deletions(-) diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index f3b3c70ebf..a1552800e9 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -57,6 +57,7 @@ #include "util/virnetdevopenvswitch.h" #include "virsystemd.h" #include "virhostuptime.h" +#include "virdaemon.h" =20 #include "driver.h" =20 @@ -78,192 +79,6 @@ virNetServerProgramPtr qemuProgram =3D NULL; =20 volatile bool driversInitialized =3D false; =20 -enum { - VIR_DAEMON_ERR_NONE =3D 0, - VIR_DAEMON_ERR_PIDFILE, - VIR_DAEMON_ERR_RUNDIR, - VIR_DAEMON_ERR_INIT, - VIR_DAEMON_ERR_SIGNAL, - VIR_DAEMON_ERR_PRIVS, - VIR_DAEMON_ERR_NETWORK, - VIR_DAEMON_ERR_CONFIG, - VIR_DAEMON_ERR_HOOKS, - VIR_DAEMON_ERR_AUDIT, - VIR_DAEMON_ERR_DRIVER, - - VIR_DAEMON_ERR_LAST -}; - -VIR_ENUM_DECL(virDaemonErr); -VIR_ENUM_IMPL(virDaemonErr, - VIR_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 initialize audit system", - "Unable to initialize driver", -); - -static int daemonForkIntoBackground(const char *argv0) -{ - int statuspipe[2]; - if (virPipeQuiet(statuspipe) < 0) - return -1; - - pid_t pid =3D fork(); - switch (pid) { - case 0: - { - /* intermediate child */ - int stdinfd =3D -1; - int stdoutfd =3D -1; - int nextpid; - - VIR_FORCE_CLOSE(statuspipe[0]); - - if ((stdinfd =3D open("/dev/null", O_RDONLY)) <=3D STDERR_FILE= NO) - goto cleanup; - if ((stdoutfd =3D open("/dev/null", O_WRONLY)) <=3D STDERR_FIL= ENO) - 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: /* grandchild */ - return statuspipe[1]; - case -1: /* error */ - goto cleanup; - default: /* intermediate child succeeded */ - _exit(EXIT_SUCCESS); - } - - cleanup: - VIR_FORCE_CLOSE(stdoutfd); - VIR_FORCE_CLOSE(stdinfd); - VIR_FORCE_CLOSE(statuspipe[1]); - _exit(EXIT_FAILURE); - - } - - case -1: /* error in parent */ - goto error; - - default: - { - /* parent */ - int ret; - char status; - - VIR_FORCE_CLOSE(statuspipe[1]); - - /* We wait to make sure the first child forked successfully */ - if (virProcessWait(pid, NULL, false) < 0) - goto error; - - /* If we get here, then the grandchild was spawned, so we - * must exit. 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; - - VIR_FORCE_CLOSE(statuspipe[0]); - - if (ret !=3D 1) { - fprintf(stderr, - _("%s: error: unable to determine if daemon is " - "running: %s\n"), argv0, - g_strerror(errno)); - exit(EXIT_FAILURE); - } else if (status !=3D 0) { - fprintf(stderr, - _("%s: error: %s. Check /var/log/messages or run " - "without --daemon for more info.\n"), argv0, - virDaemonErrTypeToString(status)); - exit(EXIT_FAILURE); - } - _exit(EXIT_SUCCESS); - } - } - - error: - VIR_FORCE_CLOSE(statuspipe[0]); - VIR_FORCE_CLOSE(statuspipe[1]); - return -1; -} - - -static int -daemonUnixSocketPaths(struct daemonConfig *config, - bool privileged, - char **sockfile, - char **rosockfile, - char **admsockfile) -{ - int ret =3D -1; - char *rundir =3D NULL; - - if (config->unix_sock_dir) { - *sockfile =3D g_strdup_printf("%s/%s-sock", config->unix_sock_dir, - SOCK_PREFIX); - - if (privileged) { - *rosockfile =3D g_strdup_printf("%s/%s-sock-ro", - config->unix_sock_dir, SOCK_PREF= IX); - *admsockfile =3D g_strdup_printf("%s/%s-admin-sock", - config->unix_sock_dir, SOCK_PRE= FIX); - } - } else { - if (privileged) { - *sockfile =3D g_strdup_printf("%s/libvirt/%s-sock", - RUNSTATEDIR, SOCK_PREFIX); - *rosockfile =3D g_strdup_printf("%s/libvirt/%s-sock-ro", - RUNSTATEDIR, SOCK_PREFIX); - *admsockfile =3D g_strdup_printf("%s/libvirt/%s-admin-sock", - RUNSTATEDIR, SOCK_PREFIX); - } else { - mode_t old_umask; - - rundir =3D virGetUserRuntimeDirectory(); - - old_umask =3D umask(077); - if (virFileMakePath(rundir) < 0) { - umask(old_umask); - goto cleanup; - } - umask(old_umask); - - *sockfile =3D g_strdup_printf("%s/%s-sock", rundir, SOCK_PREFI= X); - *admsockfile =3D g_strdup_printf("%s/%s-admin-sock", rundir, S= OCK_PREFIX); - } - } - - ret =3D 0; - cleanup: - VIR_FREE(rundir); - return ret; -} - - static void daemonErrorHandler(void *opaque G_GNUC_UNUSED, virErrorPtr err G_GNUC_UNUSED) { @@ -604,58 +419,6 @@ daemonSetupNetDevOpenvswitch(struct daemonConfig *conf= ig) } =20 =20 -/* - * Set up the logging environment - * By default if daemonized all errors go to journald/a logfile - * but if verbose or error debugging is asked for then also output - * informational and debug messages. Default size if 64 kB. - */ -static int -daemonSetupLogging(struct daemonConfig *config, - bool privileged, - bool verbose, - bool godaemon) -{ - virLogReset(); - - /* - * Logging setup 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(DAEMON_NAME, godaemon, privileged); - - if (virLogGetNbOutputs() =3D=3D 0) - virLogSetOutputs(virLogGetDefaultOutput()); - - return 0; -} - - static int daemonSetupAccessManager(struct daemonConfig *config) { @@ -1148,10 +911,13 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } =20 - if (daemonSetupLogging(config, privileged, verbose, godaemon) < 0) { - VIR_ERROR(_("Can't initialize logging")); - exit(EXIT_FAILURE); - } + virDaemonSetupLogging(DAEMON_NAME, + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon); =20 /* Let's try to initialize global variable that holds the host's boot = time. */ if (virHostBootTimeInit() < 0) { @@ -1178,11 +944,12 @@ int main(int argc, char **argv) { } VIR_DEBUG("Decided on pid file path '%s'", NULLSTR(pid_file)); =20 - if (daemonUnixSocketPaths(config, - privileged, - &sock_file, - &sock_file_ro, - &sock_file_adm) < 0) { + if (virDaemonUnixSocketPaths(SOCK_PREFIX, + privileged, + config->unix_sock_dir, + &sock_file, + &sock_file_ro, + &sock_file_adm) < 0) { VIR_ERROR(_("Can't determine socket paths")); exit(EXIT_FAILURE); } @@ -1198,7 +965,7 @@ int main(int argc, char **argv) { goto cleanup; } =20 - if ((statuswrite =3D daemonForkIntoBackground(argv[0])) < 0) { + if ((statuswrite =3D virDaemonForkIntoBackground(argv[0])) < 0) { VIR_ERROR(_("Failed to fork as daemon: %s"), g_strerror(errno)); goto cleanup; --=20 2.25.1