pkill.c revision 1.1 1 1.1 ad /* $NetBSD: pkill.c,v 1.1 2002/03/01 11:21:58 ad Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 ad * must display the following acknowledgement:
20 1.1 ad * This product includes software developed by the NetBSD
21 1.1 ad * Foundation, Inc. and its contributors.
22 1.1 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 ad * contributors may be used to endorse or promote products derived
24 1.1 ad * from this software without specific prior written permission.
25 1.1 ad *
26 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.1 ad */
38 1.1 ad
39 1.1 ad #include <sys/cdefs.h>
40 1.1 ad #ifndef LINT
41 1.1 ad __RCSID("$NetBSD: pkill.c,v 1.1 2002/03/01 11:21:58 ad Exp $");
42 1.1 ad #endif /* !LINT */
43 1.1 ad
44 1.1 ad #include <sys/types.h>
45 1.1 ad #include <sys/param.h>
46 1.1 ad #include <sys/sysctl.h>
47 1.1 ad #include <sys/proc.h>
48 1.1 ad #include <sys/queue.h>
49 1.1 ad #include <sys/stat.h>
50 1.1 ad
51 1.1 ad #include <stdio.h>
52 1.1 ad #include <stdlib.h>
53 1.1 ad #include <limits.h>
54 1.1 ad #include <string.h>
55 1.1 ad #include <unistd.h>
56 1.1 ad #include <signal.h>
57 1.1 ad #include <regex.h>
58 1.1 ad #include <ctype.h>
59 1.1 ad #include <kvm.h>
60 1.1 ad #include <err.h>
61 1.1 ad #include <pwd.h>
62 1.1 ad #include <grp.h>
63 1.1 ad #include <errno.h>
64 1.1 ad
65 1.1 ad #define STATUS_MATCH 0
66 1.1 ad #define STATUS_NOMATCH 1
67 1.1 ad #define STATUS_BADUSAGE 2
68 1.1 ad #define STATUS_ERROR 3
69 1.1 ad
70 1.1 ad enum listtype {
71 1.1 ad LT_GENERIC,
72 1.1 ad LT_USER,
73 1.1 ad LT_GROUP,
74 1.1 ad LT_TTY,
75 1.1 ad LT_PGRP,
76 1.1 ad LT_SID
77 1.1 ad };
78 1.1 ad
79 1.1 ad struct list {
80 1.1 ad SLIST_ENTRY(list) li_chain;
81 1.1 ad long li_number;
82 1.1 ad };
83 1.1 ad
84 1.1 ad SLIST_HEAD(listhead, list);
85 1.1 ad
86 1.1 ad struct kinfo_proc2 *plist;
87 1.1 ad char *selected;
88 1.1 ad char *delim = "\n";
89 1.1 ad int nproc;
90 1.1 ad int pgrep;
91 1.1 ad int signum = SIGTERM;
92 1.1 ad int newest;
93 1.1 ad int inverse;
94 1.1 ad int longfmt;
95 1.1 ad int matchargs;
96 1.1 ad int fullmatch;
97 1.1 ad kvm_t *kd;
98 1.1 ad pid_t mypid;
99 1.1 ad
100 1.1 ad struct listhead euidlist = SLIST_HEAD_INITIALIZER(list);
101 1.1 ad struct listhead ruidlist = SLIST_HEAD_INITIALIZER(list);
102 1.1 ad struct listhead rgidlist = SLIST_HEAD_INITIALIZER(list);
103 1.1 ad struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list);
104 1.1 ad struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list);
105 1.1 ad struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list);
106 1.1 ad struct listhead sidlist = SLIST_HEAD_INITIALIZER(list);
107 1.1 ad
108 1.1 ad int main(int, char **);
109 1.1 ad void usage(void);
110 1.1 ad void killact(struct kinfo_proc2 *);
111 1.1 ad void grepact(struct kinfo_proc2 *);
112 1.1 ad void makelist(struct listhead *, enum listtype, char *);
113 1.1 ad
114 1.1 ad int
115 1.1 ad main(int argc, char **argv)
116 1.1 ad {
117 1.1 ad extern char *optarg;
118 1.1 ad extern int optind;
119 1.1 ad char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p;
120 1.1 ad int i, j, ch, bestidx, rv, criteria;
121 1.1 ad void (*action)(struct kinfo_proc2 *);
122 1.1 ad struct kinfo_proc2 *kp;
123 1.1 ad struct list *li;
124 1.1 ad u_int32_t bestsec, bestusec;
125 1.1 ad regex_t reg;
126 1.1 ad regmatch_t regmatch;
127 1.1 ad
128 1.1 ad if (strcmp(getprogname(), "pgrep") == 0) {
129 1.1 ad action = grepact;
130 1.1 ad pgrep = 1;
131 1.1 ad } else {
132 1.1 ad action = killact;
133 1.1 ad p = argv[1];
134 1.1 ad
135 1.1 ad if (p[0] == '-') {
136 1.1 ad p++;
137 1.1 ad i = (int)strtol(p, &p, 10);
138 1.1 ad if (*p == '\0') {
139 1.1 ad signum = i;
140 1.1 ad argv++;
141 1.1 ad } else {
142 1.1 ad if (strncasecmp(p, "sig", 3) == 0)
143 1.1 ad p += 3;
144 1.1 ad for (i = 1; i < NSIG; i++)
145 1.1 ad if (strcasecmp(sys_signame[i], p) == 0)
146 1.1 ad break;
147 1.1 ad if (i != NSIG) {
148 1.1 ad signum = i;
149 1.1 ad argv++;
150 1.1 ad }
151 1.1 ad }
152 1.1 ad }
153 1.1 ad }
154 1.1 ad
155 1.1 ad criteria = 0;
156 1.1 ad
157 1.1 ad while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:vx")) != -1)
158 1.1 ad switch (ch) {
159 1.1 ad case 'G':
160 1.1 ad makelist(&rgidlist, LT_GROUP, optarg);
161 1.1 ad criteria = 1;
162 1.1 ad break;
163 1.1 ad case 'P':
164 1.1 ad makelist(&ppidlist, LT_GENERIC, optarg);
165 1.1 ad criteria = 1;
166 1.1 ad break;
167 1.1 ad case 'U':
168 1.1 ad makelist(&ruidlist, LT_USER, optarg);
169 1.1 ad criteria = 1;
170 1.1 ad break;
171 1.1 ad case 'd':
172 1.1 ad if (!pgrep)
173 1.1 ad usage();
174 1.1 ad delim = optarg;
175 1.1 ad break;
176 1.1 ad case 'f':
177 1.1 ad matchargs = 1;
178 1.1 ad break;
179 1.1 ad case 'g':
180 1.1 ad makelist(&pgrplist, LT_PGRP, optarg);
181 1.1 ad criteria = 1;
182 1.1 ad break;
183 1.1 ad case 'l':
184 1.1 ad if (!pgrep)
185 1.1 ad usage();
186 1.1 ad longfmt = 1;
187 1.1 ad break;
188 1.1 ad case 'n':
189 1.1 ad newest = 1;
190 1.1 ad criteria = 1;
191 1.1 ad break;
192 1.1 ad case 's':
193 1.1 ad makelist(&sidlist, LT_SID, optarg);
194 1.1 ad criteria = 1;
195 1.1 ad break;
196 1.1 ad case 't':
197 1.1 ad makelist(&tdevlist, LT_TTY, optarg);
198 1.1 ad criteria = 1;
199 1.1 ad break;
200 1.1 ad case 'u':
201 1.1 ad makelist(&euidlist, LT_USER, optarg);
202 1.1 ad criteria = 1;
203 1.1 ad break;
204 1.1 ad case 'v':
205 1.1 ad inverse = 1;
206 1.1 ad break;
207 1.1 ad case 'x':
208 1.1 ad fullmatch = 1;
209 1.1 ad break;
210 1.1 ad default:
211 1.1 ad usage();
212 1.1 ad /* NOTREACHED */
213 1.1 ad }
214 1.1 ad
215 1.1 ad argc -= optind;
216 1.1 ad argv += optind;
217 1.1 ad if (argc != 0)
218 1.1 ad criteria = 1;
219 1.1 ad if (!criteria)
220 1.1 ad usage();
221 1.1 ad
222 1.1 ad mypid = getpid();
223 1.1 ad
224 1.1 ad /*
225 1.1 ad * Retrieve the list of running processes from the kernel.
226 1.1 ad */
227 1.1 ad kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf);
228 1.1 ad if (kd == NULL)
229 1.1 ad errx(STATUS_ERROR, "kvm_openfiles(): %s", buf);
230 1.1 ad
231 1.1 ad plist = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(*plist), &nproc);
232 1.1 ad if (plist == NULL)
233 1.1 ad errx(STATUS_ERROR, "kvm_getprocs2() failed");
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.1 ad if ((selected = malloc(nproc)) == NULL)
240 1.1 ad errx(STATUS_ERROR, "memory allocation failure");
241 1.1 ad memset(selected, 0, 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.1 ad if ((rv = regcomp(®, *argv, REG_EXTENDED)) != 0) {
248 1.1 ad regerror(rv, ®, buf, sizeof(buf));
249 1.1 ad errx(STATUS_BADUSAGE, "bad expression: %s", buf);
250 1.1 ad }
251 1.1 ad
252 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
253 1.1 ad if ((kp->p_flag & P_SYSTEM) != 0)
254 1.1 ad continue;
255 1.1 ad
256 1.1 ad if (matchargs) {
257 1.1 ad if ((pargv = kvm_getargv2(kd, kp, 0)) == NULL)
258 1.1 ad continue;
259 1.1 ad
260 1.1 ad j = 0;
261 1.1 ad while (j < sizeof(buf) && *pargv != NULL) {
262 1.1 ad j += snprintf(buf + j, sizeof(buf) - j,
263 1.1 ad pargv[1] != NULL ? "%s " : "%s",
264 1.1 ad pargv[0]);
265 1.1 ad pargv++;
266 1.1 ad }
267 1.1 ad
268 1.1 ad mstr = buf;
269 1.1 ad } else
270 1.1 ad mstr = kp->p_comm;
271 1.1 ad
272 1.1 ad rv = regexec(®, mstr, 1, ®match, 0);
273 1.1 ad if (rv == 0) {
274 1.1 ad if (fullmatch) {
275 1.1 ad if (regmatch.rm_so == 0 &&
276 1.1 ad regmatch.rm_eo == strlen(mstr))
277 1.1 ad selected[i] = 1;
278 1.1 ad } else
279 1.1 ad selected[i] = 1;
280 1.1 ad } else if (rv != REG_NOMATCH) {
281 1.1 ad regerror(rv, ®, buf, sizeof(buf));
282 1.1 ad errx(STATUS_ERROR, "regexec(): %s", buf);
283 1.1 ad }
284 1.1 ad }
285 1.1 ad
286 1.1 ad regfree(®);
287 1.1 ad }
288 1.1 ad
289 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
290 1.1 ad if ((kp->p_flag & P_SYSTEM) != 0)
291 1.1 ad continue;
292 1.1 ad
293 1.1 ad SLIST_FOREACH(li, &ruidlist, li_chain)
294 1.1 ad if (kp->p_ruid == (uid_t)li->li_number)
295 1.1 ad break;
296 1.1 ad if (SLIST_FIRST(&ruidlist) != NULL && li == NULL) {
297 1.1 ad selected[i] = 0;
298 1.1 ad continue;
299 1.1 ad }
300 1.1 ad
301 1.1 ad SLIST_FOREACH(li, &rgidlist, li_chain)
302 1.1 ad if (kp->p_rgid == (gid_t)li->li_number)
303 1.1 ad break;
304 1.1 ad if (SLIST_FIRST(&rgidlist) != NULL && li == NULL) {
305 1.1 ad selected[i] = 0;
306 1.1 ad continue;
307 1.1 ad }
308 1.1 ad
309 1.1 ad SLIST_FOREACH(li, &euidlist, li_chain)
310 1.1 ad if (kp->p_uid == (uid_t)li->li_number)
311 1.1 ad break;
312 1.1 ad if (SLIST_FIRST(&euidlist) != NULL && li == NULL) {
313 1.1 ad selected[i] = 0;
314 1.1 ad continue;
315 1.1 ad }
316 1.1 ad
317 1.1 ad SLIST_FOREACH(li, &ppidlist, li_chain)
318 1.1 ad if (kp->p_ppid == (uid_t)li->li_number)
319 1.1 ad break;
320 1.1 ad if (SLIST_FIRST(&ppidlist) != NULL && li == NULL) {
321 1.1 ad selected[i] = 0;
322 1.1 ad continue;
323 1.1 ad }
324 1.1 ad
325 1.1 ad SLIST_FOREACH(li, &pgrplist, li_chain)
326 1.1 ad if (kp->p__pgid == (uid_t)li->li_number)
327 1.1 ad break;
328 1.1 ad if (SLIST_FIRST(&pgrplist) != NULL && li == NULL) {
329 1.1 ad selected[i] = 0;
330 1.1 ad continue;
331 1.1 ad }
332 1.1 ad
333 1.1 ad SLIST_FOREACH(li, &tdevlist, li_chain) {
334 1.1 ad if (li->li_number == -1 &&
335 1.1 ad (kp->p_flag & P_CONTROLT) == 0)
336 1.1 ad break;
337 1.1 ad if (kp->p_tdev == (uid_t)li->li_number)
338 1.1 ad break;
339 1.1 ad }
340 1.1 ad if (SLIST_FIRST(&tdevlist) != NULL && li == NULL) {
341 1.1 ad selected[i] = 0;
342 1.1 ad continue;
343 1.1 ad }
344 1.1 ad
345 1.1 ad SLIST_FOREACH(li, &sidlist, li_chain)
346 1.1 ad if (kp->p_sid == (uid_t)li->li_number)
347 1.1 ad break;
348 1.1 ad if (SLIST_FIRST(&sidlist) != NULL && li == NULL) {
349 1.1 ad selected[i] = 0;
350 1.1 ad continue;
351 1.1 ad }
352 1.1 ad
353 1.1 ad if (argc == 0)
354 1.1 ad selected[i] = 1;
355 1.1 ad }
356 1.1 ad
357 1.1 ad if (newest) {
358 1.1 ad bestsec = 0;
359 1.1 ad bestusec = 0;
360 1.1 ad bestidx = -1;
361 1.1 ad
362 1.1 ad for (i = 0, kp = plist; i < nproc; i++, kp++) {
363 1.1 ad if (!selected[i])
364 1.1 ad continue;
365 1.1 ad
366 1.1 ad if (kp->p_ustart_sec > bestsec ||
367 1.1 ad (kp->p_ustart_sec == bestsec
368 1.1 ad && kp->p_ustart_usec > bestusec)) {
369 1.1 ad bestsec = kp->p_ustart_sec;
370 1.1 ad bestusec = kp->p_ustart_usec;
371 1.1 ad bestidx = i;
372 1.1 ad }
373 1.1 ad }
374 1.1 ad
375 1.1 ad memset(selected, 0, nproc);
376 1.1 ad if (bestidx != -1)
377 1.1 ad selected[bestidx] = 1;
378 1.1 ad }
379 1.1 ad
380 1.1 ad /*
381 1.1 ad * Take the appropriate action for each matched process, if any.
382 1.1 ad */
383 1.1 ad for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) {
384 1.1 ad if (kp->p_pid == mypid)
385 1.1 ad continue;
386 1.1 ad if (selected[i]) {
387 1.1 ad if (inverse)
388 1.1 ad continue;
389 1.1 ad } else if (!inverse)
390 1.1 ad continue;
391 1.1 ad
392 1.1 ad rv = 1;
393 1.1 ad (*action)(kp);
394 1.1 ad }
395 1.1 ad
396 1.1 ad exit(rv ? STATUS_MATCH : STATUS_NOMATCH);
397 1.1 ad }
398 1.1 ad
399 1.1 ad void
400 1.1 ad usage(void)
401 1.1 ad {
402 1.1 ad const char *ustr;
403 1.1 ad
404 1.1 ad if (pgrep)
405 1.1 ad ustr = "[-flnvx] [-d delim]";
406 1.1 ad else
407 1.1 ad ustr = "[-signal] [-fnvx]";
408 1.1 ad
409 1.1 ad fprintf(stderr, "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] "
410 1.1 ad "[-s sid] [-t tty] [-u euid] pattern ...\n", getprogname(), ustr);
411 1.1 ad
412 1.1 ad exit(STATUS_ERROR);
413 1.1 ad }
414 1.1 ad
415 1.1 ad void
416 1.1 ad killact(struct kinfo_proc2 *kp)
417 1.1 ad {
418 1.1 ad
419 1.1 ad if (kill(kp->p_pid, signum) == -1)
420 1.1 ad err(STATUS_ERROR, "signalling pid %d", (int)kp->p_pid);
421 1.1 ad }
422 1.1 ad
423 1.1 ad void
424 1.1 ad grepact(struct kinfo_proc2 *kp)
425 1.1 ad {
426 1.1 ad char **argv;
427 1.1 ad
428 1.1 ad if (longfmt && matchargs) {
429 1.1 ad if ((argv = kvm_getargv2(kd, kp, 0)) == NULL)
430 1.1 ad return;
431 1.1 ad
432 1.1 ad printf("%d ", (int)kp->p_pid);
433 1.1 ad for (; *argv != NULL; argv++) {
434 1.1 ad printf("%s", *argv);
435 1.1 ad if (argv[1] != NULL)
436 1.1 ad putchar(' ');
437 1.1 ad }
438 1.1 ad } else if (longfmt)
439 1.1 ad printf("%d %s", (int)kp->p_pid, kp->p_comm);
440 1.1 ad else
441 1.1 ad printf("%d", (int)kp->p_pid);
442 1.1 ad
443 1.1 ad printf("%s", delim);
444 1.1 ad }
445 1.1 ad
446 1.1 ad void
447 1.1 ad makelist(struct listhead *head, enum listtype type, char *src)
448 1.1 ad {
449 1.1 ad struct list *li;
450 1.1 ad struct passwd *pw;
451 1.1 ad struct group *gr;
452 1.1 ad struct stat st;
453 1.1 ad char *sp, *p, buf[MAXPATHLEN];
454 1.1 ad int empty;
455 1.1 ad
456 1.1 ad empty = 1;
457 1.1 ad
458 1.1 ad while ((sp = strsep(&src, ",")) != NULL) {
459 1.1 ad if (*sp == '\0')
460 1.1 ad usage();
461 1.1 ad
462 1.1 ad if ((li = malloc(sizeof(*li))) == NULL)
463 1.1 ad errx(STATUS_ERROR, "memory allocation failure");
464 1.1 ad SLIST_INSERT_HEAD(head, li, li_chain);
465 1.1 ad empty = 0;
466 1.1 ad
467 1.1 ad li->li_number = (uid_t)strtol(sp, &p, 0);
468 1.1 ad if (*p == '\0') {
469 1.1 ad switch (type) {
470 1.1 ad case LT_PGRP:
471 1.1 ad if (li->li_number == 0)
472 1.1 ad li->li_number = getpgrp();
473 1.1 ad break;
474 1.1 ad case LT_SID:
475 1.1 ad if (li->li_number == 0)
476 1.1 ad li->li_number = getsid(mypid);
477 1.1 ad break;
478 1.1 ad case LT_TTY:
479 1.1 ad usage();
480 1.1 ad default:
481 1.1 ad break;
482 1.1 ad }
483 1.1 ad continue;
484 1.1 ad }
485 1.1 ad
486 1.1 ad switch (type) {
487 1.1 ad case LT_USER:
488 1.1 ad if ((pw = getpwnam(sp)) == NULL)
489 1.1 ad errx(STATUS_BADUSAGE, "unknown user `%s'",
490 1.1 ad optarg);
491 1.1 ad li->li_number = pw->pw_uid;
492 1.1 ad break;
493 1.1 ad case LT_GROUP:
494 1.1 ad if ((gr = getgrnam(sp)) == NULL)
495 1.1 ad errx(STATUS_BADUSAGE, "unknown group `%s'",
496 1.1 ad optarg);
497 1.1 ad li->li_number = gr->gr_gid;
498 1.1 ad break;
499 1.1 ad case LT_TTY:
500 1.1 ad if (strcmp(sp, "-") == 0) {
501 1.1 ad li->li_number = -1;
502 1.1 ad break;
503 1.1 ad } else if (strcmp(sp, "co") == 0)
504 1.1 ad p = "console";
505 1.1 ad else if (strncmp(sp, "tty", 3) == 0)
506 1.1 ad p = sp;
507 1.1 ad else
508 1.1 ad p = NULL;
509 1.1 ad
510 1.1 ad if (p == NULL)
511 1.1 ad snprintf(buf, sizeof(buf), "/dev/tty%s", sp);
512 1.1 ad else
513 1.1 ad snprintf(buf, sizeof(buf), "/dev/%s", p);
514 1.1 ad
515 1.1 ad if (stat(buf, &st) < 0) {
516 1.1 ad if (errno == ENOENT)
517 1.1 ad errx(STATUS_BADUSAGE,
518 1.1 ad "no such tty: `%s'", sp);
519 1.1 ad err(STATUS_ERROR, "stat(%s)", sp);
520 1.1 ad }
521 1.1 ad
522 1.1 ad if ((st.st_mode & S_IFCHR) == 0)
523 1.1 ad errx(STATUS_BADUSAGE, "not a tty: `%s'", sp);
524 1.1 ad
525 1.1 ad li->li_number = st.st_rdev;
526 1.1 ad break;
527 1.1 ad default:
528 1.1 ad usage();
529 1.1 ad };
530 1.1 ad }
531 1.1 ad
532 1.1 ad if (empty)
533 1.1 ad usage();
534 1.1 ad }
535