Class-Shared
view release on metacpan or search on metacpan
lib/Class/Shared.pm view on Meta::CPAN
}
# Purpose: say whether $dir is somewhere only this user can put a socket.
# Entry: $dir is a directory name; nothing is created.
# Exit status: (1, undef) when it is ours and not writable by group or
# others, otherwise (0, $why_not).
# Side effects: none.
sub _check_dir {
my ($dir) = @_;
my @stat = stat($dir);
return (0, "cannot stat $dir: $!") unless @stat;
return (0, "$dir is owned by uid $stat[4], not by uid $>")
if $stat[4] != $>;
return (0, "$dir is writable by group or others; refusing to use it")
if $stat[2] & 0022;
return (1, undef);
}
lib/Class/Shared.pm view on Meta::CPAN
# socket is a socket owned by this user, otherwise (0, $why_not).
# Side effects: none.
# Why both: a safe directory means nobody else could have put the socket
# there; the owner check catches a socket that was already there.
sub _check_endpoint {
my ($path) = @_;
my ($safe, $why) = _check_dir(dir_of($path));
return (0, $why) unless $safe;
my @stat = stat($path);
return (0, "cannot stat $path: $!") unless @stat;
return (0, "$path is not a socket") unless -S _;
return (0, "$path is owned by uid $stat[4], not by uid $>")
if $stat[4] != $>;
return (1, undef);
}
lib/Class/Shared/Backend.pm view on Meta::CPAN
# after a rename they would go on filling a file nobody can see.
# A log that is a symbolic link or a device is left to whoever
# made it so.
sub _trim_log {
my $self = shift;
my $limit = $self->{max_log_size};
return 1 unless $limit && defined $self->{log};
my $path = File::Spec->rel2abs($self->{log});
return 1 unless lstat($path) && -f _ && -s _ > $limit;
local ($!, $@);
# From here on only this handle is read and emptied. The name may come to
# mean another file at any moment, so check that what was opened is what the
# name meant: a plain file, not a link to one.
open(my $log, '+<', $path) or return 1;
my @opened = stat($log);
my @named = lstat($path);
return 1 unless @named && -f _ && $named[0] == $opened[0] && $named[1] == $opened[1];
my $old = "$path.1";
# Made anew, owner only, so that nothing already there is written through.
unlink($old);
if (sysopen(my $to, $old, O_WRONLY | O_CREAT | O_EXCL, 0600)) {
while (sysread($log, my $chunk, $READ_CHUNK)) {
last unless syswrite($to, $chunk);
}
write_failed => 'write failed: ',
serialise => 'cannot serialise message: ',
deserialise => 'cannot deserialise message: ',
},
);
$config{cwd} = Cwd::getcwd();
$config{env_dir} = $ENV{CLASS_SHARED_DIR};
$config{env_xdg} = $ENV{XDG_RUNTIME_DIR};
# The device and inode of STDOUT, which change if it is redirected.
$config{stdout_id} = join(':', (stat(STDOUT))[0, 1]);
my $tmp = File::Temp->newdir('class-shared-domain-XXXXXX', TMPDIR => 1, CLEANUP => 1);
# A declared failure: the exact documented text, plus the new line every
# message ends with.
sub dies_with {
my ($code, $expected, $name) = @_;
throws_ok { $code->() } qr/\A\Q$expected\E\n\z/, $name;
open(my $fh, '>:raw', $path) or die "cannot write $path: $!";
print {$fh} $content or die "cannot write $path: $!";
close($fh) or die "cannot write $path: $!";
return $path;
}
sub read_mode {
my ($path) = @_;
my @stat = stat($path) or return;
return $stat[2] & 07777;
}
# Run detach_standard_streams in a child, which is the only safe place for a
# call that replaces STDIN, STDOUT and STDERR. POSIX::_exit skips the END
# blocks, so the child cannot print a second TAP plan.
sub detach_in_child {
my ($log) = @_;
is($Class::Shared::Protocol::MAX_FRAME, $config{max_frame},
'the frame limit is 64 MiB');
is($Class::Shared::Protocol::DIGEST_SIZE, $config{digest_size},
'the digest is 32 bytes');
is(Cwd::getcwd(), $config{cwd}, 'the working directory is unchanged by any failure above');
is($ENV{CLASS_SHARED_DIR}, $config{env_dir}, 'CLASS_SHARED_DIR is restored');
is($ENV{XDG_RUNTIME_DIR}, $config{env_xdg}, 'XDG_RUNTIME_DIR is restored');
ok(-d "$tmp", 'no failure removed the temporary directory');
is(join(':', (stat(STDOUT))[0, 1]), $config{stdout_id},
'STDOUT still belongs to the test process: only the children detached theirs');
};
done_testing();
open(my $fh, '>:raw', $path) or die "cannot write $path: $!";
print {$fh} $content or die "cannot write $path: $!";
close($fh) or die "cannot write $path: $!";
return $path;
}
sub read_mode {
my ($path) = @_;
my @stat = stat($path) or return;
return $stat[2] & 07777;
}
sub make_dir_with_mode {
my ($name, $mode) = @_;
my $dir = File::Spec->catdir("$tmp", $name);
mkdir($dir, $config{create_mode}) or die "cannot make $dir: $!";
chmod($mode, $dir) or die "cannot chmod $dir: $!";
my $dir = File::Spec->catdir("$tmp", $name);
mkdir($dir, $config{create_mode}) or die "cannot make $dir: $!";
chmod($mode, $dir) or die "cannot chmod $dir: $!";
return $dir;
}
sub read_mode {
my ($path) = @_;
my @stat = stat($path) or return;
return $stat[2] & 07777;
}
# A unix socket that is listening, for the endpoint proof.
sub listening_socket {
my ($path) = @_;
require IO::Socket::UNIX;
t/transition.t view on Meta::CPAN
ok(!-e "$socket$config{pid_suffix}", 'the pid file is gone');
ok(wait_until(sub { lock_is_free("$socket$config{owner_suffix}") }), 'the lock is released');
like(read_file($log), qr/\bstopped\b/, 'and stopped is logged');
};
subtest 'State: STARTING -> Trigger: lock held -> State: FAILED' => sub {
my $area = area('locked-start');
my $socket = "$area/$config{socket_name}";
my $pid = start_backend(socket => $socket, log => "$area/log");
ok(wait_until(sub { -S $socket }), 'a backend is SERVING');
my @before = stat($socket);
throws_ok {
Class::Shared::Backend->new(
socket => $socket,
log => "$area/second$config{log_suffix}",
start_timeout => $config{start_timeout},
daemonize => 0,
)->run;
} $config{error}{busy};
my @after = stat($socket);
is_deeply([$after[0], $after[1]], [$before[0], $before[1]],
'the running backend keeps its socket');
my $client = connect_raw($socket) or BAIL_OUT("cannot connect: $!");
is(ask($client, hello())->{ok}, 1, 'and still answers');
close($client);
stop_backend($pid, $socket);
};
( run in 3.769 seconds using v1.01-cache-2.11-cpan-eb134014566 )