summaryrefslogtreecommitdiffstats
path: root/bin/pkill
diff options
context:
space:
mode:
Diffstat (limited to 'bin/pkill')
-rw-r--r--bin/pkill/Makefile25
-rw-r--r--bin/pkill/Makefile.depend21
-rw-r--r--bin/pkill/pkill.1294
-rw-r--r--bin/pkill/pkill.c842
-rw-r--r--bin/pkill/tests/Makefile38
-rw-r--r--bin/pkill/tests/pgrep-F_test.sh24
-rw-r--r--bin/pkill/tests/pgrep-LF_test.sh41
-rw-r--r--bin/pkill/tests/pgrep-P_test.sh22
-rw-r--r--bin/pkill/tests/pgrep-U_test.sh38
-rw-r--r--bin/pkill/tests/pgrep-_g_test.sh38
-rw-r--r--bin/pkill/tests/pgrep-_s_test.sh22
-rw-r--r--bin/pkill/tests/pgrep-g_test.sh37
-rw-r--r--bin/pkill/tests/pgrep-i_test.sh24
-rw-r--r--bin/pkill/tests/pgrep-j_test.sh116
-rw-r--r--bin/pkill/tests/pgrep-l_test.sh20
-rw-r--r--bin/pkill/tests/pgrep-n_test.sh24
-rw-r--r--bin/pkill/tests/pgrep-o_test.sh24
-rw-r--r--bin/pkill/tests/pgrep-q_test.sh38
-rw-r--r--bin/pkill/tests/pgrep-s_test.sh37
-rw-r--r--bin/pkill/tests/pgrep-t_test.sh37
-rw-r--r--bin/pkill/tests/pgrep-v_test.sh25
-rw-r--r--bin/pkill/tests/pgrep-x_test.sh36
-rw-r--r--bin/pkill/tests/pkill-F_test.sh27
-rw-r--r--bin/pkill/tests/pkill-LF_test.sh43
-rw-r--r--bin/pkill/tests/pkill-P_test.sh25
-rw-r--r--bin/pkill/tests/pkill-U_test.sh42
-rw-r--r--bin/pkill/tests/pkill-_g_test.sh42
-rw-r--r--bin/pkill/tests/pkill-g_test.sh41
-rw-r--r--bin/pkill/tests/pkill-i_test.sh26
-rw-r--r--bin/pkill/tests/pkill-j_test.sh120
-rw-r--r--bin/pkill/tests/pkill-s_test.sh41
-rw-r--r--bin/pkill/tests/pkill-t_test.sh45
-rw-r--r--bin/pkill/tests/pkill-x_test.sh44
33 files changed, 2319 insertions, 0 deletions
diff --git a/bin/pkill/Makefile b/bin/pkill/Makefile
new file mode 100644
index 0000000..2985b37
--- /dev/null
+++ b/bin/pkill/Makefile
@@ -0,0 +1,25 @@
+# $NetBSD: Makefile,v 1.1 2002/03/01 11:21:58 ad Exp $
+# $FreeBSD$
+
+.include <src.opts.mk>
+
+PROG= pkill
+
+LIBADD= kvm jail
+
+LINKS= ${BINDIR}/pkill ${BINDIR}/pgrep
+MLINKS= pkill.1 pgrep.1
+
+#
+# If considering retirement of these compatibility symlinks,
+# keep in mind that pkill is installed to /usr/bin in other
+# OS types, e.g., NetBSD, OpenBSD, Solaris, and Linux.
+#
+SYMLINKS= ${BINDIR}/pkill /usr/bin/pkill
+SYMLINKS+= ${BINDIR}/pgrep /usr/bin/pgrep
+
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
+.include <bsd.prog.mk>
diff --git a/bin/pkill/Makefile.depend b/bin/pkill/Makefile.depend
new file mode 100644
index 0000000..35df7dc
--- /dev/null
+++ b/bin/pkill/Makefile.depend
@@ -0,0 +1,21 @@
+# $FreeBSD$
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ gnu/lib/csu \
+ gnu/lib/libgcc \
+ include \
+ include/xlocale \
+ lib/${CSU_DIR} \
+ lib/libc \
+ lib/libcompiler_rt \
+ lib/libelf \
+ lib/libjail \
+ lib/libkvm \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/bin/pkill/pkill.1 b/bin/pkill/pkill.1
new file mode 100644
index 0000000..70b7912
--- /dev/null
+++ b/bin/pkill/pkill.1
@@ -0,0 +1,294 @@
+.\" $NetBSD: pkill.1,v 1.8 2003/02/14 15:59:18 grant Exp $
+.\"
+.\" $FreeBSD$
+.\"
+.\" Copyright (c) 2002 The NetBSD Foundation, Inc.
+.\" 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.
+.\"
+.Dd August 21, 2015
+.Dt PKILL 1
+.Os
+.Sh NAME
+.Nm pgrep , pkill
+.Nd find or signal processes by name
+.Sh SYNOPSIS
+.Nm pgrep
+.Op Fl LSafilnoqvx
+.Op Fl F Ar pidfile
+.Op Fl G Ar gid
+.Op Fl M Ar core
+.Op Fl N Ar system
+.Op Fl P Ar ppid
+.Op Fl U Ar uid
+.Op Fl c Ar class
+.Op Fl d Ar delim
+.Op Fl g Ar pgrp
+.Op Fl j Ar jail
+.Op Fl s Ar sid
+.Op Fl t Ar tty
+.Op Fl u Ar euid
+.Ar pattern ...
+.Nm pkill
+.Op Fl Ar signal
+.Op Fl ILafilnovx
+.Op Fl F Ar pidfile
+.Op Fl G Ar gid
+.Op Fl M Ar core
+.Op Fl N Ar system
+.Op Fl P Ar ppid
+.Op Fl U Ar uid
+.Op Fl c Ar class
+.Op Fl g Ar pgrp
+.Op Fl j Ar jail
+.Op Fl s Ar sid
+.Op Fl t Ar tty
+.Op Fl u Ar euid
+.Ar pattern ...
+.Sh DESCRIPTION
+The
+.Nm pgrep
+command searches the process table on the running system and prints the
+process IDs of all processes that match the criteria given on the command
+line.
+.Pp
+The
+.Nm pkill
+command searches the process table on the running system and signals all
+processes that match the criteria given on the command line.
+.Pp
+The following options are available:
+.Bl -tag -width ".Fl F Ar pidfile"
+.It Fl F Ar pidfile
+Restrict matches to a process whose PID is stored in the
+.Ar pidfile
+file.
+.It Fl G Ar gid
+Restrict matches to processes with a real group ID in the comma-separated
+list
+.Ar gid .
+.It Fl I
+Request confirmation before attempting to signal each process.
+.It Fl L
+The
+.Ar pidfile
+file given for the
+.Fl F
+option must be locked with the
+.Xr flock 2
+syscall or created with
+.Xr pidfile 3 .
+.It Fl M Ar core
+Extract values associated with the name list from the specified core
+instead of the currently running system.
+.It Fl N Ar system
+Extract the name list from the specified system instead of the default,
+which is the kernel image the system has booted from.
+.It Fl P Ar ppid
+Restrict matches to processes with a parent process ID in the
+comma-separated list
+.Ar ppid .
+.It Fl S
+Search also in system processes (kernel threads).
+.It Fl U Ar uid
+Restrict matches to processes with a real user ID in the comma-separated
+list
+.Ar uid .
+.It Fl d Ar delim
+Specify a delimiter to be printed between each process ID.
+The default is a newline.
+This option can only be used with the
+.Nm pgrep
+command.
+.It Fl a
+Include process ancestors in the match list.
+By default, the current
+.Nm pgrep
+or
+.Nm pkill
+process and all of its ancestors are excluded (unless
+.Fl v
+is used).
+.It Fl c Ar class
+Restrict matches to processes running with specified login class
+.Ar class .
+.It Fl f
+Match against full argument lists.
+The default is to match against process names.
+.It Fl g Ar pgrp
+Restrict matches to processes with a process group ID in the comma-separated
+list
+.Ar pgrp .
+The value zero is taken to mean the process group ID of the running
+.Nm pgrep
+or
+.Nm pkill
+command.
+.It Fl i
+Ignore case distinctions in both the process table and the supplied pattern.
+.It Fl j Ar jail
+Restrict matches to processes inside the specified jails.
+The argument
+.Ar jail
+may be
+.Dq Li any
+to match processes in any jail,
+.Dq Li none
+to match processes not in jail,
+or a comma-separated list of jail IDs or names.
+.It Fl l
+Long output.
+For
+.Nm pgrep ,
+print the process name in addition to the process ID for each matching
+process.
+If used in conjunction with
+.Fl f ,
+print the process ID and the full argument list for each matching process.
+For
+.Nm pkill ,
+display the kill command used for each process killed.
+.It Fl n
+Select only the newest (most recently started) of the matching processes.
+.It Fl o
+Select only the oldest (least recently started) of the matching processes.
+.It Fl q
+Do not write anything to standard output.
+.It Fl s Ar sid
+Restrict matches to processes with a session ID in the comma-separated
+list
+.Ar sid .
+The value zero is taken to mean the session ID of the running
+.Nm pgrep
+or
+.Nm pkill
+command.
+.It Fl t Ar tty
+Restrict matches to processes associated with a terminal in the
+comma-separated list
+.Ar tty .
+Terminal names may be of the form
+.Pa tty Ns Ar xx
+or the shortened form
+.Ar xx .
+A single dash
+.Pq Ql -
+matches processes not associated with a terminal.
+.It Fl u Ar euid
+Restrict matches to processes with an effective user ID in the
+comma-separated list
+.Ar euid .
+.It Fl v
+Reverse the sense of the matching; display processes that do not match the
+given criteria.
+.It Fl x
+Require an exact match of the process name, or argument list if
+.Fl f
+is given.
+The default is to match any substring.
+.It Fl Ns Ar signal
+A non-negative decimal number or symbolic signal name specifying the signal
+to be sent instead of the default
+.Dv TERM .
+This option is valid only when given as the first argument to
+.Nm pkill .
+.El
+.Pp
+If any
+.Ar pattern
+operands are specified, they are used as regular expressions to match
+the command name or full argument list of each process.
+If the
+.Fl f
+option is not specified, then the
+.Ar pattern
+will attempt to match the command name.
+However, presently
+.Fx
+will only keep track of the first 19 characters of the command
+name for each process.
+Attempts to match any characters after the first 19 of a command name
+will quietly fail.
+.Pp
+Note that a running
+.Nm pgrep
+or
+.Nm pkill
+process will never consider itself nor system processes (kernel threads) as
+a potential match.
+.Sh EXIT STATUS
+The
+.Nm pgrep
+and
+.Nm pkill
+utilities
+return one of the following values upon exit:
+.Bl -tag -width indent
+.It 0
+One or more processes were matched.
+.It 1
+No processes were matched.
+.It 2
+Invalid options were specified on the command line.
+.It 3
+An internal error occurred.
+.El
+.Sh COMPATIBILITY
+Historically the option
+.Dq Fl j Li 0
+means any jail, although in other utilities such as
+.Xr ps 1
+jail ID
+.Li 0
+has the opposite meaning, not in jail.
+Therefore
+.Dq Fl j Li 0
+is deprecated, and its use is discouraged in favor of
+.Dq Fl j Li any .
+.Sh SEE ALSO
+.Xr kill 1 ,
+.Xr killall 1 ,
+.Xr ps 1 ,
+.Xr flock 2 ,
+.Xr kill 2 ,
+.Xr sigaction 2 ,
+.Xr pidfile 3 ,
+.Xr re_format 7
+.\" Xr signal 7
+.Sh HISTORY
+The
+.Nm pkill
+and
+.Nm pgrep
+utilities
+first appeared in
+.Nx 1.6 .
+They are modelled after utilities of the same name that appeared in Sun
+Solaris 7.
+They made their first appearance in
+.Fx 5.3 .
+.Sh AUTHORS
+.An Andrew Doran Aq Mt ad@NetBSD.org
diff --git a/bin/pkill/pkill.c b/bin/pkill/pkill.c
new file mode 100644
index 0000000..8c814ce
--- /dev/null
+++ b/bin/pkill/pkill.c
@@ -0,0 +1,842 @@
+/* $NetBSD: pkill.c,v 1.16 2005/10/10 22:13:20 kleink Exp $ */
+
+/*-
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
+ * 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>
+#include <sys/time.h>
+#include <sys/user.h>
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <paths.h>
+#include <string.h>
+#include <unistd.h>
+#include <signal.h>
+#include <regex.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <err.h>
+#include <pwd.h>
+#include <grp.h>
+#include <errno.h>
+#include <locale.h>
+#include <jail.h>
+
+#define STATUS_MATCH 0
+#define STATUS_NOMATCH 1
+#define STATUS_BADUSAGE 2
+#define STATUS_ERROR 3
+
+#define MIN_PID 5
+#define MAX_PID 99999
+
+/* 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))
+
+enum listtype {
+ LT_GENERIC,
+ LT_USER,
+ LT_GROUP,
+ LT_TTY,
+ LT_PGRP,
+ LT_JAIL,
+ LT_SID,
+ LT_CLASS
+};
+
+struct list {
+ SLIST_ENTRY(list) li_chain;
+ long li_number;
+ char *li_name;
+};
+
+SLIST_HEAD(listhead, list);
+
+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;
+static int quiet;
+static kvm_t *kd;
+static pid_t mypid;
+
+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);
+static struct listhead classlist = SLIST_HEAD_INITIALIZER(classlist);
+
+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);
+
+int
+main(int argc, char **argv)
+{
+ char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile;
+ const char *execf, *coref;
+ int ancestors, debug_opt, did_action;
+ int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock;
+ size_t jsz;
+ int (*action)(const struct kinfo_proc *);
+ struct kinfo_proc *kp;
+ struct list *li;
+ struct timeval best_tval;
+ regex_t reg;
+ regmatch_t regmatch;
+ pid_t pid;
+
+ setlocale(LC_ALL, "");
+
+ 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 {
+ 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++;
+ argc--;
+ }
+ }
+ }
+ }
+
+ ancestors = 0;
+ criteria = 0;
+ debug_opt = 0;
+ pidfile = NULL;
+ pidfilelock = 0;
+ quiet = 0;
+ execf = NULL;
+ coref = _PATH_DEVNULL;
+
+ while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ac:d:fg:ij:lnoqs:t:u:vx")) != -1)
+ switch (ch) {
+ case 'D':
+ debug_opt++;
+ break;
+ case 'F':
+ pidfile = optarg;
+ criteria = 1;
+ break;
+ case 'G':
+ makelist(&rgidlist, LT_GROUP, optarg);
+ criteria = 1;
+ break;
+ case 'I':
+ if (pgrep)
+ usage();
+ interactive = 1;
+ break;
+ case 'L':
+ pidfilelock = 1;
+ break;
+ case 'M':
+ coref = optarg;
+ break;
+ case 'N':
+ execf = optarg;
+ break;
+ case 'P':
+ makelist(&ppidlist, LT_GENERIC, optarg);
+ criteria = 1;
+ break;
+ case 'S':
+ if (!pgrep)
+ usage();
+ kthreads = 1;
+ break;
+ case 'U':
+ makelist(&ruidlist, LT_USER, optarg);
+ criteria = 1;
+ break;
+ case 'a':
+ ancestors++;
+ break;
+ case 'c':
+ makelist(&classlist, LT_CLASS, 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 'j':
+ makelist(&jidlist, LT_JAIL, optarg);
+ criteria = 1;
+ break;
+ case 'l':
+ longfmt = 1;
+ break;
+ case 'n':
+ newest = 1;
+ criteria = 1;
+ break;
+ case 'o':
+ oldest = 1;
+ criteria = 1;
+ break;
+ case 'q':
+ if (!pgrep)
+ usage();
+ quiet = 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:
+ usage();
+ /* NOTREACHED */
+ }
+
+ argc -= optind;
+ argv += optind;
+ if (argc != 0)
+ criteria = 1;
+ if (!criteria)
+ usage();
+ if (newest && oldest)
+ errx(STATUS_ERROR, "Options -n and -o are mutually exclusive");
+ if (pidfile != NULL)
+ pidfromfile = takepid(pidfile, pidfilelock);
+ else {
+ if (pidfilelock) {
+ errx(STATUS_ERROR,
+ "Option -L doesn't make sense without -F");
+ }
+ pidfromfile = -1;
+ }
+
+ mypid = getpid();
+
+ /*
+ * Retrieve the list of running processes from the kernel.
+ */
+ kd = kvm_openfiles(execf, coref, NULL, O_RDONLY, buf);
+ if (kd == NULL)
+ errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
+
+ /*
+ * Use KERN_PROC_PROC instead of KERN_PROC_ALL, since we
+ * just want processes and not individual kernel threads.
+ */
+ if (pidfromfile >= 0)
+ plist = kvm_getprocs(kd, KERN_PROC_PID, pidfromfile, &nproc);
+ else
+ plist = kvm_getprocs(kd, KERN_PROC_PROC, 0, &nproc);
+ if (plist == NULL) {
+ errx(STATUS_ERROR, "Cannot get process list (%s)",
+ kvm_geterr(kd));
+ }
+
+ /*
+ * Allocate memory which will be used to keep track of the
+ * selection.
+ */
+ if ((selected = malloc(nproc)) == NULL) {
+ err(STATUS_ERROR, "Cannot allocate memory for %d processes",
+ nproc);
+ }
+ memset(selected, 0, nproc);
+
+ /*
+ * Refine the selection.
+ */
+ for (; *argv != NULL; argv++) {
+ if ((rv = regcomp(&reg, *argv, cflags)) != 0) {
+ regerror(rv, &reg, buf, sizeof(buf));
+ errx(STATUS_BADUSAGE,
+ "Cannot compile regular expression `%s' (%s)",
+ *argv, buf);
+ }
+
+ for (i = 0, kp = plist; i < nproc; i++, kp++) {
+ if (PSKIP(kp)) {
+ if (debug_opt > 0)
+ fprintf(stderr, "* Skipped %5d %3d %s\n",
+ kp->ki_pid, kp->ki_uid, kp->ki_comm);
+ continue;
+ }
+
+ if (matchargs &&
+ (pargv = kvm_getargv(kd, kp, 0)) != NULL) {
+ jsz = 0;
+ while (jsz < sizeof(buf) && *pargv != NULL) {
+ jsz += snprintf(buf + jsz,
+ sizeof(buf) - jsz,
+ pargv[1] != NULL ? "%s " : "%s",
+ pargv[0]);
+ pargv++;
+ }
+ mstr = buf;
+ } else
+ mstr = kp->ki_comm;
+
+ rv = regexec(&reg, mstr, 1, &regmatch, 0);
+ if (rv == 0) {
+ if (fullmatch) {
+ if (regmatch.rm_so == 0 &&
+ regmatch.rm_eo ==
+ (off_t)strlen(mstr))
+ selected[i] = 1;
+ } else
+ selected[i] = 1;
+ } else if (rv != REG_NOMATCH) {
+ regerror(rv, &reg, buf, sizeof(buf));
+ errx(STATUS_ERROR,
+ "Regular expression evaluation error (%s)",
+ buf);
+ }
+ if (debug_opt > 1) {
+ const char *rv_res = "NoMatch";
+ if (selected[i])
+ rv_res = "Matched";
+ fprintf(stderr, "* %s %5d %3d %s\n", rv_res,
+ kp->ki_pid, kp->ki_uid, mstr);
+ }
+ }
+
+ regfree(&reg);
+ }
+
+ for (i = 0, kp = plist; i < nproc; i++, kp++) {
+ if (PSKIP(kp))
+ continue;
+
+ if (pidfromfile >= 0 && kp->ki_pid != pidfromfile) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &ruidlist, li_chain)
+ if (kp->ki_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->ki_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->ki_uid == (uid_t)li->li_number)
+ break;
+ if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &ppidlist, li_chain)
+ if (kp->ki_ppid == (pid_t)li->li_number)
+ break;
+ if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &pgrplist, li_chain)
+ if (kp->ki_pgid == (pid_t)li->li_number)
+ break;
+ if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &tdevlist, li_chain) {
+ if (li->li_number == -1 &&
+ (kp->ki_flag & P_CONTROLT) == 0)
+ break;
+ if (kp->ki_tdev == (dev_t)li->li_number)
+ break;
+ }
+ if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &sidlist, li_chain)
+ if (kp->ki_sid == (pid_t)li->li_number)
+ break;
+ if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ SLIST_FOREACH(li, &jidlist, li_chain) {
+ /* 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;
+ }
+ if (SLIST_FIRST(&jidlist) != NULL && li == NULL) {
+ selected[i] = 0;
+ continue;
+ }
+
+ 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;
+ }
+
+ if (argc == 0)
+ selected[i] = 1;
+ }
+
+ 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 ? */
+ }
+ }
+ }
+
+ if (newest || oldest) {
+ best_tval.tv_sec = 0;
+ best_tval.tv_usec = 0;
+ bestidx = -1;
+
+ for (i = 0, kp = plist; i < nproc; i++, kp++) {
+ if (!selected[i])
+ continue;
+ 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". */
+ if (oldest) /* but we want the oldest */
+ continue;
+ } else {
+ /* This entry is older than previous "best". */
+ if (newest) /* but we want the newest */
+ continue;
+ }
+ /* 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;
+ }
+
+ memset(selected, 0, nproc);
+ if (bestidx != -1)
+ selected[bestidx] = 1;
+ }
+
+ /*
+ * Take the appropriate action for each matched process, if any.
+ */
+ did_action = 0;
+ for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
+ if (PSKIP(kp))
+ continue;
+ if (selected[i]) {
+ if (longfmt && !pgrep) {
+ did_action = 1;
+ printf("kill -%d %d\n", signum, kp->ki_pid);
+ }
+ if (inverse)
+ continue;
+ } else if (!inverse)
+ continue;
+ rv |= (*action)(kp);
+ }
+ if (!did_action && !pgrep && longfmt)
+ fprintf(stderr,
+ "No matching processes belonging to you were found\n");
+
+ exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
+}
+
+static void
+usage(void)
+{
+ const char *ustr;
+
+ if (pgrep)
+ ustr = "[-LSfilnoqvx] [-d delim]";
+ else
+ ustr = "[-signal] [-ILfilnovx]";
+
+ fprintf(stderr,
+ "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
+ " [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jail]\n"
+ " [-s sid] [-t tty] [-u euid] pattern ...\n",
+ getprogname(), ustr);
+
+ exit(STATUS_BADUSAGE);
+}
+
+static void
+show_process(const struct kinfo_proc *kp)
+{
+ char **argv;
+
+ if (quiet) {
+ assert(pgrep);
+ return;
+ }
+ if ((longfmt || !pgrep) && matchargs &&
+ (argv = kvm_getargv(kd, kp, 0)) != NULL) {
+ printf("%d ", (int)kp->ki_pid);
+ for (; *argv != NULL; argv++) {
+ printf("%s", *argv);
+ if (argv[1] != NULL)
+ putchar(' ');
+ }
+ } else if (longfmt || !pgrep)
+ printf("%d %s", (int)kp->ki_pid, kp->ki_comm);
+ else
+ printf("%d", (int)kp->ki_pid);
+}
+
+static int
+killact(const struct kinfo_proc *kp)
+{
+ 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')
+ 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);
+ }
+ return (1);
+}
+
+static int
+grepact(const struct kinfo_proc *kp)
+{
+
+ show_process(kp);
+ if (!quiet)
+ printf("%s", delim);
+ return (1);
+}
+
+static void
+makelist(struct listhead *head, enum listtype type, char *src)
+{
+ struct list *li;
+ struct passwd *pw;
+ struct group *gr;
+ struct stat st;
+ const char *cp;
+ char *sp, *ep, buf[MAXPATHLEN];
+ int empty;
+
+ empty = 1;
+
+ while ((sp = strsep(&src, ",")) != NULL) {
+ if (*sp == '\0')
+ usage();
+
+ if ((li = malloc(sizeof(*li))) == NULL) {
+ err(STATUS_ERROR, "Cannot allocate %zu bytes",
+ sizeof(*li));
+ }
+
+ SLIST_INSERT_HEAD(head, li, li_chain);
+ empty = 0;
+
+ if (type != LT_CLASS)
+ li->li_number = (uid_t)strtol(sp, &ep, 0);
+
+ if (type != LT_CLASS && *ep == '\0') {
+ 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;
+ case LT_JAIL:
+ 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;
+ 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;
+ default:
+ break;
+ }
+ continue;
+ }
+
+ switch (type) {
+ case LT_USER:
+ if ((pw = getpwnam(sp)) == NULL)
+ errx(STATUS_BADUSAGE, "Unknown user `%s'", sp);
+ li->li_number = pw->pw_uid;
+ break;
+ case LT_GROUP:
+ if ((gr = getgrnam(sp)) == NULL)
+ errx(STATUS_BADUSAGE, "Unknown group `%s'", sp);
+ li->li_number = gr->gr_gid;
+ break;
+ case LT_TTY:
+ if (strcmp(sp, "-") == 0) {
+ li->li_number = -1;
+ break;
+ } else if (strcmp(sp, "co") == 0) {
+ cp = "console";
+ } else {
+ cp = sp;
+ }
+
+ snprintf(buf, sizeof(buf), _PATH_DEV "%s", cp);
+ if (stat(buf, &st) != -1)
+ goto foundtty;
+
+ 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);
+
+foundtty: if ((st.st_mode & S_IFCHR) == 0)
+ errx(STATUS_BADUSAGE, "Not a tty: `%s'", sp);
+
+ li->li_number = st.st_rdev;
+ break;
+ case LT_JAIL: {
+ int jid;
+
+ if (strcmp(sp, "none") == 0)
+ li->li_number = 0;
+ else if (strcmp(sp, "any") == 0)
+ li->li_number = -1;
+ else if ((jid = jail_getid(sp)) != -1)
+ li->li_number = jid;
+ else if (*ep != '\0')
+ errx(STATUS_BADUSAGE,
+ "Invalid jail ID or name `%s'", sp);
+ break;
+ }
+ case LT_CLASS:
+ li->li_number = -1;
+ li->li_name = strdup(sp);
+ if (li->li_name == NULL)
+ err(STATUS_ERROR, "Cannot allocate memory");
+ break;
+ default:
+ usage();
+ }
+ }
+
+ if (empty)
+ usage();
+}
+
+static int
+takepid(const char *pidfile, int pidfilelock)
+{
+ char *endp, line[BUFSIZ];
+ FILE *fh;
+ long rval;
+
+ fh = fopen(pidfile, "r");
+ if (fh == NULL)
+ err(STATUS_ERROR, "Cannot open pidfile `%s'", pidfile);
+
+ 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);
+ errx(STATUS_ERROR, "File '%s' can be locked", pidfile);
+ } else {
+ if (errno != EWOULDBLOCK) {
+ errx(STATUS_ERROR,
+ "Error while locking file '%s'", pidfile);
+ }
+ }
+ }
+
+ if (fgets(line, sizeof(line), fh) == NULL) {
+ if (feof(fh)) {
+ (void)fclose(fh);
+ errx(STATUS_ERROR, "Pidfile `%s' is empty", pidfile);
+ }
+ (void)fclose(fh);
+ err(STATUS_ERROR, "Cannot read from pid file `%s'", pidfile);
+ }
+ (void)fclose(fh);
+
+ rval = strtol(line, &endp, 10);
+ if (*endp != '\0' && !isspace((unsigned char)*endp))
+ errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile);
+ else if (rval < MIN_PID || rval > MAX_PID)
+ errx(STATUS_ERROR, "Invalid pid in file `%s'", pidfile);
+ return (rval);
+}
diff --git a/bin/pkill/tests/Makefile b/bin/pkill/tests/Makefile
new file mode 100644
index 0000000..be46707
--- /dev/null
+++ b/bin/pkill/tests/Makefile
@@ -0,0 +1,38 @@
+# $FreeBSD$
+
+.include <bsd.own.mk>
+
+TAP_TESTS_SH= pgrep-F_test
+TAP_TESTS_SH+= pgrep-LF_test
+TAP_TESTS_SH+= pgrep-P_test
+TAP_TESTS_SH+= pgrep-U_test
+TAP_TESTS_SH+= pgrep-_g_test
+TAP_TESTS_SH+= pgrep-_s_test
+TAP_TESTS_SH+= pgrep-g_test
+TAP_TESTS_SH+= pgrep-i_test
+TAP_TESTS_SH+= pgrep-j_test
+TEST_METADATA.pgrep-j_test+= required_user="root"
+TEST_METADATA.pgrep-j_test+= required_programs="jail jls"
+TAP_TESTS_SH+= pgrep-l_test
+TAP_TESTS_SH+= pgrep-n_test
+TAP_TESTS_SH+= pgrep-o_test
+TAP_TESTS_SH+= pgrep-q_test
+TAP_TESTS_SH+= pgrep-s_test
+TAP_TESTS_SH+= pgrep-t_test
+TAP_TESTS_SH+= pgrep-v_test
+TAP_TESTS_SH+= pgrep-x_test
+TAP_TESTS_SH+= pkill-F_test
+TAP_TESTS_SH+= pkill-LF_test
+TAP_TESTS_SH+= pkill-P_test
+TAP_TESTS_SH+= pkill-U_test
+TAP_TESTS_SH+= pkill-_g_test
+TAP_TESTS_SH+= pkill-g_test
+TAP_TESTS_SH+= pkill-i_test
+TAP_TESTS_SH+= pkill-j_test
+TEST_METADATA.pkill-j_test+= required_user="root"
+TEST_METADATA.pkill-j_test+= required_programs="jail jls"
+TAP_TESTS_SH+= pkill-s_test
+TAP_TESTS_SH+= pkill-t_test
+TAP_TESTS_SH+= pkill-x_test
+
+.include <bsd.test.mk>
diff --git a/bin/pkill/tests/pgrep-F_test.sh b/bin/pkill/tests/pgrep-F_test.sh
new file mode 100644
index 0000000..4d8feaa
--- /dev/null
+++ b/bin/pkill/tests/pgrep-F_test.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -F <pidfile>"
+pidfile=$(pwd)/pidfile.txt
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+echo $chpid > $pidfile
+pid=`pgrep -f -F $pidfile $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill "$chpid"
+rm -f $pidfile
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-LF_test.sh b/bin/pkill/tests/pgrep-LF_test.sh
new file mode 100644
index 0000000..4818869
--- /dev/null
+++ b/bin/pkill/tests/pgrep-LF_test.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -LF <pidfile>"
+pidfile=$(pwd)/pidfile.txt
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+daemon -p $pidfile $sleep 5
+sleep 0.3
+chpid=`cat $pidfile`
+pid=`pgrep -f -L -F $pidfile $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+kill "$chpid"
+
+# Be sure we cannot find process which pidfile is not locked.
+$sleep 5 &
+sleep 0.3
+chpid=$!
+echo $chpid > $pidfile
+pgrep -f -L -F $pidfile $sleep 2>/dev/null
+ec=$?
+case $ec in
+0)
+ echo "not ok 2 - $name"
+ ;;
+*)
+ echo "ok 2 - $name"
+ ;;
+esac
+
+kill "$chpid"
+rm -f $pidfile
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-P_test.sh b/bin/pkill/tests/pgrep-P_test.sh
new file mode 100644
index 0000000..5a5cdcf
--- /dev/null
+++ b/bin/pkill/tests/pgrep-P_test.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -P <ppid>"
+ppid=$$
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -P $ppid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-U_test.sh b/bin/pkill/tests/pgrep-U_test.sh
new file mode 100644
index 0000000..2e7f24d
--- /dev/null
+++ b/bin/pkill/tests/pgrep-U_test.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -U <uid>"
+ruid=`id -ur`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -U $ruid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+kill $chpid
+rm -f $sleep
+
+name="pgrep -U <user>"
+ruid=`id -urn`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -U $ruid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-_g_test.sh b/bin/pkill/tests/pgrep-_g_test.sh
new file mode 100644
index 0000000..fbe7b08
--- /dev/null
+++ b/bin/pkill/tests/pgrep-_g_test.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -G <gid>"
+rgid=`id -gr`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -G $rgid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+kill $chpid
+rm -f $sleep
+
+name="pgrep -G <group>"
+rgid=`id -grn`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -G $rgid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-_s_test.sh b/bin/pkill/tests/pgrep-_s_test.sh
new file mode 100644
index 0000000..ce0bde3
--- /dev/null
+++ b/bin/pkill/tests/pgrep-_s_test.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+exp_pid="$(ps ax | grep '\[idle\]' | awk '{print $1}')"
+
+name="pgrep -S"
+pid=`pgrep -Sx idle`
+if [ "$pid" = "$exp_pid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+pid=`pgrep -x idle`
+if [ "$pid" != "$exp_pid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
diff --git a/bin/pkill/tests/pgrep-g_test.sh b/bin/pkill/tests/pgrep-g_test.sh
new file mode 100644
index 0000000..1414908
--- /dev/null
+++ b/bin/pkill/tests/pgrep-g_test.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -g <pgrp>"
+pgrp=`ps -o tpgid -p $$ | tail -1`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -g $pgrp $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+kill $chpid
+rm -f $sleep
+
+name="pgrep -g 0"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -g 0 $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-i_test.sh b/bin/pkill/tests/pgrep-i_test.sh
new file mode 100644
index 0000000..b9ecfbc
--- /dev/null
+++ b/bin/pkill/tests/pgrep-i_test.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -i"
+sleep=$(pwd)/sleep.txt
+usleep="${sleep}XXX"
+touch $usleep
+lsleep="${sleep}xxx"
+ln -sf /bin/sleep $usleep
+$usleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -i $lsleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill $chpid
+rm -f $sleep $usleep
diff --git a/bin/pkill/tests/pgrep-j_test.sh b/bin/pkill/tests/pgrep-j_test.sh
new file mode 100644
index 0000000..1d62813
--- /dev/null
+++ b/bin/pkill/tests/pgrep-j_test.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+# $FreeBSD$
+
+jail_name_to_jid()
+{
+ local check_name="$1"
+ jls -j "$check_name" -s | tr ' ' '\n' | grep jid= | sed -e 's/.*=//g'
+}
+
+base=pgrep_j_test
+
+if [ `id -u` -ne 0 ]; then
+ echo "1..0 # skip Test needs uid 0."
+ exit 0
+fi
+
+echo "1..4"
+
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+
+name="pgrep -j <jid>"
+sleep_amount=5
+jail -c path=/ name=${base}_1_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_1_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_1_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_1_2.pid $sleep $sleep_amount &
+
+for i in `seq 1 10`; do
+ jid1=$(jail_name_to_jid ${base}_1_1)
+ jid2=$(jail_name_to_jid ${base}_1_2)
+ jid="${jid1},${jid2}"
+ case "$jid" in
+ [0-9]+,[0-9]+)
+ break
+ ;;
+ esac
+ sleep 0.1
+done
+sleep 0.5
+
+pid1="$(pgrep -f -x -j "$jid" "$sleep $sleep_amount" | sort)"
+pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_1_1.pid)" \
+ $(cat ${PWD}/${base}_1_2.pid) | sort)
+if [ "$pid1" = "$pid2" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
+fi
+[ -f ${PWD}/${base}_1_1.pid ] && kill $(cat ${PWD}/${base}_1_1.pid)
+[ -f ${PWD}/${base}_1_2.pid ] && kill $(cat ${PWD}/${base}_1_2.pid)
+wait
+
+name="pgrep -j any"
+sleep_amount=6
+jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_2_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_2_2.pid $sleep $sleep_amount &
+
+sleep 2
+pid1="$(pgrep -f -x -j any "$sleep $sleep_amount" | sort)"
+pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_2_1.pid)" \
+ $(cat ${PWD}/${base}_2_2.pid) | sort)
+if [ "$pid1" = "$pid2" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
+fi
+[ -f ${PWD}/${base}_2_1.pid ] && kill $(cat ${PWD}/${base}_2_1.pid)
+[ -f ${PWD}/${base}_2_2.pid ] && kill $(cat ${PWD}/${base}_2_2.pid)
+wait
+
+name="pgrep -j none"
+sleep_amount=7
+daemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount &
+jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
+sleep 2
+pid="$(pgrep -f -x -j none "$sleep $sleep_amount")"
+if [ "$pid" = "$(cat ${PWD}/${base}_3_1.pid)" ]; then
+ echo "ok 3 - $name"
+else
+ echo "not ok 3 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
+fi
+[ -f ${PWD}/${base}_3_1.pid ] && kill $(cat $PWD/${base}_3_1.pid)
+[ -f ${PWD}/${base}_3_2.pid ] && kill $(cat $PWD/${base}_3_2.pid)
+wait
+
+# test 4 is like test 1 except with jname instead of jid.
+name="pgrep -j <jname>"
+sleep_amount=8
+jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount &
+
+sleep 0.5
+
+jname="${base}_4_1,${base}_4_2"
+pid1="$(pgrep -f -x -j "$jname" "$sleep $sleep_amount" | sort)"
+pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_4_1.pid)" \
+ $(cat ${PWD}/${base}_4_2.pid) | sort)
+if [ "$pid1" = "$pid2" ]; then
+ echo "ok 4 - $name"
+else
+ echo "not ok 4 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
+fi
+[ -f ${PWD}/${base}_4_1.pid ] && kill $(cat ${PWD}/${base}_4_1.pid)
+[ -f ${PWD}/${base}_4_2.pid ] && kill $(cat ${PWD}/${base}_4_2.pid)
+wait
+
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-l_test.sh b/bin/pkill/tests/pgrep-l_test.sh
new file mode 100644
index 0000000..49273b3
--- /dev/null
+++ b/bin/pkill/tests/pgrep-l_test.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -l"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pid=$!
+if [ "$pid $sleep 5" = "`pgrep -f -l $sleep`" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill $pid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-n_test.sh b/bin/pkill/tests/pgrep-n_test.sh
new file mode 100644
index 0000000..1b6fe83
--- /dev/null
+++ b/bin/pkill/tests/pgrep-n_test.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -n"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+oldpid=$!
+$sleep 5 &
+sleep 0.3
+newpid=$!
+pid=`pgrep -f -n $sleep`
+if [ "$pid" = "$newpid" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill $oldpid
+kill $newpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-o_test.sh b/bin/pkill/tests/pgrep-o_test.sh
new file mode 100644
index 0000000..250f230
--- /dev/null
+++ b/bin/pkill/tests/pgrep-o_test.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pgrep -o"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+oldpid=$!
+$sleep 5 &
+sleep 0.3
+newpid=$!
+pid=`pgrep -f -o $sleep`
+if [ "$pid" = "$oldpid" ]; then
+ echo "ok - $name"
+else
+ echo "not ok - $name"
+fi
+kill $oldpid
+kill $newpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-q_test.sh b/bin/pkill/tests/pgrep-q_test.sh
new file mode 100644
index 0000000..2626a5e
--- /dev/null
+++ b/bin/pkill/tests/pgrep-q_test.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..4"
+
+name="pgrep -q"
+sleep0=$(pwd)/sleep0.txt
+sleep1=$(pwd)/sleep1.txt
+ln -sf /bin/sleep $sleep0
+$sleep0 5 &
+sleep 0.3
+pid=$!
+out="`pgrep -q -f $sleep0 2>&1`"
+if [ $? -eq 0 ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+if [ -z "${out}" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+out="`pgrep -q -f $sleep1 2>&1`"
+if [ $? -ne 0 ]; then
+ echo "ok 3 - $name"
+else
+ echo "not ok 3 - $name"
+fi
+if [ -z "${out}" ]; then
+ echo "ok 4 - $name"
+else
+ echo "not ok 4 - $name"
+fi
+kill $pid
+rm -f $sleep0 $sleep1
diff --git a/bin/pkill/tests/pgrep-s_test.sh b/bin/pkill/tests/pgrep-s_test.sh
new file mode 100644
index 0000000..82c1ef9
--- /dev/null
+++ b/bin/pkill/tests/pgrep-s_test.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -s <sid>"
+sid=`ps -o tsid -p $$ | tail -1`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -s $sid $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+kill $chpid
+rm -f $sleep
+
+name="pgrep -s 0"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -s 0 $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-t_test.sh b/bin/pkill/tests/pgrep-t_test.sh
new file mode 100644
index 0000000..a8527d5
--- /dev/null
+++ b/bin/pkill/tests/pgrep-t_test.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -t <tty>"
+tty=`ps -x -o tty -p $$ | tail -1`
+if [ "$tty" = "??" -o "$tty" = "-" ]; then
+ tty="-"
+ ttyshort="-"
+else
+ case $tty in
+ pts/*) ttyshort=`echo $tty | cut -c 5-` ;;
+ *) ttyshort=`echo $tty | cut -c 4-` ;;
+ esac
+fi
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+chpid=$!
+pid=`pgrep -f -t $tty $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+pid=`pgrep -f -t $ttyshort $sleep`
+if [ "$pid" = "$chpid" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $chpid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-v_test.sh b/bin/pkill/tests/pgrep-v_test.sh
new file mode 100644
index 0000000..b9835bc
--- /dev/null
+++ b/bin/pkill/tests/pgrep-v_test.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pgrep -v"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pid=$!
+if [ -z "`pgrep -f -v $sleep | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+if [ ! -z "`pgrep -f -v -x x | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+kill $pid
+rm -f $sleep
diff --git a/bin/pkill/tests/pgrep-x_test.sh b/bin/pkill/tests/pgrep-x_test.sh
new file mode 100644
index 0000000..1defde2
--- /dev/null
+++ b/bin/pkill/tests/pgrep-x_test.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..4"
+
+name="pgrep -x"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pid=$!
+if [ ! -z "`pgrep -x sleep | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+if [ -z "`pgrep -x slee | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+name="pgrep -x -f"
+if [ ! -z "`pgrep -x -f ''"$sleep"' 5' | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 3 - $name"
+else
+ echo "not ok 3 - $name"
+fi
+if [ -z "`pgrep -x -f ''"$sleep"' ' | egrep '^'"$pid"'$'`" ]; then
+ echo "ok 4 - $name"
+else
+ echo "not ok 4 - $name"
+fi
+kill $pid
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-F_test.sh b/bin/pkill/tests/pkill-F_test.sh
new file mode 100644
index 0000000..48ad474
--- /dev/null
+++ b/bin/pkill/tests/pkill-F_test.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pkill -F <pidfile>"
+pidfile=$(pwd)/pidfile.txt
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+echo $! > $pidfile
+pkill -f -F $pidfile $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok - $name"
+ ;;
+*)
+ echo "not ok - $name"
+ ;;
+esac
+
+rm -f $pidfile
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-LF_test.sh b/bin/pkill/tests/pkill-LF_test.sh
new file mode 100644
index 0000000..fcafd0a
--- /dev/null
+++ b/bin/pkill/tests/pkill-LF_test.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -LF <pidfile>"
+pidfile=$(pwd)/pidfile.txt
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+daemon -p $pidfile $sleep 5
+sleep 0.3
+pkill -f -L -F $pidfile $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+
+# Be sure we cannot kill process which pidfile is not locked.
+$sleep 5 &
+sleep 0.3
+chpid=$!
+echo $chpid > $pidfile
+pkill -f -L -F $pidfile $sleep 2>/dev/null
+ec=$?
+case $ec in
+0)
+ echo "not ok 2 - $name"
+ ;;
+*)
+ echo "ok 2 - $name"
+ ;;
+esac
+
+kill "$chpid"
+rm -f $pidfile
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-P_test.sh b/bin/pkill/tests/pkill-P_test.sh
new file mode 100644
index 0000000..2030710
--- /dev/null
+++ b/bin/pkill/tests/pkill-P_test.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pkill -P <ppid>"
+ppid=$$
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -P $ppid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok - $name"
+ ;;
+*)
+ echo "not ok - $name"
+ ;;
+esac
+
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-U_test.sh b/bin/pkill/tests/pkill-U_test.sh
new file mode 100644
index 0000000..04395e5
--- /dev/null
+++ b/bin/pkill/tests/pkill-U_test.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -U <uid>"
+ruid=`id -ur`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -U $ruid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+rm -f $sleep
+
+name="pkill -U <user>"
+ruid=`id -urn`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -U $ruid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 2 - $name"
+ ;;
+*)
+ echo "not ok 2 - $name"
+ ;;
+esac
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-_g_test.sh b/bin/pkill/tests/pkill-_g_test.sh
new file mode 100644
index 0000000..1739ac8
--- /dev/null
+++ b/bin/pkill/tests/pkill-_g_test.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -G <gid>"
+rgid=`id -gr`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -G $rgid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+rm -f $sleep
+
+name="pkill -G <group>"
+rgid=`id -grn`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -G $rgid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 2 - $name"
+ ;;
+*)
+ echo "not ok 2 - $name"
+ ;;
+esac
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-g_test.sh b/bin/pkill/tests/pkill-g_test.sh
new file mode 100644
index 0000000..29d6ab7
--- /dev/null
+++ b/bin/pkill/tests/pkill-g_test.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -g <pgrp>"
+pgrp=`ps -o tpgid -p $$ | tail -1`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -g $pgrp $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+rm -f $sleep
+
+name="pkill -g 0"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -g 0 $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 2 - $name"
+ ;;
+*)
+ echo "not ok 2 - $name"
+ ;;
+esac
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-i_test.sh b/bin/pkill/tests/pkill-i_test.sh
new file mode 100644
index 0000000..b9b8df8
--- /dev/null
+++ b/bin/pkill/tests/pkill-i_test.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..1"
+
+name="pkill -i"
+sleep=$(pwd)/sleep.txt
+usleep="${sleep}XXX"
+touch $usleep
+lsleep="${sleep}xxx"
+ln -sf /bin/sleep $usleep
+$usleep 5 &
+sleep 0.3
+pkill -f -i $lsleep
+ec=$?
+case $ec in
+0)
+ echo "ok - $name"
+ ;;
+*)
+ echo "not ok - $name"
+ ;;
+esac
+rm -f $sleep $usleep
diff --git a/bin/pkill/tests/pkill-j_test.sh b/bin/pkill/tests/pkill-j_test.sh
new file mode 100644
index 0000000..0af24cf
--- /dev/null
+++ b/bin/pkill/tests/pkill-j_test.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+# $FreeBSD$
+
+jail_name_to_jid()
+{
+ local check_name="$1"
+ jls -j "$check_name" -s | tr ' ' '\n' | grep jid= | sed -e 's/.*=//g'
+}
+
+base=pkill_j_test
+
+if [ `id -u` -ne 0 ]; then
+ echo "1..0 # skip Test needs uid 0."
+ exit 0
+fi
+
+echo "1..4"
+
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+
+name="pkill -j <jid>"
+sleep_amount=5
+jail -c path=/ name=${base}_1_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_1_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_1_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_1_2.pid $sleep $sleep_amount &
+
+$sleep $sleep_amount &
+
+for i in `seq 1 10`; do
+ jid1=$(jail_name_to_jid ${base}_1_1)
+ jid2=$(jail_name_to_jid ${base}_1_2)
+ jid="${jid1},${jid2}"
+ case "$jid" in
+ [0-9]+,[0-9]+)
+ break
+ ;;
+ esac
+ sleep 0.1
+done
+sleep 0.5
+
+if pkill -f -j "$jid" $sleep && sleep 0.5 &&
+ ! -f ${PWD}/${base}_1_1.pid &&
+ ! -f ${PWD}/${base}_1_2.pid ; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi 2>/dev/null
+[ -f ${PWD}/${base}_1_1.pid ] && kill $(cat ${PWD}/${base}_1_1.pid)
+[ -f ${PWD}/${base}_1_2.pid ] && kill $(cat ${PWD}/${base}_1_2.pid)
+wait
+
+name="pkill -j any"
+sleep_amount=6
+jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_2_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_2_2.pid $sleep $sleep_amount &
+
+$sleep $sleep_amount &
+chpid3=$!
+sleep 0.5
+if pkill -f -j any $sleep && sleep 0.5 &&
+ [ ! -f ${PWD}/${base}_2_1.pid -a
+ ! -f ${PWD}/${base}_2_2.pid ] && kill $chpid3; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi 2>/dev/null
+[ -f ${PWD}/${base}_2_1.pid ] && kill $(cat ${PWD}/${base}_2_1.pid)
+[ -f ${PWD}/${base}_2_2.pid ] && kill $(cat ${PWD}/${base}_2_2.pid)
+wait
+
+name="pkill -j none"
+sleep_amount=7
+daemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount
+jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
+sleep 1
+if pkill -f -j none "$sleep $sleep_amount" && sleep 1 &&
+ [ ! -f ${PWD}/${base}_3_1.pid -a -f ${PWD}/${base}_3_2.pid ] ; then
+ echo "ok 3 - $name"
+else
+ ls ${PWD}/*.pid
+ echo "not ok 3 - $name"
+fi 2>/dev/null
+[ -f ${PWD}/${base}_3_1.pid ] && kill $(cat ${base}_3_1.pid)
+[ -f ${PWD}/${base}_3_2.pid ] && kill $(cat ${base}_3_2.pid)
+wait
+
+# test 4 is like test 1 except with jname instead of jid.
+name="pkill -j <jname>"
+sleep_amount=8
+jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount &
+
+jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \
+ command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount &
+
+$sleep $sleep_amount &
+
+sleep 0.5
+
+jname="${base}_4_1,${base}_4_2"
+if pkill -f -j "$jname" $sleep && sleep 0.5 &&
+ ! -f ${PWD}/${base}_4_1.pid &&
+ ! -f ${PWD}/${base}_4_2.pid ; then
+ echo "ok 4 - $name"
+else
+ echo "not ok 4 - $name"
+fi 2>/dev/null
+[ -f ${PWD}/${base}_4_1.pid ] && kill $(cat ${PWD}/${base}_4_1.pid)
+[ -f ${PWD}/${base}_4_2.pid ] && kill $(cat ${PWD}/${base}_4_2.pid)
+wait
+
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-s_test.sh b/bin/pkill/tests/pkill-s_test.sh
new file mode 100644
index 0000000..0a9587b
--- /dev/null
+++ b/bin/pkill/tests/pkill-s_test.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -s <sid>"
+sid=`ps -o tsid -p $$ | tail -1`
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -s $sid $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+rm -f $sleep
+
+name="pkill -s 0"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -s 0 $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 2 - $name"
+ ;;
+*)
+ echo "not ok 2 - $name"
+ ;;
+esac
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-t_test.sh b/bin/pkill/tests/pkill-t_test.sh
new file mode 100644
index 0000000..07f511b
--- /dev/null
+++ b/bin/pkill/tests/pkill-t_test.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..2"
+
+name="pkill -t <tty>"
+tty=`ps -x -o tty -p $$ | tail -1`
+if [ "$tty" = "??" -o "$tty" = "-" ]; then
+ tty="-"
+ ttyshort="-"
+else
+ case $tty in
+ pts/*) ttyshort=`echo $tty | cut -c 5-` ;;
+ *) ttyshort=`echo $tty | cut -c 4-` ;;
+ esac
+fi
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -f -t $tty $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 1 - $name"
+ ;;
+*)
+ echo "not ok 1 - $name"
+ ;;
+esac
+$sleep 5 &
+sleep 0.3
+pkill -f -t $ttyshort $sleep
+ec=$?
+case $ec in
+0)
+ echo "ok 2 - $name"
+ ;;
+*)
+ echo "not ok 2 - $name"
+ ;;
+esac
+rm -f $sleep
diff --git a/bin/pkill/tests/pkill-x_test.sh b/bin/pkill/tests/pkill-x_test.sh
new file mode 100644
index 0000000..5ff0d68
--- /dev/null
+++ b/bin/pkill/tests/pkill-x_test.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+# $FreeBSD$
+
+base=`basename $0`
+
+echo "1..4"
+
+name="pkill -x"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -x slee -P $$
+if [ $? -ne 0 ]; then
+ echo "ok 1 - $name"
+else
+ echo "not ok 1 - $name"
+fi
+pkill -x sleep -P $$
+if [ $? -eq 0 ]; then
+ echo "ok 2 - $name"
+else
+ echo "not ok 2 - $name"
+fi
+rm -f $sleep
+
+name="pkill -x -f"
+sleep=$(pwd)/sleep.txt
+ln -sf /bin/sleep $sleep
+$sleep 5 &
+sleep 0.3
+pkill -x -f "$sleep " -P $$
+if [ $? -ne 0 ]; then
+ echo "ok 3 - $name"
+else
+ echo "not ok 3 - $name"
+fi
+pkill -x -f "$sleep 5" -P $$
+if [ $? -eq 0 ]; then
+ echo "ok 4 - $name"
+else
+ echo "not ok 4 - $name"
+fi
+rm -f $sleep
OpenPOWER on IntegriCloud