Line data Source code
1 : /* nice -- run a program with modified niceness
2 : Copyright (C) 1990-2005, 2007 Free Software Foundation, Inc.
3 :
4 : This program is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation, either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : This program is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 :
17 : /* David MacKenzie <djm@gnu.ai.mit.edu> */
18 :
19 : #include <config.h>
20 : #include <stdio.h>
21 : #include <getopt.h>
22 : #include <sys/types.h>
23 :
24 : #include "system.h"
25 :
26 : #if ! HAVE_NICE
27 : /* Include this after "system.h" so we're sure to have definitions
28 : (from time.h or sys/time.h) required for e.g. the ru_utime member. */
29 : # include <sys/resource.h>
30 : #endif
31 :
32 : #include "error.h"
33 : #include "long-options.h"
34 : #include "quote.h"
35 : #include "xstrtol.h"
36 :
37 : /* The official name of this program (e.g., no `g' prefix). */
38 : #define PROGRAM_NAME "nice"
39 :
40 : #define AUTHORS "David MacKenzie"
41 :
42 : #if HAVE_NICE
43 : # define GET_NICENESS() nice (0)
44 : #else
45 : # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
46 : #endif
47 :
48 : #ifndef NZERO
49 : # define NZERO 20
50 : #endif
51 :
52 : /* This is required for Darwin Kernel Version 7.7.0. */
53 : #if NZERO == 0
54 : # undef NZERO
55 : # define NZERO 20
56 : #endif
57 :
58 : /* The name this program was run with. */
59 : char *program_name;
60 :
61 : static struct option const longopts[] =
62 : {
63 : {"adjustment", required_argument, NULL, 'n'},
64 : {NULL, 0, NULL, 0}
65 : };
66 :
67 : void
68 13 : usage (int status)
69 : {
70 13 : if (status != EXIT_SUCCESS)
71 11 : fprintf (stderr, _("Try `%s --help' for more information.\n"),
72 : program_name);
73 : else
74 : {
75 2 : printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
76 2 : printf (_("\
77 : Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
78 : With no COMMAND, print the current niceness. Nicenesses range from\n\
79 : %d (most favorable scheduling) to %d (least favorable).\n\
80 : \n\
81 : -n, --adjustment=N add integer N to the niceness (default 10)\n\
82 : "),
83 : - NZERO, NZERO - 1);
84 2 : fputs (HELP_OPTION_DESCRIPTION, stdout);
85 2 : fputs (VERSION_OPTION_DESCRIPTION, stdout);
86 2 : printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
87 2 : emit_bug_reporting_address ();
88 : }
89 13 : exit (status);
90 : }
91 :
92 : int
93 45 : main (int argc, char **argv)
94 : {
95 : int current_niceness;
96 45 : int adjustment = 10;
97 45 : char const *adjustment_given = NULL;
98 : bool ok;
99 : int i;
100 :
101 : initialize_main (&argc, &argv);
102 45 : program_name = argv[0];
103 45 : setlocale (LC_ALL, "");
104 : bindtextdomain (PACKAGE, LOCALEDIR);
105 : textdomain (PACKAGE);
106 :
107 45 : initialize_exit_failure (EXIT_FAILURE);
108 45 : atexit (close_stdout);
109 :
110 45 : parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
111 : usage, AUTHORS, (char const *) NULL);
112 :
113 115 : for (i = 1; i < argc; /* empty */)
114 : {
115 56 : char const *s = argv[i];
116 :
117 56 : if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))
118 : {
119 18 : adjustment_given = s + 1;
120 18 : ++i;
121 : }
122 : else
123 : {
124 : int optc;
125 38 : int fake_argc = argc - (i - 1);
126 38 : char **fake_argv = argv + (i - 1);
127 :
128 : /* Ensure that any getopt diagnostics use the right name. */
129 38 : fake_argv[0] = program_name;
130 :
131 : /* Initialize getopt_long's internal state. */
132 38 : optind = 0;
133 :
134 38 : optc = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);
135 38 : i += optind - 1;
136 :
137 38 : if (optc == '?')
138 4 : usage (EXIT_FAILURE);
139 34 : else if (optc == 'n')
140 13 : adjustment_given = optarg;
141 : else /* optc == -1 */
142 21 : break;
143 : }
144 : }
145 :
146 38 : if (adjustment_given)
147 : {
148 : /* If the requested adjustment is outside the valid range,
149 : silently bring it to just within range; this mimics what
150 : "setpriority" and "nice" do. */
151 : enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };
152 : long int tmp;
153 30 : if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
154 20 : error (EXIT_FAILURE, 0, _("invalid adjustment %s"),
155 : quote (adjustment_given));
156 10 : adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
157 : }
158 :
159 18 : if (i == argc)
160 : {
161 9 : if (adjustment_given)
162 : {
163 7 : error (0, 0, _("a command must be given with an adjustment"));
164 7 : usage (EXIT_FAILURE);
165 : }
166 : /* No command given; print the niceness. */
167 2 : errno = 0;
168 2 : current_niceness = GET_NICENESS ();
169 2 : if (current_niceness == -1 && errno != 0)
170 0 : error (EXIT_FAILURE, errno, _("cannot get niceness"));
171 2 : printf ("%d\n", current_niceness);
172 2 : exit (EXIT_SUCCESS);
173 : }
174 :
175 9 : errno = 0;
176 : #if HAVE_NICE
177 : ok = (nice (adjustment) != -1 || errno == 0);
178 : #else
179 9 : current_niceness = GET_NICENESS ();
180 9 : if (current_niceness == -1 && errno != 0)
181 0 : error (EXIT_FAILURE, errno, _("cannot get niceness"));
182 9 : ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);
183 : #endif
184 9 : if (!ok)
185 0 : error (errno == EPERM ? 0 : EXIT_FAILURE, errno, _("cannot set niceness"));
186 :
187 9 : execvp (argv[i], &argv[i]);
188 :
189 : {
190 9 : int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
191 9 : error (0, errno, "%s", argv[i]);
192 9 : exit (exit_status);
193 : }
194 : }
|