move remaining help text from include/usage.src.h
[oweals/busybox.git] / procps / pgrep.c
1 /* 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 GPLv2 or later, see file LICENSE in this source tree.
8  */
9
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
38 #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
45 enum {
46         /* "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,
55 };
56
57 #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))
65
66 static void act(unsigned pid, char *cmd, int signo)
67 {
68         if (pgrep) {
69                 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
70                         printf("%d %s\n", pid, cmd);
71                 else
72                         printf("%d\n", pid);
73         } else
74                 kill(pid, signo);
75 }
76
77 int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
78 int pgrep_main(int argc UNUSED_PARAM, char **argv)
79 {
80         unsigned pid;
81         int signo;
82         unsigned opt;
83         int scan_mask;
84         int matched_pid;
85         int sid2match, ppid2match;
86         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
98         /* 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;
104                         argv++;
105                 }
106         }
107
108         /* 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
124         scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
125         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]))
130                 bb_show_usage();
131
132         if (argv[0])
133                 xregcomp(&re_buffer, argv[0], 0);
134
135         matched_pid = 0;
136         cmd_last = NULL;
137         proc = NULL;
138         while ((proc = procps_scan(proc, scan_mask)) != NULL) {
139                 char *cmd;
140
141                 if (proc->pid == pid)
142                         continue;
143
144                 cmd = proc->argv0;
145                 if (!cmd) {
146                         cmd = proc->comm;
147                 } else {
148                         int i = proc->argv_len;
149                         while (--i >= 0) {
150                                 if ((unsigned char)cmd[i] < ' ')
151                                         cmd[i] = ' ';
152                         }
153                 }
154
155                 if (ppid2match >= 0 && ppid2match != proc->ppid)
156                         continue;
157                 if (sid2match >= 0  && sid2match != proc->sid)
158                         continue;
159
160                 /* NB: OPT_INVERT is always 0 or 1 */
161                 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
165                 ) {
166                         matched_pid = proc->pid;
167                         if (OPT_LAST) {
168                                 free(cmd_last);
169                                 cmd_last = xstrdup(cmd);
170                                 continue;
171                         }
172                         act(proc->pid, cmd, signo);
173                         if (OPT_FIRST)
174                                 break;
175                 }
176         }
177
178         if (cmd_last) {
179                 act(matched_pid, cmd_last, signo);
180                 if (ENABLE_FEATURE_CLEAN_UP)
181                         free(cmd_last);
182         }
183         return matched_pid == 0; /* return 1 if no processes listed/signaled */
184 }