Daemonize after transports are created and listening sockets are bound to appropriate ports. This allows to exit with nozero exit code when the binding fails.

git-svn-id: file:///home/hardaker/lib/sf-bkups/net-snmp-convert-svnrepo/trunk@17313 06827809-a52a-0410-b366-d66718629ded
diff --git a/agent/snmpd.c b/agent/snmpd.c
index f786369..da38aec 100644
--- a/agent/snmpd.c
+++ b/agent/snmpd.c
@@ -903,19 +903,6 @@
 #ifdef BUFSIZ
     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
 #endif
-    /*
-     * Initialize the world.  Detach from the shell.  Create initial user.  
-     */
-    if(!dont_fork) {
-        int quit = ! netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
-                                            NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
-        ret = netsnmp_daemonize(quit, snmp_stderrlog_status());
-        /*
-         * xxx-rks: do we care if fork fails? I think we should...
-         */
-        if(ret != 0)
-            Exit(1);                /*  Exit logs exit val for us  */
-    }
 
     SOCK_STARTUP;
     init_agent(app_name);        /* do what we need to do first. */
@@ -933,6 +920,20 @@
         Exit(1);                /*  Exit logs exit val for us  */
     }
 
+    /*
+     * Initialize the world.  Detach from the shell.  Create initial user.  
+     */
+    if(!dont_fork) {
+        int quit = ! netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
+                                            NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
+        ret = netsnmp_daemonize(quit, snmp_stderrlog_status());
+        /*
+         * xxx-rks: do we care if fork fails? I think we should...
+         */
+        if(ret != 0)
+            Exit(1);                /*  Exit logs exit val for us  */
+    }
+
 #if HAVE_GETPID
     if (pid_file != NULL) {
         /*
diff --git a/apps/snmptrapd.c b/apps/snmptrapd.c
index 71351f8..480e910 100644
--- a/apps/snmptrapd.c
+++ b/apps/snmptrapd.c
@@ -1084,55 +1084,6 @@
 #endif        
     }
 
-#ifndef WIN32
-    /*
-     * fork the process to the background if we are not printing to stderr 
-     */
-    if (dofork && netsnmp_running) {
-        int             fd;
-
-        switch (fork()) {
-        case -1:
-            fprintf(stderr, "bad fork - %s\n", strerror(errno));
-            _exit(1);
-
-        case 0:
-            /*
-             * become process group leader 
-             */
-            if (setsid() == -1) {
-                fprintf(stderr, "bad setsid - %s\n", strerror(errno));
-                _exit(1);
-            }
-
-            /*
-             * if we are forked, we don't want to print out to stdout or stderr 
-             */
-            fd = open("/dev/null", O_RDWR);
-            dup2(fd, STDIN_FILENO);
-            dup2(fd, STDOUT_FILENO);
-            dup2(fd, STDERR_FILENO);
-            close(fd);
-            break;
-
-        default:
-            _exit(0);
-        }
-    }
-#endif                          /* WIN32 */
-#if HAVE_GETPID
-    if (pid_file != NULL) {
-        if ((PID = fopen(pid_file, "w")) == NULL) {
-            snmp_log_perror("fopen");
-            exit(1);
-        }
-        fprintf(PID, "%d\n", (int) getpid());
-        fclose(PID);
-        free_config_pidFile();
-    }
-#endif
-
-    snmp_log(LOG_INFO, "NET-SNMP version %s\n", netsnmp_get_version());
     SOCK_STARTUP;
 
     if (listen_ports)
@@ -1184,6 +1135,56 @@
         }
     }
 
+#ifndef WIN32
+    /*
+     * fork the process to the background if we are not printing to stderr 
+     */
+    if (dofork && netsnmp_running) {
+        int             fd;
+
+        switch (fork()) {
+        case -1:
+            fprintf(stderr, "bad fork - %s\n", strerror(errno));
+            _exit(1);
+
+        case 0:
+            /*
+             * become process group leader 
+             */
+            if (setsid() == -1) {
+                fprintf(stderr, "bad setsid - %s\n", strerror(errno));
+                _exit(1);
+            }
+
+            /*
+             * if we are forked, we don't want to print out to stdout or stderr 
+             */
+            fd = open("/dev/null", O_RDWR);
+            dup2(fd, STDIN_FILENO);
+            dup2(fd, STDOUT_FILENO);
+            dup2(fd, STDERR_FILENO);
+            close(fd);
+            break;
+
+        default:
+            _exit(0);
+        }
+    }
+#endif                          /* WIN32 */
+#if HAVE_GETPID
+    if (pid_file != NULL) {
+        if ((PID = fopen(pid_file, "w")) == NULL) {
+            snmp_log_perror("fopen");
+            exit(1);
+        }
+        fprintf(PID, "%d\n", (int) getpid());
+        fclose(PID);
+        free_config_pidFile();
+    }
+#endif
+
+    snmp_log(LOG_INFO, "NET-SNMP version %s\n", netsnmp_get_version());
+
     /*
      * ignore early sighup during startup
      */