All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] more lscpu and chcpu changes
@ 2011-09-15  6:52 Heiko Carstens
  2011-09-15  6:52 ` [PATCH 1/6] lib,path: move path access functions from lscpu into lib/path.c Heiko Carstens
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Hi Karel,

just another batch of small changes to lscpu and chcpu.

The only thing that is planned that should follow are man page updates
to both tools. However I'd like to let a technical writer over here to
review and fix my changes before sending them.

Thanks,
Heiko

Heiko Carstens (6):
  lib,path: move path access functions from lscpu into lib/path.c
  chcpu: convert to use lib/path.c
  chcpu: provide better user feedback
  lscpu: fix -e output
  lscpu: add --offline option
  lscpu: stricter command line parsing

 include/path.h        |   17 ++++
 lib/path.c            |  217 ++++++++++++++++++++++++++++++++++++++++++++
 sys-utils/Makefile.am |    6 +-
 sys-utils/chcpu.c     |  122 +++++++++++--------------
 sys-utils/lscpu.1     |    9 ++-
 sys-utils/lscpu.c     |  237 ++++++++++---------------------------------------
 6 files changed, 348 insertions(+), 260 deletions(-)
 create mode 100644 include/path.h
 create mode 100644 lib/path.c

-- 
1.7.5.4

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

* [PATCH 1/6] lib,path: move path access functions from lscpu into lib/path.c
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-15  6:52 ` [PATCH 2/6] chcpu: convert to use lib/path.c Heiko Carstens
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

A couple of these functions already have been copied to chcpu.c,
so it makes sense to move these functions into an own file.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 include/path.h        |   15 ++++
 lib/path.c            |  191 +++++++++++++++++++++++++++++++++++++++++++++++
 sys-utils/Makefile.am |    3 +-
 sys-utils/lscpu.c     |  199 ++++++-------------------------------------------
 4 files changed, 231 insertions(+), 177 deletions(-)
 create mode 100644 include/path.h
 create mode 100644 lib/path.c

diff --git a/include/path.h b/include/path.h
new file mode 100644
index 0000000..b1ee724
--- /dev/null
+++ b/include/path.h
@@ -0,0 +1,15 @@
+#include <stdio.h>
+
+extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
+			__attribute__ ((__format__ (__printf__, 3, 4)));
+extern void path_getstr(char *result, size_t len, const char *path, ...)
+			__attribute__ ((__format__ (__printf__, 3, 4)));
+extern int path_getnum(const char *path, ...)
+		       __attribute__ ((__format__ (__printf__, 1, 2)));
+extern int path_exist(const char *path, ...)
+		      __attribute__ ((__format__ (__printf__, 1, 2)));
+extern cpu_set_t *path_cpuset(int, const char *path, ...)
+			      __attribute__ ((__format__ (__printf__, 2, 3)));
+extern cpu_set_t *path_cpulist(int, const char *path, ...)
+			       __attribute__ ((__format__ (__printf__, 2, 3)));
+extern void path_setprefix(const char *);
diff --git a/lib/path.c b/lib/path.c
new file mode 100644
index 0000000..219eed6
--- /dev/null
+++ b/lib/path.c
@@ -0,0 +1,191 @@
+/*
+ * Simple functions to access files.
+ *
+ * Taken from lscpu.c
+ *
+ * Copyright (C) 2008 Cai Qian <qcai@redhat.com>
+ * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
+ *
+ * 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 would 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
+ */
+
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "cpuset.h"
+#include "path.h"
+#include "nls.h"
+#include "c.h"
+
+static size_t prefixlen;
+static char pathbuf[PATH_MAX];
+
+static const char *
+path_vcreate(const char *path, va_list ap)
+{
+	if (prefixlen)
+		vsnprintf(pathbuf + prefixlen,
+			  sizeof(pathbuf) - prefixlen, path, ap);
+	else
+		vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
+	return pathbuf;
+}
+
+static FILE *
+path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
+{
+	FILE *f;
+	const char *p = path_vcreate(path, ap);
+
+	f = fopen(p, mode);
+	if (!f && exit_on_error)
+		err(EXIT_FAILURE, _("error: cannot open %s"), p);
+	return f;
+}
+
+FILE *
+path_fopen(const char *mode, int exit_on_error, const char *path, ...)
+{
+	FILE *fd;
+	va_list ap;
+
+	va_start(ap, path);
+	fd = path_vfopen(mode, exit_on_error, path, ap);
+	va_end(ap);
+
+	return fd;
+}
+
+void
+path_getstr(char *result, size_t len, const char *path, ...)
+{
+	FILE *fd;
+	va_list ap;
+
+	va_start(ap, path);
+	fd = path_vfopen("r", 1, path, ap);
+	va_end(ap);
+
+	if (!fgets(result, len, fd))
+		err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
+	fclose(fd);
+
+	len = strlen(result);
+	if (result[len - 1] == '\n')
+		result[len - 1] = '\0';
+}
+
+int
+path_getnum(const char *path, ...)
+{
+	FILE *fd;
+	va_list ap;
+	int result;
+
+	va_start(ap, path);
+	fd = path_vfopen("r", 1, path, ap);
+	va_end(ap);
+
+	if (fscanf(fd, "%d", &result) != 1) {
+		if (ferror(fd))
+			err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
+		else
+			errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
+	}
+	fclose(fd);
+	return result;
+}
+
+int
+path_exist(const char *path, ...)
+{
+	va_list ap;
+	const char *p;
+
+	va_start(ap, path);
+	p = path_vcreate(path, ap);
+	va_end(ap);
+
+	return access(p, F_OK) == 0;
+}
+
+static cpu_set_t *
+path_cpuparse(int maxcpus, int islist, const char *path, va_list ap)
+{
+	FILE *fd;
+	cpu_set_t *set;
+	size_t setsize, len = maxcpus * 7;
+	char buf[len];
+
+	fd = path_vfopen("r", 1, path, ap);
+
+	if (!fgets(buf, len, fd))
+		err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
+	fclose(fd);
+
+	len = strlen(buf);
+	if (buf[len - 1] == '\n')
+		buf[len - 1] = '\0';
+
+	set = cpuset_alloc(maxcpus, &setsize, NULL);
+	if (!set)
+		err(EXIT_FAILURE, _("failed to callocate cpu set"));
+
+	if (islist) {
+		if (cpulist_parse(buf, set, setsize, 0))
+			errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
+	} else {
+		if (cpumask_parse(buf, set, setsize))
+			errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
+	}
+	return set;
+}
+
+cpu_set_t *
+path_cpuset(int maxcpus, const char *path, ...)
+{
+	va_list ap;
+	cpu_set_t *set;
+
+	va_start(ap, path);
+	set = path_cpuparse(maxcpus, 0, path, ap);
+	va_end(ap);
+
+	return set;
+}
+
+cpu_set_t *
+path_cpulist(int maxcpus, const char *path, ...)
+{
+	va_list ap;
+	cpu_set_t *set;
+
+	va_start(ap, path);
+	set = path_cpuparse(maxcpus, 1, path, ap);
+	va_end(ap);
+
+	return set;
+}
+
+void
+path_setprefix(const char *prefix)
+{
+	prefixlen = strlen(prefix);
+	strncpy(pathbuf, prefix, sizeof(pathbuf));
+	pathbuf[sizeof(pathbuf) - 1] = '\0';
+}
diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am
index 1e5dcf4..f2a2c24 100644
--- a/sys-utils/Makefile.am
+++ b/sys-utils/Makefile.am
@@ -22,7 +22,8 @@ usrbin_exec_PROGRAMS += lscpu
 lscpu_SOURCES = lscpu.c $(top_srcdir)/lib/cpuset.c \
 			$(top_srcdir)/lib/strutils.c \
 			$(top_srcdir)/lib/mbsalign.c \
