[PATCH] posix: Fix open file action for posix_spawn on Linux
Rasmus Villemoes
rv@rasmusvillemoes.dk
Tue Sep 20 19:54:00 GMT 2016
On Mon, Sep 19 2016, Florian Weimer <fweimer@redhat.com> wrote:
> On 09/19/2016 02:41 PM, Adhemerval Zanella wrote:
>
>> +/* Return if file descriptor is opened. */
>> +static inline int
>> +fd_is_valid (int fd)
>> +{
>> + return fcntl_not_cancel_2 (fd, F_GETFD) != -1 || errno != EBADF;
>> +}
>> +
>> /* Function used in the clone call to setup the signals mask, posix_spawn
>> attributes, and file actions. It run on its own stack (provided by the
>> posix_spawn call). */
>> @@ -219,6 +226,15 @@ __spawni_child (void *arguments)
>>
>> case spawn_do_open:
>> {
>> + /* POSIX states that if fildes was already an open file descriptor,
>> + it shall be closed before the new file is opened. This avoid
>> + pontential issues when posix_spawn plus addopen action is called
>> + with the process already at maximum number of file descriptor
>> + opened and also for multiple actions on single-open special
>> + paths (like /dev/watchdog). */
>> + if (fd_is_valid (action->action.open_action.fd))
>> + close_not_cancel (action->action.open_action.fd);
>
> It's not clear to me why you can't just close the file descriptor
> unconditionally. It does not seem to matter whether you perform fcntl
> or close on an invalid file descriptor.
+1. Just do close+open+(if necessary dup2+close). In practice, I suppose
that actually ends up doing the fewest syscalls, assuming this is mostly
used for setting up the stdio fds.
Rasmus
More information about the Libc-alpha
mailing list