2007-09-30 03:56:01 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini pgrep/pkill implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-09-30 03:56:01 +05:30
|
|
|
*/
|
2016-11-23 10:53:44 +05:30
|
|
|
//config:config PGREP
|
|
|
|
//config: bool "pgrep"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: Look for processes by name.
|
|
|
|
//config:
|
|
|
|
//config:config PKILL
|
|
|
|
//config: bool "pkill"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: Send signals to processes by name.
|
|
|
|
|
|
|
|
//applet:IF_PGREP(APPLET(pgrep, BB_DIR_USR_BIN, BB_SUID_DROP))
|
2017-01-29 19:27:33 +05:30
|
|
|
// APPLET_ODDNAME:name main location suid_type help
|
2016-11-23 10:53:44 +05:30
|
|
|
//applet:IF_PKILL(APPLET_ODDNAME(pkill, pgrep, BB_DIR_USR_BIN, BB_SUID_DROP, pkill))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_PGREP) += pgrep.o
|
|
|
|
//kbuild:lib-$(CONFIG_PKILL) += pgrep.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define pgrep_trivial_usage
|
|
|
|
//usage: "[-flnovx] [-s SID|-P PPID|PATTERN]"
|
|
|
|
//usage:#define pgrep_full_usage "\n\n"
|
|
|
|
//usage: "Display process(es) selected by regex PATTERN\n"
|
|
|
|
//usage: "\n -l Show command name too"
|
|
|
|
//usage: "\n -f Match against entire command line"
|
|
|
|
//usage: "\n -n Show the newest process only"
|
|
|
|
//usage: "\n -o Show the oldest process only"
|
|
|
|
//usage: "\n -v Negate the match"
|
|
|
|
//usage: "\n -x Match whole name (not substring)"
|
|
|
|
//usage: "\n -s Match session ID (0 for current)"
|
|
|
|
//usage: "\n -P Match parent process ID"
|
|
|
|
//usage:
|
|
|
|
//usage:#define pkill_trivial_usage
|
|
|
|
//usage: "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]"
|
|
|
|
//usage:#define pkill_full_usage "\n\n"
|
|
|
|
//usage: "Send a signal to process(es) selected by regex PATTERN\n"
|
|
|
|
//usage: "\n -l List all signals"
|
|
|
|
//usage: "\n -f Match against entire command line"
|
|
|
|
//usage: "\n -n Signal the newest process only"
|
|
|
|
//usage: "\n -o Signal the oldest process only"
|
|
|
|
//usage: "\n -v Negate the match"
|
|
|
|
//usage: "\n -x Match whole name (not substring)"
|
|
|
|
//usage: "\n -s Match session ID (0 for current)"
|
|
|
|
//usage: "\n -P Match parent process ID"
|
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
#include "xregex.h"
|
|
|
|
|
|
|
|
/* Idea taken from kill.c */
|
|
|
|
#define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
|
|
|
|
#define pkill (ENABLE_PKILL && applet_name[1] == 'k')
|
|
|
|
|
|
|
|
enum {
|
2009-07-06 03:30:12 +05:30
|
|
|
/* "vlfxons:P:" */
|
|
|
|
OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
|
|
|
|
OPTBIT_L,
|
|
|
|
OPTBIT_F,
|
|
|
|
OPTBIT_X,
|
|
|
|
OPTBIT_O,
|
|
|
|
OPTBIT_N,
|
|
|
|
OPTBIT_S,
|
|
|
|
OPTBIT_P,
|
2007-09-30 03:56:01 +05:30
|
|
|
};
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
#define OPT_INVERT (opt & (1 << OPTBIT_V))
|
|
|
|
#define OPT_LIST (opt & (1 << OPTBIT_L))
|
|
|
|
#define OPT_FULL (opt & (1 << OPTBIT_F))
|
|
|
|
#define OPT_ANCHOR (opt & (1 << OPTBIT_X))
|
|
|
|
#define OPT_FIRST (opt & (1 << OPTBIT_O))
|
|
|
|
#define OPT_LAST (opt & (1 << OPTBIT_N))
|
|
|
|
#define OPT_SID (opt & (1 << OPTBIT_S))
|
|
|
|
#define OPT_PPID (opt & (1 << OPTBIT_P))
|
2007-09-30 03:56:01 +05:30
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
static void act(unsigned pid, char *cmd, int signo)
|
2007-09-30 03:56:01 +05:30
|
|
|
{
|
|
|
|
if (pgrep) {
|
2009-07-06 03:30:12 +05:30
|
|
|
if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("%u %s\n", pid, cmd);
|
2007-09-30 03:56:01 +05:30
|
|
|
else
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("%u\n", pid);
|
2007-09-30 03:56:01 +05:30
|
|
|
} else
|
|
|
|
kill(pid, signo);
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int pgrep_main(int argc UNUSED_PARAM, char **argv)
|
2007-09-30 03:56:01 +05:30
|
|
|
{
|
2009-07-06 03:30:12 +05:30
|
|
|
unsigned pid;
|
|
|
|
int signo;
|
2007-09-30 03:56:01 +05:30
|
|
|
unsigned opt;
|
2009-07-06 03:30:12 +05:30
|
|
|
int scan_mask;
|
2007-09-30 03:56:01 +05:30
|
|
|
int matched_pid;
|
2009-07-06 03:30:12 +05:30
|
|
|
int sid2match, ppid2match;
|
2007-09-30 03:56:01 +05:30
|
|
|
char *cmd_last;
|
|
|
|
procps_status_t *proc;
|
|
|
|
/* These are initialized to 0 */
|
|
|
|
struct {
|
|
|
|
regex_t re_buffer;
|
|
|
|
regmatch_t re_match[1];
|
|
|
|
} Z;
|
|
|
|
#define re_buffer (Z.re_buffer)
|
|
|
|
#define re_match (Z.re_match )
|
|
|
|
|
|
|
|
memset(&Z, 0, sizeof(Z));
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
/* Parse -SIGNAL for pkill. Must be first option, if present */
|
|
|
|
signo = SIGTERM;
|
|
|
|
if (pkill && argv[1] && argv[1][0] == '-') {
|
|
|
|
int temp = get_signum(argv[1]+1);
|
|
|
|
if (temp != -1) {
|
|
|
|
signo = temp;
|
|
|
|
argv++;
|
2007-09-30 03:56:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
/* Parse remaining options */
|
|
|
|
ppid2match = -1;
|
|
|
|
sid2match = -1;
|
2016-07-07 01:28:02 +05:30
|
|
|
opt = getopt32(argv, "vlfxons:+P:+", &sid2match, &ppid2match);
|
2007-09-30 03:56:01 +05:30
|
|
|
argv += optind;
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
if (pkill && OPT_LIST) { /* -l: print the whole signal list */
|
|
|
|
print_signames();
|
|
|
|
return 0;
|
2007-09-30 03:56:01 +05:30
|
|
|
}
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
pid = getpid();
|
|
|
|
if (sid2match == 0)
|
|
|
|
sid2match = getsid(pid);
|
|
|
|
|
2009-08-15 02:03:10 +05:30
|
|
|
scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
|
2009-07-06 03:30:12 +05:30
|
|
|
if (OPT_FULL)
|
|
|
|
scan_mask |= PSSCAN_ARGVN;
|
|
|
|
|
|
|
|
/* One pattern is required, if no -s and no -P */
|
|
|
|
if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
|
2007-09-30 03:56:01 +05:30
|
|
|
bb_show_usage();
|
|
|
|
|
2009-07-06 03:30:12 +05:30
|
|
|
if (argv[0])
|
2013-02-28 20:21:55 +05:30
|
|
|
xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
|
2009-07-06 03:30:12 +05:30
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
matched_pid = 0;
|
|
|
|
cmd_last = NULL;
|
|
|
|
proc = NULL;
|
|
|
|
while ((proc = procps_scan(proc, scan_mask)) != NULL) {
|
|
|
|
char *cmd;
|
2009-07-06 03:30:12 +05:30
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
if (proc->pid == pid)
|
|
|
|
continue;
|
2009-07-06 03:30:12 +05:30
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
cmd = proc->argv0;
|
2008-07-18 00:09:36 +05:30
|
|
|
if (!cmd) {
|
2007-09-30 03:56:01 +05:30
|
|
|
cmd = proc->comm;
|
2008-07-18 00:09:36 +05:30
|
|
|
} else {
|
|
|
|
int i = proc->argv_len;
|
2009-08-15 02:03:10 +05:30
|
|
|
while (--i >= 0) {
|
|
|
|
if ((unsigned char)cmd[i] < ' ')
|
|
|
|
cmd[i] = ' ';
|
2008-07-18 00:09:36 +05:30
|
|
|
}
|
|
|
|
}
|
2009-07-06 03:30:12 +05:30
|
|
|
|
|
|
|
if (ppid2match >= 0 && ppid2match != proc->ppid)
|
|
|
|
continue;
|
2016-11-28 05:52:57 +05:30
|
|
|
if (sid2match >= 0 && sid2match != proc->sid)
|
2009-07-06 03:30:12 +05:30
|
|
|
continue;
|
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
/* NB: OPT_INVERT is always 0 or 1 */
|
2010-01-28 06:54:24 +05:30
|
|
|
if (!argv[0]
|
|
|
|
|| (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
|
|
|
|
&& (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
|
|
|
|
) ^ OPT_INVERT
|
2007-09-30 03:56:01 +05:30
|
|
|
) {
|
|
|
|
matched_pid = proc->pid;
|
|
|
|
if (OPT_LAST) {
|
|
|
|
free(cmd_last);
|
2007-09-30 22:06:02 +05:30
|
|
|
cmd_last = xstrdup(cmd);
|
2007-09-30 03:56:01 +05:30
|
|
|
continue;
|
|
|
|
}
|
2009-07-06 03:30:12 +05:30
|
|
|
act(proc->pid, cmd, signo);
|
2007-09-30 03:56:01 +05:30
|
|
|
if (OPT_FIRST)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-06 03:30:12 +05:30
|
|
|
|
2007-09-30 03:56:01 +05:30
|
|
|
if (cmd_last) {
|
2009-07-06 03:30:12 +05:30
|
|
|
act(matched_pid, cmd_last, signo);
|
2007-09-30 03:56:01 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(cmd_last);
|
|
|
|
}
|
|
|
|
return matched_pid == 0; /* return 1 if no processes listed/signaled */
|
|
|
|
}
|