From nobody Mon Feb 9 11:29:50 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1646420531; cv=none; d=zohomail.com; s=zohoarc; b=hSmU36rp2/PbdHgSxtxYe2qj9ahdYTpWvce8lVO5BeNomn9ToxJr7rZ0EgnxSCkz/9waOY9TMBbX3VHzEj7QkkwATVPAcLVUrWU4nuHTVKnR3Xe+55crHRTcB25moQdxpstGpdwugiHB65Smk832T2qVj6FEa3m5B9WxZkshqHU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1646420531; 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=P8/CWiGiz0hdh+r6TmdIstp6r3DIyAuz85HXNoJ0imE=; b=WQ58fI4LXRhhaMYitXAr16vxgm/tr5lL7vzWAflJwwwIpLgaJZaD7vwh4iq8Fxfw6NwOQI3KBa1xbsw5Rb031FCPOuX5iZSIJYB5d/wmn/xZAxJ2t6DuFrqnJTKcxBbr92QEY9h0a+HvZxdGV92EGpDEbyooPtOE7azWpAWimng= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1646420531406304.8876991543069; Fri, 4 Mar 2022 11:02:11 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-gz_c2GABPUazKczJ0IST3g-1; Fri, 04 Mar 2022 14:02:06 -0500 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 50DC11091DA0; Fri, 4 Mar 2022 19:02:01 +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 2FC48604CC; Fri, 4 Mar 2022 19:02:01 +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 EDC5B4EE45; Fri, 4 Mar 2022 19:02:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 224J1xXb032289 for ; Fri, 4 Mar 2022 14:01:59 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9AF1A60C0F; Fri, 4 Mar 2022 19:01:59 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.39.194.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id B92F960BF1; Fri, 4 Mar 2022 19:01:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1646420530; 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=P8/CWiGiz0hdh+r6TmdIstp6r3DIyAuz85HXNoJ0imE=; b=ZrwunmvMroKQl0t6WyEBbCMTnU3GRW3/sd1n3L/AlvCUdY50f7RRM/RdxVonsw1YZEoyzf QrLAsyR7M3m69rSczUGO/K6Ohy2MAyS86HxTtLjK22GynA4DpTi+x0lw15Uz9CtgD7g9hn eFG+r/xYbfjKcOPytq2fO9jhjp18Cbg= X-MC-Unique: gz_c2GABPUazKczJ0IST3g-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Subject: [PATCH v2 7/8] softmmu: move parsing of -runas, -chroot and -daemonize code Date: Fri, 4 Mar 2022 18:56:19 +0000 Message-Id: <20220304185620.3272401-8-berrange@redhat.com> In-Reply-To: <20220304185620.3272401-1-berrange@redhat.com> References: <20220304185620.3272401-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Kevin Wolf , libvir-list@redhat.com, Stefan Weil , Hanna Reitz , Gerd Hoffmann , Paolo Bonzini , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Eric Blake 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1646420531876100001 With the future intent to try to move to a fully QAPI driven configuration system, we want to have any current command parsing well isolated from logic that applies the resulting configuration. We also don't want os-posix.c to contain code that is specific to the system emulators, as this file is linked to other binaries too. To satisfy these goals, we move parsing of the -runas, -chroot and -daemonize code out of the os-posix.c helper code, and pass the parsed data into APIs in os-posix.c. As a side benefit the 'os_daemonize()' function now lives up to its name and will always daemonize, instead of using global state to decide to be a no-op sometimes. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Eric Blake --- include/sysemu/os-posix.h | 4 +- include/sysemu/os-win32.h | 1 - os-posix.c | 148 +++++++++++--------------------------- os-win32.c | 9 --- softmmu/vl.c | 86 ++++++++++++++++++---- 5 files changed, 117 insertions(+), 131 deletions(-) diff --git a/include/sysemu/os-posix.h b/include/sysemu/os-posix.h index 2edf33658a..390f3f8396 100644 --- a/include/sysemu/os-posix.h +++ b/include/sysemu/os-posix.h @@ -46,7 +46,9 @@ void os_set_line_buffering(void); void os_set_proc_name(const char *s); void os_setup_signal_handling(void); void os_daemonize(void); -void os_setup_post(void); +void os_setup_post(const char *chroot_dir, + uid_t uid, gid_t gid, + const char *username); int os_mlock(void); =20 #define closesocket(s) close(s) diff --git a/include/sysemu/os-win32.h b/include/sysemu/os-win32.h index 43f569b5c2..4879f8731d 100644 --- a/include/sysemu/os-win32.h +++ b/include/sysemu/os-win32.h @@ -61,7 +61,6 @@ struct tm *localtime_r(const time_t *timep, struct tm *re= sult); =20 static inline void os_setup_signal_handling(void) {} static inline void os_daemonize(void) {} -static inline void os_setup_post(void) {} void os_set_line_buffering(void); static inline void os_set_proc_name(const char *dummy) {} =20 diff --git a/os-posix.c b/os-posix.c index 30da1a1491..f598a52a4f 100644 --- a/os-posix.c +++ b/os-posix.c @@ -42,11 +42,6 @@ #include #endif =20 -static char *user_name; -static uid_t user_uid =3D (uid_t)-1; -static gid_t user_gid =3D (gid_t)-1; - -static const char *chroot_dir; static int daemonize; static int daemon_pipe; =20 @@ -96,69 +91,6 @@ void os_set_proc_name(const char *s) } =20 =20 -static bool os_parse_runas_uid_gid(const char *optarg, - uid_t *runas_uid, gid_t *runas_gid) -{ - unsigned long lv; - const char *ep; - uid_t got_uid; - gid_t got_gid; - int rc; - - rc =3D qemu_strtoul(optarg, &ep, 0, &lv); - got_uid =3D lv; /* overflow here is ID in C99 */ - if (rc || *ep !=3D ':' || got_uid !=3D lv || got_uid =3D=3D (uid_t)-1)= { - return false; - } - - rc =3D qemu_strtoul(ep + 1, 0, 0, &lv); - got_gid =3D lv; /* overflow here is ID in C99 */ - if (rc || got_gid !=3D lv || got_gid =3D=3D (gid_t)-1) { - return false; - } - - *runas_uid =3D got_uid; - *runas_gid =3D got_gid; - return true; -} - -/* - * Parse OS specific command line options. - * return 0 if option handled, -1 otherwise - */ -int os_parse_cmd_args(int index, const char *optarg) -{ - struct passwd *user_pwd; - - switch (index) { - case QEMU_OPTION_runas: - user_pwd =3D getpwnam(optarg); - if (user_pwd) { - user_uid =3D user_pwd->pw_uid; - user_gid =3D user_pwd->pw_gid; - user_name =3D g_strdup(user_pwd->pw_name); - } else if (!os_parse_runas_uid_gid(optarg, - &user_uid, - &user_gid)) { - error_report("User \"%s\" doesn't exist" - " (and is not :)", - optarg); - exit(1); - } - break; - case QEMU_OPTION_chroot: - chroot_dir =3D optarg; - break; - case QEMU_OPTION_daemonize: - daemonize =3D 1; - break; - default: - return -1; - } - - return 0; -} - static void change_process_uid(uid_t uid, gid_t gid, const char *name) { if (setgid(gid) < 0) { @@ -202,54 +134,56 @@ static void change_root(const char *root) =20 void os_daemonize(void) { - if (daemonize) { - pid_t pid; - int fds[2]; + pid_t pid; + int fds[2]; =20 - if (pipe(fds) =3D=3D -1) { - exit(1); - } + if (pipe(fds) =3D=3D -1) { + exit(1); + } =20 - pid =3D fork(); - if (pid > 0) { - uint8_t status; - ssize_t len; + pid =3D fork(); + if (pid > 0) { + uint8_t status; + ssize_t len; =20 - close(fds[1]); + close(fds[1]); =20 - do { - len =3D read(fds[0], &status, 1); - } while (len < 0 && errno =3D=3D EINTR); + do { + len =3D read(fds[0], &status, 1); + } while (len < 0 && errno =3D=3D EINTR); =20 - /* only exit successfully if our child actually wrote - * a one-byte zero to our pipe, upon successful init */ - exit(len =3D=3D 1 && status =3D=3D 0 ? 0 : 1); + /* only exit successfully if our child actually wrote + * a one-byte zero to our pipe, upon successful init */ + exit(len =3D=3D 1 && status =3D=3D 0 ? 0 : 1); =20 - } else if (pid < 0) { - exit(1); - } + } else if (pid < 0) { + exit(1); + } =20 - close(fds[0]); - daemon_pipe =3D fds[1]; - qemu_set_cloexec(daemon_pipe); + close(fds[0]); + daemon_pipe =3D fds[1]; + qemu_set_cloexec(daemon_pipe); =20 - setsid(); + setsid(); =20 - pid =3D fork(); - if (pid > 0) { + pid =3D fork(); + if (pid > 0) { exit(0); - } else if (pid < 0) { - exit(1); - } - umask(027); - - signal(SIGTSTP, SIG_IGN); - signal(SIGTTOU, SIG_IGN); - signal(SIGTTIN, SIG_IGN); + } else if (pid < 0) { + exit(1); } + umask(027); + + signal(SIGTSTP, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + + daemonize =3D true; } =20 -void os_setup_post(void) +void os_setup_post(const char *root_dir, + uid_t runas_uid, gid_t runas_gid, + const char *runas_name) { int fd =3D 0; =20 @@ -264,11 +198,11 @@ void os_setup_post(void) } } =20 - if (chroot_dir) { - change_root(chroot_dir); + if (root_dir !=3D NULL) { + change_root(root_dir); } - if (user_uid !=3D -1 && user_gid !=3D -1) { - change_process_uid(user_uid, user_gid, user_name); + if (runas_uid !=3D -1 && runas_gid !=3D -1) { + change_process_uid(runas_uid, runas_gid, runas_name); } =20 if (daemonize) { diff --git a/os-win32.c b/os-win32.c index e31c921983..6f21b57841 100644 --- a/os-win32.c +++ b/os-win32.c @@ -61,12 +61,3 @@ void os_set_line_buffering(void) setbuf(stdout, NULL); setbuf(stderr, NULL); } - -/* - * Parse OS specific command line options. - * return 0 if option handled, -1 otherwise - */ -int os_parse_cmd_args(int index, const char *optarg) -{ - return -1; -} diff --git a/softmmu/vl.c b/softmmu/vl.c index 12b714795d..0bdd064451 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -2604,11 +2604,13 @@ static void qemu_process_help_options(void) } } =20 -static void qemu_maybe_daemonize(const char *pid_file) +static void qemu_maybe_daemonize(bool daemonize, const char *pid_file) { Error *err =3D NULL; =20 - os_daemonize(); + if (daemonize) { + os_daemonize(); + } rcu_disable_atfork(); =20 if (pid_file && !qemu_write_pidfile(pid_file, &err)) { @@ -2770,6 +2772,35 @@ void qmp_x_exit_preconfig(Error **errp) } } =20 +#ifndef WIN32 +static bool os_parse_runas_uid_gid(const char *optarg, + uid_t *runas_uid, + gid_t *runas_gid) +{ + unsigned long lv; + const char *ep; + uid_t got_uid; + gid_t got_gid; + int rc; + + rc =3D qemu_strtoul(optarg, &ep, 0, &lv); + got_uid =3D lv; /* overflow here is ID in C99 */ + if (rc || *ep !=3D ':' || got_uid !=3D lv || got_uid =3D=3D (uid_t)-1)= { + return false; + } + + rc =3D qemu_strtoul(ep + 1, 0, 0, &lv); + got_gid =3D lv; /* overflow here is ID in C99 */ + if (rc || got_gid !=3D lv || got_gid =3D=3D (gid_t)-1) { + return false; + } + + *runas_gid =3D got_gid; + *runas_uid =3D got_uid; + return true; +} +#endif /* !WIN32 */ + void qemu_init(int argc, char **argv, char **envp) { QemuOpts *opts; @@ -2780,6 +2811,14 @@ void qemu_init(int argc, char **argv, char **envp) MachineClass *machine_class; bool userconfig =3D true; FILE *vmstate_dump_file =3D NULL; + bool daemonize =3D false; +#ifndef WIN32 + struct passwd *pwd; + uid_t runas_uid =3D -1; + gid_t runas_gid =3D -1; + g_autofree char *runas_name =3D NULL; + const char *chroot_dir =3D NULL; +#endif /* !WIN32 */ =20 qemu_add_opts(&qemu_drive_opts); qemu_add_drive_opts(&qemu_legacy_drive_opts); @@ -3661,15 +3700,34 @@ void qemu_init(int argc, char **argv, char **envp) case QEMU_OPTION_nouserconfig: /* Nothing to be parsed here. Especially, do not error out= below. */ break; - default: - if (os_parse_cmd_args(popt->index, optarg)) { - error_report("Option not supported in this build"); +#ifndef WIN32 + case QEMU_OPTION_runas: + pwd =3D getpwnam(optarg); + if (pwd) { + runas_uid =3D pwd->pw_uid; + runas_gid =3D pwd->pw_gid; + runas_name =3D g_strdup(pwd->pw_name); + } else if (!os_parse_runas_uid_gid(optarg, + &runas_uid, + &runas_gid)) { + error_report("User \"%s\" doesn't exist" + " (and is not :)", + optarg); exit(1); } - if (is_daemonized()) { - qemu_log_stdio_disable(); - qemu_chr_stdio_disable(); - } + break; + case QEMU_OPTION_chroot: + chroot_dir =3D optarg; + break; + case QEMU_OPTION_daemonize: + daemonize =3D 1; + qemu_log_stdio_disable(); + qemu_chr_stdio_disable(); + break; +#endif /* !WIN32 */ + default: + error_report("Option not supported in this build"); + exit(1); } } } @@ -3679,7 +3737,7 @@ void qemu_init(int argc, char **argv, char **envp) */ loc_set_none(); =20 - qemu_validate_options(machine_opts_dict, is_daemonized()); + qemu_validate_options(machine_opts_dict, daemonize); qemu_process_sugar_options(); =20 /* @@ -3689,7 +3747,7 @@ void qemu_init(int argc, char **argv, char **envp) qemu_process_early_options(); =20 qemu_process_help_options(); - qemu_maybe_daemonize(pid_file); + qemu_maybe_daemonize(daemonize, pid_file); =20 /* * The trace backend must be initialized after daemonizing. @@ -3717,7 +3775,7 @@ void qemu_init(int argc, char **argv, char **envp) suspend_mux_open(); =20 qemu_disable_default_devices(); - qemu_create_default_devices(is_daemonized()); + qemu_create_default_devices(daemonize); qemu_create_early_backends(); =20 qemu_apply_legacy_machine_options(machine_opts_dict); @@ -3784,6 +3842,8 @@ void qemu_init(int argc, char **argv, char **envp) } qemu_init_displays(); accel_setup_post(current_machine); - os_setup_post(); +#ifndef WIN32 + os_setup_post(chroot_dir, runas_uid, runas_gid, runas_name); +#endif /* !WIN32 */ resume_mux_open(); } --=20 2.34.1