Skip to content

Commit 2d1a921

Browse files
g0mb4brndnmtthws
authored andcommitted
Use ps to check the running processes.
The previous method used `/proc` to check the running processes, but it is a Linux-specific solution. This method uses `ps`, hopefully in a POSIX-compliant way. Linux: https://man7.org/linux/man-pages/man1/ps.1.html FreeBSD: https://man.freebsd.org/cgi/man.cgi?ps(1) OpenBSD: https://man.openbsd.org/ps.1
1 parent 0a5530a commit 2d1a921

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

src/main.cc

Lines changed: 10 additions & 34 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 */
@@ -276,45 +274,23 @@ static void print_help(const char *prog_name) {
276274
}
277275

278276
static bool is_conky_already_running() {
279-
DIR* dir;
280-
struct dirent* ent;
281-
char* endptr;
282-
char buf[512];
283-
284-
const size_t len_conky = 5; // "conky"
277+
char line[512];
285278
int instances = 0;
286279

287-
if (!(dir = opendir("/proc"))) {
288-
NORM_ERR("can't open /proc: %s\n", strerror(errno));
289-
return false;
280+
FILE *fp = popen("ps xo comm", "r");
281+
if (!fp) {
282+
NORM_ERR("unable to open ps");
283+
return false;
290284
}
291285

292-
while ((ent = readdir(dir)) != NULL) {
293-
long lpid = strtol(ent->d_name, &endptr, 10);
294-
if (*endptr != '\0') {
295-
continue;
296-
}
297-
298-
snprintf(buf, sizeof(buf), "/proc/%ld/cmdline", lpid);
299-
FILE* fp = fopen(buf, "r");
300-
if (fp) {
301-
if (fgets(buf, sizeof(buf), fp) != NULL) {
302-
const char* name = strtok(buf, " ");
303-
const size_t len = strlen(name);
304-
305-
if (len >= len_conky) {
306-
name = name + len - len_conky;
307-
}
308-
309-
if (!strcmp(name, "conky")) {
310-
instances++;
311-
}
312-
}
313-
fclose(fp);
286+
while (fgets(line, sizeof(line), fp)) {
287+
if (!strncmp(line, "conky", 5)) {
288+
instances++;
314289
}
315290
}
316291

317-
closedir(dir);
292+
pclose(fp);
293+
318294
return instances > 1;
319295
}
320296

0 commit comments

Comments
 (0)