+
+/*!
+ * Add a fd to a dynamic pollfd array that is allocated and grown as needed
+ *
+ * This uses an additional array of struct polldata which stores type information
+ * (enum fdtype) and a pointer to anciliary user data.
+ *
+ * 1. Allocate the arrays with the size of "maxconns" if *fdsetp is NULL.
+ * 2. Fill in both array elements and increase count of used elements
+ *
+ * @param maxconns (r) maximum number of connections, determines array size
+ * @param fdsetp (rw) pointer to callers pointer to the pollfd array
+ * @param polldatap (rw) pointer to callers pointer to the polldata array
+ * @param fdset_usedp (rw) pointer to an int with the number of used elements
+ * @param fdset_sizep (rw) pointer to an int which stores the array sizes
+ * @param fd (r) file descriptor to add to the arrays
+ * @param fdtype (r) type of fd, currently IPC_FD or LISTEN_FD
+ * @param data (rw) pointer to data the caller want to associate with an fd
+ */
+void fdset_add_fd(int maxconns,
+ struct pollfd **fdsetp,
+ struct polldata **polldatap,
+ int *fdset_usedp,
+ int *fdset_sizep,
+ int fd,
+ enum fdtype fdtype,
+ void *data)
+{
+ struct pollfd *fdset = *fdsetp;
+ struct polldata *polldata = *polldatap;
+ int fdset_size = *fdset_sizep;
+
+ LOG(log_debug, logtype_default, "fdset_add_fd: adding fd %i in slot %i", fd, *fdset_usedp);
+
+ if (fdset == NULL) { /* 1 */
+ /* Initialize with space for all possibly active fds */
+ fdset = calloc(maxconns, sizeof(struct pollfd));
+ if (! fdset)
+ exit(EXITERR_SYS);
+
+ polldata = calloc(maxconns, sizeof(struct polldata));
+ if (! polldata)
+ exit(EXITERR_SYS);
+
+ fdset_size = maxconns;
+
+ *fdset_sizep = fdset_size;
+ *fdsetp = fdset;
+ *polldatap = polldata;
+
+ LOG(log_debug, logtype_default, "fdset_add_fd: initialized with space for %i conncections",
+ maxconns);
+ }
+
+ /* 2 */
+ fdset[*fdset_usedp].fd = fd;
+ fdset[*fdset_usedp].events = POLLIN;
+ polldata[*fdset_usedp].fdtype = fdtype;
+ polldata[*fdset_usedp].data = data;
+ (*fdset_usedp)++;
+}
+
+/*!
+ * Remove a fd from our pollfd array
+ *
+ * 1. Search fd
+ * 2a Matched last (or only) in the set ? null it and return
+ * 2b If we remove the last array elemnt, just decrease count
+ * 3. If found move all following elements down by one
+ * 4. Decrease count of used elements in array
+ *
+ * This currently doesn't shrink the allocated storage of the array.
+ *
+ * @param fdsetp (rw) pointer to callers pointer to the pollfd array
+ * @param polldatap (rw) pointer to callers pointer to the polldata array
+ * @param fdset_usedp (rw) pointer to an int with the number of used elements
+ * @param fdset_sizep (rw) pointer to an int which stores the array sizes
+ * @param fd (r) file descriptor to remove from the arrays
+ */
+void fdset_del_fd(struct pollfd **fdsetp,
+ struct polldata **polldatap,
+ int *fdset_usedp,
+ int *fdset_sizep _U_,
+ int fd)
+{
+ struct pollfd *fdset = *fdsetp;
+ struct polldata *polldata = *polldatap;
+
+ if (*fdset_usedp < 1)
+ return;
+
+ for (int i = 0; i < *fdset_usedp; i++) {
+ if (fdset[i].fd == fd) { /* 1 */
+ if ((i + 1) == *fdset_usedp) { /* 2a */
+ fdset[i].fd = -1;
+ memset(&polldata[i], 0, sizeof(struct polldata));
+ } else if (i < (*fdset_usedp - 1)) { /* 2b */
+ memmove(&fdset[i],
+ &fdset[i+1],
+ (*fdset_usedp - i - 1) * sizeof(struct pollfd)); /* 3 */
+ memmove(&polldata[i],
+ &polldata[i+1],
+ (*fdset_usedp - i - 1) * sizeof(struct polldata)); /* 3 */
+ }
+ (*fdset_usedp)--;
+ break;
+ }
+ }
+}
+
+/* Length of the space taken up by a padded control message of length len */
+#ifndef CMSG_SPACE
+#define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
+#endif
+
+/*
+ * Receive a fd on a suitable socket
+ * @args fd (r) PF_UNIX socket to receive on
+ * @args nonblocking (r) 0: fd is in blocking mode - 1: fd is nonblocking, poll for 1 sec
+ * @returns fd on success, -1 on error
+ */
+int recv_fd(int fd, int nonblocking)
+{
+ int ret;
+ struct msghdr msgh;
+ struct iovec iov[1];
+ struct cmsghdr *cmsgp = NULL;
+ char buf[CMSG_SPACE(sizeof(int))];
+ char dbuf[80];
+ struct pollfd pollfds[1];
+
+ pollfds[0].fd = fd;
+ pollfds[0].events = POLLIN;
+
+ memset(&msgh,0,sizeof(msgh));
+ memset(buf,0,sizeof(buf));
+
+ msgh.msg_name = NULL;
+ msgh.msg_namelen = 0;
+
+ msgh.msg_iov = iov;
+ msgh.msg_iovlen = 1;
+
+ iov[0].iov_base = dbuf;
+ iov[0].iov_len = sizeof(dbuf);
+
+ msgh.msg_control = buf;
+ msgh.msg_controllen = sizeof(buf);
+
+ if (nonblocking) {
+ do {
+ ret = poll(pollfds, 1, 2000); /* poll 2 seconds, evtl. multipe times (EINTR) */
+ } while ( ret == -1 && errno == EINTR );
+ if (ret != 1)
+ return -1;
+ ret = recvmsg(fd, &msgh, 0);
+ } else {
+ do {
+ ret = recvmsg(fd, &msgh, 0);
+ } while ( ret == -1 && errno == EINTR );
+ }
+
+ if ( ret == -1 ) {
+ return -1;
+ }
+
+ for ( cmsgp = CMSG_FIRSTHDR(&msgh); cmsgp != NULL; cmsgp = CMSG_NXTHDR(&msgh,cmsgp) ) {
+ if ( cmsgp->cmsg_level == SOL_SOCKET && cmsgp->cmsg_type == SCM_RIGHTS ) {
+ return *(int *) CMSG_DATA(cmsgp);
+ }
+ }
+
+ if ( ret == sizeof (int) )
+ errno = *(int *)dbuf; /* Rcvd errno */
+ else
+ errno = ENOENT; /* Default errno */
+
+ return -1;
+}
+
+/*
+ * Send a fd across a suitable socket
+ */
+int send_fd(int socket, int fd)
+{
+ int ret;
+ struct msghdr msgh;
+ struct iovec iov[1];
+ struct cmsghdr *cmsgp = NULL;
+ char *buf;
+ size_t size;
+ int er=0;
+
+ size = CMSG_SPACE(sizeof fd);
+ buf = malloc(size);
+ if (!buf) {
+ LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
+ return -1;
+ }
+
+ memset(&msgh,0,sizeof (msgh));
+ memset(buf,0, size);
+
+ msgh.msg_name = NULL;
+ msgh.msg_namelen = 0;
+
+ msgh.msg_iov = iov;
+ msgh.msg_iovlen = 1;
+
+ iov[0].iov_base = &er;
+ iov[0].iov_len = sizeof(er);
+
+ msgh.msg_control = buf;
+ msgh.msg_controllen = size;
+
+ cmsgp = CMSG_FIRSTHDR(&msgh);
+ cmsgp->cmsg_level = SOL_SOCKET;
+ cmsgp->cmsg_type = SCM_RIGHTS;
+ cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
+
+ *((int *)CMSG_DATA(cmsgp)) = fd;
+ msgh.msg_controllen = cmsgp->cmsg_len;
+
+ do {
+ ret = sendmsg(socket,&msgh, 0);
+ } while ( ret == -1 && errno == EINTR );
+ if (ret == -1) {
+ LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
+ free(buf);
+ return -1;
+ }
+ free(buf);
+ return 0;
+}