Annotation of src/usr.bin/renice/renice.c, Revision 1.6
1.6 ! pvalchev 1: /* $OpenBSD: renice.c,v 1.5 1999/03/04 16:14:58 millert 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
1.6 ! pvalchev 46: static char rcsid[] = "$OpenBSD: renice.c,v 1.5 1999/03/04 16:14:58 millert Exp $";
1.5 millert 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.6 ! pvalchev 60: int main __P((int, char **));
1.5 millert 61: int donice __P((int, uid_t, int));
62: void usage __P((void));
1.3 deraadt 63:
1.1 deraadt 64: /*
65: * Change the priority (nice) of processes
66: * or groups of processes which are already
67: * running.
68: */
1.3 deraadt 69: int
1.1 deraadt 70: main(argc, argv)
1.6 ! pvalchev 71: int argc;
1.1 deraadt 72: char **argv;
73: {
74: int which = PRIO_PROCESS;
1.5 millert 75: int errs = 0;
76: long prio, who = 0;
77: char *ep;
1.1 deraadt 78:
79: argc--, argv++;
1.5 millert 80: if (argc < 2)
81: usage();
82: prio = strtol(*argv, &ep, 10);
83: if (*ep != NULL)
84: usage();
1.1 deraadt 85: argc--, argv++;
86: if (prio > PRIO_MAX)
87: prio = PRIO_MAX;
88: if (prio < PRIO_MIN)
89: prio = PRIO_MIN;
90: for (; argc > 0; argc--, argv++) {
91: if (strcmp(*argv, "-g") == 0) {
92: which = PRIO_PGRP;
93: continue;
94: }
95: if (strcmp(*argv, "-u") == 0) {
96: which = PRIO_USER;
97: continue;
98: }
99: if (strcmp(*argv, "-p") == 0) {
100: which = PRIO_PROCESS;
101: continue;
102: }
103: if (which == PRIO_USER) {
104: register struct passwd *pwd = getpwnam(*argv);
105:
106: if (pwd == NULL) {
1.5 millert 107: warnx("%s: unknown user", *argv);
1.1 deraadt 108: continue;
109: }
110: who = pwd->pw_uid;
111: } else {
1.5 millert 112: who = strtol(*argv, &ep, 10);
113: if (*ep != NULL || who < 0) {
114: warnx("%s: bad value", *argv);
1.1 deraadt 115: continue;
116: }
117: }
1.5 millert 118: errs += donice(which, (uid_t)who, (int)prio);
1.1 deraadt 119: }
120: exit(errs != 0);
121: }
122:
1.3 deraadt 123: int
1.1 deraadt 124: donice(which, who, prio)
1.5 millert 125: int which;
126: uid_t who;
127: int prio;
1.1 deraadt 128: {
129: int oldprio;
130:
131: errno = 0, oldprio = getpriority(which, who);
132: if (oldprio == -1 && errno) {
1.5 millert 133: warn("getpriority: %d", who);
1.1 deraadt 134: return (1);
135: }
136: if (setpriority(which, who, prio) < 0) {
1.5 millert 137: warn("setpriority: %d", who);
1.1 deraadt 138: return (1);
139: }
140: printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
141: return (0);
1.5 millert 142: }
143:
144: void
145: usage()
146: {
147: extern char *__progname;
148:
149: fprintf(stderr, "usage: %s priority [[-p] pid ...] [[-g] pgrp ...] "
150: "[[-u] user ...]\n", __progname);
151: exit(1);
1.1 deraadt 152: }