Skip to contentSkip to conversation

clean up vl.c code

PATCH Superseded [email protected] 4 participants 41 messages Jun 3, 2010 – Jun 9, 2010 Kbuild

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover clean up vl.c code

From: Jes Sorensen <[email protected]>

Hi,

I have been working on a set of patches to clean up the vl.c code, by separating out OS specific code into OS specific files. Basically it introduces two header files: qemu-os-win32.h and qemu-os-posix.h as well as os-win32.c and os-posix.c.

I have tried to be as careful as I can to not break non Linux support, but as I only have a Linux build environment handy, I would appreciate it if people with other OSes could check that I didn't break anything for them. In particular I would like to know if win32 still builds.

Thanks,
Jes

Jes Sorensen (16):
vl.c: Remove double include of netinet/in.h for Solaris Create qemu-os-win32.h and move WIN32 specific declarations there Introduce os-win32.c and move polling functions from vl.c vl.c: Move host_main_loop_wait() to OS specific files. Introduce os-posix.c and create os_setup_signal_handling() Move win32 early signal handling setup to os_setup_signal_handling() Rename os_setup_signal_handling() to os_setup_early_signal_handling() Move main signal handler setup to os specificfiles.
Move find_datadir to OS specific files.
Introduce OS specific cmdline argument handling and move SMB arg to
os-posix.c
Move runas handling from vl.c to OS specific files.
Move chroot handling to OS specific files.
Move daemonize handling to OS specific files
Make os_change_process_uid and os_change_root os-posix.c local Move line-buffering setup to OS specific files.
Move set_proc_name() to OS specific files.

 Makefile.objs   |    2 +
 os-posix.c      |  344 ++++++++++++++++++++++++++++++++++++++
 os-win32.c      |  233 ++++++++++++++++++++++++++
 qemu-os-posix.h |   39 +++++
 qemu-os-win32.h |   52 ++++++
 sysemu.h        |   35 ++--
 vl.c            |  490 ++-----------------------------------------------------
 7 files changed, 703 insertions(+), 492 deletions(-)
 create mode 100644 os-posix.c
 create mode 100644 os-win32.c
 create mode 100644 qemu-os-posix.h
 create mode 100644 qemu-os-win32.h
#1 lore raw

PATCH 1/16 vl.c: Remove double include of netinet/in.h for Solaris

From: Jes Sorensen <[email protected]>

vl.c: netinet/in.h is already included once above for the generic non win32 code.

---
 vl.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
vl.c +0 -1
index 417554f..7c4298a 100644
@@ -70,7 +70,6 @@
 #include <sys/ethernet.h>
 #include <sys/sockio.h>
 #include <netinet/arp.h>
-#include <netinet/in.h>
 #include <netinet/in_systm.h>
 #include <netinet/ip.h>
 #include <netinet/ip_icmp.h> // must come after ip.h
Signature · 1 line
1.6.5.2
#2 lore raw

PATCH 2/16 Create qemu-os-win32.h and move WIN32 specific declarations there

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Create qemu-os-win32.h for WIN32 specific declarations. Move polling handling declaration into this file from sysemu.h

---
 qemu-os-win32.h |   43 +++++++++++++++++++++++++++++++++++++++++++
 sysemu.h        |   17 +----------------
 2 files changed, 44 insertions(+), 16 deletions(-)
 create mode 100644 qemu-os-win32.h
qemu-os-win32.h new file +43 -0
new file mode 100644
index 0000000..be108ad
@@ -0,0 +1,43 @@
+/*
+ * win32 specific declarations
+ *
+ * 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_OS_WIN32_H
+#define QEMU_OS_WIN32_H
+
+/* Polling handling */
+
+/* return TRUE if no sleep should be done afterwards */
+typedef int PollingFunc(void *opaque);
+
+int qemu_add_polling_cb(PollingFunc *func, void *opaque);
+void qemu_del_polling_cb(PollingFunc *func, void *opaque);
+
+/* Wait objects handling */
+typedef void WaitObjectFunc(void *opaque);
+
+int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
+void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
+
+#endif
sysemu.h +1 -16
index 879446a..13fc9a9 100644
@@ -9,6 +9,7 @@
 
 #ifdef _WIN32
 #include <windows.h>
+#include "qemu-os-win32.h"
 #endif
 
 /* vl.c */
@@ -71,22 +72,6 @@ int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f);
 void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f);
 int qemu_loadvm_state(QEMUFile *f);
 
-#ifdef _WIN32
-/* Polling handling */
-
-/* return TRUE if no sleep should be done afterwards */
-typedef int PollingFunc(void *opaque);
-
-int qemu_add_polling_cb(PollingFunc *func, void *opaque);
-void qemu_del_polling_cb(PollingFunc *func, void *opaque);
-
-/* Wait objects handling */
-typedef void WaitObjectFunc(void *opaque);
-
-int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
-void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
-#endif
-
 /* SLIRP */
 void do_info_slirp(Monitor *mon);
 
Signature · 1 line
1.6.5.2
#3 lore raw

PATCH 3/16 Introduce os-win32.c and move polling functions from vl.c

In reply to [email protected]

From: Jes Sorensen <[email protected]>

This introduces os-win32.c. It is meant to carry win32 specific functions thata are not relevant for all of QEMU as well as win32 versions of various pieces like signal handling etc.

Move win32 polling handler helper functions from vl.c to os-win32.c

---
 Makefile.objs |    1 +
 os-win32.c    |  111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c          |   80 -----------------------------------------
 3 files changed, 112 insertions(+), 80 deletions(-)
 create mode 100644 os-win32.c
Makefile.objs +1 -0
index 9796dcb..58fdb03 100644
@@ -144,6 +144,7 @@ hw-obj-$(CONFIG_ECC) += ecc.o
 hw-obj-$(CONFIG_NAND) += nand.o
 hw-obj-$(CONFIG_PFLASH_CFI01) += pflash_cfi01.o
 hw-obj-$(CONFIG_PFLASH_CFI02) += pflash_cfi02.o
+hw-obj-$(CONFIG_WIN32) += os-win32.o
 
 hw-obj-$(CONFIG_M48T59) += m48t59.o
 hw-obj-$(CONFIG_ESCC) += escc.o
os-win32.c new file +111 -0

111 lines not on this page · raw message

vl.c +0 -80

86 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#4 lore raw

PATCH 4/16 vl.c: Move host_main_loop_wait() to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move host_main_loop_wait() to OS specific files. Create qemu-os-posix.h and provide empty inline for the POSIX case.

---
 os-win32.c      |   43 +++++++++++++++++++++++++++++++++++++++++++
 qemu-os-posix.h |   33 +++++++++++++++++++++++++++++++++
 qemu-os-win32.h |    1 +
 sysemu.h        |    4 ++++
 vl.c            |   52 +---------------------------------------------------
 5 files changed, 82 insertions(+), 51 deletions(-)
 create mode 100644 qemu-os-posix.h
os-win32.c +43 -0
index 5a464cc..1f7e28b 100644
@@ -109,3 +109,46 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
     if (found)
         w->num--;
 }
+
+void os_host_main_loop_wait(int *timeout)
+{
+    int ret, ret2, i;
+    PollingEntry *pe;
+
+    /* XXX: need to suppress polling by better using win32 events */
+    ret = 0;
+    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
+        ret |= pe->func(pe->opaque);
+    }
+    if (ret == 0) {
+        int err;
+        WaitObjects *w = &wait_objects;
+
+        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
+        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
+            if (w->func[ret - WAIT_OBJECT_0])
+                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
+
+            /* Check for additional signaled events */
+            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
+
+                /* Check if event is signaled */
+                ret2 = WaitForSingleObject(w->events[i], 0);
+                if(ret2 == WAIT_OBJECT_0) {
+                    if (w->func[i])
+                        w->func[i](w->opaque[i]);
+                } else if (ret2 == WAIT_TIMEOUT) {
+                } else {
+                    err = GetLastError();
+                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
+                }
+            }
+        } else if (ret == WAIT_TIMEOUT) {
+        } else {
+            err = GetLastError();
+            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
+        }
+    }
+
+    *timeout = 0;
+}
qemu-os-posix.h new file +33 -0
new file mode 100644
index 0000000..96d1036
@@ -0,0 +1,33 @@
+/*
+ * posix specific declarations
+ *
+ * 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_OS_POSIX_H
+#define QEMU_OS_POSIX_H
+
+static inline void os_host_main_loop_wait(int *timeout)
+{
+}
+
+#endif
qemu-os-win32.h +1 -0
index be108ad..4d1cac8 100644
@@ -40,4 +40,5 @@ typedef void WaitObjectFunc(void *opaque);
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 
+void os_host_main_loop_wait(int *timeout);
 #endif
sysemu.h +4 -0
index 13fc9a9..5e4feae 100644
@@ -12,6 +12,10 @@
 #include "qemu-os-win32.h"
 #endif
 
+#ifdef CONFIG_POSIX
+#include "qemu-os-posix.h"
+#endif
+
 /* vl.c */
 extern const char *bios_name;
 
