Go to the previous, next section.

Low-Level Input/Output

This chapter describes functions for performing low-level input/output operations on file descriptors. These functions include the primitives for the higher-level I/O functions described in section Input/Output on Streams, as well as functions for performing low-level control operations for which there are no equivalents on streams.

Stream-level I/O is more flexible and usually more convenient; therefore, programmers generally use the descriptor-level functions only when necessary. These are some of the usual reasons:

Opening and Closing Files

This section describes the primitives for opening and closing files using file descriptors. The open and creat functions are declared in the header file `fcntl.h', while close is declared in `unistd.h'.

Function: int open (const char *filename, int flags[, mode_t mode])

The open function creates and returns a new file descriptor for the file named by filename. Initially, the file position indicator for the file is at the beginning of the file. The argument mode is used only when a file is created, but it doesn't hurt to supply the argument in any case.

The flags argument controls how the file is to be opened. This is a bit mask; you create the value by the bitwise OR of the appropriate parameters (using the `|' operator in C).

The flags argument must include exactly one of these values to specify the file access mode:

O_RDONLY
Open the file for read access.

O_WRONLY
Open the file for write access.

O_RDWR
Open the file for both reading and writing.

The flags argument can also include any combination of these flags:

O_APPEND
If set, then all write operations write the data at the end of the file, extending it, regardless of the current file position.

O_CREAT
If set, the file will be created if it doesn't already exist.

O_EXCL
If both O_CREAT and O_EXCL are set, then open fails if the specified file already exists.

O_NOCTTY
If filename names a terminal device, don't make it the controlling terminal for the process. See section Job Control, for information about what it means to be the controlling terminal.

O_NONBLOCK
This sets nonblocking mode. This option is usually only useful for special files such as FIFOs (see section Pipes and FIFOs) and devices such as terminals. Normally, for these files, open blocks until the file is "ready". If O_NONBLOCK is set, open returns immediately.

The O_NONBLOCK bit also affects read and write: It permits them to return immediately with a failure status if there is no input immediately available (read), or if the output can't be written immediately (write).

O_TRUNC
If the file exists and is opened for write access, truncate it to zero length. This option is only useful for regular files, not special files such as directories or FIFOs.

For more information about these symbolic constants, see section File Status Flags.

The normal return value from open is a non-negative integer file descriptor. In the case of an error, a value of -1 is returned instead. In addition to the usual file name syntax errors (see section File Name Errors), the following errno error conditions are defined for this function:

EACCES
The file exists but is not readable/writable as requested by the flags argument.

EEXIST
Both O_CREAT and O_EXCL are set, and the named file already exists.

EINTR
The open operation was interrupted by a signal. See section Primitives Interrupted by Signals.

EISDIR
The flags argument specified write access, and the file is a directory.

EMFILE
The process has too many files open.

ENFILE
The entire system, or perhaps the file system which contains the directory, cannot support any additional open files at the moment. (This problem cannot happen on the GNU system.)

ENOENT
The named file does not exist, but O_CREAT is not specified.

ENOSPC
The directory or file system that would contain the new file cannot be extended, because there is no disk space left.

ENXIO
O_NONBLOCK and O_WRONLY are both set in the flags argument, the file named by filename is a FIFO (see section Pipes and FIFOs), and no process has the file open for reading.

EROFS
The file resides on a read-only file system and any of O_WRONLY, O_RDWR, O_CREAT, and O_TRUNC are set in the flags argument.

The open function is the underlying primitive for the fopen and freopen functions, that create streams.

Obsolete function: int creat (const char *filename, mode_t mode)

This function is obsolete. The call

creat (filename, mode)

is equivalent to

open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode)

Function: int close (int filedes)

The function close closes the file descriptor filedes. Closing a file has the following consequences:

The normal return value from close is 0; a value of -1 is returned in case of failure. The following errno error conditions are defined for this function:

EBADF
The filedes argument is not a valid file descriptor.

EINTR
The call was interrupted by a signal. See section Primitives Interrupted by Signals. Here's an example of how to handle EINTR properly:

TEMP_FAILURE_RETRY (close (desc));

To close a stream, call fclose (see section Closing Streams) instead of trying to close its underlying file descriptor with close. This flushes any buffered output and updates the stream object to indicate that it is closed.

Input and Output Primitives

This section describes the functions for performing primitive input and output operations on file descriptors: read, write, and lseek. These functions are declared in the header file `unistd.h'.

Data Type: ssize_t

This data type is used to represent the sizes of blocks that can be read or written in a single operation. It is similar to size_t, but must be a signed type.

Function: ssize_t read (int filedes, void *buffer, size_t size)

The read function reads up to size bytes from the file with descriptor filedes, storing the results in the buffer. (This is not necessarily a character string and there is no terminating null character added.)

The return value is the number of bytes actually read. This might be less than size; for example, if there aren't that many bytes left in the file or if there aren't that many bytes immediately available. The exact behavior depends on what kind of file it is. Note that reading less than size bytes is not an error.

A value of zero indicates end-of-file (except if the value of the size argument is also zero). This is not considered an error. If you keep calling read while at end-of-file, it will keep returning zero and doing nothing else.

If read returns at least one character, there is no way you can tell whether end-of-file was reached. But if you did reach the end, the next read will return zero.

In case of an error, read returns -1. The following errno error conditions are defined for this function:

EAGAIN
Normally, when no input is immediately available, read waits for some input. But if the O_NONBLOCK flag is set for the file (see section File Status Flags), read returns immediately without reading any data, and reports this error.

Compatibility Note: Most versions of BSD Unix use a different error code for this: EWOULDBLOCK. In the GNU library, EWOULDBLOCK is an alias for EAGAIN, so it doesn't matter which name you use.

On some systems, reading a large amount of data from a character special file can also fail with EAGAIN if the kernel cannot find enough physical memory to lock down the user's pages. This is limited to devices that transfer with direct memory access into the user's memory, which means it does not include terminals, since they always use separate buffers inside the kernel.

EBADF
The filedes argument is not a valid file descriptor.

EINTR
read was interrupted by a signal while it was waiting for input. See section Primitives Interrupted by Signals.

EIO
For many devices, and for disk files, this error code indicates a hardware error.

EIO also occurs when a background process tries to read from the controlling terminal, and the normal action of stopping the process by sending it a SIGTTIN signal isn't working. This might happen if signal is being blocked or ignored, or because the process group is orphaned. See section Job Control, for more information about job control, and section Signal Handling, for information about signals.

The read function is the underlying primitive for all of the functions that read from streams, such as fgetc.

Function: ssize_t write (int filedes, const void *buffer, size_t size)

The write function writes up to size bytes from buffer to the file with descriptor filedes. The data in buffer is not necessarily a character string and a null character output like any other character.

The return value is the number of bytes actually written. This is normally the same as size, but might be less (for example, if the physical media being written to fills up).

In the case of an error, write returns -1. The following errno error conditions are defined for this function:

EAGAIN
Normally, write blocks until the write operation is complete. But if the O_NONBLOCK flag is set for the file (see section Control Operations on Files), it returns immediately without writing any data, and reports this error. An example of a situation that might cause the process to block on output is writing to a terminal device that supports flow control, where output has been suspended by receipt of a STOP character.

Compatibility Note: Most versions of BSD Unix use a different error code for this: EWOULDBLOCK. In the GNU library, EWOULDBLOCK is an alias for EAGAIN, so it doesn't matter which name you use.

On some systems, writing a large amount of data from a character special file can also fail with EAGAIN if the kernel cannot find enough physical memory to lock down the user's pages. This is limited to devices that transfer with direct memory access into the user's memory, which means it does not include terminals, since they always use separate buffers inside the kernel.

EBADF
The filedes argument is not a valid file descriptor.

EFBIG
The size of the file is larger than the implementation can support.

EINTR
The write operation was interrupted by a signal while it was blocked waiting for completion. See section Primitives Interrupted by Signals.

EIO
For many devices, and for disk files, this error code indicates a hardware error.

EIO also occurs when a background process tries to write to the controlling terminal, and the normal action of stopping the process by sending it a SIGTTOU signal isn't working. This might happen if the signal is being blocked or ignored. See section Job Control, for more information about job control, and section Signal Handling, for information about signals.

ENOSPC
The device is full.

EPIPE
This error is returned when you try to write to a pipe or FIFO that isn't open for reading by any process. When this happens, a SIGPIPE signal is also sent to the process; see section Signal Handling.

Unless you have arranged to prevent EINTR failures, you should check errno after each failing call to write, and if the error was EINTR, you should simply repeat the call. See section Primitives Interrupted by Signals. The easy way to do this is with the macro TEMP_FAILURE_RETRY, as follows:

nbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count));

