The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
pipe.c File Reference
#include "pipe.h"
#include "common/memory.h"
#include "kernel/errno.h"
#include "kernel/vfs/poll.h"
#include "kernel/process/process.h"
#include "kernel/signal/signal.h"
#include "kernel/memory/heap.h"
#include "kernel/assert.h"

Functions

pipe_buf_t * pipe_alloc (void)
 allocate a pipe with one open read end and one open write end.
void pipe_ref (pipe_buf_t *pipe, pipe_dir_t dir)
 open another end of a pipe (dup2 or fd inheritance).
void pipe_unref (pipe_buf_t *pipe, pipe_dir_t dir)
 close one end of a pipe.
int32_t pipe_read (pipe_buf_t *pipe, char *buf, uint32_t count, bool nonblock)
 read from a pipe, sleeping while it is empty and a write end is open.
int32_t pipe_write (pipe_buf_t *pipe, const char *buf, uint32_t count, bool nonblock)
 write every byte to a pipe, sleeping whenever it is full.
uint32_t pipe_poll (pipe_buf_t *pipe, pipe_dir_t dir)
 the readiness of one end of a pipe: the read end has POLLIN when there is data or no writer is left (POLLHUP then too); the write end has POLLOUT when PIPE_BUF bytes fit, and POLLERR when no reader is left.

Function Documentation

◆ pipe_alloc()

pipe_buf_t * pipe_alloc ( void )

allocate a pipe with one open read end and one open write end.

Returns
the new pipe, or NULL if the heap is out of memory.

◆ pipe_ref()

void pipe_ref ( pipe_buf_t * pipe,
pipe_dir_t dir )

open another end of a pipe (dup2 or fd inheritance).

Parameters
pipethe pipe.
dirwhich end the new reference is.

◆ pipe_unref()

void pipe_unref ( pipe_buf_t * pipe,
pipe_dir_t dir )

close one end of a pipe.

closing the last read end wakes blocked writers (they then see a broken pipe); closing the last write end wakes blocked readers (they then see EOF). the pipe is freed once both ends are fully closed.

Parameters
pipethe pipe.
dirwhich end is being closed.

◆ pipe_read()

int32_t pipe_read ( pipe_buf_t * pipe,
char * buf,
uint32_t count,
bool nonblock )

read from a pipe, sleeping while it is empty and a write end is open.

Parameters
pipethe pipe.
bufdestination buffer.
countmaximum number of bytes to read (> 0).
nonblockreturn -EAGAIN instead of sleeping.
Returns
bytes read; 0 only at EOF (empty and no write end open); or -EAGAIN, or -ERESTARTSYS if a signal came while it waited.

◆ pipe_write()

int32_t pipe_write ( pipe_buf_t * pipe,
const char * buf,
uint32_t count,
bool nonblock )

write every byte to a pipe, sleeping whenever it is full.

a write of at most PIPE_BUF bytes goes in as one piece.

Parameters
pipethe pipe.
bufsource buffer.
countnumber of bytes to write (> 0).
nonblockstop instead of sleeping when the pipe is full.
Returns
bytes written (less than count if every read end closed, or the pipe filled up in nonblocking mode, or a signal came); -EPIPE if no read end is open; -EAGAIN if nonblocking and nothing fitted; -ERESTARTSYS if a signal came before anything was written.

◆ pipe_poll()

uint32_t pipe_poll ( pipe_buf_t * pipe,
pipe_dir_t dir )

the readiness of one end of a pipe: the read end has POLLIN when there is data or no writer is left (POLLHUP then too); the write end has POLLOUT when PIPE_BUF bytes fit, and POLLERR when no reader is left.

Parameters
pipethe pipe.
dirwhich end.
Returns
POLL* events (kernel/vfs/poll.h).