2 # Copyright 2008 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
21 import os, sys, signal
23 # Turn application into a background daemon process.
25 # When this function returns:
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
31 # 3) If a logfile name is provided, it is opened and stdout/stderr are
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.
37 # The return value is the new pid.
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()
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.
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
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.
55 def daemonize(pidfile=None, logfile=None):
56 # fork() into background
60 raise Exception, "%s [%d]" % (e.strerror, e.errno)
62 if pid == 0: # First child of first fork()
63 # Become session leader of new session
66 # fork() into background again
70 raise Exception, "%s [%d]" % (e.strerror, e.errno)
73 os._exit(0) # Second child of second fork()
75 else: # Second child of first fork()
82 if pidfile is not None:
83 open(pidfile, 'w').write('%d\n'%pid)
86 if logfile is not None:
87 lf = open(logfile, 'a+')
91 # Prevent pinning any filesystem mounts
94 # Tell caller what pid to send future signals to
97 if __name__ == "__main__":
100 print "Hello, world, from daemon process."
102 print "Goodbye, world, from daemon process."