vl.c +1 -51

64 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#5 lore raw

PATCH 5/16 Introduce os-posix.c and create os_setup_signal_handling()

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Introcuce os-posix.c and move posix specific signal handling there.

---
 Makefile.objs |    1 +
 os-posix.c    |   41 +++++++++++++++++++++++++++++++++++++++++
 sysemu.h      |    3 +++
 vl.c          |    8 +-------
 4 files changed, 46 insertions(+), 7 deletions(-)
 create mode 100644 os-posix.c
Makefile.objs +1 -0
index 58fdb03..2d94677 100644
@@ -145,6 +145,7 @@ hw-obj-$(CONFIG_NAND) += nand.o
 hw-obj-$(CONFIG_PFLASH_CFI01) += pflash_cfi01.o
 hw-obj-$(CONFIG_PFLASH_CFI02) += pflash_cfi02.o
 hw-obj-$(CONFIG_WIN32) += os-win32.o
+hw-obj-$(CONFIG_POSIX) += os-posix.o
 
 hw-obj-$(CONFIG_M48T59) += m48t59.o
 hw-obj-$(CONFIG_ESCC) += escc.o
os-posix.c new file +41 -0

41 lines not on this page · raw message

sysemu.h +3 -0
index 5e4feae..fc438c5 100644
@@ -79,6 +79,9 @@ int qemu_loadvm_state(QEMUFile *f);
 /* SLIRP */
 void do_info_slirp(Monitor *mon);
 
+/* OS specific functions */
+void os_setup_signal_handling(void);
+
 typedef enum DisplayType
 {
     DT_DEFAULT,
vl.c +1 -7
index c655582..7a46fee 100644
@@ -2460,13 +2460,7 @@ int main(int argc, char **argv, char **envp)
 
     QLIST_INIT (&vm_change_state_head);
 #ifndef _WIN32
-    {
-        struct sigaction act;
-        sigfillset(&act.sa_mask);
-        act.sa_flags = 0;
-        act.sa_handler = SIG_IGN;
-        sigaction(SIGPIPE, &act, NULL);
-    }
+    os_setup_signal_handling();
 #else
     SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
     /* Note: cpu_interrupt() is currently not SMP safe, so we force
Signature · 1 line
1.6.5.2
#6 lore raw

Re: [Qemu-devel] [PATCH 05/16] Introduce os-posix.c and create os_setup_signal_handling()

sysemu.h int qemu_loadvm_state(QEMUFile *f); · line 84

Quoted 9 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
sysemu.h +3 -0
@@ -79,6 +79,9 @@ int qemu_loadvm_state(QEMUFile *f);
 /* SLIRP */
 void do_info_slirp(Monitor *mon);
 
+/* OS specific functions */
+void os_setup_signal_handling(void);
+

Can this go in your qemu-os-posix.h?

r~

#7 lore raw

Re: [Qemu-devel] [PATCH 05/16] Introduce os-posix.c and create os_setup_signal_handling()

Quoted 7 lines · Richard Henderson
Quoted 6 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
sysemu.h +0 -0
@@ -79,6 +79,9 @@ int qemu_loadvm_state(QEMUFile *f);
 /* SLIRP */
 void do_info_slirp(Monitor *mon);
 
Quoted 5 lines

sysemu.h int qemu_loadvm_state(QEMUFile *f); · line 84

Quoted 3 lines
sysemu.h +3 -0
+/* OS specific functions */
+void os_setup_signal_handling(void);
+

Can this go in your qemu-os-posix.h?

Seems reasonable, must be a leftover from earlier.

Cheers,
Jes

#8 lore raw

PATCH 6/16 Move win32 early signal handling setup to os_setup_signal_handling()

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move win32 early signal handling setup to os_setup_signal_handling()

---
 os-win32.c |   29 +++++++++++++++++++++++++++++
 vl.c       |   30 ------------------------------
 2 files changed, 29 insertions(+), 30 deletions(-)
os-win32.c +29 -0
index 1f7e28b..dfa90bc 100644
@@ -152,3 +152,32 @@ void os_host_main_loop_wait(int *timeout)
 
     *timeout = 0;
 }
+
+static BOOL WINAPI qemu_ctrl_handler(DWORD type)
+{
+    exit(STATUS_CONTROL_C_EXIT);
+    return TRUE;
+}
+
+void os_setup_signal_handling(void)
+{
+    /* Note: cpu_interrupt() is currently not SMP safe, so we force
+       QEMU to run on a single CPU */
+    HANDLE h;
+    DWORD mask, smask;
+    int i;
+
+    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
+
+    h = GetCurrentProcess();
+    if (GetProcessAffinityMask(h, &mask, &smask)) {
+        for(i = 0; i < 32; i++) {
+            if (mask & (1 << i))
+                break;
+        }
+        if (i != 32) {
+            mask = 1 << i;
+            SetProcessAffinityMask(h, mask);
+        }
+    }
+}
vl.c +0 -30
index 7a46fee..f43456a 100644
@@ -1986,14 +1986,6 @@ static int balloon_parse(const char *arg)
     return -1;
 }
 
-#ifdef _WIN32
-static BOOL WINAPI qemu_ctrl_handler(DWORD type)
-{
-    exit(STATUS_CONTROL_C_EXIT);
-    return TRUE;
-}
-#endif
-
 #ifndef _WIN32
 
 static void termsig_handler(int signal)
@@ -2459,29 +2451,7 @@ int main(int argc, char **argv, char **envp)
     qemu_cache_utils_init(envp);
 
     QLIST_INIT (&vm_change_state_head);
-#ifndef _WIN32
     os_setup_signal_handling();
-#else
-    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
-    /* Note: cpu_interrupt() is currently not SMP safe, so we force
-       QEMU to run on a single CPU */
-    {
-        HANDLE h;
-        DWORD mask, smask;
-        int i;
-        h = GetCurrentProcess();
-        if (GetProcessAffinityMask(h, &mask, &smask)) {
-            for(i = 0; i < 32; i++) {
-                if (mask & (1 << i))
-                    break;
-            }
-            if (i != 32) {
-                mask = 1 << i;
-                SetProcessAffinityMask(h, mask);
-            }
-        }
-    }
-#endif
 
     module_call_init(MODULE_INIT_MACHINE);
     machine = find_default_machine();
Signature · 1 line
1.6.5.2
#9 lore raw

PATCH 7/16 Rename os_setup_signal_handling() to os_setup_early_signal_handling()

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Rename os_setup_signal_handling() to os_setup_early_signal_handling()

---
 os-posix.c |    2 +-
 os-win32.c |    2 +-
 sysemu.h   |    2 +-
 vl.c       |    2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
os-posix.c +1 -1
index 914a4d1..948f662 100644
@@ -31,7 +31,7 @@
 #include "config-host.h"
 #include "sysemu.h"
 
-void os_setup_signal_handling(void)
+void os_setup_early_signal_handling(void)
 {
     struct sigaction act;
     sigfillset(&act.sa_mask);
os-win32.c +1 -1
index dfa90bc..a936f7a 100644
@@ -159,7 +159,7 @@ static BOOL WINAPI qemu_ctrl_handler(DWORD type)
     return TRUE;
 }
 
-void os_setup_signal_handling(void)
+void os_setup_early_signal_handling(void)
 {
     /* Note: cpu_interrupt() is currently not SMP safe, so we force
        QEMU to run on a single CPU */
sysemu.h +1 -1
index fc438c5..79ffd9f 100644
@@ -80,7 +80,7 @@ int qemu_loadvm_state(QEMUFile *f);
 void do_info_slirp(Monitor *mon);
 
 /* OS specific functions */
-void os_setup_signal_handling(void);
+void os_setup_early_signal_handling(void);
 
 typedef enum DisplayType
 {
vl.c +1 -1
index f43456a..372f931 100644
@@ -2451,7 +2451,7 @@ int main(int argc, char **argv, char **envp)
     qemu_cache_utils_init(envp);
 
     QLIST_INIT (&vm_change_state_head);
-    os_setup_signal_handling();
+    os_setup_early_signal_handling();
 
     module_call_init(MODULE_INIT_MACHINE);
     machine = find_default_machine();
Signature · 1 line
1.6.5.2
#10 lore raw

PATCH 8/16 Move main signal handler setup to os specificfiles.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move main signal handler setup to os specific files.

---
 os-posix.c      |   27 +++++++++++++++++++++++++++
 qemu-os-posix.h |    2 ++
 qemu-os-win32.h |    3 +++
 vl.c            |   33 +--------------------------------
 4 files changed, 33 insertions(+), 32 deletions(-)
os-posix.c +27 -0
index 948f662..01dbec2 100644
@@ -26,6 +26,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 /* Needed early for CONFIG_BSD etc. */
 #include "config-host.h"
@@ -39,3 +41,28 @@ void os_setup_early_signal_handling(void)
     act.sa_handler = SIG_IGN;
     sigaction(SIGPIPE, &act, NULL);
 }
+
+static void termsig_handler(int signal)
+{
+    qemu_system_shutdown_request();
+}
+
+static void sigchld_handler(int signal)
+{
+    waitpid(-1, NULL, WNOHANG);
+}
+
+void os_setup_signal_handling(void)
+{
+    struct sigaction act;
+
+    memset(&act, 0, sizeof(act));
+    act.sa_handler = termsig_handler;
+    sigaction(SIGINT,  &act, NULL);
+    sigaction(SIGHUP,  &act, NULL);
+    sigaction(SIGTERM, &act, NULL);
+
+    act.sa_handler = sigchld_handler;
+    act.sa_flags = SA_NOCLDSTOP;
+    sigaction(SIGCHLD, &act, NULL);
+}
qemu-os-posix.h +2 -0
index 96d1036..ff5adb1 100644
@@ -30,4 +30,6 @@ static inline void os_host_main_loop_wait(int *timeout)
 {
 }
 
+void os_setup_signal_handling(void);
+
 #endif
qemu-os-win32.h +3 -0
index 4d1cac8..4343c6d 100644
@@ -41,4 +41,7 @@ int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 
 void os_host_main_loop_wait(int *timeout);
+
+static inline void os_setup_signal_handling(void) {};
+
 #endif
vl.c +1 -32
index 372f931..fc5e8d8 100644
@@ -1986,35 +1986,6 @@ static int balloon_parse(const char *arg)
     return -1;
 }
 
-#ifndef _WIN32
-
-static void termsig_handler(int signal)
-{
-    qemu_system_shutdown_request();
-}
-
-static void sigchld_handler(int signal)
-{
-    waitpid(-1, NULL, WNOHANG);
-}
-
-static void sighandler_setup(void)
-{
-    struct sigaction act;
-
-    memset(&act, 0, sizeof(act));
-    act.sa_handler = termsig_handler;
-    sigaction(SIGINT,  &act, NULL);
-    sigaction(SIGHUP,  &act, NULL);
-    sigaction(SIGTERM, &act, NULL);
-
-    act.sa_handler = sigchld_handler;
-    act.sa_flags = SA_NOCLDSTOP;
-    sigaction(SIGCHLD, &act, NULL);
-}
-
-#endif
-
 #ifdef _WIN32
 /* Look for support files in the same directory as the executable.  */
 static char *find_datadir(const char *argv0)
@@ -3556,10 +3527,8 @@ int main(int argc, char **argv, char **envp)
 
     cpu_synchronize_all_post_init();
 
-#ifndef _WIN32
     /* must be after terminal init, SDL library changes signal handlers */
-    sighandler_setup();
-#endif
+    os_setup_signal_handling();
 
     set_numa_modes();
 
Signature · 1 line
1.6.5.2
#11 lore raw

Re: [Qemu-devel] [PATCH 08/16] Move main signal handler setup to os specificfiles.

qemu-os-win32.h int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque); · line 45

Quoted 8 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
qemu-os-win32.h +2 -0
@@ -41,4 +41,7 @@ int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 
 void os_host_main_loop_wait(int *timeout);
+
+static inline void os_setup_signal_handling(void) {};

Stray ;

r~

#12 lore raw

Re: [Qemu-devel] [PATCH 08/16] Move main signal handler setup to os specificfiles.

Quoted 11 lines · Richard Henderson

qemu-os-win32.h int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque); · line 45

Quoted 8 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
qemu-os-win32.h +2 -0
@@ -41,4 +41,7 @@ int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 
 void os_host_main_loop_wait(int *timeout);
+
+static inline void os_setup_signal_handling(void) {};

Stray ;

Sorry, not sure what you mean here?

Jes

#13 lore raw

Re: [Qemu-devel] [PATCH 08/16] Move main signal handler setup to os specificfiles.

Quoted 7 lines · Jes Sorensen
Quoted 6 lines · Richard Henderson
Quoted 5 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
qemu-os-win32.h +0 -0
@@ -41,4 +41,7 @@ int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 
Quoted 7 lines
Quoted 5 lines

qemu-os-win32.h int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque); · line 45

Quoted 3 lines
qemu-os-win32.h +2 -0
 void os_host_main_loop_wait(int *timeout);
+
+static inline void os_setup_signal_handling(void) {};

Stray ;

Sorry, not sure what you mean here?

There's a stray ';' after the function body's closing brace. Please drop it.

#14 lore raw

PATCH 9/16 Move find_datadir to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

This moves the win32 and POSIX versions of find_datadir() to OS specific files, and removes some #ifdef clutter from vl.c

---
 os-posix.c |   64 +++++++++++++++++++++++++++++++++++++++
 os-win32.c |   23 ++++++++++++++
 sysemu.h   |    1 +
 vl.c       |   98 ++---------------------------------------------------------
 4 files changed, 92 insertions(+), 94 deletions(-)
os-posix.c +64 -0
index 01dbec2..621ad06 100644
@@ -28,6 +28,7 @@
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <libgen.h>
 
 /* Needed early for CONFIG_BSD etc. */
 #include "config-host.h"
@@ -66,3 +67,66 @@ void os_setup_signal_handling(void)
     act.sa_flags = SA_NOCLDSTOP;
     sigaction(SIGCHLD, &act, NULL);
 }
+
+/* Find a likely location for support files using the location of the binary.
+   For installed binaries this will be "$bindir/../share/qemu".  When
+   running from the build tree this will be "$bindir/../pc-bios".  */
+#define SHARE_SUFFIX "/share/qemu"
+#define BUILD_SUFFIX "/pc-bios"
+char *os_find_datadir(const char *argv0)
+{
+    char *dir;
+    char *p = NULL;
+    char *res;
+    char buf[PATH_MAX];
+    size_t max_len;
+
+#if defined(__linux__)
+    {
+        int len;
+        len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
+        if (len > 0) {
+            buf[len] = 0;
+            p = buf;
+        }
+    }
+#elif defined(__FreeBSD__)
+    {
+        static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
+        size_t len = sizeof(buf) - 1;
+
+        *buf = '\0';
+        if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
+            *buf) {
+            buf[sizeof(buf) - 1] = '\0';
+            p = buf;
+        }
+    }
+#endif
+    /* If we don't have any way of figuring out the actual executable
+       location then try argv[0].  */
+    if (!p) {
+        p = realpath(argv0, buf);
+        if (!p) {
+            return NULL;
+        }
+    }
+    dir = dirname(p);
+    dir = dirname(dir);
+
+    max_len = strlen(dir) +
+        MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
+    res = qemu_mallocz(max_len);
+    snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
+    if (access(res, R_OK)) {
+        snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
+        if (access(res, R_OK)) {
+            qemu_free(res);
+            res = NULL;
+        }
+    }
+
+    return res;
+}
+#undef SHARE_SUFFIX
+#undef BUILD_SUFFIX
os-win32.c +23 -0
index a936f7a..1758538 100644
@@ -181,3 +181,26 @@ void os_setup_early_signal_handling(void)
         }
     }
 }
+
+/* Look for support files in the same directory as the executable.  */
+char *os_find_datadir(const char *argv0)
+{
+    char *p;
+    char buf[MAX_PATH];
+    DWORD len;
+
+    len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
+    if (len == 0) {
+        return NULL;
+    }
+
+    buf[len] = 0;
+    p = buf + len - 1;
+    while (p != buf && *p != '\\')
+        p--;
+    *p = 0;
+    if (access(buf, R_OK) == 0) {
+        return qemu_strdup(buf);
+    }
+    return NULL;
+}
sysemu.h +1 -0
index 79ffd9f..72f3734 100644
@@ -81,6 +81,7 @@ void do_info_slirp(Monitor *mon);
 
 /* OS specific functions */
 void os_setup_early_signal_handling(void);
