diff --git a/sau/services.py b/sau/services.py index 791b518..14e9404 100644 --- a/sau/services.py +++ b/sau/services.py @@ -15,7 +15,7 @@ proc_fd_map_re = re.compile(r'^.*(/[^\(]*) \(deleted\)$') def _get_deleted_open_files(proc): log = logging.getLogger(sau.LOGNAME) files = set() - + # try the linux-way first maps_file = '/proc/{}/maps'.format(proc.pid) if os.path.isfile(maps_file): @@ -25,7 +25,7 @@ def _get_deleted_open_files(proc): if match: files.add(match.group(1)) return files - + # on FreeBSD psutils open_files() helpfully returns a null path if a file # has been deleted. try: @@ -86,7 +86,7 @@ def restart_services(): # either of the above exceptions means the process has quit continue parent = _get_top_parent(proc) - + service_procs.add(parent) retest_procs.add(proc) @@ -95,6 +95,7 @@ def restart_services(): for proc in service_procs: try: service_exe = proc.exe() + proc_name = proc.name() except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied): log.debug('{} died before it could be restarted'.format(proc)) continue @@ -115,7 +116,6 @@ def restart_services(): elif policy == 'warn': log.warning('Service "{}" has open deleted files and should be restarted'.format(service)) continue - if not policy.startswith('silent'): log.warning('Restarting service {}'.format(service)) platform.restart_service(service) @@ -123,15 +123,13 @@ def restart_services(): recommend_restart = False tested_parents = set() for proc in retest_procs: - try: - name = proc.name() - except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied): - log.debug('{} was successfully killed'.format(proc)) - continue parent = _get_top_parent(proc) + parent_name = parent.name() if parent in tested_parents: log.debug('{} belongs to already tested parent {}'.format(proc, parent)) continue + if not parent: + continue if _get_deleted_open_files(proc): tested_parents.add(parent) @@ -141,12 +139,14 @@ def restart_services(): recommend_restart = True continue - if service in services and not services[service]: + log.debug('{} is in service {}'.format(proc, service)) + if parent_name in services and not services[parent_name]: log.warning('{} (parent {}) does not belong to a service and could not be restarted'.format(proc, parent)) recommend_restart = True continue - elif service in services: + elif parent_name in services: policy = _get_service_restart_policy(service) + log.debug('service {} has policy {}'.format(service, policy)) if policy in ('ignore', 'warn'): continue log.warning('{} (parent {}) still has deleted files open'.format(proc, parent)) @@ -195,7 +195,7 @@ def _get_service_from_proc(proc): log.error('Failed to find executable for process {}'.format(proc)) return None - try: + try: service_name = platform.identify_service_from_bin(service_exe) except sau.errors.UnknownServiceError: log.warning('Could not find service for process {}'.format(proc)) @@ -209,12 +209,12 @@ def _get_top_parent(proc): except (psutil.NoSuchProcess, psutil.ZombieProcess, psutil.AccessDenied): # either of the above exceptions means the process has quit return None - + if len(parents) < 2: log.debug('{} is its own top parent'.format(proc)) parent = proc else: log.debug('{} has top parent {}'.format(proc, parents[-2])) parent = parents[-2] - + return parent