Bugfix: xcwd always failing to fetch processes.

- Processes names may apparently contain parenthesis, which makes the old
    fscanf function always fail. (example: (sd-pam))
  - Fix bug, probably due to %*3c format, that made parent pid fetching fail
    sometimes, cutting the first digit of the ppid.
This commit is contained in:
Franck Michea 2013-09-24 02:06:40 +02:00
parent 068872e0c8
commit a625a378cb

12
xcwd.c
View File

@ -155,7 +155,8 @@ static processes_t getProcesses(void)
processes_t p = NULL; processes_t p = NULL;
#ifdef LINUX #ifdef LINUX
glob_t globbuf; glob_t globbuf;
unsigned int i, j; unsigned int i, j, k;
char line[201] = {0};
glob("/proc/[0-9]*", GLOB_NOSORT, NULL, &globbuf); glob("/proc/[0-9]*", GLOB_NOSORT, NULL, &globbuf);
p = malloc(sizeof(struct processes_s)); p = malloc(sizeof(struct processes_s));
@ -172,9 +173,12 @@ static processes_t getProcesses(void)
tn = fopen(name, "r"); tn = fopen(name, "r");
if (tn == NULL) if (tn == NULL)
continue; continue;
if(fscanf(tn, "%ld (%32[^)] %*3c %ld", &p->ps[j].pid, fread(line, 200, 1, tn);
p->ps[j].name, &p->ps[j].ppid) != 3) p->ps[j].pid = atoi(strtok(line, " "));
return NULL; k = snprintf(p->ps[j].name, 32, "%s", strtok(NULL, " ") + 1);
p->ps[j].name[k - 1] = 0;
strtok(NULL, " "); // discard process state
p->ps[j].ppid = atoi(strtok(NULL, " "));
LOG("\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name, p->ps[j].pid, LOG("\t%-20s\tpid=%6ld\tppid=%6ld\n", p->ps[j].name, p->ps[j].pid,
p->ps[j].ppid); p->ps[j].ppid);
fclose(tn); fclose(tn);