Skip to content

Commit

Permalink
Move is_conky_already_running() to src/linux.cc.
Browse files Browse the repository at this point in the history
  • Loading branch information
g0mb4 authored and brndnmtthws committed Nov 5, 2024
1 parent 3ed6524 commit b01485b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
42 changes: 42 additions & 0 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
50 changes: 7 additions & 43 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include "display-output.hh"
#include "lua-config.hh"

#include <dirent.h>

#ifdef BUILD_X11
#include "x11.h"
#endif /* BUILD_X11 */
Expand All @@ -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 */
Expand Down Expand Up @@ -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__) || \
Expand Down

0 comments on commit b01485b

Please sign in to comment.