From 8774d24133cf1399c70743416dc5727ce7c4a0f4 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 5 Oct 2017 17:10:00 +0100 Subject: [PATCH] fishdescriptor: More work, before trying to compile it Signed-off-by: Ian Jackson --- fishdescriptor/Makefile | 4 ++++ fishdescriptor/donate.c | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 fishdescriptor/Makefile diff --git a/fishdescriptor/Makefile b/fishdescriptor/Makefile new file mode 100644 index 0000000..17bb3e7 --- /dev/null +++ b/fishdescriptor/Makefile @@ -0,0 +1,4 @@ +fishdescriptor-donate.so: donate.o + $(CC) -shared -soname $(LIBCANON) -o $@ $< $(LIBS) + +donate.o: donate.c 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; } -- 2.30.2