+ my ($pid_before, $pid_after);
+ if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
+ lei_ok 'daemon-pid';
+ chomp($pid_before = $lei_out);
+ ok($pid_before, 'daemon is live');
+ }
+ for my $out ([], [qw(-f mboxcl2)]) {
+ pipe(my ($r, $w)) or BAIL_OUT $!;
+ open my $err, '+>', undef or BAIL_OUT $!;
+ my $opt = { run_mode => 0, 1 => $w, 2 => $err };
+ my $cmd = [qw(lei q -qt), @$out, 'z:1..'];
+ my $tp = start_script($cmd, undef, $opt);
+ close $w;
+ sysread($r, my $buf, 1);
+ close $r; # trigger SIGPIPE
+ $tp->join;
+ ok(WIFSIGNALED($?), "signaled @$out");
+ is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
+ seek($err, 0, 0);
+ my @err = <$err>;
+ is_deeply(\@err, [], "no errors @$out");
+ }
+ if (-d $ENV{XDG_RUNTIME_DIR} && -w _) {
+ lei_ok 'daemon-pid';
+ chomp(my $pid_after = $lei_out);
+ is($pid_after, $pid_before, 'pid unchanged') or
+ skip 'daemon died', 1;
+ skip 'not killing persistent lei-daemon', 2 if
+ $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
+ lei_ok 'daemon-kill';
+ my $alive = 1;
+ for (1..100) {
+ $alive = kill(0, $pid_after) or last;
+ tick();
+ }
+ ok(!$alive, 'daemon-kill worked');
+ }