|Main index||Section 9||Options|
Except where otherwise indicated, socket functions may sleep, and are not appropriate for use in an ithread(9) context or while holding non-sleepable kernel locks.
Sockets may be closed and freed using soclose(), which has similar semantics to close(2).
In certain circumstances, it is appropriate to destroy a socket without waiting for it to disconnect, for which soabort() is used. This is only appropriate for incoming connections which are in a partially connected state. It must be called on an unreferenced socket, by the thread which removed the socket from its listen queue, to prevent races. It will call into protocol code, so no socket locks may be held over the call. The caller of soabort() is responsible for setting the VNET context. The normal path to freeing a socket is sofree(), which handles reference counting on the socket. It should be called whenever a reference is released, and also whenever reference flags are cleared in socket or protocol code. Calls to sofree() should not be made from outside the socket layer; outside callers should use soclose() instead.
The soconnect() function is equivalent to the connect(2) system call, and initiates a connection on the socket so to the address nam. The operation will be authorized using the credential on thread td. Unlike the user system call, soconnect() returns immediately; the caller may msleep(9) on so->so_timeo while holding the socket mutex and waiting for the SS_ISCONNECTING flag to clear or so->so_error to become non-zero. If soconnect() fails, the caller must manually clear the SS_ISCONNECTING flag.
A call to sodisconnect() disconnects the socket without closing it.
The soshutdown() function is equivalent to the shutdown(2) system call, and causes part or all of a connection on a socket to be closed down.
Sockets are transitioned from non-listening status to listening with solisten().
The second argument in both sogetopt() and sosetopt() is the sopt pointer to a struct sopt describing the socket option operation. The caller-allocated structure must be zeroed, and then have its fields initialized to specify socket option operation arguments:
|Set to SOPT_SET or SOPT_GET depending on whether this is a get or set operation.|
|Specify the level in the network stack the operation is targeted at; for example, SOL_SOCKET.|
|Specify the name of the socket option to set.|
|Kernel space pointer to the argument value for the socket option.|
|Size of the argument value in bytes.|
Upcalls are removed from their socket by soupcall_clear(). The which argument again specifies whether the sending or receiving upcall is to be cleared, with SO_RCV or SO_SND.
The sosend() function is equivalent to the sendmsg(2) system call, and attempts to send bytes of data via the socket so, optionally blocking if data cannot be immediately sent. Data may be sent directly from kernel or user memory via the uio argument, or as an mbuf chain via top, avoiding a data copy. Only one of the uio or top pointers may be non- NULL. An optional destination address may be specified via a non- NULL addr argument, which may result in an implicit connect if supported by the protocol. The caller may optionally send control data mbufs via a non- NULL control argument. Flags may be passed to sosend() using the flags argument, and use the same flag name space as the sendmsg(2) system call.
Kernel callers running in ithread(9) context, or with a mutex held, will wish to use non-blocking sockets and pass the MSG_DONTWAIT flag in order to prevent these functions from sleeping.
A socket can be queried for readability, writability, out-of-band data, or end-of-file using sopoll(). The possible values for events are as for poll(2), with symbolic values POLLIN, POLLPRI, POLLOUT, POLLRDNORM, POLLWRNORM, POLLRDBAND, and POLLINGEOF taken from <sys/poll.h>.
Calls to soaccept() pass through to the protocol's accept routine to accept an incoming connection.
A copy of an existing struct sockaddr may be made using sodupsockaddr().
Protocol implementations notify the socket layer of the arrival of out-of-band data using sohasoutofband(), so that the socket layer can notify socket consumers of the available data.
An "external-format" version of a struct socket can be created using sotoxsocket(), suitable for isolating user code from changes in the kernel structure.
Protocol implementations can use sonewconn() to create a socket and attach protocol state to that socket. This can be used to create new sockets available for soaccept() on a listen socket. The returned socket has a reference count of zero.
Protocols must supply an implementation for sopoll(); sopoll_generic() is provided for the use by protocol implementations.
The functions sosend_dgram() and sosend_generic() are supplied to assist in protocol implementations of sosend().
When a protocol creates a new socket structure, it is necessary to reserve socket buffer space for that socket, by calling soreserve(). The rough inverse of this reservation is performed by sorflush(), which is called automatically by the socket framework.
When a protocol needs to wake up threads waiting for the socket to become ready to read or write, variants of sowakeup() are used. The sowakeup() function should not be called directly by protocol code, instead use the wrappers sorwakeup(), sorwakeup_locked(), sowwakeup(), and sowwakeup_locked() for readers and writers, with the corresponding socket buffer lock not already locked, or already held, respectively.
The functions sooptcopyin() and sooptcopyout() are useful for transferring struct sockopt data between user and kernel code.
The caller may need to manually clear SS_ISCONNECTING if soconnect() returns an error.
The MSG_DONTWAIT flag is not implemented for sosend(), and may not always work with soreceive() when zero copy sockets are enabled.
This manual page does not describe how to register socket upcalls or monitor a socket for readability/writability without using blocking I/O.
The soref() and sorele() functions are not described, and in most cases should not be used, due to confusing and potentially incorrect interactions when sorele() is last called after soclose().
|SOCKET (9)||May 26, 2014|
|Main index||Section 9||Options|
|“||This philosophy, in the hands of amateurs, leads to inexplicably mind-numbing botches like the existence of two programs, “head” and “tail,” which print the first part or the last part of a file, depending. Even though their operations are duals of one another, “head” and “tail” are different programs, written by different authors, and take different options!||”|
|— The Unix Haters' handbook|