-			$(top_srcdir)/lib/tt.c
+			$(top_srcdir)/lib/tt.c \
+			$(top_srcdir)/lib/path.c
 dist_man_MANS += lscpu.1
 sbin_PROGRAMS += chcpu
 chcpu_SOURCES = chcpu.c $(top_srcdir)/lib/cpuset.c
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 6a37bb3..78d936f 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -40,6 +40,7 @@
 #include "strutils.h"
 #include "bitops.h"
 #include "tt.h"
+#include "path.h"
 
 #define CACHE_MAX 100
 
@@ -184,33 +185,26 @@ enum {
 	OUTPUT_READABLE,	/* -e */
 };
 
+enum {
+	SYSTEM_LIVE = 0,	/* analyzing a live system */
+	SYSTEM_SNAPSHOT,	/* analyzing a snapshot of a different system */
+};
+
 struct lscpu_modifier {
 	int		mode;		/* OUTPUT_* */
+	int		system;		/* SYSTEM_* */
 	unsigned int	hex:1,		/* print CPU masks rather than CPU lists */
 			compat:1,	/* use backwardly compatible format */
 			allcpus:1,	/* print all CPUs */
 			online:1;	/* print online CPUs only */
 };
 
-static size_t sysrootlen;
-static char pathbuf[PATH_MAX];
 static int maxcpus;		/* size in bits of kernel cpu mask */
 
 #define is_cpu_online(_d, _cpu) \
 		((_d) && (_d)->online ? \
 			CPU_ISSET_S((_cpu), CPU_ALLOC_SIZE(maxcpus), (_d)->online) : 0)
 
-static FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...)
-		__attribute__ ((__format__ (__printf__, 3, 4)));
-static void path_getstr(char *result, size_t len, const char *path, ...)
-		__attribute__ ((__format__ (__printf__, 3, 4)));
-static int path_getnum(const char *path, ...)
-		__attribute__ ((__format__ (__printf__, 1, 2)));
-static int path_exist(const char *path, ...)
-		__attribute__ ((__format__ (__printf__, 1, 2)));
-static cpu_set_t *path_cpuset(const char *path, ...)
-		__attribute__ ((__format__ (__printf__, 1, 2)));
-
 /*
  * Parsable output
  */
@@ -256,153 +250,6 @@ static int column_name_to_id(const char *name, size_t namesz)
 	return -1;
 }
 
