Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve process ID checking #1107

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Changelog
+++++++++

3.0.2 - 01/XX/2024
3.0.2 - ??/??/2024
==================
**Bug Fixes**

- Fixed an issue where plugins with unrecognized file extensions would not be executed. (Blake Bahner)
- Fixed an issue where NCPA would fail to restart after rebooting the host server (Sebastian Wolf)
- Fixed an issue where NCPA would crash if the passive log file was not present. (Ivan-Roger)
- Fixed an issue where plugins would fail to execute if the group had permission, but the user did not. (graham-collinson)
- Fixed an issue where NCPA would crash if ssl_ciphers was set for the listener. (Ivan-Roger)
Expand Down
33 changes: 19 additions & 14 deletions agent/ncpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import sys
import tempfile
import time
import psutil

import errno
import signal
Expand Down Expand Up @@ -684,22 +685,26 @@ def check_pid(self):
msg = 'Pidfile %s contains a non-integer value' % self.pidfile
self.logger.debug(msg)
sys.exit(msg)
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# The pid doesn't exist, so remove the stale pidfile.
self.logger.debug("Daemon - check_pid() - The pid doesn't exist, so remove the stale pidfile")
os.remove(self.pidfile)

# Check that the process ID corresponds to NCPA
process = psutil.Process(pid)
if process.name().lower() in ['ncpa', 'ncpa.exe']:
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
# The pid doesn't exist, so remove the stale pidfile.
self.logger.debug("Daemon - check_pid() - The pid doesn't exist, so remove the stale pidfile")
os.remove(self.pidfile)
else:
msg = ("Daemon - check_pid() - Failed to check status of process %s "
"from pidfile %s: %s" % (pid, self.pidfile, err.strerror))
self.logger.debug(msg)
sys.exit(msg)
else:
msg = ("Daemon - check_pid() - Failed to check status of process %s "
"from pidfile %s: %s" % (pid, self.pidfile, err.strerror))
self.logger.debug(msg)
msg = ('Daemon - check_pid() - Another instance is already running (pid %s)' % pid)
self.logger.warning(msg)
sys.exit(msg)
else:
msg = ('Daemon - check_pid() - Another instance is already running (pid %s)' % pid)
self.logger.warning(msg)
sys.exit(msg)

def check_pid_writable(self):
u"""Verify the user has access to write to the pid file.
Expand Down