Add print_result to logging

Simplify PASS|FAIL|ERROR reporting for functional tests using a new
print_result() routine.

Signed-off-by: Darren Hart <dvhltc@us.ibm.com>
diff --git a/functional/futex_requeue_pi_mismatched_ops.c b/functional/futex_requeue_pi_mismatched_ops.c
index 10633a7..7e9ed61 100644
--- a/functional/futex_requeue_pi_mismatched_ops.c
+++ b/functional/futex_requeue_pi_mismatched_ops.c
@@ -67,8 +67,8 @@
 
 int main(int argc, char *argv[])
 {
+	int ret = RET_PASS;
 	pthread_t child;
-	int ret = 0;
 	int c;
 
 	while ((c = getopt(argc, argv, "chv:")) != -1) {
@@ -91,9 +91,9 @@
 	printf("%s: Detect mismatched requeue_pi operations\n",
 	       basename(argv[0]));
 
-	ret = pthread_create(&child, NULL, blocking_child, NULL);
-	if (ret) {
+	if (pthread_create(&child, NULL, blocking_child, NULL)) {
 		error("pthread_create\n", errno);
+		ret = RET_ERROR;
 		goto out;
 	}
 	/* Allow the child to block in the kernel. */
@@ -107,7 +107,6 @@
 	 */
 	ret = futex_cmp_requeue_pi(&f1, f1, &f2, 1, 0, FUTEX_PRIVATE_FLAG);
 	if (ret < 0) {
-		ret = -errno;
 		if (ret == -EINVAL) {
 			/* 
 			 * The kernel correctly detected the mismatched
@@ -116,21 +115,24 @@
 			 */
 			ret = futex_wake(&f1, 1, FUTEX_PRIVATE_FLAG);
 			if (ret == 1)
-				ret = 0;
-			else if (ret < 0)
+				ret = RET_PASS;
+			else if (ret < 0) {
 				error("futex_wake\n", errno);
-			else {
+				ret = RET_ERROR;
+			} else {
 				error("futex_wake did not wake the child\n", 0);
-				ret = -1;
+				ret = RET_ERROR;
 			}
 		} else {
 			error("futex_cmp_requeue_pi\n", errno);
+			ret = RET_ERROR;
 		}
 	} else if (ret > 0) {
 		fail("futex_cmp_requeue_pi failed to detect the mismatch\n");
+		ret = RET_FAIL;
 	} else {
 		error("futex_cmp_requeue_pi found no waiters\n", 0);
-		ret = -1;
+		ret = RET_ERROR;
 	}
 
 	pthread_join(child, NULL);
@@ -141,6 +143,6 @@
 
  out:
 	/* If the kernel crashes, we shouldn't return at all. */
-	printf("Result: %s\n", ret == 0 ? PASS : FAIL);
+	print_result(ret);
 	return ret;
 }
diff --git a/functional/futex_requeue_pi_signal_restart.c b/functional/futex_requeue_pi_signal_restart.c
index febcd3d..d0c9379 100644
--- a/functional/futex_requeue_pi_signal_restart.c
+++ b/functional/futex_requeue_pi_signal_restart.c
@@ -50,6 +50,8 @@
 	struct timespec *timeout;
 } waiter_arg_t;
 
+int waiter_ret = 0;
+
 void usage(char *prog)
 {
 	printf("Usage: %s\n", prog);
@@ -99,27 +101,28 @@
 void *waiterfn(void *arg)
 {
 	unsigned int old_val;
-	int ret;
+	int res;
+	waiter_ret = RET_PASS;
 
 	info("Waiter running\n"); 
 
 	info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
 	/* cond_wait */
 	old_val = f1;
-	ret = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL, FUTEX_PRIVATE_FLAG);
-	if (ret < 0) {
-		ret = -errno;
+	res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL, FUTEX_PRIVATE_FLAG);
+	if (res < 0) {
 		error("waiterfn\n", errno);
+		waiter_ret = RET_ERROR;
 	}
-	info("FUTEX_WAIT_REQUEUE_PI returned: %d\n", ret);
+	info("FUTEX_WAIT_REQUEUE_PI returned: %d\n", res);
 	info("w1:futex: %x\n", f2);
-	if (ret)
+	if (res)
 		futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG);
 	futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
 
-	info("Waiter exiting with %d\n", ret);
+	info("Waiter exiting with %d\n", waiter_ret);
 	info("w2:futex: %x\n", f2);
-	return (void*)(long)ret;
+	pthread_exit(NULL);
 }
 
 
@@ -128,7 +131,7 @@
 	unsigned int old_val;
 	struct sigaction sa;
 	pthread_t waiter;
-	int c, ret = 0;
+	int c, res, ret = RET_PASS;
 
 	while ((c = getopt(argc, argv, "chv:")) != -1) {
 		switch(c) {
@@ -160,9 +163,10 @@
 
 	info("m1:futex: %x\n", f2);
 	info("Creating waiter\n");
-	if ((ret = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1))) {
-		error("Creating waiting thread failed", ret);
-		exit(1);
+	if ((res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1))) {
+		error("Creating waiting thread failed", res);
+		ret = RET_ERROR;
+		goto out;
 	}
 	info("m2:futex: %x\n", f2);
 
@@ -174,13 +178,11 @@
 	info("Waking waiter via FUTEX_CMP_REQUEUE_PI\n");
 	/* cond_signal */
 	old_val = f1;
-	ret = futex_cmp_requeue_pi(&f1, old_val, &(f2),
+	res = futex_cmp_requeue_pi(&f1, old_val, &(f2),
 				   1, 0, FUTEX_PRIVATE_FLAG);
-	if (ret < 0) {
-		ret = -errno;
+	if (res < 0) {
 		error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
-		
-		/* FIXME - do something sane.... */
+		ret = RET_ERROR;
 	}
 	info("m4:futex: %x\n", f2); 
 
@@ -206,6 +208,10 @@
 	pthread_join(waiter, NULL);
 	info("m7:futex: %x\n", f2);
 
-	printf("Result: %s\n", ret ? ERROR : PASS);
+ out:
+	if (ret == RET_PASS && waiter_ret)
+		ret = waiter_ret;
+
+	print_result(ret);
 	return ret;
 }
diff --git a/functional/futex_wait_timeout.c b/functional/futex_wait_timeout.c
index 4892f29..6e3e369 100644
--- a/functional/futex_wait_timeout.c
+++ b/functional/futex_wait_timeout.c
@@ -55,7 +55,7 @@
 {
 	futex_t f1 = FUTEX_INITIALIZER;
 	struct timespec to;
-	int ret = 0;
+	int res, ret = RET_PASS;
 	int c;
 
 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
@@ -86,12 +86,12 @@
 	to.tv_nsec = timeout_ns;
 
 	info("Calling futex_wait on f1: %u @ %p\n", f1, &f1);
-	ret = futex_wait(&f1, f1, &to, FUTEX_PRIVATE_FLAG);
-	if (ret < 0 && errno == ETIMEDOUT)
-		ret = 0;
-	else
+	res = futex_wait(&f1, f1, &to, FUTEX_PRIVATE_FLAG);
+	if (!res || errno != ETIMEDOUT) {
 		fail("futex_wait returned %d\n", ret < 0 ? errno : ret);
+		ret = RET_FAIL;
+	}
 
-	printf("Result: %s\n", ret ? FAIL : PASS);
+	print_result(ret);
 	return ret;
 }
diff --git a/functional/futex_wait_wouldblock.c b/functional/futex_wait_wouldblock.c
index 1326439..310970f 100644
--- a/functional/futex_wait_wouldblock.c
+++ b/functional/futex_wait_wouldblock.c
@@ -21,7 +21,7 @@
  *
  * DESCRIPTION
  *      Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
- *      from the unexpected one.
+ *      from the expected one.
  *
  * AUTHOR
  *      Gowrishankar <gowrishankar.m@in.ibm.com>
@@ -55,7 +55,7 @@
 {
 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
 	futex_t f1 = FUTEX_INITIALIZER;
-	int ret = 0;
+	int res, ret = RET_PASS;
 	int c;
 
 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
@@ -79,19 +79,13 @@
 	       basename(argv[0]));
 
 	info("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
-	ret = futex_wait(&f1, f1, &to, FUTEX_PRIVATE_FLAG);
-	if (ret < 0) {
-		ret = -errno;
-		if (ret == -EWOULDBLOCK)
-			ret = 0;
-		else
-			fail("returned error is not '%s' in FUTEX_WAIT, but '%s'\n", \
-			     strerror(EWOULDBLOCK), strerror(errno));
-	} else {
-		fail("futex_wait returned %d\n", ret);
-		ret = -1;
+	res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
+	if (!res || errno != EWOULDBLOCK) {
+		fail("futex_wait returned: %d %s\n",
+		     res ? errno : res, res ? strerror(errno) : "");
+		ret = RET_FAIL;
 	}
 
-	printf("Result: %s\n", ret ? FAIL : PASS);
+	print_result(ret);
 	return ret;
 }
diff --git a/include/logging.h b/include/logging.h
index 558daf5..9dd68bd 100644
--- a/include/logging.h
+++ b/include/logging.h
@@ -66,6 +66,11 @@
 #define VMAX      VINFO
 int _verbose = VCRITICAL;
 
+/* Functional test return codes */
+#define RET_PASS   0
+#define RET_ERROR -1
+#define RET_FAIL  -2
+
 /**
  * log_color() - Use colored output for PASS, ERROR, and FAIL strings
  * @use_color:	use color (1) or not (0)
@@ -99,7 +104,30 @@
 	_verbose = level;
 }
 
-/* Output macros */
+/**
+ * print_result() - Print standard PASS | ERROR | FAIL results
+ * @ret:	the return value to be considered: 0 | RET_ERROR | RET_FAIL
+ *
+ * print_result() is primarily intended for functional tests.
+ */
+void print_result(int ret)
+{
+	char *result = "Unknown return code";
+	switch (ret) {
+	case RET_PASS:
+		result = PASS;
+		break;
+	case RET_ERROR:
+		result = ERROR;
+		break;
+	case RET_FAIL:
+		result = FAIL;
+		break;
+	}
+	printf("Result: %s\n", result);
+}
+
+/* log level macros */
 #define info(message, vargs...) \
 do { \
 	if (_verbose >= VINFO) \