Skip to contentSkip to conversation

move softmmu options processing from os-posix.c to vl.c

RFC PATCH In review Michael Tokarev 2 participants 17 messages Aug 12, 2023 – Sep 1, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover move softmmu options processing from os-posix.c to vl.c

qemu_init() calls os_parse_cmd_args(), which is obviously a very vl.c-specicic piece of code. It looks like when moving vl.c to softmmu/, os-posix.c should've been moved too (together with os-win32.c). But there are other functions in os-posix.c which are used by other parts of the code, eg qemu-nbd or qemu-storage-daemon both uses parts from there. The result is rather ugly. One of the side-effects is that even if system build is disabled, we still have to compile util/async-teardown.c, since it is used in os-posix.c, - and this one can not be built on ia64 due to lack of clone syscall. But qemu-nbd does not need async-teardown, which is also very softmmu-specific.

This patchset tries to address these defects. It moves the "bottom half" of vl.c options processing back into vl.c, effectively undoing commit 59a5264b99434 from 2010. We do not have many os-specific options, and this move makes qemu_init() the only function which processes options, all in the single place, which is, in my opinion, easier to manage.

Before this move, lower-level constructs (chroot, runas) are being exported.

In the end, we have almost no softmmu-related things in os-posix.c, and the same functionality can be actually used in qemu-storage-daemon or qemu-nbd. For qemu-nbd, it is now possible to switch to using -runas/-chroot/etc the same way as qemu-storage-daemon does, instead of having one more abstraction named qemu_daemon().

Michael Tokarev (7):
include/sysemu/os-posix.h: move *daemonize* declaration together
os-posix: create and export os_set_runas()
os-posix.c: create and export os_set_chroot() os-posix.c, softmmu/vl.c: move os_parse_cmd_args() into qemu_init() softmmu/vl.c: inline include/qemu/qemu-options.h into vl.c os-posix.c: move code around
util/async-teardown.c: move to softmmu/, only build it when system
build is requested

 include/qemu/qemu-options.h        |  41 --------
 include/sysemu/os-posix.h          |   8 +-
 include/sysemu/os-win32.h          |   1 -
 os-posix.c                         | 152 +++++++++--------------------
 {util => softmmu}/async-teardown.c |   0
 softmmu/meson.build                |   1 +
 softmmu/vl.c                       |  87 ++++++++++++++++-
 util/meson.build                   |   1 -
 8 files changed, 134 insertions(+), 157 deletions(-)
 delete mode 100644 include/qemu/qemu-options.h
 rename {util => softmmu}/async-teardown.c (100%)
Signature · 1 line
2.39.2
#1 lore raw

RFC PATCH 1/7 include/sysemu/os-posix.h: move *daemonize* declaration together

---
 include/sysemu/os-posix.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
include/sysemu/os-posix.h +2 -3
index 1030d39904..65b9c94e91 100644
@@ -47,13 +47,12 @@ void os_set_line_buffering(void);
 void os_setup_early_signal_handling(void);
 void os_set_proc_name(const char *s);
 void os_setup_signal_handling(void);
+int os_set_daemonize(bool d);
+bool is_daemonized(void);
 void os_daemonize(void);
 void os_setup_post(void);
 int os_mlock(void);
 
