Commit deb782dc authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Kernel.py: wait for thread when exiting

Wait for thread loop to return when exiting
parent 6cd91ede
...@@ -63,9 +63,9 @@ class Kernel(metaclass=ABCMeta): ...@@ -63,9 +63,9 @@ class Kernel(metaclass=ABCMeta):
self.to_format_mrule = "ip {ip_version} mrule {cmd} {selector} {interface_name} lookup {table_id}".format self.to_format_mrule = "ip {ip_version} mrule {cmd} {selector} {interface_name} lookup {table_id}".format
# receive signals from kernel with a background thread # receive signals from kernel with a background thread
handler_thread = Thread(target=self.handler) self.handler_thread = Thread(target=self.handler)
handler_thread.daemon = True self.handler_thread.daemon = True
handler_thread.start() self.handler_thread.start()
''' '''
Structure to create/remove virtual interfaces Structure to create/remove virtual interfaces
...@@ -189,9 +189,14 @@ class Kernel(metaclass=ABCMeta): ...@@ -189,9 +189,14 @@ class Kernel(metaclass=ABCMeta):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
def exit(self): def close_socket(self):
raise NotImplementedError raise NotImplementedError
def exit(self):
self.running = False
self.close_socket()
self.handler_thread.join()
@abstractmethod @abstractmethod
def handler(self): def handler(self):
raise NotImplementedError raise NotImplementedError
...@@ -449,12 +454,15 @@ class Kernel4(Kernel): ...@@ -449,12 +454,15 @@ class Kernel4(Kernel):
if len(self.routing[kernel_entry.source_ip]) == 0: if len(self.routing[kernel_entry.source_ip]) == 0:
self.routing.pop(kernel_entry.source_ip) self.routing.pop(kernel_entry.source_ip)
def exit(self): def close_socket(self):
self.running = False with socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IGMP) as s:
s.sendto(b'', ('', 0))
self.socket.close()
def exit(self):
# MRT DONE # MRT DONE
self.socket.setsockopt(socket.IPPROTO_IP, self.MRT_DONE, 1) self.socket.setsockopt(socket.IPPROTO_IP, self.MRT_DONE, 1)
self.socket.close() super().exit()
''' '''
...@@ -695,12 +703,15 @@ class Kernel6(Kernel): ...@@ -695,12 +703,15 @@ class Kernel6(Kernel):
if len(self.routing[kernel_entry.source_ip]) == 0: if len(self.routing[kernel_entry.source_ip]) == 0:
self.routing.pop(kernel_entry.source_ip) self.routing.pop(kernel_entry.source_ip)
def exit(self): def close_socket(self):
self.running = False with socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_ICMPV6) as s:
s.sendto(b'0000', ('', 0))
self.socket.close()
def exit(self):
# MRT DONE # MRT DONE
self.socket.setsockopt(socket.IPPROTO_IPV6, self.MRT6_DONE, 1) self.socket.setsockopt(socket.IPPROTO_IPV6, self.MRT6_DONE, 1)
self.socket.close() super().exit()
''' '''
/* /*
......
...@@ -172,7 +172,7 @@ def run_config(conf_file_path): ...@@ -172,7 +172,7 @@ def run_config(conf_file_path):
pim_globals.MULTICAST_TABLE_ID, pim_globals.UNICAST_TABLE_ID = Config.get_vrfs(conf_file_path) pim_globals.MULTICAST_TABLE_ID, pim_globals.UNICAST_TABLE_ID = Config.get_vrfs(conf_file_path)
start(conf_file_path) start(conf_file_path)
except (ImportError, ModuleNotFoundError): except (ImportError, ModuleNotFoundError):
sys.exit("PYYAML needs to be installed. Execute \"pip3 install pyyaml\"") raise Exception("PYYAML needs to be installed. Execute \"pip3 install pyyaml\"")
def print_multicast_routes(args): def print_multicast_routes(args):
if args.ipv4 or not args.ipv6: if args.ipv4 or not args.ipv6:
...@@ -200,22 +200,17 @@ def main(): ...@@ -200,22 +200,17 @@ def main():
if args.start: if args.start:
start() start()
sys.exit(0)
elif args.stop: elif args.stop:
client_socket(args) client_socket(args)
sys.exit(0)
elif args.config: elif args.config:
run_config(os.path.abspath(args.config[0])) run_config(os.path.abspath(args.config[0]))
elif args.verbose: elif args.verbose:
os.system("tail -f {}".format(PROCESS_LOG_STDOUT_FILE.format(pim_globals.MULTICAST_TABLE_ID))) os.system("tail -f {}".format(PROCESS_LOG_STDOUT_FILE.format(pim_globals.MULTICAST_TABLE_ID)))
sys.exit(0)
elif args.multicast_routes: elif args.multicast_routes:
print_multicast_routes(args) print_multicast_routes(args)
sys.exit(0)
elif not is_running(): elif not is_running():
print("PIM-DM is not running") print("PIM-DM is not running")
parser.print_usage() parser.print_usage()
sys.exit(0)
client_socket(args) client_socket(args)
......
Markdown is supported
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