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.9 2007/02/01 10:33:26 corecode 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.
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #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
69 LT_USER, /* real or effective user: uid_t */
70 LT_GROUP, /* group: gid_t */
71 LT_TTY, /* tty: dev_t */
72 LT_PPID, /* parent pid: pid_t */
73 LT_PGRP, /* process group: pid_t */
74 LT_SID, /* session id: pid_t */
75 LT_JID /* jail id: pid_t */
79 SLIST_ENTRY(list) li_chain;
88 SLIST_HEAD(listhead, list);
90 struct kinfo_proc *plist;
92 const char *delim = "\n";
104 struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
105 struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
106 struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
107 struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
108 struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
109 struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
110 struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
111 struct listhead jidlist = SLIST_HEAD_INITIALIZER(list);
114 void killact(struct kinfo_proc *, int);
115 void grepact(struct kinfo_proc *, int);
116 int parse_pid(const char *, char **, struct list *, pid_t);
117 void makelist(struct listhead *, enum listtype, char *);
120 * pkill - list or signal selected processes based on regular expression.
123 main(int argc, char **argv)
125 char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
126 int i, ch, bestidx, rv, criteria;
128 void (*action)(struct kinfo_proc *, int);
129 struct kinfo_proc *kp;
134 const char *kvmf = _PATH_DEVNULL;
136 if (strcmp(getprogname(), "pgrep") == 0) {
144 * For pkill only: parse the signal (number or name) to send.
146 if (argc > 1 && p[0] == '-') {
148 i = (int)strtol(p, &q, 10);
154 if (strncasecmp(p, "sig", 3) == 0)
156 for (i = 1; i < NSIG; i++) {
157 if (strcasecmp(sys_signame[i], p) == 0)
171 while ((ch = getopt(argc, argv, "G:P:U:d:fg:j:lns:t:u:vx")) != -1) {
174 makelist(&rgidlist, LT_GROUP, optarg);
178 makelist(&ppidlist, LT_PPID, optarg);
182 makelist(&ruidlist, LT_USER, optarg);
194 makelist(&pgrplist, LT_PGRP, optarg);
198 makelist(&jidlist, LT_JID, optarg);
211 makelist(&sidlist, LT_SID, optarg);
215 makelist(&tdevlist, LT_TTY, optarg);
219 makelist(&euidlist, LT_USER, optarg);
244 * Retrieve the list of running processes from the kernel.
246 kd = kvm_openfiles(kvmf, kvmf, NULL, O_RDONLY, buf);
248 errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
250 plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
252 errx(STATUS_ERROR, "cannot list processes");
255 * Allocate memory which will be used to keep track of the
258 if ((selected = malloc(nproc)) == NULL)
259 errx(STATUS_ERROR, "memory allocation failure");
260 memset(selected, 0, nproc);
263 * Refine the selection.
265 for (; *argv != NULL; argv++) {
266 if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
267 regerror(rv, ®, buf, sizeof(buf));
268 errx(STATUS_BADUSAGE, "bad expression: %s", buf);
271 for (i = 0, kp = plist; i < nproc; i++, kp++) {
272 if ((kp->kp_flags & P_SYSTEM) != 0 || kp->kp_pid == mypid)
276 if ((pargv = kvm_getargv(kd, kp, 0)) == NULL)
280 while (j < sizeof(buf) && *pargv != NULL) {
281 j += snprintf(buf + j, sizeof(buf) - j,
282 pargv[1] != NULL ? "%s " : "%s",
291 rv = regexec(®, mstr, 1, ®match, 0);
294 if (regmatch.rm_so == 0 &&
295 regmatch.rm_eo == (regoff_t)strlen(mstr))
299 } else if (rv != REG_NOMATCH) {
300 regerror(rv, ®, buf, sizeof(buf));
301 errx(STATUS_ERROR, "regexec(): %s", buf);
309 * Iterate through the list of processes, deselecting each one
310 * if it fails to meet the established criteria.
312 for (i = 0, kp = plist; i < nproc; i++, kp++) {
313 if ((kp->kp_flags & P_SYSTEM) != 0)
316 SLIST_FOREACH(li, &ruidlist, li_chain) {
317 if (kp->kp_ruid == li->li_datum.ld_uid)
320 if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
325 SLIST_FOREACH(li, &rgidlist, li_chain) {
326 if (kp->kp_rgid == li->li_datum.ld_gid)
329 if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
334 SLIST_FOREACH(li, &euidlist, li_chain) {
335 if (kp->kp_uid == li->li_datum.ld_uid)
338 if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
343 SLIST_FOREACH(li, &ppidlist, li_chain) {
344 if (kp->kp_ppid == li->li_datum.ld_pid)
347 if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
352 SLIST_FOREACH(li, &pgrplist, li_chain) {
353 if (kp->kp_pgid == li->li_datum.ld_pid)
356 if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
361 SLIST_FOREACH(li, &tdevlist, li_chain) {
362 if (li->li_datum.ld_dev == NODEV &&
363 (kp->kp_flags & P_CONTROLT) == 0)
365 if (kp->kp_tdev == li->li_datum.ld_dev)
368 if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
373 SLIST_FOREACH(li, &sidlist, li_chain) {
374 if (kp->kp_sid == li->li_datum.ld_pid)
377 if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
382 SLIST_FOREACH(li, &jidlist, li_chain) {
383 /* A particular jail ID, including 0 (not in jail) */
384 if (kp->kp_jailid == li->li_datum.ld_pid)
387 if (kp->kp_jailid > 0 && li->li_datum.ld_pid < 0)
390 if (SLIST_FIRST(&jidlist) != NULL && li == NULL) {
404 for (i = 0, kp = plist; i < nproc; i++, kp++) {
408 if (kp->kp_start.tv_sec > best.tv_sec ||
409 (kp->kp_start.tv_sec == best.tv_sec
410 && kp->kp_start.tv_usec > best.tv_usec)) {
411 best.tv_sec = kp->kp_start.tv_sec;
412 best.tv_usec = kp->kp_start.tv_usec;
417 memset(selected, 0, nproc);
419 selected[bestidx] = 1;
423 * Take the appropriate action for each matched process, if any.
425 for (i = 0, j = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
426 if (kp->kp_pid == mypid)
434 if ((kp->kp_flags & P_SYSTEM) != 0)
444 exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
453 ustr = "[-flnvx] [-d delim]";
455 ustr = "[-signal] [-fnvx]";
458 "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n"
459 " [-t tty] [-u euid] [-j jid] pattern ...\n",
460 getprogname(), ustr);
466 * Action callback to signal the given process (pkill).
469 killact(struct kinfo_proc *kp, int dummy __unused)
471 if (kill(kp->kp_pid, signum) == -1)
472 err(STATUS_ERROR, "signalling pid %d", (int)kp->kp_pid);
476 * Action callback to print the pid of the given process (pgrep).
479 grepact(struct kinfo_proc *kp, int printdelim)
484 fputs(delim, stdout);
486 if (longfmt && matchargs) {
487 if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
490 printf("%d ", (int)kp->kp_pid);
491 for (; *argv != NULL; argv++) {
497 printf("%d %s", (int)kp->kp_pid, kp->kp_comm);
499 printf("%d", (int)kp->kp_pid);
504 * Parse a pid from the given string. If zero, use a given default.
507 parse_pid(const char *string, char **p, struct list *li, pid_t default_pid)
511 l = strtol(string, p, 0);
512 li->li_datum.ld_pid = (l == 0 ? default_pid : (pid_t)l);
517 * Populate a list from a comma-seperated string of items.
518 * The possible valid values for each item depends on the type of list.
521 makelist(struct listhead *head, enum listtype type, char *src)
527 const char *sp, *tty_name;
528 char *p, buf[MAXPATHLEN];
533 while ((sp = strsep(&src, ",")) != NULL) {
537 if ((li = malloc(sizeof(*li))) == NULL)
538 errx(STATUS_ERROR, "memory allocation failure");
539 SLIST_INSERT_HEAD(head, li, li_chain);
544 if (!parse_pid(sp, &p, li, (pid_t)0))
548 if (!parse_pid(sp, &p, li, getpgrp()))
552 if (!parse_pid(sp, &p, li, getsid(mypid)))
556 /* default to no jail */
557 if (!parse_pid(sp, &p, li, 0))
559 if (li->li_datum.ld_pid < -1) {
560 errx(STATUS_BADUSAGE,
561 "Negative jail ID `%s'", sp);
565 li->li_datum.ld_uid = (uid_t)strtol(sp, &p, 0);
567 if ((pw = getpwnam(sp)) == NULL) {
568 errx(STATUS_BADUSAGE,
569 "unknown user `%s'", optarg);
571 li->li_datum.ld_uid = pw->pw_uid;
575 li->li_datum.ld_gid = (gid_t)strtol(sp, &p, 0);
577 if ((gr = getgrnam(sp)) == NULL) {
578 errx(STATUS_BADUSAGE,
579 "unknown group `%s'", optarg);
581 li->li_datum.ld_gid = gr->gr_gid;
585 if (strcmp(sp, "-") == 0) {
586 li->li_datum.ld_dev = NODEV;
588 } else if (strcmp(sp, "co") == 0)
589 tty_name = "console";
590 else if (strncmp(sp, "tty", 3) == 0)
595 if (tty_name == NULL)
596 snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
598 snprintf(buf, sizeof(buf), "/dev/%s", tty_name);
600 if (stat(buf, &st) < 0) {
602 errx(STATUS_BADUSAGE,
603 "no such tty: `%s'", sp);
604 err(STATUS_ERROR, "stat(%s)", sp);
607 if ((st.st_mode & S_IFCHR) == 0)
608 errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
610 li->li_datum.ld_dev = st.st_rdev;