read, readx, read64x, readv, readvx, eread, ereadv, pread, or preadv Subroutine

Purpose

Reads from a file.

Library

Item Description
read, readx, readv, readvx, read64x, pread, preadv Standard C Library (libc.a)
eread, eread MLS library (libmls.a)

Syntax

#include <unistd.h>
ssize_t read (FileDescriptorBufferNBytes)
int FileDescriptor;
void * Buffer;
size_t NBytes;
int readx (FileDescriptorBufferNBytesExtension)
int FileDescriptor;
char * Buffer;
unsigned int NBytes;
int Extension;
int read64x (FileDescriptorBufferNBytesExtension)
int FileDescriptor;
void *Buffer;
size_t NBytes;
void *Extension;
ssize_t pread (int fildes, void *buf, size_t nbyte, off_t offset);
#include <sys/uio.h>
ssize_t readv (FileDescriptorioviovCount)
int FileDescriptor;
const struct iovec * iov;
int iovCount;
ssize_t readvx (FileDescriptorioviovCountExtension)
int FileDescriptor;
struct iovec *iov;
int iovCount;
int Extension;
#include <unistd.h>
#include <sys/uio.h>
ssize_t preadv ( 
int  FileDescriptor,
const struct iovec * iov,
int iovCount,
offset_t offset);
ssize_t eread (FileDescriptor, Buffer, Nbytes,  labels)
int FileDescriptor;
const void * Buffer;
size_t  NBytes;
sec_labels_t * labels;
ssize_t ereadv (FileDescriptor, iov, iovCount, labels)
int FileDescriptor;
const struct iovec * iov;
int iovCount;
sec_labels_t * labels;

Description

The read subroutine attempts to read NBytes of data from the file that is associated with the FileDescriptor parameter into the buffer pointed to by the Buffer parameter.

The readv subroutine performs the same action but scatters the input data into the iovCount buffers specified by the array of iovec structures pointed to by the iov parameter. Each iovec entry specifies the base address and length of an area in memory where data must be placed. The readv subroutine always fills an area completely before it proceeds to the next.

The readx and readvx subroutines are the same as the read and readv subroutines, respectively, with the addition of an Extension parameter, which is needed when reading from some device drivers and when reading directories. While directories can be read directly, the opendir and readdir calls be used instead, as it is a more portable interface.

On regular files and devices capable of seeking, the read starts at a position in the file that is given by the file pointer that is associated with the FileDescriptor parameter. Upon return from the read subroutine, the file pointer is incremented by the number of bytes actually read.

Devices that are incapable of seeking always read from the current position. The value of a file pointer that is associated with such a file is undefined.

On directories, the readvx subroutine starts at the position that is specified by the file pointer that is associated with theFileDescriptor parameter. The value of this file pointer must be either 0 or a value that the file pointer had immediately after a previous call to the readvx subroutine on this directory. Upon return from the readvx subroutine, the file pointer increments by a number that does not correspond to the number of bytes copied into the buffers.

When the system is attempting to read from an empty pipe (first-in-first-out (FIFO)):

  • If no process has the pipe open for writing, the read returns 0 to indicate end-of-file.
  • If some process, has the pipe open for writing:
    • If O_NDELAY and O_NONBLOCK are clear (the default), the read blocks until some data is written or the pipe is closed by all processes that open the pipe for writing.
    • If O_NDELAY is set, the read subroutine returns a value of 0.
    • If O_NONBLOCK is set, the read subroutine returns a value of -1 and sets the global variable errno to EAGAIN.

When the system is attempting to read from a character special file that supports nonblocking reads, such as a terminal, and no data is available:

  • If O_NDELAY and O_NONBLOCK are clear (the default), the read subroutine blocks until data becomes available.
  • If O_NDELAY is set, the read subroutine returns 0.
  • If O_NONBLOCK is set, the read subroutine returns -1 and sets the errno global variable to EAGAIN if no data is available.

When the system is attempting to read a regular file that supports enforcement mode record locks, and all or part of the region to be read is locked by another process:

  • If O_NDELAY and O_NONBLOCK are clear, the read blocks the calling process until the lock is released.
  • If O_NDELAY or O_NONBLOCK is set, the read returns -1 and sets the global variable errno toEAGAIN.

