Annotation of src/usr.bin/renice/renice.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: renice.c,v 1.4 1998/12/20 01:13:33 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
1.5 ! millert 4: * Copyright (c) 1983, 1989, 1993
! 5: * The Regents of the University of California. All rights reserved.
1.1 deraadt 6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
1.5 ! millert 37: static char copyright[] =
! 38: "@(#) Copyright (c) 1983, 1989, 1993\n\
! 39: The Regents of the University of California. All rights reserved.\n";
1.1 deraadt 40: #endif /* not lint */
41:
42: #ifndef lint
1.5 ! millert 43: #if 0
! 44: static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 6/9/93";
! 45: #else
! 46: static char rcsid[] = "$OpenBSD: renice.c,v 1.4 1998/12/20 01:13:33 deraadt Exp $";
! 47: #endif
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/time.h>
52: #include <sys/resource.h>
1.5 ! millert 53:
1.1 deraadt 54: #include <stdio.h>
1.3 deraadt 55: #include <stdlib.h>
1.1 deraadt 56: #include <pwd.h>
1.5 ! millert 57: #include <err.h>
! 58: #include <errno.h>
1.1 deraadt 59:
1.5 ! millert 60: int donice __P((int, uid_t, int));
! 61: void usage __P((void));
1.3 deraadt 62:
1.1 deraadt 63: /*
64: * Change the priority (nice) of processes
65: * or groups of processes which are already
66: * running.
67: */
1.3 deraadt 68: int
1.1 deraadt 69: main(argc, argv)
70: char **argv;
71: {
72: int which = PRIO_PROCESS;
1.5 ! millert 73: int errs = 0;
! 74: long prio, who = 0;
! 75: char *ep;
1.1 deraadt 76:
77: argc--, argv++;
1.5 ! millert 78: if (argc < 2)
! 79: usage();
! 80: prio = strtol(*argv, &ep, 10);
! 81: if (*ep != NULL)
! 82: usage();
1.1 deraadt 83: argc--, argv++;
84: if (prio > PRIO_MAX)
85: prio = PRIO_MAX;
86: if (prio < PRIO_MIN)
87: prio = PRIO_MIN;
88: for (; argc > 0; argc--, argv++) {
89: if (strcmp(*argv, "-g") == 0) {
90: which = PRIO_PGRP;
91: continue;
92: }
93: if (strcmp(*argv, "-u") == 0) {
94: which = PRIO_USER;
95: continue;
96: }
97: if (strcmp(*argv, "-p") == 0) {
98: which = PRIO_PROCESS;
99: continue;
100: }
101: if (which == PRIO_USER) {
102: register struct passwd *pwd = getpwnam(*argv);
103:
104: if (pwd == NULL) {
1.5 ! millert 105: warnx("%s: unknown user", *argv);
1.1 deraadt 106: continue;
107: }
108: who = pwd->pw_uid;
109: } else {
1.5 ! millert 110: who = strtol(*argv, &ep, 10);
! 111: if (*ep != NULL || who < 0) {
! 112: warnx("%s: bad value", *argv);
1.1 deraadt 113: continue;
114: }
115: }
1.5 ! millert 116: errs += donice(which, (uid_t)who, (int)prio);
1.1 deraadt 117: }
118: exit(errs != 0);
119: }
120:
1.3 deraadt 121: int
1.1 deraadt 122: donice(which, who, prio)
1.5 ! millert 123: int which;
! 124: uid_t who;
! 125: int prio;
1.1 deraadt 126: {
127: int oldprio;
128:
129: errno = 0, oldprio = getpriority(which, who);
130: if (oldprio == -1 && errno) {
1.5 ! millert 131: warn("getpriority: %d", who);
1.1 deraadt 132: return (1);
133: }
134: if (setpriority(which, who, prio) < 0) {
1.5 ! millert 135: warn("setpriority: %d", who);
1.1 deraadt 136: return (1);
137: }
138: printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
139: return (0);
1.5 ! millert 140: }
! 141:
! 142: void
! 143: usage()
! 144: {
! 145: extern char *__progname;
! 146:
! 147: fprintf(stderr, "usage: %s priority [[-p] pid ...] [[-g] pgrp ...] "
! 148: "[[-u] user ...]\n", __progname);
! 149: exit(1);
1.1 deraadt 150: }