+char *os_find_datadir(const char *argv0);
 
 typedef enum DisplayType
 {
vl.c +4 -94

110 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#15 lore raw

PATCH 10/16 Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Introduce OS specific cmdline argument handling by calling os_parse_cmd_args() at the end of switch() statement.

In addition move SMB argument to os-posix.c

---
 os-posix.c |   34 ++++++++++++++++++++++++++++++++++
 os-win32.c |   22 ++++++++++++++++++++++
 sysemu.h   |    9 +++++++++
 vl.c       |   15 ++-------------
 4 files changed, 67 insertions(+), 13 deletions(-)
os-posix.c +34 -0
index 621ad06..66f2bf5 100644
@@ -33,6 +33,7 @@
 /* Needed early for CONFIG_BSD etc. */
 #include "config-host.h"
 #include "sysemu.h"
+#include "net/slirp.h"
 
 void os_setup_early_signal_handling(void)
 {
@@ -130,3 +131,36 @@ char *os_find_datadir(const char *argv0)
 }
 #undef SHARE_SUFFIX
 #undef BUILD_SUFFIX
+
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};
+
+/*
+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    int ret = 0;
+    switch (popt->index) {
+#ifdef CONFIG_SLIRP
+    case QEMU_OPTION_smb:
+        if (net_slirp_smb(optarg) < 0)
+            exit(1);
+        break;
+#endif
+    default:
+        ret = -1;
+    }
+    return ret;
+}
os-win32.c +22 -0
index 1758538..a311a90 100644
@@ -204,3 +204,25 @@ char *os_find_datadir(const char *argv0)
     }
     return NULL;
 }
+
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};
+
+/*
+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    return -1;
+}
sysemu.h +9 -0
index 72f3734..08ec323 100644
@@ -79,9 +79,18 @@ int qemu_loadvm_state(QEMUFile *f);
 /* SLIRP */
 void do_info_slirp(Monitor *mon);
 
+/* This is needed for vl.c and the OS specific files */
+typedef struct QEMUOption {
+    const char *name;
+    int flags;
+    int index;
+    uint32_t arch_mask;
+} QEMUOption;
+
 /* OS specific functions */
 void os_setup_early_signal_handling(void);
 char *os_find_datadir(const char *argv0);
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg);
 
 typedef enum DisplayType
 {
vl.c +2 -13
index 7f22733..838e109 100644
@@ -1909,13 +1909,6 @@ enum {
 #undef GEN_DOCS
 };
 
-typedef struct QEMUOption {
-    const char *name;
-    int flags;
-    int index;
-    uint32_t arch_mask;
-} QEMUOption;
-
 static const QEMUOption qemu_options[] = {
     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
@@ -2624,12 +2617,6 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_bootp:
                 legacy_bootp_filename = optarg;
                 break;
-#ifndef _WIN32
-            case QEMU_OPTION_smb:
-                if (net_slirp_smb(optarg) < 0)
-                    exit(1);
-                break;
-#endif
             case QEMU_OPTION_redir:
                 if (net_slirp_redir(optarg) < 0)
                     exit(1);
@@ -3126,6 +3113,8 @@ int main(int argc, char **argv, char **envp)
                     fclose(fp);
                     break;
                 }
+            default:
+                os_parse_cmd_args(popt, optarg);
             }
         }
     }
Signature · 1 line
1.6.5.2
#16 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 12 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
os-posix.c +12 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

There's no header file you can put this in? Or invent to put this in? Cause this is really kinda gross...

Quoted 10 lines
os-posix.c +10 -0
+
+/*
+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    int ret = 0;
+    switch (popt->index) {
+#ifdef CONFIG_SLIRP

os-posix.c char *os_find_datadir(const char *argv0) · line 166

Quoted 10 lines
os-posix.c +10 -0
+    case QEMU_OPTION_smb:
+        if (net_slirp_smb(optarg) < 0)
+            exit(1);
+        break;
+#endif
+    default:
+        ret = -1;
+    }
+    return ret;
+}

Why have a return value at all...

vl.c int main(int argc, char **argv, char **envp) · line 3117

Quoted 2 lines
vl.c +2 -0
+            default:
+                os_parse_cmd_args(popt, optarg);

... if you're going to ignore the results?

r~

#17 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 17 lines · Richard Henderson

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 12 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
os-posix.c +12 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

There's no header file you can put this in? Or invent to put this in? Cause this is really kinda gross...

The problem is that it requires qemu-options.h to be included, which isn't included per default for all the files. If I put it into sysemu.h at least it's going to require making every .c file build with those flags.

I agree it's gross, but I am not sure what would be a better solution.

Quoted 6 lines

os-posix.c char *os_find_datadir(const char *argv0) · line 166

Quoted 5 lines
os-posix.c +5 -0
+    default:
+        ret = -1;
+    }
+    return ret;
+}
Quoted 6 lines

Why have a return value at all...

vl.c int main(int argc, char **argv, char **envp) · line 3117

Quoted 2 lines
vl.c +2 -0
+            default:
+                os_parse_cmd_args(popt, optarg);

... if you're going to ignore the results?

I was trying to make it forward looking, but yeah we can just kill that.

Cheers,
Jes

#18 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 19 lines · Jes Sorensen
Quoted 17 lines · Richard Henderson

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 12 lines · On 06/03/2010 09:48 AM, [email protected] wrote:
os-posix.c +12 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

There's no header file you can put this in? Or invent to put this in? Cause this is really kinda gross...

Quoted 5 lines

The problem is that it requires qemu-options.h to be included, which isn't included per default for all the files. If I put it into sysemu.h at least it's going to require making every .c file build with those flags.

I agree it's gross, but I am not sure what would be a better solution.

One possible solution is to put this whole block in "qemu-options-enum.h" (or whatever) and include that in the three places that you have this block.

r~

#19 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 9 lines · Richard Henderson
Quoted 5 lines · Jes Sorensen

The problem is that it requires qemu-options.h to be included, which isn't included per default for all the files. If I put it into sysemu.h at least it's going to require making every .c file build with those flags.

I agree it's gross, but I am not sure what would be a better solution.

One possible solution is to put this whole block in "qemu-options-enum.h" (or whatever) and include that in the three places that you have this block.

I ended up moving qemu-options.h to qemu-options.def and then creating a proper qemu-options.h per Paolo's suggestion.

Let me know what you think of the v2 patch set, minus the duplicates some idiot posted because he forgot to remove the Emacs backup files :)

Cheers,
Jes

#20 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

In reply to [email protected]
Quoted 49 lines · [email protected] writes:

From: Jes Sorensen <[email protected]>

Introduce OS specific cmdline argument handling by calling os_parse_cmd_args() at the end of switch() statement.

In addition move SMB argument to os-posix.c

---
 os-posix.c |   34 ++++++++++++++++++++++++++++++++++
 os-win32.c |   22 ++++++++++++++++++++++
 sysemu.h   |    9 +++++++++
 vl.c       |   15 ++-------------
 4 files changed, 67 insertions(+), 13 deletions(-)
os-posix.c +19 -0
index 621ad06..66f2bf5 100644
@@ -33,6 +33,7 @@
 /* Needed early for CONFIG_BSD etc. */
 #include "config-host.h"
 #include "sysemu.h"
+#include "net/slirp.h"
 
 void os_setup_early_signal_handling(void)
 {
@@ -130,3 +131,36 @@ char *os_find_datadir(const char *argv0)
 }
 #undef SHARE_SUFFIX
 #undef BUILD_SUFFIX
+
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};
+
+/*
+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */

os-posix.c char *os_find_datadir(const char *argv0) · line 161

Quoted 10 lines
os-posix.c +10 -0
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    int ret = 0;
+    switch (popt->index) {
+#ifdef CONFIG_SLIRP
+    case QEMU_OPTION_smb:
+        if (net_slirp_smb(optarg) < 0)
+            exit(1);
+        break;
+#endif

Was #ifndef _WIN32 before. Impact?

Quoted 16 lines
os-posix.c +5 -0
+    default:
+        ret = -1;
+    }
+    return ret;
+}
os-win32.c +3 -0
index 1758538..a311a90 100644
@@ -204,3 +204,25 @@ char *os_find_datadir(const char *argv0)
     }
     return NULL;
 }
+
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build

os-win32.c char *os_find_datadir(const char *argv0) · line 219

Quoted 10 lines
os-win32.c +10 -0
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

I agree with Richard: this is gross.

Quoted 15 lines
os-win32.c +9 -0
+
+/*
+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    return -1;
+}
sysemu.h +0 -0
index 72f3734..08ec323 100644
@@ -79,9 +79,18 @@ int qemu_loadvm_state(QEMUFile *f);
 /* SLIRP */

sysemu.h int qemu_loadvm_state(QEMUFile *f); · line 89

Quoted 10 lines
sysemu.h +8 -0
 void do_info_slirp(Monitor *mon);
 
+/* This is needed for vl.c and the OS specific files */
+typedef struct QEMUOption {
+    const char *name;
+    int flags;
+    int index;
+    uint32_t arch_mask;
+} QEMUOption;
+

Ugh.

