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

Commit

Permalink
Merge pull request #197 from tanty/wip/tanty/remove-signal-handlers-f…
Browse files Browse the repository at this point in the history
…rom-dhcp

dhcp: only install signal handlers from the server
  • Loading branch information
icb- authored Apr 10, 2022
2 parents 90c263d + 827d85f commit 092f7ae
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 @@ -237,6 +238,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

def load_dhcp_static_config(filename):
result = ['', dict()]

Expand Down Expand Up @@ -290,6 +298,10 @@ def load_dhcp_static_config(filename):
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 092f7ae

Please sign in to comment.