]> 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 # Please read the following documents before working on tests:
5 # • http://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • http://build.i3wm.org/docs/ipc.html
12 #   (or docs/ipc)
13 #
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 #   (unless you are already familiar with Perl)
16 #
17 # Verifies that i3 does not leak any file descriptors in 'exec'.
18 #
19 use i3test;
20 use POSIX qw(mkfifo);
21 use File::Temp qw(:POSIX tempfile);
22
23 my $i3 = i3(get_socket_path());
24
25 my $tmp = tmpnam();
26 mkfifo($tmp, 0600) or die "Could not create FIFO in $tmp";
27 my ($outfh, $outname) = tempfile('/tmp/i3-ls-output.XXXXXX', UNLINK => 1);
28
29 cmd qq|exec ls -l /proc/self/fd >$outname && echo done >$tmp|;
30
31 open(my $fh, '<', $tmp);
32 # Block on the FIFO, this will return exactly when the command is done.
33 <$fh>;
34 close($fh);
35 unlink($tmp);
36
37 # Get the ls /proc/self/fd output
38 my $output;
39 {
40     local $/;
41     $output = <$outfh>;
42 }
43 close($outfh);
44
45 # Split lines, keep only those which are symlinks.
46 my @lines = grep { /->/ } split("\n", $output);
47
48 my %fds = map { /([0-9]+) -> (.+)$/; ($1, $2) } @lines;
49
50 # Filter out 0, 1, 2 (stdin, stdout, stderr).
51 delete $fds{0};
52 delete $fds{1};
53 delete $fds{2};
54
55 # Filter out the fd which is caused by ls calling readdir().
56 for my $fd (keys %fds) {
57     delete $fds{$fd} if $fds{$fd} =~ m,^/proc/\d+/fd$,;
58 }
59
60 is(scalar keys %fds, 0, 'No file descriptors leaked');
61
62 done_testing;