summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHeiko Carstens2011-09-09 11:19:34 +0200
committerKarel Zak2011-09-10 00:00:35 +0200
commit59fb133a029fea29e38a98b5177fc760a0c8dc17 (patch)
tree9cf2c4f7e4e3270b3cb544724376f9e9cdac6555
parentlib,cpuset: enforce stricter parsing of cpu lists (diff)
downloadkernel-qcow2-util-linux-59fb133a029fea29e38a98b5177fc760a0c8dc17.tar.gz
kernel-qcow2-util-linux-59fb133a029fea29e38a98b5177fc760a0c8dc17.tar.xz
kernel-qcow2-util-linux-59fb133a029fea29e38a98b5177fc760a0c8dc17.zip
chcpu,cpuset: reduce code duplication for cpu list parsing
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>
-rw-r--r--lib/cpuset.c7
-rw-r--r--sys-utils/chcpu.c28
2 files changed, 22 insertions, 13 deletions
diff --git a/lib/cpuset.c b/lib/cpuset.c
index ebaffccfb..c3b67a705 100644
--- a/lib/cpuset.c
+++ b/lib/cpuset.c
@@ -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;
}
diff --git a/sys-utils/chcpu.c b/sys-utils/chcpu.c
index 2d5725f83..a5d12c71f 100644
--- a/sys-utils/chcpu.c
+++ b/sys-utils/chcpu.c
@@ -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);