[PATCH xserver 05/10 v2] test: Add a little xinit-like program for starting servers for testing.
Eric Anholt
eric at anholt.net
Fri Sep 23 07:58:54 UTC 2016
The normal xinit is racy because it doesn't use -displayfd. This
implements the bare minimum for testing purposes, using -displayfd to
sequence starting the client, and avoids adding yet another dependency
to the server.
v2: Fix asprintf error checks.
Signed-off-by: Eric Anholt <eric at anholt.net>
---
test/.gitignore | 1 +
test/Makefile.am | 13 ++-
test/simple-xinit.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 233 insertions(+), 4 deletions(-)
create mode 100644 test/simple-xinit.c
diff --git a/test/.gitignore b/test/.gitignore
index a62fc3d70651..c44fc827ac13 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -11,5 +11,6 @@ xfree86
xkb
xtest
signal-logging
+simple-xinit
*.log
*.trs
diff --git a/test/Makefile.am b/test/Makefile.am
index 4ccadf5b0005..24bfc35bc88a 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,17 +1,22 @@
if ENABLE_UNIT_TESTS
SUBDIRS= .
-noinst_PROGRAMS = list string
+TEST_PROGS = list string
if XORG
# Tests that require at least some DDX functions in order to fully link
# For now, requires xf86 ddx, could be adjusted to use another
SUBDIRS += xi1 xi2
-noinst_PROGRAMS += xkb input xtest misc fixes xfree86 signal-logging touch
+TEST_PROGS += xkb input xtest misc fixes xfree86 signal-logging touch
if RES
-noinst_PROGRAMS += hashtabletest
+TEST_PROGS += hashtabletest
endif
endif
check_LTLIBRARIES = libxservertest.la
+noinst_PROGRAMS = \
+ simple-xinit \
+ $(TEST_PROGS) \
+ $(NULL)
+
if XVFB
XVFB_TESTS = scripts/xvfb-piglit.sh
endif
@@ -21,7 +26,7 @@ SCRIPT_TESTS = \
$(NULL)
TESTS = \
- $(noinst_PROGRAMS) \
+ $(TEST_PROGS) \
$(SCRIPT_TESTS) \
$(NULL)
diff --git a/test/simple-xinit.c b/test/simple-xinit.c
new file mode 100644
index 000000000000..565620ad71a6
--- /dev/null
+++ b/test/simple-xinit.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright © 2016 Broadcom
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <errno.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static void
+kill_server(int serverpid)
+{
+ int ret = kill(serverpid, SIGTERM);
+ int wstatus;
+
+ if (ret) {
+ fprintf(stderr, "Failed to send kill to the server: %s\n",
+ strerror(errno));
+ exit(1);
+ }
+
+ ret = waitpid(serverpid, &wstatus, 0);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to wait for X to die: %s\n", strerror(errno));
+ exit(1);
+ }
+}
+
+static void
+usage(int argc, char **argv)
+{
+ fprintf(stderr, "%s <client command> -- <server command>\n", argv[0]);
+ exit(1);
+}
+
+/* Starts the X server, returning its pid. */
+static int
+start_server(char *const *server_args)
+{
+ int serverpid = fork();
+
+ if (serverpid != 0) {
+ /* Continue along the main process that will exec the client. */
+ return serverpid;
+ }
+
+ /* Execute the server. This only returns if an error occurred. */
+ execvp(server_args[0], server_args);
+ fprintf(stderr, "Error starting the server: %s\n", strerror(errno));
+ exit(1);
+}
+
+/* Reads the display number out of the started server's display socket. */
+static int
+get_display(int displayfd)
+{
+ char display_string[10];
+ ssize_t ret;
+
+ ret = read(displayfd, display_string, sizeof(display_string - 1));
+ if (ret <= 0) {
+ fprintf(stderr, "Failed reading displayfd: %s\n", strerror(errno));
+ exit(1);
+ }
+
+ /* We've read in the display number as a string terminated by
+ * '\n', but not '\0'. Cap it and parse the number.
+ */
+ display_string[ret] = '\0';
+ return atoi(display_string);
+}
+
+static int
+start_client(char *const *client_args, int display)
+{
+ char *display_string;
+ int ret;
+ int client_pid;
+
+ ret = asprintf(&display_string, ":%d", display);
+ if (ret < 0) {
+ fprintf(stderr, "asprintf fail\n");
+ exit(1);
+ }
+
+ ret = setenv("DISPLAY", display_string, true);
+ if (ret) {
+ fprintf(stderr, "Failed to set DISPLAY\n");
+ exit(1);
+ }
+
+ client_pid = fork();
+ if (client_pid) {
+ int wstatus;
+
+ ret = waitpid(client_pid, &wstatus, 0);
+ if (ret < 0) {
+ fprintf(stderr, "Error waiting for client to start: %s\n",
+ strerror(errno));
+ return 1;
+ }
+
+ return WEXITSTATUS(wstatus);
+ } else {
+ execvp(client_args[0], client_args);
+ /* exec only returns if an error occurred. */
+ fprintf(stderr, "Error starting the client: %s\n", strerror(errno));
+ exit(1);
+ }
+}
+
+/* Splits the incoming argc/argv into a pair of NULL-terminated arrays
+ * of args.
+ */
+static void
+parse_args(int argc, char **argv,
+ char * const **out_client_args,
+ char * const **out_server_args,
+ int displayfd)
+{
+ /* We're stripping the -- and the program name, inserting two
+ * NULLs, and also the -displayfd and fd number.
+ */
+ char **args_storage = calloc(argc + 2, sizeof(char **));
+ char *const *client_args;
+ char *const *server_args = NULL;
+ char **next_arg = args_storage;
+ bool parsing_client = true;
+ int i, ret;
+ char *displayfd_string;
+
+ if (!args_storage)
+ exit(1);
+
+ client_args = args_storage;
+ for (i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "--") == 0) {
+ if (!parsing_client)
+ usage(argc, argv);
+
+ /* Cap the client list */
+ *next_arg = NULL;
+ next_arg++;
+
+ /* Move to adding into server_args. */
+ server_args = next_arg;
+ parsing_client = false;
+ continue;
+ }
+
+ *next_arg = argv[i];
+ next_arg++;
+ }
+
+ if (client_args[0] == NULL || !server_args || server_args[0] == NULL)
+ usage(argc, argv);
+
+ /* Give the server -displayfd X */
+ *next_arg = (char *)"-displayfd";
+ next_arg++;
+
+ ret = asprintf(&displayfd_string, "%d", displayfd);
+ if (ret < 0) {
+ fprintf(stderr, "asprintf fail\n");
+ exit(1);
+ }
+ *next_arg = displayfd_string;
+ next_arg++;
+
+ *out_client_args = client_args;
+ *out_server_args = server_args;
+}
+
+int
+main(int argc, char **argv)
+{
+ char * const *client_args;
+ char * const *server_args;
+ int displayfd_pipe[2];
+ int display, serverpid;
+ int ret;
+
+ ret = pipe(displayfd_pipe);
+ if (ret) {
+ fprintf(stderr, "Pipe creation failure: %s", strerror(errno));
+ exit(1);
+ }
+
+ parse_args(argc, argv, &client_args, &server_args, displayfd_pipe[1]);
+ serverpid = start_server(server_args);
+ display = get_display(displayfd_pipe[0]);
+ ret = start_client(client_args, display);
+ kill_server(serverpid);
+
+ exit(ret);
+}
--
2.9.3
More information about the xorg-devel
mailing list