diff options
Diffstat (limited to 'procps/pgrep.c')
| -rw-r--r-- | procps/pgrep.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/procps/pgrep.c b/procps/pgrep.c index 299e2dac7..adbdd08b7 100644 --- a/procps/pgrep.c +++ b/procps/pgrep.c | |||
| @@ -30,31 +30,49 @@ | |||
| 30 | //kbuild:lib-$(CONFIG_PKILL) += pgrep.o | 30 | //kbuild:lib-$(CONFIG_PKILL) += pgrep.o |
| 31 | 31 | ||
| 32 | //usage:#define pgrep_trivial_usage | 32 | //usage:#define pgrep_trivial_usage |
| 33 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 33 | //usage: "[-flanovx] [-s SID|-P PPID|PATTERN]" | 34 | //usage: "[-flanovx] [-s SID|-P PPID|PATTERN]" |
| 35 | //usage: ) | ||
| 36 | //usage: IF_PLATFORM_MINGW32( | ||
| 37 | //usage: "[-lvx] [-P PPID|PATTERN]" | ||
| 38 | //usage: ) | ||
| 34 | //usage:#define pgrep_full_usage "\n\n" | 39 | //usage:#define pgrep_full_usage "\n\n" |
| 35 | //usage: "Display process(es) selected by regex PATTERN\n" | 40 | //usage: "Display process(es) selected by regex PATTERN\n" |
| 36 | //usage: "\n -l Show command name too" | 41 | //usage: "\n -l Show command name too" |
| 42 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 37 | //usage: "\n -a Show command line too" | 43 | //usage: "\n -a Show command line too" |
| 38 | //usage: "\n -f Match against entire command line" | 44 | //usage: "\n -f Match against entire command line" |
| 39 | //usage: "\n -n Show the newest process only" | 45 | //usage: "\n -n Show the newest process only" |
| 40 | //usage: "\n -o Show the oldest process only" | 46 | //usage: "\n -o Show the oldest process only" |
| 47 | //usage: ) | ||
| 41 | //usage: "\n -v Negate the match" | 48 | //usage: "\n -v Negate the match" |
| 42 | //usage: "\n -x Match whole name (not substring)" | 49 | //usage: "\n -x Match whole name (not substring)" |
| 50 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 43 | //usage: "\n -s Match session ID (0 for current)" | 51 | //usage: "\n -s Match session ID (0 for current)" |
| 52 | //usage: ) | ||
| 44 | //usage: "\n -P Match parent process ID" | 53 | //usage: "\n -P Match parent process ID" |
| 45 | //usage: | 54 | //usage: |
| 46 | //usage:#define pkill_trivial_usage | 55 | //usage:#define pkill_trivial_usage |
| 56 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 47 | //usage: "[-l|-SIGNAL] [-xfvnoe] [-s SID|-P PPID|PATTERN]" | 57 | //usage: "[-l|-SIGNAL] [-xfvnoe] [-s SID|-P PPID|PATTERN]" |
| 58 | //usage: ) | ||
| 59 | //usage: IF_PLATFORM_MINGW32( | ||
| 60 | //usage: "[-l|-SIGNAL] [-xve] [-P PPID|PATTERN]" | ||
| 61 | //usage: ) | ||
| 48 | //usage:#define pkill_full_usage "\n\n" | 62 | //usage:#define pkill_full_usage "\n\n" |
| 49 | //usage: "Send signal to processes selected by regex PATTERN\n" | 63 | //usage: "Send signal to processes selected by regex PATTERN\n" |
| 50 | //usage: "\n -l List all signals" | 64 | //usage: "\n -l List all signals" |
| 51 | //usage: "\n -x Match whole name (not substring)" | 65 | //usage: "\n -x Match whole name (not substring)" |
| 66 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 52 | //usage: "\n -f Match against entire command line" | 67 | //usage: "\n -f Match against entire command line" |
| 53 | //usage: "\n -s SID Match session ID (0 for current)" | 68 | //usage: "\n -s SID Match session ID (0 for current)" |
| 69 | //usage: ) | ||
| 54 | //usage: "\n -P PPID Match parent process ID" | 70 | //usage: "\n -P PPID Match parent process ID" |
| 55 | //usage: "\n -v Negate the match" | 71 | //usage: "\n -v Negate the match" |
| 72 | //usage: IF_NOT_PLATFORM_MINGW32( | ||
| 56 | //usage: "\n -n Signal the newest process only" | 73 | //usage: "\n -n Signal the newest process only" |
| 57 | //usage: "\n -o Signal the oldest process only" | 74 | //usage: "\n -o Signal the oldest process only" |
| 75 | //usage: ) | ||
| 58 | //usage: "\n -e Display name and PID of the process being killed" | 76 | //usage: "\n -e Display name and PID of the process being killed" |
| 59 | 77 | ||
| 60 | #include "libbb.h" | 78 | #include "libbb.h" |
| @@ -68,25 +86,47 @@ enum { | |||
| 68 | /* "vlafxones:+P:+" */ | 86 | /* "vlafxones:+P:+" */ |
| 69 | OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */ | 87 | OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */ |
| 70 | OPTBIT_L, | 88 | OPTBIT_L, |
| 89 | #if !ENABLE_PLATFORM_MINGW32 | ||
| 71 | OPTBIT_A, | 90 | OPTBIT_A, |
| 72 | OPTBIT_F, | 91 | OPTBIT_F, |
| 92 | #else | ||
| 93 | #define OPTBIT_A OPTBIT_L | ||
| 94 | #endif | ||
| 73 | OPTBIT_X, | 95 | OPTBIT_X, |
| 96 | #if !ENABLE_PLATFORM_MINGW32 | ||
| 74 | OPTBIT_O, | 97 | OPTBIT_O, |
| 75 | OPTBIT_N, | 98 | OPTBIT_N, |
| 99 | #endif | ||
| 76 | OPTBIT_E, /* should be pkill-only, do we care? */ | 100 | OPTBIT_E, /* should be pkill-only, do we care? */ |
| 101 | #if !ENABLE_PLATFORM_MINGW32 | ||
| 77 | OPTBIT_S, | 102 | OPTBIT_S, |
| 103 | #endif | ||
| 78 | OPTBIT_P, | 104 | OPTBIT_P, |
| 79 | }; | 105 | }; |
| 80 | 106 | ||
| 81 | #define OPT_INVERT (opt & (1 << OPTBIT_V)) | 107 | #define OPT_INVERT (opt & (1 << OPTBIT_V)) |
| 82 | #define OPT_LIST (opt & (1 << OPTBIT_L)) | 108 | #define OPT_LIST (opt & (1 << OPTBIT_L)) |
| 109 | #if ENABLE_PLATFORM_MINGW32 | ||
| 110 | #define OPT_LISTFULL (0) | ||
| 111 | #define OPT_FULL (0) | ||
| 112 | #else | ||
| 83 | #define OPT_LISTFULL (opt & (1 << OPTBIT_A)) | 113 | #define OPT_LISTFULL (opt & (1 << OPTBIT_A)) |
| 84 | #define OPT_FULL (opt & (1 << OPTBIT_F)) | 114 | #define OPT_FULL (opt & (1 << OPTBIT_F)) |
| 115 | #endif | ||
| 85 | #define OPT_ANCHOR (opt & (1 << OPTBIT_X)) | 116 | #define OPT_ANCHOR (opt & (1 << OPTBIT_X)) |
| 117 | #if ENABLE_PLATFORM_MINGW32 | ||
| 118 | #define OPT_FIRST (0) | ||
| 119 | #define OPT_LAST (0) | ||
| 120 | #else | ||
| 86 | #define OPT_FIRST (opt & (1 << OPTBIT_O)) | 121 | #define OPT_FIRST (opt & (1 << OPTBIT_O)) |
| 87 | #define OPT_LAST (opt & (1 << OPTBIT_N)) | 122 | #define OPT_LAST (opt & (1 << OPTBIT_N)) |
| 123 | #endif | ||
| 88 | #define OPT_ECHO (opt & (1 << OPTBIT_E)) | 124 | #define OPT_ECHO (opt & (1 << OPTBIT_E)) |
| 125 | #if ENABLE_PLATFORM_MINGW32 | ||
| 126 | #define OPT_SID (0) | ||
| 127 | #else | ||
| 89 | #define OPT_SID (opt & (1 << OPTBIT_S)) | 128 | #define OPT_SID (opt & (1 << OPTBIT_S)) |
| 129 | #endif | ||
| 90 | #define OPT_PPID (opt & (1 << OPTBIT_P)) | 130 | #define OPT_PPID (opt & (1 << OPTBIT_P)) |
| 91 | 131 | ||
| 92 | static void act(unsigned pid, char *cmd, int signo) | 132 | static void act(unsigned pid, char *cmd, int signo) |
| @@ -112,7 +152,12 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 112 | unsigned opt; | 152 | unsigned opt; |
| 113 | int scan_mask; | 153 | int scan_mask; |
| 114 | int matched_pid; | 154 | int matched_pid; |
| 155 | #if ENABLE_PLATFORM_MINGW32 | ||
| 156 | const int sid2match = -1; | ||
| 157 | int ppid2match; | ||
| 158 | #else | ||
| 115 | int sid2match, ppid2match; | 159 | int sid2match, ppid2match; |
| 160 | #endif | ||
| 116 | char *cmd_last; | 161 | char *cmd_last; |
| 117 | procps_status_t *proc; | 162 | procps_status_t *proc; |
| 118 | /* These are initialized to 0 */ | 163 | /* These are initialized to 0 */ |
| @@ -137,8 +182,12 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 137 | 182 | ||
| 138 | /* Parse remaining options */ | 183 | /* Parse remaining options */ |
| 139 | ppid2match = -1; | 184 | ppid2match = -1; |
| 185 | #if !ENABLE_PLATFORM_MINGW32 | ||
| 140 | sid2match = -1; | 186 | sid2match = -1; |
| 141 | opt = getopt32(argv, "vlafxones:+P:+", &sid2match, &ppid2match); | 187 | opt = getopt32(argv, "vlafxones:+P:+", &sid2match, &ppid2match); |
| 188 | #else | ||
| 189 | opt = getopt32(argv, "vlxeP:+", &ppid2match); | ||
| 190 | #endif | ||
| 142 | argv += optind; | 191 | argv += optind; |
| 143 | 192 | ||
| 144 | if (pkill && OPT_LIST) { /* -l: print the whole signal list */ | 193 | if (pkill && OPT_LIST) { /* -l: print the whole signal list */ |
| @@ -147,8 +196,10 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 147 | } | 196 | } |
| 148 | 197 | ||
| 149 | pid = getpid(); | 198 | pid = getpid(); |
| 199 | #if !ENABLE_PLATFORM_MINGW32 | ||
| 150 | if (sid2match == 0) | 200 | if (sid2match == 0) |
| 151 | sid2match = getsid(pid); | 201 | sid2match = getsid(pid); |
| 202 | #endif | ||
| 152 | 203 | ||
| 153 | scan_mask = PSSCAN_COMM | PSSCAN_ARGV0; | 204 | scan_mask = PSSCAN_COMM | PSSCAN_ARGV0; |
| 154 | if (OPT_FULL) | 205 | if (OPT_FULL) |
| @@ -180,6 +231,9 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 180 | } | 231 | } |
| 181 | 232 | ||
| 182 | cmdlen = -1; | 233 | cmdlen = -1; |
| 234 | #if ENABLE_PLATFORM_MINGW32 | ||
| 235 | cmd = proc->comm; | ||
| 236 | #else | ||
| 183 | cmd = proc->argv0; | 237 | cmd = proc->argv0; |
| 184 | if (!cmd) { | 238 | if (!cmd) { |
| 185 | cmd = proc->comm; | 239 | cmd = proc->comm; |
| @@ -199,6 +253,7 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv) | |||
| 199 | cmd[i] = ' '; | 253 | cmd[i] = ' '; |
| 200 | } | 254 | } |
| 201 | } | 255 | } |
| 256 | #endif | ||
| 202 | 257 | ||
| 203 | if (OPT_INVERT) { | 258 | if (OPT_INVERT) { |
| 204 | /* "pgrep -v -P1 firefox" means "not (ppid=1 AND name=firefox)" | 259 | /* "pgrep -v -P1 firefox" means "not (ppid=1 AND name=firefox)" |
