| 1 | package Ubic::AccessGuard; | ||||||||||
| 2 | 171 | $Ubic::AccessGuard::VERSION = '1.59'; | |||||||||
| 3 | 171 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 171 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: guard for operations with temporarily different effective uid | ||||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 171 | ▶ | use Params::Validate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 171 | ▶ | use Ubic::Result qw(result); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 171 | ▶ | use Ubic::Credentials; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 171 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 171 | ▶ | use Scalar::Util qw(weaken); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 171 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | # AccessGuard is actually a singleton - there can't be two different guards, since process can't have two euids. | ||||||||||
| 17 | # So we keep weakref to any created AccessGuard. | ||||||||||
| 18 | 171 | my $ag_ref; | |||||||||
| 19 | |||||||||||
| 20 | |||||||||||
| 21 | sub new { | ||||||||||
| 22 | 344 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=3 SCAR=12.6
Pod
documented
| |||||||||||
| 23 | 344 | my ($credentials) = validate_pos(@_, { isa => 'Ubic::Credentials' }); | |||||||||
| 24 | |||||||||||
| 25 | 344 | ▶ | if ($ag_ref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | # oops, another AccessGuard already exists | ||||||||||
| 27 | 0 | my $ag = $$ag_ref; | |||||||||
| 28 | 0 | ▶ | if ($ag->{credentials}->eq($credentials)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | # new guard is the same as old guard | ||||||||||
| 30 | 0 | return $ag; | |||||||||
| 31 | } | ||||||||||
| 32 | else { | ||||||||||
| 33 | 0 | croak "Can't create AccessGuard for ".$credentials->as_string.", there is already another AccessGuard for ".$ag->{credentials}->as_string; | |||||||||
| 34 | } | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 344 | my $self = bless { | |||||||||
| 38 | credentials => $credentials, | ||||||||||
| 39 | } => $class; | ||||||||||
| 40 | |||||||||||
| 41 | try { | ||||||||||
| 42 | 344 | ▶ | $credentials->set_effective; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 43 | } | ||||||||||
| 44 | catch { | ||||||||||
| 45 | 1 | ▶ | die result('unknown', "$_"); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 344 | }; | |||||||||
| 47 | |||||||||||
| 48 | 343 | $ag_ref = \$self; | |||||||||
| 49 | 343 | weaken($ag_ref); | |||||||||
| 50 | |||||||||||
| 51 | 343 | return $self; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub DESTROY { | ||||||||||
| 55 | 344 | ▶ | my $self = shift; | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 344 | local $@; | |||||||||
| 57 | |||||||||||
| 58 | 344 | $self->{credentials}->reset_effective; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 171 | 1; | |||||||||
| 63 | |||||||||||
| 64 | __END__ | ||||||||||