All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] various small fixes and changes
@ 2011-09-09  9:19 Heiko Carstens
  2011-09-09  9:19 ` [PATCH 1/5] lib,cpuset: fix comments for cpu mask/list functions Heiko Carstens
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Hi Karel,

just another bunch of small patches which are supposed to improve or
fix some small things.

Thanks,
Heiko


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/5] lib,cpuset: fix comments for cpu mask/list functions
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
@ 2011-09-09  9:19 ` Heiko Carstens
  2011-09-09  9:19 ` [PATCH 2/5] lib,cpuset: fix odd placed braces in cpulist_parse() Heiko Carstens
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

The comments for cpumask_parse() and cpulist_parse() each describe
the wrong function. Just exchange the comments.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 lib/cpuset.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/cpuset.c b/lib/cpuset.c
index dd31f52..842e8c8 100644
--- a/lib/cpuset.c
+++ b/lib/cpuset.c
@@ -221,7 +221,7 @@ char *cpumask_create(char *str, size_t len,
 }
 
 /*
- * Parses string with list of CPU ranges.
+ * Parses string with CPUs mask.
  */
 int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize)
 {
@@ -262,7 +262,7 @@ int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize)
 }
 
 /*
- * Parses string with CPUs mask.
+ * Parses string with list of CPU ranges.
  */
 int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 {
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/5] lib,cpuset: fix odd placed braces in cpulist_parse()
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
  2011-09-09  9:19 ` [PATCH 1/5] lib,cpuset: fix comments for cpu mask/list functions Heiko Carstens
@ 2011-09-09  9:19 ` Heiko Carstens
  2011-09-09  9:19 ` [PATCH 3/5] lib,cpuset: fix stride handling " Heiko Carstens
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

The opening and closing braces for two following if statements within
cpulist_parse() are placed in an odd manner.
Just fix this to prevent broken code in the future.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 lib/cpuset.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/cpuset.c b/lib/cpuset.c
index 842e8c8..cf22aa7 100644
--- a/lib/cpuset.c
+++ b/lib/cpuset.c
@@ -289,8 +289,8 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 			if (sscanf(c1, "%u", &b) < 1)
 				return 1;
 			c1 = nexttoken(c1, ':');
-			if (c1 != NULL && (c2 == NULL || c1 < c2))
-				if (sscanf(c1, "%u", &s) < 1) {
+			if (c1 != NULL && (c2 == NULL || c1 < c2)) {
+				if (sscanf(c1, "%u", &s) < 1)
 					return 1;
 			}
 		}
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/5] lib,cpuset: fix stride handling in cpulist_parse()
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
  2011-09-09  9:19 ` [PATCH 1/5] lib,cpuset: fix comments for cpu mask/list functions Heiko Carstens
  2011-09-09  9:19 ` [PATCH 2/5] lib,cpuset: fix odd placed braces in cpulist_parse() Heiko Carstens
@ 2011-09-09  9:19 ` Heiko Carstens
  2011-09-09  9:19 ` [PATCH 4/5] lib,cpuset: enforce stricter parsing of cpu lists Heiko Carstens
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

If cpulist_parse() gets passed a cpu list with a stride value of 0
it will be stuck in an endless loop.
E.g. the following cpu list will cause an endless loop: "0-2:0".
Fix this by causing a parse error if the stride value is 0.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 lib/cpuset.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/lib/cpuset.c b/lib/cpuset.c
index cf22aa7..8592755 100644
--- a/lib/cpuset.c
+++ b/lib/cpuset.c
@@ -292,6 +292,8 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 			if (c1 != NULL && (c2 == NULL || c1 < c2)) {
 				if (sscanf(c1, "%u", &s) < 1)
 					return 1;
+				if (s == 0)
+					return 1;
 			}
 		}
 
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/5] lib,cpuset: enforce stricter parsing of cpu lists
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
                   ` (2 preceding siblings ...)
  2011-09-09  9:19 ` [PATCH 3/5] lib,cpuset: fix stride handling " Heiko Carstens
