121162Sdist /*
2*63073Sbostic * Copyright (c) 1983, 1989, 1993
3*63073Sbostic * The Regents of the University of California. All rights reserved.
438991Sbostic *
542760Sbostic * %sccs.include.redist.c%
621162Sdist */
721162Sdist
811614Ssam #ifndef lint
9*63073Sbostic static char copyright[] =
10*63073Sbostic "@(#) Copyright (c) 1983, 1989, 1993\n\
11*63073Sbostic The Regents of the University of California. All rights reserved.\n";
1238991Sbostic #endif /* not lint */
1311614Ssam
1421162Sdist #ifndef lint
15*63073Sbostic static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 06/09/93";
1638991Sbostic #endif /* not lint */
1721162Sdist
1854039Sbostic #include <sys/types.h>
1913606Ssam #include <sys/time.h>
2013606Ssam #include <sys/resource.h>
2154039Sbostic
2211614Ssam #include <stdio.h>
2311614Ssam #include <pwd.h>
2411614Ssam
2511614Ssam /*
2611614Ssam * Change the priority (nice) of processes
2711614Ssam * or groups of processes which are already
2811614Ssam * running.
2911614Ssam */
main(argc,argv)3011614Ssam main(argc, argv)
3111614Ssam char **argv;
3211614Ssam {
3311614Ssam int which = PRIO_PROCESS;
3411617Ssam int who = 0, prio, errs = 0;
3511614Ssam
3611614Ssam argc--, argv++;
3714086Ssam if (argc < 2) {
3814088Ssam fprintf(stderr, "usage: renice priority [ [ -p ] pids ] ");
3914088Ssam fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n");
4014085Ssam exit(1);
4111614Ssam }
4211614Ssam prio = atoi(*argv);
4311614Ssam argc--, argv++;
4411614Ssam if (prio > PRIO_MAX)
4511614Ssam prio = PRIO_MAX;
4611614Ssam if (prio < PRIO_MIN)
4711614Ssam prio = PRIO_MIN;
4811614Ssam for (; argc > 0; argc--, argv++) {
4914085Ssam if (strcmp(*argv, "-g") == 0) {
5014085Ssam which = PRIO_PGRP;
5114085Ssam continue;
5214085Ssam }
5314085Ssam if (strcmp(*argv, "-u") == 0) {
5414085Ssam which = PRIO_USER;
5514085Ssam continue;
5614085Ssam }
5714085Ssam if (strcmp(*argv, "-p") == 0) {
5814085Ssam which = PRIO_PROCESS;
5914085Ssam continue;
6014085Ssam }
6111614Ssam if (which == PRIO_USER) {
6211614Ssam register struct passwd *pwd = getpwnam(*argv);
6311614Ssam
6411614Ssam if (pwd == NULL) {
6511614Ssam fprintf(stderr, "renice: %s: unknown user\n",
6611614Ssam *argv);
6711614Ssam continue;
6811614Ssam }
6911617Ssam who = pwd->pw_uid;
7011614Ssam } else {
7111617Ssam who = atoi(*argv);
7211617Ssam if (who < 0) {
7311614Ssam fprintf(stderr, "renice: %s: bad value\n",
7411614Ssam *argv);
7511614Ssam continue;
7611614Ssam }
7711614Ssam }
7811617Ssam errs += donice(which, who, prio);
7911614Ssam }
8011614Ssam exit(errs != 0);
8111614Ssam }
8211614Ssam
donice(which,who,prio)8311617Ssam donice(which, who, prio)
8411617Ssam int which, who, prio;
8511617Ssam {
8614085Ssam int oldprio;
8711617Ssam extern int errno;
8811617Ssam
8914085Ssam errno = 0, oldprio = getpriority(which, who);
9011617Ssam if (oldprio == -1 && errno) {
9111617Ssam fprintf(stderr, "renice: %d: ", who);
9211617Ssam perror("getpriority");
9311617Ssam return (1);
9411617Ssam }
9511617Ssam if (setpriority(which, who, prio) < 0) {
9611617Ssam fprintf(stderr, "renice: %d: ", who);
9711617Ssam perror("setpriority");
9811617Ssam return (1);
9911617Ssam }
10011617Ssam printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
10111617Ssam return (0);
10211617Ssam }
103