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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
/* $OpenBSD: pkill.c,v 1.34 2013/11/12 13:54:51 deraadt Exp $ */
/* $NetBSD: pkill.c,v 1.5 2002/10/27 11:49:34 kleink Exp $ */
/*-
* Copyright (c) 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <regex.h>
#include <ctype.h>
#include <kvm.h>
#include <err.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#define STATUS_MATCH 0
#define STATUS_NOMATCH 1
#define STATUS_BADUSAGE 2
#define STATUS_ERROR 3
enum listtype {
LT_GENERIC,
LT_USER,
LT_GROUP,
LT_TTY,
LT_PGRP,
LT_SID,
LT_RTABLE
};
struct list {
SLIST_ENTRY(list) li_chain;
long li_number;
};
SLIST_HEAD(listhead, list);
struct kinfo_proc *plist;
char *selected;
char *delim = "\n";
int nproc;
int pgrep;
int signum = SIGTERM;
int newest;
int oldest;
int quiet;
int inverse;
int longfmt;
int matchargs;
int fullmatch;
int confirmkill;
kvm_t *kd;
pid_t mypid;
struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
struct listhead rtablist = SLIST_HEAD_INITIALIZER(list);
int main(int, char **);
void usage(void);
int killact(struct kinfo_proc *, int);
int grepact(struct kinfo_proc *, int);
void makelist(struct listhead *, enum listtype, char *);
char *getargv(struct kinfo_proc *);
int askyn(struct kinfo_proc *);
extern char *__progname;
char *
getargv(struct kinfo_proc *kp)
{
static char buf[_POSIX2_LINE_MAX];
char **pargv;
size_t j;
if ((pargv = kvm_getargv(kd, kp, 0)) == NULL) {
strlcpy(buf, kp->p_comm, sizeof(buf));
return buf;
}
j = 0;
while (j < sizeof(buf) && *pargv != NULL) {
int ret;
ret = snprintf(buf + j, sizeof(buf) - j,
pargv[1] != NULL ? "%s " : "%s", pargv[0]);
if (ret >= sizeof(buf) - j)
j += sizeof(buf) - j - 1;
else if (ret > 0)
j += ret;
pargv++;
}
return buf;
}
int
main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
char buf[_POSIX2_LINE_MAX], *mstr, *p, *q;
int i, j, ch, bestidx, rv, criteria;
int (*action)(struct kinfo_proc *, int);
struct kinfo_proc *kp;
struct list *li;
u_int32_t bestsec, bestusec;
regex_t reg;
regmatch_t regmatch;
if (strcmp(__progname, "pgrep") == 0) {
action = grepact;
pgrep = 1;
} else {
action = killact;
p = argv[1];
if (argc > 1 && p[0] == '-') {
p++;
i = (int)strtol(p, &q, 10);
if (*q == '\0') {
signum = i;
argv++;
argc--;
} else {
if (strncasecmp(p, "sig", 3) == 0)
p += 3;
for (i = 1; i < NSIG; i++)
if (strcasecmp(sys_signame[i], p) == 0)
break;
if (i != NSIG) {
signum = i;
argv++;
argc--;
}
}
}
}
criteria = 0;
while ((ch = getopt(argc, argv, "G:P:T:U:d:fg:Ilnoqs:t:u:vx")) != -1)
switch (ch) {
case 'G':
makelist(&rgidlist, LT_GROUP, optarg);
criteria = 1;
break;
case 'P':
makelist(&ppidlist, LT_GENERIC, optarg);
criteria = 1;
break;
case 'T':
makelist(&rtablist, LT_RTABLE, optarg);
criteria = 1;
break;
case 'U':
makelist(&ruidlist, LT_USER, optarg);
criteria = 1;
break;
case 'd':
if (!pgrep)
usage();
delim = optarg;
break;
case 'f':
matchargs = 1;
break;
case 'g':
makelist(&pgrplist, LT_PGRP, optarg);
criteria = 1;
break;
case 'I':
confirmkill = 1;
break;
case 'l':
longfmt = 1;
break;
case 'n':
newest = 1;
criteria = 1;
break;
case 'o':
oldest = 1;
criteria = 1;
break;
case 'q':
quiet = 1;
break;
case 's':
makelist(&sidlist, LT_SID, optarg);
criteria = 1;
break;
case 't':
makelist(&tdevlist, LT_TTY, optarg);
criteria = 1;
break;
case 'u':
makelist(&euidlist, LT_USER, optarg);
criteria = 1;
break;
case 'v':
inverse = 1;
break;
case 'x':
fullmatch = 1;
break;
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (argc != 0)
criteria = 1;
if (!criteria || (newest && oldest))
usage();
mypid = getpid();
/*
* Retrieve the list of running processes from the kernel.
*/
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
if (kd == NULL)
errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
if (plist == NULL)
errx(STATUS_ERROR, "kvm_getprocs() failed");
/*
* Allocate memory which will be used to keep track of the
* selection.
*/
if ((selected = malloc(nproc)) == NULL)
errx(STATUS_ERROR, "memory allocation failure");
memset(selected, 0, nproc);
/*
* Refine the selection.
*/
for (; *argv != NULL; argv++) {
if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
regerror(rv, ®, buf, sizeof(buf));
errx(STATUS_BADUSAGE, "bad expression: %s", buf);
}
for (i = 0, kp = plist; i < nproc; i++, kp++) {
if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
kp->p_pid == mypid)
continue;
if (matchargs)
mstr = getargv(kp);
else
mstr = kp->p_comm;
rv = regexec(®, mstr, 1, ®match, 0);
if (rv == 0) {
if (fullmatch) {
if (regmatch.rm_so == 0 &&
regmatch.rm_eo == strlen(mstr))
selected[i] = 1;
} else
selected[i] = 1;
} else if (rv != REG_NOMATCH) {
regerror(rv, ®, buf, sizeof(buf));
errx(STATUS_ERROR, "regexec(): %s", buf);
}
}
regfree(®);
}
for (i = 0, kp = plist; i < nproc; i++, kp++) {
if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
kp->p_pid == mypid)
continue;
SLIST_FOREACH(li, &ruidlist, li_chain)
if (kp->p_ruid == (uid_t)li->li_number)
break;
if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &rgidlist, li_chain)
if (kp->p_rgid == (gid_t)li->li_number)
break;
if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &euidlist, li_chain)
if (kp->p_uid == (uid_t)li->li_number)
break;
if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &ppidlist, li_chain)
if (kp->p_ppid == (uid_t)li->li_number)
break;
if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &pgrplist, li_chain)
if (kp->p__pgid == (uid_t)li->li_number)
break;
if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &tdevlist, li_chain) {
if (li->li_number == -1 &&
(kp->p_psflags & PS_CONTROLT) == 0)
break;
if (kp->p_tdev == (uid_t)li->li_number)
break;
}
if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &sidlist, li_chain)
if (kp->p_sid == (uid_t)li->li_number)
break;
if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
SLIST_FOREACH(li, &rtablist, li_chain)
if (kp->p_rtableid == (u_int32_t)li->li_number)
break;
if (SLIST_FIRST(&rtablist) != NULL && li == NULL) {
selected[i] = 0;
continue;
}
if (argc == 0)
selected[i] = 1;
}
if (newest || oldest) {
bestidx = -1;
if (newest)
bestsec = bestusec = 0;
else
bestsec = bestusec = UINT32_MAX;
for (i = 0, kp = plist; i < nproc; i++, kp++) {
if (!selected[i])
continue;
if ((newest && (kp->p_ustart_sec > bestsec ||
(kp->p_ustart_sec == bestsec
&& kp->p_ustart_usec > bestusec)))
|| (oldest && (kp->p_ustart_sec < bestsec ||
(kp->p_ustart_sec == bestsec
&& kp->p_ustart_usec < bestusec)))) {
bestsec = kp->p_ustart_sec;
bestusec = kp->p_ustart_usec;
bestidx = i;
}
}
memset(selected, 0, nproc);
if (bestidx != -1)
selected[bestidx] = 1;
}
/*
* Take the appropriate action for each matched process, if any.
*/
rv = STATUS_NOMATCH;
for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
if ((kp->p_flag & (P_SYSTEM | P_THREAD)) != 0 ||
kp->p_pid == mypid)
continue;
if (selected[i] == inverse)
continue;
switch ((*action)(kp, j++)) {
case STATUS_MATCH:
if (rv != STATUS_ERROR)
rv = STATUS_MATCH;
break;
case STATUS_NOMATCH:
j--;
break;
case STATUS_ERROR:
rv = STATUS_ERROR;
break;
}
}
if (pgrep && j && !quiet)
putchar('\n');
exit(rv);
}
void
usage(void)
{
const char *ustr;
if (pgrep)
ustr = "[-flnoqvx] [-d delim]";
else
ustr = "[-signal] [-fIlnoqvx]";
fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid]"
"\n\t[-T rtable] [-t tty] [-U uid] [-u euid] [pattern ...]\n",
__progname, ustr);
exit(STATUS_BADUSAGE);
}
int
askyn(struct kinfo_proc *kp)
{
int first, ch;
printf("kill %d %.60s? ", (int)kp->p_pid, getargv(kp));
fflush(stdout);
first = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
return (first == 'y' || first == 'Y');
}
int
killact(struct kinfo_proc *kp, int dummy)
{
int doit;
if (confirmkill) {
doit = askyn(kp);
} else {
if (longfmt && !quiet)
printf("%d %s\n", (int)kp->p_pid, kp->p_comm);
doit = 1;
}
if (doit && kill(kp->p_pid, signum) == -1) {
if (errno == ESRCH)
return (STATUS_NOMATCH);
warn("signalling pid %d", (int)kp->p_pid);
return (STATUS_ERROR);
}
return (STATUS_MATCH);
}
int
grepact(struct kinfo_proc *kp, int printdelim)
{
char **argv;
if (quiet)
return (STATUS_MATCH);
if (longfmt && matchargs)
if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
return (errno == ESRCH ? STATUS_NOMATCH : STATUS_ERROR);
if (printdelim)
fputs(delim, stdout);
if (longfmt && matchargs) {
printf("%d ", (int)kp->p_pid);
for (; *argv != NULL; argv++) {
printf("%s", *argv);
if (argv[1] != NULL)
putchar(' ');
}
} else if (longfmt)
printf("%d %s", (int)kp->p_pid, kp->p_comm);
else
printf("%d", (int)kp->p_pid);
return (STATUS_MATCH);
}
void
makelist(struct listhead *head, enum listtype type, char *src)
{
struct list *li;
struct passwd *pw;
struct group *gr;
struct stat st;
char *sp, *p, buf[MAXPATHLEN];
int empty;
empty = 1;
while ((sp = strsep(&src, ",")) != NULL) {
if (*sp == '\0')
usage();
if ((li = malloc(sizeof(*li))) == NULL)
errx(STATUS_ERROR, "memory allocation failure");
SLIST_INSERT_HEAD(head, li, li_chain);
empty = 0;
li->li_number = strtol(sp, &p, 0);
if (*p == '\0') {
switch (type) {
case LT_PGRP:
if (li->li_number == 0)
li->li_number = getpgrp();
break;
case LT_SID:
if (li->li_number == 0)
li->li_number = getsid(mypid);
break;
case LT_RTABLE:
if (li->li_number < 0 ||
li->li_number > RT_TABLEID_MAX)
errx(STATUS_BADUSAGE,
"rtable out of range");
break;
case LT_TTY:
usage();
default:
break;
}
continue;
}
switch (type) {
case LT_USER:
if ((pw = getpwnam(sp)) == NULL)
errx(STATUS_BADUSAGE, "unknown user `%s'", sp);
li->li_number = pw->pw_uid;
break;
case LT_GROUP:
if ((gr = getgrnam(sp)) == NULL)
errx(STATUS_BADUSAGE, "unknown group `%s'", sp);
li->li_number = gr->gr_gid;
break;
case LT_TTY:
if (strcmp(sp, "-") == 0) {
li->li_number = -1;
break;
} else if (strcmp(sp, "co") == 0)
p = "console";
else if (strncmp(sp, "tty", 3) == 0)
p = sp;
else
p = NULL;
if (p == NULL)
snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
else
snprintf(buf, sizeof(buf), "/dev/%s", p);
if (stat(buf, &st) < 0) {
if (errno == ENOENT)
errx(STATUS_BADUSAGE,
"no such tty: `%s'", sp);
err(STATUS_ERROR, "stat(%s)", sp);
}
if (!S_ISCHR(st.st_mode))
errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
li->li_number = st.st_rdev;
break;
default:
usage();
}
}
if (empty)
usage();
}
|