dcavalca / rpms / util-linux

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