Handle jobserver file descriptors in btest.

Message ID 88d036b7-5304-3673-318f-2ebc159c8b16@suse.cz
State New
Headers
Series Handle jobserver file descriptors in btest. |

Commit Message

Martin Liška Oct. 21, 2021, 7:47 a.m. UTC
  Hi.

The patch is about sensitive handling of file descriptors opened
by make's jobserver.

Ready for master?
Thanks,
Martin

	PR testsuite/102742

libbacktrace/ChangeLog:

	* btest.c (check_open_files): Ignore file descriptors provided
	by jobserver.
---
  libbacktrace/btest.c | 16 ++++++++++++++++
  1 file changed, 16 insertions(+)
  

Comments

Ian Lance Taylor Oct. 21, 2021, 6:15 p.m. UTC | #1
On Thu, Oct 21, 2021 at 12:48 AM Martin Liška <mliska@suse.cz> wrote:
>
> The patch is about sensitive handling of file descriptors opened
> by make's jobserver.

Thanks.  I think a better approach would be, at the start of main,
fstat the descriptors up to 10 and record the ones for which fstat
succeeds.  Then at the end of main only check the descriptors for
which fstat failed earlier.

I can work on that at some point if you don't want to tackle it.

Ian
  
Martin Liška Oct. 22, 2021, 8:15 a.m. UTC | #2
On 10/21/21 20:15, Ian Lance Taylor wrote:
> On Thu, Oct 21, 2021 at 12:48 AM Martin Liška <mliska@suse.cz> wrote:
>>
>> The patch is about sensitive handling of file descriptors opened
>> by make's jobserver.
> 
> Thanks.  I think a better approach would be, at the start of main,
> fstat the descriptors up to 10 and record the ones for which fstat
> succeeds.  Then at the end of main only check the descriptors for
> which fstat failed earlier.

Sure, makes sense.

> 
> I can work on that at some point if you don't want to tackle it.

I've just done that in the attached patch.

Is it fine?
Thanks,
Martin

> 
> Ian
>
  
Ian Lance Taylor Oct. 22, 2021, 5:32 p.m. UTC | #3
On Fri, Oct 22, 2021 at 1:15 AM Martin Liška <mliska@suse.cz> wrote:
>
> On 10/21/21 20:15, Ian Lance Taylor wrote:
> > On Thu, Oct 21, 2021 at 12:48 AM Martin Liška <mliska@suse.cz> wrote:
> >>
> >> The patch is about sensitive handling of file descriptors opened
> >> by make's jobserver.
> >
> > Thanks.  I think a better approach would be, at the start of main,
> > fstat the descriptors up to 10 and record the ones for which fstat
> > succeeds.  Then at the end of main only check the descriptors for
> > which fstat failed earlier.
>
> Sure, makes sense.
>
> >
> > I can work on that at some point if you don't want to tackle it.
>
> I've just done that in the attached patch.
>
> Is it fine?

This is OK.

Thanks.

Ian
  

Patch

diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index 9f9c03babf3..a1ea93728be 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -464,9 +464,25 @@  static void
  check_open_files (void)
  {
    int i;
+  int rfd = -1;
+  int wfd = -1;
+
+#define JS_NEEDLE "--jobserver-auth="
+
+  /* Ignore file descriptors provided by jobserver.  */
+  const char *makeflags = getenv ("MAKEFLAGS");
+  if (makeflags != NULL)
+    {
+      const char *n = strstr (makeflags, JS_NEEDLE);
+      if (n != NULL)
+	sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd);
+    }
  
    for (i = 3; i < 10; i++)
      {
+      if (i == rfd || i == wfd)
+	continue;
+
        if (close (i) == 0)
  	{
  	  fprintf (stderr,