]> git.sur5r.net Git - i3/i3/blob - testcases/lib/SocketActivation.pm
Merge branch 'fix-comment'
[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 (); # core
9 use AnyEvent::Handle; # not core
10 use Exporter 'import';
11 use v5.10;
12
13 our @EXPORT = qw(activate_i3);
14
15 #
16 # Starts i3 using socket activation. Creates a listening socket (with bind +
17 # listen) which is then passed to i3, who in turn calls accept and handles the
18 # requests.
19 #
20 # Since the kernel buffers the connect, the parent process can connect to the
21 # socket immediately after forking. It then sends a request and waits until it
22 # gets an answer. Obviously, i3 has to be initialized to actually answer the
23 # request.
24 #
25 # This way, we can wait *precisely* the amount of time which i3 waits to get
26 # ready, which is a *HUGE* speed gain (and a lot more robust) in comparison to
27 # using sleep() with a fixed amount of time.
28 #
29 # unix_socket_path: Location of the socket to use for the activation
30 # display: X11 $ENV{DISPLAY}
31 # configfile: path to the configuration file to use
32 # logpath: path to the logfile to which i3 will append
33 # cv: an AnyEvent->condvar which will be triggered once i3 is ready
34 #
35 sub activate_i3 {
36     my %args = @_;
37
38     # remove the old unix socket
39     unlink($args{unix_socket_path});
40
41     # pass all file descriptors up to three to the children.
42     # we need to set this flag before opening the socket.
43     open(my $fdtest, '<', '/dev/null');
44     $^F = fileno($fdtest);
45     close($fdtest);
46     my $socket = IO::Socket::UNIX->new(
47         Listen => 1,
48         Local => $args{unix_socket_path},
49     );
50
51     my $pid = fork;
52     if (!defined($pid)) {
53         die "could not fork()";
54     }
55     if ($pid == 0) {
56         $ENV{LISTEN_PID} = $$;
57         $ENV{LISTEN_FDS} = 1;
58         delete $ENV{DESKTOP_STARTUP_ID};
59         $ENV{DISPLAY} = $args{display};
60         $ENV{PATH} = join(':',
61             '../i3-nagbar',
62             '../i3-msg',
63             '../i3-config-wizard',
64             '../i3bar',
65             '..',
66             $ENV{PATH}
67         );
68         # Only pass file descriptors 0 (stdin), 1 (stdout), 2 (stderr) and
69         # 3 (socket) to the child.
70         $^F = 3;
71
72         # If the socket does not use file descriptor 3 by chance already, we
73         # close fd 3 and dup2() the socket to 3.
74         if (fileno($socket) != 3) {
75             POSIX::close(3);
76             POSIX::dup2(fileno($socket), 3);
77         }
78
79         # Construct the command to launch i3. Use maximum debug level, disable
80         # the interactive signalhandler to make it crash immediately instead.
81         my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
82
83         if ($args{valgrind}) {
84             $i3cmd =
85                 qq|valgrind -v --log-file="$args{outdir}/valgrind.log" | .
86                 qq|--leak-check=full --track-origins=yes --num-callers=20 | .
87                 qq|--tool=memcheck -- $i3cmd|;
88         }
89
90         # Append to $args{logpath} instead of overwriting because i3 might be
91         # run multiple times in one testcase.
92         my $cmd = "exec $i3cmd -c $args{configfile} >>$args{logpath} 2>&1";
93
94         # We need to use the shell due to using output redirections.
95         exec '/bin/sh', '-c', $cmd;
96
97         # if we are still here, i3 could not be found or exec failed. bail out.
98         exit 1;
99     }
100
101     # close the socket, the child process should be the only one which keeps a file
102     # descriptor on the listening socket.
103     $socket->close;
104
105     # We now connect (will succeed immediately) and send a request afterwards.
106     # As soon as the reply is there, i3 is considered ready.
107     my $cl = IO::Socket::UNIX->new(Peer => $args{unix_socket_path});
108     my $hdl;
109     $hdl = AnyEvent::Handle->new(
110         fh => $cl,
111         on_error => sub {
112             $hdl->destroy;
113             $args{cv}->send(0);
114         });
115
116     # send a get_tree message without payload
117     $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
118
119     # wait for the reply
120     $hdl->push_read(chunk => 1, => sub {
121         my ($h, $line) = @_;
122         $args{cv}->send(1);
123         undef $hdl;
124     });
125
126     return $pid;
127 }
128
129 1