فهرست منبع

* Now using daemon()

svn: 1074
Bryan Drewery 22 سال پیش
والد
کامیت
eb0194c937
4فایلهای تغییر یافته به همراه38 افزوده شده و 112 حذف شده
  1. 1 0
      doc/UPDATES
  2. 23 78
      src/bg.c
  3. 2 2
      src/bg.h
  4. 12 32
      src/main.c

+ 1 - 0
doc/UPDATES

@@ -33,6 +33,7 @@ This is a summary of ChangeLog basically.
 29.Added cmd: swhois, see help.
 30.Rewrote nick generator on 'nick in use'
 31.Detected promisc now shows which interface.
+32.Now using daemon(3) to fork into background.
 
 1.1.7
 

+ 23 - 78
src/bg.c

@@ -20,43 +20,40 @@
 time_t lastfork = 0;
 pid_t watcher;                  /* my child/watcher */
 
-/* Do everything we normally do after we have split off a new
- * process to the background. This includes writing a PID file
- * and informing the user of the split.
- */
-static void bg_do_detach(pid_t) __attribute__ ((noreturn));
+pid_t
+do_fork()
+{
+  pid_t pid = 0;
 
-static void
-bg_do_detach(pid_t p)
+  if (daemon(1, 1))
+    fatal(strerror(errno), 0);
+
+  pid = getpid();
+  writepid(conf.bot->pid_file, pid);
+  lastfork = now;
+  return pid;
+}
+
+void
+writepid(const char *pidfile, pid_t pid)
 {
   FILE *fp = NULL;
 
   /* Need to attempt to write pid now, not later. */
-  unlink(conf.bot->pid_file);
-  fp = fopen(conf.bot->pid_file, "w");
-  if (fp != NULL) {
-    fprintf(fp, "%u\n", p);
+  unlink(pidfile);
+  if ((fp = fopen(pidfile, "w"))) {
+    fprintf(fp, "%u\n", pid);
     if (fflush(fp)) {
       /* Kill bot incase a botchk is run from crond. */
-      printf(EGG_NOWRITE, conf.bot->pid_file);
+      printf(EGG_NOWRITE, pidfile);
       printf("  Try freeing some disk space\n");
       fclose(fp);
-      unlink(conf.bot->pid_file);
+      unlink(pidfile);
       exit(1);
-    }
-    fclose(fp);
+    } else
+      fclose(fp);
   } else
-    printf(EGG_NOWRITE, conf.bot->pid_file);
-#ifdef HUB
-  printf("Launched into the background  (pid: %d)\n\n", p);
-#else /* LEAF */
-  printf("%s launched into the background  (pid: %d)\n\n", conf.bot->nick, p);
-#endif /* HUB */
-
-#ifndef CYGWIN_HACKS
-  setpgid(p, p);
-#endif /* !CYGWIN_HACKS */
-  exit(0);
+    printf(EGG_NOWRITE, pidfile);
 }
 
 static void
@@ -105,55 +102,3 @@ init_watcher()
   }
 }
 
-void
-bg_do_split(void)
-{
-  /* Split off a new process.
-   */
-  int xx = fork();
-
-  if (xx == -1)
-    fatal("CANNOT FORK PROCESS.", 0);
-  if (xx != 0)                  /* parent */
-    bg_do_detach(xx);
-  else                          /* child */
-    init_watcher();
-/*    init_thread(getpid()); */
-#ifdef CRAZY_TRACE
-  /* block ptrace? */
-#  ifdef __linux__
-  ptrace(PTRACE_TRACEME, 0, NULL, NULL);
-#  endif
-#  ifdef BSD
-  ptrace(PT_TRACE_ME, 0, NULL, NULL);
-#  endif /* BSD */
-#endif /* CRAZY_TRACE */
-
-}
-
-void
-do_fork()
-{
-  int xx;
-
-  xx = fork();
-  if (xx == -1)
-    return;
-  if (xx != 0) {
-    FILE *fp;
-
-    unlink(conf.bot->pid_file);
-    fp = fopen(conf.bot->pid_file, "w");
-    if (fp != NULL) {
-      fprintf(fp, "%u\n", xx);
-      fclose(fp);
-    }
-  }
-  if (xx) {
-#if HAVE_SETPGID
-    setpgid(xx, xx);
-#endif
-    exit(0);
-  }
-  lastfork = now;
-}

+ 2 - 2
src/bg.h

@@ -6,7 +6,7 @@
 extern time_t 		lastfork;
 extern pid_t 		watcher;
 
-void do_fork();
-void bg_do_split();
+pid_t do_fork();
+void writepid(const char *, pid_t);
 
 #endif /* !_BG_H */

+ 12 - 32
src/main.c

@@ -8,6 +8,7 @@
 
 #include "common.h"
 #include "main.h"
+#include "color.h"
 #include "binary.h"
 #include "dcc.h"
 #include "misc.h"
@@ -823,45 +824,24 @@ int main(int argc, char **argv)
   /* we don't split cygwin because to run as a service the bot shouldn't exit.
      confuses windows ;)
    */
+  use_stderr = 0;		/* stop writing to stderr now! */
   if (backgrd) {
 #ifndef CYGWIN_HACKS
-    bg_do_split();
-  } else {
-#endif /* !CYGWIN_HACKS */
-    FILE *f = NULL;
-    int xx;
-
-    xx = getpid();
-    /* Write pid to file */
-    unlink(conf.bot->pid_file);
-    if ((f = fopen(conf.bot->pid_file, "w")) != NULL) {
-      fprintf(f, "%u\n", xx);
-      if (fflush(f)) {
-        printf(EGG_NOWRITE, conf.bot->pid_file);
-        unlink(conf.bot->pid_file);
-      }
-      fclose(f);
-    } else
-      printf(EGG_NOWRITE, conf.bot->pid_file);
-#ifdef CYGWIN_HACKS
-      printf("Launched into the background  (pid: %d)\n\n", xx);
-#endif
-  }
+    pid_t pid = do_fork();
 
-  use_stderr = 0;		/* Stop writing to stderr now */
-  if (backgrd) {
-    /* Ok, try to disassociate from controlling terminal (finger cross) */
-#ifndef CYGWIN_HACKS
-    setpgid(0, 0);
+    writepid(conf.bot->pid_file, pid);
+    printf("%s%s%c%s%s%s l%sA%su%sN%sc%sH%se%sD%s %s(%s%d%s)%s\n",
+            RED(-1), BOLD(-1), conf.bot->nick[0], BOLD_END(-1), &conf.bot->nick[1],
+            COLOR_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1), BOLD(-1), BOLD_END(-1),
+            BOLD(-1), BOLD_END(-1), YELLOW(-1), COLOR_END(-1), pid, YELLOW(-1), COLOR_END(-1));
+  
+    printf("%s launched into the background  (pid: %d)\n\n", conf.bot->nick, pid);
+  } else {
 #endif /* !CYGWIN_HACKS */
-    /*
-    freopen("/dev/null", "r", stdin);
-    freopen("/dev/null", "w", stdout);
-    freopen("/dev/null", "w", stderr);
-    */
 #ifdef CYGWIN_HACKS
     FreeConsole();
 #endif /* CYGWIN_HACKS */
+    printf("%s launched (not backgrounding)\n\n", conf.bot->nick);
   }
 
   /* Terminal emulating dcc chat */