ad9577
From 8f57ecb12194a10f3dbe6eca9f4bf0b18c4be757 Mon Sep 17 00:00:00 2001
ad9577
From: Karel Zak <kzak@redhat.com>
ad9577
Date: Wed, 5 Mar 2014 11:06:59 +0100
ad9577
Subject: [PATCH 170/173] chcpu: cleanup return codes
ad9577
ad9577
The code currently always return EXIT_SUCCESS, that's strange. It
ad9577
seems better to return 0 on success, 1 on complete failure and 64 on
ad9577
partial success.
ad9577
ad9577
Signed-off-by: Karel Zak <kzak@redhat.com>
ad9577
Upstream: http://github.com/karelzak/util-linux/commit/48fc00c1c70f3dbbd8ad6ef423bbba27dd3efb69
ad9577
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1579439
ad9577
---
ad9577
 sys-utils/chcpu.8 | 14 ++++++++++
ad9577
 sys-utils/chcpu.c | 82 ++++++++++++++++++++++++++++++++++++++++---------------
ad9577
 2 files changed, 74 insertions(+), 22 deletions(-)
ad9577
ad9577
diff --git a/sys-utils/chcpu.8 b/sys-utils/chcpu.8
ad9577
index d016b86f2..125d9d2ad 100644
ad9577
--- a/sys-utils/chcpu.8
ad9577
+++ b/sys-utils/chcpu.8
ad9577
@@ -80,6 +80,20 @@ Display help information and exit.
ad9577
 .TP
ad9577
 .BR \-V , " \-\-version"
ad9577
 Display version information and exit.
ad9577
+
ad9577
+.SH RETURN CODES
ad9577
+.B chcpu
ad9577
+has the following return codes:
ad9577
+.TP
ad9577
+.BR 0
ad9577
+success
ad9577
+.TP
ad9577
+.BR 1
ad9577
+failure
ad9577
+.TP
ad9577
+.BR 64
ad9577
+partial success
ad9577
+.RE
ad9577
 .SH AUTHOR
ad9577
 .MT heiko.carstens@de.ibm.com
ad9577
 Heiko Carstens
ad9577
diff --git a/sys-utils/chcpu.c b/sys-utils/chcpu.c
ad9577
index 1162888d5..304b80d7a 100644
ad9577
--- a/sys-utils/chcpu.c
ad9577
+++ b/sys-utils/chcpu.c
ad9577
@@ -45,6 +45,9 @@
ad9577
 
ad9577
 #define EXCL_ERROR "--{configure,deconfigure,disable,dispatch,enable}"
ad9577
 
ad9577
+/* partial success, otherwise we return regular EXIT_{SUCCESS,FAILURE} */
ad9577
+#define CHCPU_EXIT_SOMEOK	64
ad9577
+
ad9577
 #define _PATH_SYS_CPU		"/sys/devices/system/cpu"
ad9577
 #define _PATH_SYS_CPU_ONLINE	_PATH_SYS_CPU "/online"
ad9577
 #define _PATH_SYS_CPU_RESCAN	_PATH_SYS_CPU "/rescan"
ad9577
@@ -66,21 +69,28 @@ enum {
ad9577
 	CMD_CPU_DISPATCH_VERTICAL,
ad9577
 };
ad9577
 
ad9577
+/* returns:   0 = success
ad9577
+ *          < 0 = failure
ad9577
+ *          > 0 = partial success
ad9577
+ */
ad9577
 static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
ad9577
 {
ad9577
 	unsigned int cpu;
ad9577
 	int online, rc;
ad9577
 	int configured = -1;
ad9577
+	size_t fails = 0;
ad9577
 
ad9577
 	for (cpu = 0; cpu < setsize; cpu++) {
ad9577
 		if (!CPU_ISSET(cpu, cpu_set))
ad9577
 			continue;
ad9577
 		if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) {
ad9577
 			printf(_("CPU %d does not exist\n"), cpu);
ad9577
+			fails++;
ad9577
 			continue;
ad9577
 		}
ad9577
 		if (!path_exist(_PATH_SYS_CPU "/cpu%d/online", cpu)) {
ad9577
 			printf(_("CPU %d is not hot pluggable\n"), cpu);
ad9577
+			fails++;
ad9577
 			continue;
ad9577
 		}
ad9577
 		online = path_read_s32(_PATH_SYS_CPU "/cpu%d/online", cpu);
ad9577
@@ -96,30 +106,35 @@ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
ad9577
 			configured = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu);
