Skip to content
BSD syscall#28

svc · unix #28

sendmsg

Sends a message together with optional ancillary control data using a scatter-gather buffer.

Prototype

int sendmsg(int s, caddr_t msg, int flags);

Returns: int

Arguments

NameTypeDirDescription
sint-
msgcaddr_t-
flagsint-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard28
xnu-1486.2.1128
xnu-1504.15.328
xnu-1504.3.1228
xnu-1504.7.428
xnu-1504.9.1728
xnu-1504.9.2628
xnu-1504.9.3728

Examples

Pass a file descriptor over AF_UNIX

char buf[CMSG_SPACE(sizeof(int))] = {0};
struct iovec iov = { .iov_base = "x", .iov_len = 1 };
struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1,
                      .msg_control = buf, .msg_controllen = sizeof(buf) };
struct cmsghdr *c = CMSG_FIRSTHDR(&msg);
c->cmsg_level = SOL_SOCKET; c->cmsg_type = SCM_RIGHTS;
c->cmsg_len = CMSG_LEN(sizeof(int));
*(int *)CMSG_DATA(c) = fd_to_pass;
sendmsg(sock, &msg, 0);

Notes

Mirror of recvmsg on the send side. It is the only entry point that can attach SCM_RIGHTS (file-descriptor passing) on AF_UNIX, set IP_PKTINFO source addresses, or carry NECP_CLIENT_PARAMETER cmsgs that bind a UDP packet to a specific Network.framework path. msghdr.msg_flags is ignored on input; only the iov, cmsg, and destination matter.

Detection

ES has no sendmsg event. NEFilterDataProvider sees payload bytes; descriptor passing via SCM_RIGHTS can be inferred by comparing PROC_PIDFDSOCKETINFO snapshots across peers.

Related APIs

sendtosendmsg_xrecvmsgsocketpairsetsockopt