From 742c0474800867c4ada806706bb853e69bfb36bd Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Wed, 10 Jun 2020 22:55:18 +0200 Subject: [PATCH] only support sending one fd --- asyncutils.nim | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/asyncutils.nim b/asyncutils.nim index 305c600..d536261 100644 --- a/asyncutils.nim +++ b/asyncutils.nim @@ -1,4 +1,4 @@ -import asyncdispatch, threadpool, osproc +import asyncdispatch, threadpool, osproc, options from os import osLastError, newOsError @@ -47,32 +47,34 @@ proc asyncExecCmd*(command: string): Future[int] = proc asyncSendMsg*(fd: AsyncFD, data: string, - fds: openArray[AsyncFD] = []): Future[void] = + ancillaryData: Option[AsyncFD] = none(AsyncFD)): Future[void] = var retFuture = newFuture[void]("asyncSendMsg") proc cb(sock: AsyncFD): bool = - # FIXME: if file descriptors are given in fds, check if sock is a unix socket + # FIXME: if file descriptor is given in ancillaryData, check if sock is a + # unix socket result = true # sendmsg needs an array of iovec structs as described in the writev(2) man # page. The message is passed as a msghdr struct which may contain ancillary # data, see sendmsg(2) man page. We use ancillary data exclusively for - # passing file descriptors if any are given in fds. + # passing a file descriptor if one is given in ancillaryData. var iovec = IOVec(iov_base: data.cstring, iov_len: data.len.csize_t) var msg = Tmsghdr(msg_iov: addr iovec, msg_iovlen: 1) - if fds.len > 0: + if ancillaryData.isSome: # assemble ancillary data, see cmsg(3) man page - let cmsgBufferLen = CMSG_SPACE(sizeof(AsyncFD).csize_t * fds.len.csize_t) + let cmsgBufferLen = CMSG_SPACE(sizeof(AsyncFD).csize_t) let cmsgBuffer = newString(cmsgBufferLen) msg.msg_control = cmsgBuffer.cstring msg.msg_controllen = cmsgBufferLen let cmsg: ptr Tcmsghdr = CMSG_FIRSTHDR(addr msg) - cmsg.cmsg_len = CMSG_LEN(sizeof(AsyncFD).csize_t * fds.len.csize_t) + cmsg.cmsg_len = CMSG_LEN(sizeof(AsyncFD).csize_t) cmsg.cmsg_level = SOL_SOCKET cmsg.cmsg_type = SCM_RIGHTS - copyMem(CMSG_DATA(cmsg), unsafeAddr fds[0], sizeof(AsyncFD) * fds.len) + var ancillaryFd = ancillaryData.get + copyMem(CMSG_DATA(cmsg), addr ancillaryFd, sizeof(AsyncFD)) let res = sendmsg(sock.SocketHandle, addr msg, 0) if res < 0: