pkill.c (12dacf622b5b8b31e2bcca0bebc9d3dba4755a83) pkill.c (cc55ad3d724e44a9ca80db40c7a9b3a08a1ce008)
1/* $NetBSD: pkill.c,v 1.16 2005/10/10 22:13:20 kleink Exp $ */
2
3/*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation

--- 65 unchanged lines hidden (view full) ---

74
75enum listtype {
76 LT_GENERIC,
77 LT_USER,
78 LT_GROUP,
79 LT_TTY,
80 LT_PGRP,
81 LT_JID,
1/* $NetBSD: pkill.c,v 1.16 2005/10/10 22:13:20 kleink Exp $ */
2
3/*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation

--- 65 unchanged lines hidden (view full) ---

74
75enum listtype {
76 LT_GENERIC,
77 LT_USER,
78 LT_GROUP,
79 LT_TTY,
80 LT_PGRP,
81 LT_JID,
82 LT_SID
82 LT_SID,
83 LT_CLASS
83};
84
85struct list {
86 SLIST_ENTRY(list) li_chain;
87 long li_number;
84};
85
86struct list {
87 SLIST_ENTRY(list) li_chain;
88 long li_number;
89 char *li_name;
88};
89
90SLIST_HEAD(listhead, list);
91
92static struct kinfo_proc *plist;
93static char *selected;
94static const char *delim = "\n";
95static int nproc;

--- 15 unchanged lines hidden (view full) ---

111static struct listhead euidlist = SLIST_HEAD_INITIALIZER(euidlist);
112static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(ruidlist);
113static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(rgidlist);
114static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(pgrplist);
115static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(ppidlist);
116static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(tdevlist);
117static struct listhead sidlist = SLIST_HEAD_INITIALIZER(sidlist);
118static struct listhead jidlist = SLIST_HEAD_INITIALIZER(jidlist);
90};
91
92SLIST_HEAD(listhead, list);
93
94static struct kinfo_proc *plist;
95static char *selected;
96static const char *delim = "\n";
97static int nproc;

--- 15 unchanged lines hidden (view full) ---

113static struct listhead euidlist = SLIST_HEAD_INITIALIZER(euidlist);
114static struct listhead ruidlist = SLIST_HEAD_INITIALIZER(ruidlist);
115static struct listhead rgidlist = SLIST_HEAD_INITIALIZER(rgidlist);
116static struct listhead pgrplist = SLIST_HEAD_INITIALIZER(pgrplist);
117static struct listhead ppidlist = SLIST_HEAD_INITIALIZER(ppidlist);
118static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(tdevlist);
119static struct listhead sidlist = SLIST_HEAD_INITIALIZER(sidlist);
120static struct listhead jidlist = SLIST_HEAD_INITIALIZER(jidlist);
121static struct listhead classlist = SLIST_HEAD_INITIALIZER(classlist);
119
120static void usage(void) __attribute__((__noreturn__));
121static int killact(const struct kinfo_proc *);
122static int grepact(const struct kinfo_proc *);
123static void makelist(struct listhead *, enum listtype, char *);
124static int takepid(const char *, int);
125
126int

--- 47 unchanged lines hidden (view full) ---

174 criteria = 0;
175 debug_opt = 0;
176 pidfile = NULL;
177 pidfilelock = 0;
178 quiet = 0;
179 execf = NULL;
180 coref = _PATH_DEVNULL;
181
122
123static void usage(void) __attribute__((__noreturn__));
124static int killact(const struct kinfo_proc *);
125static int grepact(const struct kinfo_proc *);
126static void makelist(struct listhead *, enum listtype, char *);
127static int takepid(const char *, int);
128
129int

--- 47 unchanged lines hidden (view full) ---

177 criteria = 0;
178 debug_opt = 0;
179 pidfile = NULL;
180 pidfilelock = 0;
181 quiet = 0;
182 execf = NULL;
183 coref = _PATH_DEVNULL;
184
182 while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ad:fg:ij:lnoqs:t:u:vx")) != -1)
185 while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ac:d:fg:ij:lnoqs:t:u:vx")) != -1)
183 switch (ch) {
184 case 'D':
185 debug_opt++;
186 break;
187 case 'F':
188 pidfile = optarg;
189 criteria = 1;
190 break;

--- 26 unchanged lines hidden (view full) ---

217 break;
218 case 'U':
219 makelist(&ruidlist, LT_USER, optarg);
220 criteria = 1;
221 break;
222 case 'a':
223 ancestors++;
224 break;
186 switch (ch) {
187 case 'D':
188 debug_opt++;
189 break;
190 case 'F':
191 pidfile = optarg;
192 criteria = 1;
193 break;

--- 26 unchanged lines hidden (view full) ---

220 break;
221 case 'U':
222 makelist(&ruidlist, LT_USER, optarg);
223 criteria = 1;
224 break;
225 case 'a':
226 ancestors++;
227 break;
228 case 'c':
229 makelist(&classlist, LT_CLASS, optarg);
230 criteria = 1;
231 break;
225 case 'd':
226 if (!pgrep)
227 usage();
228 delim = optarg;
229 break;
230 case 'f':
231 matchargs = 1;
232 break;

--- 231 unchanged lines hidden (view full) ---

464 if (kp->ki_jid > 0 && li->li_number == -1)
465 break;
466 }
467 if (SLIST_FIRST(&jidlist) != NULL && li == NULL) {
468 selected[i] = 0;
469 continue;
470 }
471
232 case 'd':
233 if (!pgrep)
234 usage();
235 delim = optarg;
236 break;
237 case 'f':
238 matchargs = 1;
239 break;

--- 231 unchanged lines hidden (view full) ---

471 if (kp->ki_jid > 0 && li->li_number == -1)
472 break;
473 }
474 if (SLIST_FIRST(&jidlist) != NULL && li == NULL) {
475 selected[i] = 0;
476 continue;
477 }
478
479 SLIST_FOREACH(li, &classlist, li_chain) {
480 /*
481 * We skip P_SYSTEM processes to match ps(1) output.
482 */
483 if ((kp->ki_flag & P_SYSTEM) == 0 &&
484 kp->ki_loginclass != NULL &&
485 strcmp(kp->ki_loginclass, li->li_name) == 0)
486 break;
487 }
488 if (SLIST_FIRST(&classlist) != NULL && li == NULL) {
489 selected[i] = 0;
490 continue;
491 }
492
472 if (argc == 0)
473 selected[i] = 1;
474 }
475
476 if (!ancestors) {
477 pid = mypid;
478 while (pid) {
479 for (i = 0, kp = plist; i < nproc; i++, kp++) {

--- 77 unchanged lines hidden (view full) ---

557
558 if (pgrep)
559 ustr = "[-LSfilnoqvx] [-d delim]";
560 else
561 ustr = "[-signal] [-ILfilnovx]";
562
563 fprintf(stderr,
564 "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
493 if (argc == 0)
494 selected[i] = 1;
495 }
496
497 if (!ancestors) {
498 pid = mypid;
499 while (pid) {
500 for (i = 0, kp = plist; i < nproc; i++, kp++) {

--- 77 unchanged lines hidden (view full) ---

578
579 if (pgrep)
580 ustr = "[-LSfilnoqvx] [-d delim]";
581 else
582 ustr = "[-signal] [-ILfilnovx]";
583
584 fprintf(stderr,
585 "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
565 " [-P ppid] [-U uid] [-g pgrp] [-j jid] [-s sid]\n"
566 " [-t tty] [-u euid] pattern ...\n", getprogname(),
567 ustr);
586 " [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jid]\n"
587 " [-s sid] [-t tty] [-u euid] pattern ...\n",
588 getprogname(), ustr);
568
569 exit(STATUS_BADUSAGE);
570}
571
572static void
573show_process(const struct kinfo_proc *kp)
574{
575 char **argv;

--- 83 unchanged lines hidden (view full) ---

659 if ((li = malloc(sizeof(*li))) == NULL) {
660 err(STATUS_ERROR, "Cannot allocate %zu bytes",
661 sizeof(*li));
662 }
663
664 SLIST_INSERT_HEAD(head, li, li_chain);
665 empty = 0;
666
589
590 exit(STATUS_BADUSAGE);
591}
592
593static void
594show_process(const struct kinfo_proc *kp)
595{
596 char **argv;

--- 83 unchanged lines hidden (view full) ---

680 if ((li = malloc(sizeof(*li))) == NULL) {
681 err(STATUS_ERROR, "Cannot allocate %zu bytes",
682 sizeof(*li));
683 }
684
685 SLIST_INSERT_HEAD(head, li, li_chain);
686 empty = 0;
687
667 li->li_number = (uid_t)strtol(sp, &ep, 0);
668 if (*ep == '\0') {
688 if (type != LT_CLASS)
689 li->li_number = (uid_t)strtol(sp, &ep, 0);
690
691 if (type != LT_CLASS && *ep == '\0') {
669 switch (type) {
670 case LT_PGRP:
671 if (li->li_number == 0)
672 li->li_number = getpgrp();
673 break;
674 case LT_SID:
675 if (li->li_number == 0)
676 li->li_number = getsid(mypid);

--- 68 unchanged lines hidden (view full) ---

745 if (strcmp(sp, "none") == 0)
746 li->li_number = 0;
747 else if (strcmp(sp, "any") == 0)
748 li->li_number = -1;
749 else if (*ep != '\0')
750 errx(STATUS_BADUSAGE,
751 "Invalid jail ID `%s'", sp);
752 break;
692 switch (type) {
693 case LT_PGRP:
694 if (li->li_number == 0)
695 li->li_number = getpgrp();
696 break;
697 case LT_SID:
698 if (li->li_number == 0)
699 li->li_number = getsid(mypid);

--- 68 unchanged lines hidden (view full) ---

768 if (strcmp(sp, "none") == 0)
769 li->li_number = 0;
770 else if (strcmp(sp, "any") == 0)
771 li->li_number = -1;
772 else if (*ep != '\0')
773 errx(STATUS_BADUSAGE,
774 "Invalid jail ID `%s'", sp);
775 break;
776 case LT_CLASS:
777 li->li_number = -1;
778 li->li_name = strdup(sp);
779 if (li->li_name == NULL)
780 err(STATUS_ERROR, "Cannot allocate memory");
781 break;
753 default:
754 usage();
755 }
756 }
757
758 if (empty)
759 usage();
760}

--- 45 unchanged lines hidden ---
782 default:
783 usage();
784 }
785 }
786
787 if (empty)
788 usage();
789}

--- 45 unchanged lines hidden ---