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 General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* return conventions: functions here return 0 on success,
20 * or -1 setting errno */
22 static int fishdescriptor_sendmsg_fds(int carrier,
23 int nfds, const int fds[]) {
24 struct msghdr msg = { 0 };
26 size_t spaceneeded = nfds * sizeof(fds[0]);
27 char control[CMSG_SPACE(spaceneeded)];
32 iov.iov_len = sizeof(nfds);
34 /* compose the message */
37 msg.msg_control = control;
38 msg.msg_controllen = sizeof(control);
41 cmsg = CMSG_FIRSTHDR(&msg);
42 cmsg->cmsg_level = SOL_SOCKET;
43 cmsg->cmsg_type = SCM_RIGHTS;
44 cmsg->cmsg_len = CMSG_LEN(spaceneeded);
45 memcpy(CMSG_DATA(cmsg), fds, spaceneeded);
47 msg.msg_controllen = cmsg->cmsg_len;
49 r = sendmsg(carrier, &msg, 0);
55 static int fishdescriptor_donate(const char *path, const int *fds) {
59 carrier = socket(AF_UNIX, SOCK_STREAM, 0);
60 if (carrier < 0) goto out;
62 struct sockaddr_un suna;
64 suna.sun_family = AF_UNIX;
65 if (strlen(path) >= sizeof(suna.sun_path)) { outno = E2BIG; goto out; }
66 strcpy(suna.sun_path, path);
68 int r = connect(carrier, &suna, sizeof(suna));
72 for (nfds=0; fds[nfds] > 0; nfds++);
74 r = fishdescriptor_sendmsg_fds(carrier, nfds, fds);
79 if (carrier >= 0) close(carrier);