X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=fishdescriptor%2Fdonate.c;h=49e244d9c53b403c522f619ce738fec89e31c28b;hp=60ca2666379bfaed7e52d1c38411349905057638;hb=8774d24133cf1399c70743416dc5727ce7c4a0f4;hpb=95e48f3e3aedce1f276a5f0591f706c2df95ee72 diff --git a/fishdescriptor/donate.c b/fishdescriptor/donate.c index 60ca266..49e244d 100644 --- a/fishdescriptor/donate.c +++ b/fishdescriptor/donate.c @@ -16,7 +16,8 @@ * along with this program. If not, see . */ -/* return conventions: functions here return errno values */ +/* return conventions: functions here return 0 on success, + * or -1 setting errno */ static int fishdescriptor_sendmsg_fds(int carrier, int nfds, const int fds[]) { @@ -46,6 +47,35 @@ static int fishdescriptor_sendmsg_fds(int carrier, msg.msg_controllen = cmsg->cmsg_len; r = sendmsg(carrier, &msg, 0); - if (r < 0) - return 0; + if (r < 0) return -1; + + return 0; +} + +static int fishdescriptor_donate(const char *path, const int *fds) { + int r; + int carrier=-1; + + carrier = socket(AF_UNIX, SOCK_STREAM, 0); + if (carrier < 0) goto out; + + struct sockaddr_un suna; + memset(suna,0,&suna); + suna.sun_family = AF_UNIX; + if (strlen(path) >= sizeof(suna.sun_path)) { outno = E2BIG; goto out; } + strcpy(suna.sun_path, path); + + int r = connect(carrier, &suna, sizeof(suna)); + if (r) goto out; + + int nfds; + for (nfds=0; fds[nfds] > 0; nfds++); + + r = fishdescriptor_sendmsg_fds(carrier, nfds, fds); + if (r) goto out; + + r = 0; + out: + if (carrier >= 0) close(carrier); + return r; }