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
|
? .pidof.c.swp
? pidof
? pidof.core
Index: pidof.c
===================================================================
RCS file: /cvsroot/bmp-plugins/pidof/pidof.c,v
retrieving revision 1.4
diff -u -r1.4 pidof.c
--- pidof.c 1 May 2005 16:26:19 -0000 1.4
+++ pidof.c 13 Sep 2005 16:11:08 -0000
@@ -26,6 +26,7 @@
* $Id$
*/
+#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
@@ -37,56 +38,91 @@
#include <fcntl.h>
#include <stdlib.h>
#include <sysexits.h>
+#include <libgen.h>
-static int get_pid_of_process(char *process_name);
+static int get_pid_of_process(const char *process_name);
static void usage(void);
+static int match_argv;
+static pid_t own_pid;
+
static int
-get_pid_of_process(char *process_name)
+get_pid_of_process(const char *process_name)
{
static kvm_t *kd = NULL;
struct kinfo_proc *p;
- int i, n_processes, processes_found;
-
- processes_found = 0;
+ int i, n_processes,
+ processes_found = 0;
if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL)
(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 (strncmp(process_name, p[i].ki_comm, COMMLEN+1) == 0) {
- (void)printf("%d ", (int)p[i].ki_pid);
- processes_found++;
- }
+ 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) {
+ (void)printf("%d ", (int)p[i].ki_pid);
+ processes_found++;
+ } else if (match_argv == 1) {
+ char **p_argv = NULL;
+
+ if ((p_argv = kvm_getargv(kd, p+i, 0)) != NULL)
+ for (; *p_argv != NULL; p_argv++)
+ if (strcmp(process_name, basename(*p_argv)) == 0) {
+ (void)printf("%d ", (int)p[i].ki_pid);
+ processes_found++;
+ break;
+ }
+ }
+ }
kvm_close(kd);
}
-
- return processes_found;
+
+ return (processes_found);
}
static void
usage()
{
- (void)fprintf(stderr, "usage: pidof name1 name2 ...\n");
+ (void)fprintf(stderr, "usage: pidof [-x] name1 name2 ...\n");
exit(EX_USAGE);
}
int
main(int argc, char **argv)
{
- int i, procs_found;
+ int i, procs_found,
+ ch;
procs_found = 0;
+ match_argv = 0;
- if (argc <= 1)
- usage();
+ own_pid = getpid();
+
+ while ((ch = getopt(argc, argv, "x")) != -1) {
+ switch (ch) {
+ case 'x':
+ match_argv = 1;
+ break;
+ case '?':
+ default:
+ usage();
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
- for (i = 1; i<argc; procs_found += get_pid_of_process(argv[i++]));
+ if (argc < 1)
+ usage();
+
+ for (i = 0; i<argc; i++) {
+ procs_found += get_pid_of_process(argv[i]);
+ }
(void)printf("\n");
- return (procs_found > 0) ? 0 : 1;
+ return ((procs_found > 0) ? 0 : 1);
}
|