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