The behavior of an interrupted read subroutine depends on how the handler for the arriving signal was installed.

If the handler was installed, with an indication that subroutines must not be restarted, the read subroutine returns a value of -1 and the global variable errno is set to EINTR (even if some data was already removed).

If the handler was installed, with an indication that subroutines must be restarted:

  • If no data was read when the interrupt was handled, this read returns no value (it is restarted).
  • If data was read when the interrupt was handled, this read subroutine returns the amount of data removed.

The read64x subroutine is the same as the readx subroutine, where the Extension parameter is a pointer to a j2_ext structure (see the j2/j2_cntl.h file). The read64x subroutine is used to read an encrypted file in raw mode (see O_RAW in the fcntl.h file). Using the O_RAW flag on encrypted files has the same limitations as using O_DIRECT on regular files.

The eread and ereadv subroutines read from the stream and retrieve the message. The eread subroutine copies the number of bytes of the data from the buffer to a stream associated with the FileDescriptor parameter. The Nbyte parameter specifies the number of bytes. The Buffer parameter points to the buffer. Security information is returned in the structure pointed to by the labels parameter.

The pread function performs the same action as read, except that it reads from a given position in the file without changing the file pointer. The first three arguments to pread are the same as read with the addition of a fourth argument that is offset for the wanted position inside the file. An attempt to perform a pread on a file that is incapable of seeking results in an error.

ssize_t pread64(int fildes , void *buf , size_t nbytes , off64_t offset)

The pread64 subroutine performs the same action as pread but the limit of offset to the maximum file size for the file that is associated with the file Descriptor and DEV_OFF_MAX if the file associated with file Descriptor is a block special or character special file. If fildes refers to a socket, read is equivalent to the recv subroutine with no flags set.

Using the read or pread subroutine with a file descriptor obtained from a call to the shm_open subroutine fails with ENXIO.

The preadv subroutine performs the same action as the readv subroutine, except that the preadv subroutine reads from a given position in the file without changing the file pointer. The first three arguments of the preadv subroutine are the same as the readv subroutine with the addition of the offset argument that points to the position that you want inside the file. An error occurs when the file that the preadv subroutine reads from is incapable of seeking.

Parameters

Item Description
FileDescriptor A file descriptor that is identifying the object to be read.
Extension Provides communication with character device drivers that require more information or return extra status. Each driver interprets the Extension parameter in a device-dependent way, either as a value or as a pointer to a communication area. Drivers must apply reasonable defaults when the value of the Extension parameter is 0.

For directories, the Extension parameter determines the format in which directory entries must be returned:

  • If the value of the Extension parameter is 0, the format in which directory entries are returned depends on the value of the real directory read flag (described in the ulimit subroutine).
  • If the calling process does not have the real directory read flag set, the buffers are filled with an array of directory entries that are truncated to fit the format of the System V directory structure. This process provides compatibility with programs written for UNIX System V.
  • If the calling process has the real directory read flag set (see the ulimit subroutine), the buffers are filled with an image of the underlying implementation of the directory.
  • If the value of the Extension parameter is 1, the buffers are filled with consecutive directory entries in the format of adirent structure. This process is logically equivalent to the readdir subroutine.
  • Other values of the Extension parameter are reserved.

For tape devices, the Extension parameter determines the response of the readx subroutine when the tape drive is in variable block mode and the read request is for less than the tape's block size.

  • If the value of the Extension parameter is TAPE_SHORT_READ, the readx subroutine returns the number of bytes requested and sets the errno global variable to a value of 0.
  • If the value of the Extension parameter is 0, the readx subroutine returns a value of 0 and sets the errno global variable to ENOMEM.
iov Points to an array of iovec structures that identifies the buffers into which the data is to be placed. The iovec structure is defined in the sys/uio.h file and contains the following members:
caddr_t iov_base;
size_t iov_len;
iovCount Specifies the number of iovec structures pointed to by the iov parameter.
Buffer Points to the buffer.
NBytes Specifies the number of bytes read from the file that is associated with theFileDescriptor parameter.
Note: When reading tapes, the read subroutines use a physical tape block on each call to the subroutine. If the physical data block size is larger than specified by the Nbytes parameter, an error is returned, since all of the data from the read does not fit into the buffer that is specified by the read.