Quoted 39 lines
sysemu.h +1 -0
 /* OS specific functions */
 void os_setup_early_signal_handling(void);
 char *os_find_datadir(const char *argv0);
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg);
 
 typedef enum DisplayType
 {
vl.c +0 -13
index 7f22733..838e109 100644
@@ -1909,13 +1909,6 @@ enum {
 #undef GEN_DOCS
 };
 
-typedef struct QEMUOption {
-    const char *name;
-    int flags;
-    int index;
-    uint32_t arch_mask;
-} QEMUOption;
-
 static const QEMUOption qemu_options[] = {
     { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
@@ -2624,12 +2617,6 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_bootp:
                 legacy_bootp_filename = optarg;
                 break;
-#ifndef _WIN32
-            case QEMU_OPTION_smb:
-                if (net_slirp_smb(optarg) < 0)
-                    exit(1);
-                break;
-#endif
             case QEMU_OPTION_redir:
                 if (net_slirp_redir(optarg) < 0)
                     exit(1);

vl.c int main(int argc, char **argv, char **envp) · line 3120

Quoted 8 lines
vl.c +2 -0
@@ -3126,6 +3113,8 @@ int main(int argc, char **argv, char **envp)
                     fclose(fp);
                     break;
                 }
+            default:
+                os_parse_cmd_args(popt, optarg);
             }
         }
     }

Is this minor improvement of vl.c really worth the headaches elsewhere?

#21 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 16 lines · Markus Armbruster

os-posix.c char *os_find_datadir(const char *argv0) · line 161

Quoted 13 lines · [email protected] writes:

+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */

os-posix.c +10 -0
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    int ret = 0;
+    switch (popt->index) {
+#ifdef CONFIG_SLIRP
+    case QEMU_OPTION_smb:
+        if (net_slirp_smb(optarg) < 0)
+            exit(1);
+        break;
+#endif

Was #ifndef _WIN32 before. Impact?

It was moved to os-posix.c which is only built for non _WIN32, so it has the same effect, except it's not full of ugly #ifdef's

Quoted 6 lines
Quoted 6 lines
os-posix.c +6 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
Quoted 8 lines

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 6 lines
os-posix.c +6 -0
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

I agree with Richard: this is gross.

The enum creation is gross by itself. Only way to get around not duplicating it is to create a new header file to hold just that?

Quoted 10 lines

sysemu.h int qemu_loadvm_state(QEMUFile *f); · line 89

Quoted 8 lines
sysemu.h +8 -0
+/* This is needed for vl.c and the OS specific files */
+typedef struct QEMUOption {
+    const char *name;
+    int flags;
+    int index;
+    uint32_t arch_mask;
+} QEMUOption;
+

Ugh.

What do you mean? The real ugh! here is that it was created as a typedef. I can change the function to pass in just the index, but I don't know if we will have cases where the rest is needed.

Quoted 1 line

Is this minor improvement of vl.c really worth the headaches elsewhere?

vl.c as it is today is gross and un-maintainable. This patch gets rid of a lot of the ugly #ifdefs and makes the code easier to read and maintain.

Jes

#22 lore raw

[Qemu-devel] Re: [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 16 lines
Quoted 15 lines

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 13 lines
os-posix.c +12 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

I agree with Richard: this is gross.

Quoted 2 lines

The enum creation is gross by itself. Only way to get around not duplicating it is to create a new header file to hold just that?

I don't think it's particularly gross. At least you don't have two files to keep in sync.

You could rename qemu-options.h to qemu-options.def, and make a real header file with the typedef and the enum. Then include the header from vl.c and os-*.c.

BTW from Fedora 11 and newer you can easily build QEMU with a cross compiler. (Running it is a bit harder). These packages should suffice:

mingw32-w32api mingw32-cpp mingw32-termcap mingw32-runtime mingw32-binutils mingw32-filesystem mingw32-SDL mingw32-gcc mingw32-zlib

and you need to configure it with "--cross-prefix=i686-pc-mingw32-" (trailing dash included!).

Paolo

#23 lore raw

[Qemu-devel] Re: [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 11 lines · Paolo Bonzini
Quoted 4 lines
Quoted 1 line

I agree with Richard: this is gross.

The enum creation is gross by itself. Only way to get around not duplicating it is to create a new header file to hold just that?

I don't think it's particularly gross. At least you don't have two files to keep in sync.

You could rename qemu-options.h to qemu-options.def, and make a real header file with the typedef and the enum. Then include the header from vl.c and os-*.c.

I like this idea. I was looking at a qemu-options-somethingelse.h but I couldn't really find any appropriate name for it. Renaming the current qemu-options.h to qemu-options.def seems correct IMHO as it's not really a header file that can be included into code by itself.

Quoted 9 lines

BTW from Fedora 11 and newer you can easily build QEMU with a cross compiler. (Running it is a bit harder). These packages should suffice:

mingw32-w32api mingw32-cpp mingw32-termcap mingw32-runtime mingw32-binutils mingw32-filesystem mingw32-SDL mingw32-gcc mingw32-zlib

and you need to configure it with "--cross-prefix=i686-pc-mingw32-" (trailing dash included!).

Thanks! I'll have to take a look at this.

Cheers,
Jes

#24 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

In reply to Jes Sorensen
Quoted 18 lines · Jes Sorensen
Quoted 16 lines · Markus Armbruster

os-posix.c char *os_find_datadir(const char *argv0) · line 161

Quoted 13 lines · [email protected] writes:

+ * Parse OS specific command line options.
+ * return 0 if option handled, -1 otherwise
+ */

os-posix.c +10 -0
+int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
+{
+    int ret = 0;
+    switch (popt->index) {
+#ifdef CONFIG_SLIRP
+    case QEMU_OPTION_smb:
+        if (net_slirp_smb(optarg) < 0)
+            exit(1);
+        break;
+#endif

Was #ifndef _WIN32 before. Impact?

Quoted 2 lines

It was moved to os-posix.c which is only built for non _WIN32, so it has the same effect, except it's not full of ugly #ifdef's

I missed the fact that it is under #ifdef CONFIG_SLIRP in the current code. Sorry for the noise.

Quoted 29 lines
Quoted 6 lines
Quoted 6 lines
os-posix.c +6 -0
+/*
+ * Duplicate definition from vl.c to avoid messing up the entire build
+ */
+enum {
+#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask)     \
+    opt_enum,
Quoted 8 lines

os-posix.c char *os_find_datadir(const char *argv0) · line 146

Quoted 6 lines
os-posix.c +6 -0
+#define DEFHEADING(text)
+#include "qemu-options.h"
+#undef DEF
+#undef DEFHEADING
+#undef GEN_DOCS
+};

I agree with Richard: this is gross.

The enum creation is gross by itself. Only way to get around not duplicating it is to create a new header file to hold just that?

Quoted 10 lines

sysemu.h int qemu_loadvm_state(QEMUFile *f); · line 89

Quoted 8 lines
sysemu.h +8 -0
+/* This is needed for vl.c and the OS specific files */
+typedef struct QEMUOption {
+    const char *name;
+    int flags;
+    int index;
+    uint32_t arch_mask;
+} QEMUOption;
+

Ugh.

Quoted 3 lines

What do you mean? The real ugh! here is that it was created as a typedef. I can change the function to pass in just the index, but I don't know if we will have cases where the rest is needed.

Moving stuff out of the vl.c grabbag is cool. Moving stuff into the sysemu.h grabbag is very uncool.

Quoted 4 lines
Quoted 1 line

Is this minor improvement of vl.c really worth the headaches elsewhere?

vl.c as it is today is gross and un-maintainable. This patch gets rid of a lot of the ugly #ifdefs and makes the code easier to read and maintain.

I'm not arguing against your patch, just trying to help making it even better.

#25 lore raw

Re: [Qemu-devel] [PATCH 10/16] Introduce OS specific cmdline argument handling and move SMB arg to os-posix.c

Quoted 9 lines · Markus Armbruster
Quoted 4 lines · Jes Sorensen

On 06/04/10 10:15, Markus Armbruster wrote:
What do you mean? The real ugh! here is that it was created as a typedef. I can change the function to pass in just the index, but I don't know if we will have cases where the rest is needed.

Moving stuff out of the vl.c grabbag is cool. Moving stuff into the sysemu.h grabbag is very uncool.

I agree, I have a new version of the patch coming up shortly. I just want to apply Paolo's idea of moving qemu-options.h around a bit.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Is this minor improvement of vl.c really worth the headaches elsewhere?

vl.c as it is today is gross and un-maintainable. This patch gets rid of a lot of the ugly #ifdefs and makes the code easier to read and maintain.

I'm not arguing against your patch, just trying to help making it even better.

I was gathering that, and your input is much appreciated.

Cheers,
Jes

#26 lore raw

PATCH 11/16 Move runas handling from vl.c to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move code to handle runas, ie. change of user id of QEMU process to OS specific files and provide dummy stub for Win32.

---
 os-posix.c      |   28 ++++++++++++++++++++++++++++
 qemu-os-posix.h |    1 +
 qemu-os-win32.h |    1 +
 vl.c            |   29 +----------------------------
 4 files changed, 31 insertions(+), 28 deletions(-)
os-posix.c +28 -0
index 66f2bf5..f8a092e 100644
@@ -28,6 +28,7 @@
 #include <signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <pwd.h>
 #include <libgen.h>
 
 /* Needed early for CONFIG_BSD etc. */
@@ -35,6 +36,8 @@
 #include "sysemu.h"
 #include "net/slirp.h"
 
+static struct passwd *user_pwd;
+
 void os_setup_early_signal_handling(void)
 {
     struct sigaction act;
@@ -159,8 +162,33 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
             exit(1);
         break;
 #endif
+    case QEMU_OPTION_runas:
+        user_pwd = getpwnam(optarg);
+        if (!user_pwd) {
+            fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
+            exit(1);
+        }
+        break;
     default:
         ret = -1;
     }
     return ret;
 }
+
+void os_change_process_uid(void)
+{
+    if (user_pwd) {
+        if (setgid(user_pwd->pw_gid) < 0) {
+            fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
+            exit(1);
+        }
+        if (setuid(user_pwd->pw_uid) < 0) {
+            fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
+            exit(1);
+        }
+        if (setuid(0) != -1) {
+            fprintf(stderr, "Dropping privileges failed\n");
+            exit(1);
+        }
+    }
+}
qemu-os-posix.h +1 -0
index ff5adb1..6d8cf79 100644
@@ -31,5 +31,6 @@ static inline void os_host_main_loop_wait(int *timeout)
 }
 
 void os_setup_signal_handling(void);
+void os_change_process_uid(void);
 
 #endif
qemu-os-win32.h +1 -0
index 4343c6d..9df0eda 100644
@@ -43,5 +43,6 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void os_host_main_loop_wait(int *timeout);
 
 static inline void os_setup_signal_handling(void) {};
+static inline void os_change_process_uid(void) {};
 
 #endif
vl.c +1 -28
index 838e109..d42be8d 100644
@@ -34,7 +34,6 @@
 
 #ifndef _WIN32
 #include <libgen.h>
-#include <pwd.h>
 #include <sys/times.h>
 #include <sys/wait.h>
 #include <termios.h>
@@ -2312,9 +2311,7 @@ int main(int argc, char **argv, char **envp)
     const char *incoming = NULL;
 #ifndef _WIN32
     int fd = 0;
-    struct passwd *pwd = NULL;
     const char *chroot_dir = NULL;
-    const char *run_as = NULL;
 #endif
     int show_vnc_port = 0;
     int defconfig = 1;
@@ -3062,9 +3059,6 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_chroot:
                 chroot_dir = optarg;
                 break;
-            case QEMU_OPTION_runas:
-                run_as = optarg;
-                break;
 #endif
             case QEMU_OPTION_xen_domid:
                 if (!(xen_available())) {
@@ -3554,14 +3548,6 @@ int main(int argc, char **argv, char **envp)
 	    exit(1);
     }
 
-    if (run_as) {
-        pwd = getpwnam(run_as);
-        if (!pwd) {
-            fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
-            exit(1);
-        }
-    }
-
     if (chroot_dir) {
         if (chroot(chroot_dir) < 0) {
             fprintf(stderr, "chroot failed\n");
@@ -3573,20 +3559,7 @@ int main(int argc, char **argv, char **envp)
         }
     }
 
-    if (run_as) {
-        if (setgid(pwd->pw_gid) < 0) {
-            fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
-            exit(1);
-        }
-        if (setuid(pwd->pw_uid) < 0) {
-            fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
-            exit(1);
-        }
-        if (setuid(0) != -1) {
-            fprintf(stderr, "Dropping privileges failed\n");
-            exit(1);
-        }
-    }
+    os_change_process_uid();
 
     if (daemonize) {
         dup2(fd, 0);
Signature · 1 line
1.6.5.2
#27 lore raw

Re: [Qemu-devel] [PATCH 11/16] Move runas handling from vl.c to OS specific files.

Quoted 1 line · On 06/03/2010 09:48 AM, [email protected] wrote:

+static inline void os_change_process_uid(void) {};

Stray ;

r~

#28 lore raw

PATCH 12/16 Move chroot handling to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move chroot handling to OS specific files.

---
 os-posix.c      |   19 +++++++++++++++++++
 qemu-os-posix.h |    1 +
 qemu-os-win32.h |    1 +
 vl.c            |   18 +-----------------
 4 files changed, 22 insertions(+), 17 deletions(-)
os-posix.c +19 -0
index f8a092e..a91e1f6 100644
@@ -37,6 +37,7 @@
 #include "net/slirp.h"
 
 static struct passwd *user_pwd;
+static const char *chroot_dir;
 
 void os_setup_early_signal_handling(void)
 {
@@ -169,6 +170,9 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
             exit(1);
         }
         break;
+    case QEMU_OPTION_chroot:
+        chroot_dir = optarg;
+        break;
     default:
         ret = -1;
     }
@@ -192,3 +196,18 @@ void os_change_process_uid(void)
         }
     }
 }
+
+void os_change_root(void)
+{
+    if (chroot_dir) {
+        if (chroot(chroot_dir) < 0) {
+            fprintf(stderr, "chroot failed\n");
+            exit(1);
+        }
+        if (chdir("/")) {
+            perror("not able to chdir to /");
+            exit(1);
+        }
+    }
+
+}
qemu-os-posix.h +1 -0
index 6d8cf79..91c7b68 100644
@@ -32,5 +32,6 @@ static inline void os_host_main_loop_wait(int *timeout)
 
 void os_setup_signal_handling(void);
 void os_change_process_uid(void);
+void os_change_root(void);
 
 #endif
qemu-os-win32.h +1 -0
index 9df0eda..245b188 100644
@@ -44,5 +44,6 @@ void os_host_main_loop_wait(int *timeout);
 
 static inline void os_setup_signal_handling(void) {};
 static inline void os_change_process_uid(void) {};
+static inline void os_change_root(void) {};
 
 #endif
vl.c +1 -17
index d42be8d..7173684 100644
@@ -2311,7 +2311,6 @@ int main(int argc, char **argv, char **envp)
     const char *incoming = NULL;
 #ifndef _WIN32
     int fd = 0;
-    const char *chroot_dir = NULL;
 #endif
     int show_vnc_port = 0;
     int defconfig = 1;
@@ -3055,11 +3054,6 @@ int main(int argc, char **argv, char **envp)
                 default_cdrom = 0;
                 default_sdcard = 0;
                 break;
-#ifndef _WIN32
-            case QEMU_OPTION_chroot:
-                chroot_dir = optarg;
-                break;
-#endif
             case QEMU_OPTION_xen_domid:
                 if (!(xen_available())) {
                     printf("Option %s not supported for this target\n", popt->name);
@@ -3548,17 +3542,7 @@ int main(int argc, char **argv, char **envp)
 	    exit(1);
     }
 
-    if (chroot_dir) {
-        if (chroot(chroot_dir) < 0) {
-            fprintf(stderr, "chroot failed\n");
-            exit(1);
-        }
-        if (chdir("/")) {
-            perror("not able to chdir to /");
-            exit(1);
-        }
-    }
-
+    os_change_root();
     os_change_process_uid();
 
     if (daemonize) {
Signature · 1 line
1.6.5.2
#29 lore raw

Re: [Qemu-devel] [PATCH 12/16] Move chroot handling to OS specific files.

Quoted 1 line · On 06/03/2010 09:48 AM, [email protected] wrote:

+static inline void os_change_root(void) {};

You really like the ";", don't you. ;-)

r~

#30 lore raw

Re: [Qemu-devel] [PATCH 12/16] Move chroot handling to OS specific files.

Quoted 4 lines · Richard Henderson
Quoted 1 line · On 06/03/2010 09:48 AM, [email protected] wrote:

+static inline void os_change_root(void) {};

You really like the ";", don't you. ;-)

LOL now I get it.

Yes, ;'s are so pretty ;-)

I'll clean it up and send out a new version. Still not sure about the enmu but the rest is straight forward to handle.

Cheers,
Jes

#31 lore raw

PATCH 13/16 Move daemonize handling to OS specific files

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move daemonize handling from vl.c to OS specific files. Provide dummy stubs for Win32.

---
 os-posix.c      |  102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 os-win32.c      |    5 +++
 qemu-os-posix.h |    2 +
 qemu-os-win32.h |    2 +
 sysemu.h        |    1 +
 vl.c            |  106 ++-----------------------------------------------------
 6 files changed, 115 insertions(+), 103 deletions(-)
os-posix.c +102 -0
index a91e1f6..8a9d102 100644
@@ -38,6 +38,8 @@
 
 static struct passwd *user_pwd;
 static const char *chroot_dir;
+static int daemonize;
+static int fds[2];
 
 void os_setup_early_signal_handling(void)
 {
@@ -173,6 +175,9 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
     case QEMU_OPTION_chroot:
         chroot_dir = optarg;
         break;
+    case QEMU_OPTION_daemonize:
+        daemonize = 1;
+        break;
     default:
         ret = -1;
     }
@@ -211,3 +216,100 @@ void os_change_root(void)
     }
 
 }
