Lines 1-195
Link Here
|
1 |
--- lib/utils.c.orig 2015-09-21 03:42:11 UTC |
|
|
2 |
+++ lib/utils.c |
3 |
@@ -56,6 +56,16 @@ |
4 |
|
5 |
#include <zlib.h> |
6 |
|
7 |
+#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) |
8 |
+#include <netinet/in.h> |
9 |
+#include <stdlib.h> |
10 |
+#include <kvm.h> |
11 |
+#include <paths.h> |
12 |
+#include <sys/param.h> |
13 |
+#include <sys/sysctl.h> |
14 |
+#include <sys/user.h> |
15 |
+#endif |
16 |
+ |
17 |
extern int inet_pton(int af, const char *src, void *dst); |
18 |
|
19 |
|
20 |
@@ -2112,14 +2122,19 @@ wchar_from_utf8 (const char *utf8) |
21 |
|
22 |
#endif /* ifdef WIN32 */ |
23 |
|
24 |
-#ifdef __linux__ |
25 |
/* read the link of /proc/123/exe and compare with `process_name' */ |
26 |
static int |
27 |
find_process_in_dirent(struct dirent *dir, const char *process_name) |
28 |
{ |
29 |
char path[512]; |
30 |
/* fisrst construct a path like /proc/123/exe */ |
31 |
+#if defined(__linux__) |
32 |
if (sprintf (path, "/proc/%s/exe", dir->d_name) < 0) { |
33 |
+#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) |
34 |
+ if (sprintf (path, "/proc/%s/file", dir->d_name) < 0) { |
35 |
+#else |
36 |
+ if (1) { |
37 |
+#endif |
38 |
return -1; |
39 |
} |
40 |
|
41 |
@@ -2143,7 +2158,8 @@ find_process_in_dirent(struct dirent *di |
42 |
} |
43 |
|
44 |
/* read the /proc fs to determine whether some process is running */ |
45 |
-gboolean process_is_running (const char *process_name) |
46 |
+static gboolean |
47 |
+process_is_running_procfs (const char *process_name) |
48 |
{ |
49 |
DIR *proc_dir = opendir("/proc"); |
50 |
if (!proc_dir) { |
51 |
@@ -2168,7 +2184,8 @@ gboolean process_is_running (const char |
52 |
return FALSE; |
53 |
} |
54 |
|
55 |
-int count_process(const char *process_name) |
56 |
+static int |
57 |
+count_process_procfs(const char *process_name) |
58 |
{ |
59 |
int count = 0; |
60 |
DIR *proc_dir = opendir("/proc"); |
61 |
@@ -2192,6 +2209,14 @@ int count_process(const char *process_na |
62 |
return count; |
63 |
} |
64 |
|
65 |
+#ifdef __linux__ |
66 |
+gboolean process_is_running(const char *process_name) { |
67 |
+ return process_is_running_procfs(process_name); |
68 |
+} |
69 |
+ |
70 |
+int count_process(const char *process_name) { |
71 |
+ return count_process_procfs(process_name); |
72 |
+} |
73 |
#endif |
74 |
|
75 |
#ifdef __APPLE__ |
76 |
@@ -2202,6 +2227,119 @@ gboolean process_is_running (const char |
77 |
} |
78 |
#endif |
79 |
|
80 |
+#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) |
81 |
+#if defined(__FreeBSD__) |
82 |
+#define PSKIP(kp) ((kp)->ki_pid == mypid || \ |
83 |
+ (!kthreads && ((kp)->ki_flag & P_KTHREAD) != 0)) |
84 |
+#define KVM_OPENFILES(exec, coref, buf) \ |
85 |
+ kvm_openfiles(exec, coref, NULL, O_RDONLY, buf) |
86 |
+#define KVM_GETPROCS(kd, plist, nproc) \ |
87 |
+ kvm_getprocs(kd, KERN_PROC_PROC, 0, &nproc) |
88 |
+ |
89 |
+#elif defined(__DragonFly__) |
90 |
+#define PSKIP(kp) ((kp)->kp_pid == mypid || \ |
91 |
+ (!kthreads && ((kp)->kp_flags & P_SYSTEM) != 0)) |
92 |
+#define KVM_OPENFILES(exec, coref, buf) \ |
93 |
+ kvm_openfiles(exec, coref, NULL, O_RDONLY, buf) |
94 |
+#define KVM_GETPROCS(kd, plist, nproc) \ |
95 |
+ kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc) |
96 |
+ |
97 |
+#elif defined(__NetBSD__) |
98 |
+#define PSKIP(kp) ((kp)->kp_pid == mypid || \ |
99 |
+ ((kp)->p_flag & P_SYSTEM) != 0) |
100 |
+#define KVM_OPENFILES(exec, coref, buf) \ |
101 |
+ kvm_openfiles(exec, coref, NULL, KVM_NO_FILES, buf) |
102 |
+#define KVM_GETPROCS(kd, plist, nproc) \ |
103 |
+ kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc) |
104 |
+ |
105 |
+#elif defined(__OpenBSD__) |
106 |
+#define PSKIP(kp) ((kp)->kp_pid == mypid || \ |
107 |
+ ((kp)->p_flag & (P_SYSTEM | P_THREAD)) != 0) |
108 |
+#define KVM_OPENFILES(exec, coref, buf) \ |
109 |
+ kvm_openfiles(exec, coref, NULL, KVM_NO_FILES, buf) |
110 |
+#define KVM_GETPROCS(kd, plist, nproc) \ |
111 |
+ kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc) |
112 |
+ |
113 |
+#else |
114 |
+#define PSKIP(kp) 0 |
115 |
+#define KVM_OPENFILES(exec, coref, buf) 0 |
116 |
+#define KVM_GETPROCS(kd, plist, nproc) 0 |
117 |
+#endif |
118 |
+ |
119 |
+#ifndef WITH_PROC_FS |
120 |
+#define WITH_PROC_FS g_file_test("/proc/curproc", G_FILE_TEST_EXISTS) |
121 |
+#endif |
122 |
+ |
123 |
+static int |
124 |
+count_running_process_kvm(const char *process_name) { |
125 |
+ |
126 |
+ |
127 |
+ static kvm_t *kd; |
128 |
+ static struct kinfo_proc *plist; |
129 |
+ static int nproc; |
130 |
+ static pid_t mypid; |
131 |
+ static int kthreads; |
132 |
+ |
133 |
+ char buf[_POSIX2_LINE_MAX]; |
134 |
+ const char * execf, *coref; |
135 |
+ char **pargv; |
136 |
+ int i, selected_nproc; |
137 |
+ struct kinfo_proc *kp; |
138 |
+ |
139 |
+ selected_nproc = 0; |
140 |
+ execf = NULL; |
141 |
+ coref = _PATH_DEVNULL; |
142 |
+ |
143 |
+ mypid = getpid(); |
144 |
+ kd = KVM_OPENFILES(execf, coref, buf); |
145 |
+ if (kd == NULL) { |
146 |
+ fprintf(stderr, "Error: Cannot open kernel files (%s)", buf); |
147 |
+ exit(1); |
148 |
+ } |
149 |
+ |
150 |
+ plist = KVM_GETPROCS(kd, plist, nproc); |
151 |
+ if (plist == NULL) { |
152 |
+ fprintf(stderr, "Error: Cannot get process list (%s)", kvm_geterr(kd)); |
153 |
+ exit(1); |
154 |
+ } |
155 |
+ |
156 |
+ for(i = 0, kp = plist; i < nproc; i++, kp++) { |
157 |
+ if (PSKIP(kp)) { |
158 |
+ continue; |
159 |
+ } |
160 |
+ if ((pargv = kvm_getargv(kd, kp, 0)) != NULL) { |
161 |
+ if (strstr(pargv[0], process_name) != NULL) { |
162 |
+ selected_nproc += 1; |
163 |
+ } |
164 |
+ } |
165 |
+ } |
166 |
+ kvm_close(kd); |
167 |
+ kvm_close(kd); |
168 |
+ |
169 |
+ return selected_nproc; |
170 |
+} |
171 |
+ |
172 |
+gboolean |
173 |
+process_is_running(const char * process_name) { |
174 |
+ if (WITH_PROC_FS) { |
175 |
+ return process_is_running_procfs(process_name); |
176 |
+ } |
177 |
+ if (count_running_process_kvm(process_name) > 0) { |
178 |
+ return TRUE; |
179 |
+ } else { |
180 |
+ return FALSE; |
181 |
+ } |
182 |
+} |
183 |
+ |
184 |
+int |
185 |
+count_process(const char * process_name) { |
186 |
+ if (WITH_PROC_FS) { |
187 |
+ return count_process_procfs(process_name); |
188 |
+ } |
189 |
+ return count_running_process_kvm(process_name); |
190 |
+} |
191 |
+#endif |
192 |
+ |
193 |
char* |
194 |
ccnet_object_type_from_id (const char *object_id) |
195 |
{ |