To avoid read errors that are caused by unknown blocking sizes on tapes, set the NBytes parameter to a large value (such as 32K bytes).

offset The position in the file where the reading begins.
labels Points to the extended security attribute structure.

Return Values

Upon successful completion, the read, readx, read64x, readv, readvx, pread, and preadv subroutines return the number of bytes read and placed into buffers. The system guarantees to read the number of bytes requested if the descriptor references a normal file that has the same number of bytes left before the end of the file is reached, but in no other case.

A value of 0 is returned when the end of the file is reached. (For information about communication files, see the ioctl and termio files.)

Otherwise, a value of -1 is returned, the global variable errno is set to identify the error, and the content of the buffer pointed to by the Buffer or iov parameter is indeterminate.

Upon successful completion, the eread and ereadv subroutines return a value of 0. Otherwise, the global variable errno is set to identify the error.

Error Codes

The read, readx, read64x, readv, readvx, pread, eread, ereadv, and preadv subroutines are unsuccessful if one or more of the following are true:

Item Description
EBADMSG The file is a STREAM file that is set to control-normal mode and the message that is waiting to be read includes a control part.
EBADF The FileDescriptor parameter is not a valid file descriptor open for reading.
EINVAL The file position pointer that is associated with the FileDescriptor parameter was negative.
EINVAL The sum of the iov_len values in the iov array was negative or overflowed a 32-bit integer.
EINVAL The value of the iovCount parameter was not 1 - 16, inclusive.
EINVAL The value of the Nbytes parameter that is larger than OFF_MAX, was requested on the 32-bit kernel. This issue is a case where the system call is requested from a 64-bit application that is running on a 32-bit kernel.
Item Description
EINVAL The STREAM or multiplexer that is referenced by FileDescriptor is linked (directly or indirectly) downstream from a multiplexer.
EAGAIN The file was marked for non-blocking I/O, and no data was ready to be read.
EFAULT The Buffer or part of the iov points to a location outside of the allocated address space of the process.
EFAULT The user does not have authority to access the Buffer.
EDEADLK A deadlock would occur if the calling process were to sleep until the region to be read was unlocked.
EINTR A read was interrupted by a signal before any data arrived, and the signal handler was installed with an indication that subroutines are not to be restarted.
EIO An I/O error occurred while reading from the file system.
EIO The process is a member of a background process that is attempting to read from its controlling terminal, and either the process is ignoring or blocking the SIGTTIN signal or the process group has no parent process.
EFBIG An offset greater than MAX_FILESIZE was requested on the 32-bit kernel.
ENXIO

The read or pread subroutine was used with a file descriptor obtained from a call to the shm_open subroutine.

Item Description
EOVERFLOW An attempt was made to read from a regular file where NBytes was greater than zero and the starting offset was before the end-of-file and was greater than or equal to the offset maximum established in the open file description that is associated with FileDescriptor.

The read, readx, readv, readvx, pread, and preadv subroutines might be unsuccessful if the following is true:

Item Description
ENXIO A request was made of a nonexistent device, or the request was outside the capabilities of the device.
ESPIPE fildes is associated with a pipe or FIFO.

If Network File System (NFS) is installed on the system, the read system call can also fail if the following is true:

Item Description
ETIMEDOUT The connection that is timed out.
The read64x subroutine was unsuccessful if the EINVAL error code is returned:
Item Description
EINVAL The j2_ext structure was not initialized correctly. For example, the version was wrong, or the file was not encrypted.
EINVAL The j2_ext structure was passed issuing the J2EXTCMD_RDRAW command for files that were not opened in raw-mode.
The eread and ereadv subroutines were unsuccessful if one of the following error codes is true:
Item Description
ENOMEM The memory or space is too small.
EACCES Permission Denied. The user has insufficient privileges to read data.
ERESTART ERESTART is used to determine if whether a system call is restartable or not.
The readv subroutine was unsuccessful if the following error code is true:
Item Description
EINVAL The value of the iovCount parameter is greater than 15.