unbound 0.1
tube.c File Reference

This file contains pipe service functions. More...

#include "config.h"
#include "util/tube.h"
#include "util/log.h"
#include "util/net_help.h"
#include "util/netevent.h"
#include "util/fptr_wlist.h"
#include "util/ub_event.h"

Macros

#define socketpair(f, t, p, sv)
 no socketpair() available, like on Minix 3.1.7, use pipe

Functions

struct tube * tube_create (void)
 Create a pipe.
void tube_delete (struct tube *tube)
 Delete and destroy a pipe.
void tube_close_read (struct tube *tube)
 Close read part of the pipe.
void tube_close_write (struct tube *tube)
 Close write part of the pipe.
void tube_remove_bg_listen (struct tube *tube)
 Remove bg listen setup from event base.
void tube_remove_bg_write (struct tube *tube)
 Remove bg write setup from event base.
static void fd_drain (int fd, uint32_t len)
 Drain the pipe of bytes.
int tube_handle_listen (struct comm_point *c, void *arg, int error, struct comm_reply *ATTR_UNUSED(reply_info))
int tube_handle_write (struct comm_point *c, void *arg, int error, struct comm_reply *ATTR_UNUSED(reply_info))
int tube_write_msg (struct tube *tube, uint8_t *buf, uint32_t len, int nonblock)
 Write length bytes followed by message.
int tube_read_msg (struct tube *tube, uint8_t **buf, uint32_t *len, int nonblock)
 Read length bytes followed by message.
static int pollit (int fd, struct timeval *t)
 perform poll() on the fd
int tube_poll (struct tube *tube)
 See if data is ready for reading on the tube without blocking.
int tube_wait (struct tube *tube)
 Wait for data to be ready for reading on the tube.
int tube_wait_timeout (struct tube *tube, int msec)
 Wait for data to be ready with a timeout.
int tube_read_fd (struct tube *tube)
 Get FD that is readable when new information arrives.
int tube_setup_bg_listen (struct tube *tube, struct comm_base *base, tube_callback_type *cb, void *arg)
 Start listening for information over the pipe.
int tube_setup_bg_write (struct tube *tube, struct comm_base *base)
 Start background write handler for the pipe.
int tube_queue_item (struct tube *tube, uint8_t *msg, size_t len)
 Append data item to background list of writes.
void tube_handle_signal (int ATTR_UNUSED(fd), short ATTR_UNUSED(events), void *ATTR_UNUSED(arg))

Detailed Description

This file contains pipe service functions.

Macro Definition Documentation

◆ socketpair

#define socketpair ( f,
t,
p,
sv )
Value:
pipe(sv)

no socketpair() available, like on Minix 3.1.7, use pipe

Referenced by create_socketpair(), and tube_create().

Function Documentation

◆ tube_create()

struct tube * tube_create ( void )

Create a pipe.

Returns
: new tube struct or NULL on error.

References fd_set_nonblock(), log_err(), socketpair, tube::sr, tube::sw, tube_create(), and tube_delete().

Referenced by tube_create(), ub_ctx_create(), and worker_create().

◆ tube_delete()

void tube_delete ( struct tube * tube)

◆ tube_close_read()

void tube_close_read ( struct tube * tube)

Close read part of the pipe.

The tube can no longer be read from.

Parameters
tubetube to operate on.

References tube::sr, and tube_close_read().

Referenced by daemon_start_others(), libworker_bg(), libworker_dobg(), tube_close_read(), and tube_delete().

◆ tube_close_write()

void tube_close_write ( struct tube * tube)

Close write part of the pipe.

The tube can no longer be written to.

Parameters
tubetube to operate on.

References tube::sw, and tube_close_write().

Referenced by libworker_bg(), libworker_dobg(), thread_start(), tube_close_write(), and tube_delete().

◆ tube_remove_bg_listen()

void tube_remove_bg_listen ( struct tube * tube)

Remove bg listen setup from event base.

Parameters
tubewhat tube to cleanup

References tube::cmd_msg, comm_point_delete(), tube::listen_com, and tube_remove_bg_listen().

Referenced by libworker_dobg(), tube_delete(), and tube_remove_bg_listen().

◆ tube_remove_bg_write()

void tube_remove_bg_write ( struct tube * tube)

Remove bg write setup from event base.

Parameters
tubewhat tube to cleanup

References tube_res_list::buf, comm_point_delete(), tube_res_list::next, tube::res_com, tube::res_last, tube::res_list, and tube_remove_bg_write().

Referenced by libworker_dobg(), tube_delete(), and tube_remove_bg_write().

◆ fd_drain()

void fd_drain ( int fd,
uint32_t len )
static

Drain the pipe of bytes.

References fd_drain(), and tube_res_list::len.

Referenced by fd_drain(), and tube_read_msg().

◆ tube_write_msg()

int tube_write_msg ( struct tube * tube,
uint8_t * buf,
uint32_t len,
int nonblock )

Write length bytes followed by message.

Parameters
tubethe tube to write on. If that tube is a pipe, its write fd is used as the socket to write on. Is nonblocking. Set to blocking by the function, and back to non-blocking at exit of function.
bufthe message.
lenlength of message.
nonblockif set to true, the first write is nonblocking. If the first write fails the function returns -1. If set false, the first write is blocking.
Returns
: all remainder writes are nonblocking. return 0 on error, in that case blocking/nonblocking of socket is unknown. return 1 if all OK.

References tube_res_list::buf, fd_set_block(), fd_set_nonblock(), tube_res_list::len, log_err(), tube::sw, and tube_write_msg().

Referenced by distribute_cmd(), libworker_dobg(), server_stats_reply(), tube_write_msg(), ub_cancel(), ub_resolve_async(), ub_stop_bg(), and worker_send_cmd().

◆ tube_read_msg()

int tube_read_msg ( struct tube * tube,
uint8_t ** buf,
uint32_t * len,
int nonblock )

Read length bytes followed by message.

Parameters
tubeThe tube to read on. If that tube is a pipe, its read fd is used as the socket to read on. Is nonblocking. Set to blocking by the function, and back to non-blocking at exit of function.
bufthe message, malloced.
lenlength of message, returned.
nonblockif set to true, the first read is nonblocking. If the first read fails the function returns -1. If set false, the first read is blocking.
Returns
: all remainder reads are nonblocking. return 0 on error, in that case blocking/nonblocking of socket is unknown. On EOF 0 is returned. return 1 if all OK.

References tube_res_list::buf, fd_drain(), fd_set_block(), fd_set_nonblock(), tube_res_list::len, log_err(), tube::sr, and tube_read_msg().

Referenced by daemon_remote_exec(), server_stats_obtain(), tube_read_msg(), ub_process(), ub_stop_bg(), ub_wait(), and worker_stop_and_wait().

◆ pollit()

int pollit ( int fd,
struct timeval * t )
static

perform poll() on the fd

References pollit().

Referenced by pollit(), tube_poll(), and tube_wait().

◆ tube_poll()

int tube_poll ( struct tube * tube)

See if data is ready for reading on the tube without blocking.

Parameters
tubetube to check for readable items
Returns
true if readable items are present. False if not (or error). true on pipe_closed.

References pollit(), tube::sr, and tube_poll().

Referenced by tube_poll(), and ub_poll().

◆ tube_wait()

int tube_wait ( struct tube * tube)

Wait for data to be ready for reading on the tube.

is blocking. No timeout.

Parameters
tubethe tube to wait on.
Returns
: if there was something to read (false on error). true on pipe_closed.

References pollit(), tube::sr, and tube_wait().

Referenced by tube_wait(), and ub_wait().

◆ tube_wait_timeout()

int tube_wait_timeout ( struct tube * tube,
int msec )

Wait for data to be ready with a timeout.

Parameters
tubethe tube to wait on.
msectimeout in milliseconds.
Returns
1 if there is something to read within timeout, readability. 0 on a timeout. On failures -1, like errors.

References tube::sr, and tube_wait_timeout().

Referenced by server_stats_obtain(), and tube_wait_timeout().

◆ tube_read_fd()

int tube_read_fd ( struct tube * tube)

Get FD that is readable when new information arrives.

Parameters
tube
Returns
file descriptor.

References tube::sr, and tube_read_fd().

Referenced by tube_read_fd(), and ub_fd().

◆ tube_setup_bg_listen()

int tube_setup_bg_listen ( struct tube * tube,
struct comm_base * base,
tube_callback_type * cb,
void * arg )

Start listening for information over the pipe.

Background registration of a read listener, callback when read completed. Do not mix with tube_read_msg style direct reads from the pipe.

Parameters
tubetube to listen on
basewhat base to register event callback.
cbcallback routine.
arguser argument for callback routine.
Returns
true if successful, false on error.

References tube::listen_arg, tube::listen_cb, tube::listen_com, log_err(), tube::sr, and tube_setup_bg_listen().

Referenced by libworker_dobg(), tube_setup_bg_listen(), and worker_init().

◆ tube_setup_bg_write()

int tube_setup_bg_write ( struct tube * tube,
struct comm_base * base )

Start background write handler for the pipe.

Do not mix with tube_write_msg style direct writes to the pipe.

Parameters
tubetube to write on
basewhat base to register event handler on.
Returns
true if successful, false on error.

References log_err(), tube::res_com, tube::sw, and tube_setup_bg_write().

Referenced by libworker_dobg(), and tube_setup_bg_write().

◆ tube_queue_item()

int tube_queue_item ( struct tube * tube,
uint8_t * msg,
size_t len )

Append data item to background list of writes.

Mallocs a list entry behind the scenes. Not locked behind the scenes, call from one thread or lock on outside.

Parameters
tubewhat tube to queue on.
msgmemory message to send. Is free()d after use. Put at the end of the to-send queue.
lenlength of item.
Returns
0 on failure (msg freed).

References tube_res_list::buf, tube_res_list::len, log_err(), tube_res_list::next, tube::res_com, tube::res_last, tube::res_list, and tube_queue_item().

Referenced by add_bg_result(), and tube_queue_item().