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
|
--- a/pidof.c 2006-09-21 15:14:03 +0100
+++ b/pidof.c 2006-09-21 15:13:47 +0100
@@ -53,14 +53,22 @@
struct kinfo_proc *p;
int i, n_processes,
processes_found = 0;
+ char *pname = NULL;
+
+ if (process_name == NULL)
+ return 0;
+
+ if ((pname = strdup(basename(process_name))) == NULL)
+ return 0;
- if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL)
- (void)errx(1, "%s", kvm_geterr(kd));
- else {
+ if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, NULL)) == NULL) {
+ free(pname);
+ (void)errx(1, "%s", kvm_geterr(kd));
+ } else {
p = kvm_getprocs(kd, KERN_PROC_PROC, 0, &n_processes);
for (i = 0; i<n_processes; i++) {
if (p[i].ki_pid != own_pid)
- if (strncmp(process_name, p[i].ki_comm, COMMLEN+1) == 0) {
+ if (strncmp(pname, p[i].ki_comm, COMMLEN+1) == 0) {
(void)printf("%d ", (int)p[i].ki_pid);
processes_found++;
} else if (match_argv == 1) {
@@ -68,7 +76,7 @@
if ((p_argv = kvm_getargv(kd, p+i, 0)) != NULL)
if ( *p_argv != NULL )
- if (strcmp(process_name, basename(*p_argv)) == 0) {
+ if (strcmp(pname, basename(*p_argv)) == 0) {
(void)printf("%d ", (int)p[i].ki_pid);
processes_found++;
break;
@@ -77,6 +85,7 @@
}
kvm_close(kd);
+ free(pname);
}
return (processes_found);
|