[PATCH v3 07/10] tests: support waitpid()

Leonid Bobrov mazocomp at disroot.org
Sat Feb 9 07:09:39 UTC 2019


Signed-off-by: Leonid Bobrov <mazocomp at disroot.org>
---
 configure.ac            |  4 ++++
 tests/test-compositor.c | 25 +++++++++++++++++++++++--
 tests/test-runner.c     | 29 ++++++++++++++++++++++++++++-
 3 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8563968..3c227a2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -98,6 +98,10 @@ RT_LIBS="$LIBS"
 LIBS="$SAVE_LIBS"
 AC_SUBST(RT_LIBS)
 
+# waitid() and signal.h are needed for the test suite.
+AC_CHECK_FUNCS([waitid])
+AC_CHECK_HEADERS([signal.h])
+
 AC_ARG_ENABLE([libraries],
 	      [AC_HELP_STRING([--disable-libraries],
 			      [Disable compilation of wayland libraries])],
diff --git a/tests/test-compositor.c b/tests/test-compositor.c
index 72f6351..6e12630 100644
--- a/tests/test-compositor.c
+++ b/tests/test-compositor.c
@@ -23,6 +23,8 @@
  * SOFTWARE.
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
@@ -86,8 +88,8 @@ get_socket_name(void)
 	static char retval[64];
 
 	gettimeofday(&tv, NULL);
-	snprintf(retval, sizeof retval, "wayland-test-%d-%ld%ld",
-		 getpid(), tv.tv_sec, tv.tv_usec);
+	snprintf(retval, sizeof retval, "wayland-test-%d-%lld%lld",
+		 getpid(), (long long)tv.tv_sec, (long long)tv.tv_usec);
 
 	return retval;
 }
@@ -97,10 +99,15 @@ handle_client_destroy(void *data)
 {
 	struct client_info *ci = data;
 	struct display *d;
+#ifdef HAVE_WAITID
 	siginfo_t status;
+#else
+	int istatus;
+#endif
 
 	d = ci->display;
 
+#ifdef HAVE_WAITID
 	assert(waitid(P_PID, ci->pid, &status, WEXITED) != -1);
 
 	switch (status.si_code) {
@@ -118,6 +125,20 @@ handle_client_destroy(void *data)
 		ci->exit_code = status.si_status;
 		break;
 	}
+#else
+	assert(waitpid(ci->pid, &istatus, WNOHANG) != -1);
+
+	if (WIFSIGNALED(istatus)) {
+		fprintf(stderr, "Client '%s' was killed by signal %d\n",
+			ci->name, WTERMSIG(istatus));
+		ci->exit_code = WEXITSTATUS(istatus);
+	} else if (WIFEXITED(istatus)) {
+		if (WEXITSTATUS(istatus) != EXIT_SUCCESS)
+			fprintf(stderr, "Client '%s' exited with code %d\n",
+				ci->name, WEXITSTATUS(istatus));
+		ci->exit_code = WEXITSTATUS(istatus);
+	}
+#endif
 
 	++d->clients_terminated_no;
 	if (d->clients_no == d->clients_terminated_no) {
diff --git a/tests/test-runner.c b/tests/test-runner.c
index 1487dc4..7fa72eb 100644
--- a/tests/test-runner.c
+++ b/tests/test-runner.c
@@ -23,6 +23,8 @@
  * SOFTWARE.
  */
 
+#include "config.h"
+
 #define _GNU_SOURCE
 
 #include <unistd.h>
@@ -32,6 +34,7 @@
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <string.h>
+#include <signal.h>
 #include <assert.h>
 #include <dlfcn.h>
 #include <errno.h>
@@ -293,7 +296,11 @@ int main(int argc, char *argv[])
 	const struct test *t;
 	pid_t pid;
 	int total, pass;
+#ifdef HAVE_WAITID
 	siginfo_t info;
+#else
+	int status;
+#endif
 
 	if (isatty(fileno(stderr)))
 		is_atty = 1;
@@ -336,7 +343,8 @@ int main(int argc, char *argv[])
 		if (pid == 0)
 			run_test(t); /* never returns */
 
-		if (waitid(P_PID, pid, &info, WEXITED)) {
+#ifdef HAVE_WAITID
+		if (waitid(P_PID, 0, &info, WEXITED)) {
 			stderr_set_color(RED);
 			fprintf(stderr, "waitid failed: %m\n");
 			stderr_reset_color();
@@ -367,6 +375,25 @@ int main(int argc, char *argv[])
 
 			break;
 		}
+#else
+		if (waitpid(-1, &status, 0) == -1) {
+			fprintf(stderr, "waitpid failed: %s\n",
+				strerror(errno));
+			abort();
+		}
+
+		fprintf(stderr, "test \"%s\":\t", t->name);
+		if (WIFEXITED(status)) {
+			fprintf(stderr, "exit status %d", WEXITSTATUS(status));
+			if (WEXITSTATUS(status) == EXIT_SUCCESS)
+				success = 1;
+			} else if (WIFSIGNALED(status)) {
+				fprintf(stderr, "signal %d", WTERMSIG(status));
+			}
+#endif
+
+		if (t->must_fail)
+			success = !success;
 
 		if (success) {
 			pass++;
-- 
2.20.1



More information about the wayland-devel mailing list