[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tmda-workers
Subject:    running tmda-ofmipd as a daemon
From:       Andrew <andrew () arda ! homeunix ! net>
Date:       2002-10-03 4:11:39
[Download RAW message or body]

I've prepared a patch that adds an option to tmda-ofmipd '-D, --daemon' 
that puts the program into the background. With this option, you don't 
need to use an '&' at the end of a command string to background 
tmda-ofmipd. The impetus for this patch was that I was having problems 
starting tmda-ofmipd from an init script on my Mandrake 8.2 box. There 
is a thread on the tmda-users mailing list that describes the problems I 
was having more fully.

So, here is the patch. I hope people find it useful enough to be 
incorporated into the distribution. Comments are welcome, but if you 
post to tell me what I did wrong, you have to tell me why it's wrong and 
how to do it right.

Andrew

["tmda.diff" (text/plain)]

? tmda.diff
Index: bin/ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/ChangeLog,v
retrieving revision 1.233
diff -u -r1.233 ChangeLog
--- bin/ChangeLog	21 Sep 2002 00:27:44 -0000	1.233
+++ bin/ChangeLog	3 Oct 2002 03:58:08 -0000
@@ -1,3 +1,9 @@
+2002-10-02  Andrew St. Jean  <andrew@arda.homeunix.net>
+
+	* tmda-ofmipd: Add a new option '-D, --daemon' to force the
+	program to run in the background. The PID of the daemonized
+	process is printed to standard out.
+
 2002-09-20  Jason R. Mastaler  <jasonrm@hrothgar.la.mastaler.com>
 
 	* tmda-pending (main): Run the headers through an RFC 2047 decoder
Index: bin/tmda-ofmipd
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/tmda-ofmipd,v
retrieving revision 1.17
diff -u -r1.17 tmda-ofmipd
--- bin/tmda-ofmipd	11 Sep 2002 22:35:59 -0000	1.17
+++ bin/tmda-ofmipd	3 Oct 2002 03:58:11 -0000
@@ -42,6 +42,10 @@
     --debug
 	Turn on debugging prints.
 
+    -D
+    --daemon
+        Run tmda-ofmipd as a background process.
+
     -u <username>
     --username <username>
 	The username that this program should run under.  The default
@@ -132,6 +136,7 @@
 program = sys.argv[0]
 configdir = None
 authprog = None
+daemon = 0
 remoteauth = { 'proto': None,
                'host':  'localhost',
                'port':  None,
@@ -191,7 +196,7 @@
 
 try:
     opts, args = getopt.getopt(sys.argv[1:],
-                               'p:u:R:A:a:c:C:dVh', ['proxyport=',
+                               'p:u:R:A:a:c:C:dDVh', ['proxyport=',
                                                      'username=',
                                                      'authfile=',
                                                      'remoteauth=',
@@ -199,6 +204,7 @@
                                                      'configdir=',
                                                      'connections=',
                                                      'debug',
+						     'daemon',
                                                      'version',
                                                      'help'])
 except getopt.error, msg:
@@ -215,6 +221,8 @@
         sys.exit()
     elif opt in ('-d', '--debug'):
         DEBUGSTREAM = sys.stderr
+    elif opt in ('-D', '--daemon'):
+        daemon = 1
     elif opt in ('-p', '--proxyport'):
 	proxyport = arg
     elif opt in ('-u', '--username'):
@@ -1033,4 +1041,28 @@
 
 # This is the end my friend.
 if __name__ == '__main__':
+    if daemon:
+        try:
+	    pid = os.fork()
+	    if pid > 0:
+	        sys.exit(0)
+	except OSError:
+	    value = sys.exc_info()[1]
+	    print 'Failed to daemonize process: %s' % value
+	    sys.exit(1)
+
+	os.chdir('/')
+	os.setsid()
+	os.umask(0)
+
+	try:
+	    pid = os.fork()
+	    if pid > 0:
+	        print pid
+		sys.exit(0)
+	except OSError:
+	    value = sys.exc_info()[1]
+	    print 'Failed to daemonize process: %s' % value
+	    sys.exit(1)
+
     main()

_________________________________________________
tmda-workers mailing list (tmda-workers@tmda.net)
http://tmda.net/lists/listinfo/tmda-workers

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic