Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions gui-agent/vmside.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@

#include <assert.h>
#include <errno.h>
#include <stdatomic.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -144,6 +146,7 @@ struct genlist *windows_list;
struct genlist *embeder_list;
typedef struct _global_handles Ghandles;
Ghandles *ghandles_for_vchan_reinitialize;
atomic_bool terminating;

#define SKIP_NONMANAGED_WINDOW do { \
if (!list_lookup(windows_list, window)) { \
Expand Down Expand Up @@ -1976,13 +1979,40 @@ static pid_t do_execute_xorg(
}
}

static void terminate_and_cleanup_xorg(Ghandles *g) {
/* this cannot be a SIGCHLD signal handler because of signal safety */
static void *xorg_waitpid_thread(void *p) {
Ghandles *g = p;
int status;
while (waitpid(g->x_pid, &status, 0) < 0) {
if (errno != EINTR) {
/* Looking at waitpid(2), everything other than EINTR would
* indicate a logic error; log this and give up. */
perror("waitpid");
abort();
}
}

if (atomic_load(&terminating)) {
exit(0);
Comment on lines +1995 to +1996
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the exit status really be zero here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens if we get SIGTERM. Not sure if exit(0) is what we should do in this case, but it is what the previous code did. What would you suggest?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure tbh.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we would want to reset the SIGTERM sigaction and send SIGTERM to ourselves, so that the parent gets told we were killed by SIGTERM? Seems like unnecessary complexity though, if I'm being honest.

} else {
if (WIFEXITED(status)) {
fprintf(stderr, "Xorg exited unexpectedly (exit code %d)\n",
WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "Xorg terminated unexpectedly (signal %d)\n",
WTERMSIG(status));
} else {
fprintf(stderr, "Xorg terminated unexpectedly\n");
}

exit(1);
}
}

static void terminate_and_cleanup_xorg(Ghandles *g) {
if (g->x_pid != (pid_t)-1) {
atomic_store(&terminating, true);
kill(g->x_pid, SIGTERM);
waitpid(g->x_pid, &status, 0);
g->x_pid = -1;
}
}

Expand Down Expand Up @@ -2185,7 +2215,7 @@ static void handle_sigterm()
{
Ghandles *g = ghandles_for_vchan_reinitialize;
terminate_and_cleanup_xorg(g);
exit(0);
while (1) pause();
}

static void usage()
Expand Down Expand Up @@ -2253,6 +2283,7 @@ int main(int argc, char **argv)
int xfd;
Ghandles g;
int wait_fds[2];
pthread_t waitpid_thread;

parse_args(&g, argc, argv);

Expand All @@ -2274,6 +2305,11 @@ int main(int argc, char **argv)
vchan_register_at_eof(handle_guid_disconnect);
send_protocol_version(g.vchan);
g.x_pid = get_xconf_and_run_x(&g);
if ((errno = pthread_create(&waitpid_thread, NULL, xorg_waitpid_thread, &g)) != 0) {
perror("pthread_create");
kill(g.x_pid, SIGTERM);
exit(1);
}
mkghandles(&g);
ghandles_for_vchan_reinitialize = &g;
/* Turn on Composite for all children of root window. This way X server
Expand Down Expand Up @@ -2315,7 +2351,6 @@ int main(int argc, char **argv)
fprintf(stderr, "XFixes not available, cursor shape handling off");

XAutoRepeatOff(g.display);
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, handle_sigterm);
windows_list = list_new();
embeder_list = list_new();
Expand Down