2002-02-02 04:17:29 +05:30
|
|
|
/* emacs settings: -*- c-basic-offset: 8 tab-width: 8 -*-
|
|
|
|
*
|
|
|
|
* pgrep/pkill -- utilities to filter the process table
|
|
|
|
*
|
|
|
|
* Copyright 2000 Kjetil Torgrim Homme <kjetilho@ifi.uio.no>
|
|
|
|
*
|
|
|
|
* May be distributed under the conditions of the
|
|
|
|
* GNU General Public License; a copy is in COPYING
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
|
|
|
* Changes by Albert Cahalan, 2002.
|
|
|
|
*
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-10-23 13:23:16 +05:30
|
|
|
#include <limits.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "proc/readproc.h"
|
|
|
|
#include "proc/sig.h"
|
|
|
|
#include "proc/devname.h"
|
|
|
|
#include "proc/sysinfo.h"
|
2002-10-04 04:38:14 +05:30
|
|
|
#include "proc/version.h" /* procps_version */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
static int i_am_pkill = 0;
|
2002-10-12 09:55:57 +05:30
|
|
|
static const char *progname = "pgrep";
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
union el {
|
|
|
|
long num;
|
|
|
|
char * str;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* User supplied arguments */
|
|
|
|
|
|
|
|
static int opt_full = 0;
|
|
|
|
static int opt_long = 0;
|
2002-10-23 13:23:16 +05:30
|
|
|
static int opt_oldest = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
static int opt_newest = 0;
|
|
|
|
static int opt_negate = 0;
|
|
|
|
static int opt_exact = 0;
|
|
|
|
static int opt_signal = SIGTERM;
|
|
|
|
|
2002-10-12 09:55:57 +05:30
|
|
|
static const char *opt_delim = "\n";
|
2002-02-02 04:17:29 +05:30
|
|
|
static union el *opt_pgrp = NULL;
|
2002-12-09 01:58:40 +05:30
|
|
|
static union el *opt_rgid = NULL;
|
2002-02-02 04:17:29 +05:30
|
|
|
static union el *opt_ppid = NULL;
|
|
|
|
static union el *opt_sid = NULL;
|
|
|
|
static union el *opt_term = NULL;
|
|
|
|
static union el *opt_euid = NULL;
|
2002-12-09 01:58:40 +05:30
|
|
|
static union el *opt_ruid = NULL;
|
2002-02-02 04:17:29 +05:30
|
|
|
static char *opt_pattern = NULL;
|
|
|
|
|
|
|
|
|
2002-12-09 13:23:09 +05:30
|
|
|
static int usage (int opt) NORETURN;
|
|
|
|
static int usage (int opt)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (i_am_pkill)
|
2002-10-23 13:23:16 +05:30
|
|
|
fprintf (stderr, "Usage: pkill [-SIGNAL] [-fvx] ");
|
2002-02-02 05:10:38 +05:30
|
|
|
else
|
2002-10-23 13:23:16 +05:30
|
|
|
fprintf (stderr, "Usage: pgrep [-flvx] [-d DELIM] ");
|
|
|
|
fprintf (stderr, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n"
|
2002-02-02 04:17:29 +05:30
|
|
|
"\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] "
|
|
|
|
"[PATTERN]\n");
|
|
|
|
exit (opt == '?' ? 0 : 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static union el *
|
2002-11-26 03:06:43 +05:30
|
|
|
split_list (const char *restrict str, int (*convert)(const char *, union el *))
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
char *copy = strdup (str);
|
|
|
|
char *ptr = copy;
|
|
|
|
char *sep_pos;
|
|
|
|
int i = 1, size = 32;
|
|
|
|
union el *list;
|
|
|
|
|
|
|
|
list = malloc (size * sizeof (union el));
|
|
|
|
if (list == NULL)
|
|
|
|
exit (3);
|
|
|
|
|
|
|
|
do {
|
2002-11-26 03:06:43 +05:30
|
|
|
sep_pos = strchr (ptr, ',');
|
2002-02-02 04:17:29 +05:30
|
|
|
if (sep_pos)
|
|
|
|
*sep_pos = 0;
|
|
|
|
if (convert (ptr, &list[i]))
|
|
|
|
++i;
|
|
|
|
else
|
|
|
|
exit (2);
|
|
|
|
if (i == size) {
|
|
|
|
size *= 2;
|
|
|
|
list = realloc (list, size * sizeof (union el));
|
|
|
|
if (list == NULL)
|
|
|
|
exit (3);
|
|
|
|
}
|
|
|
|
if (sep_pos)
|
|
|
|
ptr = sep_pos + 1;
|
|
|
|
} while (sep_pos);
|
|
|
|
|
|
|
|
free (copy);
|
|
|
|
if (i == 1) {
|
|
|
|
free (list);
|
|
|
|
list = NULL;
|
|
|
|
} else {
|
|
|
|
list[0].num = i - 1;
|
|
|
|
}
|
|
|
|
return (list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* strict_atol returns a Boolean: TRUE if the input string contains a
|
|
|
|
plain number, FALSE if there are any non-digits. */
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
strict_atol (const char *restrict str, long *restrict value)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int sign = 1;
|
|
|
|
|
|
|
|
if (*str == '+')
|
|
|
|
++str;
|
|
|
|
else if (*str == '-') {
|
|
|
|
++str;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; *str; ++str) {
|
|
|
|
if (! isdigit (*str))
|
|
|
|
return (0);
|
|
|
|
res *= 10;
|
|
|
|
res += *str - '0';
|
|
|
|
}
|
|
|
|
*value = sign * res;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_uid (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
|
|
|
|
if (strict_atol (name, &e->num))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
pwd = getpwnam (name);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
fprintf (stderr, "%s: invalid user name: %s\n",
|
|
|
|
progname, name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
e->num = pwd->pw_uid;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_gid (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
|
|
|
|
if (strict_atol (name, &e->num))
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
grp = getgrnam (name);
|
|
|
|
if (grp == NULL) {
|
|
|
|
fprintf (stderr, "%s: invalid group name: %s\n",
|
|
|
|
progname, name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
e->num = grp->gr_gid;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_pgrp (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
|
|
|
fprintf (stderr, "%s: invalid process group: %s\n",
|
|
|
|
progname, name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (e->num == 0)
|
|
|
|
e->num = getpgrp ();
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_sid (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
|
|
|
fprintf (stderr, "%s: invalid session id: %s\n",
|
|
|
|
progname, name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (e->num == 0)
|
|
|
|
e->num = getsid (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_num (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
|
|
|
fprintf (stderr, "%s: not a number: %s\n",
|
|
|
|
progname, name);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
conv_str (const char *restrict name, union el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
e->str = strdup (name);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
match_numlist (long value, const union el *restrict list)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
if (list == NULL)
|
|
|
|
found = 0;
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = list[0].num; i > 0; i--) {
|
|
|
|
if (list[i].num == value)
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-11-26 03:06:43 +05:30
|
|
|
match_strlist (const char *restrict value, const union el *restrict list)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
if (list == NULL)
|
|
|
|
found = 0;
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = list[0].num; i > 0; i--) {
|
|
|
|
if (! strcmp (list[i].str, value))
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-11-26 03:06:43 +05:30
|
|
|
output_numlist (const union el *restrict list)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < list[0].num; i++)
|
|
|
|
printf ("%ld%s", list[i].num, opt_delim);
|
|
|
|
|
|
|
|
if (list[0].num)
|
|
|
|
printf ("%ld\n", list[i].num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-11-26 03:06:43 +05:30
|
|
|
output_strlist (const union el *restrict list)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2002-12-21 19:43:33 +05:30
|
|
|
// FIXME: escape codes
|
2002-02-02 04:17:29 +05:30
|
|
|
int i;
|
|
|
|
for (i = 1; i < list[0].num; i++)
|
|
|
|
printf ("%s%s", list[i].str, opt_delim);
|
|
|
|
|
|
|
|
if (list[0].num)
|
|
|
|
printf ("%s\n", list[i].str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PROCTAB *
|
2002-09-30 12:41:30 +05:30
|
|
|
do_openproc (void)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
PROCTAB *ptp;
|
2002-12-09 01:58:40 +05:30
|
|
|
int flags = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
if (opt_pattern || opt_full)
|
2002-10-09 09:58:48 +05:30
|
|
|
flags |= PROC_FILLCOM;
|
2002-12-09 01:58:40 +05:30
|
|
|
if (opt_ruid || opt_rgid)
|
2002-02-02 04:17:29 +05:30
|
|
|
flags |= PROC_FILLSTATUS;
|
2002-12-09 01:58:40 +05:30
|
|
|
if (opt_oldest || opt_newest || opt_pgrp || opt_sid || opt_term)
|
|
|
|
flags |= PROC_FILLSTAT;
|
|
|
|
if (!(flags & PROC_FILLSTAT))
|
|
|
|
flags |= PROC_FILLSTATUS; // FIXME: need one, and PROC_FILLANY broken
|
2002-02-02 04:17:29 +05:30
|
|
|
if (opt_euid && !opt_negate) {
|
|
|
|
int num = opt_euid[0].num;
|
|
|
|
int i = num;
|
|
|
|
uid_t *uids = malloc (num * sizeof (uid_t));
|
|
|
|
if (uids == NULL)
|
|
|
|
exit (3);
|
|
|
|
while (i-- > 0) {
|
|
|
|
uids[i] = opt_euid[i+1].num;
|
|
|
|
}
|
|
|
|
flags |= PROC_UID;
|
|
|
|
ptp = openproc (flags, uids, num);
|
|
|
|
} else {
|
|
|
|
ptp = openproc (flags);
|
|
|
|
}
|
|
|
|
return (ptp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static regex_t *
|
2002-09-30 12:41:30 +05:30
|
|
|
do_regcomp (void)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
regex_t *preg = NULL;
|
|
|
|
|
|
|
|
if (opt_pattern) {
|
|
|
|
char *re;
|
|
|
|
char errbuf[256];
|
|
|
|
int re_err;
|
|
|
|
|
|
|
|
preg = malloc (sizeof (regex_t));
|
|
|
|
if (preg == NULL)
|
|
|
|
exit (3);
|
|
|
|
if (opt_exact) {
|
|
|
|
re = malloc (strlen (opt_pattern) + 5);
|
|
|
|
if (re == NULL)
|
|
|
|
exit (3);
|
|
|
|
sprintf (re, "^(%s)$", opt_pattern);
|
|
|
|
} else {
|
|
|
|
re = opt_pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
re_err = regcomp (preg, re, REG_EXTENDED | REG_NOSUB);
|
|
|
|
if (re_err) {
|
|
|
|
regerror (re_err, preg, errbuf, sizeof(errbuf));
|
2005-10-31 05:15:47 +05:30
|
|
|
fputs(errbuf,stderr);
|
2002-02-02 04:17:29 +05:30
|
|
|
exit (2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return preg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static union el *
|
2002-09-30 12:41:30 +05:30
|
|
|
select_procs (void)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
PROCTAB *ptp;
|
|
|
|
proc_t task;
|
2002-10-23 13:23:16 +05:30
|
|
|
unsigned long long saved_start_time; // for new/old support
|
|
|
|
pid_t saved_pid = 0; // for new/old support
|
2002-02-02 04:17:29 +05:30
|
|
|
int matches = 0;
|
|
|
|
int size = 32;
|
|
|
|
regex_t *preg;
|
|
|
|
pid_t myself = getpid();
|
|
|
|
union el *list;
|
|
|
|
char cmd[4096];
|
|
|
|
|
|
|
|
list = malloc (size * sizeof (union el));
|
|
|
|
if (list == NULL)
|
|
|
|
exit (3);
|
|
|
|
|
|
|
|
ptp = do_openproc ();
|
|
|
|
preg = do_regcomp ();
|
2002-10-23 13:23:16 +05:30
|
|
|
|
|
|
|
if (opt_newest) saved_start_time = 0ULL;
|
|
|
|
if (opt_oldest) saved_start_time = ~0ULL;
|
|
|
|
if (opt_newest) saved_pid = 0;
|
|
|
|
if (opt_oldest) saved_pid = INT_MAX;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
memset (&task, 0, sizeof (task));
|
|
|
|
while (readproc (ptp, &task)) {
|
|
|
|
int match = 1;
|
|
|
|
|
2003-09-20 13:59:55 +05:30
|
|
|
if (task.XXXID == myself)
|
2002-02-02 04:17:29 +05:30
|
|
|
continue;
|
2002-10-23 13:23:16 +05:30
|
|
|
else if (opt_newest && task.start_time < saved_start_time)
|
|
|
|
match = 0;
|
|
|
|
else if (opt_oldest && task.start_time > saved_start_time)
|
2002-02-02 04:17:29 +05:30
|
|
|
match = 0;
|
|
|
|
else if (opt_ppid && ! match_numlist (task.ppid, opt_ppid))
|
|
|
|
match = 0;
|
|
|
|
else if (opt_pgrp && ! match_numlist (task.pgrp, opt_pgrp))
|
|
|
|
match = 0;
|
|
|
|
else if (opt_euid && ! match_numlist (task.euid, opt_euid))
|
|
|
|
match = 0;
|
2002-12-09 01:58:40 +05:30
|
|
|
else if (opt_ruid && ! match_numlist (task.ruid, opt_ruid))
|
2002-02-02 04:17:29 +05:30
|
|
|
match = 0;
|
2002-12-09 01:58:40 +05:30
|
|
|
else if (opt_rgid && ! match_numlist (task.rgid, opt_rgid))
|
2002-02-02 04:17:29 +05:30
|
|
|
match = 0;
|
|
|
|
else if (opt_sid && ! match_numlist (task.session, opt_sid))
|
|
|
|
match = 0;
|
|
|
|
else if (opt_term) {
|
2002-12-03 14:37:59 +05:30
|
|
|
if (task.tty == 0) {
|
2002-02-02 04:17:29 +05:30
|
|
|
match = 0;
|
|
|
|
} else {
|
|
|
|
char tty[256];
|
|
|
|
dev_to_tty (tty, sizeof(tty) - 1,
|
2003-09-20 13:59:55 +05:30
|
|
|
task.tty, task.XXXID, ABBREV_DEV);
|
2002-02-02 04:17:29 +05:30
|
|
|
match = match_strlist (tty, opt_term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt_long || (match && opt_pattern)) {
|
|
|
|
if (opt_full && task.cmdline) {
|
|
|
|
int i = 0;
|
|
|
|
int bytes = sizeof (cmd) - 1;
|
|
|
|
|
|
|
|
/* make sure it is always NUL-terminated */
|
|
|
|
cmd[bytes] = 0;
|
|
|
|
/* make room for SPC in loop below */
|
|
|
|
--bytes;
|
|
|
|
|
|
|
|
strncpy (cmd, task.cmdline[i], bytes);
|
|
|
|
bytes -= strlen (task.cmdline[i++]);
|
|
|
|
while (task.cmdline[i] && bytes > 0) {
|
|
|
|
strncat (cmd, " ", bytes);
|
|
|
|
strncat (cmd, task.cmdline[i], bytes);
|
|
|
|
bytes -= strlen (task.cmdline[i++]) + 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strcpy (cmd, task.cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match && opt_pattern) {
|
|
|
|
if (regexec (preg, cmd, 0, NULL, 0) != 0)
|
|
|
|
match = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match ^ opt_negate) { /* Exclusive OR is neat */
|
|
|
|
if (opt_newest) {
|
2002-10-23 13:23:16 +05:30
|
|
|
if (saved_start_time == task.start_time &&
|
2003-09-20 13:59:55 +05:30
|
|
|
saved_pid > task.XXXID)
|
2002-10-23 13:23:16 +05:30
|
|
|
continue;
|
|
|
|
saved_start_time = task.start_time;
|
2003-09-20 13:59:55 +05:30
|
|
|
saved_pid = task.XXXID;
|
2002-10-23 13:23:16 +05:30
|
|
|
matches = 0;
|
|
|
|
}
|
|
|
|
if (opt_oldest) {
|
|
|
|
if (saved_start_time == task.start_time &&
|
2003-09-20 13:59:55 +05:30
|
|
|
saved_pid < task.XXXID)
|
2002-02-02 04:17:29 +05:30
|
|
|
continue;
|
2002-10-23 13:23:16 +05:30
|
|
|
saved_start_time = task.start_time;
|
2003-09-20 13:59:55 +05:30
|
|
|
saved_pid = task.XXXID;
|
2002-02-02 04:17:29 +05:30
|
|
|
matches = 0;
|
|
|
|
}
|
|
|
|
if (opt_long) {
|
2002-12-21 19:43:33 +05:30
|
|
|
char buff[5096]; // FIXME
|
2003-09-20 13:59:55 +05:30
|
|
|
sprintf (buff, "%d %s", task.XXXID, cmd);
|
2002-02-02 04:17:29 +05:30
|
|
|
list[++matches].str = strdup (buff);
|
|
|
|
} else {
|
2003-09-20 13:59:55 +05:30
|
|
|
list[++matches].num = task.XXXID;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
if (matches == size) {
|
|
|
|
size *= 2;
|
|
|
|
list = realloc (list,
|
|
|
|
size * sizeof (union el));
|
|
|
|
if (list == NULL)
|
|
|
|
exit (3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&task, 0, sizeof (task));
|
|
|
|
}
|
|
|
|
closeproc (ptp);
|
|
|
|
|
|
|
|
list[0].num = matches;
|
|
|
|
return (list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-26 02:42:25 +05:30
|
|
|
static void
|
|
|
|
parse_opts (int argc, char **argv)
|
|
|
|
{
|
|
|
|
char opts[32] = "";
|
|
|
|
int opt;
|
|
|
|
int criteria_count = 0;
|
|
|
|
|
|
|
|
if (strstr (argv[0], "pkill")) {
|
|
|
|
i_am_pkill = 1;
|
|
|
|
progname = "pkill";
|
|
|
|
/* Look for a signal name or number as first argument */
|
|
|
|
if (argc > 1 && argv[1][0] == '-') {
|
|
|
|
int sig;
|
|
|
|
sig = signal_name_to_number (argv[1] + 1);
|
|
|
|
if (sig == -1 && isdigit (argv[1][1]))
|
|
|
|
sig = atoi (argv[1] + 1);
|
|
|
|
if (sig != -1) {
|
|
|
|
int i;
|
|
|
|
for (i = 2; i < argc; i++)
|
|
|
|
argv[i-1] = argv[i];
|
|
|
|
--argc;
|
|
|
|
opt_signal = sig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* These options are for pgrep only */
|
|
|
|
strcat (opts, "ld:");
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat (opts, "fnovxP:g:s:u:U:G:t:?V");
|
|
|
|
|
|
|
|
while ((opt = getopt (argc, argv, opts)) != -1) {
|
|
|
|
switch (opt) {
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'G':
|
|
|
|
opt_rgid = split_list (optarg, conv_gid);
|
|
|
|
if (opt_rgid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
// case 'L': // OpenBSD: print full args along w/ PID
|
|
|
|
// opt_printargs = 1;
|
|
|
|
// break;
|
|
|
|
case 'P':
|
|
|
|
opt_ppid = split_list (optarg, conv_num);
|
|
|
|
if (opt_ppid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'U':
|
|
|
|
opt_ruid = split_list (optarg, conv_uid);
|
|
|
|
if (opt_ruid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
2004-07-21 19:57:56 +05:30
|
|
|
++criteria_count;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'V':
|
|
|
|
fprintf(stdout, "%s (%s)\n", progname, procps_version);
|
|
|
|
exit(0);
|
2002-11-26 02:42:25 +05:30
|
|
|
case 'd':
|
|
|
|
opt_delim = strdup (optarg);
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'f':
|
|
|
|
opt_full = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
|
|
|
case 'g':
|
2002-11-26 03:06:43 +05:30
|
|
|
opt_pgrp = split_list (optarg, conv_pgrp);
|
2002-11-26 02:42:25 +05:30
|
|
|
if (opt_pgrp == NULL)
|
|
|
|
usage (opt);
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
// case 'i':
|
|
|
|
// opt_insensitive = 1;
|
|
|
|
// break;
|
|
|
|
case 'l':
|
|
|
|
opt_long = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'n':
|
|
|
|
if (opt_oldest|opt_negate|opt_newest)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
2004-07-21 19:57:56 +05:30
|
|
|
opt_newest = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
++criteria_count;
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'o':
|
|
|
|
if (opt_oldest|opt_negate|opt_newest)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
2004-07-21 19:57:56 +05:30
|
|
|
opt_oldest = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
++criteria_count;
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 's':
|
|
|
|
opt_sid = split_list (optarg, conv_sid);
|
|
|
|
if (opt_sid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
|
|
|
case 't':
|
2002-11-26 03:06:43 +05:30
|
|
|
opt_term = split_list (optarg, conv_str);
|
2002-11-26 02:42:25 +05:30
|
|
|
if (opt_term == NULL)
|
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2004-07-21 19:57:56 +05:30
|
|
|
case 'u':
|
|
|
|
opt_euid = split_list (optarg, conv_uid);
|
|
|
|
if (opt_euid == NULL)
|
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
if (opt_oldest|opt_negate|opt_newest)
|
|
|
|
usage (opt);
|
|
|
|
opt_negate = 1;
|
|
|
|
break;
|
|
|
|
// Solaris -x, the standard, does ^(regexp)$
|
|
|
|
// OpenBSD -x, being broken, does a plain string
|
|
|
|
case 'x':
|
|
|
|
opt_exact = 1;
|
|
|
|
break;
|
2002-11-26 02:42:25 +05:30
|
|
|
case '?':
|
|
|
|
usage (opt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind == 1)
|
|
|
|
opt_pattern = argv[optind];
|
|
|
|
else if (argc - optind > 1)
|
|
|
|
usage (0);
|
|
|
|
else if (criteria_count == 0) {
|
|
|
|
fprintf (stderr, "%s: No matching criteria specified\n",
|
|
|
|
progname);
|
|
|
|
usage (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
union el *procs;
|
|
|
|
|
|
|
|
parse_opts (argc, argv);
|
|
|
|
|
|
|
|
procs = select_procs ();
|
|
|
|
if (i_am_pkill) {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i <= procs[0].num; i++) {
|
2004-10-19 23:34:31 +05:30
|
|
|
if (kill (procs[i].num, opt_signal) != -1) continue;
|
|
|
|
if (errno==ESRCH) continue; // gone now, which is OK
|
|
|
|
fprintf (stderr, "pkill: %ld - %s\n",
|
|
|
|
procs[i].num, strerror (errno));
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (opt_long)
|
|
|
|
output_strlist (procs);
|
|
|
|
else
|
|
|
|
output_numlist (procs);
|
|
|
|
}
|
|
|
|
return ((procs[0].num) == 0 ? 1 : 0);
|
|
|
|
}
|