The write function is the underlying primitive for all of the functions that write to streams, such as fputc.

Setting the File Position of a Descriptor

Just as you can set the file position of a stream with fseek, you can set the file position of a descriptor with lseek. This specifies the position in the file for the next read or write operation. See section File Positioning, for more information on the file position and what it means.

To read the current file position value from a descriptor, use lseek (desc, 0, SEEK_CUR).

Function: off_t lseek (int filedes, off_t offset, int whence)

The lseek function is used to change the file position of the file with descriptor filedes.

The whence argument specifies how the offset should be interpreted in the same way as for the fseek function, and can be one of the symbolic constants SEEK_SET, SEEK_CUR, or SEEK_END.

SEEK_SET
Specifies that whence is a count of characters from the beginning of the file.

SEEK_CUR
Specifies that whence is a count of characters from the current file position. This count may be positive or negative.

SEEK_END
Specifies that whence is a count of characters from the end of the file. A negative count specifies a position within the current extent of the file; a positive count specifies a position past the current end. If you set the position past the current end, and actually write data, you will extend the file with zeros up to that position.

The return value from lseek is normally the resulting file position, measured in bytes from the beginning of the file. You can use this feature together with SEEK_CUR to read the current file position.

You can set the file position past the current end of the file. This does not by itself make the file longer; lseek never changes the file. But subsequent output at that position will extend the file's size.

