]> err.no Git - util-linux/commitdiff
taskset: proper numbers parsing
authorKarel Zak <kzak@redhat.com>
Thu, 30 Sep 2010 21:27:42 +0000 (23:27 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 30 Sep 2010 21:29:14 +0000 (23:29 +0200)
Reported-by: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
schedutils/Makefile.am
schedutils/chrt.c
schedutils/ionice.c
schedutils/schedutils.c [new file with mode: 0644]
schedutils/schedutils.h [new file with mode: 0644]
schedutils/taskset.c

index 13d45c85d57a1f883c249c6d356a208d402a94f0..c83e5eaff733386e642a83591f961b8d0383910f 100644 (file)
@@ -2,19 +2,24 @@ include $(top_srcdir)/config/include-Makefile.am
 
 if BUILD_SCHEDUTILS
 
+srcs_common = schedutils.c schedutils.h
+
 usrbin_exec_PROGRAMS = chrt
 dist_man_MANS = chrt.1
 
+chrt_SOURCES = chrt.c $(srcs_common)
+
 if HAVE_IOPRIO_GET
 if HAVE_IOPRIO_SET
 usrbin_exec_PROGRAMS += ionice
+ionice_SOURCES = ionice.c $(srcs_common)
 dist_man_MANS += ionice.1
 endif
 endif
 
 if HAVE_SCHED_GETAFFINITY
 usrbin_exec_PROGRAMS += taskset
-taskset_SOURCES = taskset.c $(top_srcdir)/lib/cpuset.c
+taskset_SOURCES = taskset.c $(top_srcdir)/lib/cpuset.c $(srcs_common)
 dist_man_MANS += taskset.1
 endif
 
index 2d0254fa997c3fa778bc4c5ff5391a3b37891711..89b12c7012a528b6acb6ebff4d8c01e04d80a810 100644 (file)
@@ -32,6 +32,8 @@
 #include "c.h"
 #include "nls.h"
 
+#include "schedutils.h"
+
 /* the SCHED_BATCH is supported since Linux 2.6.16
  *  -- temporary workaround for people with old glibc headers
  */
@@ -238,9 +240,7 @@ int main(int argc, char *argv[])
                        break;
                case 'p':
                        errno = 0;
-                       pid = strtol(argv[argc - 1], NULL, 10);
-                       if (errno)
-                               err(EXIT_FAILURE, _("failed to parse pid"));
+                       pid = getnum(argv[argc - 1], _("failed to parse pid"));
                        break;
                case 'r':
                        policy = SCHED_RR;
@@ -268,9 +268,7 @@ int main(int argc, char *argv[])
        }
 
        errno = 0;
-       priority = strtol(argv[optind], NULL, 10);
-       if (errno)
-               err(EXIT_FAILURE, _("failed to parse priority"));
+       priority = getnum(argv[optind], _("failed to parse priority"));
 
 #ifdef SCHED_RESET_ON_FORK
        /* sanity check */
index 3ad4ef9a6d707bea8fe35ff36e191855d2b142a3..34132f071da5bf4211a8e9b11ddb73425dabd046 100644 (file)
@@ -18,6 +18,8 @@
 
 #include "nls.h"
 
+#include "schedutils.h"
+
 static int tolerant;
 
 static inline int ioprio_set(int which, int who, int ioprio)
@@ -91,28 +93,6 @@ static void usage(int rc)
        exit(rc);
 }
 
-static long getnum(const char *str)
-{
-       long num;
-       char *end = NULL;
-
-       if (str == NULL || *str == '\0')
-               goto err;
-       errno = 0;
-       num = strtol(str, &end, 10);
-
-       if (errno || (end && *end))
-               goto err;
-
-       return num;
-err:
-       if (errno)
-               err(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
-       else
-               errx(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
-       return 0;
-}
-
 int main(int argc, char *argv[])
 {
        int ioprio = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c;
@@ -125,15 +105,15 @@ int main(int argc, char *argv[])
        while ((c = getopt(argc, argv, "+n:c:p:th")) != EOF) {
                switch (c) {
                case 'n':
-                       ioprio = getnum(optarg);
+                       ioprio = getnum(optarg, _("failed to parse class data"));
                        set |= 1;
                        break;
                case 'c':
-                       ioclass = getnum(optarg);
+                       ioclass = getnum(optarg, _("failed to parse class"));
                        set |= 2;
                        break;
                case 'p':
-                       pid = getnum(optarg);
+                       pid = getnum(optarg, _("failed to parse pid"));
                        break;
                case 't':
                        tolerant = 1;
@@ -167,7 +147,7 @@ int main(int argc, char *argv[])
                ioprio_print(pid);
 
                for(; argv[optind]; ++optind) {
-                       pid = getnum(argv[optind]);
+                       pid = getnum(argv[optind], _("failed to parse pid"));
                        ioprio_print(pid);
                }
        } else {
@@ -176,7 +156,7 @@ int main(int argc, char *argv[])
 
                        for(; argv[optind]; ++optind)
                        {
-                               pid = getnum(argv[optind]);
+                               pid = getnum(argv[optind], _("failed to parse pid"));
                                ioprio_setpid(pid, ioprio, ioclass);
                        }
                }
diff --git a/schedutils/schedutils.c b/schedutils/schedutils.c
new file mode 100644 (file)
index 0000000..9d6051b
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
+ *
+ * Released under the terms of the GNU General Public License version 2
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <err.h>
+
+#include "nls.h"
+
+long getnum(const char *str, const char *errmesg)
+{
+       long num;
+       char *end = NULL;
+
+       if (str == NULL || *str == '\0')
+               goto err;
+       errno = 0;
+       num = strtol(str, &end, 10);
+
+       if (errno || (end && *end))
+               goto err;
+
+       return num;
+err:
+       if (errno)
+               err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
+       else
+               errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
+       return 0;
+}
diff --git a/schedutils/schedutils.h b/schedutils/schedutils.h
new file mode 100644 (file)
index 0000000..80e4f7b
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef UTIL_LINUX_SCHED_UTILS
+#define UTIL_LINUX_SCHED_UTILS
+
+extern long getnum(const char *str, const char *errmesg);
+
+#endif
+
index 2f1bc745fbdaa30ad2aae5300506b62e2cee5206..201fc1589f2387c0af721715470e8bdd40d44600 100644 (file)
@@ -32,6 +32,8 @@
 #include "cpuset.h"
 #include "nls.h"
 
+#include "schedutils.h"
+
 static void __attribute__((__noreturn__)) usage(FILE *out)
 {
        fprintf(out,
@@ -87,7 +89,7 @@ int main(int argc, char *argv[])
        while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) {
                switch (opt) {
                case 'p':
-                       pid = atoi(argv[argc - 1]);
+                       pid = getnum(argv[argc - 1], _("failed to parse pid"));
                        break;
                case 'c':
                        c_opt = 1;