123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- #include "libbb.h"
- #include "xregex.h"
- #define pgrep (ENABLE_PGREP && (!ENABLE_PKILL || applet_name[1] == 'g'))
- #define pkill (ENABLE_PKILL && (!ENABLE_PGREP || applet_name[1] == 'k'))
- enum {
-
- OPTBIT_V = 0,
- OPTBIT_L,
- OPTBIT_A,
- OPTBIT_F,
- OPTBIT_X,
- OPTBIT_O,
- OPTBIT_N,
- OPTBIT_S,
- OPTBIT_P,
- };
- #define OPT_INVERT (opt & (1 << OPTBIT_V))
- #define OPT_LIST (opt & (1 << OPTBIT_L))
- #define OPT_LISTFULL (opt & (1 << OPTBIT_A))
- #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))
- static void act(unsigned pid, char *cmd, int signo)
- {
- if (pgrep) {
- if (option_mask32 & ((1 << OPTBIT_L)|(1 << OPTBIT_A)))
- printf("%u %s\n", pid, cmd);
- else
- printf("%u\n", pid);
- } else
- kill(pid, signo);
- }
- int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int pgrep_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned pid;
- int signo;
- unsigned opt;
- int scan_mask;
- int matched_pid;
- int sid2match, ppid2match;
- char *cmd_last;
- procps_status_t *proc;
-
- 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));
-
- signo = SIGTERM;
- if (pkill && argv[1] && argv[1][0] == '-') {
- int temp = get_signum(argv[1]+1);
- if (temp != -1) {
- signo = temp;
- argv++;
- }
- }
-
- ppid2match = -1;
- sid2match = -1;
- opt = getopt32(argv, "vlafxons:+P:+", &sid2match, &ppid2match);
- argv += optind;
- if (pkill && OPT_LIST) {
- print_signames();
- return 0;
- }
- pid = getpid();
- if (sid2match == 0)
- sid2match = getsid(pid);
- scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
- if (OPT_FULL)
- scan_mask |= PSSCAN_ARGVN;
-
- if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
- bb_show_usage();
- if (argv[0])
- xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
- matched_pid = 0;
- cmd_last = NULL;
- proc = NULL;
- while ((proc = procps_scan(proc, scan_mask)) != NULL) {
- char *cmd;
- int cmdlen, match;
- if (proc->pid == pid)
- continue;
- if (!OPT_INVERT) {
-
- if (ppid2match >= 0 && ppid2match != proc->ppid)
- continue;
- if (sid2match >= 0 && sid2match != proc->sid)
- continue;
- }
- cmdlen = -1;
- cmd = proc->argv0;
- if (!cmd) {
- cmd = proc->comm;
- } else {
- int i = proc->argv_len;
- if (!OPT_LISTFULL)
- cmdlen = strlen(cmd);
-
- if (i && cmd[i-1] == '\0')
- i--;
- while (--i >= 0) {
- if ((unsigned char)cmd[i] < ' ')
- cmd[i] = ' ';
- }
- }
- if (OPT_INVERT) {
-
- if (ppid2match >= 0 && ppid2match != proc->ppid)
- goto got_it;
- if (sid2match >= 0 && sid2match != proc->sid)
- goto got_it;
- }
- match = !argv[0];
- if (!match) {
- again:
- match = (regexec(&re_buffer, cmd, 1, re_match, 0) == 0);
- if (!match && cmd != proc->comm) {
-
- cmdlen = -1;
- cmd = proc->comm;
- goto again;
- }
- if (match && OPT_ANCHOR) {
-
- match = (re_match[0].rm_so == 0 && cmd[re_match[0].rm_eo] == '\0');
- }
- }
-
- if (match ^ OPT_INVERT) {
- got_it:
- matched_pid = proc->pid;
- if (OPT_LAST) {
- free(cmd_last);
- cmd_last = xstrdup(cmd);
- continue;
- }
- if (cmdlen >= 0)
- cmd[cmdlen] = '\0';
- act(proc->pid, cmd, signo);
- if (OPT_FIRST)
- break;
- }
- }
- if (cmd_last) {
- act(matched_pid, cmd_last, signo);
- if (ENABLE_FEATURE_CLEAN_UP)
- free(cmd_last);
- }
- return matched_pid == 0;
- }
|