pgrep.c (6821B)
1 /* see LICENSE file for copyright and license details */ 2 #include <sys/types.h> 3 4 #include <ctype.h> 5 #include <errno.h> 6 #include <libgen.h> 7 #include <limits.h> 8 #include <pwd.h> 9 #include <regex.h> 10 #include <signal.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 16 #include "paths.h" 17 #include "proc.h" 18 #include "sig.h" 19 #include "util.h" 20 21 enum { 22 PGREP_fflag = 1 << 0, /* match against full cmdline, not just comm */ 23 PGREP_lflag = 1 << 1, /* list pid and comm */ 24 PGREP_aflag = 1 << 2, /* list pid and full cmdline */ 25 PGREP_xflag = 1 << 3, /* require an exact match */ 26 PGREP_vflag = 1 << 4, /* invert: print non-matching pids */ 27 PGREP_cflag = 1 << 5, /* print a match count, not the pids */ 28 }; 29 30 struct hit { 31 pid_t pid; 32 unsigned long long starttime; 33 }; 34 35 /* pkill is pgrep that sends a signal to what it finds instead of listing it: 36 * the one program, as grep is egrep and fgrep, so what is a match is written 37 * once */ 38 static int killing, signo = SIGTERM; 39 static int flags; 40 static regex_t preg; 41 static uid_t user_uid; 42 static int have_user; 43 static pid_t self; 44 static struct hit *hits; 45 static size_t nhits, hitscap; 46 static int count; 47 48 /* newest/oldest are mutually exclusive with each other and with 49 * printing every match as it is found, so they buffer every match 50 * instead of streaming it: 0 means "print as found" */ 51 enum { PGREP_NONE = 0, PGREP_NEWEST, PGREP_OLDEST }; 52 static int select_mode; 53 54 static int 55 matches(const char *target) 56 { 57 regmatch_t m; 58 int r; 59 60 r = regexec(&preg, target, 1, &m, 0); 61 if (r != 0) 62 return 0; 63 if (flags & PGREP_xflag) 64 return m.rm_so == 0 && (size_t)m.rm_eo == strlen(target); 65 return 1; 66 } 67 68 static void 69 record(pid_t pid, unsigned long long starttime) 70 { 71 if (nhits == hitscap) { 72 hitscap = hitscap ? hitscap * 2 : 64; 73 hits = ereallocarray(hits, hitscap, sizeof(*hits)); 74 } 75 hits[nhits].pid = pid; 76 hits[nhits].starttime = starttime; 77 nhits++; 78 } 79 80 static void 81 show(pid_t pid, const char *comm) 82 { 83 char cmdline[BUFSIZ]; 84 85 count++; 86 if (killing) { 87 if (kill(pid, signo) < 0) 88 weprintf("kill %d:", pid); 89 return; 90 } 91 if (flags & PGREP_cflag) 92 return; 93 if (flags & PGREP_aflag) { 94 if (parsecmdline(pid, cmdline, sizeof(cmdline)) < 0) 95 printf("%d %s\n", pid, comm); 96 else 97 printf("%d %s\n", pid, cmdline); 98 } else if (flags & PGREP_lflag) { 99 printf("%d %s\n", pid, comm); 100 } else { 101 printf("%d\n", pid); 102 } 103 } 104 105 static int 106 pgrepr(const struct procinfo *p, void *arg) 107 { 108 struct procstat ps; 109 char cmdline[BUFSIZ]; 110 const char *target = p->comm; 111 int matched; 112 113 (void)arg; 114 if (p->pid == self) 115 return 0; 116 if (have_user && p->euid != user_uid) 117 return 0; 118 if ((flags & PGREP_fflag) && proc_args(p->pid, cmdline, sizeof(cmdline)) == 0) 119 target = cmdline; 120 121 matched = matches(target); 122 if (flags & PGREP_vflag) 123 matched = !matched; 124 if (!matched) 125 return 0; 126 127 ps.starttime = (unsigned long long)p->start; 128 if (select_mode != PGREP_NONE) 129 record(p->pid, ps.starttime); 130 else 131 show(p->pid, p->comm); 132 return 0; 133 } 134 135 static void 136 usage(void) 137 { 138 if (killing) 139 eprintf("usage: %s [-signal] [-fxv] [-u user] [-n | -o] pattern\n", argv0); 140 eprintf("usage: %s [-flaxvc] [-u user] [-n | -o] pattern\n", argv0); 141 } 142 143 /* what the program was started as, without its directory */ 144 static int 145 invoked_as(const char *name) 146 { 147 const char *base = strrchr(argv0, '/'); 148 149 return strcmp(base ? base + 1 : argv0, name) == 0; 150 } 151 152 // ?man pgrep: list processes by name 153 // ?man arguments: pattern 154 // ?man pgrep matches pattern, a POSIX extended regular expression, 155 // ?man against each running process's name (or full command line 156 // ?man with -f) and prints the pid of every match, one per line. run as 157 // ?man pkill it sends them a signal instead, TERM unless -signal names one 158 int 159 main(int argc, char *argv[]) 160 { 161 char namebuf[PATH_MAX]; 162 struct procstat ps; 163 struct passwd *pw; 164 const char *user = NULL; 165 size_t i, pick; 166 167 argv0 = argv[0]; 168 if (invoked_as("pkill")) { 169 killing = 1; 170 // a leading -TERM or -9 is the signal, as kill takes it 171 if (argc > 1 && argv[1][0] == '-' && argv[1][1] 172 && (isdigit((unsigned char)argv[1][1]) || isupper((unsigned char)argv[1][1]))) { 173 if (isdigit((unsigned char)argv[1][1])) 174 signo = (int)estrtonum(argv[1] + 1, 0, INT_MAX); 175 else if (str2sig(argv[1] + 1, &signo) < 0) 176 eprintf("%s: bad signal name\n", argv[1] + 1); 177 argc--; 178 argv++; 179 argv[0] = argv[-1]; 180 } 181 } 182 183 ARGBEGIN 184 { 185 // ?man -f: match the full command line instead of just the name 186 case 'f': 187 flags |= PGREP_fflag; 188 break; 189 // ?man -l: list the matched name alongside each pid 190 case 'l': 191 flags |= PGREP_lflag; 192 break; 193 // ?man -a: list the full command line alongside each pid 194 case 'a': 195 flags |= PGREP_aflag; 196 break; 197 // ?man -x: require pattern to match the whole name or cmdline 198 case 'x': 199 flags |= PGREP_xflag; 200 break; 201 // ?man -v: invert the match, printing non-matching pids 202 case 'v': 203 flags |= PGREP_vflag; 204 break; 205 // ?man -c: print a count of matches instead of their pids 206 case 'c': 207 flags |= PGREP_cflag; 208 break; 209 // ?man -u:user only match processes owned by user (name or uid) 210 case 'u': 211 user = EARGF(usage()); 212 break; 213 // ?man -n{sel}: select only the most recently started match 214 case 'n': 215 select_mode = PGREP_NEWEST; 216 break; 217 // ?man -o{sel}: select only the oldest match 218 case 'o': 219 select_mode = PGREP_OLDEST; 220 break; 221 default: 222 usage(); 223 } 224 ARGEND; 225 226 if (argc != 1) 227 usage(); 228 229 if (user) { 230 errno = 0; 231 pw = getpwnam(user); 232 if (pw) { 233 user_uid = pw->pw_uid; 234 } else { 235 if (errno) 236 eprintf("getpwnam %s:", user); 237 user_uid = (uid_t)estrtonum(user, 0, UINT_MAX); 238 } 239 have_user = 1; 240 } 241 242 eregcomp(&preg, argv[0], REG_EXTENDED); 243 244 self = getpid(); 245 if (proc_each(pgrepr, NULL) < 0) 246 eprintf("the process table cannot be read:"); 247 248 if (select_mode != PGREP_NONE && nhits > 0) { 249 pick = 0; 250 for (i = 1; i < nhits; i++) { 251 if (select_mode == PGREP_NEWEST ? hits[i].starttime > hits[pick].starttime 252 : hits[i].starttime < hits[pick].starttime) 253 pick = i; 254 } 255 if (parsestat(hits[pick].pid, &ps) == 0) 256 strlcpy(namebuf, ps.comm, sizeof(namebuf)); 257 else 258 namebuf[0] = '\0'; 259 show(hits[pick].pid, namebuf); 260 } 261 262 if (flags & PGREP_cflag) 263 printf("%d\n", count); 264 265 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 266 return 2; 267 return count > 0 ? 0 : 1; 268 }