1 /* nice -- run a program with modified niceness
2 Copyright (C) 1990-2012 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* David MacKenzie <djm@gnu.ai.mit.edu> */
22 #include <sys/types.h>
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>
36 /* The official name of this program (e.g., no 'g' prefix). */
37 #define PROGRAM_NAME "nice"
39 #define AUTHORS proper_name ("David MacKenzie")
42 # define GET_NICENESS() nice (0)
44 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
51 /* This is required for Darwin Kernel Version 7.7.0. */
57 static struct option
const longopts
[] =
59 {"adjustment", required_argument
, NULL
, 'n'},
60 {GETOPT_HELP_OPTION_DECL
},
61 {GETOPT_VERSION_OPTION_DECL
},
68 if (status
!= EXIT_SUCCESS
)
72 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name
);
74 Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
75 With no COMMAND, print the current niceness. Niceness values range from\n\
76 %d (most favorable to the process) to %d (least favorable to the process).\n\
78 -n, --adjustment=N add integer N to the niceness (default 10)\n\
81 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
82 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
83 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
84 emit_ancillary_info ();
90 perm_related_errno (int err
)
92 return err
== EACCES
|| err
== EPERM
;
96 main (int argc
, char **argv
)
100 char const *adjustment_given
= NULL
;
104 initialize_main (&argc
, &argv
);
105 set_program_name (argv
[0]);
106 setlocale (LC_ALL
, "");
107 bindtextdomain (PACKAGE
, LOCALEDIR
);
108 textdomain (PACKAGE
);
110 initialize_exit_failure (EXIT_CANCELED
);
111 atexit (close_stdout
);
113 for (i
= 1; i
< argc
; /* empty */)
115 char const *s
= argv
[i
];
117 if (s
[0] == '-' && ISDIGIT (s
[1 + (s
[1] == '-' || s
[1] == '+')]))
119 adjustment_given
= s
+ 1;
125 int fake_argc
= argc
- (i
- 1);
126 char **fake_argv
= argv
+ (i
- 1);
128 /* Ensure that any getopt diagnostics use the right name. */
129 fake_argv
[0] = argv
[0];
131 /* Initialize getopt_long's internal state. */
134 c
= getopt_long (fake_argc
, fake_argv
, "+n:", longopts
, NULL
);
140 adjustment_given
= optarg
;
146 case_GETOPT_HELP_CHAR
;
148 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
151 usage (EXIT_CANCELED
);
160 if (adjustment_given
)
162 /* If the requested adjustment is outside the valid range,
163 silently bring it to just within range; this mimics what
164 "setpriority" and "nice" do. */
165 enum { MIN_ADJUSTMENT
= 1 - 2 * NZERO
, MAX_ADJUSTMENT
= 2 * NZERO
- 1 };
167 if (LONGINT_OVERFLOW
< xstrtol (adjustment_given
, NULL
, 10, &tmp
, ""))
168 error (EXIT_CANCELED
, 0, _("invalid adjustment %s"),
169 quote (adjustment_given
));
170 adjustment
= MAX (MIN_ADJUSTMENT
, MIN (tmp
, MAX_ADJUSTMENT
));
175 if (adjustment_given
)
177 error (0, 0, _("a command must be given with an adjustment"));
178 usage (EXIT_CANCELED
);
180 /* No command given; print the niceness. */
182 current_niceness
= GET_NICENESS ();
183 if (current_niceness
== -1 && errno
!= 0)
184 error (EXIT_CANCELED
, errno
, _("cannot get niceness"));
185 printf ("%d\n", current_niceness
);
191 ok
= (nice (adjustment
) != -1 || errno
== 0);
193 current_niceness
= GET_NICENESS ();
194 if (current_niceness
== -1 && errno
!= 0)
195 error (EXIT_CANCELED
, errno
, _("cannot get niceness"));
196 ok
= (setpriority (PRIO_PROCESS
, 0, current_niceness
+ adjustment
) == 0);
200 error (perm_related_errno (errno
) ? 0
201 : EXIT_CANCELED
, errno
, _("cannot set niceness"));
202 /* error() flushes stderr, but does not check for write failure.
203 Normally, we would catch this via our atexit() hook of
204 close_stdout, but execvp() gets in the way. If stderr
205 encountered a write failure, there is no need to try calling
208 exit (EXIT_CANCELED
);
211 execvp (argv
[i
], &argv
[i
]);
214 int exit_status
= (errno
== ENOENT
? EXIT_ENOENT
: EXIT_CANNOT_INVOKE
);
215 error (0, errno
, "%s", argv
[i
]);