xref: /freebsd/usr.bin/renice/renice.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
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. Neither the name of the University nor the names of its contributors
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
33 static const char copyright[] =
34 "@(#) Copyright (c) 1983, 1989, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)renice.c	8.1 (Berkeley) 6/9/93";
41 #endif /* not lint */
42 #endif
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/types.h>
48 #include <sys/time.h>
49 #include <sys/resource.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <pwd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 
59 static int	donice(int, int, int, int);
60 static int	getnum(const char *, const char *, int *);
61 static void	usage(void);
62 
63 /*
64  * Change the priority (nice) of processes
65  * or groups of processes which are already
66  * running.
67  */
68 int
69 main(int argc, char *argv[])
70 {
71 	struct passwd *pwd;
72 	int errs, incr, prio, which, who;
73 
74 	errs = 0;
75 	incr = 0;
76 	which = PRIO_PROCESS;
77 	who = 0;
78 	argc--, argv++;
79 	if (argc < 2)
80 		usage();
81 	if (strcmp(*argv, "-n") == 0) {
82 		incr = 1;
83 		argc--, argv++;
84 		if (argc < 2)
85 			usage();
86 	}
87 	if (getnum("priority", *argv, &prio))
88 		return (1);
89 	argc--, argv++;
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 			if ((pwd = getpwnam(*argv)) != NULL)
105 				who = pwd->pw_uid;
106 			else if (getnum("uid", *argv, &who)) {
107 				errs++;
108 				continue;
109 			} else if (who < 0) {
110 				warnx("%s: bad value", *argv);
111 				errs++;
112 				continue;
113 			}
114 		} else {
115 			if (getnum("pid", *argv, &who)) {
116 				errs++;
117 				continue;
118 			}
119 			if (who < 0) {
120 				warnx("%s: bad value", *argv);
121 				errs++;
122 				continue;
123 			}
124 		}
125 		errs += donice(which, who, prio, incr);
126 	}
127 	exit(errs != 0);
128 }
129 
130 static int
131 donice(int which, int who, int prio, int incr)
132 {
133 	int oldprio;
134 
135 	errno = 0;
136 	oldprio = getpriority(which, who);
137 	if (oldprio == -1 && errno) {
138 		warn("%d: getpriority", who);
139 		return (1);
140 	}
141 	if (incr)
142 		prio = oldprio + prio;
143 	if (prio > PRIO_MAX)
144 		prio = PRIO_MAX;
145 	if (prio < PRIO_MIN)
146 		prio = PRIO_MIN;
147 	if (setpriority(which, who, prio) < 0) {
148 		warn("%d: setpriority", who);
149 		return (1);
150 	}
151 	fprintf(stderr, "%d: old priority %d, new priority %d\n", who,
152 	    oldprio, prio);
153 	return (0);
154 }
155 
156 static int
157 getnum(const char *com, const char *str, int *val)
158 {
159 	long v;
160 	char *ep;
161 
162 	errno = 0;
163 	v = strtol(str, &ep, 10);
164 	if (v < INT_MIN || v > INT_MAX || errno == ERANGE) {
165 		warnx("%s argument %s is out of range.", com, str);
166 		return (1);
167 	}
168 	if (ep == str || *ep != '\0' || errno != 0) {
169 		warnx("Bad %s argument: %s.", com, str);
170 		return (1);
171 	}
172 
173 	*val = (int)v;
174 	return (0);
175 }
176 
177 static void
178 usage(void)
179 {
180 	fprintf(stderr, "%s\n%s\n",
181 "usage: renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]",
182 "       renice -n increment [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]");
183 	exit(1);
184 }
185