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