Re-factor osdep code + macro and brace fixes
v2 cover Re-factor osdep code + macro and brace fixes
From: Jes Sorensen <[email protected]>
Hi,
Here is another set of patches which tries to split up osdep.c further into posix and win32 versions. It introduces oslib-{posix,win32}.c files which are used for functions that are OS specific core library functionality, like gettimeofday(), and which is used by both QEMU and support applications like qemu-img. Other functions are moved to os-{posix,win32}.c. In addtion there are a couple of minor fixes for bad macro names.
In some cases braces were added to code when it was moved, to make it compliant with the QEMU bracing rules.
v2 fixes the issues pointed out by Paolo and Blue Swirl, as well as reorganizes the code slightly to make the patches build incrementally using mingw32 for win32.
Cheers,
Jes
Jes Sorensen (9):
Move QEMU OS dependant library functions to OS specific files Move osdep socket code to oslib-{posix,win32}.c qemu_pipe() is used only by POSIX code, so move to oslib-posix.c We only support eventfd under POSIX, move qemu_eventfd() to
os-posix.c
Move qemu_gettimeofday() to OS specific files
Do not redefine reserved key-words TRUE/FALSE Separate qemu_pidfile() into OS specific versions Consolidate oom_check() functions
Remove unncessary includes
Makefile | 6 +- Makefile.objs | 9 ++- hw/bt-sdp.c | 21 +++-- os-posix.c | 53 +++++++++++ os-win32.c | 24 +++++ osdep.c | 256 ---------------------------------------------------- osdep.h | 15 --- oslib-posix.c | 109 ++++++++++++++++++++++ oslib-win32.c | 121 +++++++++++++++++++++++++ posix-aio-compat.c | 1 + qemu-common.h | 1 + qemu-img.c | 1 + qemu-malloc.c | 14 +--- qemu-os-posix.h | 3 + qemu-os-win32.h | 9 ++ qemu-tool.c | 1 + 16 files changed, 348 insertions(+), 296 deletions(-) create mode 100644 oslib-posix.c create mode 100644 oslib-win32.c
Signature · 1 line
1.7.2.3
PATCH v2 1/9 Move QEMU OS dependant library functions to OS specific files
From: Jes Sorensen <[email protected]>
This moves library functions used by both QEMU and the QEMU tools, such as qemu-img, qemu-nbd etc. from osdep.c to oslib-{posix,win32}.c
In addition it introduces oslib-obj.y to the Makefile set to be included by the various targets, instead of relying on these library functions magically getting included via block-obj-y.
--- Makefile | 6 ++-- Makefile.objs | 9 +++++- osdep.c | 85 --------------------------------------------------------- oslib-posix.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ oslib-win32.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 89 deletions(-) create mode 100644 oslib-posix.c create mode 100644 oslib-win32.c
Makefile +3 -3
@@ -129,11 +129,11 @@ version-obj-$(CONFIG_WIN32) += version.o qemu-img.o: qemu-img-cmds.h qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS) -qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) +qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) -qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) +qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) -qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) +qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@," GEN $@")
Makefile.objs +8 -1
@@ -5,10 +5,16 @@ qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o qobject-obj-y += qerror.o ####################################################################### +# oslib-obj-y is code depending on the OS (win32 vs posix) +oslib-obj-y = osdep.o +oslib-obj-$(CONFIG_WIN32) += oslib-win32.o +oslib-obj-$(CONFIG_POSIX) += oslib-posix.o + +####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o -block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o +block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o block-obj-$(CONFIG_POSIX) += posix-aio-compat.o block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
@@ -50,6 +56,7 @@ common-obj-y += $(net-obj-y) common-obj-y += $(qobject-obj-y) common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX)) common-obj-y += readline.o console.o cursor.o async.o qemu-error.o +common-obj-y += $(oslib-obj-y) common-obj-$(CONFIG_WIN32) += os-win32.o common-obj-$(CONFIG_POSIX) += os-posix.o
osdep.c +0 -85
91 lines not on this page · raw message
oslib-posix.c new file +74 -0
74 lines not on this page · raw message
oslib-win32.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v2 2/9 Move osdep socket code to oslib-{posix, win32}.c
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- osdep.c | 38 -------------------------------------- oslib-posix.c | 15 +++++++++++++++ oslib-win32.c | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 38 deletions(-)
osdep.c +0 -38
@@ -147,44 +147,6 @@ int qemu_gettimeofday(qemu_timeval *tp)#endif /* _WIN32 */ -#ifdef _WIN32 -void socket_set_nonblock (int fd) -{ -unsigned long opt =1 ; -ioctlsocket (fd,FIONBIO , &opt); -} - -int inet_aton (const char *cp,struct in_addr *ia) -{ -uint32_t addr =inet_addr (cp); -if (addr ==0xffffffff ) -return 0 ; - ia->s_addr = addr; -return 1 ; -} - -void qemu_set_cloexec (int fd) -{ -} - -#else - -void socket_set_nonblock (int fd) -{ -int f; - f =fcntl (fd,F_GETFL ); -fcntl (fd,F_SETFL , f |O_NONBLOCK ); -} - -void qemu_set_cloexec (int fd) -{ -int f; - f =fcntl (fd,F_GETFD ); -fcntl (fd,F_SETFD , f |FD_CLOEXEC ); -} - -#endif -/* * Opens a file with FD_CLOEXEC set */
oslib-posix.c +15 -0
@@ -29,6 +29,7 @@#include "config-host.h" #include "sysemu.h" #include "trace.h" +#include "qemu_socket.h" #if !defined(_POSIX_C_SOURCE) || defined(__sun__)static void *oom_check (void *ptr)
@@ -72,3 +73,17 @@ void qemu_vfree(void *ptr)trace_qemu_vfree (ptr);free (ptr); } + +void socket_set_nonblock (int fd) +{ +int f; + f =fcntl (fd,F_GETFL ); +fcntl (fd,F_SETFL , f |O_NONBLOCK ); +} + +void qemu_set_cloexec (int fd) +{ +int f; + f =fcntl (fd,F_GETFD ); +fcntl (fd,F_SETFD , f |FD_CLOEXEC ); +}
oslib-win32.c +21 -0
@@ -29,6 +29,7 @@#include "config-host.h" #include "sysemu.h" #include "trace.h" +#include "qemu_socket.h" static void *oom_check (void *ptr) {
@@ -71,3 +72,23 @@ void qemu_vfree(void *ptr)trace_qemu_vfree (ptr);VirtualFree (ptr,0 ,MEM_RELEASE ); } + +void socket_set_nonblock (int fd) +{ +unsigned long opt =1 ; +ioctlsocket (fd,FIONBIO , &opt); +} + +int inet_aton (const char *cp,struct in_addr *ia) +{ +uint32_t addr =inet_addr (cp); +if (addr ==0xffffffff ) { +return 0 ; + } + ia->s_addr = addr; +return 1 ; +} + +void qemu_set_cloexec (int fd) +{ +}
Signature · 1 line
1.7.2.3
PATCH v2 3/9 qemu_pipe() is used only by POSIX code, so move to oslib-posix.c
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- osdep.c | 22 ---------------------- oslib-posix.c | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-)
osdep.c +0 -22
@@ -235,28 +235,6 @@ int qemu_eventfd(int fds[2]) return qemu_pipe(fds); } - -/* -* Creates a pipe with FD_CLOEXEC set on both file descriptors -*/ -int qemu_pipe (int pipefd[2 ]) -{ -int ret; - -#ifdef CONFIG_PIPE2 - ret =pipe2 (pipefd,O_CLOEXEC ); -if (ret !=-1 || errno !=ENOSYS ) { -return ret; - } -#endif - ret =pipe (pipefd); -if (ret ==0 ) { -qemu_set_cloexec (pipefd[0 ]); -qemu_set_cloexec (pipefd[1 ]); - } - -return ret; -}#endif /*
oslib-posix.c +22 -0
@@ -87,3 +87,25 @@ void qemu_set_cloexec(int fd) f =fcntl (fd,F_GETFD );fcntl (fd,F_SETFD , f |FD_CLOEXEC ); } + +/* +* Creates a pipe with FD_CLOEXEC set on both file descriptors +*/ +int qemu_pipe (int pipefd[2 ]) +{ +int ret; + +#ifdef CONFIG_PIPE2 + ret =pipe2 (pipefd,O_CLOEXEC ); +if (ret !=-1 || errno !=ENOSYS ) { +return ret; + } +#endif + ret =pipe (pipefd); +if (ret ==0 ) { +qemu_set_cloexec (pipefd[0 ]); +qemu_set_cloexec (pipefd[1 ]); + } + +return ret; +}
Signature · 1 line
1.7.2.3
PATCH v2 4/9 We only support eventfd under POSIX, move qemu_eventfd() to os-posix.c
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- os-posix.c | 32 ++++++++++++++++++++++++++++++++ osdep.c | 34 ---------------------------------- 2 files changed, 32 insertions(+), 34 deletions(-)
os-posix.c +32 -0
@@ -43,6 +43,10 @@#include <sys/prctl.h> #endif +#ifdef CONFIG_EVENTFD +#include <sys/eventfd.h> +#endif +static struct passwd *user_pwd;static const char *chroot_dir;static int daemonize;
@@ -329,3 +333,31 @@ void os_set_line_buffering(void) {setvbuf (stdout,NULL , _IOLBF,0 ); } + +/* +* Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. +*/ +int qemu_eventfd (int fds[2 ]) +{ +#ifdef CONFIG_EVENTFD +int ret; + + ret =eventfd (0 ,0 ); +if (ret >=0 ) { + fds[0 ] = ret; +qemu_set_cloexec (ret); +if ((fds[1 ] =dup (ret)) ==-1 ) { +close (ret); +return -1 ; + } +qemu_set_cloexec (fds[1 ]); +return 0 ; + } + +if (errno !=ENOSYS ) { +return -1 ; + } +#endif + +return qemu_pipe (fds); +}
osdep.c +0 -34
@@ -44,10 +44,6 @@extern int madvise (caddr_t ,size_t ,int );#endif -#ifdef CONFIG_EVENTFD -#include <sys/eventfd.h> -#endif -#ifdef _WIN32#include <windows.h> #elif defined(CONFIG_BSD )
@@ -207,36 +203,6 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)return total; } -#ifndef _WIN32 -/* -* Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. -*/ -int qemu_eventfd (int fds[2 ]) -{ -#ifdef CONFIG_EVENTFD -int ret; - - ret =eventfd (0 ,0 ); -if (ret >=0 ) { - fds[0 ] = ret; -qemu_set_cloexec (ret); -if ((fds[1 ] =dup (ret)) ==-1 ) { -close (ret); -return -1 ; - } -qemu_set_cloexec (fds[1 ]); -return 0 ; - } - -if (errno !=ENOSYS ) { -return -1 ; - } -#endif - -return qemu_pipe (fds); -} -#endif -/* * Opens a socket with FD_CLOEXEC set */
Signature · 1 line
1.7.2.3
PATCH v2 5/9 Move qemu_gettimeofday() to OS specific files
In reply to [email protected]From: Jes Sorensen <[email protected]>
In addition add sysemu.h includes to file requiring a prototype for ffs()
--- hw/bt-sdp.c | 1 + osdep.c | 31 ------------------------------- osdep.h | 15 --------------- oslib-win32.c | 27 +++++++++++++++++++++++++++ posix-aio-compat.c | 1 + qemu-img.c | 1 + qemu-os-posix.h | 3 +++ qemu-os-win32.h | 9 +++++++++ qemu-tool.c | 1 + 9 files changed, 43 insertions(+), 46 deletions(-)
hw/bt-sdp.c +1 -0
@@ -19,6 +19,7 @@#include "qemu-common.h" #include "bt.h" +#include "sysemu.h" struct bt_l2cap_sdp_state_s {struct bt_l2cap_conn_params_s *channel;
osdep.c +0 -31
@@ -111,37 +111,6 @@ int qemu_create_pidfile(const char *filename)return 0 ; } -#ifdef _WIN32 - -/* mingw32 needs ffs for compilations without optimization. */ -int ffs (int i) -{ -/* Use gcc's builtin ffs. */ -return __builtin_ffs (i); -} - -/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ -#define _W32_FT_OFFSET (116444736000000000ULL) - -int qemu_gettimeofday (qemu_timeval *tp) -{ -union { -unsigned long long ns100;/*time since 1 Jan 1601 in 100ns units */ -FILETIME ft; - } _now; - -if (tp) - { -GetSystemTimeAsFileTime (&_now.ft); - tp->tv_usec=(long )((_now.ns100 /10ULL ) %1000000ULL ); - tp->tv_sec= (long )((_now.ns100 - _W32_FT_OFFSET) /10000000ULL ); - } -/* Always return 0 as per Open Group Base Specifications Issue 6. -Do not set errno on error. */ -return 0 ; -} -#endif /* _WIN32 */ -/* * Opens a file with FD_CLOEXEC set
osdep.h +0 -15
@@ -127,19 +127,4 @@ int qemu_madvise(void *addr, size_t len, int advice);int qemu_create_pidfile (const char *filename); -#ifdef _WIN32 -int ffs (int i); - -int setenv (const char *name,const char *value,int overwrite); - -typedef struct { -long tv_sec; -long tv_usec; -}qemu_timeval ; -int qemu_gettimeofday (qemu_timeval *tp); -#else -typedef struct timeval qemu_timeval ; -#define qemu_gettimeofday (tp) gettimeofday(tp, NULL); -#endif /* !_WIN32 */ -#endif
oslib-win32.c +27 -0
@@ -92,3 +92,30 @@ int inet_aton(const char *cp, struct in_addr *ia)void qemu_set_cloexec (int fd) { } + +/* mingw32 needs ffs for compilations without optimization. */ +int ffs (int i) +{ +/* Use gcc's builtin ffs. */ +return __builtin_ffs (i); +} + +/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ +#define _W32_FT_OFFSET (116444736000000000ULL) + +int qemu_gettimeofday (qemu_timeval *tp) +{ +union { +unsigned long long ns100;/*time since 1 Jan 1601 in 100ns units */ +FILETIME ft; + } _now; + +if (tp) { +GetSystemTimeAsFileTime (&_now.ft); + tp->tv_usec=(long )((_now.ns100 /10ULL ) %1000000ULL ); + tp->tv_sec= (long )((_now.ns100 - _W32_FT_OFFSET) /10000000ULL ); + } +/* Always return 0 as per Open Group Base Specifications Issue 6. +Do not set errno on error. */ +return 0 ; +}
posix-aio-compat.c +1 -0
@@ -24,6 +24,7 @@#include "qemu-queue.h" #include "osdep.h" +#include "sysemu.h" #include "qemu-common.h" #include "trace.h" #include "block_int.h"
qemu-img.c +1 -0
@@ -24,6 +24,7 @@#include "qemu-common.h" #include "qemu-option.h" #include "osdep.h" +#include "sysemu.h" #include "block_int.h" #include <stdio.h>
qemu-os-posix.h +3 -0
@@ -36,4 +36,7 @@ void os_setup_signal_handling(void);void os_daemonize (void );void os_setup_post (void ); +typedef struct timeval qemu_timeval ; +#define qemu_gettimeofday (tp) gettimeofday(tp, NULL) +#endif
qemu-os-win32.h +9 -0
@@ -52,4 +52,13 @@ static inline void os_set_proc_name(const char *dummy) {}# define EPROTONOSUPPORT EINVAL#endif +int ffs (int i); +int setenv (const char *name,const char *value,int overwrite); + +typedef struct { +long tv_sec; +long tv_usec; +}qemu_timeval ; +int qemu_gettimeofday (qemu_timeval *tp); +#endif
qemu-tool.c +1 -0
@@ -15,6 +15,7 @@#include "monitor.h" #include "qemu-timer.h" #include "qemu-log.h" +#include "sysemu.h" #include <sys/time.h>
Signature · 1 line
1.7.2.3
[Qemu-devel] Re: [PATCH 5/9] Move qemu_gettimeofday() to OS specific files
Quoted 4 lines · On Sat, Oct 16, 2010 at 4:04 PM, <[email protected]> wrote:
From: Jes Sorensen <[email protected]>
In addition add
sysemu.hincludes to file requiring a prototype forffs()
There are probably a lot more files which would need that:
/src/qemu/hw/sd.c: In function 'sd_normal_command':
/src/qemu/hw/sd.c:738:13: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration] /src/qemu/hw/max7310.c: In function 'max7310_tx':
/src/qemu/hw/max7310.c:94:13: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration] /src/qemu/hw/unin_pci.c: In function 'unin_get_config_reg':
/src/qemu/hw/unin_pci.c:101:9: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration]
Perhaps the prototype should be added someplace else.
[Qemu-devel] Re: [PATCH 5/9] Move qemu_gettimeofday() to OS specific files
Quoted 17 lines · Blue Swirl
Quoted 4 lines · On Sat, Oct 16, 2010 at 4:04 PM, <[email protected]> wrote:
From: Jes Sorensen <[email protected]>
In addition add
sysemu.hincludes to file requiring a prototype forffs()There are probably a lot more files which would need that:
/src/qemu/hw/sd.c: In function 'sd_normal_command':
/src/qemu/hw/sd.c:738:13: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration] /src/qemu/hw/max7310.c: In function 'max7310_tx':
/src/qemu/hw/max7310.c:94:13: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration] /src/qemu/hw/unin_pci.c: In function 'unin_get_config_reg':
/src/qemu/hw/unin_pci.c:101:9: error: implicit declaration of function 'ffs' [-Werror=implicit-function-declaration]
Quoted 1 line
Perhaps the prototype should be added someplace else.
I guess we'll have to bite the bullet. I don't really like it, but I moved it to qemu-common.h to be consistent with the POSIX code. POSIX relies on ffs() to be provided by strings.h which we include in qemu-common.h
Should build (I hope) in the next patch. I tried building arm-softmmu here but it wouldn't build for me at all due to other things so I couldn't test it.
Cheers,
Jes
PATCH v2 6/9 Do not redefine reserved key-words TRUE/FALSE
In reply to [email protected]From: Jes Sorensen <[email protected]>
TRUE/FALSE are generally reserved keywords and shouldn't be defined in a driver like this. Rename the macros to SDP_TRUE and SDP_FALSE respectively.
--- hw/bt-sdp.c | 20 ++++++++++---------- 1 files changed, 10 insertions(+), 10 deletions(-)
hw/bt-sdp.c +10 -10
@@ -787,11 +787,11 @@ static void sdp_service_db_build(struct bt_l2cap_sdp_state_s *sdp, .type = SDP_DTYPE_UUID | SDP_DSIZE_16, \ .value.uint = val, \ }, -#define TRUE { \ +#define SDP_TRUE { \ .type = SDP_DTYPE_BOOL | SDP_DSIZE_1, \ .value.uint = 1, \ }, -#define FALSE { \ +#define SDP_FALSE { \ .type = SDP_DTYPE_BOOL | SDP_DSIZE_1, \ .value.uint = 0, \ },
@@ -843,8 +843,8 @@ SERVICE(hid,/* TODO: extract from l2cap_device->device.class[0] */ ATTRIBUTE(DEVICE_SUBCLASS, UINT8(0x40)) ATTRIBUTE(COUNTRY_CODE, UINT8(0x15)) - ATTRIBUTE(VIRTUAL_CABLE, TRUE) -ATTRIBUTE (RECONNECT_INITIATE , FALSE) + ATTRIBUTE(VIRTUAL_CABLE, SDP_TRUE) +ATTRIBUTE (RECONNECT_INITIATE ,SDP_FALSE ) /* TODO: extract from hid->usbdev->report_desc */ ATTRIBUTE(DESCRIPTOR_LIST, LIST( LIST(UINT8(0x22) ARRAY(
@@ -884,12 +884,12 @@ SERVICE(hid,ATTRIBUTE (LANG_ID_BASE_LIST ,LIST ( LIST(UINT16(0x0409) UINT16(0x0100)) )) - ATTRIBUTE(SDP_DISABLE, FALSE) - ATTRIBUTE(BATTERY_POWER, TRUE) - ATTRIBUTE(REMOTE_WAKEUP, TRUE) - ATTRIBUTE(BOOT_DEVICE, TRUE) /* XXX: untested */ + ATTRIBUTE(SDP_DISABLE, SDP_FALSE) + ATTRIBUTE(BATTERY_POWER, SDP_TRUE) + ATTRIBUTE(REMOTE_WAKEUP, SDP_TRUE) + ATTRIBUTE(BOOT_DEVICE, SDP_TRUE) /* XXX: untested */ ATTRIBUTE(SUPERVISION_TIMEOUT, UINT16(0x0c80)) - ATTRIBUTE(NORMALLY_CONNECTABLE, TRUE) + ATTRIBUTE(NORMALLY_CONNECTABLE, SDP_TRUE)ATTRIBUTE (PROFILE_VERSION ,UINT16 (0x0100 )) )
@@ -937,7 +937,7 @@ SERVICE(pnp,/* Profile specific */ ATTRIBUTE(SPECIFICATION_ID, UINT16(0x0100)) ATTRIBUTE(VERSION, UINT16(0x0100)) - ATTRIBUTE(PRIMARY_RECORD, TRUE) + ATTRIBUTE(PRIMARY_RECORD, SDP_TRUE) ) static int bt_l2cap_sdp_new_ch(struct bt_l2cap_device_s *dev,
Signature · 1 line
1.7.2.3
PATCH v2 7/9 Separate qemu_pidfile() into OS specific versions
In reply to [email protected]From: Jes Sorensen <[email protected]>
--- os-posix.c | 21 +++++++++++++++++++++ os-win32.c | 24 ++++++++++++++++++++++++ osdep.c | 38 -------------------------------------- 3 files changed, 45 insertions(+), 38 deletions(-)
os-posix.c +21 -0
@@ -361,3 +361,24 @@ int qemu_eventfd(int fds[2])return qemu_pipe (fds); } + +int qemu_create_pidfile (const char *filename) +{ +char buffer[128 ]; +int len; +int fd; + + fd =qemu_open (filename,O_RDWR |O_CREAT ,0600 ); +if (fd ==-1 ) { +return -1 ; + } +if (lockf (fd,F_TLOCK ,0 ) ==-1 ) { +return -1 ; + } + len =snprintf (buffer,sizeof (buffer),"%ld\n" , (long )getpid ()); +if (write (fd, buffer, len) != len) { +return -1 ; + } + +return 0 ; +}
os-win32.c +24 -0
@@ -240,3 +240,27 @@ void os_pidfile_error(void) {fprintf (stderr,"Could not acquire pid file: %s\n" ,strerror (errno)); } + +int qemu_create_pidfile (const char *filename) +{ +char buffer[128 ]; +int len; +HANDLE file; +OVERLAPPED overlap; +BOOL ret; +memset (&overlap,0 ,sizeof (overlap)); + + file =CreateFile (filename,GENERIC_WRITE ,FILE_SHARE_READ ,NULL , +OPEN_ALWAYS ,FILE_ATTRIBUTE_NORMAL ,NULL ); + +if (file ==INVALID_HANDLE_VALUE ) { +return -1 ; + } + len =snprintf (buffer,sizeof (buffer),"%ld\n" , (long )getpid ()); + ret =WriteFileEx (file, (LPCVOID )buffer, (DWORD )len, + &overlap,NULL ); +if (ret ==0 ) { +return -1 ; + } +return 0 ; +}
osdep.c +0 -38
@@ -73,44 +73,6 @@ int qemu_madvise(void *addr, size_t len, int advice)#endif } -int qemu_create_pidfile (const char *filename) -{ -char buffer[128 ]; -int len; -#ifndef _WIN32 -int fd; - - fd =qemu_open (filename,O_RDWR |O_CREAT ,0600 ); -if (fd ==-1 ) -return -1 ; - -if (lockf (fd,F_TLOCK ,0 ) ==-1 ) -return -1 ; - - len =snprintf (buffer,sizeof (buffer),"%ld\n" , (long )getpid ()); -if (write (fd, buffer, len) != len) -return -1 ; -#else -HANDLE file; -OVERLAPPED overlap; -BOOL ret; -memset (&overlap,0 ,sizeof (overlap)); - - file =CreateFile (filename,GENERIC_WRITE ,FILE_SHARE_READ ,NULL , -OPEN_ALWAYS ,FILE_ATTRIBUTE_NORMAL ,NULL ); - -if (file ==INVALID_HANDLE_VALUE ) -return -1 ; - - len =snprintf (buffer,sizeof (buffer),"%ld\n" , (long )getpid ()); - ret =WriteFileEx (file, (LPCVOID )buffer, (DWORD )len, - &overlap,NULL ); -if (ret ==0 ) -return -1 ; -#endif -return 0 ; -} -/* * Opens a file with FD_CLOEXEC set
Signature · 1 line
1.7.2.3
PATCH v2 8/9 Consolidate oom_check() functions
In reply to [email protected]From: Jes Sorensen <[email protected]>
This consolidates the duplicated oom_check() functions, as well as splitting them into OS dependant versions to avoid the #ifdef grossness that was present in the old osdep.c version.
--- oslib-posix.c | 8 +++----- oslib-win32.c | 6 +++--- qemu-common.h | 1 + qemu-malloc.c | 14 +++----------- 4 files changed, 10 insertions(+), 19 deletions(-)
oslib-posix.c +3 -5
@@ -31,8 +31,7 @@#include "trace.h" #include "qemu_socket.h" -#if !defined(_POSIX_C_SOURCE) || defined(__sun__) -static void *oom_check (void *ptr) +void *qemu_oom_check (void *ptr) {if (ptr ==NULL ) {fprintf (stderr,"Failed to allocate memory: %s\n" ,strerror (errno));
@@ -40,7 +39,6 @@ static void *oom_check(void *ptr) }return ptr; } -#endif void *qemu_memalign (size_t alignment,size_t size) {
@@ -54,9 +52,9 @@ void *qemu_memalign(size_t alignment, size_t size) abort(); }#elif defined(CONFIG_BSD) - ptr =oom_check (valloc (size)); + ptr =qemu_oom_check (valloc (size));#else - ptr =oom_check (memalign (alignment, size)); + ptr =qemu_oom_check (memalign (alignment, size));#endif trace_qemu_memalign (alignment, size, ptr);return ptr;
oslib-win32.c +3 -3
@@ -31,7 +31,7 @@#include "trace.h" #include "qemu_socket.h" -static void *oom_check (void *ptr) +void *qemu_oom_check (void *ptr) {if (ptr ==NULL ) {fprintf (stderr,"Failed to allocate memory: %lu\n" ,GetLastError ());
@@ -47,7 +47,7 @@ void *qemu_memalign(size_t alignment, size_t size)if (!size) {abort (); } - ptr =oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE )); + ptr =qemu_oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE ));trace_qemu_memalign (alignment, size, ptr);return ptr; }
@@ -62,7 +62,7 @@ void *qemu_vmalloc(size_t size)if (!size) {abort (); } - ptr =oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE )); + ptr =qemu_oom_check (VirtualAlloc (NULL , size,MEM_COMMIT ,PAGE_READWRITE ));trace_qemu_vmalloc (size, ptr);return ptr; }
qemu-common.h +1 -0
@@ -174,6 +174,7 @@ const char *path(const char *pathname);#define qemu_isascii (c) isascii((unsigned char)(c))#define qemu_toascii (c) toascii((unsigned char)(c)) +void *qemu_oom_check (void *ptr);void *qemu_malloc (size_t size);void *qemu_realloc (void *ptr,size_t size);void *qemu_mallocz (size_t size);
qemu-malloc.c +3 -11
@@ -25,14 +25,6 @@#include "trace.h" #include <stdlib.h> -static void *oom_check (void *ptr) -{ -if (ptr ==NULL ) { -abort (); - } -return ptr; -} -void qemu_free (void *ptr) {trace_qemu_free (ptr);
@@ -54,7 +46,7 @@ void *qemu_malloc(size_t size)if (!size && !allow_zero_malloc ()) {abort (); } - ptr =oom_check (malloc (size ? size :1 )); + ptr =qemu_oom_check (malloc (size ? size :1 ));trace_qemu_malloc (size, ptr);return ptr; }
@@ -65,7 +57,7 @@ void *qemu_realloc(void *ptr, size_t size)if (!size && !allow_zero_malloc ()) {abort (); } - newptr =oom_check (realloc (ptr, size ? size :1 )); + newptr =qemu_oom_check (realloc (ptr, size ? size :1 ));trace_qemu_realloc (ptr, size, newptr);return newptr; }
@@ -75,7 +67,7 @@ void *qemu_mallocz(size_t size)if (!size && !allow_zero_malloc ()) {abort (); } -return oom_check (calloc (1 , size ? size :1 )); +return qemu_oom_check (calloc (1 , size ? size :1 )); }char *qemu_strdup (const char *str)
Signature · 1 line
1.7.2.3
[Qemu-devel] Re: [PATCH 8/9] Consolidate oom_check() functions
Quoted 5 lines · On Sat, Oct 16, 2010 at 4:04 PM, <[email protected]> wrote:
From: Jes Sorensen <[email protected]>
This consolidates the duplicated
oom_check()functions, as well as splitting them into OS dependant versions to avoid the #ifdef grossness that was present in the oldosdep.cversion.
This would break user emulators:
LINK i386-linux-user/qemu-i386
qemu-malloc.o: In function `qemu_realloc':
/src/qemu/qemu-malloc.c:60: undefined reference to qemu_oom_check' qemu-malloc.o: In function qemu_malloc':
/src/qemu/qemu-malloc.c:49: undefined reference to qemu_oom_check' qemu-malloc.o: In function qemu_mallocz':
/src/qemu/qemu-malloc.c:70: undefined reference to `qemu_oom_check'
collect2: ld returned 1 exit status
[Qemu-devel] Re: [PATCH 8/9] Consolidate oom_check() functions
Quoted 7 lines · Blue Swirl
Quoted 5 lines · On Sat, Oct 16, 2010 at 4:04 PM, <[email protected]> wrote:
From: Jes Sorensen <[email protected]>
This consolidates the duplicated
oom_check()functions, as well as splitting them into OS dependant versions to avoid the #ifdef grossness that was present in the oldosdep.cversion.
Quoted 9 lines
This would break user emulators:
LINK i386-linux-user/qemu-i386
qemu-malloc.o: In function `qemu_realloc':
/src/qemu/qemu-malloc.c:60: undefined reference toqemu_oom_check' qemu-malloc.o: In functionqemu_malloc':
/src/qemu/qemu-malloc.c:49: undefined reference toqemu_oom_check' qemu-malloc.o: In functionqemu_mallocz':
/src/qemu/qemu-malloc.c:70: undefined reference to `qemu_oom_check'
collect2: ld returned 1 exit status
I'll have a look.
Thanks,
Jes
PATCH v2 9/9 Remove unncessary includes
In reply to [email protected]From: Jes Sorensen <[email protected]>
No need to include stdlib.h for BSD as it is included by qemu-common.h, windows.h is handled by sysemu.h and osdep.c no longer needs malloc.h
--- osdep.c | 8 -------- 1 files changed, 0 insertions(+), 8 deletions(-)
osdep.c +0 -8
@@ -44,14 +44,6 @@extern int madvise (caddr_t ,size_t ,int );#endif -#ifdef _WIN32 -#include <windows.h> -#elif defined(CONFIG_BSD ) -#include <stdlib.h> -#else -#include <malloc.h> -#endif -#include "qemu-common.h" #include "trace.h" #include "sysemu.h"
Signature · 1 line
1.7.2.3