From nobody Sun May 5 09:27:43 2024 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 Reviewed-by: Michal Privoznik --- 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 From nobody Sun May 5 09:27:43 2024 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 Reviewed-by: Michal Privoznik --- 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 From nobody Sun May 5 09:27:43 2024 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=1585235972; cv=none; d=zohomail.com; s=zohoarc; b=GFfQXDoiXzpsZvFDlCNY71kfsOvO8VX96LPAqYoNAIoBGtkMpcsuzRTKBX9JZdh1dWYJrJq4WLQOElT3MWmT9OTPjBwhoGb96cVkJJpsnScB+mj1sb832Y14kbgXhB1cIbgxRrya7z9hXHiK9UCWCcJfeesKo0DTq/RHNWrpXos= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585235972; 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=fzsQlJpDrJztWkakbcxS59E7KSAoT8DXNyfzhrR2/xg=; b=h42uLshv0NUiUaT5H18I8+Zq6zgsuZSmbXQ2kQDAsrub3T3YpPYKmTCaqlo5yhyRbovBv6fvAAUOO7m4zWgN/fDWOHDB6TIiL0ugNzQbtvRmjHuW+HjMiFipoOwVAYOeCzy+3qz9XMLw/dYBq9EcPRGhZsu/1HHPgyS3MePMA/4= 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 158523597289687.57910280295118; Thu, 26 Mar 2020 08:19:32 -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-302-zfLI9O2NPJ67pjKyMxp9ww-1; Thu, 26 Mar 2020 11:19:26 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5F632800EBD; 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 36143CDBC5; 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 D89B51800B74; Thu, 26 Mar 2020 15:19:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 02QFJJ5u029296 for ; Thu, 26 Mar 2020 11:19:19 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7150F107CD16; Thu, 26 Mar 2020 15:19:19 +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 6CBC0107CD19 for ; Thu, 26 Mar 2020 15:19:16 +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 9C3B68F7830 for ; Thu, 26 Mar 2020 15:19:16 +0000 (UTC) Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-100-ebV2qbSGOPGbemC0HHpAIw-1; Thu, 26 Mar 2020 11:19:14 -0400 Received: by mail-wm1-f52.google.com with SMTP id m3so7456386wmi.0 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.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2020 08:19:01 -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=fzsQlJpDrJztWkakbcxS59E7KSAoT8DXNyfzhrR2/xg=; b=QCQaNhvz0yE+6Sodkt9bH2K/y0lAMqp01cWq2seNTai4lJV4aKwMoyEeVDr6Rlph8YmGX1 fy1WdLef5RRIu9FlyTAfh5vsn6ywwvMcggc704iSt9fzUoKHaaeupD05uMqVQU7rmvMxlh NnP5kvbLoISpjNV+4Bl6UwU25FYbWgU= X-MC-Unique: zfLI9O2NPJ67pjKyMxp9ww-1 X-MC-Unique: ebV2qbSGOPGbemC0HHpAIw-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=kQXDNQx4JdT0FdU7JmEJZ7hGSqphCIdpjS6GbwWGUuc=; b=WHzjwDKzFl/nl5UFAdX+xfMQOc7qMpRzdiTgQWUoUQqobRnwSts52IG8iKzu8gn/+y xU2338QKUBIURVKFK3d+ikzTq5ESeWXe3au1CyQtfacD69hWPMuiFzGRtYGvgu3hR+DO g0qZOW1CHNzHpGEikOpbMH+EPnYEOGNZBjgdv9dFcF7RFSmjoGF05L0WHph98eGgeOAz 7jNjtCAVVFnqV6JJcSZlkYnOrXSvzfTH+Cz0XUuhrRkGAkgcIj91DCTOI7W9QXl0EFMS 7l7dnEs+8vDvJ0LTExMWn+xOo9ZSYqe06KfdA0AgTyWmgKOqbLvX74yQq6ScuPKjsRUC UC6w== X-Gm-Message-State: ANhLgQ1Ux2xqgq7m2cbr8FiFOfZEUlDLGRpLZUCPcw2ednfieOpAYvZw jTKpqJsonYqR+QWCB5UDB3kXyii0vXQ= X-Google-Smtp-Source: ADFU+vv/P+e8Vic53p7CGu+SVjNcwgZNsyPcQlPC8IXepkRFUAP03td+R4/ezhzu7bqzW30moF8rNA== X-Received: by 2002:a1c:7dc8:: with SMTP id y191mr389524wmc.167.1585235951524; Thu, 26 Mar 2020 08:19:11 -0700 (PDT) From: Rafael Fonseca To: libvir-list@redhat.com Subject: [PATCH 3/4] logging: use shared daemon startup code Date: Thu, 26 Mar 2020 16:18:02 +0100 Message-Id: <20200326151803.2349357-4-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.3 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 02QFJJ5u029296 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.11 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 Reviewed-by: Michal Privoznik --- src/logging/log_daemon.c | 239 +++++---------------------------------- 1 file changed, 29 insertions(+), 210 deletions(-) diff --git a/src/logging/log_daemon.c b/src/logging/log_daemon.c index 3319bf1f6f..dcafcda926 100644 --- a/src/logging/log_daemon.c +++ b/src/logging/log_daemon.c @@ -44,7 +44,7 @@ #include "viruuid.h" #include "virstring.h" #include "virgettext.h" -#include "virenum.h" +#include "virdaemon.h" =20 #include "log_daemon_dispatch.h" #include "log_protocol.h" @@ -65,36 +65,6 @@ virLogDaemonPtr logDaemon =3D NULL; =20 static bool execRestart; =20 -enum { - VIR_LOG_DAEMON_ERR_NONE =3D 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 -}; - -VIR_ENUM_DECL(virDaemonErr); -VIR_ENUM_IMPL(virDaemonErr, - VIR_LOG_DAEMON_ERR_LAST, - "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", - "Unable to re-execute daemon", -); - static void * virLogDaemonClientNew(virNetServerClientPtr client, void *opaque); @@ -294,122 +264,6 @@ virLogDaemonNewPostExecRestart(virJSONValuePtr object= , bool privileged, } =20 =20 -static int -virLogDaemonForkIntoBackground(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 -virLogDaemonUnixSocketPaths(bool privileged, - char **sockfile, - char **adminSockfile) -{ - if (privileged) { - *sockfile =3D g_strdup(RUNSTATEDIR "/libvirt/virtlogd-sock"); - *adminSockfile =3D g_strdup(RUNSTATEDIR "/libvirt/virtlogd-admin-s= ock"); - } 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/virtlogd-sock", rundir); - *adminSockfile =3D g_strdup_printf("%s/virtlogd-admin-sock", rundi= r); - } - return 0; -} - - static void virLogDaemonErrorHandler(void *opaque G_GNUC_UNUSED, virErrorPtr err G_GNUC_UNUSED) @@ -419,50 +273,6 @@ virLogDaemonErrorHandler(void *opaque G_GNUC_UNUSED, } =20 =20 -static void -virLogDaemonSetupLogging(virLogDaemonConfigPtr 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("virtlogd", godaemon, privileged); - - if (virLogGetNbOutputs() =3D=3D 0) - virLogSetOutputs(virLogGetDefaultOutput()); -} - - =20 /* Display version information. */ static void @@ -951,7 +761,13 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } =20 - virLogDaemonSetupLogging(config, privileged, verbose, godaemon); + virDaemonSetupLogging("virtlogd", + config->log_level, + config->log_filters, + config->log_outputs, + privileged, + verbose, + godaemon); =20 if (!pid_file && virPidFileConstructPath(privileged, @@ -963,9 +779,12 @@ int main(int argc, char **argv) { } VIR_DEBUG("Decided on pid file path '%s'", NULLSTR(pid_file)); =20 - if (virLogDaemonUnixSocketPaths(privileged, - &sock_file, - &admin_sock_file) < 0) { + if (virDaemonUnixSocketPaths("virtlogd", + privileged, + NULL, + &sock_file, + NULL, + &admin_sock_file) < 0) { VIR_ERROR(_("Can't determine socket paths")); exit(EXIT_FAILURE); } @@ -995,7 +814,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_LOG_DAEMON_ERR_RUNDIR; + ret =3D VIR_DAEMON_ERR_RUNDIR; umask(old_umask); goto cleanup; } @@ -1006,7 +825,7 @@ int main(int argc, char **argv) { &pid_file_fd, privileged, config)) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 @@ -1028,7 +847,7 @@ int main(int argc, char **argv) { goto cleanup; } =20 - if ((statuswrite =3D virLogDaemonForkIntoBackground(argv[0])) = < 0) { + if ((statuswrite =3D virDaemonForkIntoBackground(argv[0])) < 0= ) { VIR_ERROR(_("Failed to fork as daemon: %s"), g_strerror(errno)); goto cleanup; @@ -1037,19 +856,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_LOG_DAEMON_ERR_PIDFILE; + ret =3D VIR_DAEMON_ERR_PIDFILE; goto cleanup; } =20 if (!(logDaemon =3D virLogDaemonNew(config, privileged))) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 if (virSystemdGetActivation(actmap, G_N_ELEMENTS(actmap), &act) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } =20 @@ -1061,7 +880,7 @@ int main(int argc, char **argv) { sock_file, 0700, 0, 0, NULL, false, 0, 1) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } if (virNetServerAddServiceUNIX(adminSrv, @@ -1069,13 +888,13 @@ int main(int argc, char **argv) { admin_sock_file, 0700, 0, 0, NULL, false, 0, 1) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } =20 if (act && virSystemdActivationComplete(act) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_NETWORK; + ret =3D VIR_DAEMON_ERR_NETWORK; goto cleanup; } } else { @@ -1093,7 +912,7 @@ int main(int argc, char **argv) { } =20 if ((virLogDaemonSetupSignals(logDaemon->dmn)) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_SIGNAL; + ret =3D VIR_DAEMON_ERR_SIGNAL; goto cleanup; } =20 @@ -1101,11 +920,11 @@ int main(int argc, char **argv) { VIR_LOG_MANAGER_PROTOCOL_PRO= GRAM_VERSION, virLogManagerProtocolProcs, virLogManagerProtocolNProcs)= )) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } if (virNetServerAddProgram(logSrv, logProgram) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } =20 @@ -1114,11 +933,11 @@ int main(int argc, char **argv) { ADMIN_PROTOCOL_VERSION, adminProcs, adminNProcs))) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } if (virNetServerAddProgram(adminSrv, adminProgram) < 0) { - ret =3D VIR_LOG_DAEMON_ERR_INIT; + ret =3D VIR_DAEMON_ERR_INIT; goto cleanup; } } @@ -1147,7 +966,7 @@ int main(int argc, char **argv) { virLogDaemonPreExecRestart(state_file, logDaemon->dmn, argv) < 0) - ret =3D VIR_LOG_DAEMON_ERR_REEXEC; + ret =3D VIR_DAEMON_ERR_REEXEC; else ret =3D 0; =20 --=20 2.25.1 From nobody Sun May 5 09:27:43 2024 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 Reviewed-by: Michal Privoznik --- 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