+
+void os_daemonize(void)
+{
+    if (daemonize) {
+	pid_t pid;
+
+	if (pipe(fds) == -1)
+	    exit(1);
+
+	pid = fork();
+	if (pid > 0) {
+	    uint8_t status;
+	    ssize_t len;
+
+	    close(fds[1]);
+
+	again:
+            len = read(fds[0], &status, 1);
+            if (len == -1 && (errno == EINTR))
+                goto again;
+
+            if (len != 1)
+                exit(1);
+            else if (status == 1) {
+                fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
+                exit(1);
+            } else
+                exit(0);
+	} else if (pid < 0)
+            exit(1);
+
+	close(fds[0]);
+	qemu_set_cloexec(fds[1]);
+
+	setsid();
+
+	pid = 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);
+    }
+}
+
+void os_setup_post(void)
+{
+    int fd = 0;
+
+    if (daemonize) {
+	uint8_t status = 0;
+	ssize_t len;
+
+    again1:
+	len = write(fds[1], &status, 1);
+	if (len == -1 && (errno == EINTR))
+	    goto again1;
+
+	if (len != 1)
+	    exit(1);
+
+        if (chdir("/")) {
+            perror("not able to chdir to /");
+            exit(1);
+        }
+	TFR(fd = qemu_open("/dev/null", O_RDWR));
+	if (fd == -1)
+	    exit(1);
+    }
+
+    os_change_root();
+    os_change_process_uid();
+
+    if (daemonize) {
+        dup2(fd, 0);
+        dup2(fd, 1);
+        dup2(fd, 2);
+
+        close(fd);
+    }
+}
+
+void os_pidfile_error(void)
+{
+    if (daemonize) {
+        uint8_t status = 1;
+        if (write(fds[1], &status, 1) != 1) {
+            perror("daemonize. Writing to pipe\n");
+        }
+    } else
+        fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
+}
os-win32.c +5 -0
index a311a90..86ff327 100644
@@ -226,3 +226,8 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
 {
     return -1;
 }
+
+void os_pidfile_error(void)
+{
+    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
+}
qemu-os-posix.h +2 -0
index 91c7b68..9b07660 100644
@@ -33,5 +33,7 @@ static inline void os_host_main_loop_wait(int *timeout)
 void os_setup_signal_handling(void);
 void os_change_process_uid(void);
 void os_change_root(void);
+void os_daemonize(void);
+void os_setup_post(void);
 
 #endif
qemu-os-win32.h +2 -0
index 245b188..ccb9691 100644
@@ -45,5 +45,7 @@ void os_host_main_loop_wait(int *timeout);
 static inline void os_setup_signal_handling(void) {};
 static inline void os_change_process_uid(void) {};
 static inline void os_change_root(void) {};
+static inline void os_daemonize(void) {};
+static inline void os_setup_post(void) {};
 
 #endif
sysemu.h +1 -0
index 08ec323..aa44a20 100644
@@ -91,6 +91,7 @@ typedef struct QEMUOption {
 void os_setup_early_signal_handling(void);
 char *os_find_datadir(const char *argv0);
 int os_parse_cmd_args(const QEMUOption *popt, const char *optarg);
+void os_pidfile_error(void);
 
 typedef enum DisplayType
 {
vl.c +3 -103

141 lines not on this page · raw message

Signature · 1 line
1.6.5.2
#32 lore raw

PATCH 14/16 Make os_change_process_uid and os_change_root os-posix.c local

In reply to [email protected]

From: Jes Sorensen <[email protected]>

os_change_process_uid() and os_change_root() are now only called from os-posix.c, so no need to keep win32 stubs for them.

---
 os-posix.c      |    8 ++++----
 qemu-os-posix.h |    2 --
 qemu-os-win32.h |    2 --
 3 files changed, 4 insertions(+), 8 deletions(-)
os-posix.c +4 -4
index 8a9d102..7ac6f07 100644
@@ -184,7 +184,7 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg)
     return ret;
 }
 
-void os_change_process_uid(void)
+static void change_process_uid(void)
 {
     if (user_pwd) {
         if (setgid(user_pwd->pw_gid) < 0) {
@@ -202,7 +202,7 @@ void os_change_process_uid(void)
     }
 }
 
-void os_change_root(void)
+static void change_root(void)
 {
     if (chroot_dir) {
         if (chroot(chroot_dir) < 0) {
@@ -291,8 +291,8 @@ void os_setup_post(void)
 	    exit(1);
     }
 
-    os_change_root();
-    os_change_process_uid();
+    change_root();
+    change_process_uid();
 
     if (daemonize) {
         dup2(fd, 0);
qemu-os-posix.h +0 -2
index 9b07660..8be583d 100644
@@ -31,8 +31,6 @@ static inline void os_host_main_loop_wait(int *timeout)
 }
 
 void os_setup_signal_handling(void);
-void os_change_process_uid(void);
-void os_change_root(void);
 void os_daemonize(void);
 void os_setup_post(void);
 
qemu-os-win32.h +0 -2
index ccb9691..facd3d6 100644
@@ -43,8 +43,6 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque);
 void os_host_main_loop_wait(int *timeout);
 
 static inline void os_setup_signal_handling(void) {};
-static inline void os_change_process_uid(void) {};
-static inline void os_change_root(void) {};
 static inline void os_daemonize(void) {};
 static inline void os_setup_post(void) {};
 
Signature · 1 line
1.6.5.2
#33 lore raw

PATCH 15/16 Move line-buffering setup to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move line-buffering setup to OS specific files.

---
 os-posix.c      |    5 +++++
 qemu-os-posix.h |    1 +
 qemu-os-win32.h |    2 ++
 vl.c            |    5 +----
 4 files changed, 9 insertions(+), 4 deletions(-)
os-posix.c +5 -0
index 7ac6f07..7530276 100644
@@ -313,3 +313,8 @@ void os_pidfile_error(void)
     } else
         fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
 }
+
+void os_set_line_buffering(void)
+{
+    setvbuf(stdout, NULL, _IOLBF, 0);
+}
qemu-os-posix.h +1 -0
index 8be583d..cb210ba 100644
@@ -30,6 +30,7 @@ static inline void os_host_main_loop_wait(int *timeout)
 {
 }
 
+void os_set_line_buffering(void);
 void os_setup_signal_handling(void);
 void os_daemonize(void);
 void os_setup_post(void);
qemu-os-win32.h +2 -0
index facd3d6..1709cf6 100644
@@ -45,5 +45,7 @@ void os_host_main_loop_wait(int *timeout);
 static inline void os_setup_signal_handling(void) {};
 static inline void os_daemonize(void) {};
 static inline void os_setup_post(void) {};
+/* Win32 doesn't support line-buffering and requires size >= 2 */
+static inline void os_set_line_buffering(void) {};
 
 #endif
vl.c +1 -4
index bb8abbf..3dbc789 100644
@@ -3216,10 +3216,7 @@ int main(int argc, char **argv, char **envp)
         exit(1);
     }
 
-#ifndef _WIN32
-    /* Win32 doesn't support line-buffering and requires size >= 2 */
-    setvbuf(stdout, NULL, _IOLBF, 0);
-#endif
+    os_set_line_buffering();
 
     if (init_timer_alarm() < 0) {
         fprintf(stderr, "could not initialize alarm timer\n");
Signature · 1 line
1.6.5.2
#34 lore raw

PATCH 16/16 Move set_proc_name() to OS specific files.

In reply to [email protected]

From: Jes Sorensen <[email protected]>

Move handling to change process name to POSIX specific files plus add a better error message to cover the case where the feature isn't supported.

---
 os-posix.c      |   24 ++++++++++++++++++++++++
 qemu-os-posix.h |    1 +
 qemu-os-win32.h |    1 +
 vl.c            |   19 +------------------
 4 files changed, 27 insertions(+), 18 deletions(-)
os-posix.c +24 -0
index 7530276..03105f7 100644
@@ -36,6 +36,10 @@
 #include "sysemu.h"
 #include "net/slirp.h"
 
+#ifdef CONFIG_LINUX
+#include <sys/prctl.h>
+#endif
+
 static struct passwd *user_pwd;
 static const char *chroot_dir;
 static int daemonize;
@@ -138,6 +142,26 @@ char *os_find_datadir(const char *argv0)
 #undef SHARE_SUFFIX
 #undef BUILD_SUFFIX
 
+void os_set_proc_name(const char *s)
+{
+#if defined(PR_SET_NAME)
+    char name[16];
+    if (!s)
+        return;
+    name[sizeof(name) - 1] = 0;
+    strncpy(name, s, sizeof(name));
+    /* Could rewrite argv[0] too, but that's a bit more complicated.
+       This simple way is enough for `top'. */
+    if (prctl(PR_SET_NAME, name)) {
+        perror("unable to change process name");
+        exit(1);
+    }
+#else
+    fprintf(stderr, "Change of process name not supported by your OS\n");
+    exit(1);
+#endif    	
+}
+
 /*
  * Duplicate definition from vl.c to avoid messing up the entire build
  */
qemu-os-posix.h +1 -0
index cb210ba..ed5c058 100644
@@ -31,6 +31,7 @@ static inline void os_host_main_loop_wait(int *timeout)
 }
 
 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);
