ovs.fatal_signal: Reorder definitions to be more easily readable.
[openvswitch] / python / ovs / fatal_signal.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 atexit
16 import logging
17 import os
18 import signal
19
20 _hooks = []
21
22 def add_hook(hook, cancel, run_at_exit):
23     _init()
24
25     global _hooks
26     _hooks.append((hook, cancel, run_at_exit))
27
28 def fork():
29     """Clears all of the fatal signal hooks without executing them.  If any of
30     the hooks passed a 'cancel' function to add_hook(), then those functions
31     will be called, allowing them to free resources, etc.
32
33     Following a fork, one of the resulting processes can call this function to
34     allow it to terminate without calling the hooks registered before calling
35     this function.  New hooks registered after calling this function will take
36     effect normally."""
37     global _hooks
38     for hook, cancel, run_at_exit in _hooks:
39         if cancel:
40             cancel()
41
42     _hooks = []
43 \f
44 _added_hook = False
45 _files = {}
46
47 def add_file_to_unlink(file):
48     """Registers 'file' to be unlinked when the program terminates via
49     sys.exit() or a fatal signal."""
50     global _added_hook
51     if not _added_hook:
52         _added_hook = True
53         add_hook(_unlink_files, _cancel_files, True)
54     _files[file] = None
55
56 def remove_file_to_unlink(file):
57     """Unregisters 'file' from being unlinked when the program terminates via
58     sys.exit() or a fatal signal."""
59     if file in _files:
60         del _files[file]
61
62 def unlink_file_now(file):
63     """Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'.
64     Returns 0 if successful, otherwise a positive errno value."""
65     error = _unlink(file)
66     if error:
67         logging.warning("could not unlink \"%s\" (%s)"
68                         % (file, os.strerror(error)))
69     remove_file_to_unlink(file)
70     return error
71
72 def _unlink_files():
73     for file in _files:
74         _unlink(file)
75
76 def _cancel_files():
77     global _added_hook
78     global _files
79     _added_hook = False
80     _files = {}
81
82 def _unlink(file):
83     try:
84         os.unlink(file)
85         return 0
86     except OSError, e:
87         return e.errno
88 \f
89 def _signal_handler(signr, frame):
90     _call_hooks(signr)
91
92     # Re-raise the signal with the default handling so that the program
93     # termination status reflects that we were killed by this signal.
94     signal.signal(signr, signal.SIG_DFL)
95     os.kill(os.getpid(), signr)
96
97 def _atexit_handler():
98     _call_hooks(0)
99
100 recurse = False
101 def _call_hooks(signr):
102     global recurse
103     if recurse:
104         return
105     recurse = True
106
107     for hook, cancel, run_at_exit in _hooks:
108         if signr != 0 or run_at_exit:
109             hook()
110
111 _inited = False
112 def _init():
113     global _inited
114     if not _inited:
115         _inited = True
116
117         for signr in (signal.SIGTERM, signal.SIGINT,
118                       signal.SIGHUP, signal.SIGALRM):
119             if signal.getsignal(signr) == signal.SIG_DFL:
120                 signal.signal(signr, _signal_handler)
121         atexit.register(_atexit_handler)