(root)/
gettext-0.22.4/
gettext-tools/
gnulib-lib/
pipe-filter-aux.h
       1  /* Auxiliary code for filtering of data through a subprocess.
       2     Copyright (C) 2001-2003, 2008-2023 Free Software Foundation, Inc.
       3     Written by Bruno Haible <haible@clisp.cons.org>, 2009.
       4  
       5     This program is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU General Public License as published by
       7     the Free Software Foundation, either version 3 of the License, or
       8     (at your option) any later version.
       9  
      10     This program is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU General Public License for more details.
      14  
      15     You should have received a copy of the GNU General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE.  */
      19  #if !_GL_CONFIG_H_INCLUDED
      20   #error "Please include config.h first."
      21  #endif
      22  
      23  _GL_INLINE_HEADER_BEGIN
      24  #ifndef PIPE_FILTER_AUX_INLINE
      25  # define PIPE_FILTER_AUX_INLINE _GL_INLINE
      26  #endif
      27  
      28  #if defined _WIN32 && ! defined __CYGWIN__
      29  /* In the pipe-filter-* modules we want to use the write() function that is
      30     not overridden to emulate SIGPIPE behaviour, because we don't want force
      31     the caller to do
      32       signal (SIGPIPE, SIG_DFL);
      33     To reproduce the problem, use a gnulib testdir for the modules
      34     'pipe-filter-gi', 'write', 'sigpipe'.  */
      35  # undef write
      36  # define write _write
      37  #endif
      38  
      39  #ifndef SSIZE_MAX
      40  # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
      41  #endif
      42  #ifdef _AIX
      43  /* On AIX, despite having select() and despite having put the file descriptor
      44     in non-blocking mode, it can happen that select() reports that fd[1] is
      45     writable but writing a large amount of data to fd[1] then fails with errno
      46     EAGAIN.  Seen with test-pipe-filter-gi1 on AIX 7.2, with data sizes of
      47     29 KB.  So, limit the size of data passed to the write() call to 4 KB.  */
      48  # undef SSIZE_MAX
      49  # define SSIZE_MAX 4096
      50  #endif
      51  
      52  /* We use a child process, and communicate through a bidirectional pipe.
      53     To avoid deadlocks, let the child process decide when it wants to read
      54     or to write, and let the parent behave accordingly.  The parent uses
      55     select() to know whether it must write or read.  On platforms without
      56     select(), we use non-blocking I/O.  (This means the parent is busy
      57     looping while waiting for the child.  Not good.  But hardly any platform
      58     lacks select() nowadays.)  */
      59  
      60  /* On BeOS and OS/2 kLIBC select() works only on sockets, not on normal file
      61     descriptors.  */
      62  #if defined __BEOS__ || defined __KLIBC__
      63  # undef HAVE_SELECT
      64  #endif
      65  
      66  #ifdef EINTR
      67  
      68  /* EINTR handling for close(), read(), write(), select().
      69     These functions can return -1/EINTR even though we don't have any
      70     signal handlers set up, namely when we get interrupted via SIGSTOP.  */
      71  
      72  PIPE_FILTER_AUX_INLINE int
      73  nonintr_close (int fd)
      74  {
      75    int retval;
      76  
      77    do
      78      retval = close (fd);
      79    while (retval < 0 && errno == EINTR);
      80  
      81    return retval;
      82  }
      83  #undef close /* avoid warning related to gnulib module unistd */
      84  #define close nonintr_close
      85  
      86  PIPE_FILTER_AUX_INLINE ssize_t
      87  nonintr_read (int fd, void *buf, size_t count)
      88  {
      89    ssize_t retval;
      90  
      91    do
      92      retval = read (fd, buf, count);
      93    while (retval < 0 && errno == EINTR);
      94  
      95    return retval;
      96  }
      97  #undef read /* avoid warning related to gnulib module unistd */
      98  #define read nonintr_read
      99  
     100  PIPE_FILTER_AUX_INLINE ssize_t
     101  nonintr_write (int fd, const void *buf, size_t count)
     102  {
     103    ssize_t retval;
     104  
     105    do
     106      retval = write (fd, buf, count);
     107    while (retval < 0 && errno == EINTR);
     108  
     109    return retval;
     110  }
     111  #undef write /* avoid warning on VMS */
     112  #define write nonintr_write
     113  
     114  #endif
     115  
     116  /* Non-blocking I/O.  */
     117  #if HAVE_SELECT
     118  # define IS_EAGAIN(errcode) 0
     119  #else
     120  # ifdef EWOULDBLOCK
     121  #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN || (errcode) == EWOULDBLOCK)
     122  # else
     123  #  define IS_EAGAIN(errcode) ((errcode) == EAGAIN)
     124  # endif
     125  #endif
     126  
     127  _GL_INLINE_HEADER_END