Author: joergr
Date: 2005-04-06 13:13:49 -0400 (Wed, 06 Apr 2005)
New Revision: 42613

Added:
   trunk/mcs/tools/mono-service/ChangeLog
Modified:
   trunk/mcs/tools/mono-service/mono-service.cs
Log:
2004-04-06  Joerg Rosenkranz  <[EMAIL PROTECTED]>

        * mono-service.cs: 
            Pay attention to CanStop and CanPauseAndContinue. 
            Call Dispose of service. Fixed exe name in usage output.
            Filling and using lock file to prevent multiple instances.



Added: trunk/mcs/tools/mono-service/ChangeLog
===================================================================
--- trunk/mcs/tools/mono-service/ChangeLog      2005-04-06 16:52:58 UTC (rev 
42612)
+++ trunk/mcs/tools/mono-service/ChangeLog      2005-04-06 17:13:49 UTC (rev 
42613)
@@ -0,0 +1,6 @@
+2004-04-06  Joerg Rosenkranz  <[EMAIL PROTECTED]>
+
+       * mono-service.cs: 
+           Pay attention to CanStop and CanPauseAndContinue. 
+           Call Dispose of service. Fixed exe name in usage output.
+           Filling and using lock file to prevent multiple instances.

Modified: trunk/mcs/tools/mono-service/mono-service.cs
===================================================================
--- trunk/mcs/tools/mono-service/mono-service.cs        2005-04-06 16:52:58 UTC 
(rev 42612)
+++ trunk/mcs/tools/mono-service/mono-service.cs        2005-04-06 17:13:49 UTC 
(rev 42613)
@@ -33,7 +33,7 @@
        {
                Console.Error.WriteLine (
                                         "Usage is:\n" +
-                                        "monod [-d:DIRECTORY] [-l:LOCKFILE] 
[-n:NAME] [-m:LOGNAME] service.exe\n");
+                                        "mono-service [-d:DIRECTORY] 
[-l:LOCKFILE] [-n:NAME] [-m:LOGNAME] service.exe\n");
                Environment.Exit (1);
        }
 
@@ -97,6 +97,29 @@
                        }
                }
 
+               // Use lockfile to allow only one instance
+               if (lockfile == null)
+                       lockfile = String.Format ("/tmp/{0}.lock", 
Path.GetFileName (assembly));
+
+               int lfp = Syscall.open (lockfile, 
OpenFlags.O_RDWR|OpenFlags.O_CREAT, 
+                       
FilePermissions.S_IRUSR|FilePermissions.S_IWUSR|FilePermissions.S_IRGRP);
+       
+               if (lfp<0)  {
+                       error ("Cannot open lock file.");
+                       return 1;
+               }
+       
+               if (Syscall.lockf(lfp, LockFlags.F_TLOCK,0)<0)  {
+                       info ("Daemon is already running.");
+                       return 0;
+               }
+               
+               // Write pid to lock file
+               string pid = Syscall.getpid ().ToString () + 
Environment.NewLine;
+               IntPtr buf = Marshal.StringToCoTaskMemAnsi (pid);
+               Syscall.write (lfp, buf, (ulong)pid.Length);
+               Marshal.FreeCoTaskMem (buf);
+
                Assembly a = null;
                
                try {
@@ -114,9 +137,6 @@
                        return 1;
                }
 
-               if (lockfile == null)
-                       lockfile = String.Format ("/tmp/{0}.lock", 
Path.GetFileName (assembly));
-
                MethodInfo entry = a.EntryPoint;
                if (entry == null){
                        error ("Entry point not defined in service");
@@ -180,22 +200,31 @@
                                
                                switch (v){
                                case Signum.SIGTERM:
-                                       info ("Stopping service {0}", 
service.ServiceName);
-                                       call (service, "OnStop", null);
-                                       running = false;
+                                       if (service.CanStop) {
+                                               info ("Stopping service {0}", 
service.ServiceName);
+                                               call (service, "OnStop", null);
+                                               running = false;
+                                       }
                                        break;
                                case Signum.SIGUSR1:
-                                       info ("Pausing service {0}", 
service.ServiceName);
-                                       call (service, "OnPause", null);
+                                       if (service.CanPauseAndContinue) {
+                                               info ("Pausing service {0}", 
service.ServiceName);
+                                               call (service, "OnPause", null);
+                                       }
                                        break;
                                case Signum.SIGUSR2:
-                                       info ("Continuing service {0}", 
service.ServiceName);
-                                       call (service, "OnContinue", null);
+                                       if (service.CanPauseAndContinue) {
+                                               info ("Continuing service {0}", 
service.ServiceName);
+                                               call (service, "OnContinue", 
null);
+                                       }
                                        break;
                                }
                        }
                }
 
+               foreach (ServiceBase svc in services){
+                       svc.Dispose ();
+               }
                
                return 0;
        }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to