unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH] RFC: Add posix_spawn_file_actions_closefrom
Date: Fri, 24 May 2019 16:37:26 +0200	[thread overview]
Message-ID: <87sgt3q5p5.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <24658485-5030-1d8f-7236-38765f2ab5b6@linaro.org> (Adhemerval Zanella's message of "Fri, 24 May 2019 11:06:41 -0300")

* Adhemerval Zanella:

>> The test doesn't exercise the gaps case.
>
> Do you mean gaps in file descriptor initial set before posix_spawn?

Yes, where the directory descriptor is in the middle of the closefrom
range.

>>> +/* Close all file descriptor up to FROM by interacting /proc/self/fd.
>>> +   Any failure should */
>>> +static bool
>>> +spawn_closefrom (int from)
>>> +{
>>> +  /* Increasing the buffer size incurs in less getdents syscalls from
>>> +     readdir, however it would require more stack size to be allocated
>>> +     on __spawnix.  */
>>> +  char buffer[sizeof (struct __dirstream) + 2 * sizeof (struct dirent)];
>> 
>> We could allocate this on the heap, in the parent.  Maybe we could
>> opendir in the parent, and play with the underlying descriptor in the
>> child?  Then you wouldn't need to add __opendir_inplace at all.  Given
>> that we know what our implementation looks like, this should be fairly
>> safe.
>
> I don't have a strong opinion here, it would add some complexity on parent
> helper which would need to transverse all file actions, call opendir, and
> deallocate after helper process returns.  My idea is to keep the required 
> logic more in place, so its more obvious where things are initiated.

You could turn one of the padding elements in posix_spawn_file_actions_t
into a flag and have posix_spawn_file_actions_addclosefrom_np set the
flag.  Then the second iteration isn't necessary.

>>> +  DIR *dp;
>>> +  if ((dp = __opendir_inplace ("/proc/self/fd", buffer, sizeof buffer))
>>> +      == NULL)
>>> +    return false;
>> 
>> This could check for ENFILE/EMFILE/ENOMEM and try closing descriptors
>> directly in case of that error, to make room for the new descriptor.
>> But perhaps that's not worth the complexity.
>
> Hum, this could be an enhancement indeed.  However the main issue is 
> to find which is the lower opened file descriptor greater than FROM
> without polling /proc/self/fd or by using close with random file
> descriptors.

You can do close (from), close (from + 1), etc., up to a certain limit,
and retry if one of the close calls doesn't return EBADF.  The magic
limit is needed in case the closefrom does not overlap with any file
descriptors.

>>> +    {
>>> +      if (dirp->d_name[0] == '.')
>>> +        continue;
>>> +
>>> +      char *endptr;
>>> +      long int fd = strtol (dirp->d_name, &endptr, 10);
>>> +      if (*endptr != '\0' || fd < 0 || fd > INT_MAX)
>>> +	{
>>> +	  ret = false;
>>> +	  break;
>>> +	}
>>> +
>>> +      if (fd == dirfd (dp) || fd < from)
>>> +        continue;
>>> +
>>> +      __close (fd);
>>> +    }
>>> +  __closedir (dp);
>>> +
>>> +  return ret;
>>> +}
>> 
>> I'm not sure if this is entirely correct.  If we close some descriptors,
>> and then readdir calls getdents64, what will the kernel return?  Will
>> there be a gap in the descriptor list?  (Curiously, it's the same issue
>> we have the the fork handler list. 8-)
>
> It does not seems to be case with my experiments.  I hack opendir to 
> allocate the minimum workable buffer (__dirstream plus a 
> struct dirent, about 40 bytes on x86_64) to force each readdir to
> call getdents.  A simple testcase shows:

It's still looks very implementation-defined to me.  proc_readfd_common
does this:

	for (fd = ctx->pos - 2;
	     fd < files_fdtable(files)->max_fds;
	     fd++, ctx->pos++) {

And I think ctx->pos somehow corresponds to d_off.  But I don't see a
1:1 correspondence between descriptors and offsets.  I wonder whether
the single-entry case is indeed the worst-possible test case for this.

Thanks,
Florian

  reply	other threads:[~2019-05-24 14:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-21 18:48 [PATCH] RFC: Add posix_spawn_file_actions_closefrom Adhemerval Zanella
2019-05-24 11:34 ` Florian Weimer
2019-05-24 14:06   ` Adhemerval Zanella
2019-05-24 14:37     ` Florian Weimer [this message]
2019-05-24 14:55       ` Adhemerval Zanella
2019-05-27 21:02         ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87sgt3q5p5.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).