From 3fc4c57191401f566ef6789005e2cb29547bd8f2 Mon Sep 17 00:00:00 2001 From: luckky Date: Fri, 20 Dec 2024 15:42:41 +0800 Subject: [PATCH] sysSentry: add pid file manager --- .../pidFileManager/pid_file_manager.h | 57 +++++++++++++++++++ .../pidFileManager/pid_file_manager.py | 46 +++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 src/libsentry/pidFileManager/pid_file_manager.h create mode 100644 src/libsentry/pidFileManager/pid_file_manager.py diff --git a/src/libsentry/pidFileManager/pid_file_manager.h b/src/libsentry/pidFileManager/pid_file_manager.h new file mode 100644 index 0000000..b9bcc6a --- /dev/null +++ b/src/libsentry/pidFileManager/pid_file_manager.h @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +static int handle_file_lock(int fd, bool lock) +{ + int ret; + struct flock fl; + fl.l_type = lock ? F_WRLCK : F_UNLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + + ret = fcntl(fd, F_SETLK, &fl); + if (ret < 0) { + fprintf(stderr, "fcntl failed, error msg is %s\n", strerror(errno)); + } else { + fprintf(stdout, "fcntl success, lock ret code is %d\n", ret); + } + return ret; +} + +static int check_and_set_pid_file(char* pid_file_path) +{ + int ret, fd; + fd = open(pid_file_path, O_CREAT | O_RDWR, 0600); + if (fd < 0) { + fprintf(stderr, "open file %s failed!\n", pid_file_path); + return -1; + } + + ret = handle_file_lock(fd, true); + if (ret < 0) { + fprintf(stderr, "%s is already running\n", TOOL_NAME); + close(fd); + return ret; + } + return fd; +} + +static int release_pid_file(int fd, char* pid_file_path) +{ + int ret; + ret = handle_file_lock(fd, false); + if (ret < 0) { + fprintf(stderr, "release pid file %s lock failed, error msg is %s\n", pid_file_path, strerror(errno)); + return ret; + } + + close(fd); + ret = remove(pid_file_path); + if (ret < 0) { + fprintf(stderr, "remove %s failed, error msg is %s\n", pid_file_path, strerror(errno)); + } + return ret; +} diff --git a/src/libsentry/pidFileManager/pid_file_manager.py b/src/libsentry/pidFileManager/pid_file_manager.py new file mode 100644 index 0000000..6b08a86 --- /dev/null +++ b/src/libsentry/pidFileManager/pid_file_manager.py @@ -0,0 +1,46 @@ +import os +import fcntl +import logging +from io import IOBase + +def check_and_set_pid_file(pid_file_path: str) -> int: + if type(pid_file_path) != str or pid_file_path == "": + logging.error(f"Invalid pid file path: {pid_file_path}!") + return -1 + pid_file_fd = -1 + try: + pid_file_fd = open(pid_file_path, "w") + except Exception as e: + logging.error(f"Failed to open pid file: {pid_file_path}, error is {e}") + return -1 + try: + fcntl.flock(pid_file_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except Exception as e: + logging.error(f"Failed to get lock on pid file: {pid_file_path}, error is {e}") + pid_file_fd.close() + return -1 + return pid_file_fd + +def release_pid_file(pid_file_path: str, pid_file_fd: IOBase) -> bool: + if type(pid_file_fd) != IOBase or pid_file_fd < 0: + logging.error(f"Invalid pid file fd: {pid_file_fd}!") + return False + if type(pid_file_path) != str or pid_file_path == "": + logging.error(f"Invalid pid file path: {pid_file_path}!") + pid_file_fd.close() + return False + + try: + fcntl.flock(pid_file_fd, fcntl.LOCK_UN) + except Exception as e: + logging.error(f"Failed to unlock pid file {pid_file_path}, error is {e}") + pid_file_fd.close() + return False + pid_file_fd.close() + try: + os.remove(pid_file_path) + except Exception as e: + logging.error(f"Failed to remove the pid file {pid_file_path}, error is {e}") + return False + return True + -- Gitee