2009-07-12 22:27:56 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* netutils/thttpd/timers.c
|
|
|
|
|
* FD watcher routines for poll()
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
|
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
|
*
|
|
|
|
|
* Derived from the file of the same name in the original THTTPD package:
|
|
|
|
|
*
|
|
|
|
|
* Copyright <EFBFBD> 1999,2000 by Jef Poskanzer <jef@mail.acme.com>.
|
|
|
|
|
* 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 <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
#include <poll.h>
|
2009-08-16 21:24:18 +02:00
|
|
|
|
#include <debug.h>
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
|
|
|
|
#include "config.h"
|
2009-09-11 17:15:35 +02:00
|
|
|
|
#include "httpd_alloc.h"
|
2009-07-12 22:27:56 +02:00
|
|
|
|
#include "fdwatch.h"
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_THTTPD
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
|
# define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Data
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
2009-07-18 20:04:05 +02:00
|
|
|
|
static long nwatches = 0;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Function Prototypes
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
#ifdef CONFIG_THTTPD_FDWATCH_DEBUG
|
|
|
|
|
static void fdwatch_dump(const char *msg, FAR struct fdwatch_s *fw)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
nvdbg("%s\n", msg);
|
|
|
|
|
nvdbg("nwatched: %d nfds: %d\n", fw->nwatched, fw->nfds);
|
|
|
|
|
for (i = 0; i < fw->nwatched; i++)
|
|
|
|
|
{
|
|
|
|
|
nvdbg("%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]);
|
|
|
|
|
}
|
|
|
|
|
nvdbg("nactive: %d next: %d\n", fw->nactive, fw->next);
|
|
|
|
|
for (i = 0; i < fw->nactive; i++)
|
|
|
|
|
{
|
|
|
|
|
nvdbg("%2d. %d active\n", i, fw->ready[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
# define fdwatch_dump(m,f)
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
nvdbg("pollndx: %d\n", pollndx);
|
|
|
|
|
return pollndx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ndbg("No poll index for fd %d: %d\n", fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/* Initialize the fdwatch data structures. Returns -1 on failure. */
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
struct fdwatch_s *fdwatch_initialize(int nfds)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-02 20:12:22 +02:00
|
|
|
|
FAR struct fdwatch_s *fw;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
/* Allocate the fdwatch data structure */
|
|
|
|
|
|
|
|
|
|
fw = (struct fdwatch_s*)zalloc(sizeof(struct fdwatch_s));
|
|
|
|
|
if (!fw)
|
|
|
|
|
{
|
|
|
|
|
ndbg("Failed to allocate fdwatch\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 22:27:56 +02:00
|
|
|
|
/* Initialize the fdwatch data structures. */
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
fw->nfds = nfds;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-09-11 17:15:35 +02:00
|
|
|
|
fw->client = (void**)httpd_malloc(sizeof(void*) * nfds);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (!fw->client)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-07-18 20:04:05 +02:00
|
|
|
|
goto errout_with_allocations;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-11 17:15:35 +02:00
|
|
|
|
fw->pollfds = (struct pollfd*)httpd_malloc(sizeof(struct pollfd) * nfds);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
if (!fw->pollfds)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-07-18 20:04:05 +02:00
|
|
|
|
goto errout_with_allocations;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-11 17:15:35 +02:00
|
|
|
|
fw->ready = (uint8*)httpd_malloc(sizeof(uint8) * nfds);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (!fw->ready)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-07-18 20:04:05 +02:00
|
|
|
|
goto errout_with_allocations;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
2009-08-16 21:24:18 +02:00
|
|
|
|
|
|
|
|
|
fdwatch_dump("Initial state:", fw);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
return fw;
|
|
|
|
|
|
|
|
|
|
errout_with_allocations:
|
|
|
|
|
fdwatch_uninitialize(fw);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Uninitialize the fwdatch data structure */
|
|
|
|
|
|
|
|
|
|
void fdwatch_uninitialize(struct fdwatch_s *fw)
|
|
|
|
|
{
|
|
|
|
|
if (fw)
|
|
|
|
|
{
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Uninitializing:", fw);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (fw->client)
|
2009-07-18 20:04:05 +02:00
|
|
|
|
{
|
2009-09-11 17:15:35 +02:00
|
|
|
|
httpd_free(fw->client);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fw->pollfds)
|
|
|
|
|
{
|
2009-09-11 17:15:35 +02:00
|
|
|
|
httpd_free(fw->pollfds);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (fw->ready)
|
2009-07-18 20:04:05 +02:00
|
|
|
|
{
|
2009-09-11 17:15:35 +02:00
|
|
|
|
httpd_free(fw->ready);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-09-11 17:15:35 +02:00
|
|
|
|
httpd_free(fw);
|
2009-07-18 20:04:05 +02:00
|
|
|
|
}
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add a descriptor to the watch list. rw is either FDW_READ or FDW_WRITE. */
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
void fdwatch_add_fd(struct fdwatch_s *fw, int fd, void *client_data)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-08 17:14:10 +02:00
|
|
|
|
nvdbg("fd: %d client_data: %p\n", fd, client_data);
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Before adding:", fw);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (fw->nwatched >= fw->nfds)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("too many fds\n");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Save the new fd at the end of the list */
|
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
fw->pollfds[fw->nwatched].fd = fd;
|
|
|
|
|
fw->pollfds[fw->nwatched].events = POLLIN;
|
|
|
|
|
fw->client[fw->nwatched] = client_data;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Increment the count of watched descriptors */
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
fw->nwatched++;
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("After adding:", fw);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Remove a descriptor from the watch list. */
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
void fdwatch_del_fd(struct fdwatch_s *fw, int fd)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
|
|
|
|
int pollndx;
|
2009-08-02 20:12:22 +02:00
|
|
|
|
|
|
|
|
|
nvdbg("fd: %d\n", fd);
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Before deleting:", fw);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Get the index associated with the fd */
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
pollndx = fdwatch_pollndx(fw, fd);
|
|
|
|
|
if (pollndx >= 0)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Decrement the number of fds in the poll table */
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
fw->nwatched--;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Replace the deleted one with the one at the the end
|
|
|
|
|
* of the list.
|
|
|
|
|
*/
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
if (pollndx != fw->nwatched)
|
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
fw->pollfds[pollndx] = fw->pollfds[fw->nwatched];
|
|
|
|
|
fw->client[pollndx] = fw->client[fw->nwatched];
|
2009-08-02 20:12:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("After deleting:", fw);
|
2009-07-12 22:27:56 +02: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
|
|
|
|
nwatches++;
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Wait for activity on any of the desciptors. When poll() returns, ret
|
|
|
|
|
* will hold the number of descriptors with activity (or zero on a timeout
|
|
|
|
|
* or <0 on an error.
|
|
|
|
|
*/
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Before waiting:", fw);
|
|
|
|
|
nvdbg("Waiting... (timeout %d)\n", timeout_msecs);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
fw->nactive = 0;
|
|
|
|
|
fw->next = 0;
|
2009-08-16 21:24:18 +02:00
|
|
|
|
ret = poll(fw->pollfds, fw->nwatched, (int)timeout_msecs);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
nvdbg("Awakened: %d\n", ret);
|
|
|
|
|
|
|
|
|
|
/* Look through all of the descriptors and make a list of all of them than
|
|
|
|
|
* have activity.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (ret > 0)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-02 20:12:22 +02:00
|
|
|
|
for (i = 0; i < fw->nwatched; i++)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Is there activity on this descriptor? */
|
|
|
|
|
|
|
|
|
|
if (fw->pollfds[i].revents & (POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL))
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Yes... save it in a shorter list */
|
|
|
|
|
|
|
|
|
|
fw->ready[fw->nactive++] = fw->pollfds[i].fd;
|
|
|
|
|
if (fw->nactive == ret)
|
|
|
|
|
{
|
|
|
|
|
/* We have all of them, break out early */
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Return the number of descriptors with activity */
|
|
|
|
|
|
|
|
|
|
nvdbg("nactive: %d\n", fw->nactive);
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("After wakeup:", fw);
|
2009-08-02 20:12:22 +02:00
|
|
|
|
return ret;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check if a descriptor was ready. */
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
int fdwatch_check_fd(struct fdwatch_s *fw, int fd)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
|
|
|
|
int pollndx;
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
nvdbg("fd: %d\n", fd);
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Checking:", fw);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
/* Get the index associated with the fd */
|
2009-07-12 22:27:56 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
pollndx = fdwatch_pollndx(fw, fd);
|
|
|
|
|
if (pollndx >= 0 && (fw->pollfds[pollndx].revents & POLLERR) == 0)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-16 17:26:52 +02:00
|
|
|
|
return fw->pollfds[pollndx].revents & (POLLIN | POLLHUP | POLLNVAL);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 20:12:22 +02:00
|
|
|
|
nvdbg("POLLERR fd: %d\n", fd);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-18 20:04:05 +02:00
|
|
|
|
void *fdwatch_get_next_client_data(struct fdwatch_s *fw)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("Before getting client data:", fw);
|
2009-08-08 17:14:10 +02:00
|
|
|
|
if (fw->next >= fw->nwatched)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-08 17:14:10 +02:00
|
|
|
|
nvdbg("All client data returned: %d\n", fw->next);
|
|
|
|
|
return (void*)-1;
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
2009-08-08 17:14:10 +02:00
|
|
|
|
|
2009-08-16 17:26:52 +02:00
|
|
|
|
nvdbg("client_data[%d]: %p\n", fw->next, fw->client[fw->next]);
|
|
|
|
|
return fw->client[fw->next++];
|
2009-07-12 22:27:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Generate debugging statistics ndbg message. */
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
|
2009-07-18 20:04:05 +02:00
|
|
|
|
void fdwatch_logstats(struct fdwatch_s *fw, long secs)
|
2009-07-12 22:27:56 +02:00
|
|
|
|
{
|
2009-08-16 21:24:18 +02:00
|
|
|
|
fdwatch_dump("LOG stats:", fw);
|
2009-07-12 22:27:56 +02:00
|
|
|
|
if (secs > 0)
|
2009-08-02 20:12:22 +02:00
|
|
|
|
{
|
|
|
|
|
ndbg("fdwatch - %ld polls (%g/sec)\n", nwatches, (float)nwatches / secs);
|
|
|
|
|
}
|
2009-07-12 22:27:56 +02:00
|
|
|
|
nwatches = 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif /* CONFIG_THTTPD */
|
|
|
|
|
|