| 1 |
|
| /* |
| 2 |
|
| This program is free software; you can redistribute it and/or modify it |
| 3 |
|
| under the same terms as Perl itself. |
| 4 |
|
| |
| 5 |
|
| Copyright (C) 2002-2014 Jens Thoms Toerring <jt@toerring.de> |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| |
| 9 |
|
| #include "EXTERN.h" |
| 10 |
|
| #include "perl.h" |
| 11 |
|
| #include "XSUB.h" |
| 12 |
|
| #include <fcntl.h> |
| 13 |
|
| |
| 14 |
|
| |
| 15 |
|
| MODULE = File::FcntlLock PACKAGE = File::FcntlLock |
| 16 |
|
| |
| 17 |
|
| PROTOTYPES: ENABLE |
| 18 |
|
| |
| 19 |
|
| |
| 20 |
|
| SV * |
| 21 |
|
| C_fcntl_lock( fd, function, flock_hash, int_err ) |
| 22 |
|
| int fd |
| 23 |
|
| int function |
| 24 |
|
| SV *flock_hash |
| 25 |
|
| SV *int_err |
| 26 |
|
| |
| 27 |
|
| INIT: |
| 28 |
|
| struct flock flock_struct; |
| 29 |
|
| HV *fs; |
| 30 |
|
| SV **sv_type, |
| 31 |
|
| **sv_whence, |
| 32 |
|
| **sv_start, |
| 33 |
|
| **sv_len; |
| 34 |
|
| |
| 35 |
39 |
| sv_setiv( int_err, 0 ); |
| 36 |
|
| |
| 37 |
39 |
▶ | if ( ! SvROK( flock_hash ) ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! SvROK( flock_hash ) ) |
0 |
39 |
39 |
|
| 38 |
|
| { |
| 39 |
0 |
| sv_setiv( int_err, 1 ); |
| 40 |
0 |
| XSRETURN_UNDEF; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
39 |
| fs = ( HV * ) SvRV( flock_hash ); |
| 44 |
|
| |
| 45 |
|
| CODE: |
| 46 |
|
| /* Let's be careful and not assume that anything at all will work */ |
| 47 |
|
| |
| 48 |
39 |
▶ | if ( ( sv_type = hv_fetch( fs, "l_type", 6, 0 ) ) == NULL |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ( sv_type = hv_fetch( fs, "l_type", 6, 0 ) ) == NULL |
39 |
0 |
39 |
|
| 49 |
39 |
▶ | || ( sv_whence = hv_fetch( fs, "l_whence", 8, 0 ) ) == NULL |
Branch: 1 branch
| Branch | True | False | Total |
| || ( sv_whence = hv_fetch( fs, "l_whence", 8, 0 ) ) == NULL |
39 |
0 |
39 |
|
| 50 |
39 |
▶ | || ( sv_start = hv_fetch( fs, "l_start", 7, 0 ) ) == NULL |
Branch: 1 branch
| Branch | True | False | Total |
| || ( sv_start = hv_fetch( fs, "l_start", 7, 0 ) ) == NULL |
39 |
0 |
39 |
|
| 51 |
39 |
▶ | || ( sv_len = hv_fetch( fs, "l_len", 5, 0 ) ) == NULL ) |
Branch: 1 branch
| Branch | True | False | Total |
| || ( sv_len = hv_fetch( fs, "l_len", 5, 0 ) ) == NULL ) |
0 |
39 |
39 |
|
| 52 |
|
| { |
| 53 |
0 |
| sv_setiv( int_err, 1 ); |
| 54 |
0 |
| XSRETURN_UNDEF; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
39 |
| flock_struct.l_type = SvIV( *sv_type ); |
| 58 |
39 |
| flock_struct.l_whence = SvIV( *sv_whence ); |
| 59 |
39 |
| flock_struct.l_start = SvIV( *sv_start ); |
| 60 |
39 |
| flock_struct.l_len = SvIV( *sv_len ); |
| 61 |
|
| |
| 62 |
|
| /* Now call fcntl(2) - if we want the lock immediately but some other |
| 63 |
|
| process is holding it we return 'undef' (people can find out about |
| 64 |
|
| the reasons by checking errno). The same happens if we wait for the |
| 65 |
|
| lock but receive a signal before we obtain the lock. */ |
| 66 |
|
| |
| 67 |
39 |
▶ | if ( fcntl( fd, function, &flock_struct ) != 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( fcntl( fd, function, &flock_struct ) != 0 ) |
0 |
39 |
39 |
|
| 68 |
0 |
| XSRETURN_UNDEF; |
| 69 |
|
| |
| 70 |
|
| /* Now to find out who's holding the lock we now must unpack the |
| 71 |
|
| structure we got back from fcntl(2) and store it in the hash we |
| 72 |
|
| got passed. */ |
| 73 |
|
| |
| 74 |
39 |
▶ | if ( function == F_GETLK ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( function == F_GETLK ) |
15 |
24 |
39 |
|
| 75 |
|
| { |
| 76 |
15 |
| hv_store( fs, "l_type", 6, newSViv( flock_struct.l_type ), 0 ); |
| 77 |
15 |
| hv_store( fs, "l_whence", 8, newSViv( flock_struct.l_whence ), 0 ); |
| 78 |
15 |
| hv_store( fs, "l_start", 7, newSViv( flock_struct.l_start ), 0 ); |
| 79 |
15 |
| hv_store( fs, "l_len", 5, newSViv( flock_struct.l_len ), 0 ); |
| 80 |
15 |
| hv_store( fs, "l_pid", 5, newSViv( flock_struct.l_pid ), 0 ); |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* Return the systems return value of the fcntl(2) call (which is 0) |
| 84 |
|
| but in a way that can't be mistaken as meaning false (shamelessly |
| 85 |
|
| stolen from pp_sys.c in the the Perl sources). */ |
| 86 |
|
| |
| 87 |
39 |
| RETVAL = newSVpvn( "0 but true", 10 ); |
| 88 |
|
| |
| 89 |
|
| OUTPUT: |
| 90 |
|
| RETVAL |