1 package SocketActivation;
2 # vim:ts=4:sw=4:expandtab
6 use IO::Socket::UNIX; # core
7 use Cwd qw(abs_path); # core
8 use POSIX qw(:fcntl_h); # core
9 use AnyEvent::Handle; # not core
10 use AnyEvent::Util; # not core
11 use Exporter 'import';
14 our @EXPORT = qw(activate_i3);
17 # Starts i3 using socket activation. Creates a listening socket (with bind +
18 # listen) which is then passed to i3, who in turn calls accept and handles the
21 # Since the kernel buffers the connect, the parent process can connect to the
22 # socket immediately after forking. It then sends a request and waits until it
23 # gets an answer. Obviously, i3 has to be initialized to actually answer the
26 # This way, we can wait *precisely* the amount of time which i3 waits to get
27 # ready, which is a *HUGE* speed gain (and a lot more robust) in comparison to
28 # using sleep() with a fixed amount of time.
30 # unix_socket_path: Location of the socket to use for the activation
31 # display: X11 $ENV{DISPLAY}
32 # configfile: path to the configuration file to use
33 # logpath: path to the logfile to which i3 will append
34 # cv: an AnyEvent->condvar which will be triggered once i3 is ready
39 # remove the old unix socket
40 unlink($args{unix_socket_path});
42 my $socket = IO::Socket::UNIX->new(
44 Local => $args{unix_socket_path},
49 die "could not fork()";
52 $ENV{LISTEN_PID} = $$;
54 delete $ENV{DESKTOP_STARTUP_ID};
56 # $SHELL could be set to fish, which will horribly break running shell
57 # commands via i3’s exec feature. This happened e.g. when having
58 # “set-option -g default-shell "/usr/bin/fish"” in ~/.tmux.conf
60 unless ($args{dont_create_temp_dir}) {
61 $ENV{XDG_RUNTIME_DIR} = '/tmp/i3-testsuite/';
62 mkdir $ENV{XDG_RUNTIME_DIR};
64 $ENV{DISPLAY} = $args{display};
66 # We are about to exec, but we did not modify $^F to include $socket
67 # when creating the socket (because the file descriptor could have a
68 # number != 3 which would lead to i3 leaking a file descriptor). This
69 # caused Perl to set the FD_CLOEXEC flag, which would close $socket on
70 # exec(), effectively *NOT* passing $socket to the new process.
71 # Therefore, we explicitly clear FD_CLOEXEC (the only flag right now)
72 # by setting the flags to 0.
73 POSIX::fcntl($socket, F_SETFD, 0) or die "Could not clear fd flags: $!";
75 # If the socket does not use file descriptor 3 by chance already, we
76 # close fd 3 and dup2() the socket to 3.
77 if (fileno($socket) != 3) {
79 POSIX::dup2(fileno($socket), 3);
80 POSIX::close(fileno($socket));
83 # Make sure no file descriptors are open. Strangely, I got an open file
84 # descriptor pointing to AnyEvent/Impl/EV.pm when testing.
85 AnyEvent::Util::close_all_fds_except(0, 1, 2, 3);
87 # Construct the command to launch i3. Use maximum debug level, disable
88 # the interactive signalhandler to make it crash immediately instead.
89 # Also disable logging to SHM since we redirect the logs anyways.
90 # Force Xinerama because we use Xdmx for multi-monitor tests.
91 my $i3cmd = q|i3 --shmlog-size=0 --disable-signalhandler --force-xinerama|;
92 if (!$args{validate_config}) {
93 # We only set logging if i3 is actually started, but not if we only
94 # validate the config file. This is to keep logging to a minimum as
95 # such a test will likely want to inspect the log file.
96 $i3cmd .= q| -V -d all|;
100 my $outdir = $args{outdir};
101 my $test = $args{testname};
103 if ($args{restart}) {
104 $i3cmd .= ' -L ' . abs_path('restart-state.golden');
107 if ($args{validate_config}) {
111 if ($args{valgrind}) {
113 qq|valgrind --log-file="$outdir/valgrind-for-$test.log" | .
114 qq|--suppressions="./valgrind.supp" | .
115 qq|--leak-check=full --track-origins=yes --num-callers=20 | .
116 qq|--tool=memcheck -- $i3cmd|;
119 my $logfile = "$outdir/i3-log-for-$test";
120 # Append to $logfile instead of overwriting because i3 might be
121 # run multiple times in one testcase.
122 my $cmd = "exec $i3cmd -c $args{configfile} >>$logfile 2>&1";
125 my $out = "$outdir/strace-for-$test.log";
127 # We overwrite LISTEN_PID with the correct process ID to make
128 # socket activation work (LISTEN_PID has to match getpid(),
129 # otherwise the LISTEN_FDS will be treated as a left-over).
130 $cmd = qq|strace -fF -s2048 -v -o "$out" -- | .
131 'sh -c "export LISTEN_PID=\$\$; ' . $cmd . '"';
135 my $out = "$outdir/xtrace-for-$test.log";
137 # See comment in $args{strace} branch.
138 $cmd = qq|xtrace -n -o "$out" -- | .
139 'sh -c "export LISTEN_PID=\$\$; ' . $cmd . '"';
142 # We need to use the shell due to using output redirections.
143 exec '/bin/sh', '-c', $cmd;
145 # if we are still here, i3 could not be found or exec failed. bail out.
149 # close the socket, the child process should be the only one which keeps a file
150 # descriptor on the listening socket.
153 if ($args{validate_config}) {
158 # We now connect (will succeed immediately) and send a request afterwards.
159 # As soon as the reply is there, i3 is considered ready.
160 my $cl = IO::Socket::UNIX->new(Peer => $args{unix_socket_path});
162 $hdl = AnyEvent::Handle->new(
169 # send a get_tree message without payload
170 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
173 $hdl->push_read(chunk => 1, => sub {