Merge pull request #334 from mmcco/cpu

Plug mem leak, improve CPU enumeration logic
This commit is contained in:
Hisham Muhammad 2016-01-04 18:02:36 -02:00
commit 77cffaacf6
1 changed files with 8 additions and 4 deletions

View File

@ -48,14 +48,16 @@ static long fscale;
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
int mib[] = { CTL_HW, HW_NCPU }; int mib[] = { CTL_HW, HW_NCPU };
int fmib[] = { CTL_KERN, KERN_FSCALE }; int fmib[] = { CTL_KERN, KERN_FSCALE };
int i; int i, e;
OpenBSDProcessList* fpl = calloc(1, sizeof(OpenBSDProcessList)); OpenBSDProcessList* fpl = calloc(1, sizeof(OpenBSDProcessList));
ProcessList* pl = (ProcessList*) fpl; ProcessList* pl = (ProcessList*) fpl;
size_t size = sizeof(pl->cpuCount); size_t size = sizeof(pl->cpuCount);
ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(OpenBSDProcess), usersTable, pidWhiteList, userId);
pl->cpuCount = 1; // default to 1 on sysctl() error e = sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0);
(void)sysctl(mib, 2, &pl->cpuCount, &size, NULL, 0); if (e == -1 || pl->cpuCount < 1) {
pl->cpuCount = 1;
}
fpl->cpus = realloc(fpl->cpus, pl->cpuCount * sizeof(CPUData)); fpl->cpus = realloc(fpl->cpus, pl->cpuCount * sizeof(CPUData));
size = sizeof(fscale); size = sizeof(fscale);
@ -80,6 +82,8 @@ void ProcessList_delete(ProcessList* this) {
const OpenBSDProcessList* fpl = (OpenBSDProcessList*) this; const OpenBSDProcessList* fpl = (OpenBSDProcessList*) this;
if (fpl->kd) kvm_close(fpl->kd); if (fpl->kd) kvm_close(fpl->kd);
free(fpl->cpus);
ProcessList_done(this); ProcessList_done(this);
free(this); free(this);
} }