File Coverage

lib/POSIX/1003/Fcntl.pm
Criterion Covered Total %
statement 53 112 47.3
branch 12 66 18.1
condition 9 41 21.9
subroutine 13 28 46.4
pod 18 18 100.0
total 105 265 39.6


line stmt bran cond sub pod time code
1             # Copyrights 2011-2020 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution POSIX-1003. Meta-POD processed with
6             # OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package POSIX::1003::Fcntl;
10 4     4   1618 use vars '$VERSION';
  4         8  
  4         215  
11             $VERSION = '1.02';
12              
13 4     4   23 use base 'POSIX::1003::Module';
  4         8  
  4         481  
14              
15 4     4   27 use warnings;
  4         7  
  4         102  
16 4     4   24 use strict;
  4         7  
  4         161  
17              
18 4     4   793 use POSIX::1003::FdIO qw/SEEK_SET O_CLOEXEC/;
  4         8  
  4         24  
19 4     4   1082 use POSIX::1003::Errno qw/ENOSYS/;
  4         8  
  4         30  
20              
21             my @constants;
22             my @functions = qw/fcntl
23             fcntl_dup
24             getfd_control
25             setfd_control
26             getfd_flags
27             setfd_flags
28             setfd_lock
29             getfd_islocked
30             getfd_owner
31             setfd_owner
32             setfd_signal
33             getfd_signal
34             setfd_lease
35             getfd_lease
36             setfd_notify
37             setfd_pipe_size
38             getfd_pipe_size
39              
40             flock
41             flockfd
42              
43             lockf
44             /;
45              
46             our %EXPORT_TAGS =
47             ( constants => \@constants
48             , functions => \@functions
49             , flock => [ qw/flock flockfd LOCK_SH LOCK_EX LOCK_UN LOCK_NB/ ]
50             , lockf => [ qw/lockf F_LOCK F_TLOCK F_ULOCK F_TEST/ ]
51             , tables => [ qw/%fcntl/ ]
52             );
53              
54             our @IN_CORE = qw/fcntl flock/;
55              
56             my $fcntl;
57              
58             # We need to address all of our own constants via this HASH, because
59             # they will not be available at compile-time of this file.
60             our %fcntl;
61              
62             BEGIN {
63 4     4   105 $fcntl = fcntl_table;
64 4         79 push @constants, keys %$fcntl;
65 4         31 tie %fcntl, 'POSIX::1003::ReadOnlyTable', $fcntl;
66             }
67              
68             # required parameter which does not get used by the OS.
69 4     4   28 use constant UNUSED => 0;
  4         14  
  4         6425  
