From: James Arcus Date: Sun, 23 Jun 2019 06:41:58 +0000 (+0800) Subject: Add SIGUSR1 traceback (made by jimbo, committed by TPG) X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=115d5500cfbdc1543f64602cb2b1b0764dcbb276;p=uccvend-vendserver.git Add SIGUSR1 traceback (made by jimbo, committed by TPG) --- diff --git a/TracebackPrinter.py b/TracebackPrinter.py new file mode 100644 index 0000000..d89d695 --- /dev/null +++ b/TracebackPrinter.py @@ -0,0 +1,30 @@ +""" +TracebackPrinter.py - Prints the current stack trace to a specified +file when SIGUSR1 is received. +--- +Author: James Arcus +Based-On: python-traceback-signal + +Author: Angelcam +""" + +import os +import sys +import traceback +import signal + +def print_traceback(wfd, frame): + msg = "Traceback signal received.\nTraceback (most recent call last):\n" + msg += ''.join(traceback.format_stack(frame)) + os.write(wfd, msg) + +def register_sigusr(wfd): + def sigusr_handler(_, frame): + # first param is which signal + print_traceback(wfd, frame) + + signal.signal(signal.SIGUSR1, sigusr_handler) + +def traceback_init(f): + wfd = os.open(f, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o600) + register_sigusr(wfd) diff --git a/VendServer/VendServer.py b/VendServer/VendServer.py index c3cb3cb..cf9c8bb 100755 --- a/VendServer/VendServer.py +++ b/VendServer/VendServer.py @@ -20,6 +20,7 @@ from SnackConfig import get_snack#, get_snacks import socket from posix import geteuid from OpenDispense import OpenDispense as Dispense +import TracebackPrinter CREDITS=""" This vending machine software brought to you by: @@ -963,6 +964,7 @@ def parse_args(): op.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False, help='spit out lots of debug output') op.add_option('-q', '--quiet', dest='quiet', action='store_true', default=False, help='only report errors') op.add_option('--pid-file', dest='pid_file', metavar='FILE', default='', help='store daemon\'s pid in the given file') + op.add_option('--traceback-file', dest='traceback_file', default='', help='destination to print tracebacks when receiving SIGUSR1') options, args = op.parse_args() if len(args) != 0: @@ -991,7 +993,7 @@ def set_stuff_up(): if options.daemon: become_daemon() set_up_logging(options) if options.pid_file != '': create_pid_file(options.pid_file) - + if options.traceback_file != '': TracebackPrinter.traceback_init(options.traceback_file) return options, config_opts def clean_up_nicely(options, config_opts): @@ -1055,7 +1057,7 @@ def do_vend_server(options, config_opts): continue # run_forever(rfh, wfh, options, config_opts) - + try: vserver = VendServer() vserver.run_forever(rfh, wfh, options, config_opts)