If the file position cannot be changed, or the operation is in some way invalid, lseek returns a value of -1. The following errno error conditions are defined for this function:

EBADF
The filedes is not a valid file descriptor.

EINVAL
The whence argument value is not valid, or the resulting file offset is not valid.

ESPIPE
The filedes corresponds to a pipe or FIFO, which cannot be positioned. (There may be other kinds of files that cannot be positioned either, but the behavior is not specified in those cases.)

The lseek function is the underlying primitive for the fseek, ftell and rewind functions, which operate on streams instead of file descriptors.

You can have multiple descriptors for the same file if you open the file more than once, or if you duplicate a descriptor with dup. Descriptors that come from separate calls to open have independent file positions; using lseek on one descriptor has no effect on the other. For example,

{
  int d1, d2;
  char buf[4];
  d1 = open ("foo", O_RDONLY);
  d2 = open ("foo", O_RDONLY);
  lseek (d1, 1024, SEEK_SET);
  read (d2, buf, 4);
}

will read the first four characters of the file `foo'. (The error-checking code necessary for a real program has been omitted here for brevity.)

By contrast, descriptors made by duplication share a common file position with the original descriptor that was duplicated. Anything which alters the file position of one of the duplicates, including reading or writing data, affects all of them alike. Thus, for example,

{
  int d1, d2, d3;
  char buf1[4], buf2[4];
  d1 = open ("foo", O_RDONLY);
  d2 = dup (d1);
  d3 = dup (d2);
  lseek (d3, 1024, SEEK_SET);
  read (d1, buf1, 4);
  read (d2, buf2, 4);
}

will read four characters starting with the 1024'th character of `foo', and then four more characters starting with the 1028'th character.

Data Type: off_t

This is an arithmetic data type used to represent file sizes. In the GNU system, this is equivalent to fpos_t or long int.

These three aliases for the `SEEK_...' constants exist for the sake of compatibility with older BSD systems. They are defined in two different header files: `fcntl.h' and `sys/file.h'.

L_SET
An alias for SEEK_SET.

L_INCR
An alias for SEEK_CUR.

L_XTND
An alias for SEEK_END.

Descriptors and Streams

Given an open file descriptor, you can create a stream for it with the fdopen function. You can get the underlying file descriptor for an existing stream with the fileno function. These functions are declared in the header file `stdio.h'.

Function: FILE * fdopen (int filedes, const char *opentype)

The fdopen function returns a new stream for the file descriptor filedes.

The opentype argument is interpreted in the same way as for the fopen function (see section Opening Streams), except that the `b' option is not permitted; this is because GNU makes no distinction between text and binary files. Also, "w" and "w+" do not cause truncation of the file; these have affect only when opening a file, and in this case the file has already been opened. You must make sure that the opentype argument matches the actual mode of the open file descriptor.

The return value is the new stream. If the stream cannot be created (for example, if the modes for the file indicated by the file descriptor do not permit the access specified by the opentype argument), a null pointer is returned instead.

For an example showing the use of the fdopen function, see section Creating a Pipe.

Function: int fileno (FILE *stream)

This function returns the file descriptor associated with the stream stream. If an error is detected (for example, if the stream is not valid) or if stream does not do I/O to a file, fileno returns -1.

There are also symbolic constants defined in `unistd.h' for the file descriptors belonging to the standard streams stdin, stdout, and stderr; see section Standard Streams.

STDIN_FILENO
This macro has value 0, which is the file descriptor for standard input.

STDOUT_FILENO
This macro has value 1, which is the file descriptor for standard output.

STDERR_FILENO
This macro has value 2, which is the file descriptor for standard error output.

Precautions for Mixing Streams and Descriptors

You can have multiple file descriptors and streams (let's call both streams and descriptors "channels" for short) connected to the same file, but you must take care to avoid confusion between channels. There are two cases to consider: linked channels that share a single file position value, and independent channels that have their own file positions.

It's best to use just one channel in your program for actual data transfer to any given file, except when all the access is for input. For example, if you open a pipe (something you can only do at the file descriptor level), either do all I/O with the descriptor, or construct a stream from the descriptor with fdopen and then do all I/O with the stream.

Linked Channels

Channels that come from a single opening share the same file position; we call them linked channels. Linked channels result when you make a stream from a descriptor using fdopen, when you get a descriptor from a stream with fileno, and when you copy a descriptor with dup or dup2. For files that don't support random access, such as terminals and pipes, all channels are effectively linked. On random-access files, all append-type output streams are effectively linked to each other.

If you have been using a stream for I/O, and you want to do I/O using another channel (either a stream or a descriptor) that is linked to it, you must first clean up the stream that you have been using. See section Cleaning Streams.

Terminating a process, or executing a new program in the process, destroys all the streams in the process. If descriptors linked to these streams persist in other processes, their file positions become undefined as a result. To prevent this, you must clean up the streams before destroying them.

Independent Channels

When you open channels (streams or descriptors) separately on a seekable file, each channel has its own file position. These are called independent channels.

The system handles each channel independently. Most of the time, this is quite predictable and natural (especially for input): each channel can read or write sequentially at its own place in the file. The precautions you should take are these:

If you do output to one channel at the end of the file, this will certainly leave the other independent channels positioned somewhere before the new end. If you want them to output at the end, you must set their file positions to end of file, first. (This is not necessary if you use an append-type descriptor or stream; they always output at the current end of the file.) In order to make the end-of-file position accurate, you must clean the output channel you were using, if it is a stream. (This is necessary even if you plan to use an append-type channel next.)

It's impossible for two channels to have separate file pointers for a file that doesn't support random access. Thus, channels for reading or writing such files are always linked, never independent. Append-type channels are also always linked. For these channels, follow the rules for linked channels; see section Linked Channels.

Cleaning Streams

On the GNU system, you can clean up any stream with fclean:

Function: int fclean (stream)

Clean up the stream stream so that its buffer is empty. If stream is doing output, force it out. If stream is doing input, give the data in the buffer back to the system, arranging to reread it.

On other systems, you can use fflush to clean a stream in most cases.

You can skip the fclean or fflush if you know the stream is already clean. A stream is clean whenever its buffer is empty. For example, an unbuffered stream is always clean. An input stream that is at end-of-file is clean. A line-buffered stream is clean when the last character output was a newline.

There is one case in which cleaning a stream is impossible on most systems. This is when the stream is doing input from a file that is not random-access. Such streams typically read ahead, and when the file is not random access, there is no way to give back the excess data already read. When an input stream reads from a random-access file, fflush does clean the stream, but leaves the file pointer at an unpredictable place; you must set the file pointer before doing any further I/O. On the GNU system, using fclean avoids both of these problems.

Closing an output-only stream also does fflush, so this is a valid way of cleaning an output stream. On the GNU system, closing an input stream does fclean.

You need not clean a stream before using its descriptor for control operations such as setting terminal modes; these operations don't affect the file position and are not affected by it. You can use any descriptor for these operations, and all channels are affected simultaneously. However, text already "output" to a stream but still buffered by the stream will be subject to the new terminal modes when subsequently flushed. To make sure "past" output is covered by the terminal settings that were in effect at the time, flush the output streams for that terminal before setting the modes. See section Terminal Modes.

Waiting for Input or Output

Sometimes a program needs to accept input on multiple input channels whenever input arrives. For example, some workstations may have devices such as a digitizing tablet, function button box, or dial box that are connected via normal asynchronous serial interfaces; good user interface style requires responding immediately to input on any device. Another example is a program that acts as a server to several other processes via pipes or sockets.

You cannot normally use read for this purpose, because this blocks the program until input is available on one particular file descriptor; input on other channels won't wake it up. You could set nonblocking mode and poll each file descriptor in turn, but this is very inefficient.

A better solution is to use the select function. This blocks the program until input or output is ready on a specified set of file descriptors, or until timer expires, whichever comes first. This facility is declared in the header file `sys/types.h'.

The file descriptor sets for the select function are specified as fd_set objects. Here is the description of the data type and some macros for manipulating these objects.

Data Type: fd_set

The fd_set data type represents file descriptor sets for the select function. It is actually a bit array.

Macro: int FD_SETSIZE

The value of this macro is the maximum number of file descriptors that a fd_set object can hold information about. On systems with a fixed maximum number, FD_SETSIZE is at least that number. On some systems, including GNU, there is no absolute limit on the number of descriptors open, but this macro still has a constant value which controls the number of bits in an fd_set.

Macro: void FD_ZERO (fd_set *set)

This macro initializes the file descriptor set set to be the empty set.

Macro: void FD_SET (int filedes, fd_set *set)

This macro adds filedes to the file descriptor set set.

Macro: void FD_CLR (int filedes, fd_set *set)

This macro removes filedes from the file descriptor set set.

Macro: int FD_ISSET (int filedes, fd_set *set)

This macro returns a nonzero value (true) if filedes is a member of the the file descriptor set set, and zero (false) otherwise.

Next, here is the description of the select function itself.

Function: int select (int nfds, fd_set *read_fds, fd_set *write_fds, fd_set *except_fds, struct timeval *timeout)

The select function blocks the calling process until there is activity on any of the specified sets of file descriptors, or until the timeout period has expired.

The file descriptors specified by the read_fds argument are checked to see if they are ready for reading; the write_fds file descriptors are checked to see if they are ready for writing; and the except_fds file descriptors are checked for exceptional conditions. You can pass a null pointer for any of these arguments if you are not interested in checking for that kind of condition.

"Exceptional conditions" does not mean errors--errors are reported immediately when an erroneous system call is executed, and do not constitute a state of the descriptor. Rather, they include conditions such as the presence of an urgent message on a socket. (See section Sockets, for information on urgent messages.)

The select function checks only the first nfds file descriptors. The usual thing is to pass FD_SETSIZE as the value of this argument.

The timeout specifies the maximum time to wait. If you pass a null pointer for this argument, it means to block indefinitely until one of the file descriptors is ready. Otherwise, you should provide the time in struct timeval format; see section High-Resolution Calendar. Specify zero as the time (a struct timeval containing all zeros) if you want to find out which descriptors are ready without waiting if none are ready.

The normal return value from select is the total number of ready file descriptors in all of the sets. Each of the argument sets is overwritten with information about the descriptors that are ready for the corresponding operation. Thus, to see if a particular descriptor desc has input, use FD_ISSET (desc, read_fds) after select returns.

If select returns because the timeout period expires, it returns a value of zero.

Any signal will cause select to return immediately. So if your program uses signals, you can't rely on select to keep waiting for the full time specified. If you want to be sure of waiting for a particular amount of time, you must check for EINTR and repeat the select with a newly calculated timeout based on the current time. See the example below. See also section Primitives Interrupted by Signals.

If an error occurs, select returns -1 and does not modify the argument file descriptor sets. The following errno error conditions are defined for this function:

EBADF
One of the file descriptor sets specified an invalid file descriptor.

EINTR
The operation was interrupted by a signal. See section Primitives Interrupted by Signals.

EINVAL
The timeout argument is invalid; one of the components is negative or too large.

Portability Note: The select function is a BSD Unix feature.

Here is an example showing how you can use select to establish a timeout period for reading from a file descriptor. The input_timeout function blocks the calling process until input is available on the file descriptor, or until the timeout period expires.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

int 
input_timeout (int filedes, unsigned int seconds)
{
  fd_set set;
  struct timeval timeout;

  /* Initialize the file descriptor set.  */
  FD_ZERO (&set);
  FD_SET (filedes, &set);

  /* Initialize the timeout data structure.  */
  timeout.tv_sec = seconds;
  timeout.tv_usec = 0;

  /* select returns 0 if timeout, 1 if input available, -1 if error.  */
  return TEMP_FAILURE_RETRY (select (FD_SETSIZE, &set, NULL, NULL, &timeout));
}

int
main (void)
{
  fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 5));
  return 0;
}

There is another example showing the use of select to multiplex input from multiple sockets in section Byte Stream Connection Server Example.

Control Operations on Files

This section describes how you can perform various other operations on file descriptors, such as inquiring about or setting flags describing the status of the file descriptor, manipulating record locks, and the like. All of these operations are performed by the function fcntl.

The second argument to the fcntl function is a command that specifies which operation to perform. The function and macros that name various flags that are used with it are declared in the header file `fcntl.h'. (Many of these flags are also used by the open function; see section Opening and Closing Files.)

Function: int fcntl (int filedes, int command, ...)

The fcntl function performs the operation specified by command on the file descriptor filedes. Some commands require additional arguments to be supplied. These additional arguments and the return value and error conditions are given in the detailed descriptions of the individual commands.

Briefly, here is a list of what the various commands are.

F_DUPFD
Duplicate the file descriptor (return another file descriptor pointing to the same open file). See section Duplicating Descriptors.

F_GETFD
Get flags associated with the file descriptor. See section File Descriptor Flags.

F_SETFD
Set flags associated with the file descriptor. See section File Descriptor Flags.

F_GETFL
Get flags associated with the open file. See section File Status Flags.

F_SETFL
Set flags associated with the open file. See section File Status Flags.

F_GETLK
Get a file lock. See section File Locks.

F_SETLK
Set or clear a file lock. See section File Locks.

F_SETLKW
Like F_SETLK, but wait for completion. See section File Locks.

F_GETOWN
Get process or process group ID to receive SIGIO signals. See section Interrupt-Driven Input.

F_SETOWN
Set process or process group ID to receive SIGIO signals. See section Interrupt-Driven Input.

Duplicating Descriptors

You can duplicate a file descriptor, or allocate another file descriptor that refers to the same open file as the original. Duplicate descriptors share one file position and one set of file status flags (see section File Status Flags), but each has its own set of file descriptor flags (see section File Descriptor Flags).

The major use of duplicating a file descriptor is to implement redirection of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.

You can perform this operation using the fcntl function with the F_DUPFD command, but there are also convenient functions dup and dup2 for duplicating descriptors.

The fcntl function and flags are declared in `fcntl.h', while prototypes for dup and dup2 are in the header file `unistd.h'.

Function: int dup (int old)

This function copies descriptor old to the first available descriptor number (the first number not currently open). It is equivalent to fcntl (old, F_DUPFD, 0).

Function: int dup2 (int old, int new)

This function copies the descriptor old to descriptor number new.

If old is an invalid descriptor, then dup2 does nothing; it does not close new. Otherwise, the new duplicate of old replaces any previous meaning of descriptor new, as if new were closed first.

If old and new are different numbers, and old is a valid descriptor number, then dup2 is equivalent to:

close (new);
fcntl (old, F_DUPFD, new)

However, dup2 does this atomically; there is no instant in the middle of calling dup2 at which new is closed and not yet a duplicate of old.

Macro: int F_DUPFD

This macro is used as the command argument to fcntl, to copy the file descriptor given as the first argument.

The form of the call in this case is:

fcntl (old, F_DUPFD, next_filedes)

The next_filedes argument is of type int and specifies that the file descriptor returned should be the next available one greater than or equal to this value.

The return value from fcntl with this command is normally the value of the new file descriptor. A return value of -1 indicates an error. The following errno error conditions are defined for this command:

EBADF
The old argument is invalid.

EINVAL
The next_filedes argument is invalid.

EMFILE
There are no more file descriptors available--your program is already using the maximum.

ENFILE is not a possible error code for dup2 because dup2 does not create a new opening of a file; duplicate descriptors do not count toward the limit which ENFILE indicates. EMFILE is possible because it refers to the limit on distinct descriptor numbers in use in one process.

Here is an example showing how to use dup2 to do redirection. Typically, redirection of the standard streams (like stdin) is done by a shell or shell-like program before calling one of the exec functions (see section Executing a File) to execute a new program in a child process. When the new program is executed, it creates and initializes the standard streams to point to the corresponding file descriptors, before its main function is invoked.

So, to redirect standard input to a file, the shell could do something like:

pid = fork ();
if (pid == 0)
  {
    char *filename;
    char *program;
    int file;
    ...
    file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
    dup2 (file, STDIN_FILENO);
    TEMP_FAILURE_RETRY (close (file));
    execv (program, NULL);
  }

There is also a more detailed example showing how to implement redirection in the context of a pipeline of processes in section Launching Jobs.

File Descriptor Flags

File descriptor flags are miscellaneous attributes of a file descriptor. These flags are associated with particular file descriptors, so that if you have created duplicate file descriptors from a single opening of a file, each descriptor has its own set of flags.

Currently there is just one file descriptor flag: FD_CLOEXEC, which causes the descriptor to be closed if you use any of the exec... functions (see section Executing a File).

The symbols in this section are defined in the header file `fcntl.h'.

Macro: int F_GETFD

This macro is used as the command argument to fcntl, to specify that it should return the file descriptor flags associated with the filedes argument.

The normal return value from fcntl with this command is a nonnegative number which can be interpreted as the bitwise OR of the individual flags (except that currently there is only one flag to use).

In case of an error, fcntl returns -1. The following errno error conditions are defined for this command:

EBADF
The filedes argument is invalid.

Macro: int F_SETFD

This macro is used as the command argument to fcntl, to specify that it should set the file descriptor flags associated with the filedes argument. This requires a third int argument to specify the new flags, so the form of the call is:

fcntl (filedes, F_SETFD, new_flags)

The normal return value from fcntl with this command is an unspecified value other than -1, which indicates an error. The flags and error conditions are the same as for the F_GETFD command.

The following macro is defined for use as a file descriptor flag with the fcntl function. The value is an integer constant usable as a bit mask value.

Macro: int FD_CLOEXEC

This flag specifies that the file descriptor should be closed when an exec function is invoked; see section Executing a File. When a file descriptor is allocated (as with open or dup), this bit is initially cleared on the new file descriptor, meaning that descriptor will survive into the new program after exec.

If you want to modify the file descriptor flags, you should get the current flags with F_GETFD and modify the value. Don't assume that the flag listed here is the only ones that are implemented; your program may be run years from now and more flags may exist then. For example, here is a function to set or clear the flag FD_CLOEXEC without altering any other flags:

/* Set the FD_CLOEXEC flag of desc if value is nonzero,
   or clear the flag if value is 0.
   Return 0 on success, or -1 on error with errno set. */ 

int
set_cloexec_flag (int desc, int value)
{
  int oldflags = fcntl (desc, F_GETFD, 0);
  /* If reading the flags failed, return error indication now.
  if (oldflags < 0)
    return oldflags;
  /* Set just the flag we want to set. */
  if (value != 0)
    oldflags |= FD_CLOEXEC;
  else
    oldflags &= ~FD_CLOEXEC;
  /* Store modified flag word in the descriptor. */
  return fcntl (desc, F_SETFD, oldflags);
}

File Status Flags

File status flags are used to specify attributes of the opening of a file. Unlike the file descriptor flags discussed in section File Descriptor Flags, the file status flags are shared by duplicated file descriptors resulting from a single opening of the file.

The file status flags are initialized by the open function from the flags argument of the open function. Some of the flags are meaningful only in open and are not remembered subsequently; many of the rest cannot subsequently be changed, though you can read their values by examining the file status flags.

A few file status flags can be changed at any time using fcntl. These include O_APPEND and O_NONBLOCK.

The symbols in this section are defined in the header file `fcntl.h'.

Macro: int F_GETFL

This macro is used as the command argument to fcntl, to read the file status flags for the open file with descriptor filedes.

The normal return value from fcntl with this command is a nonnegative number which can be interpreted as the bitwise OR of the individual flags. The flags are encoded like the flags argument to open (see section Opening and Closing Files), but only the file access modes and the O_APPEND and O_NONBLOCK flags are meaningful here. Since the file access modes are not single-bit values, you can mask off other bits in the returned flags with O_ACCMODE to compare them.

