From b01485b3da39592ac0903ef0bd42be0e4b889824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3th=20J=C3=A1nos?= Date: Mon, 4 Nov 2024 09:14:44 +0100 Subject: [PATCH] Move `is_conky_already_running()` to src/linux.cc. --- src/linux.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ src/linux.h | 2 ++ src/main.cc | 50 +++++++------------------------------------------- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/linux.cc b/src/linux.cc index 5a2a822f7..24bc40506 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -3147,3 +3147,45 @@ void get_top_info(void) { calc_io_each(); /* percentage of I/O for each task */ #endif /* BUILD_IOSTATS */ } + +/****************************************** + * Check if more than one conky process * + * is running * + ******************************************/ + +bool is_conky_already_running(void) { + DIR *dir; + struct dirent *ent; + char buf[512]; + int instances = 0; + + if (!(dir = opendir("/proc"))) { + NORM_ERR("can't open /proc: %s\n", strerror(errno)); + return false; + } + + while ((ent = readdir(dir)) != NULL) { + char *endptr = ent->d_name; + long lpid = strtol(ent->d_name, &endptr, 10); + if (*endptr != '\0') { + continue; + } + + snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid); + FILE *fp = fopen(buf, "r"); + if (!fp) { + continue; + } + + if (fgets(buf, sizeof(buf), fp) != NULL) { + char *conky = strstr(buf, "(conky)"); + if (conky) { + instances++; + } + } + fclose(fp); + } + + closedir(dir); + return instances > 1; +} diff --git a/src/linux.h b/src/linux.h index 91027f233..f5b9f4097 100644 --- a/src/linux.h +++ b/src/linux.h @@ -58,6 +58,8 @@ int update_stat(void); void print_distribution(struct text_object *, char *, unsigned int); +bool is_conky_already_running(void); + extern char e_iface[64]; extern char interfaces_arr[MAX_NET_INTERFACES][64]; diff --git a/src/main.cc b/src/main.cc index cdbc24437..327ef59fd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -36,8 +36,6 @@ #include "display-output.hh" #include "lua-config.hh" -#include - #ifdef BUILD_X11 #include "x11.h" #endif /* BUILD_X11 */ @@ -46,6 +44,10 @@ #include "ccurl_thread.h" #endif /* BUILD_CURL */ +#if defined(__linux__) +#include "linux.h" +#endif /* Linux */ + #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) #include "freebsd.h" #endif /* FreeBSD */ @@ -271,49 +273,11 @@ static void print_help(const char *prog_name) { " (and quit)\n" " -p, --pause=SECS pause for SECS seconds at startup " "before doing anything\n" - " -U, --unique only one conky process can be created\n", - prog_name); -} - #if defined(__linux__) -// NOTE: Only works on systems where there is a /proc/[pid]/stat file. -static bool is_conky_already_running() { - DIR *dir; - struct dirent *ent; - char buf[512]; - int instances = 0; - - if (!(dir = opendir("/proc"))) { - NORM_ERR("can't open /proc: %s\n", strerror(errno)); - return false; - } - - while ((ent = readdir(dir)) != NULL) { - char *endptr = ent->d_name; - long lpid = strtol(ent->d_name, &endptr, 10); - if (*endptr != '\0') { - continue; - } - - snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid); - FILE *fp = fopen(buf, "r"); - if (!fp) { - continue; - } - - if (fgets(buf, sizeof(buf), fp) != NULL) { - char *conky = strstr(buf, "(conky)"); - if (conky) { - instances++; - } - } - fclose(fp); - } - - closedir(dir); - return instances > 1; -} + " -U, --unique only one conky process can be created\n" #endif /* Linux */ + , prog_name); +} inline void reset_optind() { #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \