]> git.sur5r.net Git - i3/i3/blob - testcases/lib/SocketActivation.pm
Merge pull request #3433 from orestisf1993/janitorial
[i3/i3] / testcases / lib / SocketActivation.pm
1 package SocketActivation;
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
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';
12 use v5.10;
13
14 our @EXPORT = qw(activate_i3);
15
16 #
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
19 # requests.
20 #
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
24 # request.
25 #
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.
29 #
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
35 #
36 sub activate_i3 {
37     my %args = @_;
38
39     # remove the old unix socket
40     unlink($args{unix_socket_path});
41
42     my $socket = IO::Socket::UNIX->new(
43         Listen => 1,
44         Local => $args{unix_socket_path},
45     );
46
47     my $pid = fork;
48     if (!defined($pid)) {
49         die "could not fork()";
50     }
51     if ($pid == 0) {
52         # Start a process group so that in the parent, we can kill the entire
53         # process group and immediately kill i3bar and any other child
54         # processes.
55         setpgrp;
56
57         $ENV{LISTEN_PID} = $$;
58         $ENV{LISTEN_FDS} = 1;
59         delete $ENV{DESKTOP_STARTUP_ID};
60         delete $ENV{I3SOCK};
61         # $SHELL could be set to fish, which will horribly break running shell
62         # commands via i3’s exec feature. This happened e.g. when having
63         # “set-option -g default-shell "/usr/bin/fish"” in ~/.tmux.conf
64         delete $ENV{SHELL};
65         unless ($args{dont_create_temp_dir}) {
66             $ENV{XDG_RUNTIME_DIR} = '/tmp/i3-testsuite/';
67             mkdir $ENV{XDG_RUNTIME_DIR};
68         }
69         $ENV{DISPLAY} = $args{display};
70
71         # We are about to exec, but we did not modify $^F to include $socket
72         # when creating the socket (because the file descriptor could have a
73         # number != 3 which would lead to i3 leaking a file descriptor). This
74         # caused Perl to set the FD_CLOEXEC flag, which would close $socket on
75         # exec(), effectively *NOT* passing $socket to the new process.
76         # Therefore, we explicitly clear FD_CLOEXEC (the only flag right now)
77         # by setting the flags to 0.
78         POSIX::fcntl($socket, F_SETFD, 0) or die "Could not clear fd flags: $!";
79
80         # If the socket does not use file descriptor 3 by chance already, we
81         # close fd 3 and dup2() the socket to 3.
82         if (fileno($socket) != 3) {
83             POSIX::close(3);
84             POSIX::dup2(fileno($socket), 3);
85             POSIX::close(fileno($socket));
86         }
87
88         # Make sure no file descriptors are open. Strangely, I got an open file
89         # descriptor pointing to AnyEvent/Impl/EV.pm when testing.
90         AnyEvent::Util::close_all_fds_except(0, 1, 2, 3);
91
92         # Construct the command to launch i3. Use maximum debug level, disable
93         # the interactive signalhandler to make it crash immediately instead.
94         # Also disable logging to SHM since we redirect the logs anyways.
95         # Force Xinerama because we use Xdmx for multi-monitor tests.
96         my $i3cmd = q|i3 --shmlog-size=0 --disable-signalhandler|;
97         if (!defined($args{inject_randr15})) {
98             $i3cmd .= q| --force-xinerama|;
99         }
100         if (!$args{validate_config}) {
101             # We only set logging if i3 is actually started, but not if we only
102             # validate the config file. This is to keep logging to a minimum as
103             # such a test will likely want to inspect the log file.
104             $i3cmd .= q| -V -d all|;
105         }
106
107         # For convenience:
108         my $outdir = $args{outdir};
109         my $test = $args{testname};
110
111         if ($args{restart}) {
112             $i3cmd .= ' -L ' . abs_path('restart-state.golden');
113         }
114
115         if ($args{validate_config}) {
116             $i3cmd .= ' -C';
117         }
118
119         if ($args{valgrind}) {
120             $i3cmd =
121                 qq|valgrind --log-file="$outdir/valgrind-for-$test.log" | .
122                 qq|--suppressions="./valgrind.supp" | .
123                 qq|--leak-check=full --track-origins=yes --num-callers=20 | .
124                 qq|--tool=memcheck -- $i3cmd|;
125         }
126
127         my $logfile = "$outdir/i3-log-for-$test";
128         # Append to $logfile instead of overwriting because i3 might be
129         # run multiple times in one testcase.
130         my $cmd = "exec $i3cmd -c $args{configfile} >>$logfile 2>&1";
131
132         if ($args{strace}) {
133             my $out = "$outdir/strace-for-$test.log";
134
135             # We overwrite LISTEN_PID with the correct process ID to make
136             # socket activation work (LISTEN_PID has to match getpid(),
137             # otherwise the LISTEN_FDS will be treated as a left-over).
138             $cmd = qq|strace -fF -s2048 -v -o "$out" -- | .
139                      'sh -c "export LISTEN_PID=\$\$; ' . $cmd . '"';
140         }
141
142         if ($args{xtrace}) {
143             my $out = "$outdir/xtrace-for-$test.log";
144
145             # See comment in $args{strace} branch.
146             $cmd = qq|xtrace -n -o "$out" -- | .
147                      'sh -c "export LISTEN_PID=\$\$; ' . $cmd . '"';
148         }
149
150         if ($args{inject_randr15}) {
151             # See comment in $args{strace} branch.
152             $cmd = 'test.inject_randr15 --getmonitors_reply="' .
153                    $args{inject_randr15} . '" ' .
154                    ($args{inject_randr15_outputinfo}
155                     ? '--getoutputinfo_reply="' .
156                       $args{inject_randr15_outputinfo} . '" '
157                     : '') .
158                    '-- ' .
159                    'sh -c "export LISTEN_PID=\$\$; ' . $cmd . '"';
160         }
161
162         # We need to use the shell due to using output redirections.
163         exec '/bin/sh', '-c', $cmd;
164
165         # if we are still here, i3 could not be found or exec failed. bail out.
166         exit 1;
167     }
168
169     # close the socket, the child process should be the only one which keeps a file
170     # descriptor on the listening socket.
171     $socket->close;
172
173     if ($args{validate_config}) {
174         $args{cv}->send(1);
175         return $pid;
176     }
177
178     # We now connect (will succeed immediately) and send a request afterwards.
179     # As soon as the reply is there, i3 is considered ready.
180     my $cl = IO::Socket::UNIX->new(Peer => $args{unix_socket_path});
181     my $hdl;
182     $hdl = AnyEvent::Handle->new(
183         fh => $cl,
184         on_error => sub {
185             $hdl->destroy;
186             $args{cv}->send(0);
187         });
188
189     # send a get_tree message without payload
190     $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
191
192     # wait for the reply
193     $hdl->push_read(chunk => 1, => sub {
194         my ($h, $line) = @_;
195         $args{cv}->send(1);
196         undef $hdl;
197     });
198
199     return $pid;
200 }
201
202 1