Commit 29aed56e authored by Emma Anholt's avatar Emma Anholt
Browse files

simple-xinit: Avoid hanging if the server fails to set up the display fd.



Early fatal errors may leave us with nothing in the displayfd pipe,
and we'd block forever.
Signed-off-by: Emma Anholt's avatarEric Anholt <eric@anholt.net>
parent f7069723
......@@ -60,6 +60,15 @@ usage(int argc, char **argv)
exit(1);
}
static int server_displayfd;
static const char *server_dead = "server_dead";
static void
handle_sigchld(int sig)
{
write(server_displayfd, server_dead, strlen(server_dead));
}
/* Starts the X server, returning its pid. */
static int
start_server(char *const *server_args)
......@@ -71,6 +80,17 @@ start_server(char *const *server_args)
exit(1);
} else if (server_pid != 0) {
/* Continue along the main process that will exec the client. */
struct sigaction sa;
sa.sa_handler = handle_sigchld;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, 0) == -1) {
fprintf(stderr, "Failed to set up signal handler: %s\n",
strerror(errno));
exit(1);
}
return server_pid;
}
......@@ -84,7 +104,7 @@ start_server(char *const *server_args)
static int
get_display(int displayfd)
{
char display_string[10];
char display_string[20];
ssize_t ret;
ret = read(displayfd, display_string, sizeof(display_string) - 1);
......@@ -97,6 +117,12 @@ get_display(int displayfd)
* '\n', but not '\0'. Cap it and parse the number.
*/
display_string[ret] = '\0';
if (strncmp(display_string, server_dead, strlen(server_dead)) == 0) {
fprintf(stderr, "Server failed to start before setting up displayfd\n");
exit(1);
}
return atoi(display_string);
}
......@@ -222,7 +248,8 @@ main(int argc, char **argv)
exit(1);
}
parse_args(argc, argv, &client_args, &server_args, displayfd_pipe[1]);
server_displayfd = displayfd_pipe[1];
parse_args(argc, argv, &client_args, &server_args, server_displayfd);
server_pid = start_server(server_args);
display = get_display(displayfd_pipe[0]);
ret = start_client(client_args, display);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment