Question about fcntl64
Adhemerval Zanella
adhemerval.zanella@linaro.org
Thu Sep 13 12:50:00 GMT 2018
On 13/09/2018 05:09, Florian Weimer wrote:
> On 09/13/2018 03:10 AM, Hongzhi, Song wrote:
>> 1.part of source code: ltp/testcases/kernel/syscalls/fcntl/fcntl34.c
>> ---
>> struct flock64 lck = {
>> Â Â Â Â Â Â Â Â Â .l_whence = SEEK_SET,
>>        .l_start = 0,
>> --->   .l_len   = 1, <---
>> };
>> lck.l_type = F_WRLCK
>> SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
>> ---
>>
>> 2.SAFE_FCNTL is defined as a macro:
>> ---
>> #define SAFE_FCNTL(fd, cmd, ...)Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â \
>> Â Â Â ---> ({int tst_ret_ = fcntl(fd, cmd, ##__VA_ARGS__);Â Â Â Â \ <---
>>       tst_ret_ == -1 ?                                 \
>>        tst_brk(TBROK | TERRNO,                         \
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
>> Â Â Â Â Â : tst_ret_;})
>> ---
>
> The expectation in glibc 2.28 is that you use fcntl64 with struct flock64, just as you would use struct stat64 with fstat64. (Without -D_FILE_OFFSET_BITS=64, of course.)
>
> Thanks,
> Florian
To give more context to Florian's answer, it was indeed changed on 2.28
to fix BZ#20251 "Fix Linux fcntl OFD locks for non-LFS architectures"
(commit id 06ab719d30b01da401150068054d3b8ea93dd12f).
The issue was 32 bits programs without _FILE_OFFSET_BITS=64 or
_LARGEFILE64_SOURCE interfaces, the struct flock members for OFD flags
were treated as 32-bits along fcntl64 syscall and it ended up passing
garbage to kernel.
We decided to explicit requite fcntl64 with flock64 mainly because
1. The fnctl variadic interface forces us to chose which combination
we support (because we can't guess which type in the incoming
OFD lock structure and flock used is also used on old flock
interface).
1. when OFD was included in Linux it explicitly disabled 32-bits
offset support. So to actually use OFD locks you need to use
fcntl64 syscalls.
2. consistency with other LFS interfaces where *64 interfaces expects
*64 inputs (pread and off_t for instance).
So the combination of OFD locks/fcntl and OFD locks64/fcntl64 will both
work (for locks/fcntl glibc will internally convert to locks64 and handle
overflow if the case), however flock64/fcntl or flock/fcntl64 are both not
supported.
More information about the Libc-help
mailing list