diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-09-06 18:12:21 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-09-06 18:12:21 -0700 |
commit | 01ad8fc5b1928c01367b32022ceab09f1dc5dc26 (patch) | |
tree | b6e0959f436c98916e2b1ff8e12f541cad3b2f42 /cloudinit | |
parent | 75bec162045856b39b483d11a0b3a7a75886efd2 (diff) | |
download | vyos-cloud-init-01ad8fc5b1928c01367b32022ceab09f1dc5dc26.tar.gz vyos-cloud-init-01ad8fc5b1928c01367b32022ceab09f1dc5dc26.zip |
Add capturing of basic signal handlers to
handle those signals more gracefully and
with better messaging than what comes builtin.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/signal_handler.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/cloudinit/signal_handler.py b/cloudinit/signal_handler.py new file mode 100644 index 00000000..244293b4 --- /dev/null +++ b/cloudinit/signal_handler.py @@ -0,0 +1,65 @@ +# vi: ts=4 expandtab +# +# Copyright (C) 2012 Canonical Ltd. +# Copyright (C) 2012 Yahoo! Inc. +# +# Author: Scott Moser <scott.moser@canonical.com> +# Author: Joshua Harlow <harlowja@yahoo-inc.com> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import inspect +import signal +import sys + +from StringIO import StringIO + +from cloudinit import util +from cloudinit import version as vr + + +EXIT_FOR = { + signal.SIGINT: ('Cloud-init %(version)s interrupted, exiting...', 1), + signal.SIGTERM: ('Cloud-init %(version)s terminated, exiting...', 1), + # signal.SIGKILL: ('Cloud-init killed, exiting...', 1), - Can't be caught... + signal.SIGABRT: ('Cloud-init %(version)s aborted, exiting...', 1), +} + + +def _pprint_frame(frame, depth=1, max_depth=3): + if depth > max_depth or not frame: + return + frame_info = inspect.getframeinfo(frame) + prefix = " " * (depth * 2) + contents = StringIO() + contents.write("%sFilename: %s\n" % (prefix, frame_info.filename)) + contents.write("%sFunction: %s\n" % (prefix, frame_info.function)) + contents.write("%sLine number: %s\n" % (prefix, frame_info.lineno)) + util.multi_log(contents.getvalue()) + _pprint_frame(frame.f_back, depth + 1, max_depth) + + +def _handle_exit(signum, frame): + (msg, rc) = EXIT_FOR[signum] + msg = msg % ({'version': vr.version()}) + util.multi_log("%s\n" % (msg)) + _pprint_frame(frame) + sys.exit(rc) + + +def attach_handlers(): + sigs_attached = 0 + for signum in EXIT_FOR.keys(): + signal.signal(signum, _handle_exit) + sigs_attached += len(EXIT_FOR) + return sigs_attached |