Skip to content

Commit

Permalink
Kernel.py: wait for thread when exiting
Browse files Browse the repository at this point in the history
Wait for thread loop to return when exiting
  • Loading branch information
leoplo committed May 3, 2022
1 parent 7519836 commit c5995ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
31 changes: 21 additions & 10 deletions pimdm/Kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def __init__(self, kernel_socket):
self.tree_logger = Main.logger.getChild('KernelTree')

# receive signals from kernel with a background thread
handler_thread = Thread(target=self.handler)
handler_thread.daemon = True
handler_thread.start()
self.handler_thread = Thread(target=self.handler)
self.handler_thread.daemon = True
self.handler_thread.start()

'''
Structure to create/remove virtual interfaces
Expand Down Expand Up @@ -177,9 +177,14 @@ def remove_multicast_route(self, kernel_entry: KernelEntry):
raise NotImplementedError

@abstractmethod
def exit(self):
def close_socket(self):
raise NotImplementedError

def exit(self):
self.running = False
self.close_socket()
self.handler_thread.join()

@abstractmethod
def handler(self):
raise NotImplementedError
Expand Down Expand Up @@ -412,12 +417,15 @@ def remove_multicast_route(self, kernel_entry: KernelEntry):
if len(self.routing[kernel_entry.source_ip]) == 0:
self.routing.pop(kernel_entry.source_ip)

def exit(self):
self.running = False
def close_socket(self):
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
self.socket.setsockopt(socket.IPPROTO_IP, self.MRT_DONE, 1)
self.socket.close()
super().exit()


'''
Expand Down Expand Up @@ -660,12 +668,15 @@ def remove_multicast_route(self, kernel_entry: KernelEntry):
if len(self.routing[kernel_entry.source_ip]) == 0:
self.routing.pop(kernel_entry.source_ip)

def exit(self):
self.running = False
def close_socket(self):
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
self.socket.setsockopt(socket.IPPROTO_IPV6, self.MRT6_DONE, 1)
self.socket.close()
super().exit()

'''
/*
Expand Down
11 changes: 3 additions & 8 deletions pimdm/Run.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,33 +180,28 @@ def main():

if args.start:
start()
sys.exit(0)
elif args.stop:
client_socket(args)
sys.exit(0)
elif args.config:
try:
from pimdm import Config
conf_file_path = os.path.abspath(args.config[0])
pim_globals.MULTICAST_TABLE_ID, pim_globals.UNICAST_TABLE_ID = Config.get_vrfs(conf_file_path)
start(conf_file_path)
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\"")
elif args.verbose:
os.system("tail -f {}".format(PROCESS_LOG_STDOUT_FILE.format(pim_globals.MULTICAST_TABLE_ID)))
sys.exit(0)
elif args.multicast_routes:
if args.ipv4 or not args.ipv6:
os.system("ip mroute show table " + str(pim_globals.MULTICAST_TABLE_ID))
elif args.ipv6:
os.system("ip -6 mroute show table " + str(pim_globals.MULTICAST_TABLE_ID))
sys.exit(0)
elif not is_running():
print("PIM-DM is not running")
parser.print_usage()
sys.exit(0)

client_socket(args)
else:
client_socket(args)

def process_file_path():
return os.path.join(PROCESS_DIRECTORY, str(pim_globals.MULTICAST_TABLE_ID))
Expand Down

0 comments on commit c5995ae

Please sign in to comment.