AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
pipe.c File Reference

Pipes, and FIFOs: a ring buffer in memory, written at one end and read at the other. More...

#include <core/system.h>
#include <ds/ringbuf.h>
#include <aquila/fcntl.h>
#include <fs/pipe.h>
#include <fs/posix.h>
#include <sys/sched.h>
#include <sys/signal.h>

Go to the source code of this file.

Enumerations

enum  { END_READ , END_WRITE , END_RDWR }

Functions

static int has_readers (struct pipe *pipe)
 Says whether a pipe has an end open for reading.
static int has_writers (struct pipe *pipe)
 Says whether a pipe has an end open for writing.
static void pipefs_pfree (struct pipe *pipe)
 Frees a pipe and its ring buffer.
static ssize_t pipefs_read (struct vnode *node, off_t offset, struct iobuf *io)
 Reads what the pipe's ring buffer has, up to what is asked for.
static ssize_t pipefs_write (struct vnode *node, off_t offset, struct iobuf *io)
 Writes into the pipe's ring buffer, as much as fits.
static int pipefs_close (struct vnode *node)
 Closes an end of a pipe, when its last open file is closed.
static int pipefs_eof (struct file *file)
 Says whether a read is at the end of the file: no writers are left, and the pipe is empty.
static int pipefs_can_read (struct file *file, size_t size)
 Says whether a read of size bytes would go on without waiting.
static int pipefs_can_write (struct file *file, size_t size)
 Says whether a write of size bytes would go on without waiting.
static int pipefs_mkpipe (struct pipe **ref)
 Makes a new pipe, with no ends.
static struct vnode * pipefs_end (struct pipe *pipe, int which)
 Returns a new reference to an end of a pipe (END_*), making the end if it doesn't exist yet, or NULL if there was no memory.
int pipefs_pipe (struct file *read, struct file *write)
 Makes an anonymous pipe, for pipe(): one open file for each end.
int pipefs_fifo_open (struct file *file)
 Opens a FIFO: the open file's vnode becomes an end of the FIFO's pipe, made by its first open.

Variables

struct fs pipefs

Detailed Description

Pipes, and FIFOs: a ring buffer in memory, written at one end and read at the other.

Readers and writers sleep on one wait queue until there is data or room.

Definition in file pipe.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
END_READ 
END_WRITE 
END_RDWR 

Definition at line 26 of file pipe.c.

Function Documentation

◆ has_readers()

int has_readers ( struct pipe * pipe)
static

Says whether a pipe has an end open for reading.

Definition at line 29 of file pipe.c.

References has_readers(), pipe::r_node, and pipe::rw_node.

Referenced by has_readers(), pipefs_can_write(), pipefs_close(), pipefs_fifo_open(), and pipefs_write().

◆ has_writers()

int has_writers ( struct pipe * pipe)
static

Says whether a pipe has an end open for writing.

Definition at line 35 of file pipe.c.

References has_writers(), pipe::rw_node, and pipe::w_node.

Referenced by has_writers(), pipefs_can_read(), pipefs_close(), pipefs_eof(), and pipefs_fifo_open().

◆ pipefs_can_read()

int pipefs_can_read ( struct file * file,
size_t size )
static

Says whether a read of size bytes would go on without waiting.

Definition at line 113 of file pipe.c.

References has_writers(), vnode::p, pipefs_can_read(), and pipe::ring.

Referenced by pipefs_can_read().

◆ pipefs_can_write()

int pipefs_can_write ( struct file * file,
size_t size )
static

Says whether a write of size bytes would go on without waiting.

Definition at line 125 of file pipe.c.

References has_readers(), vnode::p, pipefs_can_write(), pipe::ring, and ringbuf::size.

Referenced by pipefs_can_write().

◆ pipefs_close()

int pipefs_close ( struct vnode * node)
static

Closes an end of a pipe, when its last open file is closed.

The pipe goes with its last end; otherwise, the other end is woken up to see the end of the file, or that writing fails.

Definition at line 73 of file pipe.c.

References pipe::fifo, vnode::fifo, has_readers(), has_writers(), kfree(), NULL, vnode::p, pipefs_close(), pipefs_pfree(), pipe::queue, pipe::r_node, pipe::rw_node, vfs_close(), pipe::w_node, and waitq_wakeup().

Referenced by pipefs_close().

◆ pipefs_end()

struct vnode * pipefs_end ( struct pipe * pipe,
int which )
static

Returns a new reference to an end of a pipe (END_*), making the end if it doesn't exist yet, or NULL if there was no memory.

Definition at line 163 of file pipe.c.

References END_READ, END_WRITE, vnode::fs, kmalloc(), M_ZERO, vnode::mode, NULL, vnode::p, pipefs, pipefs_end(), pipe::queue, pipe::r_node, vnode::read_queue, vnode::ref, pipe::rw_node, pipe::w_node, and vnode::write_queue.

Referenced by pipefs_end(), pipefs_fifo_open(), and pipefs_pipe().

◆ pipefs_eof()

int pipefs_eof ( struct file * file)
static

Says whether a read is at the end of the file: no writers are left, and the pipe is empty.

Definition at line 105 of file pipe.c.

References has_writers(), pipefs_eof(), and pipe::ring.

Referenced by pipefs_eof().

◆ pipefs_fifo_open()

int pipefs_fifo_open ( struct file * file)

Opens a FIFO: the open file's vnode becomes an end of the FIFO's pipe, made by its first open.

Opening for reading waits for a writer, and opening for writing for a reader, unless the file is non-blocking (then a writer without readers fails with ENXIO) or opened read-write (which is both).

Parameters
filethe open file, whose vnode is the FIFO; it's left as it was on failure
Returns
0, -ENXIO, -ENOMEM, or -ERESTARTSYS if a signal came while it waited

Definition at line 239 of file pipe.c.

References END_RDWR, END_READ, END_WRITE, pipe::fifo, vnode::fifo, file::flags, has_readers(), has_writers(), NULL, pipefs_end(), pipefs_fifo_open(), pipefs_mkpipe(), pipefs_pfree(), pipe::queue, pipe::r_opens, vnode::ref, vfs_close(), pipe::w_opens, waitq_sleep(), and waitq_wakeup().

Referenced by pipefs_fifo_open(), and vfs_file_open().

◆ pipefs_mkpipe()

int pipefs_mkpipe ( struct pipe ** ref)
static

Makes a new pipe, with no ends.

Returns 0 or -ENOMEM.

Definition at line 142 of file pipe.c.

References kmalloc(), M_ZERO, PIPE_BUFLEN, pipefs_mkpipe(), pipefs_pfree(), and pipe::ring.

Referenced by pipefs_fifo_open(), pipefs_mkpipe(), and pipefs_pipe().

◆ pipefs_pfree()

void pipefs_pfree ( struct pipe * pipe)
static

Frees a pipe and its ring buffer.

Definition at line 41 of file pipe.c.

References kfree(), pipefs_pfree(), and pipe::ring.

Referenced by pipefs_close(), pipefs_fifo_open(), pipefs_mkpipe(), pipefs_pfree(), and pipefs_pipe().

◆ pipefs_pipe()

int pipefs_pipe ( struct file * read,
struct file * write )

Makes an anonymous pipe, for pipe(): one open file for each end.

Parameters
readthe open file for the reading end, its vnode set here
writethe open file for the writing end, its vnode and flags set here
Returns
0, -EINVAL, or -ENOMEM

Definition at line 199 of file pipe.c.

References END_READ, END_WRITE, file::flags, kfree(), NULL, pipefs_end(), pipefs_mkpipe(), pipefs_pfree(), and pipefs_pipe().

Referenced by pipefs_pipe(), and sys_pipe().

◆ pipefs_read()

ssize_t pipefs_read ( struct vnode * node,
off_t offset,
struct iobuf * io )
static

Reads what the pipe's ring buffer has, up to what is asked for.

Definition at line 51 of file pipe.c.

References vnode::p, pipefs_read(), iobuf::resid, and pipe::ring.

Referenced by pipefs_read().

◆ pipefs_write()

ssize_t pipefs_write ( struct vnode * node,
off_t offset,
struct iobuf * io )
static

Writes into the pipe's ring buffer, as much as fits.

Fails with -EPIPE if no end is open for reading.

Definition at line 59 of file pipe.c.

References has_readers(), vnode::p, pipefs_write(), iobuf::resid, and pipe::ring.

Referenced by pipefs_write().

Variable Documentation

◆ pipefs

struct fs pipefs
Initial value:
= {
.name = "pipefs",
.vops = {
.read = pipefs_read,
.write = pipefs_write,
.close = pipefs_close,
},
.fops = {
.read = posix_file_read,
.write = posix_file_write,
.close = posix_file_close,
.can_read = pipefs_can_read,
.can_write = pipefs_can_write,
.eof = pipefs_eof,
},
}
static ssize_t pipefs_write(struct vnode *node, off_t offset, struct iobuf *io)
Writes into the pipe's ring buffer, as much as fits.
Definition pipe.c:59
static int pipefs_can_read(struct file *file, size_t size)
Says whether a read of size bytes would go on without waiting.
Definition pipe.c:113
static ssize_t pipefs_read(struct vnode *node, off_t offset, struct iobuf *io)
Reads what the pipe's ring buffer has, up to what is asked for.
Definition pipe.c:51
static int pipefs_eof(struct file *file)
Says whether a read is at the end of the file: no writers are left, and the pipe is empty.
Definition pipe.c:105
static int pipefs_can_write(struct file *file, size_t size)
Says whether a write of size bytes would go on without waiting.
Definition pipe.c:125
static int pipefs_close(struct vnode *node)
Closes an end of a pipe, when its last open file is closed.
Definition pipe.c:73
int posix_file_close(struct file *file)
Closes an open file by dropping its reference to its vnode.
Definition close.c:15
ssize_t posix_file_read(struct file *file, struct iobuf *io)
Reads from an open file at its offset, and moves the offset past what was read.
Definition read.c:29
ssize_t posix_file_write(struct file *file, struct iobuf *io)
Writes to an open file at its offset, and moves the offset past what was written.
Definition write.c:25

Definition at line 308 of file pipe.c.

Referenced by pipefs_end().