816304fa075df1102630c347c8bb918763438532
[sliver-openvswitch.git] / tests / test-daemon.py
1 # Copyright (c) 2010, 2011 Nicira Networks.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import getopt
16 import logging
17 import signal
18 import sys
19 import time
20
21 import ovs.daemon
22 import ovs.util
23
24
25 def handler(signum, _):
26     raise Exception("Signal handler called with %d" % signum)
27
28
29 def main(argv):
30     logging.basicConfig(level=logging.DEBUG)
31
32     signal.signal(signal.SIGHUP, handler)
33
34     try:
35         options, args = getopt.gnu_getopt(
36             argv[1:], 'b', ["bail", "help"] + ovs.daemon.LONG_OPTIONS)
37     except getopt.GetoptError, geo:
38         sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
39         sys.exit(1)
40
41     bail = False
42     for key, value in options:
43         if key == '--help':
44             usage()
45         elif key in ['-b', '--bail']:
46             bail = True
47         elif not ovs.daemon.parse_opt(key, value):
48             sys.stderr.write("%s: unhandled option %s\n"
49                              % (ovs.util.PROGRAM_NAME, key))
50             sys.exit(1)
51
52     ovs.daemon.daemonize_start()
53     if bail:
54         sys.stderr.write("%s: exiting after daemonize_start() as requested\n"
55                          % ovs.util.PROGRAM_NAME)
56         sys.exit(1)
57     ovs.daemon.daemonize_complete()
58
59     while True:
60         time.sleep(1)
61
62
63 def usage():
64     sys.stdout.write("""\
65 %s: Open vSwitch daemonization test program for Python
66 usage: %s [OPTIONS]
67 """ % ovs.util.PROGRAM_NAME)
68     ovs.daemon.usage()
69     sys.stdout.write("""
70 Other options:
71   -h, --help              display this help message
72   -b, --bail              exit with an error after daemonize_start()
73 """)
74     sys.exit(0)
75
76 if __name__ == '__main__':
77     try:
78         main(sys.argv)
79     except SystemExit:
80         # Let system.exit() calls complete normally
81         raise
82     except:
83         sys.exit(ovs.daemon.RESTART_EXIT_CODE)