@ 2011-09-09  9:19 ` Heiko Carstens
  2011-09-09  9:19 ` [PATCH 5/5] chcpu,cpuset: reduce code duplication for cpu list parsing Heiko Carstens
  2011-09-09 22:10 ` [PATCH 0/5] various small fixes and changes Karel Zak
  5 siblings, 0 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

The current cpulist_parse() function ignores extra non-parsable characters
at the end of the to be parsed cpu list string.
E.g. it would accept something like "0bla" and just set bit 0 in the cpu set.
Since such a string is invalid implement stricter parsing that makes sure
that everything of the string has been succesfully parsed.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 lib/cpuset.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/cpuset.c b/lib/cpuset.c
index 8592755..ebaffcc 100644
--- a/lib/cpuset.c
+++ b/lib/cpuset.c
@@ -268,8 +268,9 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 {
 	size_t max = cpuset_nbits(setsize);
 	const char *p, *q;
-	q = str;
+	int r;
 
+	q = str;
 	CPU_ZERO_S(setsize, set);
 
 	while (p = q, q = nexttoken(q, ','), p) {
@@ -277,8 +278,9 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 		unsigned int b;	/* end of range */
 		unsigned int s;	/* stride */
 		const char *c1, *c2;
+		char c;
 
-		if (sscanf(p, "%u", &a) < 1)
+		if ((r = sscanf(p, "%u%c", &a, &c)) < 1)
 			return 1;
 		b = a;
 		s = 1;
@@ -286,11 +288,11 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 		c1 = nexttoken(p, '-');
 		c2 = nexttoken(p, ',');
 		if (c1 != NULL && (c2 == NULL || c1 < c2)) {
-			if (sscanf(c1, "%u", &b) < 1)
+			if ((r = sscanf(c1, "%u%c", &b, &c)) < 1)
 				return 1;
 			c1 = nexttoken(c1, ':');
 			if (c1 != NULL && (c2 == NULL || c1 < c2)) {
-				if (sscanf(c1, "%u", &s) < 1)
+				if ((r = sscanf(c1, "%u%c", &s, &c)) < 1)
 					return 1;
 				if (s == 0)
 					return 1;
@@ -307,6 +309,8 @@ int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)
 		}
 	}
 
+	if (r == 2)
+		return 1;
 	return 0;
 }
 
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 5/5] chcpu,cpuset: reduce code duplication for cpu list parsing
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
                   ` (3 preceding siblings ...)
  2011-09-09  9:19 ` [PATCH 4/5] lib,cpuset: enforce stricter parsing of cpu lists Heiko Carstens
@ 2011-09-09  9:19 ` Heiko Carstens
  2011-09-09 22:10 ` [PATCH 0/5] various small fixes and changes Karel Zak
  5 siblings, 0 replies; 7+ messages in thread
From: Heiko Carstens @ 2011-09-09  9:19 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

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      |    7 ++++++-
 sys-utils/chcpu.c |   28 ++++++++++++++++------------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/lib/cpuset.c b/lib/cpuset.c
index ebaffcc..c3b67a7 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 2d5725f..a5d12c7 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);
-- 
1.7.5.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/5] various small fixes and changes
  2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
                   ` (4 preceding siblings ...)
  2011-09-09  9:19 ` [PATCH 5/5] chcpu,cpuset: reduce code duplication for cpu list parsing Heiko Carstens
@ 2011-09-09 22:10 ` Karel Zak
  5 siblings, 0 replies; 7+ messages in thread
From: Karel Zak @ 2011-09-09 22:10 UTC (permalink / raw)
  To: Heiko Carstens; +Cc: util-linux

On Fri, Sep 09, 2011 at 11:19:29AM +0200, Heiko Carstens wrote:
> just another bunch of small patches which are supposed to improve or
> fix some small things.

 Merged, thanks!

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-09-09 22:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-09  9:19 [PATCH 0/5] various small fixes and changes Heiko Carstens
2011-09-09  9:19 ` [PATCH 1/5] lib,cpuset: fix comments for cpu mask/list functions Heiko Carstens
2011-09-09  9:19 ` [PATCH 2/5] lib,cpuset: fix odd placed braces in cpulist_parse() Heiko Carstens
2011-09-09  9:19 ` [PATCH 3/5] lib,cpuset: fix stride handling " Heiko Carstens
2011-09-09  9:19 ` [PATCH 4/5] lib,cpuset: enforce stricter parsing of cpu lists Heiko Carstens
2011-09-09  9:19 ` [PATCH 5/5] chcpu,cpuset: reduce code duplication for cpu list parsing Heiko Carstens
2011-09-09 22:10 ` [PATCH 0/5] various small fixes and changes Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.