]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
sim: use gnulib to set nonblocking mode users/vapier/sim/gnulib
authorMike Frysinger <vapier@gentoo.org>
Tue, 8 Jun 2021 22:06:07 +0000 (18:06 -0400)
committerMike Frysinger <vapier@gentoo.org>
Mon, 1 Jan 2024 23:23:28 +0000 (18:23 -0500)
Replace various custom ad-hoc fcntl/O_NONBLOCK implementations with
the gnulib nonblocking module.  This makes our code much tidier and
portable to other systems (e.g. Windows).

sim/bfin/dv-bfin_emac.c
sim/common/dv-sockser.c
sim/common/sim-io.c
sim/ppc/main.c

index 1ab7cd63554dafd188566e805d6bf779224671be..82a2ef247827c7a164b1eb99519d02d758cf1e62 100644 (file)
@@ -35,6 +35,8 @@
 #include <linux/if_tun.h>
 #endif
 
+#include "nonblocking.h"
+
 #ifdef HAVE_LINUX_IF_TUN_H
 # define WITH_TUN 1
 #else
@@ -567,8 +569,7 @@ bfin_emac_tap_init (struct hw *me)
       return;
     }
 
-  flags = fcntl (emac->tap, F_GETFL);
-  fcntl (emac->tap, F_SETFL, flags | O_NONBLOCK);
+  set_nonblocking_flag (emac->tap, true);
 #endif
 }
 
index 698cab839209db5d7977a8963f084d20a7d8d1f4..82f45cdfa9787d2697d2cdbc0a5864a21bfdc2b6 100644 (file)
@@ -37,6 +37,8 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <sys/time.h>
 #include <sys/types.h>
 
+#include "nonblocking.h"
+
 #include "sim-main.h"
 #include "sim-assert.h"
 #include "sim-options.h"
@@ -253,17 +255,13 @@ connected_p (SIM_DESC sd)
     return 0;
 
   /* Set non-blocking i/o.  */
-#if defined(F_GETFL) && defined(O_NONBLOCK)
-  flags = fcntl (sockser_fd, F_GETFL);
-  flags |= O_NONBLOCK;
-  if (fcntl (sockser_fd, F_SETFL, flags) == -1)
+  if (set_nonblocking_flag (sockser_fd, true))
     {
       sim_io_eprintf (sd, "unable to set nonblocking i/o");
       close (sockser_fd);
       sockser_fd = -1;
       return 0;
     }
-#endif
   return 1;
 }
 
index fc0d42faf117cce485634fe3a792b2de2b59f1e2..5a899844deec1b6fba5c9edbddba57a74c3e3754 100644 (file)
@@ -33,6 +33,8 @@
 
 #undef open
 
+#include "nonblocking.h"
+
 #include "sim-main.h"
 #include "sim-io.h"
 #include "sim/callback.h"
@@ -347,23 +349,20 @@ sim_io_poll_read (SIM_DESC sd,
                  char *buf,
                  int sizeof_buf)
 {
-#if defined(O_NONBLOCK) && defined(F_GETFL) && defined(F_SETFL)
   int fd = STATE_CALLBACK (sd)->fdmap[sim_io_fd];
-  int flags;
   int status;
   int nr_read;
   int result;
   STATE_CALLBACK (sd)->last_errno = 0;
   /* get the old status */
-  flags = fcntl (fd, F_GETFL, 0);
-  if (flags == -1)
+  status = get_nonblocking_flag (fd);
+  if (status == -1)
     {
-      perror ("sim_io_poll_read");
+      perror ("sim_io_read_stdin");
       return 0;
     }
   /* temp, disable blocking IO */
-  status = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
-  if (status == -1)
+  if (status == 0 && set_nonblocking_flag (fd, true) == -1)
     {
       perror ("sim_io_read_stdin");
       return 0;
@@ -381,16 +380,12 @@ sim_io_poll_read (SIM_DESC sd,
       STATE_CALLBACK (sd)->last_errno = errno;
     }
   /* return to regular vewing */
-  status = fcntl (fd, F_SETFL, flags);
-  if (status == -1)
+  if (status == 0 && set_nonblocking_flag (fd, false) == -1)
     {
       perror ("sim_io_read_stdin");
       /* return 0; */
     }
   return result;
-#else
-  return sim_io_read (sd, sim_io_fd, buf, sizeof_buf);
-#endif
 }
 
 int
index aa1c85e0af913b90d66e94c5fbd432c1bd1ebfef..fb2b1b8bda6cf0bec4bb042d99bf06111d4b31ff 100644 (file)
@@ -26,6 +26,8 @@
 
 #include <signal.h>
 
+#include "nonblocking.h"
+
 #include "psim.h"
 #include "options.h"
 #include "device.h" /* FIXME: psim should provide the interface */
 
 #include "environ.h"
 
-#if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL)
-#undef WITH_STDIO
-#define WITH_STDIO DO_USE_STDIO
-#endif
-
 
 static psim *simulation = NULL;
 
@@ -150,22 +147,19 @@ sim_io_read_stdin(char *buf,
     return sim_io_eof;
     break;
   case DONT_USE_STDIO:
-#if defined(O_NONBLOCK) && defined(F_GETFL) && defined(F_SETFL)
     {
       /* check for input */
-      int flags;
       int status;
       int nr_read;
       int result;
       /* get the old status */
-      flags = fcntl(0, F_GETFL, 0);
-      if (flags == -1) {
+      status = get_nonblocking_flag (0);
+      if (status == -1) {
        perror("sim_io_read_stdin");
        return sim_io_eof;
       }
       /* temp, disable blocking IO */
-      status = fcntl(0, F_SETFL, flags | O_NONBLOCK);
-      if (status == -1) {
+      if (status == 0 && set_nonblocking_flag (0, true) == -1) {
        perror("sim_io_read_stdin");
        return sim_io_eof;
       }
@@ -183,15 +177,13 @@ sim_io_read_stdin(char *buf,
          result = sim_io_eof;
       }
       /* return to regular vewing */
-      status = fcntl(0, F_SETFL, flags);
-      if (status == -1) {
+      if (status == 0 && set_nonblocking_flag (0, false) == -1) {
        perror("sim_io_read_stdin");
        return sim_io_eof;
       }
       return result;
     }
     break;
-#endif
   default:
     error("sim_io_read_stdin: invalid switch\n");
     break;