blib/lib/Unix/Groups/FFI.pm

filter: Statementstmt 79.4% 62 / 78 Branchbran 47.0% 16 / 34 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 68.4% 91 / 133 CC 20 SCAR 23.5
CC
20
Cov
68%
__ANON__
22.5
__ANON__
7.7
__ANON__
6.5
CRAP 10.5
↑ Summary
1 package Unix::Groups::FFI;
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 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Errno qw(EINVAL ERANGE);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FFI::Platypus;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use constant {
11 1 MAX_NGROUPS_MAX => 65536,
12 MAX_GETGROUPLIST_TRIES => 5,
13 GETGROUPLIST_COUNT_LOW => 32,
14 GETGROUPLIST_COUNT_HIGH => 256,
15 GETGROUPLIST_COUNT_MAX => 65536,
16 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = '0.002';
19  
20 1 our @EXPORT_OK = qw(getgroups setgroups getgrouplist initgroups);
21  
22 1 our @CARP_NOT = qw(FFI::Platypus);
23  
24 1 my $ffi = FFI::Platypus->new(lib => [undef], ignore_not_found => 1);
25  
26 $ffi->attach(getgroups => ['int', 'gid_t[]'] => 'int', sub {
27 2 ▶ my ($xsub) = @_;
Subroutine __ANON__: called CC=6 SCAR=20.5
28 2 my $count = $xsub->(0, []);
29 2 ▶ croak "$!" if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 0 2 2
30 2 ▶ return () if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 2 2
31 2 my @groups = (0)x$count;
32 2 my $rc = $xsub->($count, \@groups);
33 2 ▶ if ($rc < 0 and $! == EINVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc < 0 and $! == 22) 0 2 2
Condition33%1/3
$rc < 0 and $! == 22
!ll&&!rl&&r
200
MC/DC0%0/2
$rc < 0 and $! == 22
$rc < 0 $! == 22
Truth table33%1/3
A and B
A= $rc < 0B= $! == 22
ABresult
0X0
100
111
34 0 @groups = (0)x(MAX_NGROUPS_MAX);
35 0 $rc = $xsub->(MAX_NGROUPS_MAX, \@groups);
36 }
37 2 ▶ croak "$!" if $rc < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc < 0 0 2 2
38 2 return @groups[0..$rc-1];
39 1 });
40  
41 $ffi->attach(setgroups => ['size_t', 'gid_t[]'] => 'int', sub {
42 2 ▶ my ($xsub, @groups) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.6
43 2 my $rc = $xsub->(scalar(@groups), \@groups);
44 2 ▶ croak "$!" if $rc < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc < 0 2 0 2
45 0 return 0;
46 1 });
47  
48 $ffi->attach(getgrouplist => ['string', 'gid_t', 'gid_t[]', 'int*'] => 'int', sub {
49 4 ▶ my ($xsub, $user, $group) = @_;
Subroutine __ANON__: called CC=10 SCAR=31.1
50 4 ▶ $user = '' unless defined $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user 0 4 4
51 4 my $gid = (getpwnam $user)[3];
52 4 ▶ do { $! = EINVAL; croak "$!" } unless defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 2 2 4
53 2 ▶ $group = $gid unless defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 1 1 2
54 2 my ($count, $last_count, @groups) = (1, 1, 0);
55 2 my $rc = $xsub->($user, $group, \@groups, \$count);
56 2 my $tries = 0;
57 2 ▶ while ($rc < 0 and $tries++ < MAX_GETGROUPLIST_TRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc < 0 and $tries++ < 5) 0 2 2
Condition33%1/3
$rc < 0 and $tries++ < 5
!ll&&!rl&&r
200
MC/DC0%0/2
$rc < 0 and $tries++ < 5
$rc < 0 $tries++ < 5
Truth table33%1/3
A and B
A= $rc < 0B= $tries++ < 5
ABresult
0X0
100
111
58 0 ▶ if ($count <= $last_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= $last_count) 0 0 0
59 # count too small, but didn't get a larger one
60 # some implementations short-circuit so we have to guess
61 0 ▶ if ($last_count < GETGROUPLIST_COUNT_LOW) {
Branch: 2 branches
BranchTrueFalseTotal
if ($last_count < 32) { } 0 0 0
elsif ($last_count < 256) { } 0 0 0
62 0 $count = GETGROUPLIST_COUNT_LOW;
63 } elsif ($last_count < GETGROUPLIST_COUNT_HIGH) {
64 0 $count = GETGROUPLIST_COUNT_HIGH;
65 } else {
66 0 $count = GETGROUPLIST_COUNT_MAX;
67 }
68 }
69 0 @groups = (0)x$count;
70 0 $last_count = $count;
71 0 $rc = $xsub->($user, $group, \@groups, \$count);
72 }
73 2 ▶ do { $! = ERANGE; croak "$!" } if $rc < 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($rc < 0) 0 2 2
74 2 return @groups[0..$count-1];
75 1 });
76  
77 $ffi->attach(initgroups => ['string', 'gid_t'] => 'int', sub {
78 1 ▶ my ($xsub, $user, $group) = @_;
Subroutine __ANON__: called CC=5 SCAR=18.7
79 1 ▶ $user = '' unless defined $user;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user 0 1 1
80 1 my $gid = (getpwnam $user)[3];
81 1 ▶ do { $! = EINVAL; croak "$!" } unless defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gid) 0 1 1
82 1 ▶ $group = $gid unless defined $group;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $group 0 1 1
83 1 my $rc = $xsub->($user, $group);
84 1 ▶ croak "$!" if $rc < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc < 0 1 0 1
85 0 return 0;
86 1 });
87  
88 1 1;
89  
90 =head1 NAME
91  
92 Unix::Groups::FFI - Interface to Unix group syscalls
93  
94 =head1 SYNOPSIS
95  
96 use Unix::Groups::FFI qw(getgroups setgroups getgrouplist initgroups);
97  
98 my @gids = getgroups;
99 setgroups(@gids);
100 my @gids = getgrouplist($username, $gid);
101 initgroups($username, $gid);
102  
103 =head1 DESCRIPTION
104  
105 This module provides a L<FFI|FFI::Platypus> interface to several syscalls
106 related to Unix groups, including L<getgroups(2)>, L<setgroups(2)>,
107 L<getgrouplist(3)>, and L<initgroups(3)>. As such it will only work on
108 Unix-like operating systems.
109  
110 =head1 FUNCTIONS
111  
112 All functions are exported individually on demand. A function will not be
113 available for export if the system does not implement the corresponding
114 syscall.
115  
116 =head2 getgroups
117  
118 my @gids = getgroups;
119  
120 Returns the supplementary group IDs of the current process via L<getgroups(2)>.
121  
122 =head2 setgroups
123  
124 setgroups(@gids);
125  
126 Sets the supplementary group IDs for the current process via L<setgroups(2)>.
127 Attempting to set more than C<NGROUPS_MAX> groups (32 before Linux 2.6.4 or
128 65536 since Linux 2.6.4) will result in an C<EINVAL> error. Passing an empty
129 list of group IDs may result in unspecified behavior. The C<CAP_SETGID>
130 L<capability|capabilities(7)> or equivalent privilege is required.
131  
132 =head2 getgrouplist
133  
134 my @gids = getgrouplist($username, $gid);
135 my @gids = getgrouplist($username);
136  
137 Returns the group IDs for all groups of which C<$username> is a member, also
138 including C<$gid> (without repetition), via L<getgrouplist(3)>. If C<$username>
139 does not exist on the system, an C<EINVAL> error will result.
140  
141 As a special case, the primary group ID of C<$username> is included if C<$gid>
142 is not passed.
143  
144 =head2 initgroups
145  
146 initgroups($username, $gid);
147 initgroups($username);
148  
149 Initializes the supplementary group access list for the current process to all
150 groups of which C<$username> is a member, also including C<$gid> (without
151 repetition), via L<initgroups(3)>. If C<$username> does not exist on the
152 system, an C<EINVAL> error will result. The C<CAP_SETGID>
153 L<capability|capabilities(7)> or equivalent privilege is required.
154  
155 As a special case, the primary group ID of C<$username> is included if C<$gid>
156 is not passed.
157  
158 =head1 ERROR HANDLING
159  
160 All functions will throw an exception containing the syscall error message in
161 the event of an error. L<perlvar/"$!"> will also have been set by the syscall,
162 so you could check it after trapping the exception for finer exception
163 handling:
164  
165 use Unix::Groups::FFI 'setgroups';
166 use Syntax::Keyword::Try;
167 use Errno qw(EINVAL EPERM ENOMEM);
168  
169 try { setgroups((0)x2**16) }
170 catch {
171 if ($! == EINVAL) {
172 die 'Tried to set too many groups';
173 } elsif ($! == EPERM) {
174 die 'Insufficient privileges to set groups';
175 } elsif ($! == ENOMEM) {
176 die 'Out of memory';
177 } else {
178 die $@;
179 }
180 }
181  
182 See the documentation for each syscall for details on the possible error codes.
183  
184 =head1 BUGS
185  
186 Report any issues on the public bugtracker.
187  
188 =head1 AUTHOR
189  
190 Dan Book <dbook@cpan.org>
191  
192 =head1 COPYRIGHT AND LICENSE
193  
194 This software is Copyright (c) 2018 by Dan Book.
195  
196 This is free software, licensed under:
197  
198 The Artistic License 2.0 (GPL Compatible)
199  
200 =head1 SEE ALSO
201  
202 L<POSIX>, L<credentials(7)>, L<capabilities(7)>
↑ Summary