2 * Copyright (C) 2009 Citrix Ltd.
3 * Copyright (C) 2017 Citrix Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; version 2.1 only. with the special
8 * exception on linking described in file LICENSE.
11 int libxl__sendmsg_fds(libxl__gc *gc, int carrier,
12 const void *data, size_t datalen,
13 int nfds, const int fds[], const char *what) {
14 struct msghdr msg = { 0 };
16 size_t spaceneeded = nfds * sizeof(fds[0]);
17 char control[CMSG_SPACE(spaceneeded)];
21 iov.iov_base = (void*)data;
22 iov.iov_len = datalen;
24 /* compose the message */
27 msg.msg_control = control;
28 msg.msg_controllen = sizeof(control);
31 cmsg = CMSG_FIRSTHDR(&msg);
32 cmsg->cmsg_level = SOL_SOCKET;
33 cmsg->cmsg_type = SCM_RIGHTS;
34 cmsg->cmsg_len = CMSG_LEN(spaceneeded);
35 memcpy(CMSG_DATA(cmsg), fds, spaceneeded);
37 msg.msg_controllen = cmsg->cmsg_len;
39 r = sendmsg(carrier, &msg, 0);
41 LOGE(ERROR, "failed to send fd-carrying message (%s)", what);