2005-11-16 11:03:00 +00:00
|
|
|
/* $NetBSD: pkill.c,v 1.16 2005/10/10 22:13:20 kleink Exp $ */
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2002 The NetBSD Foundation, Inc.
|
2005-11-16 10:36:44 +00:00
|
|
|
* Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
|
2004-03-25 19:08:48 +00: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>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/stat.h>
|
2005-03-20 11:42:26 +00:00
|
|
|
#include <sys/time.h>
|
2004-03-25 21:58:56 +00:00
|
|
|
#include <sys/user.h>
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2010-02-12 18:52:24 +00:00
|
|
|
#include <assert.h>
|
2004-03-25 19:08:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
2004-03-25 21:58:56 +00:00
|
|
|
#include <paths.h>
|
2004-03-25 19:08:48 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <ctype.h>
|
2004-03-25 21:58:56 +00:00
|
|
|
#include <fcntl.h>
|
2004-03-25 19:08:48 +00:00
|
|
|
#include <kvm.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
2004-07-15 08:13:56 +00:00
|
|
|
#include <locale.h>
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
#define STATUS_MATCH 0
|
|
|
|
#define STATUS_NOMATCH 1
|
|
|
|
#define STATUS_BADUSAGE 2
|
|
|
|
#define STATUS_ERROR 3
|
|
|
|
|
2005-03-20 11:03:11 +00:00
|
|
|
#define MIN_PID 5
|
|
|
|
#define MAX_PID 99999
|
|
|
|
|
2005-03-20 11:30:26 +00:00
|
|
|
/* Ignore system-processes (if '-S' flag is not specified) and myself. */
|
|
|
|
#define PSKIP(kp) ((kp)->ki_pid == mypid || \
|
|
|
|
(!kthreads && ((kp)->ki_flag & P_KTHREAD) != 0))
|
2004-03-25 21:58:56 +00:00
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
enum listtype {
|
|
|
|
LT_GENERIC,
|
|
|
|
LT_USER,
|
|
|
|
LT_GROUP,
|
|
|
|
LT_TTY,
|
|
|
|
LT_PGRP,
|
Fix and extend the -j option to pkill/pgrep WRT the jail
wildcard specifications. Earlier the only wildcard syntax
was "-j 0" for "any jail". There were at least
two shortcomings in it: First, jail ID 0 was abused; it
meant "no jail" in other utils, e.g., ps(1). Second, it
was impossible to match processed not in jail, which could
be useful to rc.d developers. Therefore a new syntax is
introduced: "-j any" means any jail while "-j none" means
out of jail. The old syntax is preserved for compatibility,
but now it's deprecated because it's limited and confusing.
Update the respective regression tests. While I'm here,
make the tests more complex but sensitive: Start several
processes, some in jail and some out of jail, so we can
detect that only the right processes are killed by pkill
or matched by pgrep.
Reviewed by: gad, pjd
MFC after: 1 week
2006-11-23 11:55:17 +00:00
|
|
|
LT_JID,
|
2013-08-09 08:38:51 +00:00
|
|
|
LT_SID,
|
|
|
|
LT_CLASS
|
2004-03-25 19:08:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct list {
|
|
|
|
SLIST_ENTRY(list) li_chain;
|
|
|
|
long li_number;
|
2013-08-09 08:38:51 +00:00
|
|
|
char *li_name;
|
2004-03-25 19:08:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SLIST_HEAD(listhead, list);
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static struct kinfo_proc *plist;
|
|
|
|
static char *selected;
|
|
|
|
static const char *delim = "\n";
|
|
|
|
static int nproc;
|
|
|
|
static int pgrep;
|
|
|
|
static int signum = SIGTERM;
|
|
|
|
static int newest;
|
|
|
|
static int oldest;
|
|
|
|
static int interactive;
|
|
|
|
static int inverse;
|
|
|
|
static int longfmt;
|
|
|
|
static int matchargs;
|
|
|
|
static int fullmatch;
|
|
|
|
static int kthreads;
|
|
|
|
static int cflags = REG_EXTENDED;
|
2010-02-12 18:52:24 +00:00
|
|
|
static int quiet;
|
2005-11-16 11:03:00 +00:00
|
|
|
static kvm_t *kd;
|
|
|
|
static pid_t mypid;
|
|
|
|
|
2009-12-28 22:56:30 +00:00
|
|
|
static struct listhead euidlist = SLIST_HEAD_INITIALIZER(euidlist);
|
|
|
|
static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(ruidlist);
|
|
|
|
static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(rgidlist);
|
|
|
|
static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(pgrplist);
|
|
|
|
static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(ppidlist);
|
|
|
|
static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(tdevlist);
|
|
|
|
static struct listhead sidlist = SLIST_HEAD_INITIALIZER(sidlist);
|
|
|
|
static struct listhead jidlist = SLIST_HEAD_INITIALIZER(jidlist);
|
2013-08-09 08:38:51 +00:00
|
|
|
static struct listhead classlist = SLIST_HEAD_INITIALIZER(classlist);
|
2005-11-16 11:03:00 +00:00
|
|
|
|
|
|
|
static void usage(void) __attribute__((__noreturn__));
|
|
|
|
static int killact(const struct kinfo_proc *);
|
|
|
|
static int grepact(const struct kinfo_proc *);
|
|
|
|
static void makelist(struct listhead *, enum listtype, char *);
|
|
|
|
static int takepid(const char *, int);
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2005-08-25 20:10:47 +00:00
|
|
|
char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile;
|
2004-03-26 19:29:57 +00:00
|
|
|
const char *execf, *coref;
|
2010-06-20 08:48:30 +00:00
|
|
|
int ancestors, debug_opt, did_action;
|
2005-08-25 20:10:47 +00:00
|
|
|
int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock;
|
2004-03-26 00:15:20 +00:00
|
|
|
size_t jsz;
|
2005-11-16 11:03:00 +00:00
|
|
|
int (*action)(const struct kinfo_proc *);
|
2004-03-25 21:58:56 +00:00
|
|
|
struct kinfo_proc *kp;
|
2004-03-25 19:08:48 +00:00
|
|
|
struct list *li;
|
2004-03-26 00:15:20 +00:00
|
|
|
struct timeval best_tval;
|
2004-03-25 19:08:48 +00:00
|
|
|
regex_t reg;
|
|
|
|
regmatch_t regmatch;
|
2009-05-17 04:34:14 +00:00
|
|
|
pid_t pid;
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2004-07-15 08:13:56 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
if (strcmp(getprogname(), "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 {
|
2011-02-04 16:40:50 +00:00
|
|
|
if (strncasecmp(p, "SIG", 3) == 0)
|
2004-03-25 19:08:48 +00:00
|
|
|
p += 3;
|
|
|
|
for (i = 1; i < NSIG; i++)
|
|
|
|
if (strcasecmp(sys_signame[i], p) == 0)
|
|
|
|
break;
|
|
|
|
if (i != NSIG) {
|
|
|
|
signum = i;
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 04:34:14 +00:00
|
|
|
ancestors = 0;
|
2004-03-25 19:08:48 +00:00
|
|
|
criteria = 0;
|
2004-03-26 01:43:19 +00:00
|
|
|
debug_opt = 0;
|
2005-08-25 20:10:47 +00:00
|
|
|
pidfile = NULL;
|
|
|
|
pidfilelock = 0;
|
2010-02-12 18:52:24 +00:00
|
|
|
quiet = 0;
|
2010-02-08 21:23:48 +00:00
|
|
|
execf = NULL;
|
|
|
|
coref = _PATH_DEVNULL;
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2013-08-09 08:38:51 +00:00
|
|
|
while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ac:d:fg:ij:lnoqs:t:u:vx")) != -1)
|
2004-03-25 19:08:48 +00:00
|
|
|
switch (ch) {
|
2004-03-26 01:43:19 +00:00
|
|
|
case 'D':
|
|
|
|
debug_opt++;
|
|
|
|
break;
|
2005-03-20 11:03:11 +00:00
|
|
|
case 'F':
|
2005-08-25 20:10:47 +00:00
|
|
|
pidfile = optarg;
|
2005-03-20 11:03:11 +00:00
|
|
|
criteria = 1;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
case 'G':
|
|
|
|
makelist(&rgidlist, LT_GROUP, optarg);
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2005-11-16 10:36:44 +00:00
|
|
|
case 'I':
|
|
|
|
if (pgrep)
|
|
|
|
usage();
|
|
|
|
interactive = 1;
|
|
|
|
break;
|
2005-08-25 20:10:47 +00:00
|
|
|
case 'L':
|
|
|
|
pidfilelock = 1;
|
|
|
|
break;
|
2004-03-25 23:19:16 +00:00
|
|
|
case 'M':
|
|
|
|
coref = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
execf = optarg;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
case 'P':
|
|
|
|
makelist(&ppidlist, LT_GENERIC, optarg);
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2005-03-20 11:30:26 +00:00
|
|
|
case 'S':
|
|
|
|
if (!pgrep)
|
|
|
|
usage();
|
|
|
|
kthreads = 1;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
case 'U':
|
|
|
|
makelist(&ruidlist, LT_USER, optarg);
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2009-05-17 04:34:14 +00:00
|
|
|
case 'a':
|
|
|
|
ancestors++;
|
|
|
|
break;
|
2013-08-09 08:38:51 +00:00
|
|
|
case 'c':
|
|
|
|
makelist(&classlist, LT_CLASS, optarg);
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
case 'd':
|
|
|
|
if (!pgrep)
|
|
|
|
usage();
|
|
|
|
delim = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
matchargs = 1;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
makelist(&pgrplist, LT_PGRP, optarg);
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2005-03-20 11:12:13 +00:00
|
|
|
case 'i':
|
|
|
|
cflags |= REG_ICASE;
|
|
|
|
break;
|
2005-03-20 10:53:54 +00:00
|
|
|
case 'j':
|
Fix and extend the -j option to pkill/pgrep WRT the jail
wildcard specifications. Earlier the only wildcard syntax
was "-j 0" for "any jail". There were at least
two shortcomings in it: First, jail ID 0 was abused; it
meant "no jail" in other utils, e.g., ps(1). Second, it
was impossible to match processed not in jail, which could
be useful to rc.d developers. Therefore a new syntax is
introduced: "-j any" means any jail while "-j none" means
out of jail. The old syntax is preserved for compatibility,
but now it's deprecated because it's limited and confusing.
Update the respective regression tests. While I'm here,
make the tests more complex but sensitive: Start several
processes, some in jail and some out of jail, so we can
detect that only the right processes are killed by pkill
or matched by pgrep.
Reviewed by: gad, pjd
MFC after: 1 week
2006-11-23 11:55:17 +00:00
|
|
|
makelist(&jidlist, LT_JID, optarg);
|
2005-03-20 10:53:54 +00:00
|
|
|
criteria = 1;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
case 'l':
|
|
|
|
longfmt = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
newest = 1;
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2005-03-20 11:42:26 +00:00
|
|
|
case 'o':
|
|
|
|
oldest = 1;
|
|
|
|
criteria = 1;
|
|
|
|
break;
|
2010-02-12 18:52:24 +00:00
|
|
|
case 'q':
|
|
|
|
if (!pgrep)
|
|
|
|
usage();
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
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)
|
|
|
|
usage();
|
2005-03-20 11:42:26 +00:00
|
|
|
if (newest && oldest)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "Options -n and -o are mutually exclusive");
|
2005-08-25 20:10:47 +00:00
|
|
|
if (pidfile != NULL)
|
|
|
|
pidfromfile = takepid(pidfile, pidfilelock);
|
|
|
|
else {
|
2005-11-16 11:03:00 +00:00
|
|
|
if (pidfilelock) {
|
|
|
|
errx(STATUS_ERROR,
|
|
|
|
"Option -L doesn't make sense without -F");
|
|
|
|
}
|
2005-08-25 20:10:47 +00:00
|
|
|
pidfromfile = -1;
|
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
mypid = getpid();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the list of running processes from the kernel.
|
|
|
|
*/
|
2004-03-26 19:29:57 +00:00
|
|
|
kd = kvm_openfiles(execf, coref, NULL, O_RDONLY, buf);
|
2004-03-25 19:08:48 +00:00
|
|
|
if (kd == NULL)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2004-03-30 15:10:50 +00:00
|
|
|
/*
|
|
|
|
* Use KERN_PROC_PROC instead of KERN_PROC_ALL, since we
|
|
|
|
* just want processes and not individual kernel threads.
|
|
|
|
*/
|
2013-11-10 05:22:29 +00:00
|
|
|
if (pidfromfile >= 0)
|
|
|
|
plist = kvm_getprocs(kd, KERN_PROC_PID, pidfromfile, &nproc);
|
|
|
|
else
|
|
|
|
plist = kvm_getprocs(kd, KERN_PROC_PROC, 0, &nproc);
|
2005-11-16 11:03:00 +00:00
|
|
|
if (plist == NULL) {
|
|
|
|
errx(STATUS_ERROR, "Cannot get process list (%s)",
|
|
|
|
kvm_geterr(kd));
|
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate memory which will be used to keep track of the
|
|
|
|
* selection.
|
|
|
|
*/
|
2005-11-16 11:03:00 +00:00
|
|
|
if ((selected = malloc(nproc)) == NULL) {
|
|
|
|
err(STATUS_ERROR, "Cannot allocate memory for %d processes",
|
|
|
|
nproc);
|
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
memset(selected, 0, nproc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Refine the selection.
|
|
|
|
*/
|
|
|
|
for (; *argv != NULL; argv++) {
|
2005-03-20 11:12:13 +00:00
|
|
|
if ((rv = regcomp(®, *argv, cflags)) != 0) {
|
2004-03-25 19:08:48 +00:00
|
|
|
regerror(rv, ®, buf, sizeof(buf));
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_BADUSAGE,
|
|
|
|
"Cannot compile regular expression `%s' (%s)",
|
|
|
|
*argv, buf);
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
2005-03-20 11:30:26 +00:00
|
|
|
if (PSKIP(kp)) {
|
2004-03-26 01:43:19 +00:00
|
|
|
if (debug_opt > 0)
|
2004-03-26 02:20:52 +00:00
|
|
|
fprintf(stderr, "* Skipped %5d %3d %s\n",
|
2004-03-26 01:43:19 +00:00
|
|
|
kp->ki_pid, kp->ki_uid, kp->ki_comm);
|
2004-03-25 19:08:48 +00:00
|
|
|
continue;
|
2004-03-26 01:43:19 +00:00
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2005-03-20 11:30:26 +00:00
|
|
|
if (matchargs &&
|
|
|
|
(pargv = kvm_getargv(kd, kp, 0)) != NULL) {
|
2004-03-26 00:15:20 +00:00
|
|
|
jsz = 0;
|
|
|
|
while (jsz < sizeof(buf) && *pargv != NULL) {
|
|
|
|
jsz += snprintf(buf + jsz,
|
|
|
|
sizeof(buf) - jsz,
|
2004-03-25 19:08:48 +00:00
|
|
|
pargv[1] != NULL ? "%s " : "%s",
|
|
|
|
pargv[0]);
|
|
|
|
pargv++;
|
|
|
|
}
|
|
|
|
mstr = buf;
|
|
|
|
} else
|
2004-03-25 21:58:56 +00:00
|
|
|
mstr = kp->ki_comm;
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
rv = regexec(®, mstr, 1, ®match, 0);
|
|
|
|
if (rv == 0) {
|
|
|
|
if (fullmatch) {
|
|
|
|
if (regmatch.rm_so == 0 &&
|
2004-03-26 00:36:27 +00:00
|
|
|
regmatch.rm_eo ==
|
|
|
|
(off_t)strlen(mstr))
|
2004-03-25 19:08:48 +00:00
|
|
|
selected[i] = 1;
|
|
|
|
} else
|
|
|
|
selected[i] = 1;
|
|
|
|
} else if (rv != REG_NOMATCH) {
|
|
|
|
regerror(rv, ®, buf, sizeof(buf));
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR,
|
|
|
|
"Regular expression evaluation error (%s)",
|
|
|
|
buf);
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
2004-03-26 01:43:19 +00:00
|
|
|
if (debug_opt > 1) {
|
|
|
|
const char *rv_res = "NoMatch";
|
|
|
|
if (selected[i])
|
|
|
|
rv_res = "Matched";
|
2004-03-26 02:20:52 +00:00
|
|
|
fprintf(stderr, "* %s %5d %3d %s\n", rv_res,
|
2004-03-26 01:43:19 +00:00
|
|
|
kp->ki_pid, kp->ki_uid, mstr);
|
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
regfree(®);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
2005-03-20 11:30:26 +00:00
|
|
|
if (PSKIP(kp))
|
2004-03-25 19:08:48 +00:00
|
|
|
continue;
|
|
|
|
|
2005-03-20 11:03:11 +00:00
|
|
|
if (pidfromfile >= 0 && kp->ki_pid != pidfromfile) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
SLIST_FOREACH(li, &ruidlist, li_chain)
|
2004-03-25 21:58:56 +00:00
|
|
|
if (kp->ki_ruid == (uid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-25 23:35:14 +00:00
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
SLIST_FOREACH(li, &rgidlist, li_chain)
|
2004-03-25 21:58:56 +00:00
|
|
|
if (kp->ki_rgid == (gid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &euidlist, li_chain)
|
2004-03-25 21:58:56 +00:00
|
|
|
if (kp->ki_uid == (uid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &ppidlist, li_chain)
|
2004-03-25 22:08:52 +00:00
|
|
|
if (kp->ki_ppid == (pid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &pgrplist, li_chain)
|
2004-03-25 22:08:52 +00:00
|
|
|
if (kp->ki_pgid == (pid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &tdevlist, li_chain) {
|
|
|
|
if (li->li_number == -1 &&
|
2004-03-25 21:58:56 +00:00
|
|
|
(kp->ki_flag & P_CONTROLT) == 0)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
2004-06-17 17:16:53 +00:00
|
|
|
if (kp->ki_tdev == (dev_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLIST_FOREACH(li, &sidlist, li_chain)
|
2004-03-25 22:08:52 +00:00
|
|
|
if (kp->ki_sid == (pid_t)li->li_number)
|
2004-03-25 19:08:48 +00:00
|
|
|
break;
|
|
|
|
if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-03-20 10:53:54 +00:00
|
|
|
SLIST_FOREACH(li, &jidlist, li_chain) {
|
Fix and extend the -j option to pkill/pgrep WRT the jail
wildcard specifications. Earlier the only wildcard syntax
was "-j 0" for "any jail". There were at least
two shortcomings in it: First, jail ID 0 was abused; it
meant "no jail" in other utils, e.g., ps(1). Second, it
was impossible to match processed not in jail, which could
be useful to rc.d developers. Therefore a new syntax is
introduced: "-j any" means any jail while "-j none" means
out of jail. The old syntax is preserved for compatibility,
but now it's deprecated because it's limited and confusing.
Update the respective regression tests. While I'm here,
make the tests more complex but sensitive: Start several
processes, some in jail and some out of jail, so we can
detect that only the right processes are killed by pkill
or matched by pgrep.
Reviewed by: gad, pjd
MFC after: 1 week
2006-11-23 11:55:17 +00:00
|
|
|
/* A particular jail ID, including 0 (not in jail) */
|
|
|
|
if (kp->ki_jid == (int)li->li_number)
|
|
|
|
break;
|
|
|
|
/* Any jail */
|
|
|
|
if (kp->ki_jid > 0 && li->li_number == -1)
|
|
|
|
break;
|
2005-03-20 10:53:54 +00:00
|
|
|
}
|
|
|
|
if (SLIST_FIRST(&jidlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-08-09 08:38:51 +00:00
|
|
|
SLIST_FOREACH(li, &classlist, li_chain) {
|
|
|
|
/*
|
|
|
|
* We skip P_SYSTEM processes to match ps(1) output.
|
|
|
|
*/
|
|
|
|
if ((kp->ki_flag & P_SYSTEM) == 0 &&
|
|
|
|
strcmp(kp->ki_loginclass, li->li_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (SLIST_FIRST(&classlist) != NULL && li == NULL) {
|
|
|
|
selected[i] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
if (argc == 0)
|
|
|
|
selected[i] = 1;
|
|
|
|
}
|
|
|
|
|
2009-05-17 04:34:14 +00:00
|
|
|
if (!ancestors) {
|
|
|
|
pid = mypid;
|
|
|
|
while (pid) {
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
|
|
|
if (PSKIP(kp))
|
|
|
|
continue;
|
|
|
|
if (kp->ki_pid == pid) {
|
|
|
|
selected[i] = 0;
|
|
|
|
pid = kp->ki_ppid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == nproc) {
|
|
|
|
if (pid == mypid)
|
|
|
|
pid = getppid();
|
|
|
|
else
|
|
|
|
break; /* Maybe we're in a jail ? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-20 11:42:26 +00:00
|
|
|
if (newest || oldest) {
|
2004-03-26 00:15:20 +00:00
|
|
|
best_tval.tv_sec = 0;
|
|
|
|
best_tval.tv_usec = 0;
|
2004-03-25 19:08:48 +00:00
|
|
|
bestidx = -1;
|
|
|
|
|
|
|
|
for (i = 0, kp = plist; i < nproc; i++, kp++) {
|
|
|
|
if (!selected[i])
|
|
|
|
continue;
|
2005-03-20 11:42:26 +00:00
|
|
|
if (bestidx == -1) {
|
|
|
|
/* The first entry of the list which matched. */
|
|
|
|
;
|
|
|
|
} else if (timercmp(&kp->ki_start, &best_tval, >)) {
|
|
|
|
/* This entry is newer than previous "best". */
|
2005-03-20 11:47:44 +00:00
|
|
|
if (oldest) /* but we want the oldest */
|
2005-03-20 11:42:26 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
/* This entry is older than previous "best". */
|
2005-03-20 11:47:44 +00:00
|
|
|
if (newest) /* but we want the newest */
|
2005-03-20 11:42:26 +00:00
|
|
|
continue;
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
2005-03-20 11:42:26 +00:00
|
|
|
/* This entry is better than previous "best" entry. */
|
|
|
|
best_tval.tv_sec = kp->ki_start.tv_sec;
|
|
|
|
best_tval.tv_usec = kp->ki_start.tv_usec;
|
|
|
|
bestidx = i;
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(selected, 0, nproc);
|
|
|
|
if (bestidx != -1)
|
|
|
|
selected[bestidx] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take the appropriate action for each matched process, if any.
|
|
|
|
*/
|
2010-06-20 08:48:30 +00:00
|
|
|
did_action = 0;
|
2004-03-25 19:08:48 +00:00
|
|
|
for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
|
2005-03-20 11:30:26 +00:00
|
|
|
if (PSKIP(kp))
|
2004-03-25 19:08:48 +00:00
|
|
|
continue;
|
|
|
|
if (selected[i]) {
|
2010-06-20 08:48:30 +00:00
|
|
|
if (longfmt && !pgrep) {
|
|
|
|
did_action = 1;
|
|
|
|
printf("kill -%d %d\n", signum, kp->ki_pid);
|
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
if (inverse)
|
|
|
|
continue;
|
|
|
|
} else if (!inverse)
|
|
|
|
continue;
|
2005-11-16 11:03:00 +00:00
|
|
|
rv |= (*action)(kp);
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
2010-06-20 08:48:30 +00:00
|
|
|
if (!did_action && !pgrep && longfmt)
|
|
|
|
fprintf(stderr,
|
|
|
|
"No matching processes belonging to you were found\n");
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
|
|
|
|
}
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static void
|
2004-03-25 19:08:48 +00:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
const char *ustr;
|
|
|
|
|
|
|
|
if (pgrep)
|
2010-02-12 18:52:24 +00:00
|
|
|
ustr = "[-LSfilnoqvx] [-d delim]";
|
2004-03-25 19:08:48 +00:00
|
|
|
else
|
2010-06-20 08:48:30 +00:00
|
|
|
ustr = "[-signal] [-ILfilnovx]";
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
fprintf(stderr,
|
2005-03-20 11:03:11 +00:00
|
|
|
"usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
|
2013-08-09 08:38:51 +00:00
|
|
|
" [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jid]\n"
|
|
|
|
" [-s sid] [-t tty] [-u euid] pattern ...\n",
|
|
|
|
getprogname(), ustr);
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
exit(STATUS_BADUSAGE);
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 10:36:44 +00:00
|
|
|
static void
|
2005-11-16 11:03:00 +00:00
|
|
|
show_process(const struct kinfo_proc *kp)
|
2004-03-25 19:08:48 +00:00
|
|
|
{
|
|
|
|
char **argv;
|
|
|
|
|
2010-02-12 18:52:24 +00:00
|
|
|
if (quiet) {
|
|
|
|
assert(pgrep);
|
|
|
|
return;
|
|
|
|
}
|
2005-11-16 10:36:44 +00:00
|
|
|
if ((longfmt || !pgrep) && matchargs &&
|
2005-03-20 11:30:26 +00:00
|
|
|
(argv = kvm_getargv(kd, kp, 0)) != NULL) {
|
2004-03-25 21:58:56 +00:00
|
|
|
printf("%d ", (int)kp->ki_pid);
|
2004-03-25 19:08:48 +00:00
|
|
|
for (; *argv != NULL; argv++) {
|
|
|
|
printf("%s", *argv);
|
|
|
|
if (argv[1] != NULL)
|
|
|
|
putchar(' ');
|
|
|
|
}
|
2005-11-16 10:36:44 +00:00
|
|
|
} else if (longfmt || !pgrep)
|
2004-03-25 21:58:56 +00:00
|
|
|
printf("%d %s", (int)kp->ki_pid, kp->ki_comm);
|
2004-03-25 19:08:48 +00:00
|
|
|
else
|
2004-03-25 21:58:56 +00:00
|
|
|
printf("%d", (int)kp->ki_pid);
|
2005-11-16 10:36:44 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static int
|
|
|
|
killact(const struct kinfo_proc *kp)
|
2005-11-16 10:36:44 +00:00
|
|
|
{
|
|
|
|
int ch, first;
|
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
/*
|
|
|
|
* Be careful, ask before killing.
|
|
|
|
*/
|
|
|
|
printf("kill ");
|
|
|
|
show_process(kp);
|
|
|
|
printf("? ");
|
|
|
|
fflush(stdout);
|
|
|
|
first = ch = getchar();
|
|
|
|
while (ch != '\n' && ch != EOF)
|
|
|
|
ch = getchar();
|
|
|
|
if (first != 'y' && first != 'Y')
|
2005-11-16 11:03:00 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (kill(kp->ki_pid, signum) == -1) {
|
|
|
|
/*
|
|
|
|
* Check for ESRCH, which indicates that the process
|
|
|
|
* disappeared between us matching it and us
|
|
|
|
* signalling it; don't issue a warning about it.
|
|
|
|
*/
|
|
|
|
if (errno != ESRCH)
|
|
|
|
warn("signalling pid %d", (int)kp->ki_pid);
|
|
|
|
/*
|
|
|
|
* 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-11-16 10:36:44 +00:00
|
|
|
}
|
2005-11-16 11:03:00 +00:00
|
|
|
return (1);
|
2005-11-16 10:36:44 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static int
|
|
|
|
grepact(const struct kinfo_proc *kp)
|
2005-11-16 10:36:44 +00:00
|
|
|
{
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2005-11-16 10:36:44 +00:00
|
|
|
show_process(kp);
|
2010-02-12 18:52:24 +00:00
|
|
|
if (!quiet)
|
|
|
|
printf("%s", delim);
|
2005-11-16 11:03:00 +00:00
|
|
|
return (1);
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static void
|
2004-03-25 19:08:48 +00:00
|
|
|
makelist(struct listhead *head, enum listtype type, char *src)
|
|
|
|
{
|
|
|
|
struct list *li;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
struct stat st;
|
2008-09-28 16:44:15 +00:00
|
|
|
const char *cp;
|
2005-11-16 11:03:00 +00:00
|
|
|
char *sp, *ep, buf[MAXPATHLEN];
|
2004-03-25 19:08:48 +00:00
|
|
|
int empty;
|
|
|
|
|
|
|
|
empty = 1;
|
|
|
|
|
|
|
|
while ((sp = strsep(&src, ",")) != NULL) {
|
|
|
|
if (*sp == '\0')
|
|
|
|
usage();
|
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
if ((li = malloc(sizeof(*li))) == NULL) {
|
|
|
|
err(STATUS_ERROR, "Cannot allocate %zu bytes",
|
|
|
|
sizeof(*li));
|
|
|
|
}
|
|
|
|
|
2004-03-25 19:08:48 +00:00
|
|
|
SLIST_INSERT_HEAD(head, li, li_chain);
|
|
|
|
empty = 0;
|
|
|
|
|
2013-08-09 08:38:51 +00:00
|
|
|
if (type != LT_CLASS)
|
|
|
|
li->li_number = (uid_t)strtol(sp, &ep, 0);
|
|
|
|
|
|
|
|
if (type != LT_CLASS && *ep == '\0') {
|
2004-03-25 19:08:48 +00: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;
|
Fix and extend the -j option to pkill/pgrep WRT the jail
wildcard specifications. Earlier the only wildcard syntax
was "-j 0" for "any jail". There were at least
two shortcomings in it: First, jail ID 0 was abused; it
meant "no jail" in other utils, e.g., ps(1). Second, it
was impossible to match processed not in jail, which could
be useful to rc.d developers. Therefore a new syntax is
introduced: "-j any" means any jail while "-j none" means
out of jail. The old syntax is preserved for compatibility,
but now it's deprecated because it's limited and confusing.
Update the respective regression tests. While I'm here,
make the tests more complex but sensitive: Start several
processes, some in jail and some out of jail, so we can
detect that only the right processes are killed by pkill
or matched by pgrep.
Reviewed by: gad, pjd
MFC after: 1 week
2006-11-23 11:55:17 +00:00
|
|
|
case LT_JID:
|
|
|
|
if (li->li_number < 0)
|
|
|
|
errx(STATUS_BADUSAGE,
|
|
|
|
"Negative jail ID `%s'", sp);
|
|
|
|
/* For compatibility with old -j */
|
|
|
|
if (li->li_number == 0)
|
|
|
|
li->li_number = -1; /* any jail */
|
|
|
|
break;
|
2010-01-04 10:50:17 +00:00
|
|
|
case LT_TTY:
|
|
|
|
if (li->li_number < 0)
|
|
|
|
errx(STATUS_BADUSAGE,
|
|
|
|
"Negative /dev/pts tty `%s'", sp);
|
|
|
|
snprintf(buf, sizeof(buf), _PATH_DEV "pts/%s",
|
|
|
|
sp);
|
|
|
|
if (stat(buf, &st) != -1)
|
|
|
|
goto foundtty;
|
|
|
|
if (errno == ENOENT)
|
|
|
|
errx(STATUS_BADUSAGE, "No such tty: `"
|
|
|
|
_PATH_DEV "pts/%s'", sp);
|
|
|
|
err(STATUS_ERROR, "Cannot access `"
|
|
|
|
_PATH_DEV "pts/%s'", sp);
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case LT_USER:
|
|
|
|
if ((pw = getpwnam(sp)) == NULL)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_BADUSAGE, "Unknown user `%s'", sp);
|
2004-03-25 19:08:48 +00:00
|
|
|
li->li_number = pw->pw_uid;
|
|
|
|
break;
|
|
|
|
case LT_GROUP:
|
|
|
|
if ((gr = getgrnam(sp)) == NULL)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_BADUSAGE, "Unknown group `%s'", sp);
|
2004-03-25 19:08:48 +00:00
|
|
|
li->li_number = gr->gr_gid;
|
|
|
|
break;
|
|
|
|
case LT_TTY:
|
|
|
|
if (strcmp(sp, "-") == 0) {
|
|
|
|
li->li_number = -1;
|
|
|
|
break;
|
2005-11-16 11:03:00 +00:00
|
|
|
} else if (strcmp(sp, "co") == 0) {
|
2004-03-26 00:15:20 +00:00
|
|
|
cp = "console";
|
2005-11-16 11:03:00 +00:00
|
|
|
} else {
|
2004-03-26 00:15:20 +00:00
|
|
|
cp = sp;
|
2005-11-16 11:03:00 +00:00
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2008-09-28 16:44:15 +00:00
|
|
|
snprintf(buf, sizeof(buf), _PATH_DEV "%s", cp);
|
2008-09-30 17:30:39 +00:00
|
|
|
if (stat(buf, &st) != -1)
|
|
|
|
goto foundtty;
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2008-09-30 17:30:39 +00:00
|
|
|
snprintf(buf, sizeof(buf), _PATH_DEV "tty%s", cp);
|
|
|
|
if (stat(buf, &st) != -1)
|
|
|
|
goto foundtty;
|
|
|
|
|
|
|
|
if (errno == ENOENT)
|
|
|
|
errx(STATUS_BADUSAGE, "No such tty: `%s'", sp);
|
|
|
|
err(STATUS_ERROR, "Cannot access `%s'", sp);
|
2004-03-25 19:08:48 +00:00
|
|
|
|
2008-09-30 17:30:39 +00:00
|
|
|
foundtty: if ((st.st_mode & S_IFCHR) == 0)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_BADUSAGE, "Not a tty: `%s'", sp);
|
2004-03-25 19:08:48 +00:00
|
|
|
|
|
|
|
li->li_number = st.st_rdev;
|
|
|
|
break;
|
Fix and extend the -j option to pkill/pgrep WRT the jail
wildcard specifications. Earlier the only wildcard syntax
was "-j 0" for "any jail". There were at least
two shortcomings in it: First, jail ID 0 was abused; it
meant "no jail" in other utils, e.g., ps(1). Second, it
was impossible to match processed not in jail, which could
be useful to rc.d developers. Therefore a new syntax is
introduced: "-j any" means any jail while "-j none" means
out of jail. The old syntax is preserved for compatibility,
but now it's deprecated because it's limited and confusing.
Update the respective regression tests. While I'm here,
make the tests more complex but sensitive: Start several
processes, some in jail and some out of jail, so we can
detect that only the right processes are killed by pkill
or matched by pgrep.
Reviewed by: gad, pjd
MFC after: 1 week
2006-11-23 11:55:17 +00:00
|
|
|
case LT_JID:
|
|
|
|
if (strcmp(sp, "none") == 0)
|
|
|
|
li->li_number = 0;
|
|
|
|
else if (strcmp(sp, "any") == 0)
|
|
|
|
li->li_number = -1;
|
|
|
|
else if (*ep != '\0')
|
|
|
|
errx(STATUS_BADUSAGE,
|
|
|
|
"Invalid jail ID `%s'", sp);
|
|
|
|
break;
|
2013-08-09 08:38:51 +00:00
|
|
|
case LT_CLASS:
|
|
|
|
li->li_number = -1;
|
|
|
|
li->li_name = strdup(sp);
|
|
|
|
if (li->li_name == NULL)
|
|
|
|
err(STATUS_ERROR, "Cannot allocate memory");
|
|
|
|
break;
|
2004-03-25 19:08:48 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2005-11-16 11:03:00 +00:00
|
|
|
}
|
2004-03-25 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty)
|
|
|
|
usage();
|
|
|
|
}
|
2005-03-20 11:03:11 +00:00
|
|
|
|
2005-11-16 11:03:00 +00:00
|
|
|
static int
|
2005-08-25 20:10:47 +00:00
|
|
|
takepid(const char *pidfile, int pidfilelock)
|
2005-03-20 11:03:11 +00:00
|
|
|
{
|
|
|
|
char *endp, line[BUFSIZ];
|
|
|
|
FILE *fh;
|
|
|
|
long rval;
|
2005-03-20 11:47:44 +00:00
|
|
|
|
2005-03-20 11:03:11 +00:00
|
|
|
fh = fopen(pidfile, "r");
|
|
|
|
if (fh == NULL)
|
2005-11-16 11:03:00 +00:00
|
|
|
err(STATUS_ERROR, "Cannot open pidfile `%s'", pidfile);
|
2005-03-20 11:47:44 +00:00
|
|
|
|
2005-08-25 20:10:47 +00:00
|
|
|
if (pidfilelock) {
|
|
|
|
/*
|
|
|
|
* If we can lock pidfile, this means that daemon is not
|
|
|
|
* running, so would be better not to kill some random process.
|
|
|
|
*/
|
|
|
|
if (flock(fileno(fh), LOCK_EX | LOCK_NB) == 0) {
|
|
|
|
(void)fclose(fh);
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "File '%s' can be locked", pidfile);
|
2005-08-25 20:10:47 +00:00
|
|
|
} else {
|
|
|
|
if (errno != EWOULDBLOCK) {
|
|
|
|
errx(STATUS_ERROR,
|
2005-11-16 11:03:00 +00:00
|
|
|
"Error while locking file '%s'", pidfile);
|
2005-08-25 20:10:47 +00:00
|
|
|
}
|
2005-08-24 19:38:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-20 11:03:11 +00:00
|
|
|
if (fgets(line, sizeof(line), fh) == NULL) {
|
|
|
|
if (feof(fh)) {
|
|
|
|
(void)fclose(fh);
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "Pidfile `%s' is empty", pidfile);
|
2005-03-20 11:03:11 +00:00
|
|
|
}
|
|
|
|
(void)fclose(fh);
|
2005-11-16 11:03:00 +00:00
|
|
|
err(STATUS_ERROR, "Cannot read from pid file `%s'", pidfile);
|
2005-03-20 11:03:11 +00:00
|
|
|
}
|
|
|
|
(void)fclose(fh);
|
2005-03-20 11:47:44 +00:00
|
|
|
|
2005-03-20 11:03:11 +00:00
|
|
|
rval = strtol(line, &endp, 10);
|
|
|
|
if (*endp != '\0' && !isspace((unsigned char)*endp))
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile);
|
2005-03-20 11:03:11 +00:00
|
|
|
else if (rval < MIN_PID || rval > MAX_PID)
|
2005-11-16 11:03:00 +00:00
|
|
|
errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile);
|
2005-03-20 11:03:11 +00:00
|
|
|
return (rval);
|
|
|
|
}
|