1
# Copyright (C) 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Signal handling for the smart server code."""
22
from bzrlib import trace
25
# I'm pretty sure this has to be global, since signal handling is per-process.
27
# TODO: Using a dict means that the order of calls is unordered. We could use a
28
# list and then do something like LIFO ordering. A dict was chosen so
29
# that you could have a key to easily remove your entry. However, you
30
# could just use the callable itself as the indexed part, and even in
31
# large cases, we shouldn't have more than 100 or so callbacks
33
def _sighup_handler(signal_number, interrupted_frame):
34
"""This is the actual function that is registered for handling SIGHUP.
36
It will call out to all the registered functions, letting them know that a
37
graceful termination has been requested.
39
if _on_sighup is None:
41
trace.mutter('Caught SIGHUP, sending graceful shutdown requests.')
42
for ref in _on_sighup.valuerefs():
47
except KeyboardInterrupt:
50
trace.mutter('Error occurred while running SIGHUP handlers:')
51
trace.log_exception_quietly()
54
def install_sighup_handler():
55
"""Setup a handler for the SIGHUP signal."""
56
if getattr(signal, "SIGHUP", None) is None:
57
# If we can't install SIGHUP, there is no reason (yet) to do graceful
61
old_signal = signal.signal(signal.SIGHUP, _sighup_handler)
62
old_dict = _setup_on_hangup_dict()
63
return old_signal, old_dict
66
def _setup_on_hangup_dict():
67
"""Create something for _on_sighup.
69
This is done when we install the sighup handler, and for tests that want to
70
test the functionality. If this hasn'nt been called, then
71
register_on_hangup is a no-op. As is unregister_on_hangup.
75
_on_sighup = weakref.WeakValueDictionary()
79
def restore_sighup_handler(orig):
80
"""Pass in the returned value from install_sighup_handler to reset."""
82
old_signal, old_dict = orig
83
if old_signal is not None:
84
signal.signal(signal.SIGHUP, old_signal)
88
# TODO: Should these be single-use callables? Meaning that once we've triggered
89
# SIGHUP and called them, they should auto-remove themselves? I don't
90
# think so. Callers need to clean up during shutdown anyway, so that we
91
# don't end up with lots of garbage in the _on_sighup dict. On the other
92
# hand, we made _on_sighup a WeakValueDictionary in case cleanups didn't
93
# get fired properly. Maybe we just assume we don't have to do it?
94
def register_on_hangup(identifier, a_callable):
95
"""Register for us to call a_callable as part of a graceful shutdown."""
96
if _on_sighup is None:
98
_on_sighup[identifier] = a_callable
101
def unregister_on_hangup(identifier):
102
"""Remove a callback from being called during sighup."""
103
if _on_sighup is None:
106
del _on_sighup[identifier]
107
except KeyboardInterrupt:
110
# This usually runs as a tear-down step. So we don't want to propagate
112
trace.mutter('Error occurred during unregister_on_hangup:')
113
trace.log_exception_quietly()