1diff -I '\$Id: ' -u -r -b -w -p -d --new-file --exclude-from=/Users/rstory/.rcfiles/diff-ignore SVN/snmplib/system.c APPLE/snmplib/system.c 2--- SVN/snmplib/system.c 3+++ APPLE/snmplib/system.c 4@@ -138,6 +138,10 @@ SOFTWARE. 5 #include <sys/systeminfo.h> 6 #endif 7 8+#if defined(darwin9) 9+#include <crt_externs.h> /* for _NSGetArgv() */ 10+#endif 11+ 12 #include <net-snmp/types.h> 13 #include <net-snmp/output_api.h> 14 #include <net-snmp/utilities.h> 15@@ -156,6 +160,31 @@ SOFTWARE. 16 # define LOOPBACK 0x7f000001 17 #endif 18 19+static void 20+_daemon_prep(int stderr_log) 21+{ 22+ /* Avoid keeping any directory in use. */ 23+ chdir("/"); 24+ 25+ if (stderr_log) 26+ return; 27+ 28+ /* 29+ * Close inherited file descriptors to avoid 30+ * keeping unnecessary references. 31+ */ 32+ close(0); 33+ close(1); 34+ close(2); 35+ 36+ /* 37+ * Redirect std{in,out,err} to /dev/null, just in case. 38+ */ 39+ open("/dev/null", O_RDWR); 40+ dup(0); 41+ dup(0); 42+} 43+ 44 /** 45 * fork current process into the background. 46 * 47@@ -184,6 +213,22 @@ netsnmp_daemonize(int quit_immediately, 48 int i = 0; 49 DEBUGMSGT(("daemonize","deamonizing...\n")); 50 #if HAVE_FORK 51+#if defined(darwin9) 52+ char path [PATH_MAX] = ""; 53+ uint32_t size = sizeof (path); 54+ 55+ /* 56+ * if we are already launched in a "daemonized state", just 57+ * close & redirect the file descriptors 58+ */ 59+ if(getppid() <= 2) { 60+ _daemon_prep(stderr_log); 61+ return 0; 62+ } 63+ 64+ if (_NSGetExecutablePath (path, &size)) 65+ return -1; 66+#endif 67 /* 68 * Fork to return control to the invoking process and to 69 * guarantee that we aren't a process group leader. 70@@ -225,26 +270,23 @@ netsnmp_daemonize(int quit_immediately, 71 72 DEBUGMSGT(("daemonize","child continuing\n")); 73 74- /* Avoid keeping any directory in use. */ 75- chdir("/"); 76- 77- if (!stderr_log) { 78- /* 79- * Close inherited file descriptors to avoid 80- * keeping unnecessary references. 81- */ 82- close(0); 83- close(1); 84- close(2); 85- 86+#if ! defined(darwin9) 87+#error "darwin9 MUST be defined for Apple builds" 88+ _daemon_prep(stderr_log); 89+#else 90 /* 91- * Redirect std{in,out,err} to /dev/null, just in 92- * case. 93+ * Some darwin calls (using mach ports) don't work after 94+ * a fork. So, now that we've forked, we re-exec ourself 95+ * to ensure that the child's mach ports are all set up correctly, 96+ * the getppid call above will prevent the exec child from 97+ * forking... 98 */ 99- open("/dev/null", O_RDWR); 100- dup(0); 101- dup(0); 102- } 103+ char * const *argv = *_NSGetArgv (); 104+ DEBUGMSGT(("daemonize","re-execing forked child\n")); 105+ execv (path, argv); 106+ snmp_log(LOG_ERR,"Forked child unable to re-exec - %s.\n", strerror (errno)); 107+ exit (0); 108+#endif 109 } 110 #endif /* !WIN32 */ 111 } 112