-int os_set_daemonize(bool d);
-bool is_daemonized(void);
-
 /**
  * qemu_alloc_stack:
  * @sz: pointer to a size_t holding the requested usable stack size
Signature · 1 line
2.39.2
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH RFC 1/7] include/sysemu/os-posix.h: move *daemonize* declaration together

Quoted 4 lines · Michael Tokarev
---
 include/sysemu/os-posix.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
Quoted 25 lines
include/sysemu/os-posix.h +2 -3
index 1030d39904..65b9c94e91 100644
@@ -47,13 +47,12 @@ void os_set_line_buffering(void);
 void os_setup_early_signal_handling(void);
 void os_set_proc_name(const char *s);
 void os_setup_signal_handling(void);
+int os_set_daemonize(bool d);
+bool is_daemonized(void);
 void os_daemonize(void);
 void os_setup_post(void);
 int os_mlock(void);
 
-int os_set_daemonize(bool d);
-bool is_daemonized(void);
-
 /**
  * qemu_alloc_stack:
  * @sz: pointer to a size_t holding the requested usable stack size
Signature · 1 line
2.39.2
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#3 lore raw

Re: [PATCH RFC 1/7] include/sysemu/os-posix.h: move *daemonize* declaration together

Quoted 7 lines · Eric Blake
Quoted 4 lines · Michael Tokarev
---
  include/sysemu/os-posix.h | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

Eric, thank you very much for the review!
This is usually an ungrateful work.. :)

/mjt

#4 lore raw

RFC PATCH 2/7 os-posix: create and export os_set_runas()

In reply to Michael Tokarev
---
 include/sysemu/os-posix.h |  1 +
 os-posix.c                | 23 ++++++++++++++++-------
 2 files changed, 17 insertions(+), 7 deletions(-)
include/sysemu/os-posix.h +1 -0
index 65b9c94e91..d32630f9e7 100644
@@ -50,6 +50,7 @@ void os_setup_signal_handling(void);
 int os_set_daemonize(bool d);
 bool is_daemonized(void);
 void os_daemonize(void);
+bool os_set_runas(const char *optarg);
 void os_setup_post(void);
 int os_mlock(void);
 
os-posix.c +16 -7
index cfcb96533c..0202bb4898 100644
@@ -102,8 +102,14 @@ void os_set_proc_name(const char *s)
 #endif
 }
 
-
-static bool os_parse_runas_uid_gid(const char *optarg)
+/*
+ * Prepare to change user ID. optarg can be one of 3 forms:
+ *   - a username, in which case user ID will be changed to its uid,
+ *     with primary and supplementary groups set up too;
+ *   - a nemeric uid, in which case only the uid will be set;
+ *   - a pair of numeric uid:gid.
+ */
+bool os_set_runas(const char *optarg)
 {
     unsigned long lv;
     const char *ep;
@@ -111,6 +117,13 @@ static bool os_parse_runas_uid_gid(const char *optarg)
     gid_t got_gid;
     int rc;
 
+    user_pwd = getpwnam(optarg);
+    if (user_pwd) {
+        user_uid = -1;
+        user_gid = -1;
+        return true;
+    }
+
     rc = qemu_strtoul(optarg, &ep, 0, &lv);
     got_uid = lv; /* overflow here is ID in C99 */
     if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
@@ -137,11 +150,7 @@ int os_parse_cmd_args(int index, const char *optarg)
 {
     switch (index) {
     case QEMU_OPTION_runas:
-        user_pwd = getpwnam(optarg);
-        if (user_pwd) {
-            user_uid = -1;
-            user_gid = -1;
-        } else if (!os_parse_runas_uid_gid(optarg)) {
+        if (!os_set_runas(optarg)) {
             error_report("User \"%s\" doesn't exist"
                          " (and is not <uid>:<gid>)",
                          optarg);
Signature · 1 line
2.39.2
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH RFC 2/7] os-posix: create and export os_set_runas()

Quoted 6 lines · Michael Tokarev
---
 include/sysemu/os-posix.h |  1 +
 os-posix.c                | 23 ++++++++++++++++-------
 2 files changed, 17 insertions(+), 7 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#6 lore raw

RFC PATCH 3/7 os-posix.c: create and export os_set_chroot()

In reply to Michael Tokarev
---
 include/sysemu/os-posix.h | 1 +
 os-posix.c                | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
include/sysemu/os-posix.h +1 -0
index d32630f9e7..8a66763395 100644
@@ -51,6 +51,7 @@ int os_set_daemonize(bool d);
 bool is_daemonized(void);
 void os_daemonize(void);
 bool os_set_runas(const char *optarg);
+void os_set_chroot(const char *optarg);
 void os_setup_post(void);
 int os_mlock(void);
 
os-posix.c +7 -2
index 0202bb4898..081f3db685 100644
@@ -159,7 +159,7 @@ int os_parse_cmd_args(int index, const char *optarg)
         break;
     case QEMU_OPTION_chroot:
         warn_report("option is deprecated, use '-run-with chroot=...' instead");
-        chroot_dir = optarg;
+        os_set_chroot(optarg);
         break;
     case QEMU_OPTION_daemonize:
         daemonize = 1;
@@ -184,7 +184,7 @@ int os_parse_cmd_args(int index, const char *optarg)
 #endif
         str = qemu_opt_get(opts, "chroot");
         if (str) {
-            chroot_dir = str;
+            os_set_chroot(str);
         }
         break;
     }
@@ -232,6 +232,11 @@ static void change_process_uid(void)
     }
 }
 
+void os_set_chroot(const char *optarg)
+{
+    chroot_dir = optarg;
+}
+
 static void change_root(void)
 {
     if (chroot_dir) {
Signature · 1 line
2.39.2
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH RFC 3/7] os-posix.c: create and export os_set_chroot()

Quoted 6 lines · Michael Tokarev
---
 include/sysemu/os-posix.h | 1 +
 os-posix.c                | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#8 lore raw

RFC PATCH 4/7 os-posix.c, softmmu/vl.c: move os_parse_cmd_args() into qemu_init()

In reply to Michael Tokarev

This will stop linking softmmu-specific os_parse_cmd_args() into every qemu executable which happens to use other functions from os-posix.c, such as os_set_line_buffering() or os_setup_signal_handling().

Also, since there's no win32-specific options, *all* option parsing is now done in softmmu/vl.c:qemu_init(), which is easier to read without extra indirection, - all options are in the single function now.

This effectively reverts commit 59a5264b99434.

---
 include/sysemu/os-posix.h |  1 -
 include/sysemu/os-win32.h |  1 -
 os-posix.c                | 79 ---------------------------------------
 softmmu/vl.c              | 76 +++++++++++++++++++++++++++++++++++--
 4 files changed, 73 insertions(+), 84 deletions(-)
include/sysemu/os-posix.h +0 -1
index 8a66763395..6dfdcbb086 100644
@@ -42,7 +42,6 @@
 extern "C" {
 #endif
 
-int os_parse_cmd_args(int index, const char *optarg);
 void os_set_line_buffering(void);
 void os_setup_early_signal_handling(void);
 void os_set_proc_name(const char *s);
include/sysemu/os-win32.h +0 -1
index 91aa0d7ec0..8ae30fac15 100644
@@ -101,7 +101,6 @@ static inline void os_setup_signal_handling(void) {}
 static inline void os_daemonize(void) {}
 static inline void os_setup_post(void) {}
 static inline void os_set_proc_name(const char *dummy) {}
-static inline int os_parse_cmd_args(int index, const char *optarg) { return -1; }
 void os_set_line_buffering(void);
 void os_setup_early_signal_handling(void);
 
os-posix.c +0 -79

100 lines not on this page · raw message

softmmu/vl.c +73 -3

102 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v1 4/7
#9 lore raw

Re: [PATCH RFC 4/7] os-posix.c, softmmu/vl.c: move os_parse_cmd_args() into qemu_init()

Quoted 20 lines · Michael Tokarev

This will stop linking softmmu-specific os_parse_cmd_args() into every qemu executable which happens to use other functions from os-posix.c, such as os_set_line_buffering() or os_setup_signal_handling().

Also, since there's no win32-specific options, *all* option parsing is now done in softmmu/vl.c:qemu_init(), which is easier to read without extra indirection, - all options are in the single function now.

This effectively reverts commit 59a5264b99434.

---
 include/sysemu/os-posix.h |  1 -
 include/sysemu/os-win32.h |  1 -
 os-posix.c                | 79 ---------------------------------------
 softmmu/vl.c              | 76 +++++++++++++++++++++++++++++++++++--
 4 files changed, 73 insertions(+), 84 deletions(-)

+++ b/os-posix.c

os-posix.c bool os_set_runas(const char *optarg) · line 163

Quoted 4 lines
os-posix.c +0 -4
-    case QEMU_OPTION_chroot:
-        warn_report("option is deprecated, use '-run-with chroot=...' instead");
-        os_set_chroot(optarg);
-        break;

How long has this been deprecated (to make sure code motion still makes sense, rather than deletion)...

/me reads docs/about/deprecated.rst

Ah, just recently, in 8.1. Okay.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#10 lore raw

RFC PATCH 5/7 softmmu/vl.c: inline include/qemu/qemu-options.h into vl.c

In reply to Michael Tokarev

qemu-options.h just includes qemu-options.def with some #defines. We already do this in vl.c in other place. Since no other file includes qemu-options.h anymore, just inline it in vl.c.

This effectively reverts second half of commit 59a5264b99434.

---
 include/qemu/qemu-options.h | 41 -------------------------------------
 softmmu/vl.c                | 11 +++++++++-
 2 files changed, 10 insertions(+), 42 deletions(-)
 delete mode 100644 include/qemu/qemu-options.h
include/qemu/qemu-options.h deleted +0 -41
deleted file mode 100644
index 4a62c83c45..0000000000
@@ -1,41 +0,0 @@
-/*
- * qemu-options.h
- *
- * Defines needed for command line argument processing.
- *
- * Copyright (c) 2003-2008 Fabrice Bellard
- * Copyright (c) 2010 Jes Sorensen <[email protected]>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef QEMU_OPTIONS_H
-#define QEMU_OPTIONS_H
-
-enum {
-
-#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
-    opt_enum,
-#define DEFHEADING(text)
-#define ARCHHEADING(text, arch_mask)
-
-#include "qemu-options.def"
-};
-
-#endif
softmmu/vl.c +10 -1
index 0a74810ca3..78b6570f19 100644
@@ -91,7 +91,6 @@
 #include "qapi/qobject-input-visitor.h"
 #include "qemu/option.h"
 #include "qemu/config-file.h"
-#include "qemu/qemu-options.h"
 #include "qemu/main-loop.h"
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
@@ -894,6 +893,16 @@ static void help(int exitcode)
     exit(exitcode);
 }
 
+enum {
+
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#define ARCHHEADING(text, arch_mask)
+
+#include "qemu-options.def"
+};
+
 #define HAS_ARG 0x0001
 
 typedef struct QEMUOption {
Signature · 1 line
2.39.2
Reviewed-by on v1 5/7
#11 lore raw

Re: [PATCH RFC 5/7] softmmu/vl.c: inline include/qemu/qemu-options.h into vl.c

Quoted 4 lines · Michael Tokarev

qemu-options.h just includes qemu-options.def with some #defines. We already do this in vl.c in other place. Since no other file includes qemu-options.h anymore, just inline it in vl.c.

Quoted 8 lines

This effectively reverts second half of commit 59a5264b99434.

---
 include/qemu/qemu-options.h | 41 -------------------------------------
 softmmu/vl.c                | 11 +++++++++-
 2 files changed, 10 insertions(+), 42 deletions(-)
 delete mode 100644 include/qemu/qemu-options.h
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#12 lore raw

RFC PATCH 6/7 os-posix.c: move code around

In reply to Michael Tokarev

this moves code blocks so that functions and variables which belongs to the same concept are now close to each other. There's no actual code changes in there.

---
 os-posix.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)
os-posix.c +27 -22
index e4cdcf8839..76f0edc4f9 100644
@@ -43,17 +43,6 @@
 #include <sys/prctl.h>
 #endif
 
-/*
- * Must set all three of these at once.
- * Legal combinations are              unset   by name   by uid
- */
-static struct passwd *user_pwd;    /*   NULL   non-NULL   NULL   */
-static uid_t user_uid = (uid_t)-1; /*   -1      -1        >=0    */
-static gid_t user_gid = (gid_t)-1; /*   -1      -1        >=0    */
-
-static const char *chroot_dir;
-static int daemonize;
-static int daemon_pipe;
 
 void os_setup_early_signal_handling(void)
 {
@@ -100,6 +89,15 @@ void os_set_proc_name(const char *s)
 #endif
 }
 
+
+/*
+ * Must set all three of these at once.
+ * Legal combinations are              unset   by name   by uid
+ */
+static struct passwd *user_pwd;    /*   NULL   non-NULL   NULL   */
+static uid_t user_uid = (uid_t)-1; /*   -1      -1        >=0    */
+static gid_t user_gid = (gid_t)-1; /*   -1      -1        >=0    */
+
 /*
  * Prepare to change user ID. optarg can be one of 3 forms:
  *   - a username, in which case user ID will be changed to its uid,
@@ -177,6 +175,9 @@ static void change_process_uid(void)
     }
 }
 
+
+static const char *chroot_dir;
+
 void os_set_chroot(const char *optarg)
 {
     chroot_dir = optarg;
@@ -197,6 +198,21 @@ static void change_root(void)
 
 }
 
+
+static int daemonize;
+static int daemon_pipe;
+
+bool is_daemonized(void)
+{
+    return daemonize;
+}
+
+int os_set_daemonize(bool d)
+{
+    daemonize = d;
+    return 0;
+}
+
 void os_daemonize(void)
 {
     if (daemonize) {
@@ -290,17 +306,6 @@ void os_set_line_buffering(void)
     setvbuf(stdout, NULL, _IOLBF, 0);
 }
 
-bool is_daemonized(void)
-{
-    return daemonize;
-}
-
-int os_set_daemonize(bool d)
-{
-    daemonize = d;
-    return 0;
-}
-
 int os_mlock(void)
 {
 #ifdef HAVE_MLOCKALL
Signature · 1 line
2.39.2
Reviewed-by on v1 6/7
#13 lore raw

Re: [PATCH RFC 6/7] os-posix.c: move code around

Quoted 9 lines · Michael Tokarev

this moves code blocks so that functions and variables which belongs to the same concept are now close to each other. There's no actual code changes in there.

---
 os-posix.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#14 lore raw

RFC PATCH 7/7 util/async-teardown.c: move to softmmu/, only build it when system build is requested

In reply to Michael Tokarev
---
 {util => softmmu}/async-teardown.c | 0
 softmmu/meson.build                | 1 +
 util/meson.build                   | 1 -
 3 files changed, 1 insertion(+), 1 deletion(-)
 rename {util => softmmu}/async-teardown.c (100%)
softmmu/async-teardown.c renamed from util/async-teardown.c +0 -0
similarity index 100%
rename from util/async-teardown.c
rename to softmmu/async-teardown.c
softmmu/meson.build +1 -0
index ea5603f021..c18b7ad738 100644
@@ -37,3 +37,4 @@ endif
 
 system_ss.add(when: seccomp, if_true: files('qemu-seccomp.c'))
 system_ss.add(when: fdt, if_true: files('device_tree.c'))
+system_ss.add(when: 'CONFIG_LINUX', if_true: files('async-teardown.c'))
util/meson.build +0 -1
index a375160286..c4827fd70a 100644
@@ -3,7 +3,6 @@ util_ss.add(files('thread-context.c'), numa)
 if not config_host_data.get('CONFIG_ATOMIC64')
   util_ss.add(files('atomic64.c'))
 endif
-util_ss.add(when: 'CONFIG_LINUX', if_true: files('async-teardown.c'))
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('aio-posix.c'))
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('fdmon-poll.c'))
 if config_host_data.get('CONFIG_EPOLL_CREATE1')
Signature · 1 line
2.39.2
Reviewed-by on v1 7/7
#15 lore raw

Re: [PATCH RFC 7/7] util/async-teardown.c: move to softmmu/, only build it when system build is requested

Quoted 8 lines · Michael Tokarev
---
 {util => softmmu}/async-teardown.c | 0
 softmmu/meson.build                | 1 +
 util/meson.build                   | 1 -
 3 files changed, 1 insertion(+), 1 deletion(-)
 rename {util => softmmu}/async-teardown.c (100%)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#16 lore raw
In reply to Michael Tokarev
Quoted 16 lines · Michael Tokarev

qemu_init() calls os_parse_cmd_args(), which is obviously a very vl.c-specicic piece of code. It looks like when moving vl.c to softmmu/, os-posix.c should've been moved too (together with os-win32.c). But there are other functions in os-posix.c which are used by other parts of the code, eg qemu-nbd or qemu-storage-daemon both uses parts from there. The result is rather ugly. One of the side-effects is that even if system build is disabled, we still have to compile util/async-teardown.c, since it is used in os-posix.c, - and this one can not be built on ia64 due to lack of clone syscall. But qemu-nbd does not need async-teardown, which is also very softmmu-specific.

This patchset tries to address these defects. It moves the "bottom half" of vl.c options processing back into vl.c, effectively undoing commit 59a5264b99434 from 2010. We do not have many os-specific options, and this move makes qemu_init() the only function which processes options, all in the single place, which is, in my opinion, easier to manage.

Quoted 7 lines

Before this move, lower-level constructs (chroot, runas) are being exported.

In the end, we have almost no softmmu-related things in os-posix.c, and the same functionality can be actually used in qemu-storage-daemon or qemu-nbd. For qemu-nbd, it is now possible to switch to using -runas/-chroot/etc the same way as qemu-storage-daemon does, instead of having one more abstraction named qemu_daemon().

Given that qemu-nbd is affected, I'm happy to queue this through my NBD tree if no one else picks it up first.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org