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."""
19
from __future__ import absolute_import
24
from bzrlib import trace
27
# I'm pretty sure this has to be global, since signal handling is per-process.
29
# TODO: Using a dict means that the order of calls is unordered. We could use a
30
# list and then do something like LIFO ordering. A dict was chosen so
31
# that you could have a key to easily remove your entry. However, you
32
# could just use the callable itself as the indexed part, and even in
33
# large cases, we shouldn't have more than 100 or so callbacks
35
def _sighup_handler(signal_number, interrupted_frame):
36
"""This is the actual function that is registered for handling SIGHUP.
38
It will call out to all the registered functions, letting them know that a
39
graceful termination has been requested.
41
if _on_sighup is None:
43
trace.mutter('Caught SIGHUP, sending graceful shutdown requests.')
44
for ref in _on_sighup.valuerefs():
49
except KeyboardInterrupt:
52
trace.mutter('Error occurred while running SIGHUP handlers:')
53
trace.log_exception_quietly()
56
def install_sighup_handler():
57
"""Setup a handler for the SIGHUP signal."""
58
if getattr(signal, "SIGHUP", None) is None:
59
# If we can't install SIGHUP, there is no reason (yet) to do graceful
63
old_signal = signal.signal(signal.SIGHUP, _sighup_handler)
64
old_dict = _setup_on_hangup_dict()
65
return old_signal, old_dict
68
def _setup_on_hangup_dict():
69
"""Create something for _on_sighup.
71
This is done when we install the sighup handler, and for tests that want to
72
test the functionality. If this hasn'nt been called, then
73
register_on_hangup is a no-op. As is unregister_on_hangup.
77
_on_sighup = weakref.WeakValueDictionary()
81
def restore_sighup_handler(orig):
82
"""Pass in the returned value from install_sighup_handler to reset."""
84
old_signal, old_dict = orig
85
if old_signal is not None:
86
signal.signal(signal.SIGHUP, old_signal)
90
# TODO: Should these be single-use callables? Meaning that once we've triggered
91
# SIGHUP and called them, they should auto-remove themselves? I don't
92
# think so. Callers need to clean up during shutdown anyway, so that we
93
# don't end up with lots of garbage in the _on_sighup dict. On the other
94
# hand, we made _on_sighup a WeakValueDictionary in case cleanups didn't
95
# get fired properly. Maybe we just assume we don't have to do it?
96
def register_on_hangup(identifier, a_callable):
97
"""Register for us to call a_callable as part of a graceful shutdown."""
98
if _on_sighup is None:
100
_on_sighup[identifier] = a_callable
103
def unregister_on_hangup(identifier):
104
"""Remove a callback from being called during sighup."""
105
if _on_sighup is None:
108
del _on_sighup[identifier]
109
except KeyboardInterrupt:
112
# This usually runs as a tear-down step. So we don't want to propagate
114
trace.mutter('Error occurred during unregister_on_hangup:')
115
trace.log_exception_quietly()