The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
unix_socket.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file unix_socket.h
5 * @brief Unix domain stream sockets, with file descriptor passing.
6 *
7 * a server binds a socket to a name and listens; a client connects to
8 * the name and gets a connected pair. each side writes into the other's
9 * receive buffer. sendmsg can pass open files (SCM_RIGHTS): they travel
10 * with the first byte of the message, and recvmsg installs them as new
11 * fds in the receiver. this is how an app hands its window buffer (a
12 * shared memory fd) to the compositor.
13 *
14 * the names are kept in a kernel table, not as files in the filesystem:
15 * any path works as a name, and nothing shows up in the directory.
16 *
17 * the layouts below are the user ABI (<sys/socket.h>, <sys/un.h>,
18 * <sys/uio.h> in libc); they are the same as Linux's.
19 */
20
21#include "common/error.h"
22#include "common/stdint.h"
23
24struct file;
25
26#define AF_UNIX 1
27#define SOCK_STREAM 1
28#define SOCK_NONBLOCK 0x00000800 // with the type: O_NONBLOCK on the new socket
29#define SOCK_CLOEXEC 0x00080000 // with the type: FD_CLOEXEC on the new fd
30
31#define SOL_SOCKET 1
32#define SCM_RIGHTS 1
33
34#define MSG_CTRUNC 0x0008 // recvmsg: not all passed files fitted
35#define MSG_DONTWAIT 0x0040 // this call does not block
36#define MSG_NOSIGNAL 0x4000 // sendmsg: no SIGPIPE when the peer is gone
37#define MSG_CMSG_CLOEXEC 0x40000000 // recvmsg: FD_CLOEXEC on the received fds
38
39#define UNIX_PATH_MAX 108
40#define SOCKET_MAX_FDS 16 // files per message
41
47
48typedef struct
49{
51 size_t len;
52} kiovec_t;
53
64
65typedef struct
66{
67 size_t len;
71
72typedef struct unix_socket unix_socket_t;
73
74/**
75 * make a new, unconnected socket and its open file.
76 * @param flags O_NONBLOCK or 0.
77 */
79
80/**
81 * make two connected sockets (socketpair).
82 */
83kerr_t unix_socket_pair(uint32_t flags, struct file **a, struct file **b);
84
85/**
86 * the socket of an open file, or NULL if the file is not a socket.
87 */
89
90/**
91 * give a socket a name. the name must not be in use.
92 * @return 0, or -EADDRINUSE, -EINVAL.
93 */
95
96/**
97 * let a bound socket take connections.
98 * @return 0, or -EINVAL (not bound, or connected).
99 */
101
102/**
103 * connect to the listening socket with this name.
104 * @return 0, or -ECONNREFUSED, -EISCONN.
105 */
107
108/**
109 * take the next connection of a listening socket (blocks unless the file
110 * is non-blocking).
111 * @param flags O_NONBLOCK or 0, for the new socket's file.
112 * @param out receives the open file of the new connection.
113 * @return 0, or -EINVAL, -EAGAIN, -ERESTARTSYS, -ENOMEM.
114 */
115int32_t unix_socket_accept(struct file *listener, uint32_t flags, struct file **out);
116
117/**
118 * send data from user memory, with files to pass (may be 0 of them).
119 * @param files the files (the socket takes a reference to each).
120 * @param dontwait do not block, as if the file were non-blocking.
121 * @return the bytes sent, or -EPIPE, -ENOTCONN, -EAGAIN, -ERESTARTSYS.
122 */
123int32_t unix_socket_send(struct file *file, const kiovec_t *iov, uint32_t iov_count,
124 struct file **files, uint32_t file_count, bool dontwait);
125
126/**
127 * receive data into user memory, and the files passed with its first
128 * byte. a call stops before the first byte of another message that
129 * passes files, so each message's files come with their own data.
130 * @param files receives the passed files (the caller owns the references).
131 * @param file_count receives their number (0 if none).
132 * @return the bytes received (0: the peer is gone and all is read), or
133 * -ENOTCONN, -EAGAIN, -ERESTARTSYS.
134 */
135int32_t unix_socket_receive(struct file *file, const kiovec_t *iov, uint32_t iov_count,
136 struct file **files, uint32_t *file_count, bool dontwait);
kerr_t
unified kernel error codes.
Definition error.h:14
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
unsigned short uint16_t
Definition stdint.h:13
int int32_t
Definition stdint.h:14
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
int socket(int domain, int type, int protocol)
an open-file description.
Definition file.h:64
Definition unix_socket.h:66
size_t len
Definition unix_socket.h:67
int32_t type
Definition unix_socket.h:69
int32_t level
Definition unix_socket.h:68
Definition unix_socket.h:49
uintptr_t base
Definition unix_socket.h:50
size_t len
Definition unix_socket.h:51
Definition unix_socket.h:55
uint32_t namelen
Definition unix_socket.h:57
int32_t flags
Definition unix_socket.h:62
uintptr_t name
Definition unix_socket.h:56
uintptr_t iov
Definition unix_socket.h:58
size_t iovlen
Definition unix_socket.h:59
size_t controllen
Definition unix_socket.h:61
uintptr_t control
Definition unix_socket.h:60
Definition unix_socket.h:43
char sun_path[UNIX_PATH_MAX]
Definition unix_socket.h:45
uint16_t sun_family
Definition unix_socket.h:44
kerr_t unix_socket_pair(uint32_t flags, file_t **a, file_t **b)
make two connected sockets (socketpair).
Definition unix_socket.c:299
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).
Definition unix_socket.c:502
kerr_t unix_socket_create(uint32_t flags, file_t **out)
make a new, unconnected socket and its open file.
Definition unix_socket.c:281
unix_socket_t * unix_socket_of(file_t *file)
the socket of an open file, or NULL if the file is not a socket.
Definition unix_socket.c:181
int32_t unix_socket_listen(unix_socket_t *s, uint32_t backlog)
let a bound socket take connections.
Definition unix_socket.c:363
int32_t unix_socket_connect(unix_socket_t *s, const char *path)
connect to the listening socket with this name.
Definition unix_socket.c:380
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).
Definition unix_socket.c:431
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.
Definition unix_socket.c:605
int32_t unix_socket_bind(unix_socket_t *s, const char *path)
give a socket a name.
Definition unix_socket.c:319
#define UNIX_PATH_MAX
Definition unix_socket.h:39
struct unix_socket unix_socket_t
Definition unix_socket.h:72