BIO_S_FD(3) | Library Functions Manual | BIO_S_FD(3)

# NAME

`BIO_s_fd`, `BIO_set_fd`, `BIO_get_fd`, `BIO_new_fd`, `BIO_fd_non_fatal_error`, `BIO_fd_should_retry` — file descriptor BIO

# SYNOPSIS

library “libcrypto”
`#include <openssl/bio.h>`

const BIO_METHOD *
`BIO_s_fd`(void);

long
`BIO_set_fd`(BIO *b, int fd, long close_flag);

long
`BIO_get_fd`(BIO *b, int *c);

BIO *
`BIO_new_fd`(int fd, int close_flag);

int
`BIO_fd_non_fatal_error`(int errnum);

int
`BIO_fd_should_retry`(int retval);

# DESCRIPTION

`BIO_s_fd`() returns the file descriptor BIO method. This is a wrapper around the platform's file descriptor routines such as read(2) and write(2).

BIO_read(3) and BIO_write(3) read or write the underlying descriptor. BIO_puts(3) is supported but BIO_gets(3) is not.

If the close flag is set, close(2) is called on the underlying file descriptor when the BIO is freed.

BIO_reset(3) attempts to set the file pointer to the start of the file using `lseek`(fd, 0, 0).

BIO_seek(3) sets the file pointer to position ofs from start of file using `lseek`(fd, ofs, 0).

BIO_tell(3) returns the current file position by calling `lseek`(fd, 0, 1).

`BIO_set_fd`() sets the file descriptor of BIO b to fd and the close flag to close_flag.

`BIO_get_fd`() places the file descriptor in c if it is not `NULL` and also returns the file descriptor.

`BIO_new_fd`() returns a file descriptor BIO using fd and close_flag.

`BIO_fd_non_fatal_error`() determines whether the error status code errnum represents a recoverable error. `BIO_fd_should_retry`() determines whether a recoverable error occurred by inspecting both errno(2) and retval, which is supposed to usually be the return value of a previously called function like BIO_read(3) or BIO_write(3). These two functions are mostly used internally; in application code, it is usually easier and more robust to use BIO_should_retry(3), which works for any BIO type.

The behaviour of BIO_read(3) and BIO_write(3) depends on the behavior of the platform's read(2) and write(2) calls on the descriptor. If the underlying file descriptor is in a non-blocking mode, then the BIO will behave in the manner described in the BIO_read(3) and BIO_should_retry(3) manual pages.

File descriptor BIOs should not be used for socket I/O. Use socket BIOs instead.

BIO_ctrl(3) cmd arguments correspond to macros as follows:

cmd constant | corresponding macro
`BIO_C_FILE_SEEK` | BIO_seek(3)
`BIO_C_FILE_TELL` | BIO_tell(3)
`BIO_C_GET_FD` | `BIO_get_fd`()
`BIO_C_SET_FD` | `BIO_set_fd`()
`BIO_CTRL_GET_CLOSE` | BIO_get_close(3)
`BIO_CTRL_RESET` | BIO_reset(3)
`BIO_CTRL_SET_CLOSE` | BIO_set_close(3)

# RETURN VALUES

`BIO_s_fd`() returns the file descriptor BIO method.

When called on a file descriptor BIO object, BIO_method_type(3) returns the constant `BIO_TYPE_FD` and BIO_method_name(3) returns a pointer to the static string "file descriptor".

`BIO_set_fd`() always returns 1.

`BIO_get_fd`() returns the file descriptor or -1 if the BIO has not been initialized.

`BIO_new_fd`() returns the newly allocated BIO or `NULL` if an error occurred.

`BIO_fd_non_fatal_error`() returns 1 if errnum is `EAGAIN`, `EALREADY`, `EINPROGRESS`, `EINTR`, or `ENOTCONN` and 0 otherwise, even if errnum is 0.

`BIO_fd_should_retry`() returns 1 if `BIO_fd_non_fatal_error`(errno) is 1 and retval is either 0 or -1, or 0 otherwise.

# EXAMPLES

This is a file descriptor BIO version of "Hello World":

    BIO *out;
    out = BIO_new_fd(fileno(stdout), BIO_NOCLOSE);
    BIO_printf(out, "Hello World\n");
    BIO_free(out);

# SEE ALSO

BIO_new(3), BIO_read(3), BIO_s_socket(3), BIO_seek(3), BIO_should_retry(3)

# HISTORY

`BIO_s_fd`(), `BIO_set_fd`(), and `BIO_get_fd`() first appeared in SSLeay 0.6.0, `BIO_fd_should_retry`() in SSLeay 0.6.5, and `BIO_new_fd`() and `BIO_fd_non_fatal_error`() in SSLeay 0.8.0. All these functions have been available since OpenBSD 2.4.

June 8, 2025 | Debian