xref: /titanic_50/usr/src/cmd/renice/renice.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/resource.h>
45*7c478bd9Sstevel@tonic-gate #include <stdio.h>
46*7c478bd9Sstevel@tonic-gate #include <pwd.h>
47*7c478bd9Sstevel@tonic-gate #include <grp.h>
48*7c478bd9Sstevel@tonic-gate #include <project.h>
49*7c478bd9Sstevel@tonic-gate #include <nl_types.h>
50*7c478bd9Sstevel@tonic-gate #include <locale.h>
51*7c478bd9Sstevel@tonic-gate #include <errno.h>
52*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
53*7c478bd9Sstevel@tonic-gate #include <string.h>
54*7c478bd9Sstevel@tonic-gate #include <unistd.h>
55*7c478bd9Sstevel@tonic-gate #include <ctype.h>
56*7c478bd9Sstevel@tonic-gate #include <zone.h>
57*7c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static void usage(void);
60*7c478bd9Sstevel@tonic-gate static int donice(int which, id_t who, int prio, int increment, char *who_s);
61*7c478bd9Sstevel@tonic-gate static int parse_obsolete_options(int argc, char **argv);
62*7c478bd9Sstevel@tonic-gate static int name2id(char *);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #define	PRIO_MAX		19
65*7c478bd9Sstevel@tonic-gate #define	PRIO_MIN		-20
66*7c478bd9Sstevel@tonic-gate #define	RENICE_DEFAULT_PRIORITY	10
67*7c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_INCREMENT	1
68*7c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_ABSOLUTE	0
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate typedef struct {
71*7c478bd9Sstevel@tonic-gate 	int	id;
72*7c478bd9Sstevel@tonic-gate 	char	*name;
73*7c478bd9Sstevel@tonic-gate } type_t;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static type_t types[] = {
76*7c478bd9Sstevel@tonic-gate 	{ PRIO_PROCESS,		"pid"		},
77*7c478bd9Sstevel@tonic-gate 	{ PRIO_PGRP,		"pgid"		},
78*7c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"uid"		},
79*7c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"user"		},
80*7c478bd9Sstevel@tonic-gate 	{ PRIO_TASK,		"taskid"	},
81*7c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"projid"	},
82*7c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"project"	},
83*7c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"gid"		},
84*7c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"group"		},
85*7c478bd9Sstevel@tonic-gate 	{ PRIO_SESSION,		"sid"		},
86*7c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zone"		},
87*7c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zoneid"	},
88*7c478bd9Sstevel@tonic-gate 	{ PRIO_CONTRACT,	"ctid"		},
89*7c478bd9Sstevel@tonic-gate 	{ 0,			NULL		}
90*7c478bd9Sstevel@tonic-gate };
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * Change the priority (nice) of processes
94*7c478bd9Sstevel@tonic-gate  * or groups of processes which are already
95*7c478bd9Sstevel@tonic-gate  * running.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	int c;
101*7c478bd9Sstevel@tonic-gate 	int optflag = 0;
102*7c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
103*7c478bd9Sstevel@tonic-gate 	id_t who = 0;
104*7c478bd9Sstevel@tonic-gate 	int errs = 0;
105*7c478bd9Sstevel@tonic-gate 	char *end_ptr;
106*7c478bd9Sstevel@tonic-gate 	int incr = RENICE_DEFAULT_PRIORITY;
107*7c478bd9Sstevel@tonic-gate 	int prio_type = RENICE_PRIO_INCREMENT;
108*7c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
109*7c478bd9Sstevel@tonic-gate 	struct group *grp;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
112*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
113*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
114*7c478bd9Sstevel@tonic-gate #endif
115*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if (argc < 2)
118*7c478bd9Sstevel@tonic-gate 		(void) usage();
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	/*
121*7c478bd9Sstevel@tonic-gate 	 * There is ambiguity in the renice options spec.
122*7c478bd9Sstevel@tonic-gate 	 * If argv[1] is in the valid range of priority values then
123*7c478bd9Sstevel@tonic-gate 	 * treat it as a priority.  Otherwise, treat it as a pid.
124*7c478bd9Sstevel@tonic-gate 	 */
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (isdigit(argv[1][0])) {
127*7c478bd9Sstevel@tonic-gate 		if (strtol(argv[1], (char **)NULL, 10) > (PRIO_MAX+1)) {
128*7c478bd9Sstevel@tonic-gate 			argc--;			/* renice pid ... */
129*7c478bd9Sstevel@tonic-gate 			argv++;
130*7c478bd9Sstevel@tonic-gate 			prio_type = RENICE_PRIO_INCREMENT;
131*7c478bd9Sstevel@tonic-gate 		} else {			/* renice priority ... */
132*7c478bd9Sstevel@tonic-gate 			exit(parse_obsolete_options(argc, argv));
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 	} else if ((argv[1][0] == '-' || argv[1][0] == '+') &&
135*7c478bd9Sstevel@tonic-gate 			isdigit(argv[1][1])) {	/* renice priority ... */
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		exit(parse_obsolete_options(argc, argv));
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	} else {	/* renice [-n increment] [-g|-p|-u] ID ... */
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "n:gpui:")) != -1) {
142*7c478bd9Sstevel@tonic-gate 			switch (c) {
143*7c478bd9Sstevel@tonic-gate 			case 'n':
144*7c478bd9Sstevel@tonic-gate 				incr = strtol(optarg, &end_ptr, 10);
145*7c478bd9Sstevel@tonic-gate 				prio_type = RENICE_PRIO_INCREMENT;
146*7c478bd9Sstevel@tonic-gate 				if (*end_ptr != '\0')
147*7c478bd9Sstevel@tonic-gate 					usage();
148*7c478bd9Sstevel@tonic-gate 				break;
149*7c478bd9Sstevel@tonic-gate 			case 'g':
150*7c478bd9Sstevel@tonic-gate 				which = PRIO_PGRP;
151*7c478bd9Sstevel@tonic-gate 				optflag++;
152*7c478bd9Sstevel@tonic-gate 				break;
153*7c478bd9Sstevel@tonic-gate 			case 'p':
154*7c478bd9Sstevel@tonic-gate 				which = PRIO_PROCESS;
155*7c478bd9Sstevel@tonic-gate 				optflag++;
156*7c478bd9Sstevel@tonic-gate 				break;
157*7c478bd9Sstevel@tonic-gate 			case 'u':
158*7c478bd9Sstevel@tonic-gate 				which = PRIO_USER;
159*7c478bd9Sstevel@tonic-gate 				optflag++;
160*7c478bd9Sstevel@tonic-gate 				break;
161*7c478bd9Sstevel@tonic-gate 			case 'i':
162*7c478bd9Sstevel@tonic-gate 				which = name2id(optarg);
163*7c478bd9Sstevel@tonic-gate 				optflag++;
164*7c478bd9Sstevel@tonic-gate 				break;
165*7c478bd9Sstevel@tonic-gate 			default:
166*7c478bd9Sstevel@tonic-gate 				usage();
167*7c478bd9Sstevel@tonic-gate 			}
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 		argc -= optind;
171*7c478bd9Sstevel@tonic-gate 		argv += optind;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		if (argc == 0 || (optflag > 1))
174*7c478bd9Sstevel@tonic-gate 			usage();
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 		if (isdigit(argv[0][0])) {
180*7c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 			/* if a zone id, make sure it is valid */
183*7c478bd9Sstevel@tonic-gate 			if (who >= 0 && end_ptr != *argv &&
184*7c478bd9Sstevel@tonic-gate 			    *end_ptr == '\0' && (which != PRIO_ZONE ||
185*7c478bd9Sstevel@tonic-gate 			    getzonenamebyid(who, NULL, 0) != -1) &&
186*7c478bd9Sstevel@tonic-gate 			    (which != PRIO_CONTRACT || who != 0)) {
187*7c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
188*7c478bd9Sstevel@tonic-gate 				    *argv);
189*7c478bd9Sstevel@tonic-gate 				continue;
190*7c478bd9Sstevel@tonic-gate 			}
191*7c478bd9Sstevel@tonic-gate 		}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		switch (which) {
194*7c478bd9Sstevel@tonic-gate 		case PRIO_USER:
195*7c478bd9Sstevel@tonic-gate 			if ((pwd = getpwnam(*argv)) != NULL) {
196*7c478bd9Sstevel@tonic-gate 				who = pwd->pw_uid;
197*7c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
198*7c478bd9Sstevel@tonic-gate 				    *argv);
199*7c478bd9Sstevel@tonic-gate 			} else {
200*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
201*7c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
202*7c478bd9Sstevel@tonic-gate 				    *argv);
203*7c478bd9Sstevel@tonic-gate 				errs++;
204*7c478bd9Sstevel@tonic-gate 			}
205*7c478bd9Sstevel@tonic-gate 			break;
206*7c478bd9Sstevel@tonic-gate 		case PRIO_GROUP:
207*7c478bd9Sstevel@tonic-gate 			if ((grp = getgrnam(*argv)) != NULL) {
208*7c478bd9Sstevel@tonic-gate 				who = grp->gr_gid;
209*7c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
210*7c478bd9Sstevel@tonic-gate 				    *argv);
211*7c478bd9Sstevel@tonic-gate 			} else {
212*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
213*7c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown group: %s\n"),
214*7c478bd9Sstevel@tonic-gate 				    *argv);
215*7c478bd9Sstevel@tonic-gate 				errs++;
216*7c478bd9Sstevel@tonic-gate 			}
217*7c478bd9Sstevel@tonic-gate 			break;
218*7c478bd9Sstevel@tonic-gate 		case PRIO_PROJECT:
219*7c478bd9Sstevel@tonic-gate 			if ((who = getprojidbyname(*argv)) != (id_t)-1) {
220*7c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
221*7c478bd9Sstevel@tonic-gate 				    *argv);
222*7c478bd9Sstevel@tonic-gate 			} else {
223*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
224*7c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown project: %s\n"),
225*7c478bd9Sstevel@tonic-gate 				    *argv);
226*7c478bd9Sstevel@tonic-gate 				errs++;
227*7c478bd9Sstevel@tonic-gate 			}
228*7c478bd9Sstevel@tonic-gate 			break;
229*7c478bd9Sstevel@tonic-gate 		case PRIO_ZONE:
230*7c478bd9Sstevel@tonic-gate 			if (zone_get_id(*argv, &who) != 0) {
231*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
232*7c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown zone: %s\n"),
233*7c478bd9Sstevel@tonic-gate 				    *argv);
234*7c478bd9Sstevel@tonic-gate 				errs++;
235*7c478bd9Sstevel@tonic-gate 				break;
236*7c478bd9Sstevel@tonic-gate 			}
237*7c478bd9Sstevel@tonic-gate 			errs += donice(which, who, incr, prio_type, *argv);
238*7c478bd9Sstevel@tonic-gate 			break;
239*7c478bd9Sstevel@tonic-gate 		default:
240*7c478bd9Sstevel@tonic-gate 			/*
241*7c478bd9Sstevel@tonic-gate 			 * In all other cases it is invalid id or name
242*7c478bd9Sstevel@tonic-gate 			 */
243*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
244*7c478bd9Sstevel@tonic-gate 			    gettext("renice: bad value: %s\n"), *argv);
245*7c478bd9Sstevel@tonic-gate 			errs++;
246*7c478bd9Sstevel@tonic-gate 		}
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	return (errs != 0);
250*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate static int
254*7c478bd9Sstevel@tonic-gate parse_obsolete_options(int argc, char *argv[])
255*7c478bd9Sstevel@tonic-gate {
256*7c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
257*7c478bd9Sstevel@tonic-gate 	id_t who = 0;
258*7c478bd9Sstevel@tonic-gate 	int prio;
259*7c478bd9Sstevel@tonic-gate 	int errs = 0;
260*7c478bd9Sstevel@tonic-gate 	char *end_ptr;
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	argc--;
263*7c478bd9Sstevel@tonic-gate 	argv++;
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	if (argc < 2) {
266*7c478bd9Sstevel@tonic-gate 		usage();
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	prio = strtol(*argv, &end_ptr, 10);
270*7c478bd9Sstevel@tonic-gate 	if (*end_ptr != '\0') {
271*7c478bd9Sstevel@tonic-gate 		usage();
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	if (prio == 20) {
275*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
276*7c478bd9Sstevel@tonic-gate 			gettext("renice: nice value 20 rounded down to 19\n"));
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	argc--;
280*7c478bd9Sstevel@tonic-gate 	argv++;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
283*7c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-g") == 0) {
284*7c478bd9Sstevel@tonic-gate 			which = PRIO_PGRP;
285*7c478bd9Sstevel@tonic-gate 			continue;
286*7c478bd9Sstevel@tonic-gate 		}
287*7c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-u") == 0) {
288*7c478bd9Sstevel@tonic-gate 			which = PRIO_USER;
289*7c478bd9Sstevel@tonic-gate 			continue;
290*7c478bd9Sstevel@tonic-gate 		}
291*7c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-p") == 0) {
292*7c478bd9Sstevel@tonic-gate 			which = PRIO_PROCESS;
293*7c478bd9Sstevel@tonic-gate 			continue;
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 		if (which == PRIO_USER && !isdigit(argv[0][0])) {
296*7c478bd9Sstevel@tonic-gate 			struct passwd *pwd = getpwnam(*argv);
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 			if (pwd == NULL) {
299*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
300*7c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
301*7c478bd9Sstevel@tonic-gate 				    *argv);
302*7c478bd9Sstevel@tonic-gate 				errs++;
303*7c478bd9Sstevel@tonic-gate 				continue;
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 			who = pwd->pw_uid;
306*7c478bd9Sstevel@tonic-gate 		} else {
307*7c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
308*7c478bd9Sstevel@tonic-gate 			if ((who < 0) || (*end_ptr != '\0')) {
309*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
310*7c478bd9Sstevel@tonic-gate 				    gettext("renice: bad value: %s\n"), *argv);
311*7c478bd9Sstevel@tonic-gate 				errs++;
312*7c478bd9Sstevel@tonic-gate 				continue;
313*7c478bd9Sstevel@tonic-gate 			}
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 		errs += donice(which, who, prio, RENICE_PRIO_ABSOLUTE, *argv);
316*7c478bd9Sstevel@tonic-gate 	}
317*7c478bd9Sstevel@tonic-gate 	return (errs != 0);
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate static int
323*7c478bd9Sstevel@tonic-gate donice(int which, id_t who, int prio, int increment, char *who_s)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	int oldprio;
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	oldprio = getpriority(which, who);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	if (oldprio == -1 && errno) {
330*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %d:"), who);
331*7c478bd9Sstevel@tonic-gate 		perror("getpriority");
332*7c478bd9Sstevel@tonic-gate 		return (1);
333*7c478bd9Sstevel@tonic-gate 	}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 	if (increment)
336*7c478bd9Sstevel@tonic-gate 		prio = oldprio + prio;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	if (setpriority(which, who, prio) < 0) {
339*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %s:"), who_s);
340*7c478bd9Sstevel@tonic-gate 		if (errno == EPERM && prio < oldprio)
341*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
342*7c478bd9Sstevel@tonic-gate 			    " Cannot lower nice value.\n"));
343*7c478bd9Sstevel@tonic-gate 		else
344*7c478bd9Sstevel@tonic-gate 			perror("setpriority");
345*7c478bd9Sstevel@tonic-gate 		return (1);
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	return (0);
349*7c478bd9Sstevel@tonic-gate }
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate static void
352*7c478bd9Sstevel@tonic-gate usage()
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
355*7c478bd9Sstevel@tonic-gate 	    gettext("usage: renice [-n increment] [-i idtype] ID ...\n"));
356*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
357*7c478bd9Sstevel@tonic-gate 	    gettext("       renice [-n increment] [-g | -p | -u] ID ...\n"));
358*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
359*7c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
360*7c478bd9Sstevel@tonic-gate 	    "[-p] pid ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
361*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
362*7c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
363*7c478bd9Sstevel@tonic-gate 	    " -g pgrp ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
364*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
365*7c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
366*7c478bd9Sstevel@tonic-gate 	    " -u user ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
367*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
368*7c478bd9Sstevel@tonic-gate 	    gettext("  where %d <= priority <= %d\n"), PRIO_MIN, PRIO_MAX);
369*7c478bd9Sstevel@tonic-gate 	exit(2);
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate static int
373*7c478bd9Sstevel@tonic-gate name2id(char *name)
374*7c478bd9Sstevel@tonic-gate {
375*7c478bd9Sstevel@tonic-gate 	type_t *type = types;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	while (type->name != NULL) {
378*7c478bd9Sstevel@tonic-gate 		if (strcmp(type->name, name) == 0)
379*7c478bd9Sstevel@tonic-gate 			return (type->id);
380*7c478bd9Sstevel@tonic-gate 		type++;
381*7c478bd9Sstevel@tonic-gate 	}
382*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("renice: unknown id type: %s\n"), name);
383*7c478bd9Sstevel@tonic-gate 	exit(1);
384*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
385*7c478bd9Sstevel@tonic-gate }
386