Bump to version 1.22.1
[platform/upstream/busybox.git] / procps / renice.c
index 711ed16..77f400a 100644 (file)
@@ -4,20 +4,7 @@
  *
  * Copyright (C) 2005  Manuel Novoa III  <mjn3@codepoet.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 /* Notes:
  *   following IDs (if any).  Multiple switches are allowed.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/resource.h>
-#include "busybox.h"
-
-#if (PRIO_PROCESS < CHAR_MIN) || (PRIO_PROCESS > CHAR_MAX)
-#error Assumption violated : PRIO_PROCESS value
-#endif
-#if (PRIO_PGRP < CHAR_MIN) || (PRIO_PGRP > CHAR_MAX)
-#error Assumption violated : PRIO_PGRP value
-#endif
-#if (PRIO_USER < CHAR_MIN) || (PRIO_USER > CHAR_MAX)
-#error Assumption violated : PRIO_USER value
-#endif
-
-static inline int int_add_no_wrap(int a, int b)
-{
-       int s = a + b;
+//usage:#define renice_trivial_usage
+//usage:       "{{-n INCREMENT} | PRIORITY} [[-p | -g | -u] ID...]"
+//usage:#define renice_full_usage "\n\n"
+//usage:       "Change scheduling priority for a running process\n"
+//usage:     "\n       -n      Adjust current nice value (smaller is faster)"
+//usage:     "\n       -p      Process id(s) (default)"
+//usage:     "\n       -g      Process group id(s)"
+//usage:     "\n       -u      Process user name(s) and/or id(s)"
 
-       if (b < 0) {
-               if (s > a) s = INT_MIN;
-       } else {
-               if (s < a) s = INT_MAX;
-       }
+#include "libbb.h"
+#include <sys/resource.h>
 
-       return s;
-}
+void BUG_bad_PRIO_PROCESS(void);
+void BUG_bad_PRIO_PGRP(void);
+void BUG_bad_PRIO_USER(void);
 
-int renice_main(int argc, char **argv)
+int renice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int renice_main(int argc UNUSED_PARAM, char **argv)
 {
-       static const char Xetpriority_msg[] = "%d : %cetpriority";
+       static const char Xetpriority_msg[] ALIGN1 = "%cetpriority";
 
        int retval = EXIT_SUCCESS;
-       int which = PRIO_PROCESS;       /* Default 'which' value. */
+       int which = PRIO_PROCESS;  /* Default 'which' value. */
        int use_relative = 0;
        int adjustment, new_priority;
-       id_t who;
+       unsigned who;
+       char *arg;
 
-       ++argv;
+       /* Yes, they are not #defines in glibc 2.4! #if won't work */
+       if (PRIO_PROCESS < CHAR_MIN || PRIO_PROCESS > CHAR_MAX)
+               BUG_bad_PRIO_PROCESS();
+       if (PRIO_PGRP < CHAR_MIN || PRIO_PGRP > CHAR_MAX)
+               BUG_bad_PRIO_PGRP();
+       if (PRIO_USER < CHAR_MIN || PRIO_USER > CHAR_MAX)
+               BUG_bad_PRIO_USER();
+
+       arg = *++argv;
 
        /* Check if we are using a relative adjustment. */
-       if (argv[0] && (argv[0][0] == '-') && (argv[0][1] == 'n') && !argv[0][2]) {
+       if (arg && arg[0] == '-' && arg[1] == 'n') {
                use_relative = 1;
-               ++argv;
+               if (!arg[2])
+                       arg = *++argv;
+               else
+                       arg += 2;
        }
 
-       if (!*argv) {                           /* No args?  Then show usage. */
+       if (!arg) {  /* No args?  Then show usage. */
                bb_show_usage();
        }
 
        /* Get the priority adjustment (absolute or relative). */
-       adjustment = bb_xgetlarg(*argv, 10, INT_MIN, INT_MAX);
+       adjustment = xatoi_range(arg, INT_MIN/2, INT_MAX/2);
 
-       while (*++argv) {
+       while ((arg = *++argv) != NULL) {
                /* 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]))) {
+               if (arg[0] == '-' && arg[1]) {
+                       static const char opts[] ALIGN1 = {
+                               'p', 'g', 'u', 0, PRIO_PROCESS, PRIO_PGRP, PRIO_USER
+                       };
+                       const char *p = strchr(opts, arg[1]);
+                       if (p) {
                                which = p[4];
-                               continue;
+                               if (!arg[2])
+                                       continue;
+                               arg += 2;
                        }
                }
 
                /* Process an ID arg. */
                if (which == PRIO_USER) {
                        struct passwd *p;
-                       if (!(p = getpwnam(*argv))) {
-                               bb_error_msg("unknown user: %s", *argv);
+                       p = getpwnam(arg);
+                       if (!p) {
+                               bb_error_msg("unknown user %s", arg);
                                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);
+                       who = bb_strtou(arg, NULL, 10);
+                       if (errno) {
+                               bb_error_msg("invalid number '%s'", arg);
                                goto HAD_ERROR;
                        }
                }
@@ -123,14 +109,14 @@ int renice_main(int argc, char **argv)
                if (use_relative) {
                        int old_priority;
 
-                       errno = 0;       /* Needed for getpriority error detection. */
+                       errno = 0;  /* Needed for getpriority error detection. */
                        old_priority = getpriority(which, who);
                        if (errno) {
-                               bb_perror_msg(Xetpriority_msg, who, 'g');
+                               bb_perror_msg(Xetpriority_msg, 'g');
                                goto HAD_ERROR;
                        }
 
-                       new_priority = int_add_no_wrap(old_priority, adjustment);
+                       new_priority = old_priority + adjustment;
                } else {
                        new_priority = adjustment;
                }
@@ -139,8 +125,8 @@ int renice_main(int argc, char **argv)
                        continue;
                }
 
-               bb_perror_msg(Xetpriority_msg, who, 's');
      HAD_ERROR:
+               bb_perror_msg(Xetpriority_msg, 's');
+ HAD_ERROR:
                retval = EXIT_FAILURE;
        }