Annotation of src/usr.bin/renice/renice.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: renice.c,v 1.3 1997/06/20 10:02:31 deraadt Exp $ */
1.2 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1983 The Regents of the University of California.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: char copyright[] =
38: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
39: All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: /*static char sccsid[] = "from: @(#)renice.c 5.3 (Berkeley) 6/1/90";*/
1.4 ! deraadt 44: static char rcsid[] = "$OpenBSD: renice.c,v 1.3 1997/06/20 10:02:31 deraadt Exp $";
1.1 deraadt 45: #endif /* not lint */
46:
47: #include <sys/types.h>
48: #include <sys/time.h>
49: #include <sys/resource.h>
50: #include <stdio.h>
1.3 deraadt 51: #include <stdlib.h>
1.1 deraadt 52: #include <pwd.h>
53:
1.3 deraadt 54: int donice __P((int, int, int));
55:
1.1 deraadt 56: /*
57: * Change the priority (nice) of processes
58: * or groups of processes which are already
59: * running.
60: */
1.3 deraadt 61: int
1.1 deraadt 62: main(argc, argv)
63: char **argv;
64: {
65: int which = PRIO_PROCESS;
66: int who = 0, prio, errs = 0;
67:
68: argc--, argv++;
69: if (argc < 2) {
1.4 ! deraadt 70: fprintf(stderr, "usage: renice priority [[-p] pid ...] ");
! 71: fprintf(stderr, "[[-g] pgrp ...] [[-u] user ...]\n");
1.1 deraadt 72: exit(1);
73: }
74: prio = atoi(*argv);
75: argc--, argv++;
76: if (prio > PRIO_MAX)
77: prio = PRIO_MAX;
78: if (prio < PRIO_MIN)
79: prio = PRIO_MIN;
80: for (; argc > 0; argc--, argv++) {
81: if (strcmp(*argv, "-g") == 0) {
82: which = PRIO_PGRP;
83: continue;
84: }
85: if (strcmp(*argv, "-u") == 0) {
86: which = PRIO_USER;
87: continue;
88: }
89: if (strcmp(*argv, "-p") == 0) {
90: which = PRIO_PROCESS;
91: continue;
92: }
93: if (which == PRIO_USER) {
94: register struct passwd *pwd = getpwnam(*argv);
95:
96: if (pwd == NULL) {
97: fprintf(stderr, "renice: %s: unknown user\n",
98: *argv);
99: continue;
100: }
101: who = pwd->pw_uid;
102: } else {
103: who = atoi(*argv);
104: if (who < 0) {
105: fprintf(stderr, "renice: %s: bad value\n",
106: *argv);
107: continue;
108: }
109: }
110: errs += donice(which, who, prio);
111: }
112: exit(errs != 0);
113: }
114:
1.3 deraadt 115: int
1.1 deraadt 116: donice(which, who, prio)
117: int which, who, prio;
118: {
119: int oldprio;
120: extern int errno;
121:
122: errno = 0, oldprio = getpriority(which, who);
123: if (oldprio == -1 && errno) {
124: fprintf(stderr, "renice: %d: ", who);
125: perror("getpriority");
126: return (1);
127: }
128: if (setpriority(which, who, prio) < 0) {
129: fprintf(stderr, "renice: %d: ", who);
130: perror("setpriority");
131: return (1);
132: }
133: printf("%d: old priority %d, new priority %d\n", who, oldprio, prio);
134: return (0);
135: }