1 /* $NetBSD: pkill.c,v 1.7 2004/02/15 17:03:30 soren Exp $ */
2 /* $DragonFly: src/usr.bin/pkill/pkill.c,v 1.7 2005/02/15 20:31:29 cpressey Exp $ */
5 * Copyright (c) 2002 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
44 #include <sys/queue.h>
46 #include <sys/fcntl.h>
63 #define STATUS_MATCH 0
64 #define STATUS_NOMATCH 1
65 #define STATUS_BADUSAGE 2
66 #define STATUS_ERROR 3
78 SLIST_ENTRY(list) li_chain;
87 SLIST_HEAD(listhead, list);
89 struct kinfo_proc *plist;
91 const char *delim = "\n";
103 struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
104 struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
105 struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
106 struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
107 struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
108 struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
109 struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
112 void killact(struct kinfo_proc *, int);
113 void grepact(struct kinfo_proc *, int);
114 int parse_pid(const char *, char **, struct list *, pid_t);
115 void makelist(struct listhead *, enum listtype, char *);
118 main(int argc, char **argv)
120 char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
121 int i, ch, bestidx, rv, criteria;
123 void (*action)(struct kinfo_proc *, int);
124 struct kinfo_proc *kp;
129 const char *kvmf = _PATH_DEVNULL;
131 if (strcmp(getprogname(), "pgrep") == 0) {
138 if (argc > 1 && p[0] == '-') {
140 i = (int)strtol(p, &q, 10);
146 if (strncasecmp(p, "sig", 3) == 0)
148 for (i = 1; i < NSIG; i++) {
149 if (strcasecmp(sys_signame[i], p) == 0)
163 while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:u:vx")) != -1) {
166 makelist(&rgidlist, LT_GROUP, optarg);
170 makelist(&ppidlist, LT_PPID, optarg);
174 makelist(&ruidlist, LT_USER, optarg);
186 makelist(&pgrplist, LT_PGRP, optarg);
199 makelist(&sidlist, LT_SID, optarg);
203 makelist(&tdevlist, LT_TTY, optarg);
207 makelist(&euidlist, LT_USER, optarg);
232 * Retrieve the list of running processes from the kernel.
234 kd = kvm_openfiles(kvmf, kvmf, NULL, O_RDONLY, buf);
236 errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
238 plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
240 errx(STATUS_ERROR, "cannot list processes");
243 * Allocate memory which will be used to keep track of the
246 if ((selected = malloc(nproc)) == NULL)
247 errx(STATUS_ERROR, "memory allocation failure");
248 memset(selected, 0, nproc);
251 * Refine the selection.
253 for (; *argv != NULL; argv++) {
254 if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
255 regerror(rv, ®, buf, sizeof(buf));
256 errx(STATUS_BADUSAGE, "bad expression: %s", buf);
259 for (i = 0, kp = plist; i < nproc; i++, kp++) {
260 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0 || kp->kp_proc.p_pid == mypid)
264 if ((pargv = kvm_getargv(kd, kp, 0)) == NULL)
268 while (j < sizeof(buf) && *pargv != NULL) {
269 j += snprintf(buf + j, sizeof(buf) - j,
270 pargv[1] != NULL ? "%s " : "%s",
277 mstr = kp->kp_thread.td_comm;
279 rv = regexec(®, mstr, 1, ®match, 0);
282 if (regmatch.rm_so == 0 &&
283 regmatch.rm_eo == strlen(mstr))
287 } else if (rv != REG_NOMATCH) {
288 regerror(rv, ®, buf, sizeof(buf));
289 errx(STATUS_ERROR, "regexec(): %s", buf);
296 for (i = 0, kp = plist; i < nproc; i++, kp++) {
297 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0)
300 SLIST_FOREACH(li, &ruidlist, li_chain) {
301 if (kp->kp_eproc.e_ucred.cr_ruid == li->li_datum.ld_uid)
304 if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
309 SLIST_FOREACH(li, &rgidlist, li_chain) {
310 if (kp->kp_eproc.e_ucred.cr_rgid == li->li_datum.ld_gid)
313 if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
318 SLIST_FOREACH(li, &euidlist, li_chain) {
319 if (kp->kp_eproc.e_ucred.cr_uid == li->li_datum.ld_uid)
322 if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
327 SLIST_FOREACH(li, &ppidlist, li_chain) {
328 if (kp->kp_eproc.e_ppid == li->li_datum.ld_pid)
331 if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
336 SLIST_FOREACH(li, &pgrplist, li_chain) {
337 if (kp->kp_eproc.e_pgid == li->li_datum.ld_pid)
340 if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
345 SLIST_FOREACH(li, &tdevlist, li_chain) {
346 if (li->li_datum.ld_dev == NODEV &&
347 (kp->kp_proc.p_flag & P_CONTROLT) == 0)
349 if (kp->kp_eproc.e_tdev == li->li_datum.ld_dev)
352 if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
357 SLIST_FOREACH(li, &sidlist, li_chain) {
358 if (kp->kp_eproc.e_sess->s_sid == li->li_datum.ld_pid)
361 if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
375 for (i = 0, kp = plist; i < nproc; i++, kp++) {
379 if (kp->kp_thread.td_start.tv_sec > best.tv_sec ||
380 (kp->kp_thread.td_start.tv_sec == best.tv_sec
381 && kp->kp_thread.td_start.tv_usec > best.tv_usec)) {
382 best.tv_sec = kp->kp_thread.td_start.tv_sec;
383 best.tv_usec = kp->kp_thread.td_start.tv_usec;
388 memset(selected, 0, nproc);
390 selected[bestidx] = 1;
394 * Take the appropriate action for each matched process, if any.
396 for (i = 0, j = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
397 if (kp->kp_proc.p_pid == mypid)
405 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0)
415 exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
424 ustr = "[-flnvx] [-d delim]";
426 ustr = "[-signal] [-fnvx]";
429 "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n"
430 " [-t tty] [-u euid] pattern ...\n", getprogname(),
437 killact(struct kinfo_proc *kp, int dummy)
439 dummy = 0; /* unused */
441 if (kill(kp->kp_proc.p_pid, signum) == -1)
442 err(STATUS_ERROR, "signalling pid %d", (int)kp->kp_proc.p_pid);
446 grepact(struct kinfo_proc *kp, int printdelim)
451 fputs(delim, stdout);
453 if (longfmt && matchargs) {
454 if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
457 printf("%d ", (int)kp->kp_proc.p_pid);
458 for (; *argv != NULL; argv++) {
464 printf("%d %s", (int)kp->kp_proc.p_pid, kp->kp_thread.td_comm);
466 printf("%d", (int)kp->kp_proc.p_pid);
471 parse_pid(const char *string, char **p, struct list *li, pid_t default_pid)
475 l = strtol(string, p, 0);
476 li->li_datum.ld_pid = (l == 0 ? default_pid : (pid_t)l);
481 makelist(struct listhead *head, enum listtype type, char *src)
487 const char *sp, *tty_name;
488 char *p, buf[MAXPATHLEN];
493 while ((sp = strsep(&src, ",")) != NULL) {
497 if ((li = malloc(sizeof(*li))) == NULL)
498 errx(STATUS_ERROR, "memory allocation failure");
499 SLIST_INSERT_HEAD(head, li, li_chain);
504 if (!parse_pid(sp, &p, li, (pid_t)0))
508 if (!parse_pid(sp, &p, li, getpgrp()))
512 if (!parse_pid(sp, &p, li, getsid(mypid)))
516 li->li_datum.ld_uid = (uid_t)strtol(sp, &p, 0);
518 if ((pw = getpwnam(sp)) == NULL) {
519 errx(STATUS_BADUSAGE,
520 "unknown user `%s'", optarg);
522 li->li_datum.ld_uid = pw->pw_uid;
526 li->li_datum.ld_gid = (gid_t)strtol(sp, &p, 0);
528 if ((gr = getgrnam(sp)) == NULL) {
529 errx(STATUS_BADUSAGE,
530 "unknown group `%s'", optarg);
532 li->li_datum.ld_gid = gr->gr_gid;
536 if (strcmp(sp, "-") == 0) {
537 li->li_datum.ld_dev = NODEV;
539 } else if (strcmp(sp, "co") == 0)
540 tty_name = "console";
541 else if (strncmp(sp, "tty", 3) == 0)
546 if (tty_name == NULL)
547 snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
549 snprintf(buf, sizeof(buf), "/dev/%s", tty_name);
551 if (stat(buf, &st) < 0) {
553 errx(STATUS_BADUSAGE,
554 "no such tty: `%s'", sp);
555 err(STATUS_ERROR, "stat(%s)", sp);
558 if ((st.st_mode & S_IFCHR) == 0)
559 errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
561 li->li_datum.ld_dev = st.st_rdev;