From 0e44d64d25bc347cfb462d720f5f6d4ac47167a3 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 5 Apr 2025 10:33:09 +0200 Subject: [PATCH 1/3] Add search_proc() function Signed-off-by: DL6ER --- src/procps.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/procps.h | 1 + 2 files changed, 57 insertions(+) diff --git a/src/procps.c b/src/procps.c index e74c5775..5e4d855b 100644 --- a/src/procps.c +++ b/src/procps.c @@ -359,3 +359,59 @@ bool parse_proc_stat(unsigned long *total_sum, unsigned long *idle_sum) return true; } + +/** + * @brief Searches for a process by its name in the /proc filesystem. + * + * This function iterates through the directories in /proc, which represent + * process IDs (PIDs), and checks the "comm" file in each directory to find + * a process with a matching name. + * + * @param name The name of the process to search for. + * + * @return The PID of the process if found, or -1 if no matching process is found + * or if an error occurs (e.g., unable to open /proc or a file). + * + * @note This function assumes the /proc filesystem is available and accessible. + * It also assumes that the "comm" file in each process directory contains + * the name of the process. + */ +pid_t search_proc(const char *name) +{ + DIR *dir = opendir("/proc"); + if(dir == NULL) + return -1; + + struct dirent *entry; + while((entry = readdir(dir)) != NULL) + { + // Check if the entry is a directory and contains only digits + // We skip ".", "..", "self", and friends + if(entry->d_type == DT_DIR && isdigit(entry->d_name[0])) + { + char filename[64]; + snprintf(filename, sizeof(filename), "/proc/%s/comm", entry->d_name); + FILE *file = fopen(filename, "r"); + if(file != NULL) + { + char comm[PROC_PATH_SIZ + 1] = { 0 }; + // Read the command name from the file + if(fscanf(file, "%"xstr(PROC_PATH_SIZ)"s", comm) == 1) + { + if(strcmp(comm, name) == 0) + { + // Found a matching process + fclose(file); + closedir(dir); + return atoi(entry->d_name); + } + } + fclose(file); + } + } + } + + // No process found with the given name + closedir(dir); + return -1; +} diff --git a/src/procps.h b/src/procps.h index f5f6fae7..af441686 100644 --- a/src/procps.h +++ b/src/procps.h @@ -49,5 +49,6 @@ struct proc_meminfo { bool getProcessMemory(struct proc_mem *mem, const unsigned long total_memory); bool parse_proc_meminfo(struct proc_meminfo *mem); bool parse_proc_stat(unsigned long *total_sum, unsigned long *idle_sum); +pid_t search_proc(const char *name); #endif // PROCPS_H From 17d6232fbcc7ee43d412f13e54ba8b06123d8589 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sat, 5 Apr 2025 10:36:12 +0200 Subject: [PATCH 2/3] Add check for existing clock disciplining NTP clients before starting embedded NTP server --- src/ntp/client.c | 9 +++++++++ test/libs/bats | 1 + 2 files changed, 10 insertions(+) create mode 160000 test/libs/bats diff --git a/src/ntp/client.c b/src/ntp/client.c index 159b5b62..7d62c977 100644 --- a/src/ntp/client.c +++ b/src/ntp/client.c @@ -41,6 +41,8 @@ #include "database/message-table.h" // check_capability() #include "capabilities.h" +// search_proc() +#include "procps.h" // Required accuracy of the NTP sync in seconds in order to start the NTP server // thread. If the NTP sync is less accurate than this value, the NTP server @@ -746,6 +748,13 @@ bool ntp_start_sync_thread(pthread_attr_t *attr) ntp_server_start(); return false; } + // Return early if a clock disciplining NTP client is detected + // Checks chrony, the ntp family (ntp, ntpsec and openntpd), and ntpd-rs + if(search_proc("chronyd") > 0 || search_proc("ntpd") > 0 || search_proc("ntp-daemon") > 0) + { + log_info("Clock disciplining NTP client detected, not starting embedded NTP client/server"); + return false; + } // Check if we have the ambient capabilities to set the system time. // Without CAP_SYS_TIME, we cannot set the system time and the NTP diff --git a/test/libs/bats b/test/libs/bats new file mode 160000 index 00000000..429e8641 --- /dev/null +++ b/test/libs/bats @@ -0,0 +1 @@ +Subproject commit 429e86417b52f09d491778bb3cd14eaa1d25462f From 455ff634e348bc47fa429c111b02e8fcdba39c56 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 16 Apr 2025 20:19:41 +0200 Subject: [PATCH 3/3] Undo submodule addition Signed-off-by: DL6ER --- test/libs/bats | 1 - 1 file changed, 1 deletion(-) delete mode 160000 test/libs/bats diff --git a/test/libs/bats b/test/libs/bats deleted file mode 160000 index 429e8641..00000000 --- a/test/libs/bats +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 429e86417b52f09d491778bb3cd14eaa1d25462f