diff options
-rw-r--r-- | gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am | 3 | ||||
-rw-r--r-- | gnuradio-core/src/python/gnuradio/gruimpl/daemon.py | 102 | ||||
-rw-r--r-- | gnuradio-examples/python/audio/Makefile.am | 1 | ||||
-rwxr-xr-x | gnuradio-examples/python/audio/dial_tone_daemon.py | 57 |
4 files changed, 162 insertions, 1 deletions
diff --git a/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am b/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am index 7fbc6ec56..b2ed7e64b 100644 --- a/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am +++ b/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am @@ -35,6 +35,7 @@ grupython_PYTHON = \ os_read_exactly.py \ sdr_1000.py \ seq_with_cursor.py \ - socket_stuff.py + socket_stuff.py \ + daemon.py CLEANFILES = *.pyc diff --git a/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py b/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py new file mode 100644 index 000000000..4fa972ad2 --- /dev/null +++ b/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py @@ -0,0 +1,102 @@ +# +# Copyright 2008 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# +import os, sys, signal + +# Turn application into a background daemon process. +# +# When this function returns: +# +# 1) The calling process is disconnected from its controlling terminal +# and will not exit when the controlling session exits +# 2) If a pidfile name is provided, it is created and the new pid is +# written into it. +# 3) If a logfile name is provided, it is opened and stdout/stderr are +# redirected to it. +# 4) The process current working directory is changed to '/' to avoid +# pinning any filesystem mounts. +# 5) The process umask is set to 0111. +# +# The return value is the new pid. +# +# To create GNU Radio applications that operate as daemons, add a call to this +# function after all initialization but just before calling gr.top_block.run() +# or .start(). +# +# Daemonized GNU Radio applications may be stopped by sending them a +# SIGINT, SIGKILL, or SIGTERM, e.g., using 'kill pid' from the command line. +# +# If your application uses gr.top_block.run(), the flowgraph will be stopped +# and the function will return. You should allow your daemon program to exit +# at this point. +# +# If your application uses gr.top_block.start(), you are responsible for hooking +# the Python signal handler (see 'signal' module) and calling gr.top_block.stop() +# on your top block, and otherwise causing your daemon process to exit. +# + +def daemonize(pidfile=None, logfile=None): + # fork() into background + try: + pid = os.fork() + except OSError, e: + raise Exception, "%s [%d]" % (e.strerror, e.errno) + + if pid == 0: # First child of first fork() + # Become session leader of new session + os.setsid() + + # fork() into background again + try: + pid = os.fork() + except OSError, e: + raise Exception, "%s [%d]" % (e.strerror, e.errno) + + if pid != 0: + os._exit(0) # Second child of second fork() + + else: # Second child of first fork() + os._exit(0) + + os.umask(0111) + + # Write pid + pid = os.getpid() + if pidfile is not None: + open(pidfile, 'w').write('%d\n'%pid) + + # Redirect streams + if logfile is not None: + lf = open(logfile, 'a+') + sys.stdout = lf + sys.stderr = lf + + # Prevent pinning any filesystem mounts + os.chdir('/') + + # Tell caller what pid to send future signals to + return pid + +if __name__ == "__main__": + import time + daemonize() + print "Hello, world, from daemon process." + time.sleep(20) + print "Goodbye, world, from daemon process." diff --git a/gnuradio-examples/python/audio/Makefile.am b/gnuradio-examples/python/audio/Makefile.am index eeb25d706..dc200219e 100644 --- a/gnuradio-examples/python/audio/Makefile.am +++ b/gnuradio-examples/python/audio/Makefile.am @@ -27,6 +27,7 @@ EXTRA_DIST = \ audio_play.py \ audio_to_file.py \ dial_tone.py \ + dial_tone_daemon.py \ dial_tone_wav.py \ mono_tone.py \ multi_tone.py \ diff --git a/gnuradio-examples/python/audio/dial_tone_daemon.py b/gnuradio-examples/python/audio/dial_tone_daemon.py new file mode 100755 index 000000000..d30d0e117 --- /dev/null +++ b/gnuradio-examples/python/audio/dial_tone_daemon.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# +# Copyright 2004,2005,2007,2008 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr, gru +from gnuradio import audio +from gnuradio.eng_option import eng_option +from optparse import OptionParser +import os + +class my_top_block(gr.top_block): + + def __init__(self): + gr.top_block.__init__(self) + + parser = OptionParser(option_class=eng_option) + parser.add_option("-O", "--audio-output", type="string", default="", + help="pcm output device name. E.g., hw:0,0 or /dev/dsp") + parser.add_option("-r", "--sample-rate", type="eng_float", default=48000, + help="set sample rate to RATE (48000)") + (options, args) = parser.parse_args () + if len(args) != 0: + parser.print_help() + raise SystemExit, 1 + + sample_rate = int(options.sample_rate) + ampl = 0.1 + + src0 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 350, ampl) + src1 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 440, ampl) + dst = audio.sink (sample_rate, options.audio_output) + self.connect (src0, (dst, 0)) + self.connect (src1, (dst, 1)) + + +if __name__ == '__main__': + pid = gru.daemonize() + print "To stop this program, enter 'kill %d'" % pid + my_top_block().run() |