blob: 3a717ecff071ce2e4c9dd0f942f468c9e92c5c47 [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 *
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 */
Denis Vlasenko72e1c892007-09-29 22:26:01 +00009#include "libbb.h"
10#include "xregex.h"
11
12/* Idea taken from kill.c */
13#define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
14#define pkill (ENABLE_PKILL && applet_name[1] == 'k')
15
16enum {
Denys Vlasenko57dc5342009-07-06 00:00:12 +020017 /* "vlfxons:P:" */
18 OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
19 OPTBIT_L,
20 OPTBIT_F,
21 OPTBIT_X,
22 OPTBIT_O,
23 OPTBIT_N,
24 OPTBIT_S,
25 OPTBIT_P,
Denis Vlasenko72e1c892007-09-29 22:26:01 +000026};
27
Denys Vlasenko57dc5342009-07-06 00:00:12 +020028#define OPT_INVERT (opt & (1 << OPTBIT_V))
29#define OPT_LIST (opt & (1 << OPTBIT_L))
30#define OPT_FULL (opt & (1 << OPTBIT_F))
31#define OPT_ANCHOR (opt & (1 << OPTBIT_X))
32#define OPT_FIRST (opt & (1 << OPTBIT_O))
33#define OPT_LAST (opt & (1 << OPTBIT_N))
34#define OPT_SID (opt & (1 << OPTBIT_S))
35#define OPT_PPID (opt & (1 << OPTBIT_P))
Denis Vlasenko72e1c892007-09-29 22:26:01 +000036
Denys Vlasenko57dc5342009-07-06 00:00:12 +020037static void act(unsigned pid, char *cmd, int signo)
Denis Vlasenko72e1c892007-09-29 22:26:01 +000038{
39 if (pgrep) {
Denys Vlasenko57dc5342009-07-06 00:00:12 +020040 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
Denis Vlasenko72e1c892007-09-29 22:26:01 +000041 printf("%d %s\n", pid, cmd);
42 else
43 printf("%d\n", pid);
44 } else
45 kill(pid, signo);
46}
47
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000049int pgrep_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko72e1c892007-09-29 22:26:01 +000050{
Denys Vlasenko57dc5342009-07-06 00:00:12 +020051 unsigned pid;
52 int signo;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000053 unsigned opt;
Denys Vlasenko57dc5342009-07-06 00:00:12 +020054 int scan_mask;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000055 int matched_pid;
Denys Vlasenko57dc5342009-07-06 00:00:12 +020056 int sid2match, ppid2match;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000057 char *cmd_last;
58 procps_status_t *proc;
59 /* These are initialized to 0 */
60 struct {
61 regex_t re_buffer;
62 regmatch_t re_match[1];
63 } Z;
64#define re_buffer (Z.re_buffer)
65#define re_match (Z.re_match )
66
67 memset(&Z, 0, sizeof(Z));
68
Denys Vlasenko57dc5342009-07-06 00:00:12 +020069 /* Parse -SIGNAL for pkill. Must be first option, if present */
70 signo = SIGTERM;
71 if (pkill && argv[1] && argv[1][0] == '-') {
72 int temp = get_signum(argv[1]+1);
73 if (temp != -1) {
74 signo = temp;
Denis Vlasenko72e1c892007-09-29 22:26:01 +000075 argv++;
76 }
77 }
78
Denys Vlasenko57dc5342009-07-06 00:00:12 +020079 /* Parse remaining options */
80 ppid2match = -1;
81 sid2match = -1;
82 opt_complementary = "s+:P+"; /* numeric opts */
83 opt = getopt32(argv, "vlfxons:P:", &sid2match, &ppid2match);
84 argv += optind;
85
86 if (pkill && OPT_LIST) { /* -l: print the whole signal list */
87 print_signames();
88 return 0;
89 }
90
91 pid = getpid();
92 if (sid2match == 0)
93 sid2match = getsid(pid);
94
Denys Vlasenko16d1e3c2009-08-14 22:33:10 +020095 scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
Denys Vlasenko57dc5342009-07-06 00:00:12 +020096 if (OPT_FULL)
97 scan_mask |= PSSCAN_ARGVN;
98
99 /* One pattern is required, if no -s and no -P */
100 if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000101 bb_show_usage();
102
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200103 if (argv[0])
104 xregcomp(&re_buffer, argv[0], 0);
105
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000106 matched_pid = 0;
107 cmd_last = NULL;
108 proc = NULL;
109 while ((proc = procps_scan(proc, scan_mask)) != NULL) {
110 char *cmd;
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200111
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000112 if (proc->pid == pid)
113 continue;
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200114
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000115 cmd = proc->argv0;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000116 if (!cmd) {
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000117 cmd = proc->comm;
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000118 } else {
119 int i = proc->argv_len;
Denys Vlasenko16d1e3c2009-08-14 22:33:10 +0200120 while (--i >= 0) {
121 if ((unsigned char)cmd[i] < ' ')
122 cmd[i] = ' ';
Denis Vlasenko3b3ca112008-07-17 18:39:36 +0000123 }
124 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200125
126 if (ppid2match >= 0 && ppid2match != proc->ppid)
127 continue;
128 if (sid2match >= 0 && sid2match != proc->sid)
129 continue;
130
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000131 /* NB: OPT_INVERT is always 0 or 1 */
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200132 if (!argv[0] ||
133 (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000134 && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000135 ) {
136 matched_pid = proc->pid;
137 if (OPT_LAST) {
138 free(cmd_last);
Denis Vlasenko5fb09652007-09-30 16:36:02 +0000139 cmd_last = xstrdup(cmd);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000140 continue;
141 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200142 act(proc->pid, cmd, signo);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000143 if (OPT_FIRST)
144 break;
145 }
146 }
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200147
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000148 if (cmd_last) {
Denys Vlasenko57dc5342009-07-06 00:00:12 +0200149 act(matched_pid, cmd_last, signo);
Denis Vlasenko72e1c892007-09-29 22:26:01 +0000150 if (ENABLE_FEATURE_CLEAN_UP)
151 free(cmd_last);
152 }
153 return matched_pid == 0; /* return 1 if no processes listed/signaled */
154}