In case of an error, fcntl returns -1. The following errno error conditions are defined for this command:

EBADF
The filedes argument is invalid.

Macro: int F_SETFL

This macro is used as the command argument to fcntl, to set the file status flags for the open file corresponding to the filedes argument. This command requires a third int argument to specify the new flags, so the call looks like this:

fcntl (filedes, F_SETFL, new_flags)

You can't change the access mode for the file in this way; that is, whether the file descriptor was opened for reading or writing. You can only change the O_APPEND and O_NONBLOCK flags.

The normal return value from fcntl with this command is an unspecified value other than -1, which indicates an error. The error conditions are the same as for the F_GETFL command.

The following macros are defined for use in analyzing and constructing file status flag values:

O_APPEND
The bit that enables append mode for the file. If set, then all write operations write the data at the end of the file, extending it, regardless of the current file position.

O_NONBLOCK
The bit that enables nonblocking mode for the file. If this bit is set, read requests on the file can return immediately with a failure status if there is no input immediately available, instead of blocking. Likewise, write requests can also return immediately with a failure status if the output can't be written immediately.

O_NDELAY
This is a synonym for O_NONBLOCK, provided for compatibility with BSD.

Macro: int O_ACCMODE

This macro stands for a mask that can be bitwise-ANDed with the file status flag value to produce a value representing the file access mode. The mode will be O_RDONLY, O_WRONLY, or O_RDWR.

O_RDONLY
Open the file for read access.

O_WRONLY
Open the file for write access.

O_RDWR
Open the file for both reading and writing.

If you want to modify the file status flags, you should get the current flags with F_GETFL and modify the value. Don't assume that the flags listed here are the only ones that are implemented; your program may be run years from now and more flags may exist then. For example, here is a function to set or clear the flag O_NONBLOCK without altering any other flags:

/* Set the O_NONBLOCK flag of desc if value is nonzero,
   or clear the flag if value is 0.
   Return 0 on success, or -1 on error with errno set. */ 

int
set_nonblock_flag (int desc, int value)
{
  int oldflags = fcntl (desc, F_GETFL, 0);
  /* If reading the flags failed, return error indication now. */
  if (oldflags < 0)
    return oldflags;
  /* Set just the flag we want to set. */
  if (value != 0)
    oldflags |= O_NONBLOCK;
  else
    oldflags &= ~O_NONBLOCK;
  /* Store modified flag word in the descriptor. */
  return fcntl (desc, F_SETFL, oldflags);
}

File Locks

The remaining fcntl commands are used to support record locking, which permits multiple cooperating programs to prevent each other from simultaneously accessing parts of a file in error-prone ways.

An exclusive or write lock gives a process exclusive access for writing to the specified part of the file. While a write lock is in place, no other process can lock that part of the file.

A shared or read lock prohibits any other process from requesting a write lock on the specified part of the file. However, other processes can request read locks.

The read and write functions do not actually check to see whether there are any locks in place. If you want to implement a locking protocol for a file shared by multiple processes, your application must do explicit fcntl calls to request and clear locks at the appropriate points.

Locks are associated with processes. A process can only have one kind of lock set for each byte of a given file. When any file descriptor for that file is closed by the process, all of the locks that process holds on that file are released, even if the locks were made using other descriptors that remain open. Likewise, locks are released when a process exits, and are not inherited by child processes created using fork (see section Creating a Process).

