1 /* vi: set sw=4 ts=4: */
3 * nice implementation for busybox
5 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 #include <sys/resource.h>
19 static inline int int_add_no_wrap(int a, int b)
24 if (s > a) s = INT_MIN;
26 if (s < a) s = INT_MAX;
32 int nice_main(int argc, char **argv)
34 static const char Xetpriority_msg[] = "cannot %cet priority";
36 int old_priority, adjustment;
38 errno = 0; /* Needed for getpriority error detection. */
39 old_priority = getpriority(PRIO_PROCESS, 0);
41 bb_perror_msg_and_die(Xetpriority_msg, 'g');
44 if (!*++argv) { /* No args, so (GNU) output current nice value. */
45 bb_printf("%d\n", old_priority);
46 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
49 adjustment = 10; /* Set default adjustment. */
51 if ((argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { /* "-n" */
52 if (argc < 4) { /* Missing priority and/or utility! */
55 adjustment = xatoi(argv[1]);
59 { /* Set our priority. Handle integer wrapping for old + adjust. */
60 int new_priority = int_add_no_wrap(old_priority, adjustment);
62 if (setpriority(PRIO_PROCESS, 0, new_priority) < 0) {
63 bb_perror_msg_and_die(Xetpriority_msg, 's');
67 execvp(*argv, argv); /* Now exec the desired program. */
69 /* The exec failed... */
70 xfunc_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */
71 bb_perror_msg_and_die("%s", *argv);