On 8/7/19 10:56 AM, Jakub Jelinek wrote:
> On Wed, Aug 07, 2019 at 10:45:08AM +0200, Martin Liška wrote:
>> @@ -155,3 +156,16 @@ lrealpath (const char *filename)
>>    /* This system is a lost cause, just duplicate the filename.  */
>>    return strdup (filename);
>>  }
>> +
>> +/* Return true when FD file descriptor exists.  */
>> +
>> +int
>> +fd_exists (int fd)
>> +{
>> +#if defined(_WIN32)
>> +  HANDLE h = (HANDLE) _get_osfhandle (fd);
>> +  return h != (HANDLE) -1;
>> +#else
>> +  return fcntl (fd, F_GETFD) >= 0;
>> +#endif
>> +}
> 
> Judging from gnulib
> http://erislabs.net/gitweb/?p=gnulib.git;a=commitdiff_plain;h=021c8619190757f535c72ad5cdb1d624e19620d6
> the #if condition for Windows should be probably different
> and it would be worth to have a fallback for when F_GETFD isn't defined
> e.g. through dup2 (fd, fd) < 0.

Thank you for the review. I updated the patch accordingly.

Martin

> 
> And for the libiberty changes you want Ian to review them.
> 
>       Jakub
> 

>From 7d3593a4189bd970ae752abab36c8a5bc4681847 Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Tue, 6 Aug 2019 13:04:40 +0200
Subject: [PATCH] Fix file descriptor existence of MinGW.

gcc/ChangeLog:

2019-08-07  Martin Liska  <mli...@suse.cz>

	PR bootstrap/91352
	* gcc.c (driver::detect_jobserver): Use fd_exists.
	* lto-wrapper.c (jobserver_active_p): Likewise.

include/ChangeLog:

2019-08-07  Martin Liska  <mli...@suse.cz>

	PR bootstrap/91352
	* libiberty.h (fd_exists): New function.

libiberty/ChangeLog:

2019-08-07  Martin Liska  <mli...@suse.cz>

	PR bootstrap/91352
	* lrealpath.c (fd_exists): New function.
---
 gcc/gcc.c             |  4 ++--
 gcc/lto-wrapper.c     |  4 ++--
 include/libiberty.h   |  4 ++++
 libiberty/lrealpath.c | 16 ++++++++++++++++
 4 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 18a07426290..e86b35453c2 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -8380,8 +8380,8 @@ driver::detect_jobserver () const
 	    = (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) == 2
 	       && rfd > 0
 	       && wfd > 0
-	       && fcntl (rfd, F_GETFD) >= 0
-	       && fcntl (wfd, F_GETFD) >= 0);
+	       && fd_exists (rfd)
+	       && fd_exists (wfd));
 
 	  /* Drop the jobserver if it's not working now.  */
 	  if (!jobserver)
diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index 3414adedd26..a3b0130b7cb 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -1237,8 +1237,8 @@ jobserver_active_p (void)
   return (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) == 2
 	  && rfd > 0
 	  && wfd > 0
-	  && fcntl (rfd, F_GETFD) >= 0
-	  && fcntl (wfd, F_GETFD) >= 0);
+	  && fd_exists (rfd)
+	  && fd_exists (wfd));
 }
 
 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
diff --git a/include/libiberty.h b/include/libiberty.h
index 635519e088a..254c00bcc6b 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -137,6 +137,10 @@ extern const char *unix_lbasename (const char *) ATTRIBUTE_RETURNS_NONNULL ATTRI
 
 extern char *lrealpath (const char *);
 
+/* Return true when FD file descriptor exists.  */
+
+extern int fd_exists (int fd);
+
 /* Concatenate an arbitrary number of strings.  You must pass NULL as
    the last argument of this function, to terminate the list of
    strings.  Allocates memory using xmalloc.  */
diff --git a/libiberty/lrealpath.c b/libiberty/lrealpath.c
index 7f66dc2b1bd..fcf9c0bb9bb 100644
--- a/libiberty/lrealpath.c
+++ b/libiberty/lrealpath.c
@@ -49,6 +49,7 @@ components will be simplified.  The returned value will be allocated using
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#include <fcntl.h>
 
 /* On GNU libc systems the declaration is only visible with _GNU_SOURCE.  */
 #if defined(HAVE_CANONICALIZE_FILE_NAME) \
@@ -155,3 +156,18 @@ lrealpath (const char *filename)
   /* This system is a lost cause, just duplicate the filename.  */
   return strdup (filename);
 }
+
+/* Return true when FD file descriptor exists.  */
+
+int
+fd_exists (int fd)
+{
+#if defined(_WIN32)
+  HANDLE h = (HANDLE) _get_osfhandle (fd);
+  return h != (HANDLE) -1;
+#elif defined(F_GETFD)
+  return fcntl (fd, F_GETFD) >= 0;
+#else
+  return dup2 (fd, fd) < 0;
+#endif
+}
-- 
2.22.0

Reply via email to