| 1 | package Linux::Perl::getrandom; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =encoding utf-8 | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Linux::Perl::getrandom | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | my $numbytes = Linux::Perl::getrandom::x86_64->getrandom( | ||||||||||||||||||||
| 15 | buffer => \$buffer, | ||||||||||||||||||||
| 16 | flags => [ 'RANDOM', 'NONBLOCK' ], | ||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # … or, platform-neutral: | ||||||||||||||||||||
| 20 | my $numbytes = Linux::Perl::getrandom->getrandom( | ||||||||||||||||||||
| 21 | buffer => \$buffer, | ||||||||||||||||||||
| 22 | flags => [ 'RANDOM', 'NONBLOCK' ], | ||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | This is an interface to Linux’s C<getrandom> system call. This system | ||||||||||||||||||||
| 28 | call is documented only for kernel 3.17 and after; however, it appears | ||||||||||||||||||||
| 29 | to be present in some earlier kernel versions. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | use Linux::Perl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 1 | ▶ | use Linux::Perl::Pointer; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | my %FLAG_VALUE = ( | |||||||||||||||||||
| 37 | NONBLOCK => 1, | ||||||||||||||||||||
| 38 | RANDOM => 2, | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | sub getrandom { | ||||||||||||||||||||
| 42 | 1 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||
|
Subroutine
getrandom: called CC=6 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | ▶ | if (!$class->can('NR_getrandom')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 1 | require Linux::Perl::ArchLoader; | |||||||||||||||||||
| 46 | 1 | $class = Linux::Perl::ArchLoader::get_arch_module($class); | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 1 | my $flags = 0; | |||||||||||||||||||
| 50 | 1 | ▶ | if ($opts{'flags'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | for my $f ( @{ $opts{'flags'} } ) { | |||||||||||||||||||
| 52 | 0 | ▶ | $flags |= $FLAG_VALUE{$f} || do { | ||||||||||||||||||
|
Condition0%0/3
$FLAG_VALUE{$f} || do {
die "Invalid flag: \342\200\234$f\342\200\235!"
}
MC/DC0%0/2
$FLAG_VALUE{$f} || do {
die "Invalid flag: \342\200\234$f\342\200\235!"
}
$FLAG_VALUE{$f}
do {
die "Invalid flag: \342\200\234$f\342\200\235!"
}
Truth table0%0/3
A || B
A= $FLAG_VALUE{$f}B= do {
die "Invalid flag: \342\200\234$f\342\200\235!"
}
| |||||||||||||||||||||
| 53 | 0 | die "Invalid flag: “$f”!"; | |||||||||||||||||||
| 54 | }; | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 1 | ▶ | if ('SCALAR' ne ref $opts{'buffer'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | die "“buffer” must be a scalar reference, not “$opts{'buffer'}”!"; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | return Linux::Perl::call( | ||||||||||||||||||||
| 63 | $class->NR_getrandom(), | ||||||||||||||||||||
| 64 | 1 | Linux::Perl::Pointer::get_address( ${ $opts{'buffer'} } ), | |||||||||||||||||||
| 65 | 1 | length( ${ $opts{'buffer'} } ), | |||||||||||||||||||
| 66 | 0 + $flags, | ||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | 1; | |||||||||||||||||||