Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
dhcp: only install signal handlers from the server
Browse files Browse the repository at this point in the history
"Silently" installing signal handlers from a module that may be
imported from another project is not the best idea.

Instead we move them to the server, which is where we want them here
and as an usage example for other PyPXE users.
  • Loading branch information
Andres Gomez committed Apr 8, 2022
1 parent c71bc52 commit cb4e3d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 1 addition & 11 deletions pypxe/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import struct
import os
import logging
import signal
import json
from collections import defaultdict
from time import time
Expand Down Expand Up @@ -130,12 +129,7 @@ def __init__(self, **server_settings):
except ValueError:
pass

signal.signal(signal.SIGINT, self.export_leases)
signal.signal(signal.SIGTERM, self.export_leases)
signal.signal(signal.SIGALRM, self.export_leases)
signal.signal(signal.SIGHUP, self.export_leases)

def export_leases(self, signum, frame):
def export_leases(self):
if self.save_leases_file:
export_safe = dict()
for lease in self.leases:
Expand All @@ -145,10 +139,6 @@ def export_leases(self, signum, frame):
json.dump(export_safe, leases_file)
self.logger.info('Exported leases to {0}'.format(self.save_leases_file))

# if keyboard interrupt, propagate upwards
if signum == signal.SIGINT:
raise KeyboardInterrupt

def get_namespaced_static(self, path, fallback = {}):
statics = self.static_config
for child in path.split('.'):
Expand Down
12 changes: 12 additions & 0 deletions pypxe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import logging
import logging.handlers
import signal

try:
import argparse
Expand Down Expand Up @@ -250,6 +251,13 @@ def main():
# configure/start DHCP server
if args.USE_DHCP:

def dhcp_export_leases(signum, frame):
dhcp_server.export_leases()

# if keyboard interrupt, propagate upwards
if signum == signal.SIGINT:
raise KeyboardInterrupt

# setup DHCP logger
dhcp_logger = helpers.get_child_logger(sys_logger, 'DHCP')
if args.DHCP_MODE_PROXY:
Expand Down Expand Up @@ -278,6 +286,10 @@ def main():
static_config = loaded_statics,
logger = dhcp_logger,
saveleases = args.LEASES_FILE)
signal.signal(signal.SIGINT, dhcp_export_leases)
signal.signal(signal.SIGTERM, dhcp_export_leases)
signal.signal(signal.SIGALRM, dhcp_export_leases)
signal.signal(signal.SIGHUP, dhcp_export_leases)
dhcpd = threading.Thread(target = dhcp_server.listen)
dhcpd.daemon = True
dhcpd.start()
Expand Down

0 comments on commit cb4e3d3

Please sign in to comment.