[Pkg-ocaml-maint-commits] r1719 - trunk/packages/mldonkey/trunk/debian/utils

Sylvain LE GALL gildor-guest at costa.debian.org
Tue Aug 30 21:43:08 UTC 2005


Author: gildor-guest
Date: 2005-08-30 21:43:08 +0000 (Tue, 30 Aug 2005)
New Revision: 1719

Modified:
   trunk/packages/mldonkey/trunk/debian/utils/mldonkey_server.ml
Log:
Correct some typos and remove stale pidfile -- SLG


Modified: trunk/packages/mldonkey/trunk/debian/utils/mldonkey_server.ml
===================================================================
--- trunk/packages/mldonkey/trunk/debian/utils/mldonkey_server.ml	2005-08-30 21:00:26 UTC (rev 1718)
+++ trunk/packages/mldonkey/trunk/debian/utils/mldonkey_server.ml	2005-08-30 21:43:08 UTC (rev 1719)
@@ -333,10 +333,11 @@
 ;;
 
 let go_home st =
-  debug st ("Chdir to home dir : "^(get_chdir st));
+  debug st ("Chdir to home dir: "^(get_chdir st));
   Unix.putenv "HOME" (get_chdir st);
   Unix.putenv "MLDONKEY_STRINGS" (Filename.concat (get_chdir st) "mlnet_strings");
-  Sys.chdir  (get_chdir st)
+  Sys.chdir (get_chdir st)
+;;
 
 let create_home st =
   if Sys.file_exists (get_chdir st) then
@@ -346,7 +347,7 @@
     begin
       let answer =    
         prerr_string ((get_chdir st)^" doesn't exists."^
-        " Do you want to create it ? ( y/N )");
+        " Do you want to create it? ( y/N )");
         flush(Pervasives.stderr);
         read_line ()
       in
@@ -369,7 +370,7 @@
 ;;
 
 let set_umask st =
-  debug st ("Set umask of the process:"^(string_of_int st.umask));
+  debug st ("Set umask of the process: "^(string_of_int st.umask));
   ignore(umask st.umask)
 ;;
 
@@ -387,14 +388,14 @@
       ignore (Unix.nice (st.nice - current_nice))
   else
     warning st ("Niceness out of bound ("^(string_of_int st.nice)
-    ^"not in ["^(string_of_int prio_min)^"; "^(string_of_int prio_max)^"]")
+    ^"not in ["^(string_of_int prio_min)^"; "^(string_of_int prio_max)^"])")
 ;;
 
 (** Create a pidfile, holding the PID value of the process *)
 let create_pidfile st =
   let pidfile = open_out (get_pidfile st)
   in
-  debug st ("Writing PID ("^(string_of_int (Unix.getpid ()))^") to pidfile : "^(get_pidfile st));
+  debug st ("Writing PID ("^(string_of_int (Unix.getpid ()))^") to pidfile: "^(get_pidfile st));
   output_string pidfile (string_of_int (Unix.getpid ()));
   output_string pidfile "\n";
   close_out pidfile
@@ -407,7 +408,7 @@
     in
     let pid_server = int_of_string (input_line pidfile)
     in
-    debug st ("Reading PID ( "^(string_of_int pid_server)^" from pidfile: "^(get_pidfile st));
+    debug st ("Reading PID ("^(string_of_int pid_server)^") from pidfile: "^(get_pidfile st));
     close_in pidfile;
     pid_server
   with 
@@ -421,7 +422,7 @@
 (** Remove a pidfile *)
 let close_pidfile st =
   try
-    debug st ("Removing pidfile : "^(get_pidfile st));
+    debug st ("Removing pidfile: "^(get_pidfile st));
     Sys.remove (get_pidfile st)
   with Sys_error(_) ->
     raise No_pidfile
@@ -468,6 +469,18 @@
     false
 ;;
 
+(** Remove stale pidfile 
+ *)
+let remove_stale_pidfile st =
+  if not (daemon_is_running st) && Sys.file_exists (get_pidfile st) then
+    (
+      debug st ("Removing stale pidfile: "^(get_pidfile st));
+      Sys.remove (get_pidfile st)
+    )
+  else
+    ()
+;;
+
 (** Get problematic INI file for mldonkey. Returns a list of all problematic
     files 
   *)
@@ -634,28 +647,38 @@
 
 let daemonize st = 
   if st.daemon then
-    if Unix.fork () = 0 then
-      if Unix.setsid () = Unix.getpid () then
+    (
+      debug st ("Fork the process");
+      if Unix.fork () = 0 then
         (
-          let fd = Unix.openfile "/dev/null" [ Unix.O_RDWR ] 0o0644 
-          in
-          List.iter (
-            fun fd_std -> 
-              Unix.close fd_std; 
-              Unix.dup2 fd fd_std
+          debug st ("Reset the group leader");
+          if Unix.setsid () = Unix.getpid () then
+            (
+              debug st ("Fork a second time the process");
+              if Unix.fork () = 0 then
+              (
+                debug st ("Close standard IO");
+                let fd = Unix.openfile "/dev/null" [ Unix.O_RDWR ] 0o0644 
+                in
+                List.iter (
+                  fun fd_std -> 
+                    Unix.close fd_std; 
+                    Unix.dup2 fd fd_std
+                  )
+                  [Unix.stdin; Unix.stdout; Unix.stderr];
+                Unix.close fd;
+                debug st ("Process is running in the background");
+                ()
+              )
+              else
+                exit 0
             )
-            [Unix.stdin; Unix.stdout; Unix.stderr];
-          Unix.close fd;
-          ()
-          (*if Unix.fork () = 0 then
-            ()
           else
-            exit 0*)
+            raise Fails_daemonize 
         )
-        else
-          raise Fails_daemonize 
-    else
-      exit 0
+      else
+        exit 0
+    )
   else
     ()  
 ;;
@@ -666,6 +689,7 @@
     raise Already_running
   else
     ();
+  remove_stale_pidfile st;
   set_nice st;
   set_uid_gid st;
   set_umask st;




More information about the Pkg-ocaml-maint-commits mailing list