Improve reading of cgroups.

This commit is contained in:
Hisham Muhammad 2015-03-08 19:47:49 -03:00
parent 39a725abc2
commit a4b03e8875
1 changed files with 18 additions and 13 deletions

View File

@ -312,22 +312,27 @@ static void LinuxProcessList_readCGroupFile(Process* process, const char* dirnam
process->cgroup = strdup(""); process->cgroup = strdup("");
return; return;
} }
char output[256];
output[0] = '\0';
char* at = output;
int left = 255;
while (!feof(file) && left > 0) {
char buffer[256]; char buffer[256];
char *ok = fgets(buffer, 255, file); char *ok = fgets(buffer, 255, file);
if (ok) { if (!ok) break;
char* trimmed = String_trim(buffer); char* group = strchr(buffer, ':');
int nFields; if (!group) break;
char** fields = String_split(trimmed, ':', &nFields); if (at != output) {
free(trimmed); *at = ';';
free(process->cgroup); at++;
if (nFields >= 3) { left--;
process->cgroup = strndup(fields[2] + 1, 10);
} else {
process->cgroup = strdup("");
} }
String_freeArray(fields); int wrote = snprintf(at, left, "%s", group);
left -= wrote;
} }
fclose(file); fclose(file);
free(process->cgroup);
process->cgroup = strdup(output);
} }
#endif #endif