/usr/share/pyshared/sclapp/signals.py is in python-sclapp 0.5.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | # Copyright (c) 2005-2007 Forest Bond.
# This file is part of the sclapp software package.
#
# sclapp is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# A copy of the license has been included in the COPYING file.
import os, signal
from copy import copy
try:
set
except NameError:
from sets import Set as set
try:
reversed
except NameError:
from sclapp.legacy_support import reversed
from sclapp.exceptions import ExitSignalError, SignalError
from sclapp.error_output import printWarning, printDebug
def _getAllSignals_iter():
for name in dir(signal):
if name.startswith('SIG') \
and (name != 'SIG_IGN') \
and (name != 'SIG_DFL'):
signum = getattr(signal, name)
yield signum
def _getRealSignals_iter(names):
for name in names:
if hasattr(signal, name):
signum = getattr(signal, name)
yield signum
ALL_SIGNALS = tuple(list(_getAllSignals_iter()))
_STD_EXIT_SIGNALS = ( )
_STD_NOTIFY_SIGNALS = ( )
_STD_DEFAULT_SIGNALS = (
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL',
'SIGABRT', 'SIGFPE', 'SIGPIPE', 'SIGTERM',
'SIGBUS', 'SIGPROF', 'SIGSYS', 'SIGTRAP',
'SIGXCPU', 'SIGXFSZ',
)
_STD_IGNORE_SIGNALS = ( 'SIGUSR1', 'SIGUSR2', 'SIGALRM', )
STD_EXIT_SIGNALS = tuple(list(_getRealSignals_iter(_STD_EXIT_SIGNALS)))
STD_NOTIFY_SIGNALS = tuple(list(_getRealSignals_iter(_STD_NOTIFY_SIGNALS)))
STD_DEFAULT_SIGNALS = tuple(list(_getRealSignals_iter(_STD_DEFAULT_SIGNALS)))
STD_IGNORE_SIGNALS = tuple(list(_getRealSignals_iter(_STD_IGNORE_SIGNALS)))
_announce_signals = True
_announce_signals_stack = [ ]
# store raised SignalError here
_exit_signal_exception = None
# signal current being handled
_handling_signal = None
# for keeping track of signal handlers; old handlers are pushed here, and
# popped back into place later
_signal_mapping_stack = [ ]
def _addToMapping(d, signums, handler_factory):
if signums is not None:
for signum in signums:
assert (signum not in d), 'Signal %u mapped more than once' % signum
d[signum] = handler_factory(signum)
def _pushSignalMapping(
exit_signals = None,
notify_signals = None,
default_signals = None,
ignore_signals = None,
announce_signals = True,
custom_handlers = None
):
global _signal_mapping_stack
d = { }
_addToMapping(d, custom_handlers, lambda signum: custom_handlers[signum])
_addToMapping(d, exit_signals, lambda signum: _exitSignalHandler)
_addToMapping(d, notify_signals, lambda signum: _notifySignalHandler)
_addToMapping(d, default_signals, lambda signum: signal.SIG_DFL)
_addToMapping(d, ignore_signals, lambda signum: signal.SIG_IGN)
for signum in d.keys():
try:
signal.signal(signum, d[signum])
except RuntimeError, e:
if e[0] == 22:
printDebug(
'Setting signal handler for signum %u: Invalid argument' % \
signum)
d[signum] = signal.getsignal(signum)
else:
raise
_signal_mapping_stack.append(d)
global _announce_signals, _announce_signals_stack
_announce_signals_stack.append(_announce_signals)
_announce_signals = announce_signals
def _popSignalMapping():
global _signal_mapping_stack
d = _signal_mapping_stack.pop()
for signum in d:
_restoreSignalHandler(signum)
global _announce_signals, _announce_signals_stack
_announce_signals = _announce_signals_stack.pop()
return d
def _pushCurrentSignalMapping():
global _signal_mapping_stack
d = { }
for signum in ALL_SIGNALS:
d[signum] = signal.getsignal(signum)
_signal_mapping_stack.append(d)
def _restoreSignalHandler(signum):
for mapping in reversed(_signal_mapping_stack):
if signum in mapping:
signal.signal(signum, mapping[signum])
def _getSignalsByHandler_iter(handler):
for signum in ALL_SIGNALS:
if signal.getsignal(signum) == handler:
yield signum
def _getSignalHandlers_iter():
for signum in ALL_SIGNALS:
yield signal.getsignal(signum)
# stores list of caught signal numbers
_caught_signals = set([ ])
def _ignoreExitSignals():
for signum in getExitSignals():
signal.signal(signum, _ignoreExitSignalHandler)
def _ignoreExitSignalHandler(signum, frame):
handler = signal.signal(signum, signal.SIG_IGN)
global _caught_signals, _exit_signal_exception, _handling_signal
if _handling_signal is None:
_handling_signal = signum
else:
_handleLameSignal(signum, handler)
return
_signalPrint('caught signal %i (ignored)' % signum)
_caught_signals.add(signum)
_handling_signal = None
signal.signal(signum, handler)
def _exitSignalHandler(signum, frame):
signal.signal(signum, signal.SIG_IGN)
global _handling_signal, _exit_signal_exception
if _exit_signal_exception is not None:
_handleLameSignal(signum, _ignoreExitSignalHandler)
return
_exit_signal_exception = ExitSignalError(signum)
_handling_signal = signum
_signalPrint('caught exit signal %i' % signum)
_caught_signals.add(signum)
_handling_signal = None
_ignoreExitSignals()
raise _exit_signal_exception
def _notifySignalHandler(signum, frame):
handler = signal.signal(signum, signal.SIG_IGN)
global _exit_signal_exception, _handling_signal
if _exit_signal_exception is not None:
_handleLameSignal(signum, handler)
return
if _handling_signal is None:
_handling_signal = signum
elif _handling_signal in getNotifySignals():
_handleLameSignal(signum, handler)
return
_signalPrint('caught signal %i' % signum)
_caught_signals.add(signum)
_handling_signal = None
signal.signal(signum, handler)
raise SignalError, signum
def _handleLameSignal(signum, handler):
_signalPrint('caught signal %i (ignored)' % signum)
_caught_signals.add(signum)
signal.signal(signum, handler)
def _signalPrint(message):
_signal_print_fn = _getSignalPrintFn()
if _signal_print_fn is not None:
from sclapp.protected_output import _protected_output_enabled, \
_signalOutput
if _protected_output_enabled:
_signalOutput()
_signal_print_fn(message)
def _getSignalPrintFn():
global _announce_signals
if _announce_signals:
return printWarning
else:
return None
def _enableSignals(signums):
for signum in signums:
if signum in getExitSignals():
if _exit_signal_exception is not None:
signal.signal(signum, _ignoreExitSignalHandler)
else:
signal.signal(signum, _exitSignalHandler)
elif signum in getNotifySignals():
signal.signal(signum, _notifySignalHandler)
elif signum in getDefaultSignals():
signal.signal(signum, signal.SIG_DFL)
elif signum in getIgnoreSignals():
signal.signal(signum, signal.SIG_IGN)
def getExitSignals():
'''getExitSignals() -> list
Returns a list containing all signal numbers that are currently being
handled by sclapp as exit signals.
'''
return set(
list(_getSignalsByHandler_iter(_exitSignalHandler)) +
list(_getSignalsByHandler_iter(_ignoreExitSignalHandler))
)
def getNotifySignals():
'''getNotifySignals() -> list
Returns a list containing all signal numbers that are currently being
handled by sclapp as notify signals.
'''
return set(list(_getSignalsByHandler_iter(_notifySignalHandler)))
def getDefaultSignals():
'''getDefaultSignals() -> list
Returns a list containing all signal numbers that are currently being
handled by sclapp as default signals (SIG_DFL).
'''
return set(list(_getSignalsByHandler_iter(signal.SIG_DFL)))
def getIgnoreSignals():
'''getIgnoreSignals() -> list
Returns a list containing all signal numbers that are currently being
handled by sclapp as ignore signals (SIG_IGN).
'''
return set(list(_getSignalsByHandler_iter(signal.SIG_IGN)))
def getCaughtSignals():
'''getCaughtSignals() -> list
Returns a list containing all signal numbers that have been caught by
sclapp signal handlers.
'''
return copy(_caught_signals)
def _parseSignal(sig):
if str(sig) == sig:
if hasattr(signal, sig):
return getattr(signal, sig)
return None
return sig
def _parseSignals(sigs):
return [signum for signum in [_parseSignal(sig) for sig in sigs] if signum]
def enableSignalHandling(exit_signals = None, notify_signals = None,
default_signals = None, ignore_signals = None, announce_signals = True,
custom_handlers = None):
'''enableSignalHandling() -> None
Enables sclapp signal handling.
'''
# Note that we use an empty list if a group is not specified:
exit_signals = _parseSignals(exit_signals or [ ])
notify_signals = _parseSignals(notify_signals or [ ])
default_signals = _parseSignals(default_signals or [ ])
ignore_signals = _parseSignals(ignore_signals or [ ])
if os.name == 'posix':
global _signal_mapping_stack
if len(_signal_mapping_stack) < 1:
_pushCurrentSignalMapping()
specified_signals = []
if custom_handlers:
specified_signals = specified_signals + custom_handlers.keys()
specified_signals = specified_signals + exit_signals
specified_signals = specified_signals + notify_signals
specified_signals = specified_signals + default_signals
specified_signals = specified_signals + ignore_signals
for signum in STD_EXIT_SIGNALS:
if signum not in specified_signals:
exit_signals.append(signum)
for signum in STD_NOTIFY_SIGNALS:
if signum not in specified_signals:
notify_signals.append(signum)
for signum in STD_DEFAULT_SIGNALS:
if signum not in specified_signals:
default_signals.append(signum)
for signum in STD_IGNORE_SIGNALS:
if signum not in specified_signals:
ignore_signals.append(signum)
_pushSignalMapping(
exit_signals = exit_signals,
notify_signals = notify_signals,
default_signals = default_signals,
ignore_signals = ignore_signals,
announce_signals = announce_signals,
custom_handlers = custom_handlers
)
def disableSignalHandling():
'''disableSignalHandling() -> None
Disables sclapp signal handling.
'''
if os.name == 'posix':
_popSignalMapping()
def signalHandlingEnabled():
handlers = list(_getSignalHandlers_iter())
if _exitSignalHandler in handlers:
return True
if _notifySignalHandler in handlers:
return True
if _ignoreExitSignalHandler in handlers:
return True
return False
|