Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 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 the GPL v2 or later, see the file LICENSE in this tarball. |
| 8 | */ |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 9 | #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 | |
| 16 | enum { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 17 | /* "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 Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 28 | #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 Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 36 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 37 | static void act(unsigned pid, char *cmd, int signo) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 38 | { |
| 39 | if (pgrep) { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 40 | if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */ |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 41 | printf("%d %s\n", pid, cmd); |
| 42 | else |
| 43 | printf("%d\n", pid); |
| 44 | } else |
| 45 | kill(pid, signo); |
| 46 | } |
| 47 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 48 | int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 49 | int pgrep_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 50 | { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 51 | unsigned pid; |
| 52 | int signo; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 53 | unsigned opt; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 54 | int scan_mask; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 55 | int matched_pid; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 56 | int sid2match, ppid2match; |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 57 | 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 Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 69 | /* 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 Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 75 | argv++; |
| 76 | } |
| 77 | } |
| 78 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 79 | /* 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 Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 95 | scan_mask = PSSCAN_COMM | PSSCAN_ARGV0; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 96 | 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 Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 101 | bb_show_usage(); |
| 102 | |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 103 | if (argv[0]) |
| 104 | xregcomp(&re_buffer, argv[0], 0); |
| 105 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 106 | matched_pid = 0; |
| 107 | cmd_last = NULL; |
| 108 | proc = NULL; |
| 109 | while ((proc = procps_scan(proc, scan_mask)) != NULL) { |
| 110 | char *cmd; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 111 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 112 | if (proc->pid == pid) |
| 113 | continue; |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 114 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 115 | cmd = proc->argv0; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 116 | if (!cmd) { |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 117 | cmd = proc->comm; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 118 | } else { |
| 119 | int i = proc->argv_len; |
Denys Vlasenko | 16d1e3c | 2009-08-14 22:33:10 +0200 | [diff] [blame] | 120 | while (--i >= 0) { |
| 121 | if ((unsigned char)cmd[i] < ' ') |
| 122 | cmd[i] = ' '; |
Denis Vlasenko | 3b3ca11 | 2008-07-17 18:39:36 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 125 | |
| 126 | if (ppid2match >= 0 && ppid2match != proc->ppid) |
| 127 | continue; |
| 128 | if (sid2match >= 0 && sid2match != proc->sid) |
| 129 | continue; |
| 130 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 131 | /* NB: OPT_INVERT is always 0 or 1 */ |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 132 | if (!argv[0] || |
| 133 | (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 134 | && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 135 | ) { |
| 136 | matched_pid = proc->pid; |
| 137 | if (OPT_LAST) { |
| 138 | free(cmd_last); |
Denis Vlasenko | 5fb0965 | 2007-09-30 16:36:02 +0000 | [diff] [blame] | 139 | cmd_last = xstrdup(cmd); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 140 | continue; |
| 141 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 142 | act(proc->pid, cmd, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 143 | if (OPT_FIRST) |
| 144 | break; |
| 145 | } |
| 146 | } |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 147 | |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 148 | if (cmd_last) { |
Denys Vlasenko | 57dc534 | 2009-07-06 00:00:12 +0200 | [diff] [blame] | 149 | act(matched_pid, cmd_last, signo); |
Denis Vlasenko | 72e1c89 | 2007-09-29 22:26:01 +0000 | [diff] [blame] | 150 | if (ENABLE_FEATURE_CLEAN_UP) |
| 151 | free(cmd_last); |
| 152 | } |
| 153 | return matched_pid == 0; /* return 1 if no processes listed/signaled */ |
| 154 | } |