Skip to content

Commit e6465e8

Browse files
g0mb4brndnmtthws
authored andcommitted
Go back to /proc.
Use `/proc/[pid]/stat` to check if conky is already running. This will only work on systems where there is a `/proc`.
1 parent f806d03 commit e6465e8

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/main.cc

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

39+
#include <dirent.h>
40+
3941
#ifdef BUILD_X11
4042
#include "x11.h"
4143
#endif /* BUILD_X11 */
@@ -273,24 +275,41 @@ static void print_help(const char *prog_name) {
273275
prog_name);
274276
}
275277

278+
// NOTE: Only works on systems where there is a /proc/[pid]/stat file.
276279
static bool is_conky_already_running() {
277-
char line[512];
280+
DIR *dir;
281+
struct dirent *ent;
282+
char buf[512];
278283
int instances = 0;
279284

280-
FILE *fp = popen("ps xo comm", "r");
281-
if (!fp) {
282-
NORM_ERR("unable to open ps");
285+
if (!(dir = opendir("/proc"))) {
286+
NORM_ERR("can't open /proc: %s\n", strerror(errno));
283287
return false;
284288
}
285289

286-
while (fgets(line, sizeof(line), fp)) {
287-
if (!strncmp(line, "conky\n", 6)) {
288-
instances++;
290+
while ((ent = readdir(dir)) != NULL) {
291+
char *endptr = ent->d_name;
292+
long lpid = strtol(ent->d_name, &endptr, 10);
293+
if (*endptr != '\0') {
294+
continue;
295+
}
296+
297+
snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
298+
FILE *fp = fopen(buf, "r");
299+
if (!fp) {
300+
continue;
289301
}
290-
}
291302

292-
pclose(fp);
303+
if (fgets(buf, sizeof(buf), fp) != NULL) {
304+
char *conky = strstr(buf, "(conky)");
305+
if (conky) {
306+
instances++;
307+
}
308+
}
309+
fclose(fp);
310+
}
293311

312+
closedir(dir);
294313
return instances > 1;
295314
}
296315

0 commit comments

Comments
 (0)