70              
71              
72             sub flockfd($$)
73 0     0 1 0 { my ($file, $flags) = @_;
74 0 0       0 my $fd = ref $file ? fileno($file) : $file;
75 0         0 _flock($fd, $flags);
76             }
77              
78              
79             sub lockf($$;$)
80 0     0 1 0 { my ($file, $flags, $len) = @_;
81 0 0       0 my $fd = ref $file ? fileno($file) : $file;
82 0   0     0 _lockf($fd, $flags, $len//0);
83             }
84              
85             #---------------
86              
87              
88             sub fcntl_dup($%)
89 2     2 1 724 { my ($file, %args) = @_;
90 2 50       12 my $fd = ref $file ? fileno($file) : $file;
91 2 50       16 my $func = $args{close_on_exec} ? $fcntl->{F_DUPFD_CLOEXEC} : $fcntl->{F_DUPFD};
92              
93             return _fcntl $fd, $fcntl->{F_DUPFD}, UNUSED
94 2 50       32 if !$args{close_on_exec};
95              
96             return _fcntl $fd, $fcntl->{F_DUPFD_CLOEXEC}, UNUSED
97 0 0       0 if defined $fcntl->{F_DUPFD_CLOEXEC};
98              
99 0         0 _fcntl $fd, $fcntl->{F_DUPFD}, UNUSED;
100 0         0 setfd_control $fd, O_CLOEXEC;
101             }
102              
103              
104             sub getfd_control($)
105 0     0 1 0 { my ($file) = @_;
106 0 0       0 my $fd = ref $file ? fileno($file) : $file;
107 0         0 _fcntl $fd, $fcntl->{F_GETFD}, UNUSED;
108             }
109              
110              
111              
112             sub setfd_control($$)
113 0     0 1 0 { my ($file, $flags) = @_;
114 0 0       0 my $fd = ref $file ? fileno($file) : $file;
115 0         0 _fcntl $fd, $fcntl->{F_SETFD}, $flags;
116             }
117              
118              
119              
120             sub getfd_flags($)
121 2     2 1 4 { my ($file) = @_;
122 2 50       8 my $fd = ref $file ? fileno($file) : $file;
123 2         22 _fcntl $fd, $fcntl->{F_GETFL}, UNUSED;
124             }
125              
126              
127             sub setfd_flags($$)
128 0     0 1 0 { my ($file, $flags) = @_;
129 0 0       0 my $fd = ref $file ? fileno($file) : $file;
130 0         0 _fcntl $fd, $fcntl->{F_SETFL}, $flags;
131             }
132              
133              
134              
135             sub setfd_lock($%)
136 2     2 1 2002217 { my ($file, %args) = @_;
137 2 50       78 my $fd = ref $file ? fileno($file) : $file;
138              
139 2         55 my $func;
140             $func = $args{wait} ? $fcntl->{F_SETLKP} : $fcntl->{F_SETLKWP}
141 2 0       42 if $args{private};
    50          
142              
143 2 50 33     71 $func //= $args{wait} ? $fcntl->{F_SETLK} : $fcntl->{F_SETLKW};
144              
145 2   33     20 $args{type} //= $fcntl->{F_RDLCK};
146 2   50     30 $args{whence} //= SEEK_SET;
147 2   50     15 $args{start} //= 0;
148 2   50     20 $args{len} //= 0;
149 2         177 _lock $fd, $func, \%args;
150             }
151              
152              
153              
154             sub getfd_islocked($%)
155 2     2 1 3003371 { my ($file, %args) = @_;
156 2 50       49 my $fd = ref $file ? fileno($file) : $file;
157 2   33     34 $args{type} //= $fcntl->{F_RDLCK};
158 2   50     44 $args{whence} //= SEEK_SET;
159 2   50     14 $args{start} //= 0;
160 2   50     20 $args{len} //= 0;
161              
162 2 50 0     27 my $func = $args{private} ? ($fcntl->{F_GETLKW}//$fcntl->{F_GETLK}) : $fcntl->{F_GETLK};
163 2 50       118 my $lock = _lock $fd, $func, \%args
164             or return undef;
165              
166             #XXX MO: how to represent "ENOSYS"?
167 2 100       47 $lock->{type}==$fcntl->{F_UNLCK} ? undef : $lock;
168             }
169              
170              
171              
172             sub getfd_owner($%)
173 0     0 1 0 { my ($file, %args) = @_;
174 0 0       0 my $fd = ref $file ? fileno($file) : $file;
175              
176 0         0 my ($type, $pid) = _own_ex $fd, $fcntl->{F_GETOWN_EX}, UNUSED, UNUSED;
177 0 0 0     0 unless(defined $type && $!==ENOSYS)
178 0         0 { $pid = _fcntl $fd, $fcntl->{F_GETOWN}, UNUSED;
179 0 0       0 if($pid < 0)
180 0         0 { $pid = -$pid;
181 0   0     0 $type = $fcntl->{F_OWNER_PGRP} // 2;
182             }
183             else
184 0   0     0 { $type = $fcntl->{F_OWNER_PID} // 1;
185             }
186             }
187              
188 0 0       0 wantarray ? ($type, $pid) : $pid;
189             }
190              
191              
192              
193             sub setfd_owner($$%)
194 0     0 1 0 { my ($file, $pid, %args) = @_;
195 0 0       0 my $fd = ref $file ? fileno($file) : $file;
196              
197             my $type = $args{type}
198 0   0     0 || ($pid < 0 ? ($fcntl->{F_OWNER_PGRP}//2) : ($fcntl->{F_OWNER_PID}//1));
199              
200 0 0       0 $pid = -$pid if $pid < 0;
201              
202 0         0 my ($t, $p) = _own_ex $fd, $fcntl->{F_SETOWN_EX}, $pid, $type;
203 0 0 0     0 unless($t && $!==ENOSYS)
204 0 0 0     0 { my $sig_pid = $type==($fcntl->{F_OWNER_PGRP}//2) ? -$pid : $pid;
205 0         0 ($t, $p) = _fcntl $fd, $fcntl->{F_SETOWN}, $pid;
206             }
207              
208 0         0 defined $t;
209             }
210              
211              
212             sub setfd_signal($$)
213 0     0 1 0 { my ($file, $signal) = @_;
214 0 0       0 my $fd = ref $file ? fileno($file) : $file;
215 0         0 _fcntl $fd, $fcntl->{F_SETSIG}, $signal;
216             }
217              
218              
219              
220             sub getfd_signal($)
221 0     0 1 0 { my $file = shift;
222 0 0       0 my $fd = ref $file ? fileno($file) : $file;
223 0         0 _fcntl $fd, $fcntl->{F_SETSIG}, UNUSED;
224             }
225              
226              
227              
228             sub setfd_lease($$)
229 0     0 1 0 { my ($file, $flags) = @_;
230 0 0       0 my $fd = ref $file ? fileno($file) : $file;
231 0         0 _fcntl $fd, $fcntl->{F_SETLEASE}, $flags;
232             }
233              
234              
235              
236             sub getfd_lease($)
237 0     0 1 0 { my $file = shift;
238 0 0       0 my $fd = ref $file ? fileno($file) : $file;
239 0         0 _fcntl $fd, $fcntl->{F_GETLEASE}, UNUSED;
240             }
241              
242              
243              
244             sub setfd_notify($$)
245 0     0 1 0 { my ($dir, $flags) = @_;
246 0 0       0 my $fd = ref $dir ? fileno($dir) : $dir;
247 0         0 _fcntl $fd, $fcntl->{F_NOTIFY}, $flags;
248             }
249              
250              
251              
252             sub setfd_pipe_size($$)
253 0     0 1 0 { my ($file, $size) = @_;
254 0 0       0 my $fd = ref $file ? fileno($file) : $file;
255 0         0 _fcntl $fd, $fcntl->{F_SETPIPE_SZ}, $size;
256             }
257              
258              
259              
260             sub getfd_pipe_size($)
261 0     0 1 0 { my $file = shift;
262 0 0       0 my $fd = ref $file ? fileno($file) : $file;
263 0         0 _fcntl $fd, $fcntl->{F_GETPIPE_SZ}, UNUSED;
264             }
265              
266             #-----------------
267              
268              
269             sub _create_constant($)
270 86     86   124 { my ($class, $name) = @_;
271 86         100 my $val = $fcntl->{$name};
272 86     0   442 sub() {$val};
  0            
273             }
274              
275             1;