static __inline__ int __syscall_nice(int incr) { int old_priority; # if 1 /* This should never fail. */ old_priority = getpriority(PRIO_PROCESS, 0); # else /* But if you want to be paranoid... */ int old_errno; old_errno = errno; __set_errno(0); old_priority = getpriority(PRIO_PROCESS, 0); if ((old_priority == -1) && errno) { return -1; } __set_errno(old_errno); # endif if (setpriority(PRIO_PROCESS, 0, int_add_no_wrap(old_priority, incr))) { __set_errno(EPERM); /* SUSv3 mandates EPERM for nice failure. */ return -1; } return 0; }
int nice_main(int argc, char **argv) { static const char Xetpriority_msg[] = "cannot %cet priority"; int old_priority, adjustment; errno = 0; /* Needed for getpriority error detection. */ old_priority = getpriority(PRIO_PROCESS, 0); if (errno) { bb_perror_msg_and_die(Xetpriority_msg, 'g'); } if (!*++argv) { /* No args, so (GNU) output current nice value. */ bb_printf("%d\n", old_priority); bb_fflush_stdout_and_exit(EXIT_SUCCESS); } adjustment = 10; /* Set default adjustment. */ if ((argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { /* "-n" */ if (argc < 4) { /* Missing priority and/or utility! */ bb_show_usage(); } adjustment = bb_xgetlarg(argv[1], 10, INT_MIN, INT_MAX); argv += 2; } { /* Set our priority. Handle integer wrapping for old + adjust. */ int new_priority = int_add_no_wrap(old_priority, adjustment); if (setpriority(PRIO_PROCESS, 0, new_priority) < 0) { bb_perror_msg_and_die(Xetpriority_msg, 's'); } } execvp(*argv, argv); /* Now exec the desired program. */ /* The exec failed... */ bb_default_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */ bb_perror_msg_and_die("%s", *argv); }
int renice_main(int argc, char **argv) { static const char Xetpriority_msg[] = "%d : %cetpriority"; int retval = EXIT_SUCCESS; int which = PRIO_PROCESS; /* Default 'which' value. */ int use_relative = 0; int adjustment, new_priority; id_t who; ++argv; /* Check if we are using a relative adjustment. */ if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) { use_relative = 1; ++argv; } if (!*argv) { /* No args? Then show usage. */ bb_show_usage(); } /* Get the priority adjustment (absolute or relative). */ adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX); while (*++argv) { /* Check for a mode switch. */ if ((argv[0][0] == '-') && argv[0][1] && !argv[0][2]) { static const char opts[] = { 'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER }; const char *p; if ((p = strchr(opts, argv[0][1]))) { which = p[4]; continue; } } /* Process an ID arg. */ if (which == PRIO_USER) { struct passwd *p; if (!(p = getpwnam(*argv))) { bb_error_msg("unknown user: %s", *argv); goto HAD_ERROR; } who = p->pw_uid; } else { char *e; errno = 0; who = strtoul(*argv, &e, 10); if (*e || (*argv == e) || errno) { bb_error_msg("bad value: %s", *argv); goto HAD_ERROR; } } /* Get priority to use, and set it. */ if (use_relative) { int old_priority; errno = 0; /* Needed for getpriority error detection. */ old_priority = getpriority(which, who); if (errno) { bb_perror_msg(Xetpriority_msg, who, 'g'); goto HAD_ERROR; } new_priority = int_add_no_wrap(old_priority, adjustment); } else { new_priority = adjustment; } if (setpriority(which, who, new_priority) == 0) { continue; } bb_perror_msg(Xetpriority_msg, who, 's'); HAD_ERROR: retval = EXIT_FAILURE; } /* No need to check for errors outputing to stderr since, if it * was used, the HAD_ERROR label was reached and retval was set. */ return retval; }