xref: /csrg-svn/usr.bin/renice/renice.c (revision 38991)
121162Sdist /*
2*38991Sbostic  * Copyright (c) 1983 The Regents of the University of California.
3*38991Sbostic  * All rights reserved.
4*38991Sbostic  *
5*38991Sbostic  * Redistribution and use in source and binary forms are permitted
6*38991Sbostic  * provided that the above copyright notice and this paragraph are
7*38991Sbostic  * duplicated in all such forms and that any documentation,
8*38991Sbostic  * advertising materials, and other materials related to such
9*38991Sbostic  * distribution and use acknowledge that the software was developed
10*38991Sbostic  * by the University of California, Berkeley.  The name of the
11*38991Sbostic  * University may not be used to endorse or promote products derived
12*38991Sbostic  * from this software without specific prior written permission.
13*38991Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*38991Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*38991Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621162Sdist  */
1721162Sdist 
1811614Ssam #ifndef lint
1921162Sdist char copyright[] =
20*38991Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
2121162Sdist  All rights reserved.\n";
22*38991Sbostic #endif /* not lint */
2311614Ssam 
2421162Sdist #ifndef lint
25*38991Sbostic static char sccsid[] = "@(#)renice.c	5.2 (Berkeley) 09/05/89";
26*38991Sbostic #endif /* not lint */
2721162Sdist 
2813606Ssam #include <sys/time.h>
2913606Ssam #include <sys/resource.h>
3011614Ssam #include <stdio.h>
3111614Ssam #include <pwd.h>
3211614Ssam 
3311614Ssam /*
3411614Ssam  * Change the priority (nice) of processes
3511614Ssam  * or groups of processes which are already
3611614Ssam  * running.
3711614Ssam  */
3811614Ssam main(argc, argv)
3911614Ssam 	char **argv;
4011614Ssam {
4111614Ssam 	int which = PRIO_PROCESS;
4211617Ssam 	int who = 0, prio, errs = 0;
4311614Ssam 
4411614Ssam 	argc--, argv++;
4514086Ssam 	if (argc < 2) {
4614088Ssam 		fprintf(stderr, "usage: renice priority [ [ -p ] pids ] ");
4714088Ssam 		fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n");
4814085Ssam 		exit(1);
4911614Ssam 	}
5011614Ssam 	prio = atoi(*argv);
5111614Ssam 	argc--, argv++;
5211614Ssam 	if (prio > PRIO_MAX)
5311614Ssam 		prio = PRIO_MAX;
5411614Ssam 	if (prio < PRIO_MIN)
5511614Ssam 		prio = PRIO_MIN;
5611614Ssam 	for (; argc > 0; argc--, argv++) {
5714085Ssam 		if (strcmp(*argv, "-g") == 0) {
5814085Ssam 			which = PRIO_PGRP;
5914085Ssam 			continue;
6014085Ssam 		}
6114085Ssam 		if (strcmp(*argv, "-u") == 0) {
6214085Ssam 			which = PRIO_USER;
6314085Ssam 			continue;
6414085Ssam 		}
6514085Ssam 		if (strcmp(*argv, "-p") == 0) {
6614085Ssam 			which = PRIO_PROCESS;
6714085Ssam 			continue;
6814085Ssam 		}
6911614Ssam 		if (which == PRIO_USER) {
7011614Ssam 			register struct passwd *pwd = getpwnam(*argv);
7111614Ssam 
7211614Ssam 			if (pwd == NULL) {
7311614Ssam 				fprintf(stderr, "renice: %s: unknown user\n",
7411614Ssam 					*argv);
7511614Ssam 				continue;
7611614Ssam 			}
7711617Ssam 			who = pwd->pw_uid;
7811614Ssam 		} else {
7911617Ssam 			who = atoi(*argv);
8011617Ssam 			if (who < 0) {
8111614Ssam 				fprintf(stderr, "renice: %s: bad value\n",
8211614Ssam 					*argv);
8311614Ssam 				continue;
8411614Ssam 			}
8511614Ssam 		}
8611617Ssam 		errs += donice(which, who, prio);
8711614Ssam 	}
8811614Ssam 	exit(errs != 0);
8911614Ssam }
9011614Ssam 
9111617Ssam donice(which, who, prio)
9211617Ssam 	int which, who, prio;
9311617Ssam {
9414085Ssam 	int oldprio;
9511617Ssam 	extern int errno;
9611617Ssam 
9714085Ssam 	errno = 0, oldprio = getpriority(which, who);
9811617Ssam 	if (oldprio == -1 && errno) {
9911617Ssam 		fprintf(stderr, "renice: %d: ", who);
10011617Ssam 		perror("getpriority");
10111617Ssam 		return (1);
10211617Ssam 	}
10311617Ssam 	if (setpriority(which, who, prio) < 0) {
10411617Ssam 		fprintf(stderr, "renice: %d: ", who);
10511617Ssam 		perror("setpriority");
10611617Ssam 		return (1);
10711617Ssam 	}
10811617Ssam 	printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
10911617Ssam 	return (0);
11011617Ssam }
111