Skip to content
Snippets Groups Projects
Commit af5c2f94 authored by James Arcus's avatar James Arcus
Browse files

VendServer - add writing traceback to file on SIGUSR1

parent b37129e7
No related merge requests found
"""
TracebackPrinter.py - Prints the current stack trace to a specified
file when SIGUSR1 is received.
---
Author: James Arcus <jimbo@ucc.asn.au>
Based-On: python-traceback-signal
<https://github.com/angelcam/python-traceback-signal>
Author: Angelcam <dev@angelcam.com>
"""
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)
...@@ -20,6 +20,7 @@ from SnackConfig import get_snack#, get_snacks ...@@ -20,6 +20,7 @@ from SnackConfig import get_snack#, get_snacks
import socket import socket
from posix import geteuid from posix import geteuid
from OpenDispense import OpenDispense as Dispense from OpenDispense import OpenDispense as Dispense
import TracebackPrinter
CREDITS=""" CREDITS="""
This vending machine software brought to you by: This vending machine software brought to you by:
...@@ -963,6 +964,7 @@ def parse_args(): ...@@ -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('-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('-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('--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() options, args = op.parse_args()
if len(args) != 0: if len(args) != 0:
...@@ -991,7 +993,7 @@ def set_stuff_up(): ...@@ -991,7 +993,7 @@ def set_stuff_up():
if options.daemon: become_daemon() if options.daemon: become_daemon()
set_up_logging(options) set_up_logging(options)
if options.pid_file != '': create_pid_file(options.pid_file) if options.pid_file != '': create_pid_file(options.pid_file)
if options.traceback_file != '': TracebackPrinter.traceback_init(options.traceback_file)
return options, config_opts return options, config_opts
def clean_up_nicely(options, config_opts): def clean_up_nicely(options, config_opts):
...@@ -1055,7 +1057,7 @@ def do_vend_server(options, config_opts): ...@@ -1055,7 +1057,7 @@ def do_vend_server(options, config_opts):
continue continue
# run_forever(rfh, wfh, options, config_opts) # run_forever(rfh, wfh, options, config_opts)
try: try:
vserver = VendServer() vserver = VendServer()
vserver.run_forever(rfh, wfh, options, config_opts) vserver.run_forever(rfh, wfh, options, config_opts)
......
...@@ -16,6 +16,7 @@ DESC="VendServer" ...@@ -16,6 +16,7 @@ DESC="VendServer"
NAME=vendserver NAME=vendserver
DAEMON="/usr/local/uccvend-vendserver/vendserver" DAEMON="/usr/local/uccvend-vendserver/vendserver"
PIDFILE=/var/run/$NAME.pid PIDFILE=/var/run/$NAME.pid
TBFILE=/var/run/vendtraces.log
SCRIPTNAME=/etc/init.d/$NAME SCRIPTNAME=/etc/init.d/$NAME
DAEMON_ARGS="" DAEMON_ARGS=""
...@@ -29,7 +30,7 @@ fi ...@@ -29,7 +30,7 @@ fi
d_start() { d_start() {
start-stop-daemon --start --pidfile $PIDFILE --nicelevel 5 \ start-stop-daemon --start --pidfile $PIDFILE --nicelevel 5 \
--startas $DAEMON -- -d -sdaemon --pid-file=$PIDFILE \ --startas $DAEMON -- -d -sdaemon --pid-file=$PIDFILE \
$DAEMON_ARGS --traceback-file=$TBFILE $DAEMON_ARGS
} }
d_stop() { d_stop() {
......
...@@ -22,7 +22,7 @@ test_requirements = [ ...@@ -22,7 +22,7 @@ test_requirements = [
setup( setup(
name='uccvend-vendserver', name='uccvend-vendserver',
version='1.1', version='1.1.1',
description='UCC Snack Machine Server Code', description='UCC Snack Machine Server Code',
long_description=readme + '\n\n' + history, long_description=readme + '\n\n' + history,
author='ACC Murphy', author='ACC Murphy',
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment