|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "unix_socket.h"#include "common/memory.h"#include "common/string.h"#include "kernel/errno.h"#include "kernel/memory/heap.h"#include "kernel/process/process.h"#include "kernel/signal/signal.h"#include "kernel/sync/spinlock.h"#include "kernel/sync/wait_queue.h"#include "kernel/vfs/file.h"#include "kernel/vfs/poll.h"#include "kernel/vfs/stat.h"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. | |
| #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).
| #define MAX_BACKLOG 16 |
| #define MAX_NAMES 32 |
| typedef struct rights rights_t |
| enum state_t |
| unix_socket_t * unix_socket_of | ( | file_t * | file | ) |
the socket of an open file, or NULL if the file is not a socket.
make a new, unconnected socket and its open file.
| flags | O_NONBLOCK or 0. |
make two connected sockets (socketpair).
| int32_t unix_socket_bind | ( | unix_socket_t * | socket, |
| const char * | path ) |
give a socket a name.
the name must not be in use.
| 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.
take the next connection of a listening socket (blocks unless the file is non-blocking).
| flags | O_NONBLOCK or 0, for the new socket's file. |
| out | receives the open file of the new connection. |
| 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).
| files | the files (the socket takes a reference to each). |
| dontwait | do not block, as if the file were non-blocking. |
| 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.
| files | receives the passed files (the caller owns the references). |
| file_count | receives their number (0 if none). |