pkill.c revision 1.24 1 1.24 christos /* $NetBSD: pkill.c,v 1.24 2009/02/28 18:16:11 christos Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.20 pavel * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad #include <sys/cdefs.h>
33 1.2 ad #ifndef lint
34 1.24 christos __RCSID("$NetBSD: pkill.c,v 1.24 2009/02/28 18:16:11 christos Exp $");
35 1.2 ad #endif /* !lint */
36 1.1 ad
37 1.1 ad #include <sys/types.h>
38 1.1 ad #include <sys/param.h>
39 1.1 ad #include <sys/sysctl.h>
40 1.1 ad #include <sys/proc.h>
41 1.1 ad #include <sys/queue.h>
42 1.1 ad #include <sys/stat.h>
43 1.1 ad
44 1.1 ad #include <stdio.h>
45 1.1 ad #include <stdlib.h>
46 1.1 ad #include <limits.h>
47 1.1 ad #include <string.h>
48 1.1 ad #include <unistd.h>
49 1.1 ad #include <signal.h>
50 1.1 ad #include <regex.h>
51 1.1 ad #include <ctype.h>
52 1.1 ad #include <kvm.h>
53 1.1 ad #include <err.h>
54 1.1 ad #include <pwd.h>
55 1.1 ad #include <grp.h>
56 1.1 ad #include <errno.h>
57 1.11 christos #include <paths.h>
58 1.1 ad
59 1.1 ad #define STATUS_MATCH 0
60 1.1 ad #define STATUS_NOMATCH 1
61 1.1 ad #define STATUS_BADUSAGE 2
62 1.1 ad #define STATUS_ERROR 3
63 1.1 ad
64 1.1 ad enum listtype {
65 1.1 ad LT_GENERIC,
66 1.1 ad LT_USER,
67 1.1 ad LT_GROUP,
68 1.1 ad LT_TTY,
69 1.1 ad LT_PGRP,
70 1.1 ad LT_SID
71 1.1 ad };
72 1.1 ad
73 1.1 ad struct list {
74 1.1 ad SLIST_ENTRY(list) li_chain;
75 1.1 ad long li_number;
76 1.1 ad };
77 1.1 ad
78 1.1 ad SLIST_HEAD(listhead, list);
79 1.1 ad
80 1.11 christos static struct kinfo_proc2 *plist;
81 1.11 christos static char *selected;
82 1.11 christos static const char *delim = "\n";
83 1.11 christos static int nproc;
84 1.11 christos static int pgrep;
85 1.11 christos static int signum = SIGTERM;
86 1.11 christos static int newest;
87 1.11 christos static int inverse;
88 1.11 christos static int longfmt;
89 1.11 christos static int matchargs;
90 1.11 christos static int fullmatch;
91 1.11 christos static int cflags = REG_EXTENDED;
92 1.11 christos static kvm_t *kd;
93 1.11 christos static pid_t mypid;
94 1.11 christos
95 1.11 christos static struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
96 1.11 christos static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
97 1.11 christos static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
98 1.11 christos static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
99 1.11 christos static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
100 1.11 christos static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
101 1.11 christos static struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
102 1.1 ad
103 1.1 ad int main(int, char **);
104 1.22 perry static void usage(void) __dead;
105 1.14 dsainty static int killact(const struct kinfo_proc2 *);
106 1.14 dsainty static int grepact(const struct kinfo_proc2 *);
107 1.11 christos static void makelist(struct listhead *, enum listtype, char *);
108 1.1 ad
109 1.1 ad int
110 1.1 ad main(int argc, char **argv)
111 1.1 ad {
112 1.14 dsainty char buf[_POSIX2_LINE_MAX], **pargv, *q;
113 1.1 ad int i, j, ch, bestidx, rv, criteria;
114 1.14 dsainty int (*action)(const struct kinfo_proc2 *);
115 1.14 dsainty const struct kinfo_proc2 *kp;
116 1.1 ad struct list *li;
117 1.14 dsainty const char *mstr, *p;
118 1.1 ad u_int32_t bestsec, bestusec;
119 1.1 ad regex_t reg;
120 1.1 ad regmatch_t regmatch;
121 1.1 ad
122 1.11 christos setprogname(argv[0]);
123 1.11 christos
124 1.1 ad if (strcmp(getprogname(), "pgrep") == 0) {
125 1.1 ad action = grepact;
126 1.1 ad pgrep = 1;
127 1.1 ad } else {
128 1.1 ad action = killact;
129 1.1 ad p = argv[1];
130 1.1 ad
131 1.3 augustss if (argc > 1 && p[0] == '-') {
132 1.1 ad p++;
133 1.3 augustss i = (int)strtol(p, &q, 10);
134 1.3 augustss if (*q == '\0') {
135 1.1 ad signum = i;
136 1.1 ad argv++;
137 1.3 augustss argc--;
138 1.1 ad } else {
139 1.1 ad if (strncasecmp(p, "sig", 3) == 0)
140 1.1 ad p += 3;
141 1.1 ad for (i = 1; i < NSIG; i++)
142 1.1 ad if (strcasecmp(sys_signame[i], p) == 0)
143 1.1 ad break;
144 1.1 ad if (i != NSIG) {
145 1.1 ad signum = i;
146 1.1 ad argv++;
147 1.3 augustss argc--;
148 1.1 ad }
149 1.1 ad }
150 1.1 ad }
151 1.1 ad }
152 1.1 ad
153 1.1 ad criteria = 0;
154 1.1 ad
155 1.9 sketch while ((ch = getopt(argc, argv, "G:P:U:d:fg:ilns:t:u:vx")) != -1)
156 1.1 ad switch (ch) {
157 1.1 ad case 'G':
158 1.1 ad makelist(&rgidlist, LT_GROUP, optarg);
159 1.1 ad criteria = 1;
160 1.1 ad break;
161 1.1 ad case 'P':
162 1.1 ad makelist(&ppidlist, LT_GENERIC, optarg);
163 1.1 ad criteria = 1;
164 1.1 ad break;
165 1.1 ad case 'U':
166 1.1 ad makelist(&ruidlist, LT_USER, optarg);
167 1.1 ad criteria = 1;
168 1.1 ad break;
169 1.1 ad case 'd':
170 1.1 ad if (!pgrep)
171 1.1 ad usage();
172 1.1 ad delim = optarg;
173 1.1 ad break;
174 1.1 ad case 'f':
175 1.1 ad matchargs = 1;
176 1.1 ad break;
177 1.1 ad case 'g':
178 1.1 ad makelist(&pgrplist, LT_PGRP, optarg);
179 1.1 ad criteria = 1;
180 1.1 ad break;
181 1.9 sketch case 'i':
182 1.9 sketch cflags |= REG_ICASE;
183 1.9 sketch break;
184 1.1 ad case 'l':
185 1.1 ad longfmt = 1;
186 1.1 ad break;
187 1.1 ad case 'n':
188 1.1 ad newest = 1;
189 1.1 ad criteria = 1;
190 1.1 ad break;
191 1.1 ad case 's':
192 1.1 ad makelist(&sidlist, LT_SID, optarg);
193 1.1 ad criteria = 1;
194 1.1 ad break;
195 1.1 ad case 't':
196 1.1 ad makelist(&tdevlist, LT_TTY, optarg);
197 1.1 ad criteria = 1;
198 1.1 ad break;
199 1.1 ad case 'u':
200 1.1 ad makelist(&euidlist, LT_USER, optarg);
201 1.1 ad criteria = 1;
202 1.1 ad break;
203 1.1 ad case 'v':
204 1.1 ad inverse = 1;
205 1.1 ad break;
206 1.1 ad case 'x':
207 1.1 ad fullmatch = 1;
208 1.1 ad break;
209 1.1 ad default:
210 1.1 ad usage();
211 1.1 ad /* NOTREACHED */
212 1.1 ad }
213 1.1 ad
214 1.1 ad argc -= optind;
215 1.1 ad argv += optind;
216 1.1 ad if (argc != 0)
217 1.1 ad criteria = 1;
218 1.1 ad if (!criteria)
219 1.1 ad usage();
220 1.1 ad
221 1.1 ad mypid = getpid();
222 1.1 ad
223 1.1 ad /*
224 1.1 ad * Retrieve the list of running processes from the kernel.
225 1.1 ad */
226 1.1 ad kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
227 1.1 ad if (kd == NULL)
228 1.11 christos errx(STATUS_ERROR, "Cannot open kernel files (%s)", buf);
229 1.1 ad
230 1.1 ad plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
231 1.1 ad if (plist == NULL)
232 1.11 christos errx(STATUS_ERROR, "Cannot get process list (%s)",
233 1.11 christos kvm_geterr(kd));
234 1.1 ad
235 1.1 ad /*
236 1.1 ad * Allocate memory which will be used to keep track of the
237 1.1 ad * selection.
238 1.1 ad */
239 1.12 christos if ((selected = calloc((size_t)1, (size_t)nproc)) == NULL)
240 1.11 christos err(STATUS_ERROR, "Cannot allocate memory for %d processes",
241 1.11 christos nproc);
242 1.1 ad
243 1.1 ad /*
244 1.1 ad * Refine the selection.
245 1.1 ad */
246 1.1 ad for (; *argv != NULL; argv++) {
247 1.9 sketch if ((rv = regcomp(®, *argv, cflags)) != 0) {
248 1.12 christos (void)regerror(rv, ®, buf, sizeof(buf));
249 1.11 christos errx(STATUS_BADUSAGE,
250 1.11 christos "Cannot compile regular expression `%s' (%s)",
251 1.11 christos *argv, buf);
252 1.1 ad }
253 1.1 ad
254 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
255 1.20 pavel if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
256 1.1 ad continue;
257 1.1 ad
258 1.1 ad if (matchargs) {
259 1.1 ad if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
260 1.1 ad continue;
261 1.1 ad
262 1.1 ad j = 0;
263 1.1 ad while (j < sizeof(buf) && *pargv != NULL) {
264 1.1 ad j += snprintf(buf + j, sizeof(buf) - j,
265 1.1 ad pargv[1] != NULL ? "%s " : "%s",
266 1.1 ad pargv[0]);
267 1.1 ad pargv++;
268 1.1 ad }
269 1.1 ad
270 1.1 ad mstr = buf;
271 1.1 ad } else
272 1.1 ad mstr = kp->p_comm;
273 1.1 ad
274 1.1 ad rv = regexec(®, mstr, 1, ®match, 0);
275 1.1 ad if (rv == 0) {
276 1.1 ad if (fullmatch) {
277 1.1 ad if (regmatch.rm_so == 0 &&
278 1.1 ad regmatch.rm_eo == strlen(mstr))
279 1.1 ad selected[i] = 1;
280 1.1 ad } else
281 1.1 ad selected[i] = 1;
282 1.1 ad } else if (rv != REG_NOMATCH) {
283 1.12 christos (void)regerror(rv, ®, buf, sizeof(buf));
284 1.11 christos errx(STATUS_ERROR,
285 1.11 christos "Regular expression evaluation error (%s)",
286 1.11 christos buf);
287 1.1 ad }
288 1.1 ad }
289 1.1 ad
290 1.1 ad regfree(®);
291 1.1 ad }
292 1.1 ad
293 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
294 1.20 pavel if ((kp->p_flag & P_SYSTEM) != 0)
295 1.1 ad continue;
296 1.1 ad
297 1.1 ad SLIST_FOREACH(li, &ruidlist, li_chain)
298 1.1 ad if (kp->p_ruid == (uid_t)li->li_number)
299 1.1 ad break;
300 1.1 ad if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
301 1.1 ad selected[i] = 0;
302 1.1 ad continue;
303 1.1 ad }
304 1.1 ad
305 1.1 ad SLIST_FOREACH(li, &rgidlist, li_chain)
306 1.1 ad if (kp->p_rgid == (gid_t)li->li_number)
307 1.1 ad break;
308 1.1 ad if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
309 1.1 ad selected[i] = 0;
310 1.1 ad continue;
311 1.1 ad }
312 1.1 ad
313 1.1 ad SLIST_FOREACH(li, &euidlist, li_chain)
314 1.1 ad if (kp->p_uid == (uid_t)li->li_number)
315 1.1 ad break;
316 1.1 ad if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
317 1.1 ad selected[i] = 0;
318 1.1 ad continue;
319 1.1 ad }
320 1.1 ad
321 1.1 ad SLIST_FOREACH(li, &ppidlist, li_chain)
322 1.1 ad if (kp->p_ppid == (uid_t)li->li_number)
323 1.1 ad break;
324 1.1 ad if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
325 1.1 ad selected[i] = 0;
326 1.1 ad continue;
327 1.1 ad }
328 1.1 ad
329 1.1 ad SLIST_FOREACH(li, &pgrplist, li_chain)
330 1.1 ad if (kp->p__pgid == (uid_t)li->li_number)
331 1.1 ad break;
332 1.1 ad if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
333 1.1 ad selected[i] = 0;
334 1.1 ad continue;
335 1.1 ad }
336 1.1 ad
337 1.1 ad SLIST_FOREACH(li, &tdevlist, li_chain) {
338 1.1 ad if (li->li_number == -1 &&
339 1.20 pavel (kp->p_flag & P_CONTROLT) == 0)
340 1.1 ad break;
341 1.1 ad if (kp->p_tdev == (uid_t)li->li_number)
342 1.1 ad break;
343 1.1 ad }
344 1.1 ad if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
345 1.1 ad selected[i] = 0;
346 1.1 ad continue;
347 1.1 ad }
348 1.1 ad
349 1.1 ad SLIST_FOREACH(li, &sidlist, li_chain)
350 1.1 ad if (kp->p_sid == (uid_t)li->li_number)
351 1.1 ad break;
352 1.1 ad if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
353 1.1 ad selected[i] = 0;
354 1.1 ad continue;
355 1.1 ad }
356 1.1 ad
357 1.1 ad if (argc == 0)
358 1.1 ad selected[i] = 1;
359 1.1 ad }
360 1.1 ad
361 1.1 ad if (newest) {
362 1.1 ad bestsec = 0;
363 1.1 ad bestusec = 0;
364 1.1 ad bestidx = -1;
365 1.1 ad
366 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
367 1.1 ad if (!selected[i])
368 1.1 ad continue;
369 1.1 ad
370 1.1 ad if (kp->p_ustart_sec > bestsec ||
371 1.1 ad (kp->p_ustart_sec == bestsec
372 1.1 ad && kp->p_ustart_usec > bestusec)) {
373 1.1 ad bestsec = kp->p_ustart_sec;
374 1.1 ad bestusec = kp->p_ustart_usec;
375 1.1 ad bestidx = i;
376 1.1 ad }
377 1.1 ad }
378 1.1 ad
379 1.12 christos (void)memset(selected, 0, (size_t)nproc);
380 1.1 ad if (bestidx != -1)
381 1.1 ad selected[bestidx] = 1;
382 1.1 ad }
383 1.1 ad
384 1.1 ad /*
385 1.1 ad * Take the appropriate action for each matched process, if any.
386 1.1 ad */
387 1.1 ad for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
388 1.1 ad if (kp->p_pid == mypid)
389 1.1 ad continue;
390 1.1 ad if (selected[i]) {
391 1.1 ad if (inverse)
392 1.1 ad continue;
393 1.1 ad } else if (!inverse)
394 1.4 ad continue;
395 1.4 ad
396 1.20 pavel if ((kp->p_flag & P_SYSTEM) != 0)
397 1.1 ad continue;
398 1.1 ad
399 1.10 christos rv |= (*action)(kp);
400 1.1 ad }
401 1.1 ad
402 1.12 christos return rv ? STATUS_MATCH : STATUS_NOMATCH;
403 1.1 ad }
404 1.1 ad
405 1.11 christos static void
406 1.1 ad usage(void)
407 1.1 ad {
408 1.1 ad const char *ustr;
409 1.1 ad
410 1.1 ad if (pgrep)
411 1.9 sketch ustr = "[-filnvx] [-d delim]";
412 1.1 ad else
413 1.21 erh ustr = "[-signal] [-filnvx]";
414 1.1 ad
415 1.11 christos (void)fprintf(stderr,
416 1.17 wiz "Usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] [-t tty]\n"
417 1.17 wiz " [-U uid] [-u euid] pattern ...\n", getprogname(),
418 1.7 soren ustr);
419 1.1 ad
420 1.16 kleink exit(STATUS_BADUSAGE);
421 1.1 ad }
422 1.1 ad
423 1.11 christos static int
424 1.14 dsainty killact(const struct kinfo_proc2 *kp)
425 1.1 ad {
426 1.21 erh if (longfmt)
427 1.21 erh grepact(kp);
428 1.10 christos if (kill(kp->p_pid, signum) == -1) {
429 1.13 dsainty
430 1.13 dsainty /*
431 1.13 dsainty * Check for ESRCH, which indicates that the process
432 1.13 dsainty * disappeared between us matching it and us
433 1.15 kleink * signalling it; don't issue a warning about it.
434 1.13 dsainty */
435 1.15 kleink if (errno != ESRCH)
436 1.15 kleink warn("signalling pid %d", (int)kp->p_pid);
437 1.1 ad
438 1.15 kleink /*
439 1.15 kleink * Return 0 to indicate that the process should not be
440 1.15 kleink * considered a match, since we didn't actually get to
441 1.15 kleink * signal it.
442 1.15 kleink */
443 1.15 kleink return 0;
444 1.10 christos }
445 1.10 christos
446 1.10 christos return 1;
447 1.1 ad }
448 1.1 ad
449 1.11 christos static int
450 1.14 dsainty grepact(const struct kinfo_proc2 *kp)
451 1.1 ad {
452 1.1 ad char **argv;
453 1.1 ad
454 1.1 ad if (longfmt && matchargs) {
455 1.13 dsainty
456 1.13 dsainty /*
457 1.13 dsainty * If kvm_getargv2() failed the process has probably
458 1.13 dsainty * disappeared. Return 0 to indicate that the process
459 1.13 dsainty * should not be considered a match, since we are no
460 1.13 dsainty * longer in a position to output it as a match.
461 1.13 dsainty */
462 1.1 ad if ((argv = kvm_getargv2(kd, kp, 0)) == NULL)
463 1.10 christos return 0;
464 1.1 ad
465 1.11 christos (void)printf("%d ", (int)kp->p_pid);
466 1.1 ad for (; *argv != NULL; argv++) {
467 1.11 christos (void)printf("%s", *argv);
468 1.1 ad if (argv[1] != NULL)
469 1.11 christos (void)putchar(' ');
470 1.1 ad }
471 1.1 ad } else if (longfmt)
472 1.11 christos (void)printf("%d %s", (int)kp->p_pid, kp->p_comm);
473 1.1 ad else
474 1.11 christos (void)printf("%d", (int)kp->p_pid);
475 1.1 ad
476 1.11 christos (void)printf("%s", delim);
477 1.10 christos
478 1.10 christos return 1;
479 1.1 ad }
480 1.1 ad
481 1.11 christos static void
482 1.1 ad makelist(struct listhead *head, enum listtype type, char *src)
483 1.1 ad {
484 1.1 ad struct list *li;
485 1.1 ad struct passwd *pw;
486 1.1 ad struct group *gr;
487 1.1 ad struct stat st;
488 1.11 christos char *sp, *ep, buf[MAXPATHLEN];
489 1.11 christos const char *p;
490 1.1 ad int empty;
491 1.11 christos const char *prefix = _PATH_DEV;
492 1.1 ad
493 1.1 ad empty = 1;
494 1.1 ad
495 1.1 ad while ((sp = strsep(&src, ",")) != NULL) {
496 1.1 ad if (*sp == '\0')
497 1.1 ad usage();
498 1.1 ad
499 1.1 ad if ((li = malloc(sizeof(*li))) == NULL)
500 1.11 christos err(STATUS_ERROR, "Cannot allocate %zd bytes",
501 1.11 christos sizeof(*li));
502 1.1 ad SLIST_INSERT_HEAD(head, li, li_chain);
503 1.1 ad empty = 0;
504 1.1 ad
505 1.11 christos li->li_number = (uid_t)strtol(sp, &ep, 0);
506 1.24 christos if (*ep == '\0' && type != LT_TTY) {
507 1.1 ad switch (type) {
508 1.1 ad case LT_PGRP:
509 1.1 ad if (li->li_number == 0)
510 1.1 ad li->li_number = getpgrp();
511 1.1 ad break;
512 1.1 ad case LT_SID:
513 1.1 ad if (li->li_number == 0)
514 1.1 ad li->li_number = getsid(mypid);
515 1.1 ad break;
516 1.1 ad default:
517 1.1 ad break;
518 1.1 ad }
519 1.1 ad continue;
520 1.1 ad }
521 1.1 ad
522 1.1 ad switch (type) {
523 1.1 ad case LT_USER:
524 1.1 ad if ((pw = getpwnam(sp)) == NULL)
525 1.11 christos errx(STATUS_BADUSAGE, "Unknown user `%s'",
526 1.8 abs sp);
527 1.1 ad li->li_number = pw->pw_uid;
528 1.1 ad break;
529 1.1 ad case LT_GROUP:
530 1.1 ad if ((gr = getgrnam(sp)) == NULL)
531 1.11 christos errx(STATUS_BADUSAGE, "Unknown group `%s'",
532 1.8 abs sp);
533 1.1 ad li->li_number = gr->gr_gid;
534 1.1 ad break;
535 1.1 ad case LT_TTY:
536 1.24 christos p = sp;
537 1.24 christos if (*sp == '/')
538 1.24 christos prefix = "";
539 1.24 christos else if (strcmp(sp, "-") == 0) {
540 1.1 ad li->li_number = -1;
541 1.1 ad break;
542 1.1 ad } else if (strcmp(sp, "co") == 0)
543 1.1 ad p = "console";
544 1.1 ad else if (strncmp(sp, "tty", 3) == 0)
545 1.24 christos /* all set */;
546 1.24 christos else if (strncmp(sp, "pts/", 4) == 0)
547 1.24 christos /* all set */;
548 1.24 christos else if (*ep != '\0' || (strlen(sp) == 2 && *sp == '0'))
549 1.11 christos prefix = _PATH_TTY;
550 1.24 christos else
551 1.24 christos prefix = _PATH_DEV_PTS;
552 1.1 ad
553 1.11 christos (void)snprintf(buf, sizeof(buf), "%s%s", prefix, p);
554 1.1 ad
555 1.11 christos if (stat(buf, &st) == -1) {
556 1.1 ad if (errno == ENOENT)
557 1.1 ad errx(STATUS_BADUSAGE,
558 1.24 christos "No such tty: `%s'", buf);
559 1.24 christos err(STATUS_ERROR, "Cannot access `%s'", buf);
560 1.1 ad }
561 1.1 ad
562 1.1 ad if ((st.st_mode & S_IFCHR) == 0)
563 1.24 christos errx(STATUS_BADUSAGE, "Not a tty: `%s'", buf);
564 1.1 ad
565 1.1 ad li->li_number = st.st_rdev;
566 1.1 ad break;
567 1.1 ad default:
568 1.1 ad usage();
569 1.11 christos }
570 1.1 ad }
571 1.1 ad
572 1.1 ad if (empty)
573 1.1 ad usage();
574 1.1 ad }
575