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

Unix domain sockets (AF_UNIX): stream sockets between processes of this machine, named by a path in the file system. More...

#include <core/system.h>
#include <aquila/ioctl.h>
#include <sys/sched.h>
#include <fs/vfs.h>
#include <ds/ringbuf.h>
#include <net/socket.h>
#include <sys/uaccess.h>
#include <unix.h>
#include <aquila/fcntl.h>

Go to the source code of this file.

Macros

#define SOCKBUF   65536

Functions

static int unix_addr_check (const struct sockaddr *addr)
 Checks an AF_UNIX address (the kernel's copy, as big as a struct sockaddr_un): its path must end within it.
static void unix_conn_wakeup (struct un_conn *conn)
 Wakes up every thread sleeping on a connection, so that they notice that its state changed.
int socket_unix_create (struct file *file, int domain, int type, int protocol)
 Makes an AF_UNIX socket, for socket().
static int socket_unix_accept (struct file *file, struct file *conn, const struct sockaddr *addr, socklen_t *len)
 Accepts a connection request on a listening socket, waiting for one unless the socket doesn't block.
static int socket_unix_bind (struct file *file, const struct sockaddr *addr, socklen_t len)
 Binds a socket to a path: a socket node is made there, which must not exist yet (-EADDRINUSE).
static int socket_unix_connect (struct file *file, const struct sockaddr *addr, socklen_t len)
 Connects to the socket bound to a path: the request waits on the listening socket until it is accepted, or refused because the listener closed (-ECONNREFUSED).
static int socket_unix_listen (struct file *file, int backlog)
 Makes a bound socket listen for connections.
static ssize_t ring_to_iobuf (struct ringbuf **ring_p, size_t n, struct iobuf *io)
 Moves up to n bytes from a connection's ring to user memory, a chunk at a time through the kernel's memory: copying to user memory may sleep (a page brought in from the disk), and the peer may free the ring meanwhile (*ring_p is NULL then).
static ssize_t iobuf_to_ring (struct ringbuf **ring_p, size_t n, struct iobuf *io)
 Moves up to n bytes from user memory to a connection's ring, as ring_to_iobuf() does the other way.
static ssize_t socket_unix_send (struct file *file, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen)
 Sends data on a connection: as much as the ring takes, waiting for room unless the socket doesn't block (with MSG_WAITALL, until all is sent).
static ssize_t socket_unix_recv (struct file *file, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen)
 Receives data from a connection: what the ring has, waiting for data unless the socket doesn't block (with MSG_WAITALL, until the buffer is full).
static int socket_unix_can_read (struct file *file, size_t len)
 Says whether a read (or an accept) wouldn't wait: how many bytes or connection requests wait, or 1 at the end of the data.
static struct waitq * socket_unix_wait_queue (struct file *file, int write)
 Returns the wait queue that a socket's readers (or writers) sleep on: a listener's accept, or the queues of a connection's side.
static int socket_unix_can_write (struct file *file, size_t len)
 Says how much a write could send without waiting: the room in the ring.
static void unix_conn_shut (struct file *file, int how)
 Shuts down one side of a connection for reading, writing or both.
static int socket_unix_shut (struct file *file, int how)
 Handles shutdown(), which only applies to a connection.
static int socket_unix_shutdown (struct file *file, int how)
 Frees a socket when its last descriptor is closed.
static int socket_unix_getname (struct file *file, struct sockaddr *addr, socklen_t *len, int peer)
 Gets a socket's name or its peer's: always unnamed, since names aren't kept.
static int socket_unix_ioctl (struct file *file, int request, void *argp)
 Handles FIONREAD: how many bytes a connection has to read.

Variables

static struct sock_ops socket_unix_ops
static struct queue * sockets = QUEUE_NEW()

Detailed Description

Unix domain sockets (AF_UNIX): stream sockets between processes of this machine, named by a path in the file system.

A server binds a socket to a path, which makes a socket node there, and listens. A client connects to that path: its request waits on the listening socket until accept() takes it. The connection has a ring buffer each way, and each side sleeps on its own wait queues when a ring is empty or full. On a connection's open files, offset says the side: 0 for the server, 1 for the client. Only SOCK_STREAM is supported.

Definition in file unix.c.

Macro Definition Documentation

◆ SOCKBUF

#define SOCKBUF   65536

Definition at line 28 of file unix.c.

Referenced by socket_unix_accept(), and socket_unix_send().

Function Documentation

◆ iobuf_to_ring()

ssize_t iobuf_to_ring ( struct ringbuf ** ring_p,
size_t n,
struct iobuf * io )
static

Moves up to n bytes from user memory to a connection's ring, as ring_to_iobuf() does the other way.

Returns how many bytes moved, or -EFAULT if a fault came first.

Definition at line 358 of file unix.c.

References iobuf::fault, iobuf_peek(), iobuf_skip(), iobuf_to_ring(), and MIN.

Referenced by iobuf_to_ring(), and socket_unix_send().

◆ ring_to_iobuf()

ssize_t ring_to_iobuf ( struct ringbuf ** ring_p,
size_t n,
struct iobuf * io )
static

Moves up to n bytes from a connection's ring to user memory, a chunk at a time through the kernel's memory: copying to user memory may sleep (a page brought in from the disk), and the peer may free the ring meanwhile (*ring_p is NULL then).

Returns how many bytes moved, or -EFAULT if a fault came first.

Definition at line 326 of file unix.c.

References iobuf_out(), MIN, ring_to_iobuf(), and ringbuf_discard().

Referenced by ring_to_iobuf(), and socket_unix_recv().

◆ socket_unix_accept()

int socket_unix_accept ( struct file * file,
struct file * conn,
const struct sockaddr * addr,
socklen_t * len )
static

Accepts a connection request on a listening socket, waiting for one unless the socket doesn't block.

conn becomes the server's side of the connection. The peer's address isn't given: names aren't kept.

Definition at line 95 of file unix.c.

References AF_UNIX_CONN, un_conn::connect, un_conn::connected, socket::domain, FILE_SOCKET, file::flags, kfree(), kmalloc(), memcpy(), file::offset, socket::p, un_conn::sico, un_conn::soci, SOCKBUF, socket_unix_accept(), waitq_sleep(), and waitq_wakeup().

Referenced by socket_unix_accept().

◆ socket_unix_bind()

int socket_unix_bind ( struct file * file,
const struct sockaddr * addr,
socklen_t len )
static

Binds a socket to a path: a socket node is made there, which must not exist yet (-EADDRINUSE).

Definition at line 155 of file unix.c.

References curproc, kfree(), kmalloc(), M_ZERO, NULL, PROC_UIO, socket_unix_bind(), sockets, unix_addr_check(), vfs_close(), vfs_lookup(), vfs_mknod(), and vfs_unlink().

Referenced by socket_unix_bind().

◆ socket_unix_can_read()

int socket_unix_can_read ( struct file * file,
size_t len )
static

Says whether a read (or an accept) wouldn't wait: how many bytes or connection requests wait, or 1 at the end of the data.

Definition at line 535 of file unix.c.

References AF_UNIX_CONN, NULL, file::offset, un_conn::sico, un_conn::sico_end, un_conn::soci, un_conn::soci_end, and socket_unix_can_read().

Referenced by socket_unix_can_read(), and socket_unix_ioctl().

◆ socket_unix_can_write()

int socket_unix_can_write ( struct file * file,
size_t len )
static

Says how much a write could send without waiting: the room in the ring.

Definition at line 594 of file unix.c.

References AF_UNIX_CONN, NULL, file::offset, un_conn::sico, ringbuf::size, un_conn::soci, and socket_unix_can_write().

Referenced by socket_unix_can_write().

◆ socket_unix_connect()

int socket_unix_connect ( struct file * file,
const struct sockaddr * addr,
socklen_t len )
static

Connects to the socket bound to a path: the request waits on the listening socket until it is accepted, or refused because the listener closed (-ECONNREFUSED).

Definition at line 223 of file unix.c.

References AF_UNIX_CONN, un_conn::connect, un_conn::connected, curproc, kfree(), kmalloc(), M_ZERO, NULL, file::offset, PROC_UIO, queue_for, socket_unix_connect(), sockets, unix_addr_check(), vfs_close(), vfs_lookup(), un_socket::vnode, waitq_sleep(), and waitq_wakeup().

Referenced by socket_unix_connect().

◆ socket_unix_create()

int socket_unix_create ( struct file * file,
int domain,
int type,
int protocol )

Makes an AF_UNIX socket, for socket().

It is neither bound nor connected yet.

Parameters
filethe open file that becomes the socket
domainAF_UNIX
typeSOCK_STREAM (the only one supported)
protocol0
Returns
0, -EPROTONOSUPPORT, or -ENOMEM

Definition at line 68 of file unix.c.

References socket::domain, FILE_SOCKET, file::flags, kmalloc(), M_ZERO, NULL, socket::ops, socket::p, socket::protocol, socket_unix_create(), socket_unix_ops, and socket::type.

Referenced by socket_create(), and socket_unix_create().

◆ socket_unix_getname()

int socket_unix_getname ( struct file * file,
struct sockaddr * addr,
socklen_t * len,
int peer )
static

Gets a socket's name or its peer's: always unnamed, since names aren't kept.

Definition at line 705 of file unix.c.

References socket_unix_getname().

Referenced by socket_unix_getname().

◆ socket_unix_ioctl()

int socket_unix_ioctl ( struct file * file,
int request,
void * argp )
static

Handles FIONREAD: how many bytes a connection has to read.

Definition at line 715 of file unix.c.

References AF_UNIX_CONN, socket_unix_can_read(), and socket_unix_ioctl().

Referenced by socket_unix_ioctl().

◆ socket_unix_listen()

int socket_unix_listen ( struct file * file,
int backlog )
static

Makes a bound socket listen for connections.

Definition at line 304 of file unix.c.

References un_socket::backlog, MAX, MIN, and socket_unix_listen().

Referenced by socket_unix_listen().

◆ socket_unix_recv()

ssize_t socket_unix_recv ( struct file * file,
struct iobuf * io,
int flags,
struct sockaddr * from,
socklen_t * fromlen )
static

Receives data from a connection: what the ring has, waiting for data unless the socket doesn't block (with MSG_WAITALL, until the buffer is full).

Returns how much was received, 0 at the end, or a negative errno.

Definition at line 458 of file unix.c.

References AF_UNIX_CONN, un_conn::client_recv, un_conn::client_send, iobuf::fault, file::flags, NULL, file::offset, iobuf::resid, ring_to_iobuf(), un_conn::server_recv, un_conn::server_send, un_conn::sico, un_conn::sico_end, un_conn::soci, un_conn::soci_end, socket_unix_recv(), waitq_sleep(), and waitq_wakeup().

Referenced by socket_unix_recv().

◆ socket_unix_send()

ssize_t socket_unix_send ( struct file * file,
struct iobuf * io,
int flags,
const struct sockaddr * to,
socklen_t tolen )
static

Sends data on a connection: as much as the ring takes, waiting for room unless the socket doesn't block (with MSG_WAITALL, until all is sent).

Returns how much was sent, or a negative errno (-EPIPE once writing is shut down).

Definition at line 387 of file unix.c.

References AF_UNIX_CONN, un_conn::client_recv, un_conn::client_send, iobuf::fault, file::flags, iobuf_to_ring(), MIN, NULL, file::offset, iobuf::resid, un_conn::server_recv, un_conn::server_send, un_conn::sico, un_conn::sico_end, un_conn::soci, un_conn::soci_end, SOCKBUF, socket_unix_send(), waitq_sleep(), and waitq_wakeup().

Referenced by socket_unix_send().

◆ socket_unix_shut()

int socket_unix_shut ( struct file * file,
int how )
static

Handles shutdown(), which only applies to a connection.

Definition at line 643 of file unix.c.

References AF_UNIX_CONN, socket_unix_shut(), and unix_conn_shut().

Referenced by socket_unix_shut().

◆ socket_unix_shutdown()

int socket_unix_shutdown ( struct file * file,
int how )
static

Frees a socket when its last descriptor is closed.

A connection's side is shut down, and the connection is freed once both sides are gone. A listener refuses the requests that wait, and is unbound.

Definition at line 658 of file unix.c.

References AF_UNIX_CONN, un_conn::connect, un_conn::connected, kfree(), NULL, un_conn::sico, un_conn::soci, socket_unix_shutdown(), sockets, unix_conn_shut(), vfs_close(), and waitq_wakeup().

Referenced by socket_unix_shutdown().

◆ socket_unix_wait_queue()

struct waitq * socket_unix_wait_queue ( struct file * file,
int write )
static

Returns the wait queue that a socket's readers (or writers) sleep on: a listener's accept, or the queues of a connection's side.

Definition at line 574 of file unix.c.

References AF_UNIX_CONN, un_conn::client_recv, un_conn::client_send, NULL, file::offset, un_conn::server_recv, un_conn::server_send, and socket_unix_wait_queue().

Referenced by socket_unix_wait_queue().

◆ unix_addr_check()

int unix_addr_check ( const struct sockaddr * addr)
static

Checks an AF_UNIX address (the kernel's copy, as big as a struct sockaddr_un): its path must end within it.

Returns 0 or -EINVAL.

Definition at line 41 of file unix.c.

References strnlen(), and unix_addr_check().

Referenced by socket_unix_bind(), socket_unix_connect(), and unix_addr_check().

◆ unix_conn_shut()

void unix_conn_shut ( struct file * file,
int how )
static

Shuts down one side of a connection for reading, writing or both.

Shut for reading, the ring it reads is freed, so the peer can't write anymore. Shut for writing, the ring it writes ends: the peer reads what is left, then the end.

Definition at line 622 of file unix.c.

References NULL, file::offset, un_conn::sico, un_conn::sico_end, un_conn::soci, un_conn::soci_end, unix_conn_shut(), and unix_conn_wakeup().

Referenced by socket_unix_shut(), socket_unix_shutdown(), and unix_conn_shut().

◆ unix_conn_wakeup()

void unix_conn_wakeup ( struct un_conn * conn)
static

Wakes up every thread sleeping on a connection, so that they notice that its state changed.

Definition at line 50 of file unix.c.

References un_conn::client_recv, un_conn::client_send, un_conn::server_recv, un_conn::server_send, unix_conn_wakeup(), and waitq_wakeup().

Referenced by unix_conn_shut(), and unix_conn_wakeup().

Variable Documentation

◆ socket_unix_ops

struct sock_ops socket_unix_ops
static
Initial value:
= {
.accept = socket_unix_accept,
.connect = socket_unix_connect,
.listen = socket_unix_listen,
.can_read = socket_unix_can_read,
.can_write = socket_unix_can_write,
.wait_queue = socket_unix_wait_queue,
.shutdown = socket_unix_shutdown,
.getname = socket_unix_getname,
}
static int socket_unix_can_read(struct file *file, size_t len)
Says whether a read (or an accept) wouldn't wait: how many bytes or connection requests wait,...
Definition unix.c:535
static int socket_unix_getname(struct file *file, struct sockaddr *addr, socklen_t *len, int peer)
Gets a socket's name or its peer's: always unnamed, since names aren't kept.
Definition unix.c:705
static ssize_t socket_unix_recv(struct file *file, struct iobuf *io, int flags, struct sockaddr *from, socklen_t *fromlen)
Receives data from a connection: what the ring has, waiting for data unless the socket doesn't block ...
Definition unix.c:458
static int socket_unix_bind(struct file *file, const struct sockaddr *addr, socklen_t len)
Binds a socket to a path: a socket node is made there, which must not exist yet (-EADDRINUSE).
Definition unix.c:155
static int socket_unix_connect(struct file *file, const struct sockaddr *addr, socklen_t len)
Connects to the socket bound to a path: the request waits on the listening socket until it is accepte...
Definition unix.c:223
static int socket_unix_can_write(struct file *file, size_t len)
Says how much a write could send without waiting: the room in the ring.
Definition unix.c:594
static int socket_unix_ioctl(struct file *file, int request, void *argp)
Handles FIONREAD: how many bytes a connection has to read.
Definition unix.c:715
static struct waitq * socket_unix_wait_queue(struct file *file, int write)
Returns the wait queue that a socket's readers (or writers) sleep on: a listener's accept,...
Definition unix.c:574
static int socket_unix_shutdown(struct file *file, int how)
Frees a socket when its last descriptor is closed.
Definition unix.c:658
static int socket_unix_accept(struct file *file, struct file *conn, const struct sockaddr *addr, socklen_t *len)
Accepts a connection request on a listening socket, waiting for one unless the socket doesn't block.
Definition unix.c:95
static int socket_unix_listen(struct file *file, int backlog)
Makes a bound socket listen for connections.
Definition unix.c:304
static ssize_t socket_unix_send(struct file *file, struct iobuf *io, int flags, const struct sockaddr *to, socklen_t tolen)
Sends data on a connection: as much as the ring takes, waiting for room unless the socket doesn't blo...
Definition unix.c:387
static int socket_unix_shut(struct file *file, int how)
Handles shutdown(), which only applies to a connection.
Definition unix.c:643

Definition at line 34 of file unix.c.

Referenced by socket_unix_create().

◆ sockets

struct queue* sockets = QUEUE_NEW()
static

Definition at line 36 of file unix.c.

Referenced by socket_unix_bind(), socket_unix_connect(), and socket_unix_shutdown().