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