xref: /illumos-gate/usr/src/cmd/renice/renice.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9acbbeafSnn  * Common Development and Distribution License (the "License").
6*9acbbeafSnn  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*9acbbeafSnn  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/resource.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate #include <grp.h>
457c478bd9Sstevel@tonic-gate #include <project.h>
467c478bd9Sstevel@tonic-gate #include <nl_types.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <unistd.h>
527c478bd9Sstevel@tonic-gate #include <ctype.h>
537c478bd9Sstevel@tonic-gate #include <zone.h>
547c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static void usage(void);
577c478bd9Sstevel@tonic-gate static int donice(int which, id_t who, int prio, int increment, char *who_s);
587c478bd9Sstevel@tonic-gate static int parse_obsolete_options(int argc, char **argv);
597c478bd9Sstevel@tonic-gate static int name2id(char *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	PRIO_MAX		19
627c478bd9Sstevel@tonic-gate #define	PRIO_MIN		-20
637c478bd9Sstevel@tonic-gate #define	RENICE_DEFAULT_PRIORITY	10
647c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_INCREMENT	1
657c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_ABSOLUTE	0
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate typedef struct {
687c478bd9Sstevel@tonic-gate 	int	id;
697c478bd9Sstevel@tonic-gate 	char	*name;
707c478bd9Sstevel@tonic-gate } type_t;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static type_t types[] = {
737c478bd9Sstevel@tonic-gate 	{ PRIO_PROCESS,		"pid"		},
747c478bd9Sstevel@tonic-gate 	{ PRIO_PGRP,		"pgid"		},
757c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"uid"		},
767c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"user"		},
777c478bd9Sstevel@tonic-gate 	{ PRIO_TASK,		"taskid"	},
787c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"projid"	},
797c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"project"	},
807c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"gid"		},
817c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"group"		},
827c478bd9Sstevel@tonic-gate 	{ PRIO_SESSION,		"sid"		},
837c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zone"		},
847c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zoneid"	},
857c478bd9Sstevel@tonic-gate 	{ PRIO_CONTRACT,	"ctid"		},
867c478bd9Sstevel@tonic-gate 	{ 0,			NULL		}
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Change the priority (nice) of processes
917c478bd9Sstevel@tonic-gate  * or groups of processes which are already
927c478bd9Sstevel@tonic-gate  * running.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate 
95a77d64afScf int
main(int argc,char * argv[])967c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	int c;
997c478bd9Sstevel@tonic-gate 	int optflag = 0;
1007c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
1017c478bd9Sstevel@tonic-gate 	id_t who = 0;
1027c478bd9Sstevel@tonic-gate 	int errs = 0;
1037c478bd9Sstevel@tonic-gate 	char *end_ptr;
1047c478bd9Sstevel@tonic-gate 	int incr = RENICE_DEFAULT_PRIORITY;
1057c478bd9Sstevel@tonic-gate 	int prio_type = RENICE_PRIO_INCREMENT;
1067c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
1077c478bd9Sstevel@tonic-gate 	struct group *grp;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1107c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1117c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (argc < 2)
1167c478bd9Sstevel@tonic-gate 		(void) usage();
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * There is ambiguity in the renice options spec.
1207c478bd9Sstevel@tonic-gate 	 * If argv[1] is in the valid range of priority values then
1217c478bd9Sstevel@tonic-gate 	 * treat it as a priority.  Otherwise, treat it as a pid.
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (isdigit(argv[1][0])) {
1257c478bd9Sstevel@tonic-gate 		if (strtol(argv[1], (char **)NULL, 10) > (PRIO_MAX+1)) {
1267c478bd9Sstevel@tonic-gate 			argc--;			/* renice pid ... */
1277c478bd9Sstevel@tonic-gate 			argv++;
1287c478bd9Sstevel@tonic-gate 			prio_type = RENICE_PRIO_INCREMENT;
1297c478bd9Sstevel@tonic-gate 		} else {			/* renice priority ... */
1307c478bd9Sstevel@tonic-gate 			exit(parse_obsolete_options(argc, argv));
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	} else if ((argv[1][0] == '-' || argv[1][0] == '+') &&
1337c478bd9Sstevel@tonic-gate 			isdigit(argv[1][1])) {	/* renice priority ... */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		exit(parse_obsolete_options(argc, argv));
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	} else {	/* renice [-n increment] [-g|-p|-u] ID ... */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "n:gpui:")) != -1) {
1407c478bd9Sstevel@tonic-gate 			switch (c) {
1417c478bd9Sstevel@tonic-gate 			case 'n':
1427c478bd9Sstevel@tonic-gate 				incr = strtol(optarg, &end_ptr, 10);
1437c478bd9Sstevel@tonic-gate 				prio_type = RENICE_PRIO_INCREMENT;
1447c478bd9Sstevel@tonic-gate 				if (*end_ptr != '\0')
1457c478bd9Sstevel@tonic-gate 					usage();
1467c478bd9Sstevel@tonic-gate 				break;
1477c478bd9Sstevel@tonic-gate 			case 'g':
1487c478bd9Sstevel@tonic-gate 				which = PRIO_PGRP;
1497c478bd9Sstevel@tonic-gate 				optflag++;
1507c478bd9Sstevel@tonic-gate 				break;
1517c478bd9Sstevel@tonic-gate 			case 'p':
1527c478bd9Sstevel@tonic-gate 				which = PRIO_PROCESS;
1537c478bd9Sstevel@tonic-gate 				optflag++;
1547c478bd9Sstevel@tonic-gate 				break;
1557c478bd9Sstevel@tonic-gate 			case 'u':
1567c478bd9Sstevel@tonic-gate 				which = PRIO_USER;
1577c478bd9Sstevel@tonic-gate 				optflag++;
1587c478bd9Sstevel@tonic-gate 				break;
1597c478bd9Sstevel@tonic-gate 			case 'i':
1607c478bd9Sstevel@tonic-gate 				which = name2id(optarg);
1617c478bd9Sstevel@tonic-gate 				optflag++;
1627c478bd9Sstevel@tonic-gate 				break;
1637c478bd9Sstevel@tonic-gate 			default:
1647c478bd9Sstevel@tonic-gate 				usage();
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		argc -= optind;
1697c478bd9Sstevel@tonic-gate 		argv += optind;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		if (argc == 0 || (optflag > 1))
1727c478bd9Sstevel@tonic-gate 			usage();
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		if (isdigit(argv[0][0])) {
1787c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			/* if a zone id, make sure it is valid */
1817c478bd9Sstevel@tonic-gate 			if (who >= 0 && end_ptr != *argv &&
1827c478bd9Sstevel@tonic-gate 			    *end_ptr == '\0' && (which != PRIO_ZONE ||
1837c478bd9Sstevel@tonic-gate 			    getzonenamebyid(who, NULL, 0) != -1) &&
1847c478bd9Sstevel@tonic-gate 			    (which != PRIO_CONTRACT || who != 0)) {
1857c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
1867c478bd9Sstevel@tonic-gate 				    *argv);
1877c478bd9Sstevel@tonic-gate 				continue;
1887c478bd9Sstevel@tonic-gate 			}
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		switch (which) {
1927c478bd9Sstevel@tonic-gate 		case PRIO_USER:
1937c478bd9Sstevel@tonic-gate 			if ((pwd = getpwnam(*argv)) != NULL) {
1947c478bd9Sstevel@tonic-gate 				who = pwd->pw_uid;
1957c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
1967c478bd9Sstevel@tonic-gate 				    *argv);
1977c478bd9Sstevel@tonic-gate 			} else {
1987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1997c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
2007c478bd9Sstevel@tonic-gate 				    *argv);
2017c478bd9Sstevel@tonic-gate 				errs++;
2027c478bd9Sstevel@tonic-gate 			}
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case PRIO_GROUP:
2057c478bd9Sstevel@tonic-gate 			if ((grp = getgrnam(*argv)) != NULL) {
2067c478bd9Sstevel@tonic-gate 				who = grp->gr_gid;
2077c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
2087c478bd9Sstevel@tonic-gate 				    *argv);
2097c478bd9Sstevel@tonic-gate 			} else {
2107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2117c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown group: %s\n"),
2127c478bd9Sstevel@tonic-gate 				    *argv);
2137c478bd9Sstevel@tonic-gate 				errs++;
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		case PRIO_PROJECT:
2177c478bd9Sstevel@tonic-gate 			if ((who = getprojidbyname(*argv)) != (id_t)-1) {
2187c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
2197c478bd9Sstevel@tonic-gate 				    *argv);
2207c478bd9Sstevel@tonic-gate 			} else {
2217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2227c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown project: %s\n"),
2237c478bd9Sstevel@tonic-gate 				    *argv);
2247c478bd9Sstevel@tonic-gate 				errs++;
2257c478bd9Sstevel@tonic-gate 			}
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 		case PRIO_ZONE:
2287c478bd9Sstevel@tonic-gate 			if (zone_get_id(*argv, &who) != 0) {
2297c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2307c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown zone: %s\n"),
2317c478bd9Sstevel@tonic-gate 				    *argv);
2327c478bd9Sstevel@tonic-gate 				errs++;
2337c478bd9Sstevel@tonic-gate 				break;
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 			errs += donice(which, who, incr, prio_type, *argv);
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 		default:
2387c478bd9Sstevel@tonic-gate 			/*
2397c478bd9Sstevel@tonic-gate 			 * In all other cases it is invalid id or name
2407c478bd9Sstevel@tonic-gate 			 */
2417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2427c478bd9Sstevel@tonic-gate 			    gettext("renice: bad value: %s\n"), *argv);
2437c478bd9Sstevel@tonic-gate 			errs++;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	return (errs != 0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static int
parse_obsolete_options(int argc,char * argv[])2517c478bd9Sstevel@tonic-gate parse_obsolete_options(int argc, char *argv[])
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
2547c478bd9Sstevel@tonic-gate 	id_t who = 0;
2557c478bd9Sstevel@tonic-gate 	int prio;
2567c478bd9Sstevel@tonic-gate 	int errs = 0;
2577c478bd9Sstevel@tonic-gate 	char *end_ptr;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	argc--;
2607c478bd9Sstevel@tonic-gate 	argv++;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (argc < 2) {
2637c478bd9Sstevel@tonic-gate 		usage();
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	prio = strtol(*argv, &end_ptr, 10);
2677c478bd9Sstevel@tonic-gate 	if (*end_ptr != '\0') {
2687c478bd9Sstevel@tonic-gate 		usage();
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (prio == 20) {
2727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2737c478bd9Sstevel@tonic-gate 			gettext("renice: nice value 20 rounded down to 19\n"));
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	argc--;
2777c478bd9Sstevel@tonic-gate 	argv++;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
2807c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-g") == 0) {
2817c478bd9Sstevel@tonic-gate 			which = PRIO_PGRP;
2827c478bd9Sstevel@tonic-gate 			continue;
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-u") == 0) {
2857c478bd9Sstevel@tonic-gate 			which = PRIO_USER;
2867c478bd9Sstevel@tonic-gate 			continue;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-p") == 0) {
2897c478bd9Sstevel@tonic-gate 			which = PRIO_PROCESS;
2907c478bd9Sstevel@tonic-gate 			continue;
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		if (which == PRIO_USER && !isdigit(argv[0][0])) {
2937c478bd9Sstevel@tonic-gate 			struct passwd *pwd = getpwnam(*argv);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 			if (pwd == NULL) {
2967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2977c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
2987c478bd9Sstevel@tonic-gate 				    *argv);
2997c478bd9Sstevel@tonic-gate 				errs++;
3007c478bd9Sstevel@tonic-gate 				continue;
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 			who = pwd->pw_uid;
3037c478bd9Sstevel@tonic-gate 		} else {
3047c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
3057c478bd9Sstevel@tonic-gate 			if ((who < 0) || (*end_ptr != '\0')) {
3067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3077c478bd9Sstevel@tonic-gate 				    gettext("renice: bad value: %s\n"), *argv);
3087c478bd9Sstevel@tonic-gate 				errs++;
3097c478bd9Sstevel@tonic-gate 				continue;
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 		errs += donice(which, who, prio, RENICE_PRIO_ABSOLUTE, *argv);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	return (errs != 0);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static int
donice(int which,id_t who,int prio,int increment,char * who_s)3207c478bd9Sstevel@tonic-gate donice(int which, id_t who, int prio, int increment, char *who_s)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	int oldprio;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	oldprio = getpriority(which, who);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (oldprio == -1 && errno) {
3277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %d:"), who);
3287c478bd9Sstevel@tonic-gate 		perror("getpriority");
3297c478bd9Sstevel@tonic-gate 		return (1);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (increment)
3337c478bd9Sstevel@tonic-gate 		prio = oldprio + prio;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (setpriority(which, who, prio) < 0) {
3367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %s:"), who_s);
337*9acbbeafSnn 		if (errno == EACCES && prio < oldprio)
3387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3397c478bd9Sstevel@tonic-gate 			    " Cannot lower nice value.\n"));
3407c478bd9Sstevel@tonic-gate 		else
3417c478bd9Sstevel@tonic-gate 			perror("setpriority");
3427c478bd9Sstevel@tonic-gate 		return (1);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	return (0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate static void
usage()3497c478bd9Sstevel@tonic-gate usage()
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3527c478bd9Sstevel@tonic-gate 	    gettext("usage: renice [-n increment] [-i idtype] ID ...\n"));
3537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3547c478bd9Sstevel@tonic-gate 	    gettext("       renice [-n increment] [-g | -p | -u] ID ...\n"));
3557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3567c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3577c478bd9Sstevel@tonic-gate 	    "[-p] pid ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3597c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3607c478bd9Sstevel@tonic-gate 	    " -g pgrp ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3627c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3637c478bd9Sstevel@tonic-gate 	    " -u user ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3657c478bd9Sstevel@tonic-gate 	    gettext("  where %d <= priority <= %d\n"), PRIO_MIN, PRIO_MAX);
3667c478bd9Sstevel@tonic-gate 	exit(2);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static int
name2id(char * name)3707c478bd9Sstevel@tonic-gate name2id(char *name)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	type_t *type = types;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	while (type->name != NULL) {
3757c478bd9Sstevel@tonic-gate 		if (strcmp(type->name, name) == 0)
3767c478bd9Sstevel@tonic-gate 			return (type->id);
3777c478bd9Sstevel@tonic-gate 		type++;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("renice: unknown id type: %s\n"), name);
3807c478bd9Sstevel@tonic-gate 	exit(1);
3817c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3827c478bd9Sstevel@tonic-gate }
383