summaryrefslogtreecommitdiff
path: root/www/chromium/patches/patch-base_process_process_handle_openbsd_cc
blob: c9d909341d992883ddf8d172b13cbf522c15f5dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Index: base/process/process_handle_openbsd.cc
--- base/process/process_handle_openbsd.cc.orig
+++ base/process/process_handle_openbsd.cc
@@ -3,8 +3,11 @@
 // found in the LICENSE file.
 
 #include "base/process/process_handle.h"
+#include "base/files/file_util.h"
 
 #include <stddef.h>
+#include <sys/param.h>
+#include <sys/proc.h>
 #include <sys/sysctl.h>
 #include <sys/types.h>
 #include <unistd.h>
@@ -14,39 +17,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, base::size(mib), NULL, &length, NULL, 0) < 0)
     return -1;
 
+  info = (struct kinfo_proc *)malloc(length);
+
   mib[5] = (length / sizeof(struct kinfo_proc));
 
-  if (sysctl(mib, base::size(mib), &info, &length, NULL, 0) < 0)
-    return -1;
+  if (sysctl(mib, base::size(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, base::size(mib), NULL, &len, NULL, 0) == -1)
+  if (sysctl(mib, base::size(mib), NULL, &length, NULL, 0) == -1)
     return FilePath();
-  mib[5] = (len / sizeof(struct kinfo_proc));
-  if (sysctl(mib, base::size(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, base::size(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);
 }
 
 }  // namespace base