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.3 2004/10/25 21:48:37 liamfoy 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;
82 SLIST_HEAD(listhead, list);
84 struct kinfo_proc *plist;
98 struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
99 struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
100 struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
101 struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
102 struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
103 struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
104 struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
107 void killact(struct kinfo_proc *);
108 void grepact(struct kinfo_proc *);
109 void makelist(struct listhead *, enum listtype, char *);
112 main(int argc, char **argv)
116 char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
117 int i, j, ch, bestidx, rv, criteria;
118 void (*action)(struct kinfo_proc *);
119 struct kinfo_proc *kp;
121 u_int32_t bestsec, bestusec;
124 const char *kvmf = _PATH_DEVNULL;
126 if (strcmp(getprogname(), "pgrep") == 0) {
133 if (argc > 1 && p[0] == '-') {
135 i = (int)strtol(p, &q, 10);
141 if (strncasecmp(p, "sig", 3) == 0)
143 for (i = 1; i < NSIG; i++)
144 if (strcasecmp(sys_signame[i], p) == 0)
157 while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:u:vx")) != -1)
160 makelist(&rgidlist, LT_GROUP, optarg);
164 makelist(&ppidlist, LT_GENERIC, optarg);
168 makelist(&ruidlist, LT_USER, optarg);
180 makelist(&pgrplist, LT_PGRP, optarg);
193 makelist(&sidlist, LT_SID, optarg);
197 makelist(&tdevlist, LT_TTY, optarg);
201 makelist(&euidlist, LT_USER, optarg);
225 * Retrieve the list of running processes from the kernel.
227 kd = kvm_openfiles(kvmf, kvmf, NULL, O_RDONLY, buf);
229 errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
231 plist = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc);
233 errx(STATUS_ERROR, "cannot list processes");
236 * Allocate memory which will be used to keep track of the
239 if ((selected = malloc(nproc)) == NULL)
240 errx(STATUS_ERROR, "memory allocation failure");
241 memset(selected, 0, nproc);
244 * Refine the selection.
246 for (; *argv != NULL; argv++) {
247 if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
248 regerror(rv, ®, buf, sizeof(buf));
249 errx(STATUS_BADUSAGE, "bad expression: %s", buf);
252 for (i = 0, kp = plist; i < nproc; i++, kp++) {
253 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0 || kp->kp_proc.p_pid == mypid)
257 if ((pargv = kvm_getargv(kd, kp, 0)) == NULL)
261 while (j < sizeof(buf) && *pargv != NULL) {
262 j += snprintf(buf + j, sizeof(buf) - j,
263 pargv[1] != NULL ? "%s " : "%s",
270 mstr = kp->kp_thread.td_comm;
272 rv = regexec(®, mstr, 1, ®match, 0);
275 if (regmatch.rm_so == 0 &&
276 regmatch.rm_eo == strlen(mstr))
280 } else if (rv != REG_NOMATCH) {
281 regerror(rv, ®, buf, sizeof(buf));
282 errx(STATUS_ERROR, "regexec(): %s", buf);
289 for (i = 0, kp = plist; i < nproc; i++, kp++) {
290 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0)
293 SLIST_FOREACH(li, &ruidlist, li_chain)
294 if (kp->kp_eproc.e_ucred.cr_ruid == (uid_t)li->li_number)
296 if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
301 SLIST_FOREACH(li, &rgidlist, li_chain)
302 if (kp->kp_eproc.e_ucred.cr_rgid == (gid_t)li->li_number)
304 if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
309 SLIST_FOREACH(li, &euidlist, li_chain)
310 if (kp->kp_eproc.e_ucred.cr_uid == (uid_t)li->li_number)
312 if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
317 SLIST_FOREACH(li, &ppidlist, li_chain)
318 if (kp->kp_eproc.e_ppid == (uid_t)li->li_number)
320 if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
325 SLIST_FOREACH(li, &pgrplist, li_chain)
326 if (kp->kp_eproc.e_pgid == (uid_t)li->li_number)
328 if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
333 SLIST_FOREACH(li, &tdevlist, li_chain) {
334 if (li->li_number == -1 &&
335 (kp->kp_proc.p_flag & P_CONTROLT) == 0)
337 if (kp->kp_eproc.e_tdev == (uid_t)li->li_number)
340 if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
345 SLIST_FOREACH(li, &sidlist, li_chain)
346 if (kp->kp_eproc.e_sess->s_sid == (uid_t)li->li_number)
348 if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
362 for (i = 0, kp = plist; i < nproc; i++, kp++) {
366 if (kp->kp_thread.td_start.tv_sec > bestsec ||
367 (kp->kp_thread.td_start.tv_sec == bestsec
368 && kp->kp_thread.td_start.tv_usec > bestusec)) {
369 bestsec = kp->kp_thread.td_start.tv_sec;
370 bestusec = kp->kp_thread.td_start.tv_usec;
375 memset(selected, 0, nproc);
377 selected[bestidx] = 1;
381 * Take the appropriate action for each matched process, if any.
383 for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
384 if (kp->kp_proc.p_pid == mypid)
392 if ((kp->kp_proc.p_flag & P_SYSTEM) != 0)
399 exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
408 ustr = "[-flnvx] [-d delim]";
410 ustr = "[-signal] [-fnvx]";
413 "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n"
414 " [-t tty] [-u euid] pattern ...\n", getprogname(),
421 killact(struct kinfo_proc *kp)
424 if (kill(kp->kp_proc.p_pid, signum) == -1)
425 err(STATUS_ERROR, "signalling pid %d", (int)kp->kp_proc.p_pid);
429 grepact(struct kinfo_proc *kp)
433 if (longfmt && matchargs) {
434 if ((argv = kvm_getargv(kd, kp, 0)) == NULL)
437 printf("%d ", (int)kp->kp_proc.p_pid);
438 for (; *argv != NULL; argv++) {
444 printf("%d %s", (int)kp->kp_proc.p_pid, kp->kp_thread.td_comm);
446 printf("%d", (int)kp->kp_proc.p_pid);
452 makelist(struct listhead *head, enum listtype type, char *src)
458 char *sp, *p, buf[MAXPATHLEN];
463 while ((sp = strsep(&src, ",")) != NULL) {
467 if ((li = malloc(sizeof(*li))) == NULL)
468 errx(STATUS_ERROR, "memory allocation failure");
469 SLIST_INSERT_HEAD(head, li, li_chain);
472 li->li_number = (uid_t)strtol(sp, &p, 0);
476 if (li->li_number == 0)
477 li->li_number = getpgrp();
480 if (li->li_number == 0)
481 li->li_number = getsid(mypid);
493 if ((pw = getpwnam(sp)) == NULL)
494 errx(STATUS_BADUSAGE, "unknown user `%s'",
496 li->li_number = pw->pw_uid;
499 if ((gr = getgrnam(sp)) == NULL)
500 errx(STATUS_BADUSAGE, "unknown group `%s'",
502 li->li_number = gr->gr_gid;
505 if (strcmp(sp, "-") == 0) {
508 } else if (strcmp(sp, "co") == 0)
510 else if (strncmp(sp, "tty", 3) == 0)
516 snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
518 snprintf(buf, sizeof(buf), "/dev/%s", p);
520 if (stat(buf, &st) < 0) {
522 errx(STATUS_BADUSAGE,
523 "no such tty: `%s'", sp);
524 err(STATUS_ERROR, "stat(%s)", sp);
527 if ((st.st_mode & S_IFCHR) == 0)
528 errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
530 li->li_number = st.st_rdev;