Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gruimpl / daemon.py
1 #
2 # Copyright 2008 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21 import os, sys, signal
22
23 # Turn application into a background daemon process.
24 #
25 # When this function returns:
26 #
27 # 1) The calling process is disconnected from its controlling terminal
28 #    and will not exit when the controlling session exits
29 # 2) If a pidfile name is provided, it is created and the new pid is
30 #    written into it.
31 # 3) If a logfile name is provided, it is opened and stdout/stderr are
32 #    redirected to it.
33 # 4) The process current working directory is changed to '/' to avoid
34 #    pinning any filesystem mounts.
35 # 5) The process umask is set to 0111.
36 #
37 # The return value is the new pid.
38 #
39 # To create GNU Radio applications that operate as daemons, add a call to this
40 # function after all initialization but just before calling gr.top_block.run()
41 # or .start().
42 #
43 # Daemonized GNU Radio applications may be stopped by sending them a
44 # SIGINT, SIGKILL, or SIGTERM, e.g., using 'kill pid' from the command line.
45 #
46 # If your application uses gr.top_block.run(), the flowgraph will be stopped
47 # and the function will return.  You should allow your daemon program to exit
48 # at this point.
49 #
50 # If your application uses gr.top_block.start(), you are responsible for hooking
51 # the Python signal handler (see 'signal' module) and calling gr.top_block.stop()
52 # on your top block, and otherwise causing your daemon process to exit.
53 #
54
55 def daemonize(pidfile=None, logfile=None):
56     # fork() into background
57     try:
58         pid = os.fork()
59     except OSError, e:
60         raise Exception, "%s [%d]" % (e.strerror, e.errno)
61
62     if pid == 0:        # First child of first fork()
63         # Become session leader of new session
64         os.setsid()
65         
66         # fork() into background again
67         try:
68             pid = os.fork()
69         except OSError, e:
70             raise Exception, "%s [%d]" % (e.strerror, e.errno)
71
72         if pid != 0:
73             os._exit(0) # Second child of second fork()
74
75     else:               # Second child of first fork()
76         os._exit(0)
77         
78     os.umask(0111)
79
80     # Write pid
81     pid = os.getpid()
82     if pidfile is not None:
83         open(pidfile, 'w').write('%d\n'%pid)
84         
85     # Redirect streams
86     if logfile is not None:
87         lf = open(logfile, 'a+')
88         sys.stdout = lf
89         sys.stderr = lf
90
91     # Prevent pinning any filesystem mounts
92     os.chdir('/')
93
94     # Tell caller what pid to send future signals to
95     return pid
96
97 if __name__ == "__main__":
98     import time
99     daemonize()
100     print "Hello, world, from daemon process."
101     time.sleep(20)
102     print "Goodbye, world, from daemon process."