renice.c revision 94355
11590Srgrimes/*
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 * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527917Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1983, 1989, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127917Scharnier#if 0
421590Srgrimesstatic char sccsid[] = "@(#)renice.c	8.1 (Berkeley) 6/9/93";
4327917Scharnier#endif
4427917Scharnierstatic const char rcsid[] =
4550477Speter  "$FreeBSD: head/usr.bin/renice/renice.c 94355 2002-04-10 11:09:46Z maxim $";
461590Srgrimes#endif /* not lint */
471590Srgrimes
481590Srgrimes#include <sys/types.h>
491590Srgrimes#include <sys/time.h>
501590Srgrimes#include <sys/resource.h>
511590Srgrimes
5227917Scharnier#include <err.h>
5359217Simp#include <errno.h>
5494355Smaxim#include <limits.h>
5594354Smaxim#include <pwd.h>
561590Srgrimes#include <stdio.h>
5727917Scharnier#include <stdlib.h>
5878718Sdd#include <string.h>
591590Srgrimes
6094355Smaximstatic int	donice(int, int, int, int);
6194355Smaximstatic int	getnum(const char *, const char *, int *);
6294354Smaximstatic void	usage(void);
6327917Scharnier
641590Srgrimes/*
651590Srgrimes * Change the priority (nice) of processes
661590Srgrimes * or groups of processes which are already
671590Srgrimes * running.
681590Srgrimes */
6927917Scharnierint
7094354Smaximmain(int argc, char *argv[])
711590Srgrimes{
7294354Smaxim	struct passwd *pwd;
7394355Smaxim	int errs, incr, prio, which, who;
741590Srgrimes
7594354Smaxim	errs = 0;
7694355Smaxim	incr = 0;
7794354Smaxim	which = PRIO_PROCESS;
7894354Smaxim	who = 0;
791590Srgrimes	argc--, argv++;
8027917Scharnier	if (argc < 2)
8127917Scharnier		usage();
8294355Smaxim	if (strcmp(*argv, "-n") == 0) {
8394355Smaxim		incr = 1;
8494355Smaxim		argc--, argv++;
8594355Smaxim		if (argc == 0)
8694355Smaxim			usage();
8794355Smaxim	}
8894355Smaxim	if (getnum("priority", *argv, &prio))
8994355Smaxim		return (1);
901590Srgrimes	prio = atoi(*argv);
911590Srgrimes	argc--, argv++;
921590Srgrimes	if (prio > PRIO_MAX)
931590Srgrimes		prio = PRIO_MAX;
941590Srgrimes	if (prio < PRIO_MIN)
951590Srgrimes		prio = PRIO_MIN;
961590Srgrimes	for (; argc > 0; argc--, argv++) {
971590Srgrimes		if (strcmp(*argv, "-g") == 0) {
981590Srgrimes			which = PRIO_PGRP;
991590Srgrimes			continue;
1001590Srgrimes		}
1011590Srgrimes		if (strcmp(*argv, "-u") == 0) {
1021590Srgrimes			which = PRIO_USER;
1031590Srgrimes			continue;
1041590Srgrimes		}
1051590Srgrimes		if (strcmp(*argv, "-p") == 0) {
1061590Srgrimes			which = PRIO_PROCESS;
1071590Srgrimes			continue;
1081590Srgrimes		}
1091590Srgrimes		if (which == PRIO_USER) {
11094354Smaxim			pwd = getpwnam(*argv);
1111590Srgrimes			if (pwd == NULL) {
11227917Scharnier				warnx("%s: unknown user", *argv);
1131590Srgrimes				continue;
1141590Srgrimes			}
1151590Srgrimes			who = pwd->pw_uid;
1161590Srgrimes		} else {
11794355Smaxim			if (getnum("pid", *argv, &who))
11894355Smaxim				continue;
1191590Srgrimes			if (who < 0) {
12027917Scharnier				warnx("%s: bad value", *argv);
1211590Srgrimes				continue;
1221590Srgrimes			}
1231590Srgrimes		}
12494355Smaxim		errs += donice(which, who, prio, incr);
1251590Srgrimes	}
1261590Srgrimes	exit(errs != 0);
1271590Srgrimes}
1281590Srgrimes
12994354Smaximstatic int
13094355Smaximdonice(int which, int who, int prio, int incr)
13127917Scharnier{
1321590Srgrimes	int oldprio;
1331590Srgrimes
13494354Smaxim	errno = 0;
13594354Smaxim	oldprio = getpriority(which, who);
1361590Srgrimes	if (oldprio == -1 && errno) {
13727917Scharnier		warn("%d: getpriority", who);
1381590Srgrimes		return (1);
1391590Srgrimes	}
14094355Smaxim	if (incr)
14194355Smaxim		prio = oldprio + prio;
14294355Smaxim	if (prio > PRIO_MAX)
14394355Smaxim		prio = PRIO_MAX;
14494355Smaxim	if (prio < PRIO_MIN)
14594355Smaxim		prio = PRIO_MIN;
1461590Srgrimes	if (setpriority(which, who, prio) < 0) {
14727917Scharnier		warn("%d: setpriority", who);
1481590Srgrimes		return (1);
1491590Srgrimes	}
1501590Srgrimes	printf("%d: old priority %d, new priority %d\n", who, 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;
16194355Smaxim	v = strtol(str, &ep, NULL);
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
17694354Smaximusage()
17794354Smaxim{
17894355Smaxim	fprintf(stderr, "%s\n%s\n",
17994355Smaxim"usage: renice [priority | [-n incr]] [ [ -p ] pids ] [ [ -g ] pgrps ]",
18094355Smaxim"              [ [ -u ] users ]");
18194354Smaxim	exit(1);
18294354Smaxim}
183