in src/watchdog/__init__.py [0:0]
def is_mount_stunnel_proc_running(state_pid, state_file, state_file_dir):
"""
Check whether the PID in the state file corresponds to a running efs-proxy/stunnel process.
Although this code was originally written to check if stunnel is running, we've modified
it to support the efs-proxy process as well.
The proxy or stunnel process is counted as running iff:
1. The pid in state file is not None.
2. The process running with the pid is a stunnel process. This is validated through process command name.
3. The process can be reached via os.kill(pid, 0).
4. Every launched stunnel process will write its process id to the pid file in the mount state_file_dir, and only
when the stunnel is terminated this pid file can be removed. Check whether the stunnel pid file exists and its
value is equal to the pid documented in state file. This step is to make sure we don't send signal later to any
stunnel process that is not owned by the mount.
:param state_pid: The pid in state file.
:param state_file: The state file path, e.g. fs-deadbeef.mnt.20560.
:param state_file_dir: The state file dir path, e.g. /var/run/efs.
"""
if not state_pid:
logging.debug("State pid is None for %s", state_file)
return False
process_name = check_process_name(state_pid)
if not process_name or (
"efs-proxy" not in str(process_name) and "stunnel" not in str(process_name)
):
logging.debug(
"Process running on %s is not an efs-proxy or stunnel process, full command: %s.",
state_pid,
str(process_name) if process_name else "",
)
return False
if not is_pid_running(state_pid):
logging.debug(
"Stunnel or efs-proxy process with pid %s is not running anymore for %s.",
state_pid,
state_file,
)
return False
pid_in_stunnel_pid_file = get_pid_in_state_dir(state_file, state_file_dir)
# efs-utils versions older than 1.32.2 does not create a pid file in state dir
# To avoid the healthy stunnel established by those version to be treated as not running due to the missing pid file, which can result in stunnel being constantly restarted,
# assuming the stunnel is still running even if the stunnel pid file does not exist.
if not pid_in_stunnel_pid_file:
logging.debug(
"Pid file of stunnel does not exist for %s. It is possible that the stunnel is no longer running or the mount was mounted using an older version efs-utils (<1.32.2). Assuming the stunnel with pid %s is still running.",
state_file,
state_pid,
)
elif int(state_pid) != int(pid_in_stunnel_pid_file):
logging.warning(
"Stunnel pid mismatch in state file (pid = %s) and stunnel pid file (pid = %s). Assuming the "
"stunnel is not running.",
int(state_pid),
int(pid_in_stunnel_pid_file),
)
return False
logging.debug("TLS tunnel for %s is running with pid %s", state_file, state_pid)
return True