I/O Utilities

group I/O Helpers

Helper functions for various I/O operations

Functions

int a_io_fd_set_nonblock(int fd)

Make a file descriptor non-blocking

Parameters:
  • fd – The file descriptor

Returns:

0 on success, -errno on failure

int a_io_fd_set_cloexec(int fd)

Make a file descriptor close-on-exec

Parameters:
  • fd – The file descriptor

Returns:

0 on success, -errno on failure

ssize_t a_io_fd_read_uninterrupted(int fd, void *data, size_t size)

Read from a file descriptor avoiding EINTR

Parameters:
  • fd – The file descriptor

  • data – Buffer to read into

  • size – Size of the buffer pointed to by data

Returns:

Read bytes on success, -errno on failure

ssize_t a_io_fd_read_full(int fd, void *data, size_t size)

Read from a file descriptor avoiding short reads

Read at most size bytes from the file descriptor, or until EOF/error.

See also

a_io_socket_recv_all_timeout for a variant safer for sockets

Warning

This method is inappropriate for sockets as it can stall for really long periods of time if the socket is stuck.

Parameters:
  • fd – The file descriptor

  • data – Buffer to read into

  • size – Size of the buffer pointed to by data

Returns:

Read bytes on success, -errno on failure.

ssize_t a_io_fd_write_uninterrupted(int fd, const void *data, size_t size)

Write to a file descriptor avoiding EINTR

Parameters:
  • fd – The file descriptor

  • data – Buffer to write from

  • size – Amount of bytes to write

Returns:

Written bytes on success, -errno on failure

ssize_t a_io_fd_write_full(int fd, const void *data, size_t size)

Write to a file descriptor avoiding short writess

Write size bytes to the file descriptor, or until error.

See also

a_io_socket_send_all_timeout for a variant safer for sockets

Warning

This method is inappropriate for sockets as it can stall for really long periods of time if the socket is stuck.

Parameters:
  • fd – The file descriptor

  • data – Buffer to write from

  • size – Amount of bytes to write

Returns:

Written bytes on success, -errno on failure.

ssize_t a_io_socket_recv_all_timeout(int socket_fd, void *data, size_t size, int timeout_s)

Recv all bytes from a socket, or until timeout

Attempt to recv size bytes from a socket, or until timeout occurs.

Parameters:
  • socket_fd – The socket file descriptor

  • data – The buffer to recv into

  • size – The size of the buffer passed by data

  • timeout_s – The maximum amount of seconds to allow for the recv

Returns:

Bytes received on success, -errno on failure.

ssize_t a_io_socket_send_all_timeout(int socket_fd, const void *data, size_t size, int timeout_s)

Send all bytes over a socket, or until timeout

Attempt to send size bytes over a socket, or until timeout occurs.

Parameters:
  • socket_fd – The socket file descriptor

  • data – The buffer to send

  • size – Amount of bytes to send

  • timeout_s – The maximum amount of seconds to allow for the send

Returns:

Bytes sent on success, -errno on failure.

int a_io_epoll_add_fd(int epoll_fd, int fd, uint32_t event_mask)

Add a file descriptor to an epoll instance

Parameters:
  • epoll_fd – The epoll instance file descriptor

  • fd – The file descriptor to add

  • event_mask – The EPOLLxxx flags to poll for

Returns:

0 on success, -errno on failure.

int a_io_epoll_mod_fd(int epoll_fd, int fd, uint32_t event_mask)

Modify a file descriptor in an epoll instance

Parameters:
  • epoll_fd – The epoll instance file descriptor

  • fd – The file descriptor to modify

  • event_mask – The new EPOLLxxx flags to poll for

Returns:

0 on success, -errno on failure.

int a_io_epoll_del_fd(int epoll_fd, int fd)

Remove a file descriptor from an epoll instance

Parameters:
  • epoll_fd – The epoll instance file descriptor

  • fd – The file descriptor to remove

Returns:

0 on success, -errno on failure.

int a_io_epoll_wait(int epoll_fd, struct epoll_event *events, int maxevents, int timeout_ms)

Wait for events on an epoll instance, avoiding EINTR

Parameters:
  • epoll_fd – The epoll instance file descriptor

  • events – The list of events to fill

  • maxevents – The amount of events that fit into the buffer passed by events

  • timeout_ms – The maximum time to poll for, in milliseconds

Returns:

Number of events received on success, -errno on failure.