|
Lines 32-49
Link Here
|
| 32 |
*/ |
32 |
*/ |
| 33 |
|
33 |
|
| 34 |
#ifndef lint |
34 |
#ifndef lint |
| 35 |
static const char copyright[] = |
35 |
static char copyright[] = |
| 36 |
"@(#) Copyright (c) 1983, 1989, 1993\n\ |
36 |
"@(#) Copyright (c) 1983, 1989, 1993\n\ |
| 37 |
The Regents of the University of California. All rights reserved.\n"; |
37 |
The Regents of the University of California. All rights reserved.\n"; |
| 38 |
#endif /* not lint */ |
38 |
#endif /* not lint */ |
| 39 |
|
39 |
|
| 40 |
#ifndef lint |
|
|
| 41 |
#if 0 |
40 |
#if 0 |
|
|
41 |
#ifndef lint |
| 42 |
static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 6/9/93"; |
42 |
static char sccsid[] = "@(#)renice.c 8.1 (Berkeley) 6/9/93"; |
| 43 |
#endif |
|
|
| 44 |
static const char rcsid[] = |
| 45 |
"$FreeBSD: src/usr.bin/renice/renice.c,v 1.8 2002/03/22 01:33:21 imp Exp $"; |
| 46 |
#endif /* not lint */ |
43 |
#endif /* not lint */ |
|
|
44 |
#endif |
| 45 |
|
| 46 |
#include <sys/cdefs.h> |
| 47 |
__FBSDID("$FreeBSD$"); |
| 47 |
|
48 |
|
| 48 |
#include <sys/types.h> |
49 |
#include <sys/types.h> |
| 49 |
#include <sys/time.h> |
50 |
#include <sys/time.h> |
|
Lines 51-62
Link Here
|
| 51 |
|
52 |
|
| 52 |
#include <err.h> |
53 |
#include <err.h> |
| 53 |
#include <errno.h> |
54 |
#include <errno.h> |
|
|
55 |
#include <limits.h> |
| 56 |
#include <pwd.h> |
| 54 |
#include <stdio.h> |
57 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> |
58 |
#include <stdlib.h> |
| 56 |
#include <string.h> |
59 |
#include <string.h> |
| 57 |
#include <pwd.h> |
60 |
#include <sysexits.h> |
| 58 |
|
61 |
|
| 59 |
int donice(int, int, int); |
62 |
static int donice(int, int, int, int); |
| 60 |
static void usage(void); |
63 |
static void usage(void); |
| 61 |
|
64 |
|
| 62 |
/* |
65 |
/* |
|
Lines 65-130
Link Here
|
| 65 |
* running. |
68 |
* running. |
| 66 |
*/ |
69 |
*/ |
| 67 |
int |
70 |
int |
| 68 |
main(argc, argv) |
71 |
main(int argc, char *argv[]) |
| 69 |
int argc; |
|
|
| 70 |
char **argv; |
| 71 |
{ |
72 |
{ |
| 72 |
int which = PRIO_PROCESS; |
73 |
struct passwd *pwd; |
| 73 |
int who = 0, prio, errs = 0; |
74 |
char *endptr; |
|
|
75 |
int errs, nflag, prio, which, who; |
| 76 |
|
| 77 |
which = PRIO_PROCESS; |
| 78 |
errs = 0, nflag = 0, who = 0; |
| 74 |
|
79 |
|
| 75 |
argc--, argv++; |
80 |
argc--, argv++; |
| 76 |
if (argc < 2) |
81 |
if (argc < 2) |
| 77 |
usage(); |
82 |
usage(); |
| 78 |
prio = atoi(*argv); |
83 |
|
|
|
84 |
/* Parse priority or -n increment. */ |
| 85 |
if (strcmp(*argv, "-n") == 0) { |
| 86 |
nflag = 1; |
| 87 |
argc--, argv++; |
| 88 |
if (argc < 2) |
| 89 |
usage(); |
| 90 |
} |
| 91 |
prio = strtol(*argv, &endptr, 10); |
| 92 |
if (*endptr || |
| 93 |
((prio == LONG_MAX || prio == LONG_MIN) && errno == ERANGE)) |
| 94 |
errx(EX_DATAERR, "Invalid input: %s", *argv); |
| 79 |
argc--, argv++; |
95 |
argc--, argv++; |
| 80 |
if (prio > PRIO_MAX) |
96 |
|
| 81 |
prio = PRIO_MAX; |
97 |
/* |
| 82 |
if (prio < PRIO_MIN) |
98 |
* Accept -g, -p, -u, or a number. If it's a number, default |
| 83 |
prio = PRIO_MIN; |
99 |
* to -p (PRIO_PROCESS). |
|
|
100 |
*/ |
| 101 |
if (strcmp(*argv, "-g") == 0) { |
| 102 |
which = PRIO_PGRP; |
| 103 |
argc--, argv++; |
| 104 |
} else if (strcmp(*argv, "-u") == 0) { |
| 105 |
which = PRIO_USER; |
| 106 |
argc--, argv++; |
| 107 |
} else if (strcmp(*argv, "-p") == 0) |
| 108 |
argc--, argv++; |
| 109 |
|
| 84 |
for (; argc > 0; argc--, argv++) { |
110 |
for (; argc > 0; argc--, argv++) { |
| 85 |
if (strcmp(*argv, "-g") == 0) { |
111 |
who = strtoul(*argv, &endptr, 10); |
| 86 |
which = PRIO_PGRP; |
112 |
|
| 87 |
continue; |
113 |
/* If argv is not a number, then we should be PRIO_USER. */ |
| 88 |
} |
114 |
if ((*endptr && which != PRIO_USER) || |
| 89 |
if (strcmp(*argv, "-u") == 0) { |
115 |
((who == LONG_MAX || who == LONG_MIN) && errno == ERANGE)) { |
| 90 |
which = PRIO_USER; |
116 |
warnx("Invalid input: %s", *argv); |
| 91 |
continue; |
|
|
| 92 |
} |
| 93 |
if (strcmp(*argv, "-p") == 0) { |
| 94 |
which = PRIO_PROCESS; |
| 95 |
continue; |
117 |
continue; |
| 96 |
} |
118 |
} |
| 97 |
if (which == PRIO_USER) { |
119 |
if (which == PRIO_USER && *endptr) { |
| 98 |
register struct passwd *pwd = getpwnam(*argv); |
120 |
pwd = getpwnam(*argv); |
| 99 |
|
121 |
|
| 100 |
if (pwd == NULL) { |
122 |
if (pwd == NULL) { |
| 101 |
warnx("%s: unknown user", *argv); |
123 |
warnx("%s: unknown user", *argv); |
| 102 |
continue; |
124 |
continue; |
| 103 |
} |
125 |
} |
| 104 |
who = pwd->pw_uid; |
126 |
who = pwd->pw_uid; |
| 105 |
} else { |
|
|
| 106 |
who = atoi(*argv); |
| 107 |
if (who < 0) { |
| 108 |
warnx("%s: bad value", *argv); |
| 109 |
continue; |
| 110 |
} |
| 111 |
} |
127 |
} |
| 112 |
errs += donice(which, who, prio); |
128 |
errs += donice(which, who, prio, nflag); |
| 113 |
} |
129 |
} |
| 114 |
exit(errs != 0); |
130 |
exit(errs); |
| 115 |
} |
131 |
} |
| 116 |
|
132 |
|
| 117 |
static void |
133 |
static void |
| 118 |
usage() |
134 |
usage(void) |
| 119 |
{ |
135 |
{ |
| 120 |
fprintf(stderr, |
136 |
|
| 121 |
"usage: renice priority [ [ -p ] pids ] [ [ -g ] pgrps ] [ [ -u ] users ]\n"); |
137 |
fprintf(stderr, "usage: renice nice_value [-g | -p | -u] ID ...\n"); |
| 122 |
exit(1); |
138 |
fprintf(stderr, " renice -n increment [-g | -p | -u] ID ...\n"); |
|
|
139 |
exit(EX_USAGE); |
| 123 |
} |
140 |
} |
| 124 |
|
141 |
|
| 125 |
int |
142 |
static int |
| 126 |
donice(which, who, prio) |
143 |
donice(int which, int who, int prio, int nflag) |
| 127 |
int which, who, prio; |
|
|
| 128 |
{ |
144 |
{ |
| 129 |
int oldprio; |
145 |
int oldprio; |
| 130 |
|
146 |
|
|
Lines 133-142
Link Here
|
| 133 |
warn("%d: getpriority", who); |
149 |
warn("%d: getpriority", who); |
| 134 |
return (1); |
150 |
return (1); |
| 135 |
} |
151 |
} |
|
|
152 |
if (nflag) |
| 153 |
prio += oldprio; /* Possible over/underflow here. */ |
| 136 |
if (setpriority(which, who, prio) < 0) { |
154 |
if (setpriority(which, who, prio) < 0) { |
| 137 |
warn("%d: setpriority", who); |
155 |
warn("%d: setpriority", who); |
| 138 |
return (1); |
156 |
return (1); |
| 139 |
} |
157 |
} |
| 140 |
printf("%d: old priority %d, new priority %d\n", who, oldprio, prio); |
158 |
printf("%d: old nice_value %d, new nice_value %d\n", who, oldprio, prio); |
| 141 |
return (0); |
159 |
return (0); |
| 142 |
} |
160 |
} |