]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1983, 1989, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
b50945d4 | 34 | /* 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL> |
7eda085c KZ |
35 | * - added Native Language Support |
36 | */ | |
37 | ||
6dbe3af9 KZ |
38 | #include <sys/types.h> |
39 | #include <sys/time.h> | |
40 | #include <sys/resource.h> | |
41 | ||
42 | #include <stdio.h> | |
43 | #include <pwd.h> | |
fd6b7a7f | 44 | #include <stdlib.h> |
66ee8158 | 45 | #include <string.h> |
7eda085c KZ |
46 | #include <errno.h> |
47 | #include "nls.h" | |
eb76ca98 | 48 | #include "c.h" |
efb8854f | 49 | #include "closestream.h" |
fd6b7a7f | 50 | |
abafd686 | 51 | static void __attribute__((__noreturn__)) usage(FILE *out) |
628cab7c | 52 | { |
ae1ae49e KZ |
53 | fputs(_("\nUsage:\n"), out); |
54 | fprintf(out, | |
ae3ca2aa BS |
55 | _(" %1$s [-n] <priority> [-p|--pid] <pid>...\n" |
56 | " %1$s [-n] <priority> -g|--pgrp <pgid>...\n" | |
57 | " %1$s [-n] <priority> -u|--user <user>...\n"), | |
296351b0 KZ |
58 | program_invocation_short_name); |
59 | ||
ae1ae49e | 60 | fputs(_("\nOptions:\n"), out); |
ae3ca2aa BS |
61 | fputs(_(" -g, --pgrp <id> interpret argument as process group ID\n" |
62 | " -n, --priority <num> specify the nice increment value\n" | |
63 | " -p, --pid <id> interpret argument as process ID (default)\n" | |
64 | " -u, --user <name|id> interpret argument as username or user ID\n" | |
65 | " -h, --help display help text and exit\n" | |
5cc224f5 | 66 | " -V, --version display version information and exit\n"), out); |
296351b0 | 67 | |
ae1ae49e | 68 | fputs(_("\nFor more information see renice(1).\n"), out); |
296351b0 KZ |
69 | |
70 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
628cab7c | 71 | } |
7cfbafda | 72 | |
5ebcab66 SK |
73 | static int |
74 | donice(int which, int who, int prio) { | |
75 | int oldprio, newprio; | |
76 | const char *idtype = _("process ID"); | |
77 | ||
78 | if (which == PRIO_USER) | |
79 | idtype = _("user ID"); | |
80 | else if (which == PRIO_PGRP) | |
81 | idtype = _("process group ID"); | |
82 | ||
83 | errno = 0; | |
84 | oldprio = getpriority(which, who); | |
85 | if (oldprio == -1 && errno) { | |
86 | warn(_("failed to get priority for %d (%s)"), who, idtype); | |
87 | return 1; | |
88 | } | |
89 | if (setpriority(which, who, prio) < 0) { | |
90 | warn(_("failed to set priority for %d (%s)"), who, idtype); | |
91 | return 1; | |
92 | } | |
93 | errno = 0; | |
94 | newprio = getpriority(which, who); | |
95 | if (newprio == -1 && errno) { | |
96 | warn(_("failed to get priority for %d (%s)"), who, idtype); | |
97 | return 1; | |
98 | } | |
99 | ||
100 | printf(_("%d (%s) old priority %d, new priority %d\n"), | |
101 | who, idtype, oldprio, newprio); | |
102 | return 0; | |
103 | } | |
104 | ||
6dbe3af9 | 105 | /* |
5cc224f5 BS |
106 | * Change the priority (the nice value) of processes |
107 | * or groups of processes which are already running. | |
6dbe3af9 | 108 | */ |
2b6fc908 KZ |
109 | int |
110 | main(int argc, char **argv) | |
6dbe3af9 KZ |
111 | { |
112 | int which = PRIO_PROCESS; | |
113 | int who = 0, prio, errs = 0; | |
628cab7c | 114 | char *endptr = NULL; |
6dbe3af9 | 115 | |
7eda085c KZ |
116 | setlocale(LC_ALL, ""); |
117 | bindtextdomain(PACKAGE, LOCALEDIR); | |
118 | textdomain(PACKAGE); | |
efb8854f | 119 | atexit(close_stdout); |
7eda085c | 120 | |
628cab7c LJ |
121 | argc--; |
122 | argv++; | |
123 | ||
124 | if (argc == 1) { | |
125 | if (strcmp(*argv, "-h") == 0 || | |
126 | strcmp(*argv, "--help") == 0) | |
296351b0 | 127 | usage(stdout); |
628cab7c LJ |
128 | |
129 | if (strcmp(*argv, "-v") == 0 || | |
5cc224f5 | 130 | strcmp(*argv, "-V") == 0 || |
628cab7c | 131 | strcmp(*argv, "--version") == 0) { |
5cc224f5 BS |
132 | printf(_("%s from %s\n"), |
133 | program_invocation_short_name, PACKAGE_STRING); | |
628cab7c LJ |
134 | exit(EXIT_SUCCESS); |
135 | } | |
6dbe3af9 | 136 | } |
628cab7c LJ |
137 | |
138 | if (argc < 2) | |
296351b0 | 139 | usage(stderr); |
628cab7c | 140 | |
7cfbafda KZ |
141 | if (strcmp(*argv, "-n") == 0 || strcmp(*argv, "--priority") == 0) { |
142 | argc--; | |
143 | argv++; | |
144 | } | |
145 | ||
628cab7c LJ |
146 | prio = strtol(*argv, &endptr, 10); |
147 | if (*endptr) | |
296351b0 | 148 | usage(stderr); |
628cab7c LJ |
149 | |
150 | argc--; | |
151 | argv++; | |
152 | ||
6dbe3af9 | 153 | for (; argc > 0; argc--, argv++) { |
628cab7c | 154 | if (strcmp(*argv, "-g") == 0 || strcmp(*argv, "--pgrp") == 0) { |
6dbe3af9 KZ |
155 | which = PRIO_PGRP; |
156 | continue; | |
157 | } | |
628cab7c | 158 | if (strcmp(*argv, "-u") == 0 || strcmp(*argv, "--user") == 0) { |
6dbe3af9 KZ |
159 | which = PRIO_USER; |
160 | continue; | |
161 | } | |
628cab7c | 162 | if (strcmp(*argv, "-p") == 0 || strcmp(*argv, "--pid") == 0) { |
6dbe3af9 KZ |
163 | which = PRIO_PROCESS; |
164 | continue; | |
165 | } | |
166 | if (which == PRIO_USER) { | |
167 | register struct passwd *pwd = getpwnam(*argv); | |
628cab7c | 168 | |
6dbe3af9 | 169 | if (pwd == NULL) { |
40cebc2d | 170 | warnx(_("unknown user %s"), *argv); |
665b3c85 | 171 | errs = 1; |
6dbe3af9 KZ |
172 | continue; |
173 | } | |
174 | who = pwd->pw_uid; | |
175 | } else { | |
628cab7c LJ |
176 | who = strtol(*argv, &endptr, 10); |
177 | if (who < 0 || *endptr) { | |
40cebc2d | 178 | warnx(_("bad value %s"), *argv); |
665b3c85 | 179 | errs = 1; |
6dbe3af9 KZ |
180 | continue; |
181 | } | |
182 | } | |
665b3c85 | 183 | errs |= donice(which, who, prio); |
6dbe3af9 | 184 | } |
628cab7c | 185 | return errs != 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
6dbe3af9 KZ |
186 | } |
187 |