1331722Seadler/*
21590Srgrimes * Copyright (c) 1983, 1989, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3127917Scharnierstatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1983, 1989, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
36111569Scharnier#if 0
371590Srgrimes#ifndef lint
381590Srgrimesstatic char sccsid[] = "@(#)renice.c	8.1 (Berkeley) 6/9/93";
39111569Scharnier#endif /* not lint */
4027917Scharnier#endif
411590Srgrimes
42111569Scharnier#include <sys/cdefs.h>
43111569Scharnier__FBSDID("$FreeBSD$");
44111569Scharnier
451590Srgrimes#include <sys/types.h>
461590Srgrimes#include <sys/time.h>
471590Srgrimes#include <sys/resource.h>
481590Srgrimes
4927917Scharnier#include <err.h>
5059217Simp#include <errno.h>
5194355Smaxim#include <limits.h>
5294354Smaxim#include <pwd.h>
531590Srgrimes#include <stdio.h>
5427917Scharnier#include <stdlib.h>
5578718Sdd#include <string.h>
561590Srgrimes
5794355Smaximstatic int	donice(int, int, int, int);
5894355Smaximstatic int	getnum(const char *, const char *, int *);
5994354Smaximstatic void	usage(void);
6027917Scharnier
611590Srgrimes/*
621590Srgrimes * Change the priority (nice) of processes
631590Srgrimes * or groups of processes which are already
641590Srgrimes * running.
651590Srgrimes */
6627917Scharnierint
6794354Smaximmain(int argc, char *argv[])
681590Srgrimes{
6994354Smaxim	struct passwd *pwd;
7094355Smaxim	int errs, incr, prio, which, who;
711590Srgrimes
7294354Smaxim	errs = 0;
7394355Smaxim	incr = 0;
7494354Smaxim	which = PRIO_PROCESS;
7594354Smaxim	who = 0;
761590Srgrimes	argc--, argv++;
7727917Scharnier	if (argc < 2)
7827917Scharnier		usage();
7994355Smaxim	if (strcmp(*argv, "-n") == 0) {
8094355Smaxim		incr = 1;
8194355Smaxim		argc--, argv++;
8294360Smaxim		if (argc < 2)
8394355Smaxim			usage();
8494355Smaxim	}
8594355Smaxim	if (getnum("priority", *argv, &prio))
8694355Smaxim		return (1);
871590Srgrimes	argc--, argv++;
881590Srgrimes	for (; argc > 0; argc--, argv++) {
891590Srgrimes		if (strcmp(*argv, "-g") == 0) {
901590Srgrimes			which = PRIO_PGRP;
911590Srgrimes			continue;
921590Srgrimes		}
931590Srgrimes		if (strcmp(*argv, "-u") == 0) {
941590Srgrimes			which = PRIO_USER;
951590Srgrimes			continue;
961590Srgrimes		}
971590Srgrimes		if (strcmp(*argv, "-p") == 0) {
981590Srgrimes			which = PRIO_PROCESS;
991590Srgrimes			continue;
1001590Srgrimes		}
1011590Srgrimes		if (which == PRIO_USER) {
10296763Stjr			if ((pwd = getpwnam(*argv)) != NULL)
10396763Stjr				who = pwd->pw_uid;
10497270Stjr			else if (getnum("uid", *argv, &who)) {
10597270Stjr				errs++;
1061590Srgrimes				continue;
10797270Stjr			} else if (who < 0) {
10896763Stjr				warnx("%s: bad value", *argv);
10997270Stjr				errs++;
11096763Stjr				continue;
1111590Srgrimes			}
1121590Srgrimes		} else {
11397270Stjr			if (getnum("pid", *argv, &who)) {
11497270Stjr				errs++;
11594355Smaxim				continue;
11697270Stjr			}
1171590Srgrimes			if (who < 0) {
11827917Scharnier				warnx("%s: bad value", *argv);
11997270Stjr				errs++;
1201590Srgrimes				continue;
1211590Srgrimes			}
1221590Srgrimes		}
12394355Smaxim		errs += donice(which, who, prio, incr);
1241590Srgrimes	}
1251590Srgrimes	exit(errs != 0);
1261590Srgrimes}
1271590Srgrimes
12894354Smaximstatic int
12994355Smaximdonice(int which, int who, int prio, int incr)
13027917Scharnier{
1311590Srgrimes	int oldprio;
1321590Srgrimes
13394354Smaxim	errno = 0;
13494354Smaxim	oldprio = getpriority(which, who);
1351590Srgrimes	if (oldprio == -1 && errno) {
13627917Scharnier		warn("%d: getpriority", who);
1371590Srgrimes		return (1);
1381590Srgrimes	}
13994355Smaxim	if (incr)
14094355Smaxim		prio = oldprio + prio;
14194355Smaxim	if (prio > PRIO_MAX)
14294355Smaxim		prio = PRIO_MAX;
14394355Smaxim	if (prio < PRIO_MIN)
14494355Smaxim		prio = PRIO_MIN;
1451590Srgrimes	if (setpriority(which, who, prio) < 0) {
14627917Scharnier		warn("%d: setpriority", who);
1471590Srgrimes		return (1);
1481590Srgrimes	}
14996761Stjr	fprintf(stderr, "%d: old priority %d, new priority %d\n", who,
15096761Stjr	    oldprio, prio);
1511590Srgrimes	return (0);
1521590Srgrimes}
15394354Smaxim
15494355Smaximstatic int
15594355Smaximgetnum(const char *com, const char *str, int *val)
15694355Smaxim{
15794355Smaxim	long v;
15894355Smaxim	char *ep;
15994355Smaxim
16094355Smaxim	errno = 0;
16196764Stjr	v = strtol(str, &ep, 10);
16294355Smaxim	if (v < INT_MIN || v > INT_MAX || errno == ERANGE) {
16394355Smaxim		warnx("%s argument %s is out of range.", com, str);
16494355Smaxim		return (1);
16594355Smaxim	}
16694355Smaxim	if (ep == str || *ep != '\0' || errno != 0) {
16794355Smaxim		warnx("Bad %s argument: %s.", com, str);
16894355Smaxim		return (1);
16994355Smaxim	}
17094355Smaxim
17194355Smaxim	*val = (int)v;
17294355Smaxim	return (0);
17394355Smaxim}
17494355Smaxim
17594354Smaximstatic void
176201382Sedusage(void)
17794354Smaxim{
17894355Smaxim	fprintf(stderr, "%s\n%s\n",
179146466Sru"usage: renice priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]",
180111569Scharnier"       renice -n increment [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...]");
18194354Smaxim	exit(1);
18294354Smaxim}
183