When making a lock, use a struct flock to specify what kind of lock and where. This data type and the associated macros for the fcntl function are declared in the header file `fcntl.h'.

struct Type: flock

This structure is used with the fcntl function to describe a file lock. It has these members:

short int l_type
Specifies the type of the lock; one of F_RDLCK, F_WRLCK, or F_UNLCK.

short int l_whence
This corresponds to the whence argument to fseek or lseek, and specifies what the offset is relative to. Its value can be one of SEEK_SET, SEEK_CUR, or SEEK_END.

off_t l_start
This specifies the offset of the start of the region to which the lock applies, and is given in bytes relative to the point specified by l_whence member.

off_t l_len
This specifies the length of the region to be locked. A value of 0 is treated specially; it means the region extends to the end of the file.

pid_t l_pid
This field is the process ID (see section Process Creation Concepts) of the process holding the lock. It is filled in by calling fcntl with the F_GETLK command, but is ignored when making a lock.

Macro: int F_GETLK

This macro is used as the command argument to fcntl, to specify that it should get information about a lock. This command requires a third argument of type struct flock * to be passed to fcntl, so that the form of the call is:

fcntl (filedes, F_GETLK, lockp)

If there is a lock already in place that would block the lock described by the lockp argument, information about that lock overwrites *lockp. Existing locks are not reported if they are compatible with making a new lock as specified. Thus, you should specify a lock type of F_WRLCK if you want to find out about both read and write locks, or F_RDLCK if you want to find out about write locks only.

There might be more than one lock affecting the region specified by the lockp argument, but fcntl only returns information about one of them. The l_whence member of the lockp structure is set to SEEK_SET and the l_start and l_len fields set to identify the locked region.

If no lock applies, the only change to the lockp structure is to update the l_type to a value of F_UNLCK.

The normal return value from fcntl with this command is an unspecified value other than -1, which is reserved to indicate an error. The following errno error conditions are defined for this command:

EBADF
The filedes argument is invalid.

EINVAL
Either the lockp argument doesn't specify valid lock information, or the file associated with filedes doesn't support locks.

Macro: int F_SETLK

This macro is used as the command argument to fcntl, to specify that it should set or clear a lock. This command requires a third argument of type struct flock * to be passed to fcntl, so that the form of the call is:

fcntl (filedes, F_SETLK, lockp)

If the process already has a lock on any part of the region, the old lock on that part is replaced with the new lock. You can remove a lock by specifying the a lock type of F_UNLCK.

If the lock cannot be set, fcntl returns immediately with a value of -1. This function does not block waiting for other processes to release locks. If fcntl succeeds, it return a value other than -1.

The following errno error conditions are defined for this function:

EACCES
EAGAIN
The lock cannot be set because it is blocked by an existing lock on the file. Some systems use EAGAIN in this case, and other systems use EACCES; your program should treat them alike, after F_SETLK.

EBADF
Either: the filedes argument is invalid; you requested a read lock but the filedes is not open for read access; or, you requested a write lock but the filedes is not open for write access.

EINVAL
Either the lockp argument doesn't specify valid lock information, or the file associated with filedes doesn't support locks.

ENOLCK
The system has run out of file lock resources; there are already too many file locks in place.

Well-designed file systems never report this error, because they have no limitation on the number of locks. However, you must still take account of the possibility of this error, as it could result from network access to a file system on another machine.

Macro: int F_SETLKW

This macro is used as the command argument to fcntl, to specify that it should set or clear a lock. It is just like the F_SETLK command, but causes the process to block (or wait) until the request can be specified.

This command requires a third argument of type struct flock *, as for the F_SETLK command.

The fcntl return values and errors are the same as for the F_SETLK command, but these additional errno error conditions are defined for this command:

EINTR
The function was interrupted by a signal while it was waiting. See section Primitives Interrupted by Signals.

EDEADLK
A deadlock condition was detected. This can happen if two processes each already controlling a locked region request a lock on the same region locked by the other process.

The following macros are defined for use as values for the l_type member of the flock structure. The values are integer constants.

F_RDLCK
This macro is used to specify a read (or shared) lock.

F_WRLCK
This macro is used to specify a write (or exclusive) lock.

F_UNLCK
This macro is used to specify that the region is unlocked.

As an example of a situation where file locking is useful, consider a program that can be run simultaneously by several different users, that logs status information to a common file. One example of such a program might be a game that uses a file to keep track of high scores. Another example might be a program that records usage or accounting information for billing purposes.

Having multiple copies of the program simultaneously writing to the file could cause the contents of the file to become mixed up. But you can prevent this kind of problem by setting a write lock on the file before actually writing to the file.

If the program also needs to read the file and wants to make sure that the contents of the file are in a consistent state, then it can also use a read lock. While the read lock is set, no other process can lock that part of the file for writing.

Remember that file locks are only a voluntary protocol for controlling access to a file. There is still potential for access to the file by programs that don't use the lock protocol.

Interrupt-Driven Input

If you set the FASYNC status flag on a file descriptor (see section File Status Flags), a SIGIO signal is sent whenever input or output becomes possible on that file descriptor. The process or process group to receive the signal can be selected by using the F_SETOWN command to the fcntl function. If the file descriptor is a socket, this also selects the recipient of SIGURG signals that are delivered when out-of-band data arrives on that socket; see section Out-of-Band Data.

If the file descriptor corresponds to a terminal device, then SIGIO signals are sent to the foreground process group of the terminal. See section Job Control.

The symbols in this section are defined in the header file `fcntl.h'.

Macro: int F_GETOWN

This macro is used as the command argument to fcntl, to specify that it should get information about the process or process group to which SIGIO signals are sent. (For a terminal, this is actually the foreground process group ID, which you can get using tcgetpgrp; see section Functions for Controlling Terminal Access.)

The return value is interpreted as a process ID; if negative, its absolute value is the process group ID.

The following errno error condition is defined for this command:

EBADF
The filedes argument is invalid.

Macro: int F_SETOWN

This macro is used as the command argument to fcntl, to specify that it should set the process or process group to which SIGIO signals are sent. This command requires a third argument of type pid_t to be passed to fcntl, so that the form of the call is:

fcntl (filedes, F_SETOWN, pid)

The pid argument should be a process ID. You can also pass a negative number whose absolute value is a process group ID.

The return value from fcntl with this command is -1 in case of error and some other value if successful. The following errno error conditions are defined for this command:

EBADF
The filedes argument is invalid.

ESRCH
There is no process or process group corresponding to pid.

Go to the previous, next section.