xref: /csrg-svn/usr.bin/renice/renice.c (revision 21162)
1*21162Sdist /*
2*21162Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21162Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21162Sdist  * specifies the terms and conditions for redistribution.
5*21162Sdist  */
6*21162Sdist 
711614Ssam #ifndef lint
8*21162Sdist char copyright[] =
9*21162Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21162Sdist  All rights reserved.\n";
11*21162Sdist #endif not lint
1211614Ssam 
13*21162Sdist #ifndef lint
14*21162Sdist static char sccsid[] = "@(#)renice.c	5.1 (Berkeley) 05/28/85";
15*21162Sdist #endif not lint
16*21162Sdist 
1713606Ssam #include <sys/time.h>
1813606Ssam #include <sys/resource.h>
1911614Ssam #include <stdio.h>
2011614Ssam #include <pwd.h>
2111614Ssam 
2211614Ssam /*
2311614Ssam  * Change the priority (nice) of processes
2411614Ssam  * or groups of processes which are already
2511614Ssam  * running.
2611614Ssam  */
2711614Ssam main(argc, argv)
2811614Ssam 	char **argv;
2911614Ssam {
3011614Ssam 	int which = PRIO_PROCESS;
3111617Ssam 	int who = 0, prio, errs = 0;
3211614Ssam 
3311614Ssam 	argc--, argv++;
3414086Ssam 	if (argc < 2) {
3514088Ssam 		fprintf(stderr, "usage: renice priority [ [ -p ] pids ] ");
3614088Ssam 		fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n");
3714085Ssam 		exit(1);
3811614Ssam 	}
3911614Ssam 	prio = atoi(*argv);
4011614Ssam 	argc--, argv++;
4111614Ssam 	if (prio > PRIO_MAX)
4211614Ssam 		prio = PRIO_MAX;
4311614Ssam 	if (prio < PRIO_MIN)
4411614Ssam 		prio = PRIO_MIN;
4511614Ssam 	for (; argc > 0; argc--, argv++) {
4614085Ssam 		if (strcmp(*argv, "-g") == 0) {
4714085Ssam 			which = PRIO_PGRP;
4814085Ssam 			continue;
4914085Ssam 		}
5014085Ssam 		if (strcmp(*argv, "-u") == 0) {
5114085Ssam 			which = PRIO_USER;
5214085Ssam 			continue;
5314085Ssam 		}
5414085Ssam 		if (strcmp(*argv, "-p") == 0) {
5514085Ssam 			which = PRIO_PROCESS;
5614085Ssam 			continue;
5714085Ssam 		}
5811614Ssam 		if (which == PRIO_USER) {
5911614Ssam 			register struct passwd *pwd = getpwnam(*argv);
6011614Ssam 
6111614Ssam 			if (pwd == NULL) {
6211614Ssam 				fprintf(stderr, "renice: %s: unknown user\n",
6311614Ssam 					*argv);
6411614Ssam 				continue;
6511614Ssam 			}
6611617Ssam 			who = pwd->pw_uid;
6711614Ssam 		} else {
6811617Ssam 			who = atoi(*argv);
6911617Ssam 			if (who < 0) {
7011614Ssam 				fprintf(stderr, "renice: %s: bad value\n",
7111614Ssam 					*argv);
7211614Ssam 				continue;
7311614Ssam 			}
7411614Ssam 		}
7511617Ssam 		errs += donice(which, who, prio);
7611614Ssam 	}
7711614Ssam 	exit(errs != 0);
7811614Ssam }
7911614Ssam 
8011617Ssam donice(which, who, prio)
8111617Ssam 	int which, who, prio;
8211617Ssam {
8314085Ssam 	int oldprio;
8411617Ssam 	extern int errno;
8511617Ssam 
8614085Ssam 	errno = 0, oldprio = getpriority(which, who);
8711617Ssam 	if (oldprio == -1 && errno) {
8811617Ssam 		fprintf(stderr, "renice: %d: ", who);
8911617Ssam 		perror("getpriority");
9011617Ssam 		return (1);
9111617Ssam 	}
9211617Ssam 	if (setpriority(which, who, prio) < 0) {
9311617Ssam 		fprintf(stderr, "renice: %d: ", who);
9411617Ssam 		perror("setpriority");
9511617Ssam 		return (1);
9611617Ssam 	}
9711617Ssam 	printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
9811617Ssam 	return (0);
9911617Ssam }
100