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