xref: /titanic_50/usr/src/cmd/renice/renice.c (revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c)
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*9acbbeafSnn35248  * Common Development and Distribution License (the "License").
6*9acbbeafSnn35248  * 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*9acbbeafSnn35248  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/resource.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <pwd.h>
467c478bd9Sstevel@tonic-gate #include <grp.h>
477c478bd9Sstevel@tonic-gate #include <project.h>
487c478bd9Sstevel@tonic-gate #include <nl_types.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <errno.h>
517c478bd9Sstevel@tonic-gate #include <stdlib.h>
527c478bd9Sstevel@tonic-gate #include <string.h>
537c478bd9Sstevel@tonic-gate #include <unistd.h>
547c478bd9Sstevel@tonic-gate #include <ctype.h>
557c478bd9Sstevel@tonic-gate #include <zone.h>
567c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static void usage(void);
597c478bd9Sstevel@tonic-gate static int donice(int which, id_t who, int prio, int increment, char *who_s);
607c478bd9Sstevel@tonic-gate static int parse_obsolete_options(int argc, char **argv);
617c478bd9Sstevel@tonic-gate static int name2id(char *);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	PRIO_MAX		19
647c478bd9Sstevel@tonic-gate #define	PRIO_MIN		-20
657c478bd9Sstevel@tonic-gate #define	RENICE_DEFAULT_PRIORITY	10
667c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_INCREMENT	1
677c478bd9Sstevel@tonic-gate #define	RENICE_PRIO_ABSOLUTE	0
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct {
707c478bd9Sstevel@tonic-gate 	int	id;
717c478bd9Sstevel@tonic-gate 	char	*name;
727c478bd9Sstevel@tonic-gate } type_t;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static type_t types[] = {
757c478bd9Sstevel@tonic-gate 	{ PRIO_PROCESS,		"pid"		},
767c478bd9Sstevel@tonic-gate 	{ PRIO_PGRP,		"pgid"		},
777c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"uid"		},
787c478bd9Sstevel@tonic-gate 	{ PRIO_USER,		"user"		},
797c478bd9Sstevel@tonic-gate 	{ PRIO_TASK,		"taskid"	},
807c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"projid"	},
817c478bd9Sstevel@tonic-gate 	{ PRIO_PROJECT,		"project"	},
827c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"gid"		},
837c478bd9Sstevel@tonic-gate 	{ PRIO_GROUP,		"group"		},
847c478bd9Sstevel@tonic-gate 	{ PRIO_SESSION,		"sid"		},
857c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zone"		},
867c478bd9Sstevel@tonic-gate 	{ PRIO_ZONE,		"zoneid"	},
877c478bd9Sstevel@tonic-gate 	{ PRIO_CONTRACT,	"ctid"		},
887c478bd9Sstevel@tonic-gate 	{ 0,			NULL		}
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Change the priority (nice) of processes
937c478bd9Sstevel@tonic-gate  * or groups of processes which are already
947c478bd9Sstevel@tonic-gate  * running.
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
97a77d64afScf46844 int
main(int argc,char * argv[])987c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	int c;
1017c478bd9Sstevel@tonic-gate 	int optflag = 0;
1027c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
1037c478bd9Sstevel@tonic-gate 	id_t who = 0;
1047c478bd9Sstevel@tonic-gate 	int errs = 0;
1057c478bd9Sstevel@tonic-gate 	char *end_ptr;
1067c478bd9Sstevel@tonic-gate 	int incr = RENICE_DEFAULT_PRIORITY;
1077c478bd9Sstevel@tonic-gate 	int prio_type = RENICE_PRIO_INCREMENT;
1087c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
1097c478bd9Sstevel@tonic-gate 	struct group *grp;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1127c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1137c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (argc < 2)
1187c478bd9Sstevel@tonic-gate 		(void) usage();
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * There is ambiguity in the renice options spec.
1227c478bd9Sstevel@tonic-gate 	 * If argv[1] is in the valid range of priority values then
1237c478bd9Sstevel@tonic-gate 	 * treat it as a priority.  Otherwise, treat it as a pid.
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (isdigit(argv[1][0])) {
1277c478bd9Sstevel@tonic-gate 		if (strtol(argv[1], (char **)NULL, 10) > (PRIO_MAX+1)) {
1287c478bd9Sstevel@tonic-gate 			argc--;			/* renice pid ... */
1297c478bd9Sstevel@tonic-gate 			argv++;
1307c478bd9Sstevel@tonic-gate 			prio_type = RENICE_PRIO_INCREMENT;
1317c478bd9Sstevel@tonic-gate 		} else {			/* renice priority ... */
1327c478bd9Sstevel@tonic-gate 			exit(parse_obsolete_options(argc, argv));
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 	} else if ((argv[1][0] == '-' || argv[1][0] == '+') &&
1357c478bd9Sstevel@tonic-gate 			isdigit(argv[1][1])) {	/* renice priority ... */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		exit(parse_obsolete_options(argc, argv));
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	} else {	/* renice [-n increment] [-g|-p|-u] ID ... */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "n:gpui:")) != -1) {
1427c478bd9Sstevel@tonic-gate 			switch (c) {
1437c478bd9Sstevel@tonic-gate 			case 'n':
1447c478bd9Sstevel@tonic-gate 				incr = strtol(optarg, &end_ptr, 10);
1457c478bd9Sstevel@tonic-gate 				prio_type = RENICE_PRIO_INCREMENT;
1467c478bd9Sstevel@tonic-gate 				if (*end_ptr != '\0')
1477c478bd9Sstevel@tonic-gate 					usage();
1487c478bd9Sstevel@tonic-gate 				break;
1497c478bd9Sstevel@tonic-gate 			case 'g':
1507c478bd9Sstevel@tonic-gate 				which = PRIO_PGRP;
1517c478bd9Sstevel@tonic-gate 				optflag++;
1527c478bd9Sstevel@tonic-gate 				break;
1537c478bd9Sstevel@tonic-gate 			case 'p':
1547c478bd9Sstevel@tonic-gate 				which = PRIO_PROCESS;
1557c478bd9Sstevel@tonic-gate 				optflag++;
1567c478bd9Sstevel@tonic-gate 				break;
1577c478bd9Sstevel@tonic-gate 			case 'u':
1587c478bd9Sstevel@tonic-gate 				which = PRIO_USER;
1597c478bd9Sstevel@tonic-gate 				optflag++;
1607c478bd9Sstevel@tonic-gate 				break;
1617c478bd9Sstevel@tonic-gate 			case 'i':
1627c478bd9Sstevel@tonic-gate 				which = name2id(optarg);
1637c478bd9Sstevel@tonic-gate 				optflag++;
1647c478bd9Sstevel@tonic-gate 				break;
1657c478bd9Sstevel@tonic-gate 			default:
1667c478bd9Sstevel@tonic-gate 				usage();
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		argc -= optind;
1717c478bd9Sstevel@tonic-gate 		argv += optind;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		if (argc == 0 || (optflag > 1))
1747c478bd9Sstevel@tonic-gate 			usage();
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		if (isdigit(argv[0][0])) {
1807c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 			/* if a zone id, make sure it is valid */
1837c478bd9Sstevel@tonic-gate 			if (who >= 0 && end_ptr != *argv &&
1847c478bd9Sstevel@tonic-gate 			    *end_ptr == '\0' && (which != PRIO_ZONE ||
1857c478bd9Sstevel@tonic-gate 			    getzonenamebyid(who, NULL, 0) != -1) &&
1867c478bd9Sstevel@tonic-gate 			    (which != PRIO_CONTRACT || who != 0)) {
1877c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
1887c478bd9Sstevel@tonic-gate 				    *argv);
1897c478bd9Sstevel@tonic-gate 				continue;
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		switch (which) {
1947c478bd9Sstevel@tonic-gate 		case PRIO_USER:
1957c478bd9Sstevel@tonic-gate 			if ((pwd = getpwnam(*argv)) != NULL) {
1967c478bd9Sstevel@tonic-gate 				who = pwd->pw_uid;
1977c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
1987c478bd9Sstevel@tonic-gate 				    *argv);
1997c478bd9Sstevel@tonic-gate 			} else {
2007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2017c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
2027c478bd9Sstevel@tonic-gate 				    *argv);
2037c478bd9Sstevel@tonic-gate 				errs++;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 		case PRIO_GROUP:
2077c478bd9Sstevel@tonic-gate 			if ((grp = getgrnam(*argv)) != NULL) {
2087c478bd9Sstevel@tonic-gate 				who = grp->gr_gid;
2097c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
2107c478bd9Sstevel@tonic-gate 				    *argv);
2117c478bd9Sstevel@tonic-gate 			} else {
2127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2137c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown group: %s\n"),
2147c478bd9Sstevel@tonic-gate 				    *argv);
2157c478bd9Sstevel@tonic-gate 				errs++;
2167c478bd9Sstevel@tonic-gate 			}
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 		case PRIO_PROJECT:
2197c478bd9Sstevel@tonic-gate 			if ((who = getprojidbyname(*argv)) != (id_t)-1) {
2207c478bd9Sstevel@tonic-gate 				errs += donice(which, who, incr, prio_type,
2217c478bd9Sstevel@tonic-gate 				    *argv);
2227c478bd9Sstevel@tonic-gate 			} else {
2237c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2247c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown project: %s\n"),
2257c478bd9Sstevel@tonic-gate 				    *argv);
2267c478bd9Sstevel@tonic-gate 				errs++;
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		case PRIO_ZONE:
2307c478bd9Sstevel@tonic-gate 			if (zone_get_id(*argv, &who) != 0) {
2317c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2327c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown zone: %s\n"),
2337c478bd9Sstevel@tonic-gate 				    *argv);
2347c478bd9Sstevel@tonic-gate 				errs++;
2357c478bd9Sstevel@tonic-gate 				break;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 			errs += donice(which, who, incr, prio_type, *argv);
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		default:
2407c478bd9Sstevel@tonic-gate 			/*
2417c478bd9Sstevel@tonic-gate 			 * In all other cases it is invalid id or name
2427c478bd9Sstevel@tonic-gate 			 */
2437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2447c478bd9Sstevel@tonic-gate 			    gettext("renice: bad value: %s\n"), *argv);
2457c478bd9Sstevel@tonic-gate 			errs++;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (errs != 0);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static int
parse_obsolete_options(int argc,char * argv[])2537c478bd9Sstevel@tonic-gate parse_obsolete_options(int argc, char *argv[])
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	int which = PRIO_PROCESS;
2567c478bd9Sstevel@tonic-gate 	id_t who = 0;
2577c478bd9Sstevel@tonic-gate 	int prio;
2587c478bd9Sstevel@tonic-gate 	int errs = 0;
2597c478bd9Sstevel@tonic-gate 	char *end_ptr;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	argc--;
2627c478bd9Sstevel@tonic-gate 	argv++;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (argc < 2) {
2657c478bd9Sstevel@tonic-gate 		usage();
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	prio = strtol(*argv, &end_ptr, 10);
2697c478bd9Sstevel@tonic-gate 	if (*end_ptr != '\0') {
2707c478bd9Sstevel@tonic-gate 		usage();
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (prio == 20) {
2747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2757c478bd9Sstevel@tonic-gate 			gettext("renice: nice value 20 rounded down to 19\n"));
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	argc--;
2797c478bd9Sstevel@tonic-gate 	argv++;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	for (; argc > 0; argc--, argv++) {
2827c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-g") == 0) {
2837c478bd9Sstevel@tonic-gate 			which = PRIO_PGRP;
2847c478bd9Sstevel@tonic-gate 			continue;
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-u") == 0) {
2877c478bd9Sstevel@tonic-gate 			which = PRIO_USER;
2887c478bd9Sstevel@tonic-gate 			continue;
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "-p") == 0) {
2917c478bd9Sstevel@tonic-gate 			which = PRIO_PROCESS;
2927c478bd9Sstevel@tonic-gate 			continue;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		if (which == PRIO_USER && !isdigit(argv[0][0])) {
2957c478bd9Sstevel@tonic-gate 			struct passwd *pwd = getpwnam(*argv);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 			if (pwd == NULL) {
2987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2997c478bd9Sstevel@tonic-gate 				    gettext("renice: unknown user: %s\n"),
3007c478bd9Sstevel@tonic-gate 				    *argv);
3017c478bd9Sstevel@tonic-gate 				errs++;
3027c478bd9Sstevel@tonic-gate 				continue;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			who = pwd->pw_uid;
3057c478bd9Sstevel@tonic-gate 		} else {
3067c478bd9Sstevel@tonic-gate 			who = strtol(*argv, &end_ptr, 10);
3077c478bd9Sstevel@tonic-gate 			if ((who < 0) || (*end_ptr != '\0')) {
3087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
3097c478bd9Sstevel@tonic-gate 				    gettext("renice: bad value: %s\n"), *argv);
3107c478bd9Sstevel@tonic-gate 				errs++;
3117c478bd9Sstevel@tonic-gate 				continue;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		errs += donice(which, who, prio, RENICE_PRIO_ABSOLUTE, *argv);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	return (errs != 0);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static int
donice(int which,id_t who,int prio,int increment,char * who_s)3227c478bd9Sstevel@tonic-gate donice(int which, id_t who, int prio, int increment, char *who_s)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int oldprio;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	oldprio = getpriority(which, who);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (oldprio == -1 && errno) {
3297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %d:"), who);
3307c478bd9Sstevel@tonic-gate 		perror("getpriority");
3317c478bd9Sstevel@tonic-gate 		return (1);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if (increment)
3357c478bd9Sstevel@tonic-gate 		prio = oldprio + prio;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (setpriority(which, who, prio) < 0) {
3387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("renice: %s:"), who_s);
339*9acbbeafSnn35248 		if (errno == EACCES && prio < oldprio)
3407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3417c478bd9Sstevel@tonic-gate 			    " Cannot lower nice value.\n"));
3427c478bd9Sstevel@tonic-gate 		else
3437c478bd9Sstevel@tonic-gate 			perror("setpriority");
3447c478bd9Sstevel@tonic-gate 		return (1);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	return (0);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static void
usage()3517c478bd9Sstevel@tonic-gate usage()
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3547c478bd9Sstevel@tonic-gate 	    gettext("usage: renice [-n increment] [-i idtype] ID ...\n"));
3557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3567c478bd9Sstevel@tonic-gate 	    gettext("       renice [-n increment] [-g | -p | -u] ID ...\n"));
3577c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3587c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3597c478bd9Sstevel@tonic-gate 	    "[-p] pid ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3617c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3627c478bd9Sstevel@tonic-gate 	    " -g pgrp ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3637c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3647c478bd9Sstevel@tonic-gate 	    gettext("       renice priority "
3657c478bd9Sstevel@tonic-gate 	    " -u user ... [-g pgrp ...] [-p pid ...] [-u user ...]\n"));
3667c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
3677c478bd9Sstevel@tonic-gate 	    gettext("  where %d <= priority <= %d\n"), PRIO_MIN, PRIO_MAX);
3687c478bd9Sstevel@tonic-gate 	exit(2);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int
name2id(char * name)3727c478bd9Sstevel@tonic-gate name2id(char *name)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	type_t *type = types;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	while (type->name != NULL) {
3777c478bd9Sstevel@tonic-gate 		if (strcmp(type->name, name) == 0)
3787c478bd9Sstevel@tonic-gate 			return (type->id);
3797c478bd9Sstevel@tonic-gate 		type++;
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("renice: unknown id type: %s\n"), name);
3827c478bd9Sstevel@tonic-gate 	exit(1);
3837c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3847c478bd9Sstevel@tonic-gate }
385