-static const char *
-path_vcreate(const char *path, va_list ap)
-{
-	if (sysrootlen)
-		vsnprintf(pathbuf + sysrootlen,
-			  sizeof(pathbuf) - sysrootlen, path, ap);
-	else
-		vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
-	return pathbuf;
-}
-
-static FILE *
-path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
-{
-	FILE *f;
-	const char *p = path_vcreate(path, ap);
-
-	f = fopen(p, mode);
-	if (!f && exit_on_error)
-		err(EXIT_FAILURE, _("error: cannot open %s"), p);
-	return f;
-}
-
-static FILE *
-path_fopen(const char *mode, int exit_on_error, const char *path, ...)
-{
-	FILE *fd;
-	va_list ap;
-
-	va_start(ap, path);
-	fd = path_vfopen(mode, exit_on_error, path, ap);
-	va_end(ap);
-
-	return fd;
-}
-
-static void
-path_getstr(char *result, size_t len, const char *path, ...)
-{
-	FILE *fd;
-	va_list ap;
-
-	va_start(ap, path);
-	fd = path_vfopen("r", 1, path, ap);
-	va_end(ap);
-
-	if (!fgets(result, len, fd))
-		err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
-	fclose(fd);
-
-	len = strlen(result);
-	if (result[len - 1] == '\n')
-		result[len - 1] = '\0';
-}
-
-static int
-path_getnum(const char *path, ...)
-{
-	FILE *fd;
-	va_list ap;
-	int result;
-
-	va_start(ap, path);
-	fd = path_vfopen("r", 1, path, ap);
-	va_end(ap);
-
-	if (fscanf(fd, "%d", &result) != 1) {
-		if (ferror(fd))
-			err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
-		else
-			errx(EXIT_FAILURE, _("parse error: %s"), pathbuf);
-	}
-	fclose(fd);
-	return result;
-}
-
-static int
-path_exist(const char *path, ...)
-{
-	va_list ap;
-	const char *p;
-
-	va_start(ap, path);
-	p = path_vcreate(path, ap);
-	va_end(ap);
-
-	return access(p, F_OK) == 0;
-}
-
-static cpu_set_t *
-path_cpuparse(int islist, const char *path, va_list ap)
-{
-	FILE *fd;
-	cpu_set_t *set;
-	size_t setsize, len = maxcpus * 7;
-	char buf[len];
-
-	fd = path_vfopen("r", 1, path, ap);
-
-	if (!fgets(buf, len, fd))
-		err(EXIT_FAILURE, _("failed to read: %s"), pathbuf);
-	fclose(fd);
-
-	len = strlen(buf);
-	if (buf[len - 1] == '\n')
-		buf[len - 1] = '\0';
-
-	set = cpuset_alloc(maxcpus, &setsize, NULL);
-	if (!set)
-		err(EXIT_FAILURE, _("failed to callocate cpu set"));
-
-	if (islist) {
-		if (cpulist_parse(buf, set, setsize, 0))
-			errx(EXIT_FAILURE, _("failed to parse CPU list %s"), buf);
-	} else {
-		if (cpumask_parse(buf, set, setsize))
-			errx(EXIT_FAILURE, _("failed to parse CPU mask %s"), buf);
-	}
-	return set;
-}
-
-static cpu_set_t *
-path_cpuset(const char *path, ...)
-{
-	va_list ap;
-	cpu_set_t *set;
-
-	va_start(ap, path);
-	set = path_cpuparse(0, path, ap);
-	va_end(ap);
-
-	return set;
-}
-
-static cpu_set_t *
-path_cpulist(const char *path, ...)
-{
-	va_list ap;
-	cpu_set_t *set;
-
-	va_start(ap, path);
-	set = path_cpuparse(1, path, ap);
-	va_end(ap);
-
-	return set;
-}
-
 /* Lookup a pattern and get the value from cpuinfo.
  * Format is:
  *
@@ -448,11 +295,11 @@ int lookup(char *line, char *pattern, char **value)
  * detect that CPU supports 64-bit mode.
  */
 static int
-init_mode(void)
+init_mode(struct lscpu_modifier *mod)
 {
 	int m = 0;
 
-	if (sysrootlen)
+	if (mod->system == SYSTEM_SNAPSHOT)
 		/* reading info from any /{sys,proc} dump, don't mix it with
 		 * information about our real CPU */
 		return 0;
@@ -470,7 +317,7 @@ init_mode(void)
 }
 
 static void
-read_basicinfo(struct lscpu_desc *desc)
+read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod)
 {
 	FILE *fp = path_fopen("r", 1, _PATH_PROC_CPUINFO);
 	char buf[BUFSIZ];
@@ -503,7 +350,7 @@ read_basicinfo(struct lscpu_desc *desc)
 			continue;
 	}
 
