nice.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * nice implementation for busybox
  4. *
  5. * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config NICE
  10. //config: bool "nice (1.8 kb)"
  11. //config: default y
  12. //config: help
  13. //config: nice runs a program with modified scheduling priority.
  14. //applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice))
  15. //kbuild:lib-$(CONFIG_NICE) += nice.o
  16. //usage:#define nice_trivial_usage
  17. //usage: "[-n ADJUST] [PROG ARGS]"
  18. //usage:#define nice_full_usage "\n\n"
  19. //usage: "Change scheduling priority, run PROG\n"
  20. //usage: "\n -n ADJUST Adjust priority by ADJUST"
  21. #include "libbb.h"
  22. int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int nice_main(int argc UNUSED_PARAM, char **argv)
  24. {
  25. int old_priority, adjustment;
  26. old_priority = getpriority(PRIO_PROCESS, 0);
  27. if (!*++argv) { /* No args, so (GNU) output current nice value. */
  28. printf("%d\n", old_priority);
  29. fflush_stdout_and_exit(EXIT_SUCCESS);
  30. }
  31. adjustment = 10; /* Set default adjustment. */
  32. if (argv[0][0] == '-') {
  33. char *nnn = argv[0] + 1;
  34. if (nnn[0] == 'n') { /* -n */
  35. nnn += 1;
  36. if (!nnn[0]) { /* "-n NNN" */
  37. nnn = *++argv;
  38. }
  39. /* else: "-nNNN" (w/o space) */
  40. }
  41. /* else: "-NNN" (NNN may be negative) - same as "-n NNN" */
  42. if (!nnn || !argv[1]) { /* Missing priority or PROG! */
  43. bb_show_usage();
  44. }
  45. adjustment = xatoi_range(nnn, INT_MIN/2, INT_MAX/2);
  46. argv++;
  47. }
  48. { /* Set our priority. */
  49. int prio = old_priority + adjustment;
  50. if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
  51. bb_perror_msg_and_die("setpriority(%d)", prio);
  52. }
  53. }
  54. BB_EXECVP_or_die(argv);
  55. }