X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=python%2Fovs%2Ffatal_signal.py;h=e6fe78386ed455fb87715e103aefcbb2db4288c3;hb=ec988646afe6aee6a63d6894a3e9b50f715d5941;hp=b931512689e7e06a24c96cb6414b9999db616caf;hpb=32fcdd44c5e43e41c285220da5db49fd9870d225;p=sliver-openvswitch.git diff --git a/python/ovs/fatal_signal.py b/python/ovs/fatal_signal.py index b93151268..e6fe78386 100644 --- a/python/ovs/fatal_signal.py +++ b/python/ovs/fatal_signal.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010, 2011 Nicira Networks +# Copyright (c) 2010, 2011 Nicira, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,16 +13,20 @@ # limitations under the License. import atexit -import logging import os import signal +import ovs.vlog + _hooks = [] +vlog = ovs.vlog.Vlog("fatal-signal") + def add_hook(hook, cancel, run_at_exit): _init() _hooks.append((hook, cancel, run_at_exit)) + def fork(): """Clears all of the fatal signal hooks without executing them. If any of the hooks passed a 'cancel' function to add_hook(), then those functions @@ -42,6 +46,7 @@ def fork(): _added_hook = False _files = {} + def add_file_to_unlink(file): """Registers 'file' to be unlinked when the program terminates via sys.exit() or a fatal signal.""" @@ -51,25 +56,28 @@ def add_file_to_unlink(file): add_hook(_unlink_files, _cancel_files, True) _files[file] = None + def remove_file_to_unlink(file): """Unregisters 'file' from being unlinked when the program terminates via sys.exit() or a fatal signal.""" if file in _files: del _files[file] + def unlink_file_now(file): """Like fatal_signal_remove_file_to_unlink(), but also unlinks 'file'. Returns 0 if successful, otherwise a positive errno value.""" error = _unlink(file) if error: - logging.warning("could not unlink \"%s\" (%s)" - % (file, os.strerror(error))) + vlog.warn("could not unlink \"%s\" (%s)" % (file, os.strerror(error))) remove_file_to_unlink(file) return error + def _unlink_files(): - for file in _files: - _unlink(file) + for file_ in _files: + _unlink(file_) + def _cancel_files(): global _added_hook @@ -77,14 +85,16 @@ def _cancel_files(): _added_hook = False _files = {} -def _unlink(file): + +def _unlink(file_): try: - os.unlink(file) + os.unlink(file_) return 0 except OSError, e: return e.errno + -def _signal_handler(signr, frame): +def _signal_handler(signr, _): _call_hooks(signr) # Re-raise the signal with the default handling so that the program @@ -92,10 +102,14 @@ def _signal_handler(signr, frame): signal.signal(signr, signal.SIG_DFL) os.kill(os.getpid(), signr) + def _atexit_handler(): _call_hooks(0) + recurse = False + + def _call_hooks(signr): global recurse if recurse: @@ -106,7 +120,10 @@ def _call_hooks(signr): if signr != 0 or run_at_exit: hook() + _inited = False + + def _init(): global _inited if not _inited: