Skip to main content

POSIX

POSIX emulation APIs to provide compatibility with the standard POSIX APIs. There is no data types defined because file descriptors are also compatible with POSIX APIs.

Example

Here is an example of using POSIX APIs with TCP offload APIs.

example_toe_posix_client.cc
#include <libmango.h>

int main() {
// open an IPC server
mango_ipc_h ipc;
mango_ipc_open ("localhost:32847", false, &ipc);

// create TCP connected with IPC server
mango_tcp_h tcp;
mango_tcp_create (ipc, &tcp);
mango_tcp_set_ipc (tcp, ipc);

// create TCP socket;
mango_socket_h sock;
mango_socket_create (tcp, &sock);
mango_socket_open (sock, NULL);

// connect to the remote server
struct sockaddr_in addr = { ... };
mango_socket_connect (sock, (struct sockaddr *) &addr, sizeof (addr));

// get POSIX file descriptor from TOE socket
int fd = mango_socket_get_fd (sock);

// send data to the remote server via POSIX APIs
char buf[128];
while (1) {
mango_posix_write (fd, buf, 128, NULL);
...
}

return 0;
}

Functions

mango_posix_read

mango_status_e mango_posix_read(int fd, void *buf, size_t len, size_t *read)

Read data from the mango-wrapped fd.

Parameters

  • in fd The file descriptor
  • in buf The buffer to read
  • in len The buffer size
  • out read The number of bytes read

Returns 0 on success, Otherwise, a negative error value.

mango_posix_readv

mango_status_e mango_posix_readv(int fd,
const struct iovec *iov,
int iovcnt,
size_t *read)

Read data from the mango-wrapped fd using I/O vector.

Parameters

  • in fd The file descriptor
  • in iov The pointer of I/O vector
  • in iovcnt The number of I/O vector entries
  • out read The number of bytes read

Returns 0 on success, Otherwise, a negative error value.

mango_posix_write

mango_status_e mango_posix_write(int fd,
const void *buf,
size_t len,
size_t *written)

Write data to the mango-wrapped fd.

Parameters

  • in fd The file descriptor
  • in buf The buffer to be written
  • in len The buffer size
  • out written The number of bytes written

Returns 0 on success, Otherwise, a negative error value.

mango_posix_writev

mango_status_e mango_posix_writev(int fd,
const struct iovec *iov,
int iovcnt,
size_t *written)

Write data to the mango-wrapped fd using I/O vector.

Parameters

  • in fd The file descriptor
  • in iov The pointer of I/O vector
  • in iovcnt The number of I/O vector entries
  • out written The number of bytes written

Returns 0 on success, Otherwise, a negative error value.

mango_posix_epoll_create

mango_status_e mango_posix_epoll_create(int flags, int *epfd)

Create a new epoll instance.

Parameters

  • in flags The flags for epoll_create1
  • out epfd The output epoll file descriptor.

Returns 0 on success, Otherwise, a negative error value.

mango_posix_epoll_ctl

mango_status_e mango_posix_epoll_ctl(int epfd,
int op,
int fd,
struct epoll_event *event)

Register epoll operations.

Parameters

  • in epfd The epoll file descriptor.
  • in op The epoll opcode.
  • in fd The file descriptor to register.
  • in event The epoll event.

Returns 0 on success, Otherwise, a negative error value.

mango_posix_epoll_wait

mango_status_e mango_posix_epoll_wait(int epfd,
struct epoll_event *event,
int max_events,
int timeout,
int *num_events)

Wait epoll events.

Parameters

  • in epfd The epoll file descriptor.
  • in event The epoll event
  • in max_events The number of maximum events
  • in timeout The epoll timeout
  • out num_events The number of detected events

Returns 0 on success, Otherwise, a negative error value.

mango_posix_poll

mango_status_e mango_posix_poll(struct pollfd *fds,
nfds_t nfds,
int timeout,
int *num_events)

wait for events on file descriptors.

Parameters

  • in fds The array of file descriptors to poll.
  • in nfds The number of file descriptors to poll.
  • in timeout The poll timeout
  • out num_events The number of detected events

Returns 0 on success, Otherwise, a negative error value.

mango_posix_check_fd

mango_status_e mango_posix_check_fd(int fd)

Check whether a fd is managed by Mango POSIX emulation APIs.

Parameters

  • in fd The file descriptor.

Returns 0 on success, Otherwise, a negative error value.

mango_posix_close

mango_status_e mango_posix_close(int fd)

Close a fd managed by Mango POSIX emulation APIs.

Parameters

  • in fd The file descriptor.

Returns 0 on success, Otherwise, a negative error value.