Darwin: Fix compilation warning regarding the deprecated daemon() function
authorAlexandre Raymond <cerbere@gmail.com>
Tue, 7 Jun 2011 03:34:10 +0000 (23:34 -0400)
committerAndreas Färber <andreas.faerber@web.de>
Tue, 14 Jun 2011 01:10:47 +0000 (03:10 +0200)
Changes since v1: create a wrapper function named qemu_daemon() in oslib-posix.c
instead of putting the OS specific workaround in qemu-nbd.c directly.

On OSX >= 10.5, daemon() is deprecated, resulting in the following warning:
----8<----
qemu-nbd.c: In function ‘main’:
qemu-nbd.c:371: warning: ‘daemon’ is deprecated (declared at /usr/include/stdlib.h:289)
----8<----

The following trick, used in mDNSResponder, takes care of this warning:
http://www.opensource.apple.com/source/mDNSResponder/mDNSResponder-258.18/mDNSPosix/PosixDaemon.c

On OSX, it temporarily renames the daemon() function before including stdlib.h
and declares it manually as an extern function. This way, the compiler does not
see the declaration from stdlib.h and thus does not display the warning.

Signed-off-by: Alexandre Raymond <cerbere@gmail.com>
Cc: Blue Swirl <blauwirbel@gmail.com>
Signed-off-by: Andreas Färber <andreas.faerber@web.de>
osdep.h
oslib-posix.c
qemu-nbd.c

diff --git a/osdep.h b/osdep.h
index 970d767078b6760e3da4cd6489d011bf6e34de34..6eb9a49ec8b75a4814a74c9af765d0ed1c51b33e 100644 (file)
--- a/osdep.h
+++ b/osdep.h
@@ -88,6 +88,7 @@
 # define QEMU_GNUC_PREREQ(maj, min) 0
 #endif
 
+int qemu_daemon(int nochdir, int noclose);
 void *qemu_memalign(size_t alignment, size_t size);
 void *qemu_vmalloc(size_t size);
 void qemu_vfree(void *ptr);
index 7bc5f7cf0951d047125c2498f812da1c5650529a..3a18e865f399e4e454a03527b471fd91a1f9b252 100644 (file)
  * THE SOFTWARE.
  */
 
+/* The following block of code temporarily renames the daemon() function so the
+   compiler does not see the warning associated with it in stdlib.h on OSX */
+#ifdef __APPLE__
+#define daemon qemu_fake_daemon_function
+#include <stdlib.h>
+#undef daemon
+extern int daemon(int, int);
+#endif
+
 #include "config-host.h"
 #include "sysemu.h"
 #include "trace.h"
 #include "qemu_socket.h"
 
+
+
+int qemu_daemon(int nochdir, int noclose)
+{
+    return daemon(nochdir, noclose);
+}
+
 void *qemu_oom_check(void *ptr)
 {
     if (ptr == NULL) {
index 110d78e6a44da7bc8710080f01547bd620f9838b..d91c02ce4976cc196ce9c3a97d5ce8b86dfc4684 100644 (file)
@@ -359,7 +359,7 @@ int main(int argc, char **argv)
 
         if (!verbose) {
             /* detach client and server */
-            if (daemon(0, 0) == -1) {
+            if (qemu_daemon(0, 0) == -1) {
                 err(EXIT_FAILURE, "Failed to daemonize");
             }
         }