]> git.sur5r.net Git - i3/i3/blob - testcases/t/180-fd-leaks.t
Merge branch 'master' into next
[i3/i3] / testcases / t / 180-fd-leaks.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Verifies that i3 does not leak any file descriptors in 'exec'.
5 #
6 use i3test;
7 use POSIX qw(mkfifo);
8 use File::Temp qw(:POSIX tempfile);
9
10 my $i3 = i3(get_socket_path());
11
12 my $tmp = tmpnam();
13 mkfifo($tmp, 0600) or die "Could not create FIFO in $tmp";
14 my ($outfh, $outname) = tempfile('/tmp/i3-ls-output.XXXXXX', UNLINK => 1);
15
16 cmd qq|exec ls -l /proc/self/fd >$outname && echo done >$tmp|;
17
18 open(my $fh, '<', $tmp);
19 # Block on the FIFO, this will return exactly when the command is done.
20 <$fh>;
21 close($fh);
22 unlink($tmp);
23
24 # Get the ls /proc/self/fd output
25 my $output;
26 {
27     local $/;
28     $output = <$outfh>;
29 }
30 close($outfh);
31
32 # Split lines, keep only those which are symlinks.
33 my @lines = grep { /->/ } split("\n", $output);
34
35 my %fds = map { /([0-9]+) -> (.+)$/; ($1, $2) } @lines;
36
37 # Filter out 0, 1, 2 (stdin, stdout, stderr).
38 delete $fds{0};
39 delete $fds{1};
40 delete $fds{2};
41
42 # Filter out the fd which is caused by ls calling readdir().
43 for my $fd (keys %fds) {
44     delete $fds{$fd} if $fds{$fd} =~ m,^/proc/\d+/fd$,;
45 }
46
47 is(scalar keys %fds, 0, 'No file descriptors leaked');
48
49 done_testing;