qemu-os-win32.h +1 -0
index 1709cf6..bb7126b 100644
@@ -47,5 +47,6 @@ static inline void os_daemonize(void) {};
 static inline void os_setup_post(void) {};
 /* Win32 doesn't support line-buffering and requires size >= 2 */
 static inline void os_set_line_buffering(void) {};
+static inline void os_set_proc_name(const char *dummy) {};
 
 #endif
vl.c +1 -18
index 3dbc789..b77dce8 100644
@@ -59,7 +59,6 @@
 #ifdef __linux__
 #include <pty.h>
 #include <malloc.h>
-#include <sys/prctl.h>
 
 #include <linux/ppdev.h>
 #include <linux/parport.h>
@@ -283,22 +282,6 @@ static int default_driver_check(QemuOpts *opts, void *opaque)
 }
 
 /***********************************************************/
-
-static void set_proc_name(const char *s)
-{
-#if defined(__linux__) && defined(PR_SET_NAME)
-    char name[16];
-    if (!s)
-        return;
-    name[sizeof(name) - 1] = 0;
-    strncpy(name, s, sizeof(name));
-    /* Could rewrite argv[0] too, but that's a bit more complicated.
-       This simple way is enough for `top'. */
-    prctl(PR_SET_NAME, name);
-#endif    	
-}
- 
-/***********************************************************/
 /* real time host monotonic timer */
 
 /* compute with 96 bit intermediate result: (a*b)/c */
@@ -2990,7 +2973,7 @@ int main(int argc, char **argv, char **envp)
 			    exit(1);
 			}
 			p += 8;
-			set_proc_name(p);
+			os_set_proc_name(p);
 		     }	
 		 }	
                 break;
Signature · 1 line
1.6.5.2
#35 lore raw
In reply to [email protected]
Quoted 9 lines · [email protected] writes:

From: Jes Sorensen <[email protected]>

Hi,

I have been working on a set of patches to clean up the vl.c code, by separating out OS specific code into OS specific files. Basically it introduces two header files: qemu-os-win32.h and qemu-os-posix.h as well as os-win32.c and os-posix.c.

Quoted 4 lines

I have tried to be as careful as I can to not break non Linux support, but as I only have a Linux build environment handy, I would appreciate it if people with other OSes could check that I didn't break anything for them. In particular I would like to know if win32 still builds.

I like moving stuff out of vl.c in general. Your moves of entire functions look like a win to me. I have doubts about spreading the option switch over three files, though.

#36 lore raw
Quoted 9 lines · Markus Armbruster
Quoted 4 lines · [email protected] writes:

I have tried to be as careful as I can to not break non Linux support, but as I only have a Linux build environment handy, I would appreciate it if people with other OSes could check that I didn't break anything for them. In particular I would like to know if win32 still builds.

I like moving stuff out of vl.c in general. Your moves of entire functions look like a win to me. I have doubts about spreading the option switch over three files, though.

The problem is right now there are too many OS specific options, but having the #ifdefs plastered all over to enable/disable them accordingly is just a nightmare and is prone to leave in inconsistent behavior for various OSes. See the set_proc_name() stuff for an example.

Cheers,
Jes

#37 lore raw
Quoted 7 lines · Jes Sorensen
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · [email protected] writes:

I have tried to be as careful as I can to not break non Linux support, but as I only have a Linux build environment handy, I would appreciate it if people with other OSes could check that I didn't break anything for them. In particular I would like to know if win32 still builds.

Quoted 8 lines
Quoted 3 lines

I like moving stuff out of vl.c in general. Your moves of entire functions look like a win to me. I have doubts about spreading the option switch over three files, though.

The problem is right now there are too many OS specific options, but having the #ifdefs plastered all over to enable/disable them accordingly is just a nightmare and is prone to leave in inconsistent behavior for various OSes. See the set_proc_name() stuff for an example.

I doubt spreading option code over separate files will help consistency.

I suspect the true root of the problem is having (too many) OS-specific options in the first place. What about parsing options the same everywhere, calling out to OS-specific functions to do the actual work? Let them fail with "can't do this on this OS".

#38 lore raw
Quoted 12 lines · Markus Armbruster
Quoted 9 lines · Jes Sorensen
Quoted 3 lines · Markus Armbruster

I like moving stuff out of vl.c in general. Your moves of entire functions look like a win to me. I have doubts about spreading the option switch over three files, though.

The problem is right now there are too many OS specific options, but having the #ifdefs plastered all over to enable/disable them accordingly is just a nightmare and is prone to leave in inconsistent behavior for various OSes. See the set_proc_name() stuff for an example.

Quoted 6 lines

I doubt spreading option code over separate files will help consistency.

I suspect the true root of the problem is having (too many) OS-specific options in the first place. What about parsing options the same everywhere, calling out to OS-specific functions to do the actual work? Let them fail with "can't do this on this OS".

That is a possibility which I did consider, but it would end up in far more os specific functions for simple assignments etc. I modeled it the way I did similar to how we handle ioctl calls in the kernel.

If there is strong feeling we should do it this way instead, I can change the code to do it this way instead. I am not married to the current approach, I just find it the lesser evil.

Cheers,
Jes

#39 lore raw
Quoted 15 lines · Jes Sorensen
Quoted 14 lines · Markus Armbruster
Quoted 9 lines · Jes Sorensen
Quoted 3 lines · Markus Armbruster

I like moving stuff out of vl.c in general. Your moves of entire functions look like a win to me. I have doubts about spreading the option switch over three files, though.

The problem is right now there are too many OS specific options, but having the #ifdefs plastered all over to enable/disable them accordingly is just a nightmare and is prone to leave in inconsistent behavior for various OSes. See the set_proc_name() stuff for an example.

I doubt spreading option code over separate files will help consistency.

Quoted 7 lines
Quoted 4 lines

I suspect the true root of the problem is having (too many) OS-specific options in the first place. What about parsing options the same everywhere, calling out to OS-specific functions to do the actual work? Let them fail with "can't do this on this OS".

That is a possibility which I did consider, but it would end up in far more os specific functions for simple assignments etc.

Far more?

option                  condition       what's needed to make it uncond.
QEMU_OPTION_smb         !_WIN32         net_slirp_smb() dummy.
QEMU_OPTION_fsdev       CONFIG_LINUX    fsdev_init_func() dummy
QEMU_OPTION_virtfs      CONFIG_LINUX    nothing, it's just short for

-fsdev & -device
QEMU_OPTION_daemonize !_WIN32 move the big #ifndef _WIN32
chunk at the end of main() into a helper, + dummy

QEMU_OPTION_chroot      !_WIN32         ditto
QEMU_OPTION_runas       !_WIN32         ditto
Quoted 6 lines

I modeled it the
way I did similar to how we handle ioctl calls in the kernel.

If there is strong feeling we should do it this way instead, I can change the code to do it this way instead. I am not married to the current approach, I just find it the lesser evil.

I'm making suggestions, not demands. If you still prefer your way after considering my suggestions, go with it.

For what it's worth, pushing OS-dependent bits out of the option parsing allows for:

    qemu: --frobnicate: Can't frobnicate under Windows

instead of

    qemu: --frobnicate: invalid option
#40 lore raw
Quoted 9 lines · Markus Armbruster
Quoted 4 lines · Jes Sorensen

On 06/04/10 13:54, Markus Armbruster wrote:
If there is strong feeling we should do it this way instead, I can change the code to do it this way instead. I am not married to the current approach, I just find it the lesser evil.

I'm making suggestions, not demands. If you still prefer your way after considering my suggestions, go with it.

Quoted 8 lines

For what it's worth, pushing OS-dependent bits out of the option parsing allows for:

    qemu: --frobnicate: Can't frobnicate under Windows

instead of

    qemu: --frobnicate: invalid option

That is true, on the other hand I think it would be cleaner to not advertise options on an OS where it is not supported. Ideally I would like them to not show up in qemu --help at all.

Cheers,
Jes