Hi all,
I found (at least I hope) ?regular? solution for mysterious FreeBSD hangs,
when ntop running in daemon mode.  The core of problem lives in fork() and
pcap_  initialization sequence. I?m not sure if its feature or bug in FreeBSD
kernel, but looks like fork() corrupt  pcap interface functionality. Simply,
pcap_open_live() and pcap_dispatch() must be called from one process context.
The sequence of  pcap_open_live() ,  fork() and  pcap_dispatch() not works, but
fork(),  pcap_open_live() and pcap_dispatch()  works without problems.

Yes, the pcap_open_live() is  the root of this problem  ?  if  fork() is called
before pcap_open_live() then all works, if its called after, then not.


The attached patch is tested on FreeBSD 4.9 and FreeBSD 5.2 ? works for all
combinations of ?K ?d switches  without problem (and without --set-pcap-
nonblocking).

Michal Meloun

--- ntop.c.orig Tue Feb  3 11:16:38 2004
+++ ntop.c      Tue Feb  3 11:36:03 2004
@@ -169,8 +169,7 @@
                            childpid ? "parent" : "child", childpid);
 #endif
     if(!childpid) { /* child */
-      traceEvent(CONST_TRACE_INFO, "INIT: Bye bye: I'm becoming a daemon...");
-      detachFromTerminal(1);
+      traceEvent(CONST_TRACE_INFO, "INIT: daemonize...");
     } else { /* father */
       traceEvent(CONST_TRACE_INFO, "INIT: Parent process is exiting (this is 
normal)");
       exit(0);
@@ -185,6 +184,9 @@
   alarm(120); /* Don't freeze */
 #endif

+  /* Last message on terminal, before switching to syslog  */
+  traceEvent(CONST_TRACE_INFO, "INIT: Bye bye: I'm becoming a daemon...");
+
 #if !defined(WIN32) && defined(MAKE_WITH_SYSLOG)
   /* Child processes must log to syslog.
    * If no facility was set through -L | --use-syslog=facility
@@ -195,22 +197,24 @@
 #endif

   if(doChdir) chdir("/");
-  setsid();  /* detach from the terminal */
-
+
   fclose(stdin);
   fclose(stdout);
   /* fclose(stderr); */

+  setsid();  /* detach from the terminal */
+
   /*
    * clear any inherited file mode creation mask
    */
   umask (0);

   /*
-   * Use linebuffered stdout
+   * Use linebuffered stdout
    */
   /* setlinebuf (stdout); */
-  setvbuf(stdout, (char *)NULL, _IOLBF, 0);
+  /* stdout is closed already */
+  /* setvbuf(stdout, (char *)NULL, _IOLBF, 0); */

 }
 #endif /* WIN32 */




--- globals-core.c.orig Tue Feb  3 11:17:14 2004
+++ globals-core.c      Tue Feb  3 11:32:10 2004
@@ -534,6 +534,15 @@
 void initNtop(char *devices) {
   char value[32];

+#ifndef WIN32
+  if(myGlobals.daemonMode) {
+    /* deamonize as far as possible, so rest of initialization runs
+       in daemon process context*/
+    daemonize();
+    traceEvent(CONST_TRACE_ALWAYSDISPLAY, "Now running as a daemon");
+  }
+#endif
+
   initIPServices();

   handleProtocols();
@@ -552,11 +561,9 @@
   /* ********************************** */

   initGdbm(myGlobals.dbPath, myGlobals.spoolPath, 0);
-
 #ifndef WIN32
   if(myGlobals.daemonMode) {
-    daemonize();
-    traceEvent(CONST_TRACE_ALWAYSDISPLAY, "Now running as a daemon");
+    detachFromTerminal(1);
   }
 #endif


_______________________________________________
Ntop-dev mailing list
[EMAIL PROTECTED]
http://listgateway.unipi.it/mailman/listinfo/ntop-dev

Reply via email to