]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chcpu,cpuset: reduce code duplication for cpu list parsing
authorHeiko Carstens <heiko.carstens@de.ibm.com>
Fri, 9 Sep 2011 09:19:34 +0000 (11:19 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 9 Sep 2011 22:00:35 +0000 (00:00 +0200)
Reduce code duplication and print better error message if an
unsupported cpu number was passed.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
lib/cpuset.c
sys-utils/chcpu.c

index ebaffccfb3bc85cefe932c53e6a0f0e61d34e199..c3b67a705337b318c2310e3264fbcb2086e7ddf9 100644 (file)
@@ -263,6 +263,11 @@ int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize)
 
 /*
  * Parses string with list of CPU ranges.
+ * Returns 0 on success.
+ * Returns 1 on error.
+ * Returns 2 if fail is set and a cpu number passed in the list doesn't fit
+ * into the cpu_set. If fail is not set cpu numbers that do not fit are
+ * ignored and 0 is returned instead.
  */
 int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 {
@@ -303,7 +308,7 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
                        return 1;
                while (a <= b) {
                        if (fail && (a >= max))
-                               return 1;
+                               return 2;
                        CPU_SET_S(a, setsize, set);
                        a += s;
                }
index 2d5725f834114c48a8e8a28722a73ea1442c84f0..a5d12c71fd06a40f17a1163b3cc98e12128da359 100644 (file)
@@ -210,6 +210,18 @@ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
        return EXIT_SUCCESS;
 }
 
+static void cpu_parse(char *cpu_string, cpu_set_t *cpu_set, size_t setsize)
+{
+       int rc;
+
+       rc = cpulist_parse(cpu_string, cpu_set, setsize, 1);
+       if (rc == 0)
+               return;
+       if (rc == 2)
+               errx(EXIT_FAILURE, _("invalid CPU number in CPU list: %s"), cpu_string);
+       errx(EXIT_FAILURE, _("failed to parse CPU list: %s"), cpu_string);
+}
+
 static void __attribute__((__noreturn__)) usage(FILE *out)
 {
        fprintf(out, _(
@@ -269,27 +281,19 @@ int main(int argc, char *argv[])
                switch (c) {
                case 'c':
                        cmd = CMD_CPU_CONFIGURE;
-                       if (cpulist_parse(argv[optind - 1], cpu_set, setsize, 1))
-                               errx(EXIT_FAILURE, _("failed to parse CPU list: %s"),
-                                    argv[optind -1 ]);
+                       cpu_parse(argv[optind - 1], cpu_set, setsize);
                        break;
                case 'd':
                        cmd = CMD_CPU_DISABLE;
-                       if (cpulist_parse(argv[optind - 1], cpu_set, setsize, 1))
-                               errx(EXIT_FAILURE, _("failed to parse CPU list: %s"),
-                                    argv[optind -1 ]);
+                       cpu_parse(argv[optind - 1], cpu_set, setsize);
                        break;
                case 'e':
                        cmd = CMD_CPU_ENABLE;
-                       if (cpulist_parse(argv[optind - 1], cpu_set, setsize, 1))
-                               errx(EXIT_FAILURE, _("failed to parse CPU list: %s"),
-                                    argv[optind -1 ]);
+                       cpu_parse(argv[optind - 1], cpu_set, setsize);
                        break;
                case 'g':
                        cmd = CMD_CPU_DECONFIGURE;
-                       if (cpulist_parse(argv[optind - 1], cpu_set, setsize, 1))
-                               errx(EXIT_FAILURE, _("failed to parse CPU list: %s"),
-                                    argv[optind -1 ]);
+                       cpu_parse(argv[optind - 1], cpu_set, setsize);
                        break;
                case 'h':
                        usage(stdout);