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

Unix domain stream sockets, with file descriptor passing. More...

#include "common/error.h"
#include "common/stdint.h"

Go to the source code of this file.

Data Structures

struct  ksockaddr_un_t
struct  kiovec_t
struct  kmsghdr_t
struct  kcmsghdr_t

Macros

#define AF_UNIX   1
#define SOCK_STREAM   1
#define SOCK_NONBLOCK   0x00000800
#define SOCK_CLOEXEC   0x00080000
#define SOL_SOCKET   1
#define SCM_RIGHTS   1
#define MSG_CTRUNC   0x0008
#define MSG_DONTWAIT   0x0040
#define MSG_NOSIGNAL   0x4000
#define MSG_CMSG_CLOEXEC   0x40000000
#define UNIX_PATH_MAX   108
#define SOCKET_MAX_FDS   16

Typedefs

typedef struct unix_socket unix_socket_t

Functions

kerr_t unix_socket_create (uint32_t flags, struct file **out)
 make a new, unconnected socket and its open file.
kerr_t unix_socket_pair (uint32_t flags, struct file **a, struct file **b)
 make two connected sockets (socketpair).
unix_socket_t * unix_socket_of (struct file *file)
 the socket of an open file, or NULL if the file is not a socket.
int32_t unix_socket_bind (unix_socket_t *socket, const char *path)
 give a socket a name.
int32_t unix_socket_listen (unix_socket_t *socket, uint32_t backlog)
 let a bound socket take connections.
int32_t unix_socket_connect (unix_socket_t *socket, const char *path)
 connect to the listening socket with this name.
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).
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).
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.

Detailed Description

Unix domain stream sockets, with file descriptor passing.

a server binds a socket to a name and listens; a client connects to the name and gets a connected pair. each side writes into the other's receive buffer. sendmsg can pass open files (SCM_RIGHTS): they travel with the first byte of the message, and recvmsg installs them as new fds in the receiver. this is how an app hands its window buffer (a shared memory fd) to the compositor.

the names are kept in a kernel table, not as files in the filesystem: any path works as a name, and nothing shows up in the directory.

the layouts below are the user ABI (<sys/socket.h>, <sys/un.h>, <sys/uio.h> in libc); they are the same as Linux's.

Macro Definition Documentation

◆ AF_UNIX

#define AF_UNIX   1

◆ SOCK_STREAM

#define SOCK_STREAM   1

◆ SOCK_NONBLOCK

#define SOCK_NONBLOCK   0x00000800

◆ SOCK_CLOEXEC

#define SOCK_CLOEXEC   0x00080000

◆ SOL_SOCKET

#define SOL_SOCKET   1

◆ SCM_RIGHTS

#define SCM_RIGHTS   1

◆ MSG_CTRUNC

#define MSG_CTRUNC   0x0008

◆ MSG_DONTWAIT

#define MSG_DONTWAIT   0x0040

◆ MSG_NOSIGNAL

#define MSG_NOSIGNAL   0x4000

◆ MSG_CMSG_CLOEXEC

#define MSG_CMSG_CLOEXEC   0x40000000

◆ UNIX_PATH_MAX

#define UNIX_PATH_MAX   108

◆ SOCKET_MAX_FDS

#define SOCKET_MAX_FDS   16

Typedef Documentation

◆ unix_socket_t

typedef struct unix_socket unix_socket_t

Function Documentation

◆ 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,
struct file ** a,
struct file ** b )

make two connected sockets (socketpair).

◆ unix_socket_of()

unix_socket_t * unix_socket_of ( struct file * file)

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

◆ 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.