UNIX(7)
โจ UNIX(7) โ Miscellaneous Information Manual ๐
okay bestie, pull up a chair because we're about to talk about UNIX domain sockets, and honestly? they're lowkey the moment. it's unix(7), from the Miscellaneous Information Manual, and it's giving "the same machine, but make it efficient." let's get into it ๐ซถ
๐ธ NAME ๐ธ
unix โ sockets for local interprocess communication
โจ SYNOPSIS โจ
#include <sys/socket.h>
#include <sys/un.h>
unix_socket = socket(AF_UNIX, type, 0);
error = socketpair(AF_UNIX, type, 0, int *sv);
๐ DESCRIPTION ๐
So the AF_UNIX socket family โ also known as AF_LOCAL โ is what you use when you want processes on the same machine to talk to each other efficiently. That's the whole vibe. Same box, no network, just vibes.
Traditionally (yes, this is old-school iconic), UNIX domain sockets can be either unnamed, or bound to a filesystem pathname (and that pathname gets marked as being of type socket). And Linux being Linux, it also supports an abstract namespace that's completely independent of the filesystem. no filesystem involvement. we love a detached queen.
The valid socket types in the UNIX domain are:
SOCK_STREAMโ for a stream-oriented socketSOCK_DGRAMโ for a datagram-oriented socket that preserves message boundaries (and, as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams. yes bestie, always. no cap.)SOCK_SEQPACKETโ for a sequenced-packet socket (since Linux 2.6.4) that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent. so organized. so disciplined.
UNIX domain sockets also support passing file descriptors or process credentials to other processes using ancillary data. yes, you can literally hand another process a file descriptor. iconic behavior fr.
๐ฆ Address format ๐ฆ
A UNIX domain socket address lives in this structure, and the structure is exactly this:
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[108]; /* Pathname */
};
The sun_family field always contains AF_UNIX. On Linux, sun_path is 108 bytes in size โ see also BUGS, below (yes, there are bugs, we'll get there).
Various system calls โ for example, bind(2), connect(2), and sendto(2) โ take a sockaddr_un argument as input. Some other system calls โ for example, getsockname(2), getpeername(2), recvfrom(2), and accept(2) โ return an argument of this type. so it goes both ways. versatile.
There are three types of address distinguished in the sockaddr_un structure:
- pathname โ a UNIX domain socket can be bound to a null-terminated filesystem pathname using
bind(2). When the address of a pathname socket is returned (by one of the system calls noted above), its length is
offsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
and sun_path contains the null-terminated pathname. (On Linux, the above offsetof() expression equates to the same value as sizeof(sa_family_t), but some other implementations include other fields before sun_path, so the offsetof() expression more portably describes the size of the address structure. portability bestie, it matters.) For further details of pathname sockets, see below.
- unnamed โ A stream socket that has not been bound to a pathname using
bind(2)has no name. Likewise, the two sockets created bysocketpair(2)are unnamed. When the address of an unnamed socket is returned, its length issizeof(sa_family_t), andsun_pathshould not be inspected. ๐ don't look at it. it's not for you. - abstract โ an abstract socket address is distinguished (from a pathname socket) by the fact that
sun_path[0]is a null byte ('\0'). The socket's address in this namespace is given by the additional bytes insun_paththat are covered by the specified length of the address structure. (Null bytes in the name have no special significance.) The name has no connection with filesystem pathnames. When the address of an abstract socket is returned, the returnedaddrlenis greater thansizeof(sa_family_t)(i.e., greater than 2), and the name of the socket is contained in the first (addrlen-sizeof(sa_family_t)) bytes ofsun_path.
๐ Pathname sockets ๐
When you're binding a socket to a pathname, there are a few rules you should observe for maximum portability and ease of coding. listen up:
- The pathname in
sun_pathshould be null-terminated. - The length of the pathname, including the terminating null byte, should not exceed the size of
sun_path. - The
addrlenargument that describes the enclosingsockaddr_unstructure should have a value of at least:
offsetof(struct sockaddr_un, sun_path)+strlen(addr.sun_path)+1
or, more simply, addrlen can be specified as sizeof(struct sockaddr_un). the simple route. we stan the simple route.
There is some variation in how implementations handle UNIX domain socket addresses that do not follow the above rules. For example, some (but not all) implementations append a null terminator if none is present in the supplied sun_path. so likeโฆ don't rely on it, bestie.
When coding portable applications, keep in mind that some implementations have sun_path as short as 92 bytes. ๐ญ the audacity honestly.
Various system calls โ accept(2), recvfrom(2), getsockname(2), getpeername(2) โ return socket address structures. When applied to UNIX domain sockets, the value-result addrlen argument supplied to the call should be initialized as above. Upon return, the argument is set to indicate the actual size of the address structure. The caller should check the value returned in this argument: if the output value exceeds the input value, then there is no guarantee that a null terminator is present in sun_path. (See BUGS.)
๐ Pathname socket ownership and permissions ๐
In the Linux implementation, pathname sockets honor the permissions of the directory they are in. Creation of a new socket fails if the process does not have write and search (execute) permission on the directory in which the socket is created.
On Linux, connecting to a stream socket object requires write permission on that socket; sending a datagram to a datagram socket likewise requires write permission on that socket. Now โ POSIX does not make any statement about the effect of the permissions on a socket file, and on some systems (e.g., older BSDs), the socket permissions are ignored. Portable programs should not rely on this feature for security. fr fr. do not do that.
When creating a new socket, the owner and group of the socket file are set according to the usual rules. The socket file has all permissions enabled, other than those that are turned off by the process umask(2).
The owner, group, and permissions of a pathname socket can be changed โ using chown(2) and chmod(2). customize your socket, it's a whole thing.
๐ Abstract sockets ๐
Socket permissions have no meaning for abstract sockets: the process umask(2) has no effect when binding an abstract socket, and changing the ownership and permissions of the object (via fchown(2) and fchmod(2)) has no effect on the accessibility of the socket. nope. none. irrelevant. ๐
Abstract sockets automatically disappear when all open references to the socket are closed. self-cleaning queen.
The abstract socket namespace is a nonportable Linux extension. iykyk.
๐ Socket options ๐
For historical reasons (always "historical reasons", isn't it), these socket options are specified with a SOL_SOCKET type even though they are AF_UNIX specific. They can be set with setsockopt(2) and read with getsockopt(2) by specifying SOL_SOCKET as the socket family.
SO_PASSCREDโ Enabling this socket option causes receipt of the credentials of the sending process in anSCM_CREDENTIALSancillary message in each subsequently received message. The returned credentials are those specified by the sender usingSCM_CREDENTIALS, or a default that includes the sender's PID, real user ID, and real group ID, if the sender did not specifySCM_CREDENTIALSancillary data. When this option is set and the socket is not yet connected, a unique name in the abstract namespace will be generated automatically. The value given as an argument tosetsockopt(2)and returned as the result ofgetsockopt(2)is an integer boolean flag.SO_PASSSECโ Enables receiving of the SELinux security label of the peer socket in an ancillary message of typeSCM_SECURITY(see below). The value given as an argument tosetsockopt(2)and returned as the result ofgetsockopt(2)is an integer boolean flag. TheSO_PASSSECoption is supported for UNIX domain datagram sockets since Linux 2.6.18; support for UNIX domain stream sockets was added in Linux 4.2.SO_PEEK_OFFโ Seesocket(7).SO_PEERCREDโ This read-only socket option returns the credentials of the peer process connected to this socket. The returned credentials are those that were in effect at the time of the call toconnect(2),listen(2), orsocketpair(2). The argument togetsockopt(2)is a pointer to aucredstructure; define the_GNU_SOURCEfeature test macro to obtain the definition of that structure from<sys/socket.h>. The use of this option is possible only for connectedAF_UNIXstream sockets and forAF_UNIXstream and datagram socket pairs created usingsocketpair(2).
๐ง Autobind feature ๐ง
If a bind(2) call specifies addrlen as sizeof(sa_family_t), or the SO_PASSCRED socket option was specified for a socket that was not explicitly bound to an address, then the socket is autobound to an abstract address. The address consists of a null byte followed by 5 bytes in the character set [0-9a-f]. Thus, there is a limit of 2^20 autobind addresses. (From Linux 2.1.15, when the autobind feature was added, 8 bytes were used, and the limit was thus 2^32 autobind addresses. The change to 5 bytes came in Linux 2.3.15.) the lore is real.
๐ Sockets API ๐
The following paragraphs describe domain-specific details and unsupported features of the sockets API for UNIX domain sockets on Linux. โจ the more you know โจ
UNIX domain sockets do not support the transmission of out-of-band data (the MSG_OOB flag for send(2) and recv(2)). nope. not a thing here.
The send(2) MSG_MORE flag is not supported by UNIX domain sockets.
Before Linux 3.4, the use of MSG_TRUNC in the flags argument of recv(2) was not supported by UNIX domain sockets.
The SO_SNDBUF socket option does have an effect for UNIX domain sockets, but the SO_RCVBUF option does not. For datagram sockets, the SO_SNDBUF value imposes an upper limit on the size of outgoing datagrams. This limit is calculated as the doubled (see socket(7)) option value less 32 bytes used for overhead.
๐ Ancillary messages ๐
Ancillary data is sent and received using sendmsg(2) and recvmsg(2). For historical reasons (there it is again), the ancillary message types listed below are specified with a SOL_SOCKET type even though they are AF_UNIX specific. To send them, set the cmsg_level field of the struct cmsghdr to SOL_SOCKET and the cmsg_type field to the type. For more information, see cmsg(3).
SCM_RIGHTSโ Send or receive a set of open file descriptors from another process. The data portion contains an integer array of the file descriptors.
Okay so this whole thing is what people call "passing a file descriptor" to another process ๐
but bestie, technically speaking, what's actually getting passed is a reference to an open file description (peep open(2)), and ngl the receiving process is probably gonna end up with a different file descriptor number anyway. Semantically it's the same energy as duplicating (dup(2)) a file descriptor straight into another process's file descriptor table. Slay ๐ซถ
So if the buffer you're using to catch that ancillary data is too small (or just... not there at all ๐), the ancillary data gets truncated (or fully discarded) and the excess file descriptors get automatically closed over in the receiving process. Not a vibe, but that's the deal.
Same story if the number of file descriptors you receive would push the process past its RLIMIT_NOFILE resource limit (go read getrlimit(2)) โ the extras get auto-closed in the receiver. RIP ๐ชฆ
And there's a kernel constant, SCM_MAX_FD, that caps how many file descriptors can be in the array. Try to send an array bigger than that and sendmsg(2) just fails with EINVAL. SCM_MAX_FD is 253 (or 255 before Linux 2.6.38). Periodt.
๐ธ SCM_CREDENTIALS ๐ธ
Send or receive UNIX credentials โจ โ yes this is for authentication, it's giving security. The credentials ride along as a struct ucred ancillary message. That structure lives in <sys/socket.h> and looks like this:
struct ucred {
pid_t pid; /* Process ID of the sending process */
uid_t uid; /* User ID of the sending process */
gid_t gid; /* Group ID of the sending process */
};
Heads up bestie: since glibc 2.8, you must define the _GNU_SOURCE feature test macro (before including any header files, okay?? before) or you won't get this struct. Don't get caught lacking ๐
The kernel checks whatever credentials the sender claims. A privileged process is allowed to specify values that don't match its own. Here's the tea:
- The sender must specify its own process ID โ unless it has the
CAP_SYS_ADMINcapability, in which case the PID of any existing process may be specified. - Its real user ID, effective user ID, or saved set-user-ID โ unless it has
CAP_SETUID. - Its real group ID, effective group ID, or saved set-group-ID โ unless it has
CAP_SETGID.
To receive a struct ucred message, you gotta enable the SO_PASSCRED option on the socket ๐
๐ฆ SCM_SECURITY ๐ฆ
Receive the SELinux security context (the security label) of the peer socket. The ancillary data you get is a null-terminated string holding the security context. Receiver bestie, you should allocate at least NAME_MAX bytes in the data portion of the ancillary message for this. โจ
And to actually receive the security context, the SO_PASSSEC option must be enabled on the socket (see above ๐).
When you're sending ancillary data with sendmsg(2), only one item of each of the above types may be included in the sent message. One. Not two. Don't be greedy ๐
At least one byte of real data should be sent when sending ancillary data. On Linux, this is required to successfully send ancillary data over a UNIX domain stream socket ๐ . When sending ancillary data over a UNIX domain datagram socket, on Linux it's not necessary to send any accompanying real data. But โ portable applications should also include at least one byte of real data when sending ancillary data over a datagram socket. Play it safe, iykyk.
When receiving from a stream socket, ancillary data forms a kind of barrier for the received data ๐ง. For example, imagine the sender transmits like this:
(1) sendmsg(2) of four bytes, with no ancillary data.
(2) sendmsg(2) of one byte, with ancillary data.
(3) sendmsg(2) of four bytes, with no ancillary data.
Now suppose the receiver does recvmsg(2) calls, each with a buffer size of 20 bytes. The first call receives five bytes of data, along with the ancillary data sent by that second sendmsg(2) call. The next call receives the remaining four bytes. The barrier said what it said ๐ง
If the space you allocated for receiving incoming ancillary data is too small, then the ancillary data gets truncated down to however many headers fit in the supplied buffer (or, for an SCM_RIGHTS file descriptor list, the list of file descriptors may be truncated). And if you provide no buffer for incoming ancillary data at all โ i.e., the msg_control field of the msghdr structure you handed to recvmsg(2) is NULL โ then the incoming ancillary data is just discarded ๐๏ธ. In both of these cases, the MSG_CTRUNC flag will be set in the msg.msg_flags value that recvmsg(2) returns. Good to know fr fr.
๐ Ioctls ๐
These ioctl(2) calls return info in value. The correct syntax is:
int value;
error = ioctl(unix_socket, ioctl_type, &value);
And ioctl_type can be:
SIOCINQโ ForSOCK_STREAMsockets, this call returns the number of unread bytes in the receive buffer. The socket must not be inLISTENstate, otherwise you get an error (EINVAL).SIOCINQis defined in<linux/sockios.h>. Alternatively you can use the synonymousFIONREAD, defined in<sys/ioctl.h>. ForSOCK_DGRAMsockets, the returned value is the same as for Internet domain datagram sockets โ check outudp(7)๐.
๐ญ ERRORS ๐ญ
Okay so when things go wrong, here's the list. Every single one, no skips:
EADDRINUSEโ The specified local address is already in use, or the filesystem socket object already exists. Someone beat you to it ๐EBADFโ This one can happen forsendmsg(2)when you're sending a file descriptor as ancillary data over a UNIX domain socket (seeSCM_RIGHTSabove). It means the file descriptor number you're sending is not valid โ like, it's not an open file descriptor, bestie ๐ฌECONNREFUSEDโ The remote address specified byconnect(2)was not a listening socket. This can also happen if the target pathname is not a socket.ECONNRESETโ Remote socket was unexpectedly closed. Rude ๐ซEFAULTโ User memory address was not valid.EINVALโ Invalid argument passed. A common cause is thatAF_UNIXwasn't specified in thesun_typefield of the passed addresses, or the socket was in an invalid state for the operation you tried to pull.EISCONNโconnect(2)called on an already connected socket, or a target address was specified on a connected socket.ENFILEโ The system-wide limit on the total number of open files has been reached.ENOENTโ The pathname in the remote address you gave toconnect(2)did not exist.ENOMEMโ Out of memory. ๐ENOTCONNโ Socket operation needs a target address, but the socket is not connected.EOPNOTSUPPโ Stream operation called on a non-stream oriented socket, or tried to use the out-of-band data option.EPERMโ The sender passed invalid credentials in thestruct ucred.EPIPEโ Remote socket was closed on a stream socket. If enabled, aSIGPIPEis sent as well. You can dodge that by passing theMSG_NOSIGNALflag tosend(2)orsendmsg(2)๐EPROTONOSUPPORTโ Passed protocol is notAF_UNIX.EPROTOTYPEโ Remote socket does not match the local socket type (SOCK_DGRAMversusSOCK_STREAM).ESOCKTNOSUPPORTโ Unknown socket type.ESRCHโ While sending an ancillary message containing credentials (SCM_CREDENTIALS), the caller specified a PID that does not match any existing process.ETOOMANYREFSโ Anothersendmsg(2)one, for when you're sending a file descriptor as ancillary data over a UNIX domain socket (again, seeSCM_RIGHTSabove ๐). It occurs if the number of "in-flight" file descriptors exceeds theRLIMIT_NOFILEresource limit and the caller doesn't have theCAP_SYS_RESOURCEcapability. An in-flight file descriptor is one that's been sent usingsendmsg(2)but hasn't yet been accepted in the recipient process usingrecvmsg(2).
This error is diagnosed since mainline Linux 4.5 (and in some earlier kernel versions where the fix has been backported). In earlier kernel versions, you could place an unlimited number of file descriptors in flight โ by sending each file descriptor with sendmsg(2) and then closing the file descriptor so it wasn't accounted against the RLIMIT_NOFILE resource limit. Wild, right?? Not anymore ๐
Other errors can be generated by the generic socket layer, or by the filesystem while generating a filesystem socket object. See the appropriate manual pages for more info, bestie.
โญ VERSIONS โญ
SCM_CREDENTIALS and the abstract namespace were introduced with Linux 2.2 and should not be used in portable programs. Some BSD-derived systems also support credential passing, but the implementation details differ. So like... know your audience ๐
๐ NOTES ๐
- Binding to a socket with a filename creates a socket in the filesystem that must be deleted by the caller when it's no longer needed (use
unlink(2)). The usual UNIX close-behind semantics apply โ the socket can be unlinked at any time and will be finally removed from the filesystem when the last reference to it is closed. Clean up after yourself, it's giving main character energy โจ - To pass file descriptors or credentials over a
SOCK_STREAMsocket, you must send or receive at least one byte of nonancillary data in the samesendmsg(2)orrecvmsg(2)call. No cap ๐ - UNIX domain stream sockets do not support the notion of out-of-band data. Sorry babe ๐
๐ BUGS ๐
okay so here's the tea, bestie, and it's a little cursed ๐
When you're binding a socket to an address, Linux is one of the implementations that appends a null terminator if you didn't bother supplying one in sun_path. In most cases this is unproblematic: when the socket address gets retrieved, it will be one byte longer than the one you supplied when the socket was bound. Ngl, fine.
BUT. There is one case where the behavior gets genuinely confusing: if 108 non-null bytes are supplied when a socket is bound, then the addition of that null terminator takes the length of the pathname beyond sizeof(sun_path). Consequently, when you retrieve the socket address (for example, via accept(2)), if the input addrlen argument for the retrieving call is specified as sizeof(struct sockaddr_un), then the returned address structure won't have a null terminator in sun_path. ๐ญ
In addition, some implementations don't require a null terminator when binding a socket (they use the addrlen argument to determine the length of sun_path), and when the socket address is retrieved on those implementations, there is no null terminator in sun_path.
Applications that retrieve socket addresses can (portably) code to handle the possibility that there is no null terminator in sun_path by respecting the fact that the number of valid bytes in the pathname is:
strnlen(addr.sun_path, addrlen - offsetof(sockaddr_un, sun_path))
Alternatively, an application can retrieve the socket address by allocating a buffer of size sizeof(struct sockaddr_un)+1 that is zeroed out before the retrieval. The retrieving call can specify addrlen as sizeof(struct sockaddr_un), and the extra zero byte ensures that there will be a null terminator for the string returned in sun_path:
void *addrp;
addrlen = sizeof(struct sockaddr_un);
addrp = malloc(addrlen + 1);
if (addrp == NULL)
/* Handle error */ ;
memset(addrp, 0, addrlen + 1);
if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == -1)
/* handle error */ ;
printf("sun_path = %s\n", ((struct sockaddr_un *) addrp)->sun_path);
This sort of messiness can be avoided if it is guaranteed that the applications that create pathname sockets follow the rules outlined above under Pathname sockets. ๐ซถ
โจ EXAMPLES โจ
The following code demonstrates the use of sequenced-packet sockets for local interprocess communication. It consists of two programs. The server program waits for a connection from the client program. The client sends each of its command-line arguments in separate messages. The server treats the incoming messages as integers and adds them up. The client sends the command string "END". The server sends back a message containing the sum of the client's integers. The client prints the sum and exits. The server waits for the next client to connect. To stop the server, the client is called with the command-line argument "DOWN".
The following output was recorded while running the server in the background and repeatedly executing the client. Execution of the server program ends when it receives the "DOWN" command.
๐ฌ Example output ๐ฌ
$ ./server &
[1] 25887
$ ./client 3 4;
Result = 7
$ ./client 11 -5;
Result = 6
$ ./client DOWN;
Result = 0
[1]+ Done ./server
$
๐ Program source ๐
/*
* File connection.h
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket"
#define BUFFER_SIZE 12
#endif // include guard
/*
* File server.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "connection.h"
int
main(void)
{
int down_flag = 0;
int ret;
int connection_socket;
int data_socket;
int result;
ssize_t r, w;
struct sockaddr_un name;
char buffer[BUFFER_SIZE];
/* Create local socket. */
connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (connection_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/*
* For portability clear the whole structure, since some
* implementations have additional (nonstandard) fields in
* the structure.
*/
memset(&name, 0, sizeof(name));
/* Bind socket to socket name. */
name.sun_family = AF_UNIX;
strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
ret = bind(connection_socket, (const struct sockaddr *) &name,
sizeof(name));
if (ret == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
/*
* Prepare for accepting connections. The backlog size is set
* to 20. So while one request is being processed other requests
* can be waiting.
*/
ret = listen(connection_socket, 20);
if (ret == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
/* This is the main loop for handling connections. */
for (;;) {
/* Wait for incoming connection. */
data_socket = accept(connection_socket, NULL, NULL);
if (data_socket == -1) {
perror("accept");
exit(EXIT_FAILURE);
}
result = 0;
for (;;) {
/* Wait for next data packet. */
r = read(data_socket, buffer, sizeof(buffer));
if (r == -1) {
perror("read");
exit(EXIT_FAILURE);
}
/* Ensure buffer is 0-terminated. */
buffer[sizeof(buffer) - 1] = 0;
/* Handle commands. */
if (!strncmp(buffer, "DOWN", sizeof(buffer))) {
down_flag = 1;
continue;
}
if (!strncmp(buffer, "END", sizeof(buffer))) {
break;
}
if (down_flag) {
continue;
}
/* Add received summand. */
result += atoi(buffer);
}
/* Send result. */
sprintf(buffer, "%d", result);
w = write(data_socket, buffer, sizeof(buffer));
if (w == -1) {
perror("write");
exit(EXIT_FAILURE);
}
/* Close socket. */
close(data_socket);
/* Quit on DOWN command. */
if (down_flag) {
break;
}
}
close(connection_socket);
/* Unlink the socket. */
unlink(SOCKET_NAME);
exit(EXIT_SUCCESS);
}
/*
* File client.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "connection.h"
int
main(int argc, char *argv[])
{
int ret;
int data_socket;
ssize_t r, w;
struct sockaddr_un addr;
char buffer[BUFFER_SIZE];
/* Create local socket. */
data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (data_socket == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/*
* For portability clear the whole structure, since some
* implementations have additional (nonstandard) fields in
* the structure.
*/
memset(&addr, 0, sizeof(addr));
/* Connect socket to socket address. */
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1);
ret = connect(data_socket, (const struct sockaddr *) &addr,
sizeof(addr));
if (ret == -1) {
fprintf(stderr, "The server is down.\n");
exit(EXIT_FAILURE);
}
/* Send arguments. */
for (int i = 1; i < argc; ++i) {
w = write(data_socket, argv[i], strlen(argv[i]) + 1);
if (w == -1) {
perror("write");
break;
}
}
/* Request result. */
strcpy(buffer, "END");
w = write(data_socket, buffer, strlen(buffer) + 1);
if (w == -1) {
perror("write");
exit(EXIT_FAILURE);
}
/* Receive result. */
r = read(data_socket, buffer, sizeof(buffer));
if (r == -1) {
perror("read");
exit(EXIT_FAILURE);
}
/* Ensure buffer is 0-terminated. */
buffer[sizeof(buffer) - 1] = 0;
printf("Result = %s\n", buffer);
/* Close socket. */
close(data_socket);
exit(EXIT_SUCCESS);
}
For examples of the use of SCM_RIGHTS, see cmsg(3) and seccomp_unotify(2). ๐ฅ
๐ SEE ALSO ๐
recvmsg(2), sendmsg(2), socket(2), socketpair(2), cmsg(3), capabilities(7), credentials(7), socket(7), udp(7)
and that's a wrap on UNIX(7), bestie!! ๐ โจ this one came from the Linux man-pages, version 6.20, dated 2026-02-08 โ go read it like five more times, you're gonna slay at sockets fr fr ๐๐ฆ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-1-para
- Copyright 1999, Andi Kleen <andi@firstfloor.org>
- Copyright 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms