Skip to content

Commit b01485b

Browse files
g0mb4brndnmtthws
authored andcommitted
Move is_conky_already_running() to src/linux.cc.
1 parent 3ed6524 commit b01485b

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

src/linux.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,3 +3147,45 @@ void get_top_info(void) {
31473147
calc_io_each(); /* percentage of I/O for each task */
31483148
#endif /* BUILD_IOSTATS */
31493149
}
3150+
3151+
/******************************************
3152+
* Check if more than one conky process *
3153+
* is running *
3154+
******************************************/
3155+
3156+
bool is_conky_already_running(void) {
3157+
DIR *dir;
3158+
struct dirent *ent;
3159+
char buf[512];
3160+
int instances = 0;
3161+
3162+
if (!(dir = opendir("/proc"))) {
3163+
NORM_ERR("can't open /proc: %s\n", strerror(errno));
3164+
return false;
3165+
}
3166+
3167+
while ((ent = readdir(dir)) != NULL) {
3168+
char *endptr = ent->d_name;
3169+
long lpid = strtol(ent->d_name, &endptr, 10);
3170+
if (*endptr != '\0') {
3171+
continue;
3172+
}
3173+
3174+
snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
3175+
FILE *fp = fopen(buf, "r");
3176+
if (!fp) {
3177+
continue;
3178+
}
3179+
3180+
if (fgets(buf, sizeof(buf), fp) != NULL) {
3181+
char *conky = strstr(buf, "(conky)");
3182+
if (conky) {
3183+
instances++;
3184+
}
3185+
}
3186+
fclose(fp);
3187+
}
3188+
3189+
closedir(dir);
3190+
return instances > 1;
3191+
}

src/linux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ int update_stat(void);
5858

5959
void print_distribution(struct text_object *, char *, unsigned int);
6060

61+
bool is_conky_already_running(void);
62+
6163
extern char e_iface[64];
6264
extern char interfaces_arr[MAX_NET_INTERFACES][64];
6365

src/main.cc

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include "display-output.hh"
3737
#include "lua-config.hh"
3838

39-
#include <dirent.h>
40-
4139
#ifdef BUILD_X11
4240
#include "x11.h"
4341
#endif /* BUILD_X11 */
@@ -46,6 +44,10 @@
4644
#include "ccurl_thread.h"
4745
#endif /* BUILD_CURL */
4846

47+
#if defined(__linux__)
48+
#include "linux.h"
49+
#endif /* Linux */
50+
4951
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
5052
#include "freebsd.h"
5153
#endif /* FreeBSD */
@@ -271,49 +273,11 @@ static void print_help(const char *prog_name) {
271273
" (and quit)\n"
272274
" -p, --pause=SECS pause for SECS seconds at startup "
273275
"before doing anything\n"
274-
" -U, --unique only one conky process can be created\n",
275-
prog_name);
276-
}
277-
278276
#if defined(__linux__)
279-
// NOTE: Only works on systems where there is a /proc/[pid]/stat file.
280-
static bool is_conky_already_running() {
281-
DIR *dir;
282-
struct dirent *ent;
283-
char buf[512];
284-
int instances = 0;
285-
286-
if (!(dir = opendir("/proc"))) {
287-
NORM_ERR("can't open /proc: %s\n", strerror(errno));
288-
return false;
289-
}
290-
291-
while ((ent = readdir(dir)) != NULL) {
292-
char *endptr = ent->d_name;
293-
long lpid = strtol(ent->d_name, &endptr, 10);
294-
if (*endptr != '\0') {
295-
continue;
296-
}
297-
298-
snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
299-
FILE *fp = fopen(buf, "r");
300-
if (!fp) {
301-
continue;
302-
}
303-
304-
if (fgets(buf, sizeof(buf), fp) != NULL) {
305-
char *conky = strstr(buf, "(conky)");
306-
if (conky) {
307-
instances++;
308-
}
309-
}
310-
fclose(fp);
311-
}
312-
313-
closedir(dir);
314-
return instances > 1;
315-
}
277+
" -U, --unique only one conky process can be created\n"
316278
#endif /* Linux */
279+
, prog_name);
280+
}
317281

318282
inline void reset_optind() {
319283
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \

0 commit comments

Comments
 (0)