[PATCH 2/3] io: Add closefrom [BZ #10353]
Adhemerval Zanella
adhemerval.zanella@linaro.org
Tue Dec 22 12:41:18 GMT 2020
On 22/12/2020 08:57, Florian Weimer wrote:
> * Adhemerval Zanella:
>
>>>> +weak_alias (__closefrom, closefrom)
>>>> diff --git a/sysdeps/unix/sysv/linux/closefrom_fallback.c b/sysdeps/unix/sysv/linux/closefrom_fallback.c
>>>> new file mode 100644
>>>> index 0000000000..48815941dd
>>>> --- /dev/null
>>>> +++ b/sysdeps/unix/sysv/linux/closefrom_fallback.c
>>>
>>>> +/* Fallback code: iterates over /proc/self/fd, closing each file descriptor
>>>> + that fall on the criteria. */
>>>> +_Bool
>>>> +__closefrom_fallback (int from)
>>>> +{
>>>> + int dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY,
>>>> + 0);
>>>> + if (dirfd == -1)
>>>> + return false;
>>>
>>> You could try to close a few descriptors if the error is ENOENT and try
>>> again.
>>
>> Do you mean EMFILE and issue a close on [from, ...] and then try again?
>> I am not really fan of this heuristics, it might mitigate the failure if
>> limit of file-descriptors are reached, but it would really depend whether
>> if 'from' is close of the first open descriptor and the how many close
>> we issue.
>
> Right, but I think it's needed for correctness. closefrom (3) should
> work even if all descriptors are open.
Alright, I think we can do something like:
int dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY, 0);
if (dirfd == -1 && errno == EMFILE)
{
int maxfd = __getdtablesize ();
for (int i = lowfd; i < maxfd; i++)
if (__close_nocancel (i) == 0)
break;
dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY, 0);
if (dirfd == -1)
goto err;
}
More information about the Libc-alpha
mailing list