| 1 | package Linux::Perl::prlimit; | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf-8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Linux::Perl::prlimit | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||
| 10 | |||||||||||||
| 11 | my ($soft, $hard) = Linux::Perl::prlimit->get( | ||||||||||||
| 12 | 0, | ||||||||||||
| 13 | Linux::Perl::prlimit::NUMBER()->{'NPROC'} | ||||||||||||
| 14 | ); | ||||||||||||
| 15 | |||||||||||||
| 16 | my ($old_soft, $old_hard) = Linux::Perl::prlimit->get( | ||||||||||||
| 17 | 0, | ||||||||||||
| 18 | Linux::Perl::prlimit::NUMBER()->{'NPROC'}, | ||||||||||||
| 19 | $new_soft, | ||||||||||||
| 20 | $new_hard, | ||||||||||||
| 21 | ); | ||||||||||||
| 22 | |||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||
| 24 | |||||||||||||
| 25 | This module provides access to Linux’s facility for getting and setting | ||||||||||||
| 26 | an arbitrary process’s rlimits. | ||||||||||||
| 27 | |||||||||||||
| 28 | =cut | ||||||||||||
| 29 | |||||||||||||
| 30 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | |||||||||||||
| 33 | 5 | ▶ | use Call::Context; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | 5 | ▶ | use Linux::Perl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | |||||||||||||
| 36 | use parent ( | ||||||||||||
| 37 | 5 | 'Linux::Perl::Base', | |||||||||||
| 38 | 'Linux::Perl::Base::BitsTest', | ||||||||||||
| 39 | 5 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | |||||||||||||
| 41 | #---------------------------------------------------------------------- | ||||||||||||
| 42 | |||||||||||||
| 43 | =head1 CONSTANTS | ||||||||||||
| 44 | |||||||||||||
| 45 | =head2 INFINITY | ||||||||||||
| 46 | |||||||||||||
| 47 | Equivalent to the kernel’s RLIM_INFINITY constant. | ||||||||||||
| 48 | |||||||||||||
| 49 | =cut | ||||||||||||
| 50 | |||||||||||||
| 51 | 5 | ▶ | use constant INFINITY => ~0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | |||||||||||||
| 53 | =head2 NAMES | ||||||||||||
| 54 | |||||||||||||
| 55 | A list of resource names, e.g., C<CPU>. (cf. C<man 2 prlimit>) | ||||||||||||
| 56 | |||||||||||||
| 57 | =cut | ||||||||||||
| 58 | |||||||||||||
| 59 | 5 | use constant NAMES => qw( | |||||||||||
| 60 | CPU | ||||||||||||
| 61 | FSIZE | ||||||||||||
| 62 | DATA | ||||||||||||
| 63 | STACK | ||||||||||||
| 64 | CORE | ||||||||||||
| 65 | RSS | ||||||||||||
| 66 | NPROC | ||||||||||||
| 67 | NOFILE | ||||||||||||
| 68 | MEMLOCK | ||||||||||||
| 69 | AS | ||||||||||||
| 70 | LOCKS | ||||||||||||
| 71 | SIGPENDING | ||||||||||||
| 72 | MSGQUEUE | ||||||||||||
| 73 | NICE | ||||||||||||
| 74 | RTPRIO | ||||||||||||
| 75 | RTTIME | ||||||||||||
| 76 | 5 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 77 | |||||||||||||
| 78 | =head2 NUMBER | ||||||||||||
| 79 | |||||||||||||
| 80 | A reference to a hash that correlates resource name (e.g., C<FSIZE>) to number. | ||||||||||||
| 81 | |||||||||||||
| 82 | =cut | ||||||||||||
| 83 | |||||||||||||
| 84 | 5 | ▶ | use constant NUMBER => { map { ( (NAMES)[$_] => $_ ) } 0 .. (NAMES - 1) }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | |||||||||||||
| 86 | 5 | ▶ | use constant _TMPL => (__PACKAGE__->_PACK_u64() x 2); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 87 | |||||||||||||
| 88 | #---------------------------------------------------------------------- | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 METHODS | ||||||||||||
| 91 | |||||||||||||
| 92 | =head2 ($soft, $hard) = I<CLASS>->get( $PID, $RESOURCE_NUM ) | ||||||||||||
| 93 | |||||||||||||
| 94 | Fetches an individual resource limit. Must be called in list context. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | |||||||||||||
| 98 | sub get { | ||||||||||||
| 99 | 8 | ▶ | my ($class, $pid, $resource) = @_; | ||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 100 | |||||||||||||
| 101 | 8 | Call::Context::must_be_list(); | |||||||||||
| 102 | |||||||||||||
| 103 | 8 | my $old = pack _TMPL(); | |||||||||||
| 104 | |||||||||||||
| 105 | 8 | return $class->_prlimit64($pid, $resource, \undef, \$old); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | =head2 ($old_soft, $old_hard) = I<CLASS>->set( $PID, $RESOURCE_NUM, $SOFT, $HARD ) | ||||||||||||
| 109 | |||||||||||||
| 110 | Sets an individual resource limit. If called in list context | ||||||||||||
| 111 | will return the old soft/hard limits. Must not be called in | ||||||||||||
| 112 | scalar context. | ||||||||||||
| 113 | |||||||||||||
| 114 | =cut | ||||||||||||
| 115 | |||||||||||||
| 116 | sub set { | ||||||||||||
| 117 | 12 | ▶ | my ($class, $pid, $resource, $soft, $hard) = @_; | ||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 118 | |||||||||||||
| 119 | 12 | my $old; | |||||||||||
| 120 | |||||||||||||
| 121 | 12 | ▶ | if (defined wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 10 | Call::Context::must_be_list(); | |||||||||||
| 123 | |||||||||||||
| 124 | 10 | $old = pack _TMPL(); | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 12 | my $new = pack _TMPL(), $soft, $hard; | |||||||||||
| 128 | |||||||||||||
| 129 | 12 | return $class->_prlimit64($pid, $resource, \$new, \$old); | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | sub _prlimit64 { | ||||||||||||
| 133 | 20 | ▶ | my ($class, $pid, $resource, $new_sr, $old_sr) = @_; | ||||||||||
|
Subroutine
_prlimit64: called CC=3 SCAR=11.0
| |||||||||||||
| 134 | |||||||||||||
| 135 | 20 | $class = $class->_get_arch_module(); | |||||||||||
| 136 | |||||||||||||
| 137 | # The `|| 0` avoids a warning from Perl about | ||||||||||||
| 138 | # undefined values passed to syscall(). | ||||||||||||
| 139 | 20 | ▶ | Linux::Perl::call( $class->NR_prlimit64(), 0 + $pid, 0 + $resource, $$new_sr, $$old_sr || 0 ); | ||||||||||
|
Condition100%2/2
$$old_sr || 0
MC/DC100%1/1
$$old_sr || 0
$$old_sr
Truth table100%2/2
A
A= $$old_sr
| |||||||||||||
| 140 | |||||||||||||
| 141 | 20 | ▶ | return wantarray ? unpack( _TMPL(), $$old_sr ) : (); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 5 | 1; | |||||||||||