/*
* libev event processing core, watcher management
*
* Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, 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 ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, 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 OTH-
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License ("GPL") version 2 or any later version,
* in which case the provisions of the GPL are applicable instead of
* the above. If you wish to allow the use of your version of this file
* only under the terms of the GPL and not to allow others to use your
* version of this file under the BSD license, indicate your decision
* by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete the
* provisions above, a recipient may use your version of this file under
* either the BSD or the GPL.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* this big block deduces configuration from config.h */
#ifndef EV_STANDALONE
# ifdef EV_CONFIG_H
# include EV_CONFIG_H
# else
# include "config.h"
# endif
# if HAVE_CLOCK_SYSCALL
# ifndef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 1
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# endif
# elif !defined(EV_USE_CLOCK_SYSCALL)
# define EV_USE_CLOCK_SYSCALL 0
# endif
# if HAVE_CLOCK_GETTIME
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# else
# ifndef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
# endif
# ifndef EV_USE_REALTIME
# define EV_USE_REALTIME 0
# endif
# endif
# ifndef EV_USE_NANOSLEEP
# if HAVE_NANOSLEEP
# define EV_USE_NANOSLEEP 1
# else
# define EV_USE_NANOSLEEP 0
# endif
# endif
# ifndef EV_USE_SELECT
# if HAVE_SELECT && HAVE_SYS_SELECT_H
# define EV_USE_SELECT 1
# else
# define EV_USE_SELECT 0
# endif
# endif
# ifndef EV_USE_POLL
# if HAVE_POLL && HAVE_POLL_H
# define EV_USE_POLL 1
# else
# define EV_USE_POLL 0
# endif
# endif
# ifndef EV_USE_EPOLL
# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
# define EV_USE_EPOLL 1
# else
# define EV_USE_EPOLL 0
# endif
# endif
# ifndef EV_USE_KQUEUE
# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
# define EV_USE_KQUEUE 1
# else
# define EV_USE_KQUEUE 0
# endif
# endif
# ifndef EV_USE_PORT
# if HAVE_PORT_H && HAVE_PORT_CREATE
# define EV_USE_PORT 1
# else
# define EV_USE_PORT 0
# endif
# endif
# ifndef EV_USE_INOTIFY
# if HAVE_INOTIFY_INIT && HAVE_SYS_INOTIFY_H
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
# endif
# ifndef EV_USE_EVENTFD
# if HAVE_EVENTFD
# define EV_USE_EVENTFD 1
# else
# define EV_USE_EVENTFD 0
# endif
# endif
#endif
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#ifdef EV_H
# include EV_H
#else
# include "ev.h"
#endif
#ifndef _WIN32
# include <sys/time.h>
# include <sys/wait.h>
# include <unistd.h>
#else
# include <io.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# ifndef EV_SELECT_IS_WINSOCKET
# define EV_SELECT_IS_WINSOCKET 1
# endif
#endif
/* this block tries to deduce configuration from header-defined symbols and defaults */
#ifndef EV_USE_CLOCK_SYSCALL
# if __linux && __GLIBC__ >= 2
# define EV_USE_CLOCK_SYSCALL 1
# else
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
#ifndef EV_USE_MONOTONIC
# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
# define EV_USE_MONOTONIC 1
# else
# define EV_USE_MONOTONIC 0
# endif
#endif
#ifndef EV_USE_REALTIME
# define EV_USE_REALTIME !EV_USE_CLOCK_SYSCALL
#endif
#ifndef EV_USE_NANOSLEEP
# if _POSIX_C_SOURCE >= 199309L
# define EV_USE_NANOSLEEP 1
# else
# define EV_USE_NANOSLEEP 0
# endif
#endif
#ifndef EV_USE_SELECT
# define EV_USE_SELECT 1
#endif
#ifndef EV_USE_POLL
# ifdef _WIN32
# define EV_USE_POLL 0
# else
# define EV_USE_POLL 1
# endif
#endif
#ifndef EV_USE_EPOLL
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_EPOLL 1
# else
# define EV_USE_EPOLL 0
# endif
#endif
#ifndef EV_USE_KQUEUE
# define EV_USE_KQUEUE 0
#endif
#ifndef EV_USE_PORT
# define EV_USE_PORT 0
#endif
#ifndef EV_USE_INOTIFY
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 4))
# define EV_USE_INOTIFY 1
# else
# define EV_USE_INOTIFY 0
# endif
#endif
#ifndef EV_PID_HASHSIZE
# if EV_MINIMAL
# define EV_PID_HASHSIZE 1
# else
# define EV_PID_HASHSIZE 16
# endif
#endif
#ifndef EV_INOTIFY_HASHSIZE
# if EV_MINIMAL
# define EV_INOTIFY_HASHSIZE 1
# else
# define EV_INOTIFY_HASHSIZE 16
# endif
#endif
#ifndef EV_USE_EVENTFD
# if __linux && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7))
# define EV_USE_EVENTFD 1
# else
# define EV_USE_EVENTFD 0
# endif
#endif
#if 0 /* debugging */
# define EV_VERIFY 3
# define EV_USE_4HEAP 1
# define EV_HEAP_CACHE_AT 1
#endif
#ifndef EV_VERIFY
# define EV_VERIFY !EV_MINIMAL
#endif
#ifndef EV_USE_4HEAP
# define EV_USE_4HEAP !EV_MINIMAL
#endif
#ifndef EV_HEAP_CACHE_AT
# define EV_HEAP_CACHE_AT !EV_MINIMAL
#endif
/* on linux, we can use a (slow) syscall to avoid a dependency on pthread, */
/* which makes programs even slower. might work on other unices, too. */
#if EV_USE_CLOCK_SYSCALL
# include <syscall.h>
# ifdef SYS_clock_gettime
# define clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 1
# else
# undef EV_USE_CLOCK_SYSCALL
# define EV_USE_CLOCK_SYSCALL 0
# endif
#endif
/* this block fixes any misconfiguration where we know we run into trouble otherwise */
#ifndef CLOCK_MONOTONIC
# undef EV_USE_MONOTONIC
# define EV_USE_MONOTONIC 0
#endif
#ifndef CLOCK_REALTIME
# undef EV_USE_REALTIME
# define EV_USE_REALTIME 0
#endif
#if !EV_STAT_ENABLE
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
#endif
#if !EV_USE_NANOSLEEP
# ifndef _WIN32
# include <sys/select.h>
# endif
#endif
#if EV_USE_INOTIFY
# include <sys/utsname.h>
# include <sys/statfs.h>
# include <sys/inotify.h>
/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
# ifndef IN_DONT_FOLLOW
# undef EV_USE_INOTIFY
# define EV_USE_INOTIFY 0
# endif
#endif
#if EV_SELECT_IS_WINSOCKET
# include <winsock.h>
#endif
#if EV_USE_EVENTFD
/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
# include <stdint.h>
# ifdef __cplusplus
extern "C" {
# endif
int eventfd (unsigned int initval, int flags);
# ifdef __cplusplus
}
# endif
#endif
/**/
#if EV_VERIFY >= 3
# define EV_FREQUENT_CHECK ev_loop_verif