1 package SocketActivation;
2 # vim:ts=4:sw=4:expandtab
6 use IO::Socket::UNIX; # core
7 use Cwd qw(abs_path); # core
9 use AnyEvent::Handle; # not core
10 use Exporter 'import';
13 our @EXPORT = qw(activate_i3);
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
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
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.
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
38 # remove the old unix socket
39 unlink($args{unix_socket_path});
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);
46 my $socket = IO::Socket::UNIX->new(
48 Local => $args{unix_socket_path},
53 die "could not fork()";
56 $ENV{LISTEN_PID} = $$;
58 delete $ENV{DESKTOP_STARTUP_ID};
59 $ENV{DISPLAY} = $args{display};
60 $ENV{PATH} = join(':',
63 '../i3-config-wizard',
68 # Only pass file descriptors 0 (stdin), 1 (stdout), 2 (stderr) and
69 # 3 (socket) to the child.
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) {
76 POSIX::dup2(fileno($socket), 3);
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";
83 if ($args{valgrind}) {
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|;
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";
94 # We need to use the shell due to using output redirections.
95 exec '/bin/sh', '-c', $cmd;
97 # if we are still here, i3 could not be found or exec failed. bail out.
101 # close the socket, the child process should be the only one which keeps a file
102 # descriptor on the listening socket.
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});
109 $hdl = AnyEvent::Handle->new(
116 # send a get_tree message without payload
117 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
120 $hdl->push_read(chunk => 1, => sub {