ad9577
 		if (enable) {
ad9577
 			rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
ad9577
-			if ((rc == -1) && (configured == 0))
ad9577
+			if ((rc == -1) && (configured == 0)) {
ad9577
 				warnx(_("CPU %d enable failed "
ad9577
 					 "(CPU is deconfigured)"), cpu);
ad9577
-			else if (rc == -1)
ad9577
+				fails++;
ad9577
+			} else if (rc == -1) {
ad9577
 				warn(_("CPU %d enable failed"), cpu);
ad9577
-			else
ad9577
+				fails++;
ad9577
+			} else
ad9577
 				printf(_("CPU %d enabled\n"), cpu);
ad9577
 		} else {
ad9577
 			if (onlinecpus && num_online_cpus() == 1) {
ad9577
 				printf(_("CPU %d disable failed "
ad9577
 					 "(last enabled CPU)\n"), cpu);
ad9577
+				fails++;
ad9577
 				continue;
ad9577
 			}
ad9577
 			rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
ad9577
-			if (rc == -1)
ad9577
+			if (rc == -1) {
ad9577
 				warn(_("CPU %d disable failed"), cpu);
ad9577
-			else {
ad9577
+				fails++;
ad9577
+			} else {
ad9577
 				printf(_("CPU %d disabled\n"), cpu);
ad9577
 				if (onlinecpus)
ad9577
 					CPU_CLR(cpu, onlinecpus);
ad9577
 			}
ad9577
 		}
ad9577
 	}
ad9577
-	return EXIT_SUCCESS;
ad9577
+
ad9577
+	return fails == 0 ? 0 : fails == setsize ? -1 : 1;
ad9577
 }
ad9577
 
ad9577
 static int cpu_rescan(void)
ad9577
@@ -129,7 +144,7 @@ static int cpu_rescan(void)
ad9577
 	if (path_write_str("1", _PATH_SYS_CPU_RESCAN) == -1)
ad9577
 		err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
ad9577
 	printf(_("Triggered rescan of CPUs\n"));
ad9577
-	return EXIT_SUCCESS;
ad9577
+	return 0;
ad9577
 }
ad9577
 
ad9577
 static int cpu_set_dispatch(int mode)
ad9577
@@ -146,23 +161,30 @@ static int cpu_set_dispatch(int mode)
ad9577
 			err(EXIT_FAILURE, _("Failed to set vertical dispatch mode"));
ad9577
 		printf(_("Successfully set vertical dispatching mode\n"));
ad9577
 	}
ad9577
-	return EXIT_SUCCESS;
ad9577
+	return 0;
ad9577
 }
ad9577
 
ad9577
+/* returns:   0 = success
ad9577
+ *          < 0 = failure
ad9577
+ *          > 0 = partial success
ad9577
+ */
ad9577
 static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
