From nobody Sun Feb 8 07:07:51 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=1585235959; cv=none; d=zohomail.com; s=zohoarc; b=aP1EdT5xBN9L/gCtmO/xrvlgSsgPzp3+z2nvAw0O19mXYP1HAiybRfNLpgKvx4HiM95qzrfLnhFaX/+zzyYSxsZXcvUD6Sc7ha4/7He4w7zRHVhN9doHCZidyF59kTvkhrLHApnfW/YyY6JJRy+BQ71U6Rp2Qi50Og25JGBMdlk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585235959; 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=bl+5YGA0pRF9TYOrBYxofzc8lSo7QGJICH+cLHRIJAA=; b=QuUgsXYjK5zEG/et89CgoFrqYjeF0OcW1mmrUoPC71uJ9aVfK52ZppL5E9NM7hTHYpcLyp2Onxue9QKf3ll8lqee/fPwcYDEt0yDLrJxhuxUxxKH3KNFWxMaMMzMncE4Y97qQEGrknWqllC3nVRcd7LiE3/qDvcOlb16curIkZo= 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 1585235959439379.2266989958998; Thu, 26 Mar 2020 08:19:19 -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-176--m-S5zrUN26MpmpcXlOxsg-1; Thu, 26 Mar 2020 11:19:15 -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 703F41005509; Thu, 26 Mar 2020 15:19:10 +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 3C4C75DA7B; Thu, 26 Mar 2020 15:19:10 +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 D9C9D180887A; Thu, 26 Mar 2020 15:19:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 02QFJ8Cc029267 for ; Thu, 26 Mar 2020 11:19:08 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6F97C20316EF; Thu, 26 Mar 2020 15:19:08 +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 6B8682026D67 for ; Thu, 26 Mar 2020 15:19:04 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.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 7856B8F7849 for ; Thu, 26 Mar 2020 15:19:04 +0000 (UTC) Received: from mail-wm1-f68.google.com (mail-wm1-f68.google.com [209.85.128.68]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-386-tSmeif1JMzeRPUMGC4Xixw-1; Thu, 26 Mar 2020 11:19:01 -0400 Received: by mail-wm1-f68.google.com with SMTP id c81so6870141wmd.4 for ; Thu, 26 Mar 2020 08:19:00 -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.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 08:18:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1585235958; 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=bl+5YGA0pRF9TYOrBYxofzc8lSo7QGJICH+cLHRIJAA=; b=a/wAC4zl3XJ794WxNEiTBlTM85K+9jKmVytST98DmzTEsK67CzaZW8pkxDemj8QPNMi30m rJ+V8lG5Lvl4sOJCbCeGkTTPBsEC6fzy0QFRqlDdXgOqOyyur9iKgdMu5T/OtUxeJ7en8U ZOEcwMALhrv6iQ3xtzOc6ttb1xBrYko= X-MC-Unique: -m-S5zrUN26MpmpcXlOxsg-1 X-MC-Unique: tSmeif1JMzeRPUMGC4Xixw-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=XY5PBBPV0NNVvPVzxwQh53ohVjTjxzFJzzQv+aVNBXc=; b=s9mn5k6ZK7h0NGQBwC1vJrglr5ooC7IpFa88f64AIF2Q1fCRCa/Gl+3h4Wo24HgFeJ vfR+q8tYsgFrgvabX8IrtHlaDwkFUklv8DPcZkfXKKPjR4kdTNy5J3AB0+QGIAUMhSxy n9tQVOUyW8b9x9AcD9u2PH91q8pGPu9Wo+8FZFspIBM8knkmvUv0vIMfCLRc4dpujfVw Hclr6GQLYUi7wPkfpa/6JgKPXUs6kQs0P8HQrY6SHuW5OfsJo/GYFxol9rN9u30l7XnN 8f4w5XJWAf6w8JbpSAe2f/h6HK1VTFnYUeKibI+o+zadJ6iKJsh1hjtsE1X9vY8UIaea RxgQ== X-Gm-Message-State: ANhLgQ3DFL/McSm/nvPhBXiVRQR6oglpcgtgxqWD7LPnqRW65AnbJbF7 9tj41ucZxIQN6wxfoCdlGLimR14MEHM= X-Google-Smtp-Source: ADFU+vthGhKCXQ7UMw4jBCn0cm005unQ7ocYz9/f4eUILJv8zDpSNKikNrj7mRglSMUTk6e0BZ6GiQ== X-Received: by 2002:a05:600c:286:: with SMTP id 6mr423054wmk.101.1585235939060; Thu, 26 Mar 2020 08:18:59 -0700 (PDT) From: Rafael Fonseca To: libvir-list@redhat.com Subject: [PATCH 1/4] util: introduce shared daemon startup code Date: Thu, 26 Mar 2020 16:18:00 +0100 Message-Id: <20200326151803.2349357-2-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.78 on 10.11.54.4 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 02QFJ8Cc029267 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" Several daemons have similar code around general daemon startup code. Let's move it into a file and share it among them. Signed-off-by: Rafael Fonseca --- src/libvirt_private.syms | 6 + src/util/Makefile.inc.am | 2 + src/util/virdaemon.c | 255 +++++++++++++++++++++++++++++++++++++++ src/util/virdaemon.h | 74 ++++++++++++ 4 files changed, 337 insertions(+) create mode 100644 src/util/virdaemon.c create mode 100644 src/util/virdaemon.h diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3f032c7963..e276f55bb1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1906,6 +1906,12 @@ virCryptoHashString; virCryptoHaveCipher; =20 =20 +# util/virdaemon.h +virDaemonForkIntoBackground; +virDaemonSetupLogging; +virDaemonUnixSocketPaths; + + # util/virdbus.h virDBusCallMethod; virDBusCloseSystemBus; diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am index 718b11a5f4..5bc60cb5ea 100644 --- a/src/util/Makefile.inc.am +++ b/src/util/Makefile.inc.am @@ -42,6 +42,8 @@ UTIL_SOURCES =3D \ util/virconf.h \ util/vircrypto.c \ util/vircrypto.h \ + util/virdaemon.c \ + util/virdaemon.h \ util/virdbus.c \ util/virdbus.h \ util/virdbuspriv.h \ diff --git a/src/util/virdaemon.c b/src/util/virdaemon.c new file mode 100644 index 0000000000..4b63b44d66 --- /dev/null +++ b/src/util/virdaemon.c @@ -0,0 +1,255 @@ +/* + * virdaemon.c: shared daemon setup code + * + * Copyright (C) 2020 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 + * . + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "virdaemon.h" +#include "virutil.h" +#include "virfile.h" +#include "virlog.h" +#include "viralloc.h" + +#include "configmake.h" + +int +virDaemonForkIntoBackground(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 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) { + goto error; + } + + /* If we got 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 w= ithout " + "--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; +} + + +/* + * 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. + */ +void +virDaemonSetupLogging(const char *daemon_name, + unsigned int log_level, + char *log_filters, + char *log_outputs, + 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 (log_level !=3D 0) + virLogSetDefaultPriority(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(log_filters)); + ignore_value(virLogSetOutputs(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()); +} + + +int +virDaemonUnixSocketPaths(const char *sock_prefix, + bool privileged, + char *unix_sock_dir, + char **sockfile, + char **rosockfile, + char **admsockfile) +{ + int ret =3D -1; + char *rundir =3D NULL; + + if (unix_sock_dir) { + if (sockfile) + *sockfile =3D g_strdup_printf("%s/%s-sock", unix_sock_dir, soc= k_prefix); + + if (privileged) { + if (rosockfile) + *rosockfile =3D g_strdup_printf("%s/%s-sock-ro", + unix_sock_dir, sock_prefix); + if (admsockfile) + *admsockfile =3D g_strdup_printf("%s/%s-admin-sock", + unix_sock_dir, sock_prefix); + } + } else { + if (privileged) { + if (sockfile) + *sockfile =3D g_strdup_printf("%s/libvirt/%s-sock", + RUNSTATEDIR, sock_prefix); + if (rosockfile) + *rosockfile =3D g_strdup_printf("%s/libvirt/%s-sock-ro", + RUNSTATEDIR, sock_prefix); + if (admsockfile) + *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); + + if (sockfile) + *sockfile =3D g_strdup_printf("%s/%s-sock", rundir, sock_p= refix); + if (admsockfile) + *admsockfile =3D g_strdup_printf("%s/%s-admin-sock", rundi= r, sock_prefix); + } + } + + ret =3D 0; + cleanup: + VIR_FREE(rundir); + return ret; +} diff --git a/src/util/virdaemon.h b/src/util/virdaemon.h new file mode 100644 index 0000000000..d032b8ddb3 --- /dev/null +++ b/src/util/virdaemon.h @@ -0,0 +1,74 @@ +/* + * virdaemon.h: shared daemon setup code + * + * Copyright (C) 2020 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 + * . + */ + +#pragma once + +#include "virenum.h" + +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_REEXEC, + 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 re-execute daemon", + "Unable to initialize audit system", + "Unable to initialize driver", +); + +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 virDaemonUnixSocketPaths(const char *sock_prefix, + bool privileged, + char *unix_sock_dir, + char **sockfile, + char **rosockfile, + char **adminSockfile); --=20 2.25.1