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