~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/breakin.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
18
import signal
19
19
 
 
20
 
 
21
_breakin_signal_number = None
 
22
_breakin_signal_name = None
 
23
 
 
24
 
20
25
def _debug(signal_number, interrupted_frame):
21
26
    import pdb
22
27
    import sys
23
 
    sys.stderr.write("** SIGQUIT received, entering debugger\n"
 
28
    sys.stderr.write("** %s received, entering debugger\n"
24
29
            "** Type 'c' to continue or 'q' to stop the process\n"
25
 
            "** Or SIGQUIT again to quit (and possibly dump core)\n"
26
 
            )
 
30
            "** Or %s again to quit (and possibly dump core)\n"
 
31
            % (_breakin_signal_name, _breakin_signal_name))
 
32
    # It seems that on Windows, when sys.stderr is to a PIPE, then we need to
 
33
    # flush. Not sure why it is buffered, but that seems to be the case.
 
34
    sys.stderr.flush()
27
35
    # restore default meaning so that you can kill the process by hitting it
28
36
    # twice
29
 
    signal.signal(signal.SIGQUIT, signal.SIG_DFL)
 
37
    signal.signal(_breakin_signal_number, signal.SIG_DFL)
30
38
    try:
31
39
        pdb.set_trace()
32
40
    finally:
33
 
        signal.signal(signal.SIGQUIT, _debug)
34
 
 
35
 
 
36
 
def hook_sigquit():
37
 
    # when sigquit (C-\) is received go into pdb
38
 
    if (os.environ.get('BZR_SIGQUIT_PDB', '1') == '0'
39
 
        or getattr(signal, 'SIGQUIT', None) is None):
40
 
        return
41
 
    signal.signal(signal.SIGQUIT, _debug)
 
41
        signal.signal(_breakin_signal_number, _debug)
 
42
 
 
43
 
 
44
def determine_signal():
 
45
    global _breakin_signal_number
 
46
    global _breakin_signal_name
 
47
    if _breakin_signal_number is not None:
 
48
        return _breakin_signal_number
 
49
    # Note: As near as I can tell, Windows is the only one to define SIGBREAK,
 
50
    #       and other platforms defined SIGQUIT. There doesn't seem to be a
 
51
    #       platform that defines both.
 
52
    #       -- jam 2009-07-30
 
53
    sigquit = getattr(signal, 'SIGQUIT', None)
 
54
    sigbreak = getattr(signal, 'SIGBREAK', None)
 
55
    if sigquit is not None:
 
56
        _breakin_signal_number = sigquit
 
57
        _breakin_signal_name = 'SIGQUIT'
 
58
    elif sigbreak is not None:
 
59
        _breakin_signal_number = sigbreak
 
60
        _breakin_signal_name = 'SIGBREAK'
 
61
 
 
62
    return _breakin_signal_number
 
63
 
 
64
 
 
65
def hook_debugger_to_signal():
 
66
    """Add a signal handler so we drop into the debugger.
 
67
 
 
68
    On Unix, this is hooked into SIGQUIT (C-\\), and on Windows, this is
 
69
    hooked into SIGBREAK (C-Pause).
 
70
    """
 
71
 
 
72
    # when sigquit (C-\) or sigbreak (C-Pause) is received go into pdb
 
73
    if os.environ.get('BZR_SIGQUIT_PDB', '1') == '0':
 
74
        # User explicitly requested we don't support this
 
75
        return
 
76
    sig = determine_signal()
 
77
    if sig is None:
 
78
        return
 
79
    # print 'hooking into %s' % (_breakin_signal_name,)
 
80
    signal.signal(sig, _debug)