getopt32: add new syntax of 'o:+' and 'o:*' for -o NUM and -o LIST
[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:     "\n        -l      Show command name too"
15 //usage:     "\n        -f      Match against entire command line"
16 //usage:     "\n        -n      Show the newest process only"
17 //usage:     "\n        -o      Show the oldest process only"
18 //usage:     "\n        -v      Negate the match"
19 //usage:     "\n        -x      Match whole name (not substring)"
20 //usage:     "\n        -s      Match session ID (0 for current)"
21 //usage:     "\n        -P      Match parent process ID"
22 //usage:
23 //usage:#define pkill_trivial_usage
24 //usage:       "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]"
25 //usage:#define pkill_full_usage "\n\n"
26 //usage:       "Send a signal to process(es) selected by regex PATTERN\n"
27 //usage:     "\n        -l      List all signals"
28 //usage:     "\n        -f      Match against entire command line"
29 //usage:     "\n        -n      Signal the newest process only"
30 //usage:     "\n        -o      Signal the oldest process only"
31 //usage:     "\n        -v      Negate the match"
32 //usage:     "\n        -x      Match whole name (not substring)"
33 //usage:     "\n        -s      Match session ID (0 for current)"
34 //usage:     "\n        -P      Match parent process ID"
35
36 #include "libbb.h"
37 #include "xregex.h"
38
39 /* Idea taken from kill.c */
40 #define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
41 #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
42
43 enum {
44         /* "vlfxons:P:" */
45         OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
46         OPTBIT_L,
47         OPTBIT_F,
48         OPTBIT_X,
49         OPTBIT_O,
50         OPTBIT_N,
51         OPTBIT_S,
52         OPTBIT_P,
53 };
54
55 #define OPT_INVERT      (opt & (1 << OPTBIT_V))
56 #define OPT_LIST        (opt & (1 << OPTBIT_L))
57 #define OPT_FULL        (opt & (1 << OPTBIT_F))
58 #define OPT_ANCHOR      (opt & (1 << OPTBIT_X))
59 #define OPT_FIRST       (opt & (1 << OPTBIT_O))
60 #define OPT_LAST        (opt & (1 << OPTBIT_N))
61 #define OPT_SID         (opt & (1 << OPTBIT_S))
62 #define OPT_PPID        (opt & (1 << OPTBIT_P))
63
64 static void act(unsigned pid, char *cmd, int signo)
65 {
66         if (pgrep) {
67                 if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
68                         printf("%u %s\n", pid, cmd);
69                 else
70                         printf("%u\n", pid);
71         } else
72                 kill(pid, signo);
73 }
74
75 int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int pgrep_main(int argc UNUSED_PARAM, char **argv)
77 {
78         unsigned pid;
79         int signo;
80         unsigned opt;
81         int scan_mask;
82         int matched_pid;
83         int sid2match, ppid2match;
84         char *cmd_last;
85         procps_status_t *proc;
86         /* These are initialized to 0 */
87         struct {
88                 regex_t re_buffer;
89                 regmatch_t re_match[1];
90         } Z;
91 #define re_buffer (Z.re_buffer)
92 #define re_match  (Z.re_match )
93
94         memset(&Z, 0, sizeof(Z));
95
96         /* Parse -SIGNAL for pkill. Must be first option, if present */
97         signo = SIGTERM;
98         if (pkill && argv[1] && argv[1][0] == '-') {
99                 int temp = get_signum(argv[1]+1);
100                 if (temp != -1) {
101                         signo = temp;
102                         argv++;
103                 }
104         }
105
106         /* Parse remaining options */
107         ppid2match = -1;
108         sid2match = -1;
109         opt = getopt32(argv, "vlfxons:+P:+", &sid2match, &ppid2match);
110         argv += optind;
111
112         if (pkill && OPT_LIST) { /* -l: print the whole signal list */
113                 print_signames();
114                 return 0;
115         }
116
117         pid = getpid();
118         if (sid2match == 0)
119                 sid2match = getsid(pid);
120
121         scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
122         if (OPT_FULL)
123                 scan_mask |= PSSCAN_ARGVN;
124
125         /* One pattern is required, if no -s and no -P */
126         if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
127                 bb_show_usage();
128
129         if (argv[0])
130                 xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
131
132         matched_pid = 0;
133         cmd_last = NULL;
134         proc = NULL;
135         while ((proc = procps_scan(proc, scan_mask)) != NULL) {
136                 char *cmd;
137
138                 if (proc->pid == pid)
139                         continue;
140
141                 cmd = proc->argv0;
142                 if (!cmd) {
143                         cmd = proc->comm;
144                 } else {
145                         int i = proc->argv_len;
146                         while (--i >= 0) {
147                                 if ((unsigned char)cmd[i] < ' ')
148                                         cmd[i] = ' ';
149                         }
150                 }
151
152                 if (ppid2match >= 0 && ppid2match != proc->ppid)
153                         continue;
154                 if (sid2match >= 0  && sid2match != proc->sid)
155                         continue;
156
157                 /* NB: OPT_INVERT is always 0 or 1 */
158                 if (!argv[0]
159                  || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
160                     && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
161                     ) ^ OPT_INVERT
162                 ) {
163                         matched_pid = proc->pid;
164                         if (OPT_LAST) {
165                                 free(cmd_last);
166                                 cmd_last = xstrdup(cmd);
167                                 continue;
168                         }
169                         act(proc->pid, cmd, signo);
170                         if (OPT_FIRST)
171                                 break;
172                 }
173         }
174
175         if (cmd_last) {
176                 act(matched_pid, cmd_last, signo);
177                 if (ENABLE_FEATURE_CLEAN_UP)
178                         free(cmd_last);
179         }
180         return matched_pid == 0; /* return 1 if no processes listed/signaled */
181 }