]> git.sur5r.net Git - i3/i3/blob - testcases/t/180-fd-leaks.t
Merge branch 'next' into master
[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 SKIP: {
24 skip "Procfs not available on $^O", 1 if $^O eq 'openbsd';
25
26 my $i3 = i3(get_socket_path());
27
28 my $tmp = tmpnam();
29 mkfifo($tmp, 0600) or die "Could not create FIFO in $tmp";
30 my ($outfh, $outname) = tempfile('/tmp/i3-ls-output.XXXXXX', UNLINK => 1);
31
32 cmd qq|exec ls -l /proc/self/fd >$outname && echo done >$tmp|;
33
34 open(my $fh, '<', $tmp);
35 # Block on the FIFO, this will return exactly when the command is done.
36 <$fh>;
37 close($fh);
38 unlink($tmp);
39
40 # Get the ls /proc/self/fd output
41 my $output;
42 {
43     local $/;
44     $output = <$outfh>;
45 }
46 close($outfh);
47
48 # Split lines, keep only those which are symlinks.
49 my @lines = grep { /->/ } split("\n", $output);
50
51 my %fds = map { /([0-9]+) -> (.+)$/; ($1, $2) } @lines;
52
53 # Filter out 0, 1, 2 (stdin, stdout, stderr).
54 delete $fds{0};
55 delete $fds{1};
56 delete $fds{2};
57
58 # Filter out the fd which is caused by ls calling readdir().
59 for my $fd (keys %fds) {
60     delete $fds{$fd} if $fds{$fd} =~ m,^/proc/\d+/fd$,;
61 }
62
63 is(scalar keys %fds, 0, 'No file descriptors leaked');
64
65 }
66
67 done_testing;