+# forks an Xdummy or Xdmx process
+sub fork_xserver {
+ my $displaynum = shift;
+ my $pid = fork();
+ die "Could not fork: $!" unless defined($pid);
+ if ($pid == 0) {
+ # Child, close stdout/stderr, then start Xdummy.
+ close STDOUT;
+ close STDERR;
+
+ exec @_;
+ exit 1;
+ }
+ push(@complete_run::CLEANUP, sub {
+ kill(15, $pid);
+ # Unlink the X11 socket, Xdmx seems to leave it there.
+ unlink($x_socketpath . $displaynum);
+ });
+
+ return $x_socketpath . $displaynum;
+}
+
+# Blocks until the socket paths specified in the given array reference actually
+# exist.
+sub wait_for_x {
+ my ($sockets_waiting) = @_;
+
+ # Wait until Xdmx actually runs. Pretty ugly solution, but as long as we
+ # can’t socket-activate X11…
+ while (1) {
+ @$sockets_waiting = grep { ! -S $_ } @$sockets_waiting;
+ last unless @$sockets_waiting;
+ sleep 0.1;
+ }
+}
+