2012-03-02 17:59:36 +05:30
|
|
|
/*
|
2002-02-02 04:17:29 +05:30
|
|
|
* pgrep/pkill -- utilities to filter the process table
|
|
|
|
*
|
|
|
|
* Copyright 2000 Kjetil Torgrim Homme <kjetilho@ifi.uio.no>
|
2012-03-02 17:59:36 +05:30
|
|
|
* Changes by Albert Cahalan, 2002,2006.
|
2013-03-18 16:28:47 +05:30
|
|
|
* Changes by Roberto Polli <rpolli@babel.it>, 2012.
|
2002-02-02 04:17:29 +05:30
|
|
|
*
|
2012-03-02 17:59:36 +05:30
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
2012-03-02 17:59:36 +05:30
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
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>
|
2006-06-21 11:15:16 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <signal.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <errno.h>
|
2011-09-27 01:33:43 +05:30
|
|
|
#include <getopt.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2012-02-12 00:32:16 +05:30
|
|
|
/* EXIT_SUCCESS is 0 */
|
|
|
|
/* EXIT_FAILURE is 1 */
|
2011-10-10 00:59:26 +05:30
|
|
|
#define EXIT_USAGE 2
|
|
|
|
#define EXIT_FATAL 3
|
|
|
|
#define XALLOC_EXIT_CODE EXIT_FATAL
|
|
|
|
|
2012-08-18 05:50:27 +05:30
|
|
|
#define CMDSTRSIZE 4096
|
|
|
|
|
2011-10-09 04:59:43 +05:30
|
|
|
#include "c.h"
|
2012-03-23 18:02:24 +05:30
|
|
|
#include "fileutils.h"
|
2011-10-09 04:59:43 +05:30
|
|
|
#include "nls.h"
|
2011-10-10 00:59:26 +05:30
|
|
|
#include "xalloc.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
#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;
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
struct el {
|
2002-02-02 04:17:29 +05:30
|
|
|
long num;
|
|
|
|
char * str;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* User supplied arguments */
|
|
|
|
|
|
|
|
static int opt_full = 0;
|
|
|
|
static int opt_long = 0;
|
2012-08-18 05:50:27 +05:30
|
|
|
static int opt_longlong = 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;
|
2009-11-24 15:30:45 +05:30
|
|
|
static int opt_count = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
static int opt_signal = SIGTERM;
|
2006-06-25 01:42:29 +05:30
|
|
|
static int opt_lock = 0;
|
2006-06-19 02:25:02 +05:30
|
|
|
static int opt_case = 0;
|
2012-01-21 17:54:02 +05:30
|
|
|
static int opt_echo = 0;
|
2013-03-18 16:28:47 +05:30
|
|
|
static int opt_threads = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2002-10-12 09:55:57 +05:30
|
|
|
static const char *opt_delim = "\n";
|
2012-01-21 17:54:02 +05:30
|
|
|
static struct el *opt_pgrp = NULL;
|
|
|
|
static struct el *opt_rgid = NULL;
|
|
|
|
static struct el *opt_pid = NULL;
|
|
|
|
static struct el *opt_ppid = NULL;
|
|
|
|
static struct el *opt_sid = NULL;
|
|
|
|
static struct el *opt_term = NULL;
|
|
|
|
static struct el *opt_euid = NULL;
|
|
|
|
static struct el *opt_ruid = NULL;
|
2002-02-02 04:17:29 +05:30
|
|
|
static char *opt_pattern = NULL;
|
2006-06-21 11:15:16 +05:30
|
|
|
static char *opt_pidfile = NULL;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-09-27 01:33:43 +05:30
|
|
|
static int __attribute__ ((__noreturn__)) usage(int opt)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-09-27 01:33:43 +05:30
|
|
|
int err = (opt == '?');
|
2007-05-28 05:46:39 +05:30
|
|
|
FILE *fp = err ? stderr : stdout;
|
|
|
|
|
2011-10-09 04:59:43 +05:30
|
|
|
fputs(USAGE_HEADER, fp);
|
2011-10-12 02:50:36 +05:30
|
|
|
fprintf(fp, _(" %s [options] <pattern>\n"), program_invocation_short_name);
|
2011-10-09 04:59:43 +05:30
|
|
|
fputs(USAGE_OPTIONS, fp);
|
|
|
|
if (i_am_pkill == 0) {
|
2012-11-21 16:41:17 +05:30
|
|
|
fputs(_(" -d, --delimiter <string> specify output delimiter\n"
|
2012-02-12 01:03:17 +05:30
|
|
|
" -l, --list-name list PID and process name\n"
|
2013-03-18 16:28:47 +05:30
|
|
|
" -v, --inverse negates the matching\n"
|
|
|
|
" -w, --lightweight list all TID\n"
|
|
|
|
), fp);
|
2011-10-09 04:59:43 +05:30
|
|
|
}
|
|
|
|
if (i_am_pkill == 1) {
|
2012-01-21 17:54:02 +05:30
|
|
|
fputs(_(" -<sig>, --signal <sig> signal to send (either number or name)\n"
|
|
|
|
" -e, --echo display what is killed\n"), fp);
|
2011-10-09 04:59:43 +05:30
|
|
|
}
|
2012-11-21 16:41:17 +05:30
|
|
|
fputs(_(" -c, --count count of matching processes\n"
|
|
|
|
" -f, --full use full process name to match\n"
|
2011-12-28 02:16:16 +05:30
|
|
|
" -g, --pgroup <id,...> match listed process group IDs\n"
|
|
|
|
" -G, --group <gid,...> match real group IDs\n"
|
|
|
|
" -n, --newest select most recently started\n"
|
|
|
|
" -o, --oldest select least recently started\n"
|
2013-02-13 23:06:11 +05:30
|
|
|
" -P, --parent <ppid,...> match only child processes of the given parent\n"
|
2011-12-28 02:16:16 +05:30
|
|
|
" -s, --session <sid,...> match session IDs\n"
|
|
|
|
" -t, --terminal <tty,...> match by controlling terminal\n"
|
|
|
|
" -u, --euid <id,...> match by effective IDs\n"
|
|
|
|
" -U, --uid <id,...> match by real IDs\n"
|
2013-02-13 23:06:11 +05:30
|
|
|
" -x, --exact match exactly with the command name\n"
|
2011-12-28 02:16:16 +05:30
|
|
|
" -F, --pidfile <file> read PIDs from file\n"
|
|
|
|
" -L, --logpidfile fail if PID file is not locked\n"), fp);
|
2011-10-09 04:59:43 +05:30
|
|
|
fputs(USAGE_SEPARATOR, fp);
|
|
|
|
fputs(USAGE_HELP, fp);
|
|
|
|
fputs(USAGE_VERSION, fp);
|
|
|
|
fprintf(fp, USAGE_MAN_TAIL("pgrep(1)"));
|
2011-09-27 01:33:43 +05:30
|
|
|
|
|
|
|
exit(fp == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static struct el *split_list (const char *restrict str, int (*convert)(const char *, struct el *))
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-10-10 00:59:26 +05:30
|
|
|
char *copy = xstrdup (str);
|
2002-02-02 04:17:29 +05:30
|
|
|
char *ptr = copy;
|
|
|
|
char *sep_pos;
|
2006-06-19 04:49:30 +05:30
|
|
|
int i = 0;
|
|
|
|
int size = 0;
|
2012-01-21 17:54:02 +05:30
|
|
|
struct el *list = NULL;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
do {
|
|
|
|
if (i == size) {
|
2006-06-19 04:49:30 +05:30
|
|
|
size = size * 5 / 4 + 4;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* add 1 because slot zero is a count */
|
2011-10-10 00:59:26 +05:30
|
|
|
list = xrealloc (list, 1 + size * sizeof *list);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2006-06-19 04:49:30 +05:30
|
|
|
sep_pos = strchr (ptr, ',');
|
|
|
|
if (sep_pos)
|
|
|
|
*sep_pos = 0;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* Use ++i instead of i++ because slot zero is a count */
|
2012-02-06 01:40:53 +05:30
|
|
|
if (list && !convert (ptr, &list[++i]))
|
2006-06-21 09:48:02 +05:30
|
|
|
exit (EXIT_USAGE);
|
2002-02-02 04:17:29 +05:30
|
|
|
if (sep_pos)
|
|
|
|
ptr = sep_pos + 1;
|
|
|
|
} while (sep_pos);
|
|
|
|
|
|
|
|
free (copy);
|
2006-06-19 04:49:30 +05:30
|
|
|
if (!i) {
|
2002-02-02 04:17:29 +05:30
|
|
|
free (list);
|
|
|
|
list = NULL;
|
|
|
|
} else {
|
2006-06-19 04:49:30 +05:30
|
|
|
list[0].num = i;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2006-06-17 09:44:57 +05:30
|
|
|
return list;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-02-12 00:32:16 +05:30
|
|
|
/* strict_atol returns a Boolean: TRUE if the input string
|
|
|
|
* contains a plain number, FALSE if there are any non-digits. */
|
2006-06-21 09:48:02 +05:30
|
|
|
static int 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;
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2006-06-21 11:15:16 +05:30
|
|
|
#include <sys/file.h>
|
|
|
|
|
2012-02-12 00:32:16 +05:30
|
|
|
/* We try a read lock. The daemon should have a write lock.
|
|
|
|
* Seen using flock: FreeBSD code */
|
2006-06-23 08:48:12 +05:30
|
|
|
static int has_flock(int fd)
|
|
|
|
{
|
|
|
|
return flock(fd, LOCK_SH|LOCK_NB)==-1 && errno==EWOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
2012-02-12 00:32:16 +05:30
|
|
|
/* We try a read lock. The daemon should have a write lock.
|
|
|
|
* Seen using fcntl: libslack */
|
2006-06-23 08:48:12 +05:30
|
|
|
static int has_fcntl(int fd)
|
|
|
|
{
|
2012-02-12 00:32:16 +05:30
|
|
|
struct flock f; /* seriously, struct flock is for a fnctl lock! */
|
2006-06-23 08:48:12 +05:30
|
|
|
f.l_type = F_RDLCK;
|
|
|
|
f.l_whence = SEEK_SET;
|
|
|
|
f.l_start = 0;
|
|
|
|
f.l_len = 0;
|
|
|
|
return fcntl(fd,F_SETLK,&f)==-1 && (errno==EACCES || errno==EAGAIN);
|
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static struct el *read_pidfile(void)
|
2006-06-21 11:15:16 +05:30
|
|
|
{
|
|
|
|
char buf[12];
|
|
|
|
int fd;
|
|
|
|
struct stat sbuf;
|
|
|
|
char *endp;
|
2011-10-07 02:28:26 +05:30
|
|
|
int n, pid;
|
2012-01-21 17:54:02 +05:30
|
|
|
struct el *list = NULL;
|
2006-06-21 11:15:16 +05:30
|
|
|
|
|
|
|
fd = open(opt_pidfile, O_RDONLY|O_NOCTTY|O_NONBLOCK);
|
|
|
|
if(fd<0)
|
2011-10-07 02:28:26 +05:30
|
|
|
goto just_ret;
|
2006-06-21 11:15:16 +05:30
|
|
|
if(fstat(fd,&sbuf) || !S_ISREG(sbuf.st_mode) || sbuf.st_size<1)
|
|
|
|
goto out;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* type of lock, if any, is not standardized on Linux */
|
2006-06-25 01:42:29 +05:30
|
|
|
if(opt_lock && !has_flock(fd) && !has_fcntl(fd))
|
2011-10-07 02:28:26 +05:30
|
|
|
goto out;
|
2006-06-21 11:15:16 +05:30
|
|
|
memset(buf,'\0',sizeof buf);
|
2011-10-07 02:28:26 +05:30
|
|
|
n = read(fd,buf+1,sizeof buf-2);
|
|
|
|
if (n<1)
|
|
|
|
goto out;
|
2006-06-21 11:15:16 +05:30
|
|
|
pid = strtoul(buf+1,&endp,10);
|
|
|
|
if(endp<=buf+1 || pid<1 || pid>0x7fffffff)
|
|
|
|
goto out;
|
|
|
|
if(*endp && !isspace(*endp))
|
|
|
|
goto out;
|
2011-10-10 00:59:26 +05:30
|
|
|
list = xmalloc(2 * sizeof *list);
|
2006-06-21 11:15:16 +05:30
|
|
|
list[0].num = 1;
|
|
|
|
list[1].num = pid;
|
|
|
|
out:
|
|
|
|
close(fd);
|
2011-10-07 02:28:26 +05:30
|
|
|
just_ret:
|
2006-06-21 11:15:16 +05:30
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_uid (const char *restrict name, struct 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) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("invalid user name: %s"), name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
e->num = pwd->pw_uid;
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_gid (const char *restrict name, struct el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
struct group *grp;
|
|
|
|
|
|
|
|
if (strict_atol (name, &e->num))
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
grp = getgrnam (name);
|
|
|
|
if (grp == NULL) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("invalid group name: %s"), name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
e->num = grp->gr_gid;
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_pgrp (const char *restrict name, struct el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("invalid process group: %s"), name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
if (e->num == 0)
|
|
|
|
e->num = getpgrp ();
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_sid (const char *restrict name, struct el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("invalid session id: %s"), name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
if (e->num == 0)
|
|
|
|
e->num = getsid (0);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_num (const char *restrict name, struct el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
if (! strict_atol (name, &e->num)) {
|
2012-01-03 13:18:43 +05:30
|
|
|
xwarnx(_("not a number: %s"), name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int conv_str (const char *restrict name, struct el *restrict e)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2011-10-10 00:59:26 +05:30
|
|
|
e->str = xstrdup (name);
|
2006-06-17 09:44:57 +05:30
|
|
|
return 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int match_numlist (long value, const struct 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;
|
|
|
|
}
|
|
|
|
}
|
2006-06-17 09:44:57 +05:30
|
|
|
return found;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static int match_strlist (const char *restrict value, const struct 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;
|
|
|
|
}
|
|
|
|
}
|
2006-06-17 09:44:57 +05:30
|
|
|
return found;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static void output_numlist (const struct el *restrict list, int num)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
int i;
|
2006-06-19 04:49:30 +05:30
|
|
|
const char *delim = opt_delim;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if(i+1==num)
|
|
|
|
delim = "\n";
|
|
|
|
printf ("%ld%s", list[i].num, delim);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static void output_strlist (const struct el *restrict list, int num)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2012-02-12 00:32:16 +05:30
|
|
|
/* FIXME: escape codes */
|
2002-02-02 04:17:29 +05:30
|
|
|
int i;
|
2006-06-19 04:49:30 +05:30
|
|
|
const char *delim = opt_delim;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if(i+1==num)
|
|
|
|
delim = "\n";
|
2012-01-21 17:54:02 +05:30
|
|
|
printf ("%lu %s%s", list[i].num, list[i].str, delim);
|
2006-06-19 04:49:30 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2006-06-21 09:48:02 +05:30
|
|
|
static PROCTAB *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))
|
2012-02-12 00:32:16 +05:30
|
|
|
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;
|
2011-10-10 00:59:26 +05:30
|
|
|
uid_t *uids = xmalloc (num * sizeof (uid_t));
|
2002-02-02 04:17:29 +05:30
|
|
|
while (i-- > 0) {
|
|
|
|
uids[i] = opt_euid[i+1].num;
|
|
|
|
}
|
|
|
|
flags |= PROC_UID;
|
|
|
|
ptp = openproc (flags, uids, num);
|
|
|
|
} else {
|
|
|
|
ptp = openproc (flags);
|
|
|
|
}
|
2006-06-17 09:44:57 +05:30
|
|
|
return ptp;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2006-06-21 09:48:02 +05:30
|
|
|
static regex_t * 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;
|
|
|
|
|
2011-10-10 00:59:26 +05:30
|
|
|
preg = xmalloc (sizeof (regex_t));
|
2002-02-02 04:17:29 +05:30
|
|
|
if (opt_exact) {
|
2011-10-10 00:59:26 +05:30
|
|
|
re = xmalloc (strlen (opt_pattern) + 5);
|
2002-02-02 04:17:29 +05:30
|
|
|
sprintf (re, "^(%s)$", opt_pattern);
|
|
|
|
} else {
|
2012-02-12 00:32:16 +05:30
|
|
|
re = opt_pattern;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2006-06-19 02:25:02 +05:30
|
|
|
re_err = regcomp (preg, re, REG_EXTENDED | REG_NOSUB | opt_case);
|
2002-02-02 04:17:29 +05:30
|
|
|
if (re_err) {
|
|
|
|
regerror (re_err, preg, errbuf, sizeof(errbuf));
|
2005-10-31 05:15:47 +05:30
|
|
|
fputs(errbuf,stderr);
|
2006-06-21 09:48:02 +05:30
|
|
|
exit (EXIT_USAGE);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return preg;
|
|
|
|
}
|
|
|
|
|
2012-01-21 17:54:02 +05:30
|
|
|
static struct el * select_procs (int *num)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
|
|
|
PROCTAB *ptp;
|
|
|
|
proc_t task;
|
2012-02-12 00:32: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;
|
2006-06-19 04:49:30 +05:30
|
|
|
int size = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
regex_t *preg;
|
|
|
|
pid_t myself = getpid();
|
2012-01-21 17:54:02 +05:30
|
|
|
struct el *list = NULL;
|
2012-08-18 05:50:27 +05:30
|
|
|
char cmdline[CMDSTRSIZE];
|
|
|
|
char cmdsearch[CMDSTRSIZE];
|
|
|
|
char cmdoutput[CMDSTRSIZE];
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2006-06-19 04:49:30 +05:30
|
|
|
ptp = do_openproc();
|
|
|
|
preg = do_regcomp();
|
2002-10-23 13:23:16 +05:30
|
|
|
|
|
|
|
if (opt_newest) saved_start_time = 0ULL;
|
2009-11-24 05:30:41 +05:30
|
|
|
else saved_start_time = ~0ULL;
|
|
|
|
|
2002-10-23 13:23:16 +05:30
|
|
|
if (opt_newest) saved_pid = 0;
|
|
|
|
if (opt_oldest) saved_pid = INT_MAX;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2006-06-19 04:49:30 +05:30
|
|
|
memset(&task, 0, sizeof (task));
|
|
|
|
while(readproc(ptp, &task)) {
|
2002-02-02 04:17:29 +05:30
|
|
|
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;
|
2006-06-21 11:15:16 +05:30
|
|
|
else if (opt_pid && ! match_numlist (task.tgid, opt_pid))
|
|
|
|
match = 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-08-18 05:50:27 +05:30
|
|
|
if (task.cmdline && (opt_longlong || opt_full) && match && opt_pattern) {
|
|
|
|
int i = 0;
|
|
|
|
int bytes = sizeof (cmdline) - 1;
|
|
|
|
|
|
|
|
/* make sure it is always NUL-terminated */
|
|
|
|
cmdline[bytes] = 0;
|
|
|
|
/* make room for SPC in loop below */
|
|
|
|
--bytes;
|
|
|
|
|
|
|
|
strncpy (cmdline, task.cmdline[i], bytes);
|
|
|
|
bytes -= strlen (task.cmdline[i++]);
|
|
|
|
while (task.cmdline[i] && bytes > 0) {
|
|
|
|
strncat (cmdline, " ", bytes);
|
|
|
|
strncat (cmdline, task.cmdline[i], bytes);
|
|
|
|
bytes -= strlen (task.cmdline[i++]) + 1;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:50:27 +05:30
|
|
|
if (opt_long || opt_longlong || (match && opt_pattern)) {
|
|
|
|
if (opt_longlong && task.cmdline)
|
|
|
|
strncpy (cmdoutput, cmdline, CMDSTRSIZE);
|
|
|
|
else
|
|
|
|
strncpy (cmdoutput, task.cmd, CMDSTRSIZE);
|
|
|
|
}
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
if (match && opt_pattern) {
|
2012-08-18 05:50:27 +05:30
|
|
|
if (opt_full && task.cmdline)
|
|
|
|
strncpy (cmdsearch, cmdline, CMDSTRSIZE);
|
|
|
|
else
|
|
|
|
strncpy (cmdsearch, task.cmd, CMDSTRSIZE);
|
|
|
|
|
|
|
|
if (regexec (preg, cmdsearch, 0, NULL, 0) != 0)
|
2002-02-02 04:17:29 +05:30
|
|
|
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;
|
|
|
|
}
|
2006-06-19 04:49:30 +05:30
|
|
|
if (matches == size) {
|
|
|
|
size = size * 5 / 4 + 4;
|
2011-10-10 00:59:26 +05:30
|
|
|
list = xrealloc(list, size * sizeof *list);
|
2006-06-19 04:49:30 +05:30
|
|
|
}
|
2012-08-18 05:50:27 +05:30
|
|
|
if (list && (opt_long || opt_longlong || opt_echo)) {
|
2012-01-21 17:54:02 +05:30
|
|
|
list[matches].num = task.XXXID;
|
2012-08-18 05:50:27 +05:30
|
|
|
list[matches++].str = xstrdup (cmdoutput);
|
2012-02-06 01:40:53 +05:30
|
|
|
} else if (list) {
|
2006-06-17 09:44:57 +05:30
|
|
|
list[matches++].num = task.XXXID;
|
2012-02-06 01:40:53 +05:30
|
|
|
} else {
|
|
|
|
xerrx(EXIT_FAILURE, _("internal error"));
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2013-03-18 16:28:47 +05:30
|
|
|
|
|
|
|
// pkill does not need subtasks!
|
|
|
|
// this control is still done at
|
|
|
|
// argparse time, but a further
|
|
|
|
// control is free
|
|
|
|
if (opt_threads && !i_am_pkill) {
|
|
|
|
proc_t subtask;
|
|
|
|
memset(&subtask, 0, sizeof (subtask));
|
|
|
|
while (readtask(ptp, &task, &subtask)){
|
|
|
|
// don't add redundand tasks
|
|
|
|
if (task.XXXID == subtask.XXXID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// eventually grow output buffer
|
|
|
|
if (matches == size) {
|
|
|
|
size = size * 5 / 4 + 4;
|
|
|
|
list = realloc(list, size * sizeof *list);
|
|
|
|
if (list == NULL)
|
|
|
|
exit (EXIT_FATAL);
|
|
|
|
}
|
|
|
|
if (opt_long) {
|
|
|
|
list[matches].str = xstrdup (cmdoutput);
|
|
|
|
list[matches++].num = subtask.XXXID;
|
|
|
|
} else {
|
|
|
|
list[matches++].num = subtask.XXXID;
|
|
|
|
}
|
|
|
|
memset(&subtask, 0, sizeof (subtask));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2013-03-11 11:30:00 +05:30
|
|
|
|
2013-03-18 16:28:47 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
memset (&task, 0, sizeof (task));
|
|
|
|
}
|
|
|
|
closeproc (ptp);
|
2006-06-17 09:44:57 +05:30
|
|
|
*num = matches;
|
|
|
|
return list;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-11-02 23:20:52 +05:30
|
|
|
static int signal_option(int *argc, char **argv)
|
2012-02-12 00:32:54 +05:30
|
|
|
{
|
|
|
|
int sig;
|
2012-06-24 17:00:22 +05:30
|
|
|
int i;
|
|
|
|
for (i = 1; i < *argc; i++) {
|
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
sig = signal_name_to_number(argv[i] + 1);
|
|
|
|
if (sig == -1 && isdigit(argv[i][1]))
|
|
|
|
sig = atoi(argv[i] + 1);
|
|
|
|
if (-1 < sig) {
|
|
|
|
memmove(argv + i, argv + i + 1,
|
|
|
|
sizeof(char *) * (*argc - i));
|
|
|
|
(*argc)--;
|
|
|
|
return sig;
|
|
|
|
}
|
2012-02-12 00:32:54 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2006-06-21 09:48:02 +05:30
|
|
|
static void parse_opts (int argc, char **argv)
|
2002-11-26 02:42:25 +05:30
|
|
|
{
|
|
|
|
char opts[32] = "";
|
|
|
|
int opt;
|
|
|
|
int criteria_count = 0;
|
|
|
|
|
2011-09-27 01:33:43 +05:30
|
|
|
enum {
|
|
|
|
SIGNAL_OPTION = CHAR_MAX + 1
|
|
|
|
};
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{"signal", required_argument, NULL, SIGNAL_OPTION},
|
|
|
|
{"count", no_argument, NULL, 'c'},
|
2012-10-11 16:04:50 +05:30
|
|
|
{"delimiter", required_argument, NULL, 'd'},
|
2011-09-27 01:33:43 +05:30
|
|
|
{"list-name", no_argument, NULL, 'l'},
|
2012-08-18 05:50:27 +05:30
|
|
|
{"list-full", no_argument, NULL, 'a'},
|
2011-09-27 01:33:43 +05:30
|
|
|
{"full", no_argument, NULL, 'f'},
|
|
|
|
{"pgroup", required_argument, NULL, 'g'},
|
|
|
|
{"group", required_argument, NULL, 'G'},
|
|
|
|
{"newest", no_argument, NULL, 'n'},
|
|
|
|
{"oldest", no_argument, NULL, 'o'},
|
|
|
|
{"parent", required_argument, NULL, 'P'},
|
|
|
|
{"session", required_argument, NULL, 's'},
|
|
|
|
{"terminal", required_argument, NULL, 't'},
|
|
|
|
{"euid", required_argument, NULL, 'u'},
|
|
|
|
{"uid", required_argument, NULL, 'U'},
|
|
|
|
{"inverse", no_argument, NULL, 'v'},
|
2013-03-18 16:28:47 +05:30
|
|
|
{"lightweight", no_argument, NULL, 'w'},
|
2011-09-27 01:33:43 +05:30
|
|
|
{"exact", no_argument, NULL, 'x'},
|
|
|
|
{"pidfile", required_argument, NULL, 'F'},
|
|
|
|
{"logpidfile", no_argument, NULL, 'L'},
|
2012-01-21 17:54:02 +05:30
|
|
|
{"echo", no_argument, NULL, 'e'},
|
2011-09-27 01:33:43 +05:30
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2011-10-12 02:50:36 +05:30
|
|
|
if (strstr (program_invocation_short_name, "pkill")) {
|
2012-02-12 00:32:54 +05:30
|
|
|
int sig;
|
|
|
|
i_am_pkill = 1;
|
|
|
|
sig = signal_option(&argc, argv);
|
|
|
|
if (-1 < sig)
|
|
|
|
opt_signal = sig;
|
2012-01-21 17:54:02 +05:30
|
|
|
/* These options are for pkill only */
|
|
|
|
strcat (opts, "e");
|
2002-11-26 02:42:25 +05:30
|
|
|
} else {
|
|
|
|
/* These options are for pgrep only */
|
2013-03-18 16:28:47 +05:30
|
|
|
strcat (opts, "lad:vw");
|
2002-11-26 02:42:25 +05:30
|
|
|
}
|
2013-03-11 11:30:00 +05:30
|
|
|
|
2012-11-21 16:41:17 +05:30
|
|
|
strcat (opts, "LF:cfnoxP:g:s:u:U:G:t:?Vh");
|
2002-11-26 02:42:25 +05:30
|
|
|
|
2011-09-27 01:33:43 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, opts, longopts, NULL)) != -1) {
|
2002-11-26 02:42:25 +05:30
|
|
|
switch (opt) {
|
2011-09-27 01:33:43 +05:30
|
|
|
case SIGNAL_OPTION:
|
|
|
|
opt_signal = signal_name_to_number (optarg);
|
|
|
|
if (opt_signal == -1 && isdigit (optarg[0]))
|
|
|
|
opt_signal = atoi (optarg);
|
|
|
|
break;
|
2012-01-21 17:54:02 +05:30
|
|
|
case 'e':
|
|
|
|
opt_echo = 1;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'D': / * FreeBSD: print info about non-matches for debugging * /
|
|
|
|
* break; */
|
|
|
|
case 'F': /* FreeBSD: the arg is a file containing a PID to match */
|
2011-10-10 00:59:26 +05:30
|
|
|
opt_pidfile = xstrdup (optarg);
|
2006-06-21 11:15:16 +05:30
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'G': /* Solaris: match rgid/rgroup */
|
|
|
|
opt_rgid = split_list (optarg, conv_gid);
|
2004-07-21 19:57:56 +05:30
|
|
|
if (opt_rgid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'I': / * FreeBSD: require confirmation before killing * /
|
2012-02-26 02:39:50 +05:30
|
|
|
* break; */
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'J': / * Solaris: match by project ID (name or number) * /
|
|
|
|
* break; */
|
|
|
|
case 'L': /* FreeBSD: fail if pidfile (see -F) not locked */
|
2006-06-25 01:42:29 +05:30
|
|
|
opt_lock++;
|
2006-06-21 11:15:16 +05:30
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'M': / * FreeBSD: specify core (OS crash dump) file * /
|
|
|
|
* break; */
|
|
|
|
/* case 'N': / * FreeBSD: specify alternate namelist file (for us, System.map -- but we don't need it) * /
|
|
|
|
* break; */
|
|
|
|
case 'P': /* Solaris: match by PPID */
|
|
|
|
opt_ppid = split_list (optarg, conv_num);
|
2004-07-21 19:57:56 +05:30
|
|
|
if (opt_ppid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'S': / * FreeBSD: don't ignore the built-in kernel tasks * /
|
|
|
|
* break; */
|
|
|
|
/* case 'T': / * Solaris: match by "task ID" (probably not a Linux task) * /
|
|
|
|
* break; */
|
|
|
|
case 'U': /* Solaris: match by ruid/rgroup */
|
|
|
|
opt_ruid = split_list (optarg, conv_uid);
|
2004-07-21 19:57:56 +05:30
|
|
|
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':
|
2011-10-09 04:59:43 +05:30
|
|
|
printf(PROCPS_NG_VERSION);
|
2006-06-21 09:48:02 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'c': / * Solaris: match by contract ID * /
|
|
|
|
* break; */
|
2009-11-24 15:30:45 +05:30
|
|
|
case 'c':
|
|
|
|
opt_count = 1;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'd': /* Solaris: change the delimiter */
|
2011-10-10 00:59:26 +05:30
|
|
|
opt_delim = xstrdup (optarg);
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'f': /* Solaris: match full process name (as in "ps -f") */
|
2004-07-21 19:57:56 +05:30
|
|
|
opt_full = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'g': /* Solaris: match pgrp */
|
|
|
|
opt_pgrp = split_list (optarg, conv_pgrp);
|
2002-11-26 02:42:25 +05:30
|
|
|
if (opt_pgrp == NULL)
|
|
|
|
usage (opt);
|
2006-06-25 01:42:29 +05:30
|
|
|
++criteria_count;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'i': / * FreeBSD: ignore case. OpenBSD: withdrawn. See -I. This sucks. * /
|
|
|
|
* if (opt_case)
|
|
|
|
* usage (opt);
|
|
|
|
* opt_case = REG_ICASE;
|
|
|
|
* break; */
|
|
|
|
/* case 'j': / * FreeBSD: restricted to the given jail ID * /
|
|
|
|
* break; */
|
|
|
|
case 'l': /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */
|
2004-07-21 19:57:56 +05:30
|
|
|
opt_long = 1;
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
2012-08-18 05:50:27 +05:30
|
|
|
case 'a':
|
|
|
|
opt_longlong = 1;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'n': /* Solaris: match only the newest */
|
2004-07-21 19:57:56 +05:30
|
|
|
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;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'o': /* Solaris: match only the oldest */
|
2004-07-21 19:57:56 +05:30
|
|
|
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;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 's': /* Solaris: match by session ID -- zero means self */
|
|
|
|
opt_sid = split_list (optarg, conv_sid);
|
2004-07-21 19:57:56 +05:30
|
|
|
if (opt_sid == NULL)
|
2002-11-26 02:42:25 +05:30
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 't': /* Solaris: match by tty */
|
|
|
|
opt_term = split_list (optarg, conv_str);
|
2002-11-26 02:42:25 +05:30
|
|
|
if (opt_term == NULL)
|
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'u': /* Solaris: match by euid/egroup */
|
|
|
|
opt_euid = split_list (optarg, conv_uid);
|
2004-07-21 19:57:56 +05:30
|
|
|
if (opt_euid == NULL)
|
|
|
|
usage (opt);
|
|
|
|
++criteria_count;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
case 'v': /* Solaris: as in grep, invert the matching (uh... applied after selection I think) */
|
2004-07-21 19:57:56 +05:30
|
|
|
if (opt_oldest|opt_negate|opt_newest)
|
|
|
|
usage (opt);
|
2012-02-12 00:32:16 +05:30
|
|
|
opt_negate = 1;
|
2004-07-21 19:57:56 +05:30
|
|
|
break;
|
2013-03-18 16:28:47 +05:30
|
|
|
case 'w': // Linux: show threads (lightweight process) too
|
|
|
|
opt_threads = 1;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* OpenBSD -x, being broken, does a plain string */
|
|
|
|
case 'x': /* Solaris: use ^(regexp)$ in place of regexp (FreeBSD too) */
|
2004-07-21 19:57:56 +05:30
|
|
|
opt_exact = 1;
|
|
|
|
break;
|
2012-02-12 00:32:16 +05:30
|
|
|
/* case 'z': / * Solaris: match by zone ID * /
|
|
|
|
* break; */
|
2011-09-27 01:33:43 +05:30
|
|
|
case 'h':
|
|
|
|
usage (opt);
|
|
|
|
break;
|
2002-11-26 02:42:25 +05:30
|
|
|
case '?':
|
2009-11-24 05:30:49 +05:30
|
|
|
usage (optopt ? optopt : opt);
|
2002-11-26 02:42:25 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-21 11:15:16 +05:30
|
|
|
|
2011-12-18 18:09:28 +05:30
|
|
|
if(opt_lock && !opt_pidfile)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("-L without -F makes no sense\n"
|
2012-02-12 00:32:16 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
2006-06-21 11:15:16 +05:30
|
|
|
|
|
|
|
if(opt_pidfile){
|
|
|
|
opt_pid = read_pidfile();
|
2011-12-18 18:09:28 +05:30
|
|
|
if(!opt_pid)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("pidfile not valid\n"
|
2012-02-12 00:32:16 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
2006-06-21 11:15:16 +05:30
|
|
|
}
|
|
|
|
|
2012-02-12 00:32:16 +05:30
|
|
|
if (argc - optind == 1)
|
2002-11-26 02:42:25 +05:30
|
|
|
opt_pattern = argv[optind];
|
|
|
|
else if (argc - optind > 1)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("only one pattern can be provided\n"
|
2012-02-12 00:32:16 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
2011-12-18 18:09:28 +05:30
|
|
|
else if (criteria_count == 0)
|
2012-01-03 13:18:43 +05:30
|
|
|
xerrx(EXIT_FAILURE, _("no matching criteria specified\n"
|
2012-02-12 00:32:16 +05:30
|
|
|
"Try `%s --help' for more information."),
|
|
|
|
program_invocation_short_name);
|
2002-11-26 02:42:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-12 02:50:36 +05:30
|
|
|
int main (int argc, char **argv)
|
2002-02-02 04:17:29 +05:30
|
|
|
{
|
2012-01-21 17:54:02 +05:30
|
|
|
struct el *procs;
|
2006-06-17 09:44:57 +05:30
|
|
|
int num;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2013-02-20 23:01:48 +05:30
|
|
|
#ifdef HAVE_PROGRAM_INVOCATION_NAME
|
2012-01-03 13:18:43 +05:30
|
|
|
program_invocation_name = program_invocation_short_name;
|
2013-02-20 23:01:48 +05:30
|
|
|
#endif
|
2011-12-07 17:57:21 +05:30
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
2012-03-23 18:02:24 +05:30
|
|
|
atexit(close_stdout);
|
2011-12-07 17:57:21 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
parse_opts (argc, argv);
|
|
|
|
|
2006-06-17 09:44:57 +05:30
|
|
|
procs = select_procs (&num);
|
2002-02-02 04:17:29 +05:30
|
|
|
if (i_am_pkill) {
|
|
|
|
int i;
|
2006-06-17 09:44:57 +05:30
|
|
|
for (i = 0; i < num; i++) {
|
2012-01-21 17:54:02 +05:30
|
|
|
if (kill (procs[i].num, opt_signal) != -1) {
|
|
|
|
if (opt_echo)
|
|
|
|
printf(_("%s killed (pid %lu)\n"), procs[i].str, procs[i].num);
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-12 02:50:36 +05:30
|
|
|
if (errno==ESRCH)
|
2012-02-26 02:39:50 +05:30
|
|
|
/* gone now, which is OK */
|
|
|
|
continue;
|
2012-01-24 23:26:29 +05:30
|
|
|
xwarn(_("killing pid %ld failed"), procs[i].num);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2013-03-11 11:30:00 +05:30
|
|
|
if (opt_count)
|
2012-11-21 16:41:17 +05:30
|
|
|
fprintf(stdout, "%d\n", num);
|
2002-02-02 04:17:29 +05:30
|
|
|
} else {
|
2009-11-24 15:30:45 +05:30
|
|
|
if (opt_count) {
|
2012-01-21 06:44:21 +05:30
|
|
|
fprintf(stdout, "%d\n", num);
|
2009-11-24 15:30:45 +05:30
|
|
|
} else {
|
2012-08-18 05:50:27 +05:30
|
|
|
if (opt_long || opt_longlong)
|
2009-11-24 15:30:45 +05:30
|
|
|
output_strlist (procs,num);
|
|
|
|
else
|
|
|
|
output_numlist (procs,num);
|
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2012-02-12 00:32:16 +05:30
|
|
|
return !num; /* exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE) */
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|