File tree Expand file tree Collapse file tree 1 file changed +28
-9
lines changed
Expand file tree Collapse file tree 1 file changed +28
-9
lines changed Original file line number Diff line number Diff line change 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.
276279static 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
You can’t perform that action at this time.
0 commit comments