2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2022-04-07 10:11:14 +02:00
|
|
|
* apps/netutils/thttpd/fdwatch.c
|
2011-03-19 22:04:13 +01:00
|
|
|
* FD watcher routines for poll()
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Derived from the file of the same name in the original THTTPD package:
|
|
|
|
*
|
2020-11-26 11:45:23 +01:00
|
|
|
* Copyright (C) 1999,2000 by Jef Poskanzer <jef@mail.acme.com>.
|
2011-03-19 22:04:13 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 AUTHOR 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 AUTHOR 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2023-02-01 14:44:33 +01:00
|
|
|
#include <sys/param.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <debug.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "thttpd_alloc.h"
|
|
|
|
#include "fdwatch.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Debug output from this file is normally suppressed. If enabled, be aware
|
2016-06-20 19:48:15 +02:00
|
|
|
* that output to stdout will interfere with CGI programs.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_FDWATCH_DEBUG
|
2020-05-04 12:52:59 +02:00
|
|
|
# define fwerr nerr
|
|
|
|
# define fwinfo ninfo
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2020-05-04 12:52:59 +02:00
|
|
|
# define fwerr _none
|
|
|
|
# define fwinfo _none
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD_FDWATCH_DEBUG
|
|
|
|
static void fdwatch_dump(const char *msg, FAR struct fdwatch_s *fw)
|
|
|
|
{
|
|
|
|
int i;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("%s\n", msg);
|
|
|
|
fwinfo("nwatched: %d nfds: %d\n", fw->nwatched, fw->nfds);
|
2011-03-19 22:04:13 +01:00
|
|
|
for (i = 0; i < fw->nwatched; i++)
|
2020-05-05 21:03:56 +02:00
|
|
|
{
|
|
|
|
fwinfo("%2d.pollfds: {fd: %d events: %02x revents: %02x} client: %p\n",
|
|
|
|
i + 1, fw->pollfds[i].fd, fw->pollfds[i].events,
|
|
|
|
fw->pollfds[i].revents, fw->client[i]);
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("nactive: %d next: %d\n", fw->nactive, fw->next);
|
2011-03-19 22:04:13 +01:00
|
|
|
for (i = 0; i < fw->nactive; i++)
|
2020-05-05 21:03:56 +02:00
|
|
|
{
|
|
|
|
fwinfo("%2d. %d active\n", i, fw->ready[i]);
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define fdwatch_dump(m,f)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int fdwatch_pollndx(FAR struct fdwatch_s *fw, int fd)
|
|
|
|
{
|
|
|
|
int pollndx;
|
|
|
|
|
|
|
|
/* Get the index associated with the fd */
|
|
|
|
|
|
|
|
for (pollndx = 0; pollndx < fw->nwatched; pollndx++)
|
|
|
|
{
|
|
|
|
if (fw->pollfds[pollndx].fd == fd)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("pollndx: %d\n", pollndx);
|
2011-03-19 22:04:13 +01:00
|
|
|
return pollndx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-26 11:47:00 +01:00
|
|
|
fwerr("ERROR: No poll index for fd %d\n", fd);
|
2011-03-19 22:04:13 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Initialize the fdwatch data structures. Returns -1 on failure. */
|
|
|
|
|
|
|
|
struct fdwatch_s *fdwatch_initialize(int nfds)
|
|
|
|
{
|
|
|
|
FAR struct fdwatch_s *fw;
|
|
|
|
|
|
|
|
/* Allocate the fdwatch data structure */
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
fw = (struct fdwatch_s *)zalloc(sizeof(struct fdwatch_s));
|
2011-03-19 22:04:13 +01:00
|
|
|
if (!fw)
|
|
|
|
{
|
2016-06-20 19:48:15 +02:00
|
|
|
fwerr("ERROR: Failed to allocate fdwatch\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the fdwatch data structures. */
|
|
|
|
|
|
|
|
fw->nfds = nfds;
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
fw->client = (void **)httpd_malloc(sizeof(void *) * nfds);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (!fw->client)
|
|
|
|
{
|
|
|
|
goto errout_with_allocations;
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
fw->pollfds = (struct pollfd *)httpd_malloc(sizeof(struct pollfd) * nfds);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (!fw->pollfds)
|
|
|
|
{
|
|
|
|
goto errout_with_allocations;
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
fw->ready = (uint8_t *)httpd_malloc(sizeof(uint8_t) * nfds);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (!fw->ready)
|
|
|
|
{
|
|
|
|
goto errout_with_allocations;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdwatch_dump("Initial state:", fw);
|
|
|
|
return fw;
|
|
|
|
|
|
|
|
errout_with_allocations:
|
|
|
|
fdwatch_uninitialize(fw);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Uninitialize the fwdatch data structure */
|
|
|
|
|
|
|
|
void fdwatch_uninitialize(struct fdwatch_s *fw)
|
|
|
|
{
|
|
|
|
if (fw)
|
|
|
|
{
|
|
|
|
fdwatch_dump("Uninitializing:", fw);
|
|
|
|
if (fw->client)
|
|
|
|
{
|
|
|
|
httpd_free(fw->client);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fw->pollfds)
|
|
|
|
{
|
|
|
|
httpd_free(fw->pollfds);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fw->ready)
|
|
|
|
{
|
|
|
|
httpd_free(fw->ready);
|
|
|
|
}
|
|
|
|
|
|
|
|
httpd_free(fw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
/* Add a descriptor to the watch list. rw is either FDW_READ or FDW_WRITE. */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
void fdwatch_add_fd(struct fdwatch_s *fw, int fd, void *client_data)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("fd: %d client_data: %p\n", fd, client_data);
|
2011-03-19 22:04:13 +01:00
|
|
|
fdwatch_dump("Before adding:", fw);
|
|
|
|
|
|
|
|
if (fw->nwatched >= fw->nfds)
|
|
|
|
{
|
2016-06-20 19:48:15 +02:00
|
|
|
fwerr("ERROR: too many fds\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new fd at the end of the list */
|
|
|
|
|
|
|
|
fw->pollfds[fw->nwatched].fd = fd;
|
|
|
|
fw->pollfds[fw->nwatched].events = POLLIN;
|
|
|
|
fw->client[fw->nwatched] = client_data;
|
|
|
|
|
|
|
|
/* Increment the count of watched descriptors */
|
|
|
|
|
|
|
|
fw->nwatched++;
|
|
|
|
fdwatch_dump("After adding:", fw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove a descriptor from the watch list. */
|
|
|
|
|
|
|
|
void fdwatch_del_fd(struct fdwatch_s *fw, int fd)
|
|
|
|
{
|
|
|
|
int pollndx;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("fd: %d\n", fd);
|
2011-03-19 22:04:13 +01:00
|
|
|
fdwatch_dump("Before deleting:", fw);
|
|
|
|
|
|
|
|
/* Get the index associated with the fd */
|
|
|
|
|
|
|
|
pollndx = fdwatch_pollndx(fw, fd);
|
|
|
|
if (pollndx >= 0)
|
|
|
|
{
|
|
|
|
/* Decrement the number of fds in the poll table */
|
|
|
|
|
|
|
|
fw->nwatched--;
|
|
|
|
|
2017-05-11 21:40:03 +02:00
|
|
|
/* Replace the deleted one with the one at the end
|
2011-03-19 22:04:13 +01:00
|
|
|
* of the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pollndx != fw->nwatched)
|
|
|
|
{
|
|
|
|
fw->pollfds[pollndx] = fw->pollfds[fw->nwatched];
|
|
|
|
fw->client[pollndx] = fw->client[fw->nwatched];
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 21:03:56 +02:00
|
|
|
|
|
|
|
fdwatch_dump("After deleting:", fw);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the watch. Return value is the number of descriptors that are ready,
|
|
|
|
* or 0 if the timeout expired, or -1 on errors. A timeout of INFTIM means
|
|
|
|
* wait indefinitely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Wait for activity on any of the descriptors. When poll() returns, ret
|
2011-03-19 22:04:13 +01:00
|
|
|
* will hold the number of descriptors with activity (or zero on a timeout
|
|
|
|
* or <0 on an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fdwatch_dump("Before waiting:", fw);
|
2020-11-26 11:47:00 +01:00
|
|
|
fwinfo("Waiting... (timeout %ld)\n", timeout_msecs);
|
2011-03-19 22:04:13 +01:00
|
|
|
fw->nactive = 0;
|
|
|
|
fw->next = 0;
|
|
|
|
ret = poll(fw->pollfds, fw->nwatched, (int)timeout_msecs);
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("Awakened: %d\n", ret);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Look through all of the descriptors and make a list of all of them than
|
|
|
|
* have activity.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < fw->nwatched; i++)
|
|
|
|
{
|
|
|
|
/* Is there activity on this descriptor? */
|
|
|
|
|
2020-05-05 21:03:56 +02:00
|
|
|
if (fw->pollfds[i].revents &
|
|
|
|
(POLLIN | POLLERR | POLLHUP | POLLNVAL))
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Yes... save it in a shorter list */
|
|
|
|
|
2022-04-07 10:11:14 +02:00
|
|
|
fwinfo("pollndx: %d fd: %d revents: %08" PRIx32 "\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
i, fw->pollfds[i].fd, fw->pollfds[i].revents);
|
|
|
|
|
|
|
|
fw->ready[fw->nactive++] = fw->pollfds[i].fd;
|
|
|
|
if (fw->nactive == ret)
|
|
|
|
{
|
|
|
|
/* We have all of them, break out early */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of descriptors with activity */
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("nactive: %d\n", fw->nactive);
|
2011-03-19 22:04:13 +01:00
|
|
|
fdwatch_dump("After wakeup:", fw);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if a descriptor was ready. */
|
|
|
|
|
|
|
|
int fdwatch_check_fd(struct fdwatch_s *fw, int fd)
|
|
|
|
{
|
|
|
|
int pollndx;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("fd: %d\n", fd);
|
2011-03-19 22:04:13 +01:00
|
|
|
fdwatch_dump("Checking:", fw);
|
|
|
|
|
|
|
|
/* Get the index associated with the fd */
|
|
|
|
|
|
|
|
pollndx = fdwatch_pollndx(fw, fd);
|
|
|
|
if (pollndx >= 0 && (fw->pollfds[pollndx].revents & POLLERR) == 0)
|
|
|
|
{
|
|
|
|
return fw->pollfds[pollndx].revents & (POLLIN | POLLHUP | POLLNVAL);
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("POLLERR fd: %d\n", fd);
|
2011-03-19 22:04:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *fdwatch_get_next_client_data(struct fdwatch_s *fw)
|
|
|
|
{
|
|
|
|
fdwatch_dump("Before getting client data:", fw);
|
|
|
|
if (fw->next >= fw->nwatched)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("All client data returned: %d\n", fw->next);
|
2022-10-15 19:55:16 +02:00
|
|
|
return (void *)(uintptr_t)-1;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
fwinfo("client_data[%d]: %p\n", fw->next, fw->client[fw->next]);
|
2011-03-19 22:04:13 +01:00
|
|
|
return fw->client[fw->next++];
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_THTTPD */
|