"Jason R. Mastaler" <[EMAIL PROTECTED]> writes:

> Things are so silent in TMDA-land lately it's eerie.  I'm planning to
> release 1.0-final tomorrow, so speak now or forever hold your peace.
> :-)

I looked at and slightly modified Tim Rice's patch to have tmda-ofmipd
write a pidfile in daemon-mode (only if it runs as root).

http://mla.libertine.org/tmda-workers/2003-12/msg00058.html

There were a couple minor problems that I fixed.  I'm attaching a diff
rather than just checking this in.  I think the changes are minimal
and won't have any impact on the actual operation of tmda-ofmipd (and
are thus "safe").  If you're not comfortable with this, it can wait
until after 1.0.  If you are, let me know and I'll check it in.


Tim

Index: tmda-ofmipd
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/tmda-ofmipd,v
retrieving revision 1.41
diff -u -r1.41 tmda-ofmipd
--- tmda-ofmipd	13 Nov 2003 19:28:08 -0000	1.41
+++ tmda-ofmipd	18 Dec 2003 23:40:04 -0000
@@ -63,6 +63,11 @@
         default is FQDN:8025 (i.e, port 8025 on the fully qualified
         domain name for the local host).
 
+    -P <pid-file>
+    --pidfile <pid-file>
+        The file used to write the process ID if we run in background
+        mode. The default is /var/run/tmda-ofmipd.pid
+
     -R proto[://host[:port]]
     --remoteauth proto[://host[:port]][/dn]
         Host to connect to to check username and password.
@@ -166,6 +171,7 @@
         If the script returns a 0, the message is allowed.  For any other
         value, the message is rejected."""
 
+import errno
 import getopt
 import os
 import signal
@@ -192,6 +198,7 @@
 DEBUGSTREAM = Devnull()
 proxyport = '%s:%s' % (FQDN, 8025)
 program = sys.argv[0]
+pidfile = '/var/run/tmda-ofmipd.pid'
 configdir = None
 authprog = None
 fallback = 0
@@ -261,7 +268,8 @@
 
 try:
     opts, args = getopt.getopt(sys.argv[1:],
-                      'p:u:a:R:A:Fc:C:dVhfbS:v:t:', ['proxyport=',
+		      'p:P:u:a:R:A:Fc:C:dVhfbS:v:t:', ['proxyport=',
+                                                     'pidfile=',
                                                      'username=',
                                                      'authfile=',
                                                      'remoteauth=',
@@ -299,6 +307,8 @@
         foreground = 0
     elif opt in ('-p', '--proxyport'):
         proxyport = arg
+    elif opt in ('-P', '--pidfile'):
+        pidfile = arg
     elif opt in ('-u', '--username'):
         username = arg
     elif opt in ('-R', '--remoteauth'):
@@ -1226,6 +1236,21 @@
             if Util.getfileuid(authfile) != pw_uid:
                 raise IOError, \
                       authfile + ' must be owned by UID ' + str(pw_uid)
+        if not foreground:
+            pfd = None
+            try:
+                pfd = os.open(pidfile, os.O_WRONLY|os.O_CREAT|os.O_EXCL, 0644)
+            except OSError, e:
+                if e.errno == errno.EEXIST:
+                    msg = "File '%s' already exists.  Another copy of tmda-" \
+                          "ofmipd is already running or a previous run of " \
+                          "tmda-ofmipd aborted, leaving the file.  Either " \
+                          "remove '%s' by hand or use the --pidfile flag to " \
+                          "specify a different file in which to store the " \
+                          "PID." % (pidfile, pidfile)
+                    warning(msg)
+                else:
+                    raise
         # try setegid()
         os.setegid(Util.getgid(username))
         # try setting the supplemental group ids
@@ -1242,7 +1267,11 @@
     # Try to fork to go to daemon unless foreground mode
     if not foreground:
         signal.signal(signal.SIGHUP, signal.SIG_IGN) # ignore SIGHUP
-        if os.fork() != 0:
+        pid = os.fork()
+        if pid != 0:
+            if pfd is not None:
+                os.write(pfd, str(pid) + '\n')
+                os.close(pfd)
             sys.exit()
 
     # Start the event loop
_________________________________________________
tmda-workers mailing list ([EMAIL PROTECTED])
http://tmda.net/lists/listinfo/tmda-workers

Reply via email to