|
Packit |
709fb3 |
/* An interface to read and write that retries after interrupts.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
Copyright (C) 1993-1994, 1998, 2002-2006, 2009-2017 Free Software
|
|
Packit |
709fb3 |
Foundation, Inc.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is free software: you can redistribute it and/or modify
|
|
Packit |
709fb3 |
it under the terms of the GNU General Public License as published by
|
|
Packit |
709fb3 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit |
709fb3 |
(at your option) any later version.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
This program is distributed in the hope that it will be useful,
|
|
Packit |
709fb3 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
709fb3 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
709fb3 |
GNU General Public License for more details.
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
You should have received a copy of the GNU General Public License
|
|
Packit |
709fb3 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <config.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* Specification. */
|
|
Packit |
709fb3 |
#ifdef SAFE_WRITE
|
|
Packit |
709fb3 |
# include "safe-write.h"
|
|
Packit |
709fb3 |
#else
|
|
Packit |
709fb3 |
# include "safe-read.h"
|
|
Packit |
709fb3 |
#endif
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* Get ssize_t. */
|
|
Packit |
709fb3 |
#include <sys/types.h>
|
|
Packit |
709fb3 |
#include <unistd.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <errno.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#ifdef EINTR
|
|
Packit |
709fb3 |
# define IS_EINTR(x) ((x) == EINTR)
|
|
Packit |
709fb3 |
#else
|
|
Packit |
709fb3 |
# define IS_EINTR(x) 0
|
|
Packit |
709fb3 |
#endif
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#include <limits.h>
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
#ifdef SAFE_WRITE
|
|
Packit |
709fb3 |
# define safe_rw safe_write
|
|
Packit |
709fb3 |
# define rw write
|
|
Packit |
709fb3 |
#else
|
|
Packit |
709fb3 |
# define safe_rw safe_read
|
|
Packit |
709fb3 |
# define rw read
|
|
Packit |
709fb3 |
# undef const
|
|
Packit |
709fb3 |
# define const /* empty */
|
|
Packit |
709fb3 |
#endif
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
/* Read(write) up to COUNT bytes at BUF from(to) descriptor FD, retrying if
|
|
Packit |
709fb3 |
interrupted. Return the actual number of bytes read(written), zero for EOF,
|
|
Packit |
709fb3 |
or SAFE_READ_ERROR(SAFE_WRITE_ERROR) upon error. */
|
|
Packit |
709fb3 |
size_t
|
|
Packit |
709fb3 |
safe_rw (int fd, void const *buf, size_t count)
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
/* Work around a bug in Tru64 5.1. Attempting to read more than
|
|
Packit |
709fb3 |
INT_MAX bytes fails with errno == EINVAL. See
|
|
Packit |
709fb3 |
<http://lists.gnu.org/archive/html/bug-gnu-utils/2002-04/msg00010.html>.
|
|
Packit |
709fb3 |
When decreasing COUNT, keep it block-aligned. */
|
|
Packit |
709fb3 |
enum { BUGGY_READ_MAXIMUM = INT_MAX & ~8191 };
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
for (;;)
|
|
Packit |
709fb3 |
{
|
|
Packit |
709fb3 |
ssize_t result = rw (fd, buf, count);
|
|
Packit |
709fb3 |
|
|
Packit |
709fb3 |
if (0 <= result)
|
|
Packit |
709fb3 |
return result;
|
|
Packit |
709fb3 |
else if (IS_EINTR (errno))
|
|
Packit |
709fb3 |
continue;
|
|
Packit |
709fb3 |
else if (errno == EINVAL && BUGGY_READ_MAXIMUM < count)
|
|
Packit |
709fb3 |
count = BUGGY_READ_MAXIMUM;
|
|
Packit |
709fb3 |
else
|
|
Packit |
709fb3 |
return result;
|
|
Packit |
709fb3 |
}
|
|
Packit |
709fb3 |
}
|