From bbd389fe8b598d5dccdd57f71b90ddc9473149b7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 5 Oct 2017 17:21:11 +0100 Subject: [PATCH] fishdescriptor: donate: some bugfixes Signed-off-by: Ian Jackson --- fishdescriptor/Makefile | 2 +- fishdescriptor/donate.c | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/fishdescriptor/Makefile b/fishdescriptor/Makefile index 17bb3e7..dd0dfbe 100644 --- a/fishdescriptor/Makefile +++ b/fishdescriptor/Makefile @@ -1,4 +1,4 @@ fishdescriptor-donate.so: donate.o - $(CC) -shared -soname $(LIBCANON) -o $@ $< $(LIBS) + $(CC) -shared -Wl,-soname $(LIBCANON) -o $@ $< $(LIBS) donate.o: donate.c diff --git a/fishdescriptor/donate.c b/fishdescriptor/donate.c index 49e244d..6e5dea7 100644 --- a/fishdescriptor/donate.c +++ b/fishdescriptor/donate.c @@ -16,11 +16,16 @@ * along with this program. If not, see . */ -/* return conventions: functions here return 0 on success, - * or -1 setting errno */ +#include +#include + +#include +#include +#include static int fishdescriptor_sendmsg_fds(int carrier, int nfds, const int fds[]) { + /* return convention: like a syscall */ struct msghdr msg = { 0 }; struct cmsghdr *cmsg; size_t spaceneeded = nfds * sizeof(fds[0]); @@ -52,20 +57,23 @@ static int fishdescriptor_sendmsg_fds(int carrier, return 0; } -static int fishdescriptor_donate(const char *path, const int *fds) { +int fishdescriptor_donate(const char *path, const int *fds) { + /* return convention: returns errno value */ + /* within function: r is like syscall return, errno value is in errno */ int r; + int esave = errno; int carrier=-1; carrier = socket(AF_UNIX, SOCK_STREAM, 0); if (carrier < 0) goto out; struct sockaddr_un suna; - memset(suna,0,&suna); + memset(&suna,0,sizeof(suna)); suna.sun_family = AF_UNIX; - if (strlen(path) >= sizeof(suna.sun_path)) { outno = E2BIG; goto out; } + if (strlen(path) >= sizeof(suna.sun_path)) { errno = E2BIG; goto out; } strcpy(suna.sun_path, path); - int r = connect(carrier, &suna, sizeof(suna)); + r = connect(carrier, (const struct sockaddr*)&suna, sizeof(suna)); if (r) goto out; int nfds; @@ -77,5 +85,7 @@ static int fishdescriptor_donate(const char *path, const int *fds) { r = 0; out: if (carrier >= 0) close(carrier); + r = !r ? 0 : !errno ? EIO : errno; + esave = errno; return r; } -- 2.30.2