ad9577
 {
ad9577
 	unsigned int cpu;
ad9577
 	int rc, current;
ad9577
+	size_t fails = 0;
ad9577
 
ad9577
 	for (cpu = 0; cpu < setsize; cpu++) {
ad9577
 		if (!CPU_ISSET(cpu, cpu_set))
ad9577
 			continue;
ad9577
 		if (!path_exist(_PATH_SYS_CPU "/cpu%d", cpu)) {
ad9577
 			printf(_("CPU %d does not exist\n"), cpu);
ad9577
+			fails++;
ad9577
 			continue;
ad9577
 		}
ad9577
 		if (!path_exist(_PATH_SYS_CPU "/cpu%d/configure", cpu)) {
ad9577
 			printf(_("CPU %d is not configurable\n"), cpu);
ad9577
+			fails++;
ad9577
 			continue;
ad9577
 		}
ad9577
 		current = path_read_s32(_PATH_SYS_CPU "/cpu%d/configure", cpu);
ad9577
@@ -178,23 +200,27 @@ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
ad9577
 		    is_cpu_online(cpu)) {
ad9577
 			printf(_("CPU %d deconfigure failed "
ad9577
 				 "(CPU is enabled)\n"), cpu);
ad9577
+			fails++;
ad9577
 			continue;
ad9577
 		}
ad9577
 		if (configure) {
ad9577
 			rc = path_write_str("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
ad9577
-			if (rc == -1)
ad9577
+			if (rc == -1) {
ad9577
 				warn(_("CPU %d configure failed"), cpu);
ad9577
-			else
ad9577
+				fails++;
ad9577
+			} else
ad9577
 				printf(_("CPU %d configured\n"), cpu);
ad9577
 		} else {
ad9577
 			rc = path_write_str("0", _PATH_SYS_CPU "/cpu%d/configure", cpu);
ad9577
-			if (rc == -1)
ad9577
+			if (rc == -1) {
ad9577
 				warn(_("CPU %d deconfigure failed"), cpu);
ad9577
-			else
ad9577
+				fails++;
ad9577
+			} else
ad9577
 				printf(_("CPU %d deconfigured\n"), cpu);
ad9577
 		}
ad9577
 	}
ad9577
-	return EXIT_SUCCESS;
ad9577
+
ad9577
+	return fails == 0 ? 0 : fails == setsize ? -1 : 1;
ad9577
 }
ad9577
 
ad9577
 static void cpu_parse(char *cpu_string, cpu_set_t *cpu_set, size_t setsize)
ad9577
@@ -233,7 +259,7 @@ int main(int argc, char *argv[])
ad9577
 	cpu_set_t *cpu_set;
ad9577
 	size_t setsize;
ad9577
 	int cmd = -1;
ad9577
-	int c;
ad9577
+	int c, rc;
ad9577
 
ad9577
 	static const struct option longopts[] = {
ad9577
 		{ "configure",	required_argument, 0, 'c' },
ad9577
@@ -317,19 +343,31 @@ int main(int argc, char *argv[])
ad9577
 
ad9577
 	switch (cmd) {
ad9577
 	case CMD_CPU_ENABLE:
ad9577
-		return cpu_enable(cpu_set, maxcpus, 1);
ad9577
+		rc = cpu_enable(cpu_set, maxcpus, 1);
ad9577
+		break;
ad9577
 	case CMD_CPU_DISABLE:
ad9577
-		return cpu_enable(cpu_set, maxcpus, 0);
ad9577
+		rc = cpu_enable(cpu_set, maxcpus, 0);
ad9577
+		break;
ad9577
 	case CMD_CPU_CONFIGURE:
ad9577
-		return cpu_configure(cpu_set, maxcpus, 1);
ad9577
+		rc = cpu_configure(cpu_set, maxcpus, 1);
ad9577
+		break;
ad9577
 	case CMD_CPU_DECONFIGURE:
ad9577
-		return cpu_configure(cpu_set, maxcpus, 0);
ad9577
+		rc = cpu_configure(cpu_set, maxcpus, 0);
ad9577
+		break;
ad9577
 	case CMD_CPU_RESCAN:
ad9577
-		return cpu_rescan();
ad9577
+		rc = cpu_rescan();
ad9577
+		break;
ad9577
 	case CMD_CPU_DISPATCH_HORIZONTAL:
ad9577
-		return cpu_set_dispatch(0);
ad9577
+		rc = cpu_set_dispatch(0);
ad9577
+		break;
ad9577
 	case CMD_CPU_DISPATCH_VERTICAL:
ad9577
-		return cpu_set_dispatch(1);
ad9577
+		rc = cpu_set_dispatch(1);
ad9577
+		break;
ad9577
+	default:
ad9577
+		rc = -EINVAL;
ad9577
+		break;
ad9577
 	}
ad9577
-	return EXIT_SUCCESS;
ad9577
+
ad9577
+	return rc == 0 ? EXIT_SUCCESS :
ad9577
+	        rc < 0 ? EXIT_FAILURE : CHCPU_EXIT_SOMEOK;
ad9577
 }
ad9577
-- 
ad9577
2.14.4
ad9577