-	desc->mode = init_mode();
+	desc->mode = init_mode(mod);
 
 	if (desc->flags) {
 		snprintf(buf, sizeof(buf), " %s ", desc->flags);
@@ -525,7 +372,7 @@ read_basicinfo(struct lscpu_desc *desc)
 		/* note that kernel_max is maximum index [NR_CPUS-1] */
 		maxcpus = path_getnum(_PATH_SYS_SYSTEM "/cpu/kernel_max") + 1;
 
-	else if (!sysrootlen)
+	else if (mod->system == SYSTEM_LIVE)
 		/* the root is '/' so we are working with data from the current kernel */
 		maxcpus = get_max_number_of_cpus();
 	else
@@ -536,7 +383,7 @@ read_basicinfo(struct lscpu_desc *desc)
 	/* get mask for online CPUs */
 	if (path_exist(_PATH_SYS_SYSTEM "/cpu/online")) {
 		size_t setsize = CPU_ALLOC_SIZE(maxcpus);
-		desc->online = path_cpulist(_PATH_SYS_SYSTEM "/cpu/online");
+		desc->online = path_cpulist(maxcpus, _PATH_SYS_SYSTEM "/cpu/online");
 		desc->nthreads = CPU_COUNT_S(setsize, desc->online);
 	}
 
@@ -733,13 +580,13 @@ read_topology(struct lscpu_desc *desc, int num)
 	if (!path_exist(_PATH_SYS_CPU "/cpu%d/topology/thread_siblings", num))
 		return;
 
-	thread_siblings = path_cpuset(_PATH_SYS_CPU
+	thread_siblings = path_cpuset(maxcpus, _PATH_SYS_CPU
 					"/cpu%d/topology/thread_siblings", num);
-	core_siblings = path_cpuset(_PATH_SYS_CPU
+	core_siblings = path_cpuset(maxcpus, _PATH_SYS_CPU
 					"/cpu%d/topology/core_siblings", num);
 	book_siblings = NULL;
 	if (path_exist(_PATH_SYS_CPU "/cpu%d/topology/book_siblings", num)) {
-		book_siblings = path_cpuset(_PATH_SYS_CPU
+		book_siblings = path_cpuset(maxcpus, _PATH_SYS_CPU
 					    "/cpu%d/topology/book_siblings", num);
 	}
 
@@ -891,8 +738,9 @@ read_cache(struct lscpu_desc *desc, int num)
 		}
 
 		/* information about how CPUs share different caches */
-		map = path_cpuset(_PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map",
-				num, i);
+		map = path_cpuset(maxcpus,
+				  _PATH_SYS_CPU "/cpu%d/cache/index%d/shared_cpu_map",
+				  num, i);
 
 		if (!ca->sharedmaps)
 			ca->sharedmaps = xcalloc(desc->ncpus, sizeof(cpu_set_t *));
@@ -916,7 +764,7 @@ read_nodes(struct lscpu_desc *desc)
 
 	/* information about how nodes share different CPUs */
 	for (i = 0; i < desc->nnodes; i++)
-		desc->nodemaps[i] = path_cpuset(
+		desc->nodemaps[i] = path_cpuset(maxcpus,
 					_PATH_SYS_SYSTEM "/node/node%d/cpumap",
 					i);
 }
@@ -1404,9 +1252,8 @@ int main(int argc, char *argv[])
 			mod->mode = c == 'p' ? OUTPUT_PARSABLE : OUTPUT_READABLE;
 			break;
 		case 's':
-			sysrootlen = strlen(optarg);
-			strncpy(pathbuf, optarg, sizeof(pathbuf));
-			pathbuf[sizeof(pathbuf) - 1] = '\0';
+			path_setprefix(optarg);
+			mod->system = SYSTEM_SNAPSHOT;
 			break;
 		case 'x':
 			mod->hex = 1;
@@ -1420,7 +1267,7 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	read_basicinfo(desc);
+	read_basicinfo(desc, mod);
 
 	for (i = 0; i < desc->ncpus; i++) {
 		if (desc->online && !is_cpu_online(desc, i))
-- 
1.7.5.4

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

* [PATCH 2/6] chcpu: convert to use lib/path.c
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
  2011-09-15  6:52 ` [PATCH 1/6] lib,path: move path access functions from lscpu into lib/path.c Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-16  8:28   ` Heiko Carstens
  2011-09-15  6:52 ` [PATCH 3/6] chcpu: provide better user feedback Heiko Carstens
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Use the common path access functions. In order to simplify chcpu also implement
and use path_writestr() which writes a string to the path specified.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 include/path.h        |    2 +
 lib/path.c            |   26 +++++++++++++++++
 sys-utils/Makefile.am |    3 +-
 sys-utils/chcpu.c     |   73 +++++++++++--------------------------------------
 4 files changed, 46 insertions(+), 58 deletions(-)

diff --git a/include/path.h b/include/path.h
index b1ee724..8e79a85 100644
--- a/include/path.h
+++ b/include/path.h
@@ -4,6 +4,8 @@ extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...
 			__attribute__ ((__format__ (__printf__, 3, 4)));
 extern void path_getstr(char *result, size_t len, const char *path, ...)
 			__attribute__ ((__format__ (__printf__, 3, 4)));
+extern int path_writestr(const char *str, const char *path, ...)
+			 __attribute__ ((__format__ (__printf__, 2, 3)));
 extern int path_getnum(const char *path, ...)
 		       __attribute__ ((__format__ (__printf__, 1, 2)));
 extern int path_exist(const char *path, ...)
diff --git a/lib/path.c b/lib/path.c
index 219eed6..e2bb398 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -58,6 +58,18 @@ path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
 	return f;
 }
 
+static int
+path_vopen(int flags, const char *path, va_list ap)
+{
+	int fd;
+	const char *p = path_vcreate(path, ap);
+
+	fd = open(p, flags);
+	if (fd == -1)
+		err(EXIT_FAILURE, _("error: cannot open %s"), p);
+	return fd;
+}
+
 FILE *
 path_fopen(const char *mode, int exit_on_error, const char *path, ...)
 {
@@ -112,6 +124,20 @@ path_getnum(const char *path, ...)
 }
 
 int
+path_writestr(const char *str, const char *path, ...)
+{
+	int fd, result;
+	va_list ap;
+
+	va_start(ap, path);
+	fd = path_vopen(O_WRONLY, path, ap);
+	va_end(ap);
+	result = write(fd, str, strlen(str));
+	close(fd);
+	return result;
+}
+
+int
 path_exist(const char *path, ...)
 {
 	va_list ap;
diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am
index f2a2c24..ab138e8 100644
--- a/sys-utils/Makefile.am
+++ b/sys-utils/Makefile.am
@@ -26,7 +26,8 @@ lscpu_SOURCES = lscpu.c $(top_srcdir)/lib/cpuset.c \
 			$(top_srcdir)/lib/path.c
 dist_man_MANS += lscpu.1
 sbin_PROGRAMS += chcpu
-chcpu_SOURCES = chcpu.c $(top_srcdir)/lib/cpuset.c
+chcpu_SOURCES = chcpu.c $(top_srcdir)/lib/cpuset.c \
+			$(top_srcdir)/lib/path.c
 dist_man_MANS += chcpu.1
 endif
 
diff --git a/sys-utils/chcpu.c b/sys-utils/chcpu.c
index a5d12c7..d4a2510 100644
--- a/sys-utils/chcpu.c
+++ b/sys-utils/chcpu.c
@@ -39,13 +39,12 @@
 #include "c.h"
 #include "strutils.h"
 #include "bitops.h"
+#include "path.h"
 
 #define _PATH_SYS_CPU		"/sys/devices/system/cpu"
 #define _PATH_SYS_CPU_RESCAN	_PATH_SYS_CPU "/rescan"
 #define _PATH_SYS_CPU_DISPATCH	_PATH_SYS_CPU "/dispatching"
 
-static char pathbuf[PATH_MAX];
-
 enum {
 	CMD_CPU_ENABLE	= 0,
 	CMD_CPU_DISABLE,
@@ -56,35 +55,10 @@ enum {
 	CMD_CPU_DISPATCH_VERTICAL,
 };
 
-static int path_open(mode_t mode, const char *path, ...)
-{
-	va_list ap;
-	int fd;
-
-	va_start(ap, path);
-	vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
-	va_end(ap);
-	fd = open(pathbuf, mode);
-	if (fd == -1)
-		err(EXIT_FAILURE, "error: cannot open %s", pathbuf);
-	return fd;
-}
-
-static int path_exist(const char *path, ...)
-{
-	va_list ap;
-
-	va_start(ap, path);
-	vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
-	va_end(ap);
-	return access(pathbuf, F_OK) == 0;
-}
-
 static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
 {
 	unsigned int cpu;
-	int fd, rc;
-	char c;
+	int online, rc;
 
 	for (cpu = 0; cpu < setsize; cpu++) {
 		if (!CPU_ISSET(cpu, cpu_set))
@@ -97,76 +71,64 @@ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
 			printf(_("CPU %d is not hot pluggable\n"), cpu);
 			continue;
 		}
-		fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/online", cpu);
-		if (read(fd, &c, 1) == -1)
-			err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
-		if ((c == '1') && (enable == 1)) {
+		online = path_getnum(_PATH_SYS_CPU "/cpu%d/online", cpu);
+		if ((online == 1) && (enable == 1)) {
 			printf(_("CPU %d is already enabled\n"), cpu);
 			continue;
 		}
-		if ((c == '0') && (enable == 0)) {
+		if ((online == 0) && (enable == 0)) {
 			printf(_("CPU %d is already disabled\n"), cpu);
 			continue;
 		}
 		if (enable) {
-			rc = write(fd, "1", 1);
+			rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
 			if (rc == -1)
 				printf(_("CPU %d enable failed (%s)\n"), cpu,
 					strerror(errno));
 			else
 				printf(_("CPU %d enabled\n"), cpu);
 		} else {
-			rc = write(fd, "0", 1);
+			rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
 			if (rc == -1)
 				printf(_("CPU %d disable failed (%s)\n"), cpu,
 					strerror(errno));
 			else
 				printf(_("CPU %d disabled\n"), cpu);
 		}
-		close(fd);
 	}
 	return EXIT_SUCCESS;
 }
 
 static int cpu_rescan(void)
 {
-	int fd;
-
 	if (!path_exist(_PATH_SYS_CPU_RESCAN))
 		errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
-	fd = path_open(O_WRONLY, _PATH_SYS_CPU_RESCAN);
-	if (write(fd, "1", 1) == -1)
+	if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1)
 		err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
-	close(fd);
 	return EXIT_SUCCESS;
 }
 
 static int cpu_set_dispatch(int mode)
 {
-	int fd;
-
 	if (!path_exist(_PATH_SYS_CPU_DISPATCH))
 		errx(EXIT_FAILURE, _("This system does not support setting "
 				     "the dispatching mode of CPUs"));
-	fd = path_open(O_WRONLY, _PATH_SYS_CPU_DISPATCH);
 	if (mode == 0) {
-		if (write(fd, "0", 1) == -1)
+		if (path_writestr("0", _PATH_SYS_CPU_DISPATCH) == -1)
 			err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode"));
 		printf(_("Succesfully set horizontal dispatching mode\n"));
 	} else {
-		if (write(fd, "1", 1) == -1)
+		if (path_writestr("1", _PATH_SYS_CPU_DISPATCH) == -1)
 			err(EXIT_FAILURE, _("Failed to set vertical dispatch mode"));
 		printf(_("Succesfully set vertical dispatching mode\n"));
 	}
-	close(fd);
 	return EXIT_SUCCESS;
 }
 
 static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
 {
 	unsigned int cpu;
-	int fd, rc;
-	char c;
+	int rc, current;
 
 	for (cpu = 0; cpu < setsize; cpu++) {
 		if (!CPU_ISSET(cpu, cpu_set))
@@ -179,33 +141,30 @@ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
 			printf(_("CPU %d is not configurable\n"), cpu);
 			continue;
 		}
-		fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/configure", cpu);
-		if (read(fd, &c, 1) == -1)
-			err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
-		if ((c == '1') && (configure == 1)) {
+		current = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
+		if ((current == 1) && (configure == 1)) {
 			printf(_("CPU %d is already configured\n"), cpu);
 			continue;
 		}
-		if ((c == '0') && (configure == 0)) {
+		if ((current == 0) && (configure == 0)) {
 			printf(_("CPU %d is already deconfigured\n"), cpu);
 			continue;
 		}
 		if (configure) {
-			rc = write(fd, "1", 1);
+			rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
 			if (rc == -1)
 				printf(_("CPU %d configure failed (%s)\n"), cpu,
 					strerror(errno));
 			else
 				printf(_("CPU %d configured\n"), cpu);
 		} else {
-			rc = write(fd, "0", 1);
+			rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/configure", cpu);
 			if (rc == -1)
 				printf(_("CPU %d deconfigure failed (%s)\n"), cpu,
 					strerror(errno));
 			else
 				printf(_("CPU %d deconfigured\n"), cpu);
 		}
-		close(fd);
 	}
 	return EXIT_SUCCESS;
 }
-- 
1.7.5.4

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

* [PATCH 3/6] chcpu: provide better user feedback
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
  2011-09-15  6:52 ` [PATCH 1/6] lib,path: move path access functions from lscpu into lib/path.c Heiko Carstens
  2011-09-15  6:52 ` [PATCH 2/6] chcpu: convert to use lib/path.c Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-15  6:52 ` [PATCH 4/6] lscpu: fix -e output Heiko Carstens
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Instead of printing error messages like "I/O resource busy" which are
supplied by strerror, give better feedback if the reason of failure
is known.
E.g. taking the last cpu offline cannot succeed, therefore print a
message that gives this "hint".

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 sys-utils/chcpu.c |   51 ++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/sys-utils/chcpu.c b/sys-utils/chcpu.c
index d4a2510..2cf015f 100644
--- a/sys-utils/chcpu.c
+++ b/sys-utils/chcpu.c
@@ -42,9 +42,16 @@
 #include "path.h"
 
 #define _PATH_SYS_CPU		"/sys/devices/system/cpu"
+#define _PATH_SYS_CPU_ONLINE	_PATH_SYS_CPU "/online"
 #define _PATH_SYS_CPU_RESCAN	_PATH_SYS_CPU "/rescan"
 #define _PATH_SYS_CPU_DISPATCH	_PATH_SYS_CPU "/dispatching"
 
+static cpu_set_t *onlinecpus;
+static size_t maxcpus;
+
+#define is_cpu_online(cpu) (CPU_ISSET_S((cpu), CPU_ALLOC_SIZE(maxcpus), onlinecpus))
+#define num_online_cpus()  (CPU_COUNT_S(CPU_ALLOC_SIZE(maxcpus), onlinecpus))
+
 enum {
 	CMD_CPU_ENABLE	= 0,
 	CMD_CPU_DISABLE,
@@ -59,6 +66,7 @@ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
 {
 	unsigned int cpu;
 	int online, rc;
+	int configured = -1;
 
 	for (cpu = 0; cpu < setsize; cpu++) {
 		if (!CPU_ISSET(cpu, cpu_set))
@@ -80,20 +88,33 @@ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
 			printf(_("CPU %d is already disabled\n"), cpu);
 			continue;
 		}
+		if (path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu))
+			configured = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
 		if (enable) {
 			rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
-			if (rc == -1)
+			if ((rc == -1) && (configured == 0))
+				printf(_("CPU %d enable failed "
+					 "(CPU is deconfigured)\n"), cpu);
+			else if (rc == -1)
 				printf(_("CPU %d enable failed (%s)\n"), cpu,
 					strerror(errno));
 			else
 				printf(_("CPU %d enabled\n"), cpu);
 		} else {
+			if (onlinecpus && num_online_cpus() == 1) {
+				printf(_("CPU %d disable failed "
+					 "(last enabled CPU)\n"), cpu);
+				continue;
+			}
 			rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
 			if (rc == -1)
 				printf(_("CPU %d disable failed (%s)\n"), cpu,
 					strerror(errno));
-			else
+			else {
 				printf(_("CPU %d disabled\n"), cpu);
+				if (onlinecpus)
+					CPU_CLR(cpu, onlinecpus);
+			}
 		}
 	}
 	return EXIT_SUCCESS;
@@ -105,6 +126,7 @@ static int cpu_rescan(void)
 		errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
 	if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1)
 		err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
+	printf(_("Triggered rescan of CPUs\n"));
 	return EXIT_SUCCESS;
 }
 
@@ -150,6 +172,12 @@ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
 			printf(_("CPU %d is already deconfigured\n"), cpu);
 			continue;
 		}
+		if ((current == 1) && (configure == 0) && onlinecpus &&
+		    is_cpu_online(cpu)) {
+			printf(_("CPU %d deconfigure failed "
+				 "(CPU is enabled)\n"), cpu);
+			continue;
+		}
 		if (configure) {
 			rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
 			if (rc == -1)
@@ -203,7 +231,6 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 int main(int argc, char *argv[])
 {
 	cpu_set_t *cpu_set;
-	unsigned int ncpus;
 	size_t setsize;
 	int cmd = -1;
 	int c;
@@ -224,11 +251,13 @@ int main(int argc, char *argv[])
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	ncpus = get_max_number_of_cpus();
-	if (ncpus <= 0)
+	maxcpus = get_max_number_of_cpus();
+	if (maxcpus <= 0)
 		errx(EXIT_FAILURE, _("cannot determine NR_CPUS; aborting"));
-	setsize = CPU_ALLOC_SIZE(ncpus);
-	cpu_set = CPU_ALLOC(ncpus);
+	if (path_exist(_PATH_SYS_CPU_ONLINE))
+		onlinecpus = path_cpulist(maxcpus, _PATH_SYS_CPU_ONLINE);
+	setsize = CPU_ALLOC_SIZE(maxcpus);
+	cpu_set = CPU_ALLOC(maxcpus);
 	if (!cpu_set)
 		err(EXIT_FAILURE, _("cpuset_alloc failed"));
 
@@ -282,13 +311,13 @@ int main(int argc, char *argv[])
 
 	switch (cmd) {
 	case CMD_CPU_ENABLE:
-		return cpu_enable(cpu_set, ncpus, 1);
+		return cpu_enable(cpu_set, maxcpus, 1);
 	case CMD_CPU_DISABLE:
-		return cpu_enable(cpu_set, ncpus, 0);
+		return cpu_enable(cpu_set, maxcpus, 0);
 	case CMD_CPU_CONFIGURE:
-		return cpu_configure(cpu_set, ncpus, 1);
+		return cpu_configure(cpu_set, maxcpus, 1);
 	case CMD_CPU_DECONFIGURE:
-		return cpu_configure(cpu_set, ncpus, 0);
+		return cpu_configure(cpu_set, maxcpus, 0);
 	case CMD_CPU_RESCAN:
 		return cpu_rescan();
 	case CMD_CPU_DISPATCH_HORIZONTAL:
-- 
1.7.5.4

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

* [PATCH 4/6] lscpu: fix -e output
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
                   ` (2 preceding siblings ...)
  2011-09-15  6:52 ` [PATCH 3/6] chcpu: provide better user feedback Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-15  6:52 ` [PATCH 5/6] lscpu: add --offline option Heiko Carstens
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

The modifier mod->allcpus must be set earlier and also must be used
earlier. The current code only reads sysfs attributes from online
cpus but skips offline cpus.
So initialize mod->allcpus earlier.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 sys-utils/lscpu.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 78d936f..63377d1 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -1266,11 +1266,13 @@ int main(int argc, char *argv[])
 			usage(stderr);
 		}
 	}
+	if (mod->mode == OUTPUT_READABLE && !mod->online)
+		mod->allcpus = 1;
 
 	read_basicinfo(desc, mod);
 
 	for (i = 0; i < desc->ncpus; i++) {
-		if (desc->online && !is_cpu_online(desc, i))
+		if (desc->online && !is_cpu_online(desc, i) && !mod->allcpus)
 			continue;
 		read_topology(desc, i);
 		read_cache(desc, i);
@@ -1300,8 +1302,6 @@ int main(int argc, char *argv[])
 		print_parsable(desc, columns, ncolumns, mod);
 		break;
 	case OUTPUT_READABLE:
-		if (!mod->online)
-			mod->allcpus = 1;
 		if (!ncolumns) {
 			/* No list was given. Just print whatever is there. */
 			columns[ncolumns++] = COL_CPU;
-- 
1.7.5.4

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

* [PATCH 5/6] lscpu: add --offline option
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
                   ` (3 preceding siblings ...)
  2011-09-15  6:52 ` [PATCH 4/6] lscpu: fix -e output Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-15  6:52 ` [PATCH 6/6] lscpu: stricter command line parsing Heiko Carstens
  2011-09-27 13:00 ` [PATCH 0/6] more lscpu and chcpu changes Karel Zak
  6 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Implement "--offline" option which only prints offline cpus. As a side effect
we can get rid of the internal "allcpus" flag, since if we want to print
informations for online and offline cpus we simply set both flags.

When reading sysfs attributes of cpus this is now done for all cpus, since
e.g. the topology informations of the online cpus may influence the
topology informations of the offline cpus. This mainly because online cpus
may contain masks which include offline cpus while offline cpus have a
missing topology directory.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 sys-utils/lscpu.1 |    9 ++++++---
 sys-utils/lscpu.c |   34 ++++++++++++++++++++++------------
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/sys-utils/lscpu.1 b/sys-utils/lscpu.1
index b073a22..47c4c31 100644
--- a/sys-utils/lscpu.1
+++ b/sys-utils/lscpu.1
@@ -6,7 +6,7 @@
 lscpu \- display information on CPU architecture
 .SH SYNOPSIS
 .B lscpu
-.RB [ \-aehpxV ]
+.RB [ \-abcehpxV ]
 .RB [ \-s
 .IR directory ]
 .SH DESCRIPTION
@@ -28,10 +28,13 @@ the defined order.
 .SH OPTIONS
 .TP
 .BR \-a , " \-\-all"
-Include online and offline CPUs in output (default for -e)
+Include online and offline CPUs in output (default for -e).
 .TP
 .BR \-b , " \-\-online"
-Include only online CPUs in output (default for -p)
+Include only online CPUs in output (default for -p).
+.TP
+.BR \-c , " \-\-offline"
+Include only offline CPUs in output.
 .TP
 .BR \-e , " \-\-extended " \fI[=list]\fP
 Print CPU list out in human-readable format.
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 63377d1..31c8fa8 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -195,8 +195,8 @@ struct lscpu_modifier {
 	int		system;		/* SYSTEM_* */
 	unsigned int	hex:1,		/* print CPU masks rather than CPU lists */
 			compat:1,	/* use backwardly compatible format */
-			allcpus:1,	/* print all CPUs */
-			online:1;	/* print online CPUs only */
+			online:1,	/* print online CPUs */
+			offline:1;	/* print offline CPUs */
 };
 
 static int maxcpus;		/* size in bits of kernel cpu mask */
@@ -957,7 +957,9 @@ print_parsable(struct lscpu_desc *desc, int cols[], int ncols,
 	for (i = 0; i < desc->ncpus; i++) {
 		int c;
 
-		if (!mod->allcpus && desc->online && !is_cpu_online(desc, i))
+		if (!mod->offline && desc->online && !is_cpu_online(desc, i))
+			continue;
+		if (!mod->online && desc->online && is_cpu_online(desc, i))
 			continue;
 		for (c = 0; c < ncols; c++) {
 			if (mod->compat && cols[c] == COL_CACHE) {
@@ -1003,7 +1005,9 @@ print_readable(struct lscpu_desc *desc, int cols[], int ncols,
 		int c;
 		struct tt_line *line;
 
-		if (!mod->allcpus && desc->online && !is_cpu_online(desc, i))
+		if (!mod->offline && desc->online && !is_cpu_online(desc, i))
+			continue;
+		if (!mod->online && desc->online && is_cpu_online(desc, i))
 			continue;
 
 		line = tt_add_line(tt, NULL);
@@ -1187,6 +1191,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 	fputs(_("\nOptions:\n"), out);
 	fputs(_(" -a, --all               print online and offline CPUs (default for -e)\n"
 		" -b, --online            print online CPUs only (default for -p)\n"
+		" -c, --offline           print offline CPUs only\n"
 		" -e, --extended[=<list>] print out a extended readable format\n"
 		" -h, --help              print this help\n"
 		" -p, --parse[=<list>]    print out a parsable format\n"
@@ -1207,6 +1212,7 @@ int main(int argc, char *argv[])
 	static const struct option longopts[] = {
 		{ "all",        no_argument,       0, 'a' },
 		{ "online",     no_argument,       0, 'b' },
+		{ "offline",    no_argument,       0, 'c' },
 		{ "help",	no_argument,       0, 'h' },
 		{ "extended",	optional_argument, 0, 'e' },
 		{ "parse",	optional_argument, 0, 'p' },
@@ -1220,22 +1226,25 @@ int main(int argc, char *argv[])
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	while ((c = getopt_long(argc, argv, "abe::hp::s:xV", longopts, NULL)) != -1) {
+	while ((c = getopt_long(argc, argv, "abce::hp::s:xV", longopts, NULL)) != -1) {
 
 		if (mod->mode != OUTPUT_SUMMARY && strchr("ep", c))
 			errx(EXIT_FAILURE,
 			     _("extended and parsable formats are mutually exclusive"));
-		if ((mod->allcpus || mod->online) && strchr("ab", c))
+		if ((mod->online || mod->offline) && strchr("abc", c))
 			errx(EXIT_FAILURE,
-			     _("--all and --online options are mutually exclusive"));
+			     _("--all, --online and --offline options are mutually exclusive"));
 
 		switch (c) {
 		case 'a':
-			mod->allcpus = 1;
+			mod->online = mod->offline = 1;
 			break;
 		case 'b':
 			mod->online = 1;
 			break;
+		case 'c':
+			mod->offline = 1;
+			break;
 		case 'h':
 			usage(stdout);
 		case 'p':
@@ -1266,14 +1275,15 @@ int main(int argc, char *argv[])
 			usage(stderr);
 		}
 	}
-	if (mod->mode == OUTPUT_READABLE && !mod->online)
-		mod->allcpus = 1;
+	/* set default cpu display mode if none was specified */
+	if (!mod->online && !mod->offline) {
+		mod->online = 1;
+		mod->offline = mod->mode == OUTPUT_READABLE ? 1 : 0;
+	}
 
 	read_basicinfo(desc, mod);
 
 	for (i = 0; i < desc->ncpus; i++) {
-		if (desc->online && !is_cpu_online(desc, i) && !mod->allcpus)
-			continue;
 		read_topology(desc, i);
 		read_cache(desc, i);
 		read_polarization(desc, i);
-- 
1.7.5.4

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

* [PATCH 6/6] lscpu: stricter command line parsing
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
                   ` (4 preceding siblings ...)
  2011-09-15  6:52 ` [PATCH 5/6] lscpu: add --offline option Heiko Carstens
@ 2011-09-15  6:52 ` Heiko Carstens
  2011-09-27 13:00 ` [PATCH 0/6] more lscpu and chcpu changes Karel Zak
  6 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-15  6:52 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Heiko Carstens

Disallow superfluous commands for lscpu like e.g. "lscpu bla" and let it
fail print the help text instead.

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

diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 31c8fa8..f21af71 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -1275,6 +1275,10 @@ int main(int argc, char *argv[])
 			usage(stderr);
 		}
 	}
+
+	if (argc != optind)
+		usage(stderr);
+
 	/* set default cpu display mode if none was specified */
 	if (!mod->online && !mod->offline) {
 		mod->online = 1;
-- 
1.7.5.4

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

* Re: [PATCH 2/6] chcpu: convert to use lib/path.c
  2011-09-15  6:52 ` [PATCH 2/6] chcpu: convert to use lib/path.c Heiko Carstens
@ 2011-09-16  8:28   ` Heiko Carstens
  0 siblings, 0 replies; 9+ messages in thread
From: Heiko Carstens @ 2011-09-16  8:28 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux

>  int
> +path_writestr(const char *str, const char *path, ...)
> +{
> +	int fd, result;
> +	va_list ap;
> +
> +	va_start(ap, path);
> +	fd = path_vopen(O_WRONLY, path, ap);
> +	va_end(ap);
> +	result = write(fd, str, strlen(str));
> +	close(fd);
> +	return result;
> +}

Hmm, this is not ok. The patch below is needed on top. Sorry...


Subject: [PATCH] lib,path: use write_all()

From: Heiko Carstens <heiko.carstens@de.ibm.com>

Since write() doesn't necessarily write the complete buffer with
one call we better use write_all() which takes care of this.

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

diff --git a/lib/path.c b/lib/path.c
index e2bb398..54b8942 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include "writeall.h"
 #include "cpuset.h"
 #include "path.h"
 #include "nls.h"
@@ -132,7 +133,7 @@ path_writestr(const char *str, const char *path, ...)
 	va_start(ap, path);
 	fd = path_vopen(O_WRONLY, path, ap);
 	va_end(ap);
-	result = write(fd, str, strlen(str));
+	result = write_all(fd, str, strlen(str));
 	close(fd);
 	return result;
 }
-- 
1.7.5.4

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

* Re: [PATCH 0/6] more lscpu and chcpu changes
  2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
                   ` (5 preceding siblings ...)
  2011-09-15  6:52 ` [PATCH 6/6] lscpu: stricter command line parsing Heiko Carstens
@ 2011-09-27 13:00 ` Karel Zak
  6 siblings, 0 replies; 9+ messages in thread
From: Karel Zak @ 2011-09-27 13:00 UTC (permalink / raw)
  To: Heiko Carstens; +Cc: util-linux

On Thu, Sep 15, 2011 at 08:52:28AM +0200, Heiko Carstens wrote:
>  include/path.h        |   17 ++++
>  lib/path.c            |  217 ++++++++++++++++++++++++++++++++++++++++++++
>  sys-utils/Makefile.am |    6 +-
>  sys-utils/chcpu.c     |  122 +++++++++++--------------
>  sys-utils/lscpu.1     |    9 ++-
>  sys-utils/lscpu.c     |  237 ++++++++++---------------------------------------
>  6 files changed, 348 insertions(+), 260 deletions(-)
>  create mode 100644 include/path.h
>  create mode 100644 lib/path.c

 Applied, thanks.

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

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

end of thread, other threads:[~2011-09-27 13:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-15  6:52 [PATCH 0/6] more lscpu and chcpu changes Heiko Carstens
2011-09-15  6:52 ` [PATCH 1/6] lib,path: move path access functions from lscpu into lib/path.c Heiko Carstens
2011-09-15  6:52 ` [PATCH 2/6] chcpu: convert to use lib/path.c Heiko Carstens
2011-09-16  8:28   ` Heiko Carstens
2011-09-15  6:52 ` [PATCH 3/6] chcpu: provide better user feedback Heiko Carstens
2011-09-15  6:52 ` [PATCH 4/6] lscpu: fix -e output Heiko Carstens
2011-09-15  6:52 ` [PATCH 5/6] lscpu: add --offline option Heiko Carstens
2011-09-15  6:52 ` [PATCH 6/6] lscpu: stricter command line parsing Heiko Carstens
2011-09-27 13:00 ` [PATCH 0/6] more lscpu and chcpu 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.