blob: 902a3a75ee319cdf22b63f833fade37dbc737520 [file] [log] [blame]
Denis Vlasenko72e1c892007-09-29 22:26:01 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini pgrep/pkill implementation for busybox
4 *
5 * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko72e1c892007-09-29 22:26:01 +00008 */
Pere Orga5bc8c002011-04-11 03:29:49 +02009
10//usage:#define pgrep_trivial_usage
11//usage: "[-flnovx] [-s SID|-P PPID|PATTERN]"
12//usage:#define pgrep_full_usage "\n\n"
13//usage: "Display process(es) selected by regex PATTERN\n"
14//usage: "\nOptions:"
15//usage: "\n -l Show command name too"
16//usage: "\n -f Match against entire command line"
17//usage: "\n -n Show the newest process only"
18//usage: "\n -o Show the oldest process only"
19//usage: "\n -v Negate the match"
20//usage: "\n -x Match whole name (not substring)"
21//usage: "\n -s Match session ID (0 for current)"
22//usage: "\n -P Match parent process ID"
23//usage:
24//usage:#define pkill_trivial_usage
25//usage: "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]"
26//usage:#define pkill_full_usage "\n\n"
27//usage: "Send a signal to process(es) selected by regex PATTERN\n"
28//usage: "\nOptions:"
29//usage: "\n -l List all signals"
30//usage: "\n -f Match against entire command line"
31//usage: "\n -n Signal the newest process only"
32//usage: "\n -o Signal the oldest process only"
33//usage: "\n -v Negate the match"
34//usage: "\n -x Match whole name (not substring)"
35//usage: "\n -s Match session ID (0 for current)"
36//usage: "\n -P Match parent process ID"
37
Denis Vlasenko72e1c892007-09-29 22:26:01 +000038#include "libbb.h"
39#include "xregex.h"
40
41/* Idea taken from kill.c */
42#define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
43#define pkill (ENABLE_PKILL && applet_name[1] == 'k')
44
45enum {
Denys Vlasenko57dc5342009-07-06 00:00:12 +020046 /* "vlfxons:P:" */
47 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
48 OPTBIT_L,
49 OPTBIT_F,
50 OPTBIT_X,
51 OPTBIT_O,
52 OPTBIT_N,
53 OPTBIT_S,
54 OPTBIT_P,
Denis Vlasenko72e1c892007-09-29 22:26:01 +000055};
56
Denys Vlasenko57dc5342009-07-06 00:00:12 +020057#define OPT_INVERT (opt & (1 << OPTBIT_V))
58#define OPT_LIST (opt & (1 << OPTBIT_L))
59#define OPT_FULL (opt & (1 << OPTBIT_F))
60#define OPT_ANCHOR (opt & (1 << OPTBIT_X))
61#define OPT_FIRST (opt & (1 << OPTBIT_O))
62#define OPT_LAST (opt & (1 << OPTBIT_N))
63#define OPT_SID (opt & (1 << OPTBIT_S))
64#define OPT_PPID (opt & (1 << OPTBIT_P))
Denis Vlasenko72e1c892007-09-29 22:26:01 +000065
Denys Vlasenko57dc5342009-07-06 00:00:12 +020066static void act(unsigned pid, char *cmd, int signo)
Denis Vlasenko72e1c892007-09-29 22:26:01 +000067{
68 if (pgrep) {
Denys Vlasenko57dc5342009-07-06 00:00:12 +020069 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
Denis Vlasenko72e1c892007-09-29 22:26:01 +000070 printf("%d %s\n", pid, cmd);
71 else
72 printf("%d\n", pid);
73 } else
74 kill(pid, signo);
75}
76
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000077int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000078int pgrep_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko72e1c892007-09-29 22:26:01 +000079{
Denys Vlasenko57dc5342009-07-06 00:00:12 +020080 unsigned pid;
81 int signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000082 unsigned opt;
Denys Vlasenko57dc5342009-07-06 00:00:12 +020083 int scan_mask;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000084 int matched_pid;
Denys Vlasenko57dc5342009-07-06 00:00:12 +020085 int sid2match, ppid2match;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000086 char *cmd_last;
87 procps_status_t *proc;
88 /* These are initialized to 0 */
89 struct {
90 regex_t re_buffer;
91 regmatch_t re_match[1];
92 } Z;
93#define re_buffer (Z.re_buffer)
94#define re_match (Z.re_match )
95
96 memset(&Z, 0, sizeof(Z));
97
Denys Vlasenko57dc5342009-07-06 00:00:12 +020098 /* Parse -SIGNAL for pkill. Must be first option, if present */
99 signo = SIGTERM;
100 if (pkill && argv[1] && argv[1][0] == '-') {
101 int temp = get_signum(argv[1]+1);
102 if (temp != -1) {
103 signo = temp;
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000104 argv++;
105 }
106 }
107
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200108 /* Parse remaining options */
109 ppid2match = -1;
110 sid2match = -1;
111 opt_complementary = "s+:P+"; /* numeric opts */
112 opt = getopt32(argv, "vlfxons:P:", &sid2match, &ppid2match);
113 argv += optind;
114
115 if (pkill && OPT_LIST) { /* -l: print the whole signal list */
116 print_signames();
117 return 0;
118 }
119
120 pid = getpid();
121 if (sid2match == 0)
122 sid2match = getsid(pid);
123
Denys Vlasenko16d1e3c2009-08-14 22:33:10 +0200124 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200125 if (OPT_FULL)
126 scan_mask |= PSSCAN_ARGVN;
127
128 /* One pattern is required, if no -s and no -P */
129 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000130 bb_show_usage();
131
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200132 if (argv[0])
133 xregcomp(&re_buffer, argv[0], 0);
134
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000135 matched_pid = 0;
136 cmd_last = NULL;
137 proc = NULL;
138 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
139 char *cmd;
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200140
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000141 if (proc->pid == pid)
142 continue;
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200143
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000144 cmd = proc->argv0;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000145 if (!cmd) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000146 cmd = proc->comm;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000147 } else {
148 int i = proc->argv_len;
Denys Vlasenko16d1e3c2009-08-14 22:33:10 +0200149 while (--i >= 0) {
150 if ((unsigned char)cmd[i] < ' ')
151 cmd[i] = ' ';
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000152 }
153 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200154
155 if (ppid2match >= 0 && ppid2match != proc->ppid)
156 continue;
157 if (sid2match >= 0 && sid2match != proc->sid)
158 continue;
159
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000160 /* NB: OPT_INVERT is always 0 or 1 */
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100161 if (!argv[0]
162 || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
163 && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
164 ) ^ OPT_INVERT
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000165 ) {
166 matched_pid = proc->pid;
167 if (OPT_LAST) {
168 free(cmd_last);
Denis Vlasenko5fb09652007-09-30 16:36:02 +0000169 cmd_last = xstrdup(cmd);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000170 continue;
171 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200172 act(proc->pid, cmd, signo);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000173 if (OPT_FIRST)
174 break;
175 }
176 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200177
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000178 if (cmd_last) {
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200179 act(matched_pid, cmd_last, signo);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000180 if (ENABLE_FEATURE_CLEAN_UP)
181 free(cmd_last);
182 }
183 return matched_pid == 0; /* return 1 if no processes listed/signaled */
184}