$OpenBSD: patch-base_process_util_openbsd_cc,v 1.10 2012/12/18 21:38:12 sthen Exp $ --- base/process_util_openbsd.cc.orig Tue Nov 13 19:02:10 2012 +++ base/process_util_openbsd.cc Fri Dec 7 06:24:10 2012 @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -30,39 +31,59 @@ namespace base { ProcessId GetParentProcessId(ProcessHandle process) { - struct kinfo_proc info; + struct kinfo_proc *info; size_t length; + pid_t ppid; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process, sizeof(struct kinfo_proc), 0 }; if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) return -1; + info = (struct kinfo_proc *)malloc(length); + mib[5] = (length / sizeof(struct kinfo_proc)); - if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) - return -1; + if (sysctl(mib, arraysize(mib), info, &length, NULL, 0) < 0) { + ppid = -1; + goto out; + } - return info.p_ppid; + ppid = info->p_ppid; + +out: + free(info); + return ppid; } FilePath GetProcessExecutablePath(ProcessHandle process) { - struct kinfo_proc kp; - size_t len; + struct kinfo_proc *info; + size_t length; + char *path = NULL; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process, sizeof(struct kinfo_proc), 0 }; - if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) == -1) + if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) == -1) return FilePath(); - mib[5] = (len / sizeof(struct kinfo_proc)); - if (sysctl(mib, arraysize(mib), &kp, &len, NULL, 0) < 0) - return FilePath(); - if ((kp.p_flag & P_SYSTEM) != 0) - return FilePath(); - if (strcmp(kp.p_comm, "chrome") == 0) - return FilePath(kp.p_comm); - return FilePath(); + info = (struct kinfo_proc *)malloc(length); + + mib[5] = (length / sizeof(struct kinfo_proc)); + + if (sysctl(mib, arraysize(mib), info, &length, NULL, 0) < 0) + goto out; + + if ((info->p_flag & P_SYSTEM) != 0) + goto out; + + if (strcmp(info->p_comm, "chrome") == 0) { + path = info->p_comm; + goto out; + } + +out: + free(info); + return FilePath(path); } ProcessIterator::ProcessIterator(const ProcessFilter* filter) @@ -194,20 +215,28 @@ ProcessMetrics* ProcessMetrics::CreateProcessMetrics(P } size_t ProcessMetrics::GetPagefileUsage() const { - struct kinfo_proc info; - size_t length; + struct kinfo_proc *info; + size_t length, pfu; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_, sizeof(struct kinfo_proc), 0 }; if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) return -1; + info = (struct kinfo_proc *)malloc(length); + mib[5] = (length / sizeof(struct kinfo_proc)); - if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) - return -1; + if (sysctl(mib, arraysize(mib), info, &length, NULL, 0) < 0) { + pfu = -1; + goto out; + } - return (info.p_vm_tsize + info.p_vm_dsize + info.p_vm_ssize); + pfu = (info->p_vm_tsize + info->p_vm_dsize + info->p_vm_ssize); + +out: + free(info); + return pfu; } size_t ProcessMetrics::GetPeakPagefileUsage() const { @@ -216,20 +245,28 @@ size_t ProcessMetrics::GetPeakPagefileUsage() const { } size_t ProcessMetrics::GetWorkingSetSize() const { - struct kinfo_proc info; - size_t length; + struct kinfo_proc *info; + size_t length, wss; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, process_, sizeof(struct kinfo_proc), 0 }; if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) return -1; + info = (struct kinfo_proc *)malloc(length); + mib[5] = (length / sizeof(struct kinfo_proc)); - if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) - return -1; + if (sysctl(mib, arraysize(mib), info, &length, NULL, 0) < 0) { + wss = -1; + goto out; + } - return info.p_vm_rssize * getpagesize(); + wss = (info->p_vm_rssize * getpagesize()); + +out: + free(info); + return wss; } size_t ProcessMetrics::GetPeakWorkingSetSize() const { @@ -270,20 +307,27 @@ bool ProcessMetrics::GetIOCounters(IoCounters* io_coun } static int GetProcessCPU(pid_t pid) { - struct kinfo_proc info; + struct kinfo_proc *info; size_t length; + int pctcpu = 0; int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid, sizeof(struct kinfo_proc), 0 }; if (sysctl(mib, arraysize(mib), NULL, &length, NULL, 0) < 0) return -1; + info = (struct kinfo_proc *)malloc(length); + mib[5] = (length / sizeof(struct kinfo_proc)); - if (sysctl(mib, arraysize(mib), &info, &length, NULL, 0) < 0) - return 0; + if (sysctl(mib, arraysize(mib), info, &length, NULL, 0) < 0) + goto out; - return info.p_pctcpu; + pctcpu = info->p_pctcpu; + +out: + free(info); + return pctcpu; } double ProcessMetrics::GetCPUUsage() {