1 --- lib/utils.c.orig 2015-09-21 03:42:11 UTC
7 +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
8 +#include <netinet/in.h>
12 +#include <sys/param.h>
13 +#include <sys/sysctl.h>
14 +#include <sys/user.h>
17 extern int inet_pton(int af, const char *src, void *dst);
20 @@ -2112,14 +2122,19 @@ wchar_from_utf8 (const char *utf8)
22 #endif /* ifdef WIN32 */
25 /* read the link of /proc/123/exe and compare with `process_name' */
27 find_process_in_dirent(struct dirent *dir, const char *process_name)
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) {
41 @@ -2143,7 +2158,8 @@ find_process_in_dirent(struct dirent *di
44 /* read the /proc fs to determine whether some process is running */
45 -gboolean process_is_running (const char *process_name)
47 +process_is_running_procfs (const char *process_name)
49 DIR *proc_dir = opendir("/proc");
51 @@ -2168,7 +2184,8 @@ gboolean process_is_running (const char
55 -int count_process(const char *process_name)
57 +count_process_procfs(const char *process_name)
60 DIR *proc_dir = opendir("/proc");
61 @@ -2192,6 +2209,14 @@ int count_process(const char *process_na
66 +gboolean process_is_running(const char *process_name) {
67 + return process_is_running_procfs(process_name);
70 +int count_process(const char *process_name) {
71 + return count_process_procfs(process_name);
76 @@ -2202,6 +2227,119 @@ gboolean process_is_running (const char
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)
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)
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)
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)
115 +#define KVM_OPENFILES(exec, coref, buf) 0
116 +#define KVM_GETPROCS(kd, plist, nproc) 0
119 +#ifndef WITH_PROC_FS
120 +#define WITH_PROC_FS g_file_test("/proc/curproc", G_FILE_TEST_EXISTS)
124 +count_running_process_kvm(const char *process_name) {
128 + static struct kinfo_proc *plist;
130 + static pid_t mypid;
131 + static int kthreads;
133 + char buf[_POSIX2_LINE_MAX];
134 + const char * execf, *coref;
136 + int i, selected_nproc;
137 + struct kinfo_proc *kp;
139 + selected_nproc = 0;
141 + coref = _PATH_DEVNULL;
144 + kd = KVM_OPENFILES(execf, coref, buf);
146 + fprintf(stderr, "Error: Cannot open kernel files (%s)", buf);
150 + plist = KVM_GETPROCS(kd, plist, nproc);
151 + if (plist == NULL) {
152 + fprintf(stderr, "Error: Cannot get process list (%s)", kvm_geterr(kd));
156 + for(i = 0, kp = plist; i < nproc; i++, kp++) {
160 + if ((pargv = kvm_getargv(kd, kp, 0)) != NULL) {
161 + if (strstr(pargv[0], process_name) != NULL) {
162 + selected_nproc += 1;
169 + return selected_nproc;
173 +process_is_running(const char * process_name) {
174 + if (WITH_PROC_FS) {
175 + return process_is_running_procfs(process_name);
177 + if (count_running_process_kvm(process_name) > 0) {
185 +count_process(const char * process_name) {
186 + if (WITH_PROC_FS) {
187 + return count_process_procfs(process_name);
189 + return count_running_process_kvm(process_name);
194 ccnet_object_type_from_id (const char *object_id)