writev()--Write to Descriptor Using Multiple Buffers


  Syntax
 #include <sys/types.h>
 #include <sys/uio.h>

 int writev(int descriptor,
            struct iovec *io_vector[],
            int vector_length)  

  Service Program Name: QP0LLIB1

  Default Public Authority: *USE

  Threadsafe: Conditional; see Usage Notes.

The writev() function is used to write data to a file or socket descriptor. writev() provides a way for the data that is going to be written to be stored in several different buffers (scatter/gather I/O).

Note: When the write completes successfully, the S_ISUID (set-user-ID) and S_ISGID (set-group-ID) bits of the file mode will be cleared. If the write is unsuccessful, the bits are undefined.

See write()--Write to Descriptor for more information related to writing to a descriptor.


Parameters

descriptor
(Input) The descriptor to which the data is to be written. The descriptor refers to either a file or a socket.

io_vector[]
(Input) The pointer to an array of type struct iovec. struct iovec contains a sequence of pointers to buffers in which the data to be written is stored. The structure pointed to by the io_vector parameter is defined in <sys/uio.h>.
      struct iovec {
         void      *iov_base;
         size_t   iov_len;
      }

iov_base and iov_len are the only fields in iovec used by sockets. iov_base contains the pointer to a buffer and iov_len contains the buffer length. The rest of the fields are reserved.

vector_length
(Input) The number of entries in io_vector.

Authorities

No authorization is required.


Return Value

value writev() was successful. The value returned is the number of bytes actually written.
-1 writev() was not successful. The errno global variable is set to indicate the error.

Error Conditions

If writev() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.

Error condition Additional information
[EACCES]

If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems.

If writing to a socket, this error code indicates one of the following:

  • The destination address specified is a broadcast address and the socket option SO_BROADCAST was not set (with a setsockopt()).
  • The process does not have the appropriate privileges to the destination address. This error code can only be returned on a socket with an address family of AF_INET and a type of SOCK_DGRAM.
[EAGAIN]  
[EBADF]  
[EBADFID]  
[EBUSY]  
[EDAMAGE]  
[EFAULT]  
[EFBIG]

The size of the object would exceed the system allowed maximum size or the process soft file size limit. The file is a regular file, nbyte is greater than 0, and the starting offset is greater than or equal to 2 GB minus 2 bytes.

[EINTR]  
[EINVAL]

For example, the file resides in a file system that does not support large files, and the starting offset exceeds 2GB minus 2 bytes.

See Usage Notes for additional possibilities.

[EIO]  
[EJRNDAMAGE]  
[EJRNENTTOOLONG]  
[EJRNINACTIVE]  
[EJRNRCVSPC]  
[ENEWJRN]  
[ENEWJRNRCV]  
[ENOMEM]  
[ENOSPC]  
[ENOTAVAIL]  
[ENOTSAFE]  
[ERESTART]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETRUNC]  
[EUNKNOWN]  

When the descriptor refers to a socket, errno could indicate one of the following errors:

Error condition Additional information
[ECONNREFUSED]

This error code can only be returned on sockets that use a connectionless transport service.

[EDESTADDRREQ]

A destination address has not been associated with the socket pointed to by the fildes parameter. This error code can only be returned on sockets that use a connectionless transport service.

[EHOSTDOWN]

This error code can only be returned on sockets that use a connectionless transport service.

[EHOSTUNREACH]

This error code can only be returned on sockets that use a connectionless transport service.

[EINTR]  
[EMSGSIZE]

The data to be sent could not be sent atomically because the size specified by nbyte is too large.

[ENETDOWN]

This error code can only be returned on sockets that use a connectionless transport service.

[ENETUNREACH]

This error code can only be returned on sockets that use a connectionless transport service.

[ENOBUFS]  
[ENOTCONN]

This error code is returned only on sockets that use a connection-oriented transport service.

[EPIPE]  
[EUNATCH]  
[EWOULDBLOCK]  

If interaction with a file server is required to access the object, errno could indicate one of the following errors:

Error condition Additional information
[EADDRNOTAVAIL]  
[ECONNABORTED]  
[ECONNREFUSED]  
[ECONNRESET]  
[EHOSTDOWN]  
[EHOSTUNREACH]  
[ENETDOWN]  
[ENETRESET]  
[ENETUNREACH]  
[ESTALE]

If you are accessing a remote file through the Network File System, the file may have been deleted at the server.

[ETIMEDOUT]  
[EUNATCH]  


Error Messages

Message ID Error Message Text
CPE3418 E Possible APAR condition or hardware failure.
CPF3CF2 E Error(s) occurred during running of &1 API.
CPF9872 E Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E Unable to set return value or error code.
CPFA0D4 E File system error occurred. Error number &1.


Usage Notes

  1. This function will fail with error code [ENOTSAFE] when all the following conditions are true:
  2. writev() only works with sockets on which a connect() has been issued, since the call does not allow the caller to specify a destination address.

  3. writev() is an atomic operation on sockets of type SOCK_DGRAM and SOCK_RAW in that it produces one packet of data every time it is issued. For example, a writev() to a datagram socket results in a single datagram.

  4. To broadcast on an AF_INET socket, the socket option SO_BROADCAST must be set (with a setsockopt()).

  5. When using a connection-oriented transport service, all errors except [EUNATCH] and [EUNKNOWN] are mapped to [EPIPE] on an output operation when either of the following occurs:

    To get the actual error, use getsockopt() with the SO_ERROR option, or perform an input operation (for example, read()).

  6. For the file systems that do not support large files, writev() will return [EINVAL] if the starting offset exceeds 2GB minus 2 bytes, regardless of how the file was opened. For the file systems that do support large files, writev() will return [EFBIG] if the starting offset exceeds 2GB minus 2 bytes and the file was not opened for large file access.

  7. Error number [EINVAL] will be returned when the specified vector_length value is less than 1 or greater than IOV_MAX, where IOV_MAX is defined in <sys/uio.h> along with the iovec structure.


  8. QFileSvr.400 File System Differences

    When connecting to a system at release V5R4M5 and earlier, QFileSvr.400 does not support large files. Otherwise, the starting offset is limited by the file system being accessed on the server.

  9. QOPT File System Differences

    When writing to files on volumes formatted in Universal Disk Format (UDF), byte locks on the range being written are ignored.

  10. QNTC File System Differences

    QNTC ignores any resource limits set using the setrlimit()--Set resource limit API when performing writev() operations.



  11. Using this function successfully on the dev/null or /dev/zero character special file results in a return value of the total number of bytes requested to be written. No data is written to the character special file. In addition, the change and modification times for the file are updated.

  12. If the write exceeds the process soft file size limit, signal SIFXFSZ is issued.


Related Information



API introduced: V3R1

[ Back to top | UNIX-Type APIs | APIs by category ]