3
# Copyright (C) 2012 Canonical Ltd.
4
# Copyright (C) 2012 Yahoo! Inc.
6
# Author: Scott Moser <scott.moser@canonical.com>
7
# Author: Joshua Harlow <harlowja@yahoo-inc.com>
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU General Public License version 3, as
11
# published by the Free Software Foundation.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
from six import StringIO
27
from cloudinit import log as logging
28
from cloudinit import util
29
from cloudinit import version as vr
31
LOG = logging.getLogger(__name__)
34
BACK_FRAME_TRACE_DEPTH = 3
36
signal.SIGINT: ('Cloud-init %(version)s received SIGINT, exiting...', 1),
37
signal.SIGTERM: ('Cloud-init %(version)s received SIGTERM, exiting...', 1),
39
# signal.SIGKILL: ('Cloud-init killed, exiting...', 1),
40
signal.SIGABRT: ('Cloud-init %(version)s received SIGABRT, exiting...', 1),
44
def _pprint_frame(frame, depth, max_depth, contents):
45
if depth > max_depth or not frame:
47
frame_info = inspect.getframeinfo(frame)
48
prefix = " " * (depth * 2)
49
contents.write("%sFilename: %s\n" % (prefix, frame_info.filename))
50
contents.write("%sFunction: %s\n" % (prefix, frame_info.function))
51
contents.write("%sLine number: %s\n" % (prefix, frame_info.lineno))
52
_pprint_frame(frame.f_back, depth + 1, max_depth, contents)
55
def _handle_exit(signum, frame):
56
(msg, rc) = EXIT_FOR[signum]
57
msg = msg % ({'version': vr.version()})
59
contents.write("%s\n" % (msg))
60
_pprint_frame(frame, 1, BACK_FRAME_TRACE_DEPTH, contents)
61
util.multi_log(contents.getvalue(),
62
console=True, stderr=False, log=LOG)
66
def attach_handlers():
68
for signum in EXIT_FOR.keys():
69
signal.signal(signum, _handle_exit)
70
sigs_attached += len(EXIT_FOR)