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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
From 7d8be1c1ffc31523e2de09d4829169b78dd391e4 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@openbsd.org>
Date: Fri, 17 May 2019 11:55:16 +0000
Subject: [PATCH] pid_get_parent: fix potential leak of kp
From 0334ea1c889331adb02d361670e24b3f85b5b0d6 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
Date: Mon, 13 May 2019 20:47:46 +0200
Subject: [PATCH] pid_get_parent: fix for OpenBSD
From ad9da2727d3e3243fd052c9feb0c55645e87d384 Mon Sep 17 00:00:00 2001
From: Jonathan Matthew <jmatthew@openbsd.org>
Date: Fri, 8 Jul 2011 10:24:32 +1000
Subject: GMountOperation::show-processes support for OpenBSD using kvm(3)
From 3d165c1a902fc0a5f32898e406a79e53ed4bcc97 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@openbsd.org>
Date: Tue, 20 Sep 2011 11:57:49 +0200
Subject: gtkmountoperation-x11: unbreak compilation on OpenBSD.
From d987a01d80126ee351727d1603a599c4edb66eca Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@openbsd.org>
Date: Sat, 15 Oct 2011 11:27:47 +0200
Subject: GMountOperation on OpenBSD: remove the need for kvm(3)
From 974212ec825d8d50bdea8ab955b6f19b8ed1672f Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
Date: Mon, 9 Jul 2012 18:20:34 +0200
Subject: OpenBSD: use G_N_ELEMENTS instead of nitems
From 8935d2f123878c31af69b6c6034069ad540b13c7 Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@gnome.org>
Date: Fri, 25 Apr 2014 14:37:59 +0200
Subject: openbsd: properly set len for gtkmountoperation-x11
Index: gtk/gtkmountoperation-x11.c
--- gtk/gtkmountoperation-x11.c.orig
+++ gtk/gtkmountoperation-x11.c
@@ -43,9 +43,6 @@
#include <errno.h>
#if defined(__OpenBSD__)
-#include <sys/param.h>
-#include <kvm.h>
-#include <fcntl.h>
#include <sys/sysctl.h>
#endif
@@ -724,6 +721,110 @@ pid_get_command_line (GPid pid)
}
/* ---------------------------------------------------------------------------------------------------- */
+
+#elif defined(__OpenBSD__)
+
+/* ---------------------------------------------------------------------------------------------------- */
+
+static GPid
+pid_get_parent (GPid pid)
+{
+ struct kinfo_proc *kp = NULL;
+ size_t len;
+ GPid ppid = 0;
+
+ /* fail if trying to get the parent of the init process (no such thing) */
+ if (pid == 1)
+ goto out;
+
+ int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
+ sizeof(struct kinfo_proc), 0 };
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1)
+ goto out;
+
+ mib[5] = (len / sizeof(struct kinfo_proc));
+
+ kp = g_malloc0 (len);
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), kp, &len, NULL, 0) < 0)
+ goto out;
+
+ ppid = kp->p_ppid;
+
+out:
+ if (kp)
+ g_free (kp);
+ return ppid;
+}
+
+static gchar *
+pid_get_env (GPid pid, const gchar *key)
+{
+ size_t len;
+ char **strs;
+ char *ret = NULL;
+ char *end;
+ int key_len;
+ int i;
+
+ int mib[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ENV };
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1)
+ return ret;
+
+ strs = g_malloc0 (len);
+
+ key_len = strlen (key);
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), strs, &len, NULL, 0) != -1)
+ {
+ for (i = 0; strs[i] != NULL; i++)
+ {
+ if (g_str_has_prefix (strs[i], key) && (*(strs[i] + key_len) == '='))
+ {
+ ret = g_strdup (strs[i] + key_len + 1);
+
+ /* skip invalid UTF-8 */
+ if (!g_utf8_validate (ret, -1, (const gchar **) &end))
+ *end = '\0';
+ break;
+ }
+ }
+ }
+
+ g_free (strs);
+ return ret;
+}
+
+static gchar *
+pid_get_command_line (GPid pid)
+{
+ size_t len;
+ char **strs;
+ char *ret, *end;
+
+ int mib[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV };
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), NULL, &len, NULL, 0) == -1)
+ return NULL;
+
+ strs = g_malloc0 (len);
+
+ if (sysctl (mib, G_N_ELEMENTS (mib), strs, &len, NULL, 0) == -1) {
+ g_free (strs);
+ return NULL;
+ }
+
+ ret = g_strjoinv (" ", strs);
+ /* skip invalid UTF-8 */
+ if (!g_utf8_validate (ret, -1, (const gchar **) &end))
+ *end = '\0';
+
+ g_free (strs);
+ return ret;
+}
+
#else
/* TODO: please implement for your OS - must return valid UTF-8 */
|