2017-04-18 14:35:34 +03:00
|
|
|
/* $NetBSD: svc_fdset.c,v 1.16 2017/04/18 11:35:34 maya Exp $ */
|
2015-11-07 02:11:09 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2015 The NetBSD Foundation, Inc.
|
2015-11-07 23:24:00 +03:00
|
|
|
* All rights reserved.
|
2015-11-07 02:11:09 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2013-03-05 23:55:22 +04:00
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2017-04-18 14:35:34 +03:00
|
|
|
__RCSID("$NetBSD: svc_fdset.c,v 1.16 2017/04/18 11:35:34 maya Exp $");
|
2015-11-06 22:34:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
#include "reentrant.h"
|
|
|
|
|
|
|
|
#include <sys/fd_set.h>
|
|
|
|
|
|
|
|
#include <rpc/rpc.h>
|
2013-03-05 23:55:22 +04:00
|
|
|
|
2015-11-07 03:42:04 +03:00
|
|
|
#ifdef FDSET_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <lwp.h>
|
|
|
|
#endif
|
2013-03-05 23:55:22 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-11-08 02:09:20 +03:00
|
|
|
#include <poll.h>
|
2013-03-05 23:55:22 +04:00
|
|
|
|
2015-11-07 03:42:04 +03:00
|
|
|
#include "svc_fdset.h"
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
#undef svc_fdset
|
|
|
|
#undef svc_maxfd
|
2015-11-08 05:46:53 +03:00
|
|
|
#ifdef _LIBC
|
2015-11-07 03:42:04 +03:00
|
|
|
extern __fd_set_256 svc_fdset;
|
2015-11-08 05:46:53 +03:00
|
|
|
#endif
|
2015-11-07 02:11:09 +03:00
|
|
|
extern int svc_maxfd;
|
2015-11-08 02:09:20 +03:00
|
|
|
int __svc_flags;
|
2015-11-07 02:11:09 +03:00
|
|
|
|
|
|
|
struct svc_fdset {
|
2015-11-08 02:09:20 +03:00
|
|
|
/* select */
|
2015-11-06 22:34:13 +03:00
|
|
|
fd_set *fdset;
|
|
|
|
int fdmax;
|
|
|
|
int fdsize;
|
2015-11-08 02:09:20 +03:00
|
|
|
/* poll */
|
|
|
|
struct pollfd *fdp;
|
|
|
|
int fdnum;
|
|
|
|
int fdused;
|
2015-11-06 22:34:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The single threaded, one global fd_set version */
|
2015-11-07 02:11:09 +03:00
|
|
|
static struct svc_fdset __svc_fdset;
|
2015-11-06 22:34:13 +03:00
|
|
|
|
|
|
|
static thread_key_t fdsetkey = -2;
|
|
|
|
|
|
|
|
#ifdef FDSET_DEBUG
|
|
|
|
|
|
|
|
static void __printflike(3, 0)
|
|
|
|
svc_header(const char *func, size_t line, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(),
|
|
|
|
(int)_lwp_self(), func, line);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
static void __printflike(4, 5)
|
|
|
|
svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds,
|
2015-11-06 22:34:13 +03:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
const char *did = "";
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
svc_header(func, line, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2015-11-08 22:30:53 +03:00
|
|
|
fprintf(stderr, "%p[%d] fd_set<", fds->fdset, fds->fdmax);
|
2015-11-07 02:11:09 +03:00
|
|
|
for (int i = 0; i <= fds->fdmax; i++) {
|
|
|
|
if (!FD_ISSET(i, fds->fdset))
|
2015-11-06 22:34:13 +03:00
|
|
|
continue;
|
|
|
|
fprintf(stderr, "%s%d", did, i);
|
|
|
|
did = ", ";
|
|
|
|
}
|
2015-11-08 22:30:53 +03:00
|
|
|
did = "";
|
|
|
|
fprintf(stderr, "> poll<");
|
|
|
|
for (int i = 0; i < fds->fdused; i++) {
|
|
|
|
int fd = fds->fdp[i].fd;
|
|
|
|
if (fd == -1)
|
|
|
|
continue;
|
|
|
|
fprintf(stderr, "%s%d", did, fd);
|
|
|
|
did = ", ";
|
|
|
|
}
|
2015-11-06 22:34:13 +03:00
|
|
|
fprintf(stderr, ">\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __printflike(3, 4)
|
|
|
|
svc_print(const char *func, size_t line, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
svc_header(func, line, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__)
|
2015-11-07 02:11:09 +03:00
|
|
|
#define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
|
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
#else
|
2015-11-07 02:11:09 +03:00
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
#define DPRINTF(...)
|
2015-11-07 02:11:09 +03:00
|
|
|
#define DPRINTF_FDSET(...)
|
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
static inline void
|
|
|
|
svc_fdset_sanitize(struct svc_fdset *fds)
|
|
|
|
{
|
|
|
|
while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset))
|
|
|
|
fds->fdmax--;
|
2015-11-08 05:46:53 +03:00
|
|
|
#ifdef _LIBC
|
2015-11-07 02:11:09 +03:00
|
|
|
/* Compat update */
|
|
|
|
if (fds == &__svc_fdset) {
|
2015-11-10 23:56:20 +03:00
|
|
|
svc_fdset = *(__fd_set_256 *)(void *)__svc_fdset.fdset;
|
2015-11-07 02:11:09 +03:00
|
|
|
svc_maxfd = __svc_fdset.fdmax;
|
|
|
|
}
|
2015-11-08 05:46:53 +03:00
|
|
|
#endif
|
2015-11-07 02:11:09 +03:00
|
|
|
}
|
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
static void
|
|
|
|
svc_fdset_free(void *v)
|
|
|
|
{
|
2015-11-07 02:11:09 +03:00
|
|
|
struct svc_fdset *fds = v;
|
|
|
|
DPRINTF_FDSET(fds, "free");
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-08 02:09:20 +03:00
|
|
|
free(fds->fdp);
|
2015-11-07 02:11:09 +03:00
|
|
|
free(fds->fdset);
|
|
|
|
free(fds);
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
2015-11-08 02:09:20 +03:00
|
|
|
static void
|
|
|
|
svc_pollfd_init(struct pollfd *pfd, int nfd)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nfd; i++) {
|
|
|
|
pfd[i].fd = -1;
|
|
|
|
pfd[i].events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
|
2015-11-08 02:17:09 +03:00
|
|
|
pfd[i].revents = 0;
|
2015-11-08 02:09:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pollfd *
|
|
|
|
svc_pollfd_alloc(struct svc_fdset *fds)
|
|
|
|
{
|
2015-11-08 22:30:53 +03:00
|
|
|
if (fds->fdp != NULL)
|
|
|
|
return fds->fdp;
|
|
|
|
|
2015-11-08 02:09:20 +03:00
|
|
|
fds->fdnum = FD_SETSIZE;
|
|
|
|
fds->fdp = calloc(fds->fdnum, sizeof(*fds->fdp));
|
|
|
|
if (fds->fdp == NULL)
|
|
|
|
return NULL;
|
|
|
|
svc_pollfd_init(fds->fdp, fds->fdnum);
|
|
|
|
return fds->fdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct svc_fdset *
|
|
|
|
svc_pollfd_add(int fd, struct svc_fdset *fds)
|
|
|
|
{
|
|
|
|
struct pollfd *pfd;
|
|
|
|
|
|
|
|
if ((pfd = svc_pollfd_alloc(fds)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < fds->fdnum; i++)
|
|
|
|
if (pfd[i].fd == -1) {
|
2015-11-08 22:30:53 +03:00
|
|
|
if (i >= fds->fdused)
|
2015-11-08 02:09:20 +03:00
|
|
|
fds->fdused = i + 1;
|
2015-11-08 22:30:53 +03:00
|
|
|
DPRINTF("add fd=%d slot=%d fdused=%d",
|
|
|
|
fd, i, fds->fdused);
|
2015-11-08 02:09:20 +03:00
|
|
|
pfd[i].fd = fd;
|
|
|
|
return fds;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfd = realloc(fds->fdp, (fds->fdnum + FD_SETSIZE) * sizeof(*fds->fdp));
|
|
|
|
if (pfd == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
svc_pollfd_init(pfd + fds->fdnum, FD_SETSIZE);
|
|
|
|
pfd[fds->fdnum].fd = fd;
|
|
|
|
fds->fdused = fds->fdnum + 1;
|
2015-11-08 22:30:53 +03:00
|
|
|
DPRINTF("add fd=%d slot=%d fdused=%d", fd, fds->fdnum, fds->fdused);
|
2015-11-08 02:09:20 +03:00
|
|
|
fds->fdnum += FD_SETSIZE;
|
2015-11-10 21:11:05 +03:00
|
|
|
fds->fdp = pfd;
|
2015-11-08 02:09:20 +03:00
|
|
|
return fds;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct svc_fdset *
|
|
|
|
svc_pollfd_del(int fd, struct svc_fdset *fds)
|
|
|
|
{
|
|
|
|
struct pollfd *pfd;
|
|
|
|
|
|
|
|
if ((pfd = svc_pollfd_alloc(fds)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < fds->fdnum; i++) {
|
|
|
|
if (pfd[i].fd != fd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pfd[i].fd = -1;
|
2015-11-08 22:30:53 +03:00
|
|
|
DPRINTF("del fd=%d slot=%d", fd, fds->fdused);
|
2015-11-08 02:09:20 +03:00
|
|
|
if (i != fds->fdused - 1)
|
|
|
|
return fds;
|
|
|
|
|
|
|
|
do
|
|
|
|
if (pfd[i].fd != -1)
|
|
|
|
break;
|
|
|
|
while (--i >= 0);
|
2015-11-08 22:30:53 +03:00
|
|
|
|
2015-11-08 02:09:20 +03:00
|
|
|
fds->fdused = i + 1;
|
2015-11-08 22:30:53 +03:00
|
|
|
DPRINTF("del fd=%d fdused=%d", fd, fds->fdused);
|
2015-11-08 02:09:20 +03:00
|
|
|
return fds;
|
|
|
|
}
|
2015-11-08 22:30:53 +03:00
|
|
|
DPRINTF("del fd=%d not found", fd);
|
2015-11-08 02:09:20 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
static struct svc_fdset *
|
|
|
|
svc_fdset_resize(int fd, struct svc_fdset *fds)
|
2015-11-06 22:34:13 +03:00
|
|
|
{
|
2015-11-07 02:11:09 +03:00
|
|
|
if (fds->fdset && fd < fds->fdsize) {
|
|
|
|
DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize);
|
|
|
|
return fds;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fd += FD_SETSIZE;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd));
|
2015-11-06 22:34:13 +03:00
|
|
|
if (newfdset == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
memset(newfdset + __NFD_BYTES(fds->fdsize), 0,
|
|
|
|
__NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize));
|
2015-11-06 22:34:13 +03:00
|
|
|
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
fds->fdset = (void *)newfdset;
|
|
|
|
DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize);
|
|
|
|
fds->fdsize = fd;
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
return fds;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
static struct svc_fdset *
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_alloc(int fd)
|
|
|
|
{
|
2015-11-07 02:11:09 +03:00
|
|
|
struct svc_fdset *fds;
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-07 06:06:32 +03:00
|
|
|
if (!__isthreaded || fdsetkey == -2)
|
|
|
|
return svc_fdset_resize(fd, &__svc_fdset);
|
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
if (fdsetkey == -1)
|
|
|
|
thr_keycreate(&fdsetkey, svc_fdset_free);
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
if ((fds = thr_getspecific(fdsetkey)) == NULL) {
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
fds = calloc(1, sizeof(*fds));
|
|
|
|
if (fds == NULL)
|
2015-11-06 22:34:13 +03:00
|
|
|
return NULL;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
(void)thr_setspecific(fdsetkey, fds);
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
if (__svc_fdset.fdsize != 0) {
|
|
|
|
*fds = __svc_fdset;
|
|
|
|
DPRINTF("switching to %p", fds->fdset);
|
2015-11-06 22:34:13 +03:00
|
|
|
} else {
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF("first thread time %p", fds->fdset);
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF("again for %p", fds->fdset);
|
|
|
|
if (fd < fds->fdsize)
|
|
|
|
return fds;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
2015-11-07 02:11:09 +03:00
|
|
|
|
|
|
|
return svc_fdset_resize(fd, fds);
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
2013-03-05 23:55:22 +04:00
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
/* allow each thread to have their own copy */
|
|
|
|
void
|
|
|
|
svc_fdset_init(int flags)
|
|
|
|
{
|
|
|
|
DPRINTF("%x", flags);
|
2015-11-08 02:09:20 +03:00
|
|
|
__svc_flags = flags;
|
2015-11-06 22:34:13 +03:00
|
|
|
if ((flags & SVC_FDSET_MT) && fdsetkey == -2)
|
|
|
|
fdsetkey = -1;
|
|
|
|
}
|
2013-03-05 23:55:22 +04:00
|
|
|
|
|
|
|
void
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_zero(void)
|
2013-03-05 23:55:22 +04:00
|
|
|
{
|
2015-11-06 22:34:13 +03:00
|
|
|
DPRINTF("zero");
|
2015-11-08 02:09:20 +03:00
|
|
|
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(0);
|
2015-11-10 21:11:05 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return;
|
2015-11-07 02:11:09 +03:00
|
|
|
memset(fds->fdset, 0, fds->fdsize);
|
2015-11-07 06:06:32 +03:00
|
|
|
fds->fdmax = -1;
|
2015-11-08 02:09:20 +03:00
|
|
|
|
|
|
|
free(fds->fdp);
|
|
|
|
fds->fdp = NULL;
|
|
|
|
fds->fdnum = fds->fdused = 0;
|
2013-03-05 23:55:22 +04:00
|
|
|
}
|
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
int
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_set(int fd)
|
2013-03-05 23:55:22 +04:00
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(fd);
|
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
FD_SET(fd, fds->fdset);
|
|
|
|
if (fd > fds->fdmax)
|
|
|
|
fds->fdmax = fd;
|
2015-11-07 06:06:32 +03:00
|
|
|
|
2015-11-08 22:30:53 +03:00
|
|
|
int rv = svc_pollfd_add(fd, fds) ? 0 : -1;
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF_FDSET(fds, "%d", fd);
|
|
|
|
|
|
|
|
svc_fdset_sanitize(fds);
|
2015-11-08 22:30:53 +03:00
|
|
|
return rv;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
2013-03-05 23:55:22 +04:00
|
|
|
|
2015-11-06 22:34:13 +03:00
|
|
|
int
|
|
|
|
svc_fdset_isset(int fd)
|
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(fd);
|
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF_FDSET(fds, "%d", fd);
|
2015-11-07 06:06:32 +03:00
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
return FD_ISSET(fd, fds->fdset) != 0;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
int
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_clr(int fd)
|
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(fd);
|
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
FD_CLR(fd, fds->fdset);
|
2015-11-08 22:30:53 +03:00
|
|
|
|
|
|
|
int rv = svc_pollfd_del(fd, fds) ? 0 : -1;
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF_FDSET(fds, "%d", fd);
|
2015-11-07 06:06:32 +03:00
|
|
|
|
|
|
|
svc_fdset_sanitize(fds);
|
2015-11-08 22:30:53 +03:00
|
|
|
return rv;
|
2013-03-05 23:55:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fd_set *
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_copy(const fd_set *orig)
|
2013-03-05 23:55:22 +04:00
|
|
|
{
|
2015-11-07 02:11:09 +03:00
|
|
|
int size = svc_fdset_getsize(0);
|
2017-04-18 14:35:34 +03:00
|
|
|
if (size == -1)
|
|
|
|
return NULL;
|
2015-11-07 02:11:09 +03:00
|
|
|
fd_set *copy = calloc(1, __NFD_BYTES(size));
|
|
|
|
if (copy == NULL)
|
2015-11-06 22:34:13 +03:00
|
|
|
return NULL;
|
|
|
|
if (orig)
|
2015-11-07 02:11:09 +03:00
|
|
|
memcpy(copy, orig, __NFD_BYTES(size));
|
|
|
|
return copy;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fd_set *
|
|
|
|
svc_fdset_get(void)
|
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(0);
|
2015-11-07 02:11:09 +03:00
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-11-07 02:11:09 +03:00
|
|
|
DPRINTF_FDSET(fds, "get");
|
2015-11-07 06:06:32 +03:00
|
|
|
svc_fdset_sanitize(fds);
|
2015-11-07 02:11:09 +03:00
|
|
|
return fds->fdset;
|
2013-03-05 23:55:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int *
|
2015-11-06 22:34:13 +03:00
|
|
|
svc_fdset_getmax(void)
|
2013-03-05 23:55:22 +04:00
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(0);
|
2015-11-06 22:34:13 +03:00
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2015-11-07 06:06:32 +03:00
|
|
|
DPRINTF_FDSET(fds, "getmax");
|
|
|
|
svc_fdset_sanitize(fds);
|
2015-11-07 02:11:09 +03:00
|
|
|
return &fds->fdmax;
|
2015-11-06 22:34:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
svc_fdset_getsize(int fd)
|
|
|
|
{
|
2015-11-07 06:06:32 +03:00
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(fd);
|
2013-03-05 23:55:22 +04:00
|
|
|
|
2015-11-07 19:58:24 +03:00
|
|
|
if (fds == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2015-11-07 06:06:32 +03:00
|
|
|
DPRINTF_FDSET(fds, "getsize");
|
2015-11-07 02:11:09 +03:00
|
|
|
return fds->fdsize;
|
2013-03-05 23:55:22 +04:00
|
|
|
}
|
2015-11-08 02:09:20 +03:00
|
|
|
|
|
|
|
struct pollfd *
|
|
|
|
svc_pollfd_copy(const struct pollfd *orig)
|
|
|
|
{
|
|
|
|
int size = svc_fdset_getsize(0);
|
2015-11-10 21:08:05 +03:00
|
|
|
if (size == -1)
|
|
|
|
return NULL;
|
2015-11-08 02:09:20 +03:00
|
|
|
struct pollfd *copy = calloc(size, sizeof(*orig));
|
|
|
|
if (copy == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (orig)
|
|
|
|
memcpy(copy, orig, size * sizeof(*orig));
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct pollfd *
|
|
|
|
svc_pollfd_get(void)
|
|
|
|
{
|
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(0);
|
|
|
|
|
|
|
|
if (fds == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
DPRINTF_FDSET(fds, "getpoll");
|
|
|
|
return fds->fdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int *
|
|
|
|
svc_pollfd_getmax(void)
|
|
|
|
{
|
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(0);
|
|
|
|
|
|
|
|
if (fds == NULL)
|
|
|
|
return NULL;
|
|
|
|
return &fds->fdused;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
svc_pollfd_getsize(int fd)
|
|
|
|
{
|
|
|
|
struct svc_fdset *fds = svc_fdset_alloc(fd);
|
|
|
|
|
|
|
|
if (fds == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
DPRINTF_FDSET(fds, "getsize");
|
|
|
|
return fds->fdnum;
|
|
|
|
}
|