2013-01-02 14:36:07 +04:00
|
|
|
/* $NetBSD: pkill.c,v 1.29 2013/01/02 10:36:07 dsl Exp $ */
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
/*-
|
2007-02-18 01:49:56 +03:00
|
|
|
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
2002-03-01 14:21:58 +03:00
|
|
|
* 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/cdefs.h>
|
2002-03-01 14:29:46 +03:00
|
|
|
#ifndef lint
|
2013-01-02 14:36:07 +04:00
|
|
|
__RCSID("$NetBSD: pkill.c,v 1.29 2013/01/02 10:36:07 dsl Exp $");
|
2002-03-01 14:29:46 +03:00
|
|
|
#endif /* !lint */
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
2013-01-02 14:36:07 +04:00
|
|
|
#include <sys/resource.h>
|
2002-03-01 14:21:58 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.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>
|
2005-07-16 20:20:35 +04:00
|
|
|
#include <paths.h>
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
#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
|
|
|
|
};
|
|
|
|
|
|
|
|
struct list {
|
|
|
|
SLIST_ENTRY(list) li_chain;
|
|
|
|
long li_number;
|
|
|
|
};
|
|
|
|
|
|
|
|
SLIST_HEAD(listhead, list);
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
static struct kinfo_proc2 *plist;
|
|
|
|
static char *selected;
|
|
|
|
static const char *delim = "\n";
|
|
|
|
static int nproc;
|
|
|
|
static int pgrep;
|
2010-12-06 07:00:11 +03:00
|
|
|
static int prenice;
|
2005-07-16 20:20:35 +04:00
|
|
|
static int signum = SIGTERM;
|
2010-12-06 07:00:11 +03:00
|
|
|
static int nicenum;
|
2005-07-16 20:20:35 +04:00
|
|
|
static int newest;
|
|
|
|
static int inverse;
|
|
|
|
static int longfmt;
|
|
|
|
static int matchargs;
|
|
|
|
static int fullmatch;
|
|
|
|
static int cflags = REG_EXTENDED;
|
|
|
|
static kvm_t *kd;
|
|
|
|
static pid_t mypid;
|
|
|
|
|
|
|
|
static struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
|
|
|
|
static struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2007-12-15 22:44:37 +03:00
|
|
|
static void usage(void) __dead;
|
2005-07-20 16:54:30 +04:00
|
|
|
static int killact(const struct kinfo_proc2 *);
|
2010-12-06 07:00:11 +03:00
|
|
|
static int reniceact(const struct kinfo_proc2 *);
|
2005-07-20 16:54:30 +04:00
|
|
|
static int grepact(const struct kinfo_proc2 *);
|
2005-07-16 20:20:35 +04:00
|
|
|
static void makelist(struct listhead *, enum listtype, char *);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2005-07-20 16:54:30 +04:00
|
|
|
char buf[_POSIX2_LINE_MAX], **pargv, *q;
|
2002-03-01 14:21:58 +03:00
|
|
|
int i, j, ch, bestidx, rv, criteria;
|
2005-07-20 16:54:30 +04:00
|
|
|
int (*action)(const struct kinfo_proc2 *);
|
|
|
|
const struct kinfo_proc2 *kp;
|
2002-03-01 14:21:58 +03:00
|
|
|
struct list *li;
|
2012-11-21 02:52:01 +04:00
|
|
|
const char *p;
|
2002-03-01 14:21:58 +03:00
|
|
|
u_int32_t bestsec, bestusec;
|
|
|
|
regex_t reg;
|
|
|
|
regmatch_t regmatch;
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2002-03-01 14:21:58 +03:00
|
|
|
if (strcmp(getprogname(), "pgrep") == 0) {
|
|
|
|
action = grepact;
|
|
|
|
pgrep = 1;
|
2010-12-06 07:00:11 +03:00
|
|
|
} else if (strcmp(getprogname(), "prenice") == 0) {
|
|
|
|
prenice = 1;
|
|
|
|
|
2002-03-01 14:21:58 +03:00
|
|
|
} else {
|
|
|
|
action = killact;
|
|
|
|
p = argv[1];
|
|
|
|
|
2002-03-04 16:47:08 +03:00
|
|
|
if (argc > 1 && p[0] == '-') {
|
2002-03-01 14:21:58 +03:00
|
|
|
p++;
|
2002-03-04 16:47:08 +03:00
|
|
|
i = (int)strtol(p, &q, 10);
|
|
|
|
if (*q == '\0') {
|
2002-03-01 14:21:58 +03:00
|
|
|
signum = i;
|
|
|
|
argv++;
|
2002-03-04 16:47:08 +03:00
|
|
|
argc--;
|
2002-03-01 14:21:58 +03:00
|
|
|
} 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++;
|
2002-03-04 16:47:08 +03:00
|
|
|
argc--;
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
criteria = 0;
|
|
|
|
|
2010-12-07 10:39:15 +03:00
|
|
|
if (prenice) {
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-l") == 0) {
|
|
|
|
longfmt = 1;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
action = reniceact;
|
|
|
|
p = argv[1];
|
|
|
|
|
|
|
|
i = (int)strtol(p, &q, 10);
|
|
|
|
if (*q == '\0') {
|
|
|
|
nicenum = i;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
} else
|
|
|
|
usage();
|
|
|
|
} else {
|
2010-12-06 07:00:11 +03:00
|
|
|
while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns: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 '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':
|
|
|
|
cflags |= REG_ICASE;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
longfmt = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
newest = 1;
|
|
|
|
criteria = 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:
|
2002-03-01 14:21:58 +03:00
|
|
|
usage();
|
2010-12-06 07:00:11 +03:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2010-12-07 10:39:15 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2010-12-06 07:00:11 +03:00
|
|
|
}
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
if (argc != 0)
|
|
|
|
criteria = 1;
|
|
|
|
if (!criteria)
|
|
|
|
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)
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
|
|
|
|
if (plist == NULL)
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_ERROR, "Cannot get process list (%s)",
|
|
|
|
kvm_geterr(kd));
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate memory which will be used to keep track of the
|
|
|
|
* selection.
|
|
|
|
*/
|
2005-07-16 23:50:32 +04:00
|
|
|
if ((selected = calloc((size_t)1, (size_t)nproc)) == NULL)
|
2005-07-16 20:20:35 +04:00
|
|
|
err(STATUS_ERROR, "Cannot allocate memory for %d processes",
|
|
|
|
nproc);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Refine the selection.
|
|
|
|
*/
|
|
|
|
for (; *argv != NULL; argv++) {
|
2005-03-16 11:52:20 +03:00
|
|
|
if ((rv = regcomp(®, *argv, cflags)) != 0) {
|
2005-07-16 23:50:32 +04:00
|
|
|
(void)regerror(rv, ®, buf, sizeof(buf));
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_BADUSAGE,
|
|
|
|
"Cannot compile regular expression `%s' (%s)",
|
|
|
|
*argv, buf);
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
2007-02-18 01:49:56 +03:00
|
|
|
if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
|
2002-03-01 14:21:58 +03:00
|
|
|
continue;
|
|
|
|
|
2012-11-21 02:52:01 +04:00
|
|
|
if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
|
|
|
|
continue;
|
2002-03-01 14:21:58 +03:00
|
|
|
if (matchargs) {
|
|
|
|
|
|
|
|
j = 0;
|
2009-04-13 04:12:16 +04:00
|
|
|
while (j < (int)sizeof(buf) && *pargv != NULL) {
|
2002-03-01 14:21:58 +03:00
|
|
|
j += snprintf(buf + j, sizeof(buf) - j,
|
|
|
|
pargv[1] != NULL ? "%s " : "%s",
|
|
|
|
pargv[0]);
|
|
|
|
pargv++;
|
|
|
|
}
|
|
|
|
} else
|
2012-11-21 02:52:01 +04:00
|
|
|
strlcpy(buf, pargv[0], sizeof(buf));
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2012-11-21 02:52:01 +04:00
|
|
|
rv = regexec(®, buf, 1, ®match, 0);
|
2002-03-01 14:21:58 +03:00
|
|
|
if (rv == 0) {
|
|
|
|
if (fullmatch) {
|
|
|
|
if (regmatch.rm_so == 0 &&
|
2012-11-21 02:52:01 +04:00
|
|
|
regmatch.rm_eo ==
|
|
|
|
(regoff_t)strlen(buf))
|
2002-03-01 14:21:58 +03:00
|
|
|
selected[i] = 1;
|
|
|
|
} else
|
|
|
|
selected[i] = 1;
|
|
|
|
} else if (rv != REG_NOMATCH) {
|
2005-07-16 23:50:32 +04:00
|
|
|
(void)regerror(rv, ®, buf, sizeof(buf));
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_ERROR,
|
|
|
|
"Regular expression evaluation error (%s)",
|
|
|
|
buf);
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
regfree(®);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
2007-02-18 01:49:56 +03:00
|
|
|
if ((kp->p_flag & P_SYSTEM) != 0)
|
2002-03-01 14:21:58 +03:00
|
|
|
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)
|
2009-04-13 04:12:16 +04:00
|
|
|
if ((uid_t)kp->p_ppid == (uid_t)li->li_number)
|
2002-03-01 14:21:58 +03:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &pgrplist, li_chain)
|
2009-04-13 04:12:16 +04:00
|
|
|
if (kp->p__pgid == (pid_t)li->li_number)
|
2002-03-01 14:21:58 +03:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &tdevlist, li_chain) {
|
|
|
|
if (li->li_number == -1 &&
|
2007-02-18 01:49:56 +03:00
|
|
|
(kp->p_flag & P_CONTROLT) == 0)
|
2002-03-01 14:21:58 +03:00
|
|
|
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)
|
2009-04-13 04:12:16 +04:00
|
|
|
if (kp->p_sid == (pid_t)li->li_number)
|
2002-03-01 14:21:58 +03:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
selected[i] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newest) {
|
|
|
|
bestsec = 0;
|
|
|
|
bestusec = 0;
|
|
|
|
bestidx = -1;
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
|
|
|
if (!selected[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-16 23:50:32 +04:00
|
|
|
(void)memset(selected, 0, (size_t)nproc);
|
2002-03-01 14:21:58 +03:00
|
|
|
if (bestidx != -1)
|
|
|
|
selected[bestidx] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take the appropriate action for each matched process, if any.
|
|
|
|
*/
|
|
|
|
for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
|
|
|
|
if (kp->p_pid == mypid)
|
|
|
|
continue;
|
|
|
|
if (selected[i]) {
|
|
|
|
if (inverse)
|
|
|
|
continue;
|
|
|
|
} else if (!inverse)
|
|
|
|
continue;
|
|
|
|
|
2007-02-18 01:49:56 +03:00
|
|
|
if ((kp->p_flag & P_SYSTEM) != 0)
|
2002-03-06 15:02:18 +03:00
|
|
|
continue;
|
|
|
|
|
2005-07-16 19:53:56 +04:00
|
|
|
rv |= (*action)(kp);
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
2005-07-16 23:50:32 +04:00
|
|
|
return rv ? STATUS_MATCH : STATUS_NOMATCH;
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
static void
|
2002-03-01 14:21:58 +03:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
const char *ustr;
|
|
|
|
|
2010-12-06 07:00:11 +03:00
|
|
|
if (prenice)
|
2010-12-07 10:39:15 +03:00
|
|
|
fprintf(stderr, "Usage: %s [-l] priority pattern ...\n",
|
2010-12-06 07:00:11 +03:00
|
|
|
getprogname());
|
|
|
|
else {
|
|
|
|
if (pgrep)
|
|
|
|
ustr = "[-filnvx] [-d delim]";
|
|
|
|
else
|
|
|
|
ustr = "[-signal] [-filnvx]";
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] "
|
|
|
|
"[-t tty]\n"
|
|
|
|
" [-U uid] [-u euid] pattern ...\n",
|
|
|
|
getprogname(), ustr);
|
|
|
|
}
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-10-11 02:13:20 +04:00
|
|
|
exit(STATUS_BADUSAGE);
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
static int
|
2005-07-20 16:54:30 +04:00
|
|
|
killact(const struct kinfo_proc2 *kp)
|
2002-03-01 14:21:58 +03:00
|
|
|
{
|
2007-03-21 23:05:18 +03:00
|
|
|
if (longfmt)
|
|
|
|
grepact(kp);
|
2005-07-16 19:53:56 +04:00
|
|
|
if (kill(kp->p_pid, signum) == -1) {
|
2005-07-20 16:40:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for ESRCH, which indicates that the process
|
|
|
|
* disappeared between us matching it and us
|
2005-10-08 16:05:55 +04:00
|
|
|
* signalling it; don't issue a warning about it.
|
2005-07-20 16:40:27 +04:00
|
|
|
*/
|
2005-10-08 16:05:55 +04:00
|
|
|
if (errno != ESRCH)
|
|
|
|
warn("signalling pid %d", (int)kp->p_pid);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-10-08 16:05:55 +04:00
|
|
|
/*
|
|
|
|
* Return 0 to indicate that the process should not be
|
|
|
|
* considered a match, since we didn't actually get to
|
|
|
|
* signal it.
|
|
|
|
*/
|
|
|
|
return 0;
|
2005-07-16 19:53:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
2010-12-06 07:00:11 +03:00
|
|
|
static int
|
|
|
|
reniceact(const struct kinfo_proc2 *kp)
|
|
|
|
{
|
|
|
|
int oldprio;
|
|
|
|
|
|
|
|
if (longfmt)
|
|
|
|
grepact(kp);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if ((oldprio = getpriority(PRIO_PROCESS, kp->p_pid)) == -1 &&
|
|
|
|
errno != 0) {
|
|
|
|
warn("%d: getpriority", kp->p_pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setpriority(PRIO_PROCESS, kp->p_pid, nicenum) == -1) {
|
|
|
|
warn("%d: setpriority", kp->p_pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)printf("%d: old priority %d, new priority %d\n",
|
|
|
|
kp->p_pid, oldprio, nicenum);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
static int
|
2005-07-20 16:54:30 +04:00
|
|
|
grepact(const struct kinfo_proc2 *kp)
|
2002-03-01 14:21:58 +03:00
|
|
|
{
|
|
|
|
char **argv;
|
|
|
|
|
|
|
|
if (longfmt && matchargs) {
|
2005-07-20 16:40:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If kvm_getargv2() failed the process has probably
|
|
|
|
* disappeared. Return 0 to indicate that the process
|
|
|
|
* should not be considered a match, since we are no
|
|
|
|
* longer in a position to output it as a match.
|
|
|
|
*/
|
2002-03-01 14:21:58 +03:00
|
|
|
if ((argv = kvm_getargv2(kd, kp, 0)) == NULL)
|
2005-07-16 19:53:56 +04:00
|
|
|
return 0;
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)printf("%d ", (int)kp->p_pid);
|
2002-03-01 14:21:58 +03:00
|
|
|
for (; *argv != NULL; argv++) {
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)printf("%s", *argv);
|
2002-03-01 14:21:58 +03:00
|
|
|
if (argv[1] != NULL)
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)putchar(' ');
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
} else if (longfmt)
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)printf("%d %s", (int)kp->p_pid, kp->p_comm);
|
2002-03-01 14:21:58 +03:00
|
|
|
else
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)printf("%d", (int)kp->p_pid);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)printf("%s", delim);
|
2005-07-16 19:53:56 +04:00
|
|
|
|
|
|
|
return 1;
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
static void
|
2002-03-01 14:21:58 +03:00
|
|
|
makelist(struct listhead *head, enum listtype type, char *src)
|
|
|
|
{
|
|
|
|
struct list *li;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
struct stat st;
|
2005-07-16 20:20:35 +04:00
|
|
|
char *sp, *ep, buf[MAXPATHLEN];
|
|
|
|
const char *p;
|
2002-03-01 14:21:58 +03:00
|
|
|
int empty;
|
2005-07-16 20:20:35 +04:00
|
|
|
const char *prefix = _PATH_DEV;
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
empty = 1;
|
|
|
|
|
|
|
|
while ((sp = strsep(&src, ",")) != NULL) {
|
|
|
|
if (*sp == '\0')
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if ((li = malloc(sizeof(*li))) == NULL)
|
2012-11-21 02:52:01 +04:00
|
|
|
err(STATUS_ERROR, "Cannot allocate %zu bytes",
|
2005-07-16 20:20:35 +04:00
|
|
|
sizeof(*li));
|
2002-03-01 14:21:58 +03:00
|
|
|
SLIST_INSERT_HEAD(head, li, li_chain);
|
|
|
|
empty = 0;
|
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
li->li_number = (uid_t)strtol(sp, &ep, 0);
|
2009-02-28 21:16:10 +03:00
|
|
|
if (*ep == '\0' && type != LT_TTY) {
|
2002-03-01 14:21:58 +03:00
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case LT_USER:
|
|
|
|
if ((pw = getpwnam(sp)) == NULL)
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_BADUSAGE, "Unknown user `%s'",
|
2005-03-02 18:31:44 +03:00
|
|
|
sp);
|
2002-03-01 14:21:58 +03:00
|
|
|
li->li_number = pw->pw_uid;
|
|
|
|
break;
|
|
|
|
case LT_GROUP:
|
|
|
|
if ((gr = getgrnam(sp)) == NULL)
|
2005-07-16 20:20:35 +04:00
|
|
|
errx(STATUS_BADUSAGE, "Unknown group `%s'",
|
2005-03-02 18:31:44 +03:00
|
|
|
sp);
|
2002-03-01 14:21:58 +03:00
|
|
|
li->li_number = gr->gr_gid;
|
|
|
|
break;
|
|
|
|
case LT_TTY:
|
2009-02-28 21:16:10 +03:00
|
|
|
p = sp;
|
|
|
|
if (*sp == '/')
|
|
|
|
prefix = "";
|
|
|
|
else if (strcmp(sp, "-") == 0) {
|
2002-03-01 14:21:58 +03:00
|
|
|
li->li_number = -1;
|
|
|
|
break;
|
|
|
|
} else if (strcmp(sp, "co") == 0)
|
|
|
|
p = "console";
|
|
|
|
else if (strncmp(sp, "tty", 3) == 0)
|
2009-02-28 21:16:10 +03:00
|
|
|
/* all set */;
|
|
|
|
else if (strncmp(sp, "pts/", 4) == 0)
|
|
|
|
/* all set */;
|
|
|
|
else if (*ep != '\0' || (strlen(sp) == 2 && *sp == '0'))
|
2005-07-16 20:20:35 +04:00
|
|
|
prefix = _PATH_TTY;
|
2009-02-28 21:16:10 +03:00
|
|
|
else
|
|
|
|
prefix = _PATH_DEV_PTS;
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
(void)snprintf(buf, sizeof(buf), "%s%s", prefix, p);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
2005-07-16 20:20:35 +04:00
|
|
|
if (stat(buf, &st) == -1) {
|
2002-03-01 14:21:58 +03:00
|
|
|
if (errno == ENOENT)
|
|
|
|
errx(STATUS_BADUSAGE,
|
2009-02-28 21:16:10 +03:00
|
|
|
"No such tty: `%s'", buf);
|
|
|
|
err(STATUS_ERROR, "Cannot access `%s'", buf);
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((st.st_mode & S_IFCHR) == 0)
|
2009-02-28 21:16:10 +03:00
|
|
|
errx(STATUS_BADUSAGE, "Not a tty: `%s'", buf);
|
2002-03-01 14:21:58 +03:00
|
|
|
|
|
|
|
li->li_number = st.st_rdev;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2005-07-16 20:20:35 +04:00
|
|
|
}
|
2002-03-01 14:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty)
|
|
|
|
usage();
|
|
|
|
}
|