From nobody Tue May 13 20:34:03 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1539767429405521.4033028249509; Wed, 17 Oct 2018 02:10:29 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7896EC051676; Wed, 17 Oct 2018 09:10:27 +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 309115F7CB; Wed, 17 Oct 2018 09:10:27 +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 D117A180B61F; Wed, 17 Oct 2018 09:10:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w9H97Ki7017214 for ; Wed, 17 Oct 2018 05:07:20 -0400 Received: by smtp.corp.redhat.com (Postfix) id 04DA058D3B; Wed, 17 Oct 2018 09:07:20 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7B29A43FF8 for ; Wed, 17 Oct 2018 09:07:16 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Wed, 17 Oct 2018 11:06:43 +0200 Message-Id: <90b0878bd471244a15989c9e8ea65e27ccd04d45.1539766897.git.mprivozn@redhat.com> In-Reply-To: References: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 01/13] virprocess: Introduce virProcessRunInFork 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 17 Oct 2018 09:10:28 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This new helper can be used to spawn a child process and run passed callback from it. This will come handy esp. if the callback is not thread safe. Signed-off-by: Michal Privoznik --- src/libvirt_private.syms | 1 + src/util/virprocess.c | 81 ++++++++++++++++++++++++++++++++++++++++ src/util/virprocess.h | 16 ++++++++ 3 files changed, 98 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 335210c31d..e2dc85310a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2625,6 +2625,7 @@ virProcessKill; virProcessKillPainfully; virProcessKillPainfullyDelay; virProcessNamespaceAvailable; +virProcessRunInFork; virProcessRunInMountNamespace; virProcessSchedPolicyTypeFromString; virProcessSchedPolicyTypeToString; diff --git a/src/util/virprocess.c b/src/util/virprocess.c index 3883c708fc..51b9ccb1bb 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -1165,6 +1165,87 @@ virProcessRunInMountNamespace(pid_t pid, } =20 =20 +static int +virProcessForkHelper(int errfd, + pid_t pid, + virProcessForkCallback cb, + void *opaque) +{ + if (cb(pid, opaque) < 0) { + virErrorPtr err =3D virGetLastError(); + if (err) { + size_t len =3D strlen(err->message) + 1; + ignore_value(safewrite(errfd, err->message, len)); + } + + return -1; + } + + return 0; +} + + +/** + * virProcessRunInFork: + * @cb: callback to run + * @opaque: opaque data to @cb + * + * Do the fork and run @cb in the child. This can be used when + * @cb does something thread unsafe, for instance. However, due + * to possible signal propagation @cb should only call async + * signal safe functions. On return, the returned value is either + * -1 with error message reported if something went bad in the + * parent, if child has died from a signal or if the child + * returned EXIT_CANCELED. Otherwise the returned value is the + * exit status of the child. + */ +int +virProcessRunInFork(virProcessForkCallback cb, + void *opaque) +{ + int ret =3D -1; + pid_t child =3D -1; + pid_t parent =3D getpid(); + int errfd[2] =3D { -1, -1 }; + + if (pipe2(errfd, O_CLOEXEC) < 0) { + virReportSystemError(errno, "%s", + _("Cannot create pipe for child")); + return -1; + } + + if ((child =3D virFork()) < 0) + goto cleanup; + + if (child =3D=3D 0) { + VIR_FORCE_CLOSE(errfd[0]); + ret =3D virProcessForkHelper(errfd[1], parent, cb, opaque); + VIR_FORCE_CLOSE(errfd[1]); + _exit(ret < 0 ? EXIT_CANCELED : ret); + } else { + int status; + VIR_AUTOFREE(char *) buf =3D NULL; + + VIR_FORCE_CLOSE(errfd[1]); + ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf)); + ret =3D virProcessWait(child, &status, false); + if (!ret) { + ret =3D status =3D=3D EXIT_CANCELED ? -1 : status; + if (ret) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("child reported: %s"), + NULLSTR(buf)); + } + } + } + + cleanup: + VIR_FORCE_CLOSE(errfd[0]); + VIR_FORCE_CLOSE(errfd[1]); + return ret; +} + + #if defined(HAVE_SYS_MOUNT_H) && defined(HAVE_UNSHARE) int virProcessSetupPrivateMountNS(void) diff --git a/src/util/virprocess.h b/src/util/virprocess.h index 5faa0892fe..1bae8c9212 100644 --- a/src/util/virprocess.h +++ b/src/util/virprocess.h @@ -93,6 +93,22 @@ int virProcessRunInMountNamespace(pid_t pid, virProcessNamespaceCallback cb, void *opaque); =20 +/** + * virProcessForkCallback: + * @pid: parent's pid + * @opaque: opaque data + * + * Callback to run in fork()-ed process. + * + * Returns: 0 on success, + * -1 on error (treated as EXIT_CANCELED) + */ +typedef int (*virProcessForkCallback)(pid_t pid, + void *opaque); + +int virProcessRunInFork(virProcessForkCallback cb, + void *opaque); + int virProcessSetupPrivateMountNS(void); =20 int virProcessSetScheduler(pid_t pid, --=20 2.18.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list