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