The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
unix_socket.c File Reference

Macros

#define BUFFER_SIZE   16384
 Unix domain stream sockets (see unix_socket.h).
#define MAX_BACKLOG   16
#define MAX_NAMES   32

Typedefs

typedef struct rights rights_t

Enumerations

enum  state_t { STATE_NEW , STATE_LISTENING , STATE_CONNECTED }

Functions

unix_socket_t * unix_socket_of (file_t *file)
 the socket of an open file, or NULL if the file is not a socket.
kerr_t unix_socket_create (uint32_t flags, file_t **out)
 make a new, unconnected socket and its open file.
kerr_t unix_socket_pair (uint32_t flags, file_t **a, file_t **b)
 make two connected sockets (socketpair).
int32_t unix_socket_bind (unix_socket_t *s, const char *path)
 give a socket a name.
int32_t unix_socket_listen (unix_socket_t *s, uint32_t backlog)
 let a bound socket take connections.
int32_t unix_socket_connect (unix_socket_t *s, const char *path)
 connect to the listening socket with this name.
int32_t unix_socket_accept (file_t *listener, uint32_t new_flags, file_t **out)
 take the next connection of a listening socket (blocks unless the file is non-blocking).
int32_t unix_socket_send (file_t *file, const kiovec_t *iov, uint32_t iov_count, file_t **files, uint32_t file_count, bool dontwait)
 send data from user memory, with files to pass (may be 0 of them).
int32_t unix_socket_receive (file_t *file, const kiovec_t *iov, uint32_t iov_count, file_t **files, uint32_t *file_count, bool dontwait)
 receive data into user memory, and the files passed with its first byte.

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   16384

Unix domain stream sockets (see unix_socket.h).

each socket has a receive buffer; its peer writes into it. the files passed with a message wait in a list, each at the stream position of the message's first byte, and a receive hands them out when it reaches that position.

lifetime: a connected socket points to its peer and the peer back. when a socket's last file reference goes (release), it unhooks itself from its peer under the lock and is freed; the peer then reads the end of the stream and its writes fail with EPIPE. a connection a listener has not accepted yet has no file; it goes with the listener.

waiting: everyone who waits for something on a socket (data to read, room in the peer's buffer, a connection to accept) sleeps on that socket's own queue, so no one sleeps on a socket that can be freed.

locking: one spinlock for all sockets. user memory is copied with it held, as for pipes (the syscall layer has checked the ranges).

◆ MAX_BACKLOG

#define MAX_BACKLOG   16

◆ MAX_NAMES

#define MAX_NAMES   32

Typedef Documentation

◆ rights_t

typedef struct rights rights_t

Enumeration Type Documentation

◆ state_t

enum state_t
Enumerator
STATE_NEW 
STATE_LISTENING 
STATE_CONNECTED 

Function Documentation

◆ unix_socket_of()

unix_socket_t * unix_socket_of ( file_t * file)

the socket of an open file, or NULL if the file is not a socket.

◆ unix_socket_create()

kerr_t unix_socket_create ( uint32_t flags,
struct file ** out )

make a new, unconnected socket and its open file.

Parameters
flagsO_NONBLOCK or 0.

◆ unix_socket_pair()

kerr_t unix_socket_pair ( uint32_t flags,
file_t ** a,
file_t ** b )

make two connected sockets (socketpair).

◆ unix_socket_bind()

int32_t unix_socket_bind ( unix_socket_t * socket,
const char * path )

give a socket a name.

the name must not be in use.

Returns
0, or -EADDRINUSE, -EINVAL.

◆ unix_socket_listen()

int32_t unix_socket_listen ( unix_socket_t * socket,
uint32_t backlog )

let a bound socket take connections.

Returns
0, or -EINVAL (not bound, or connected).

◆ unix_socket_connect()

int32_t unix_socket_connect ( unix_socket_t * socket,
const char * path )

connect to the listening socket with this name.

Returns
0, or -ECONNREFUSED, -EISCONN.

◆ unix_socket_accept()

int32_t unix_socket_accept ( struct file * listener,
uint32_t flags,
struct file ** out )

take the next connection of a listening socket (blocks unless the file is non-blocking).

Parameters
flagsO_NONBLOCK or 0, for the new socket's file.
outreceives the open file of the new connection.
Returns
0, or -EINVAL, -EAGAIN, -ERESTARTSYS, -ENOMEM.

◆ unix_socket_send()

int32_t unix_socket_send ( struct file * file,
const kiovec_t * iov,
uint32_t iov_count,
struct file ** files,
uint32_t file_count,
bool dontwait )

send data from user memory, with files to pass (may be 0 of them).

Parameters
filesthe files (the socket takes a reference to each).
dontwaitdo not block, as if the file were non-blocking.
Returns
the bytes sent, or -EPIPE, -ENOTCONN, -EAGAIN, -ERESTARTSYS.

◆ unix_socket_receive()

int32_t unix_socket_receive ( struct file * file,
const kiovec_t * iov,
uint32_t iov_count,
struct file ** files,
uint32_t * file_count,
bool dontwait )

receive data into user memory, and the files passed with its first byte.

a call stops before the first byte of another message that passes files, so each message's files come with their own data.

Parameters
filesreceives the passed files (the caller owns the references).
file_countreceives their number (0 if none).
Returns
the bytes received (0: the peer is gone and all is read), or -ENOTCONN, -EAGAIN, -ERESTARTSYS.