mirror of
https://github.com/libevent/libevent.git
synced 2025-01-09 00:56:20 +08:00
68120d9b00
C reserves all identifiers beginning with an underscore for system use. But we had been mangling our autoconf identifiers with the prefix "_EVENT_" to avoid conflict with other programs. Instead, we will now use the prefix "EVENT__". With any luck, the double-underscore will still hint "here be dragons" to anybody tempted to think that event-config.h is a stable api. This is an automatically generated patch. The script that produced it was made by running this script over config.h.in: ===== #!/usr/bin/perl -w # Run this on config.h.in use strict; my %macros = (); while (<>) { if (/^# *undef +([A-Za-z0-9_]+)/) { $macros{$1} = 1; } } print "#!/usr/bin/perl -w -i -p\n\n"; for my $k (sort keys %macros) { print "s/(?<![A-Za-z0-9_])_EVENT_$k(?![A-Za-z0-9_])/EVENT__$k/g;\n"; } == And the script that it generated was then run over all .c and .h files: #!/usr/bin/perl -w -i -p s/(?<![A-Za-z0-9_])_EVENT_DISABLE_DEBUG_MODE(?![A-Za-z0-9_])/EVENT__DISABLE_DEBUG_MODE/g; s/(?<![A-Za-z0-9_])_EVENT_DISABLE_MM_REPLACEMENT(?![A-Za-z0-9_])/EVENT__DISABLE_MM_REPLACEMENT/g; s/(?<![A-Za-z0-9_])_EVENT_DISABLE_THREAD_SUPPORT(?![A-Za-z0-9_])/EVENT__DISABLE_THREAD_SUPPORT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ACCEPT4(?![A-Za-z0-9_])/EVENT__HAVE_ACCEPT4/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARC4RANDOM(?![A-Za-z0-9_])/EVENT__HAVE_ARC4RANDOM/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARC4RANDOM_BUF(?![A-Za-z0-9_])/EVENT__HAVE_ARC4RANDOM_BUF/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ARPA_INET_H(?![A-Za-z0-9_])/EVENT__HAVE_ARPA_INET_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_CLOCK_GETTIME(?![A-Za-z0-9_])/EVENT__HAVE_CLOCK_GETTIME/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_CTL_KERN(?![A-Za-z0-9_])/EVENT__HAVE_DECL_CTL_KERN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_KERN_ARND(?![A-Za-z0-9_])/EVENT__HAVE_DECL_KERN_ARND/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_KERN_RANDOM(?![A-Za-z0-9_])/EVENT__HAVE_DECL_KERN_RANDOM/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DECL_RANDOM_UUID(?![A-Za-z0-9_])/EVENT__HAVE_DECL_RANDOM_UUID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DEVPOLL(?![A-Za-z0-9_])/EVENT__HAVE_DEVPOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_DLFCN_H(?![A-Za-z0-9_])/EVENT__HAVE_DLFCN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL_CREATE1(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL_CREATE1/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EPOLL_CTL(?![A-Za-z0-9_])/EVENT__HAVE_EPOLL_CTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EVENTFD(?![A-Za-z0-9_])/EVENT__HAVE_EVENTFD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_EVENT_PORTS(?![A-Za-z0-9_])/EVENT__HAVE_EVENT_PORTS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FCNTL(?![A-Za-z0-9_])/EVENT__HAVE_FCNTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FCNTL_H(?![A-Za-z0-9_])/EVENT__HAVE_FCNTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_FD_MASK(?![A-Za-z0-9_])/EVENT__HAVE_FD_MASK/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETADDRINFO(?![A-Za-z0-9_])/EVENT__HAVE_GETADDRINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETEGID(?![A-Za-z0-9_])/EVENT__HAVE_GETEGID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETEUID(?![A-Za-z0-9_])/EVENT__HAVE_GETEUID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_3_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_3_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_5_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_5_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETHOSTBYNAME_R_6_ARG(?![A-Za-z0-9_])/EVENT__HAVE_GETHOSTBYNAME_R_6_ARG/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETIFADDRS(?![A-Za-z0-9_])/EVENT__HAVE_GETIFADDRS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETNAMEINFO(?![A-Za-z0-9_])/EVENT__HAVE_GETNAMEINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETPROTOBYNUMBER(?![A-Za-z0-9_])/EVENT__HAVE_GETPROTOBYNUMBER/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETSERVBYNAME(?![A-Za-z0-9_])/EVENT__HAVE_GETSERVBYNAME/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_GETTIMEOFDAY(?![A-Za-z0-9_])/EVENT__HAVE_GETTIMEOFDAY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_IFADDRS_H(?![A-Za-z0-9_])/EVENT__HAVE_IFADDRS_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_ATON(?![A-Za-z0-9_])/EVENT__HAVE_INET_ATON/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_NTOP(?![A-Za-z0-9_])/EVENT__HAVE_INET_NTOP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INET_PTON(?![A-Za-z0-9_])/EVENT__HAVE_INET_PTON/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_INTTYPES_H(?![A-Za-z0-9_])/EVENT__HAVE_INTTYPES_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ISSETUGID(?![A-Za-z0-9_])/EVENT__HAVE_ISSETUGID/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_KQUEUE(?![A-Za-z0-9_])/EVENT__HAVE_KQUEUE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_LIBZ(?![A-Za-z0-9_])/EVENT__HAVE_LIBZ/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_MEMORY_H(?![A-Za-z0-9_])/EVENT__HAVE_MEMORY_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_MMAP(?![A-Za-z0-9_])/EVENT__HAVE_MMAP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NANOSLEEP(?![A-Za-z0-9_])/EVENT__HAVE_NANOSLEEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETDB_H(?![A-Za-z0-9_])/EVENT__HAVE_NETDB_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_IN6_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_IN6_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_IN_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_IN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_NETINET_TCP_H(?![A-Za-z0-9_])/EVENT__HAVE_NETINET_TCP_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_OPENSSL(?![A-Za-z0-9_])/EVENT__HAVE_OPENSSL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PIPE(?![A-Za-z0-9_])/EVENT__HAVE_PIPE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PIPE2(?![A-Za-z0-9_])/EVENT__HAVE_PIPE2/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_POLL(?![A-Za-z0-9_])/EVENT__HAVE_POLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_POLL_H(?![A-Za-z0-9_])/EVENT__HAVE_POLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PORT_CREATE(?![A-Za-z0-9_])/EVENT__HAVE_PORT_CREATE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PORT_H(?![A-Za-z0-9_])/EVENT__HAVE_PORT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PTHREAD(?![A-Za-z0-9_])/EVENT__HAVE_PTHREAD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PTHREADS(?![A-Za-z0-9_])/EVENT__HAVE_PTHREADS/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_PUTENV(?![A-Za-z0-9_])/EVENT__HAVE_PUTENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SA_FAMILY_T(?![A-Za-z0-9_])/EVENT__HAVE_SA_FAMILY_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SELECT(?![A-Za-z0-9_])/EVENT__HAVE_SELECT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SENDFILE(?![A-Za-z0-9_])/EVENT__HAVE_SENDFILE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETENV(?![A-Za-z0-9_])/EVENT__HAVE_SETENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETFD(?![A-Za-z0-9_])/EVENT__HAVE_SETFD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SETRLIMIT(?![A-Za-z0-9_])/EVENT__HAVE_SETRLIMIT/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SIGACTION(?![A-Za-z0-9_])/EVENT__HAVE_SIGACTION/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SIGNAL(?![A-Za-z0-9_])/EVENT__HAVE_SIGNAL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SPLICE(?![A-Za-z0-9_])/EVENT__HAVE_SPLICE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDARG_H(?![A-Za-z0-9_])/EVENT__HAVE_STDARG_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDDEF_H(?![A-Za-z0-9_])/EVENT__HAVE_STDDEF_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDINT_H(?![A-Za-z0-9_])/EVENT__HAVE_STDINT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STDLIB_H(?![A-Za-z0-9_])/EVENT__HAVE_STDLIB_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRINGS_H(?![A-Za-z0-9_])/EVENT__HAVE_STRINGS_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRING_H(?![A-Za-z0-9_])/EVENT__HAVE_STRING_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRLCPY(?![A-Za-z0-9_])/EVENT__HAVE_STRLCPY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRSEP(?![A-Za-z0-9_])/EVENT__HAVE_STRSEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRTOK_R(?![A-Za-z0-9_])/EVENT__HAVE_STRTOK_R/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRTOLL(?![A-Za-z0-9_])/EVENT__HAVE_STRTOLL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_ADDRINFO(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_ADDRINFO/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR16(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR16/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_IN6_ADDR_S6_ADDR32(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_IN6_ADDR_S6_ADDR32/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN6(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN6/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY(?![A-Za-z0-9_])/EVENT__HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYSCTL(?![A-Za-z0-9_])/EVENT__HAVE_SYSCTL/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_DEVPOLL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_DEVPOLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EPOLL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EPOLL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EVENTFD_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EVENTFD_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_EVENT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_EVENT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_IOCTL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_IOCTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_MMAN_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_MMAN_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_PARAM_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_PARAM_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_QUEUE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_QUEUE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_RESOURCE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_RESOURCE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SELECT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SELECT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SENDFILE_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SENDFILE_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SOCKET_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SOCKET_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_STAT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_STAT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_SYSCTL_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_SYSCTL_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_TIME_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_TIME_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_TYPES_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_TYPES_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_UIO_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_UIO_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_SYS_WAIT_H(?![A-Za-z0-9_])/EVENT__HAVE_SYS_WAIT_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TAILQFOREACH(?![A-Za-z0-9_])/EVENT__HAVE_TAILQFOREACH/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERADD(?![A-Za-z0-9_])/EVENT__HAVE_TIMERADD/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERCLEAR(?![A-Za-z0-9_])/EVENT__HAVE_TIMERCLEAR/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERCMP(?![A-Za-z0-9_])/EVENT__HAVE_TIMERCMP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_TIMERISSET(?![A-Za-z0-9_])/EVENT__HAVE_TIMERISSET/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT16_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT16_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT32_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT32_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT64_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT64_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINT8_T(?![A-Za-z0-9_])/EVENT__HAVE_UINT8_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UINTPTR_T(?![A-Za-z0-9_])/EVENT__HAVE_UINTPTR_T/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UNISTD_H(?![A-Za-z0-9_])/EVENT__HAVE_UNISTD_H/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_UNSETENV(?![A-Za-z0-9_])/EVENT__HAVE_UNSETENV/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_USLEEP(?![A-Za-z0-9_])/EVENT__HAVE_USLEEP/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_VASPRINTF(?![A-Za-z0-9_])/EVENT__HAVE_VASPRINTF/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_WORKING_KQUEUE(?![A-Za-z0-9_])/EVENT__HAVE_WORKING_KQUEUE/g; s/(?<![A-Za-z0-9_])_EVENT_HAVE_ZLIB_H(?![A-Za-z0-9_])/EVENT__HAVE_ZLIB_H/g; s/(?<![A-Za-z0-9_])_EVENT_LT_OBJDIR(?![A-Za-z0-9_])/EVENT__LT_OBJDIR/g; s/(?<![A-Za-z0-9_])_EVENT_NO_MINUS_C_MINUS_O(?![A-Za-z0-9_])/EVENT__NO_MINUS_C_MINUS_O/g; s/(?<![A-Za-z0-9_])_EVENT_NUMERIC_VERSION(?![A-Za-z0-9_])/EVENT__NUMERIC_VERSION/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE(?![A-Za-z0-9_])/EVENT__PACKAGE/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_BUGREPORT(?![A-Za-z0-9_])/EVENT__PACKAGE_BUGREPORT/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_NAME(?![A-Za-z0-9_])/EVENT__PACKAGE_NAME/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_STRING(?![A-Za-z0-9_])/EVENT__PACKAGE_STRING/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_TARNAME(?![A-Za-z0-9_])/EVENT__PACKAGE_TARNAME/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_URL(?![A-Za-z0-9_])/EVENT__PACKAGE_URL/g; s/(?<![A-Za-z0-9_])_EVENT_PACKAGE_VERSION(?![A-Za-z0-9_])/EVENT__PACKAGE_VERSION/g; s/(?<![A-Za-z0-9_])_EVENT_PTHREAD_CREATE_JOINABLE(?![A-Za-z0-9_])/EVENT__PTHREAD_CREATE_JOINABLE/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_INT(?![A-Za-z0-9_])/EVENT__SIZEOF_INT/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_LONG(?![A-Za-z0-9_])/EVENT__SIZEOF_LONG/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_LONG_LONG(?![A-Za-z0-9_])/EVENT__SIZEOF_LONG_LONG/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_OFF_T(?![A-Za-z0-9_])/EVENT__SIZEOF_OFF_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_PTHREAD_T(?![A-Za-z0-9_])/EVENT__SIZEOF_PTHREAD_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_SHORT(?![A-Za-z0-9_])/EVENT__SIZEOF_SHORT/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_SIZE_T(?![A-Za-z0-9_])/EVENT__SIZEOF_SIZE_T/g; s/(?<![A-Za-z0-9_])_EVENT_SIZEOF_VOID_P(?![A-Za-z0-9_])/EVENT__SIZEOF_VOID_P/g; s/(?<![A-Za-z0-9_])_EVENT_STDC_HEADERS(?![A-Za-z0-9_])/EVENT__STDC_HEADERS/g; s/(?<![A-Za-z0-9_])_EVENT_TIME_WITH_SYS_TIME(?![A-Za-z0-9_])/EVENT__TIME_WITH_SYS_TIME/g; s/(?<![A-Za-z0-9_])_EVENT_VERSION(?![A-Za-z0-9_])/EVENT__VERSION/g; s/(?<![A-Za-z0-9_])_EVENT__ALL_SOURCE(?![A-Za-z0-9_])/EVENT___ALL_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__FILE_OFFSET_BITS(?![A-Za-z0-9_])/EVENT___FILE_OFFSET_BITS/g; s/(?<![A-Za-z0-9_])_EVENT__GNU_SOURCE(?![A-Za-z0-9_])/EVENT___GNU_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__LARGE_FILES(?![A-Za-z0-9_])/EVENT___LARGE_FILES/g; s/(?<![A-Za-z0-9_])_EVENT__MINIX(?![A-Za-z0-9_])/EVENT___MINIX/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_1_SOURCE(?![A-Za-z0-9_])/EVENT___POSIX_1_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_PTHREAD_SEMANTICS(?![A-Za-z0-9_])/EVENT___POSIX_PTHREAD_SEMANTICS/g; s/(?<![A-Za-z0-9_])_EVENT__POSIX_SOURCE(?![A-Za-z0-9_])/EVENT___POSIX_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT__TANDEM_SOURCE(?![A-Za-z0-9_])/EVENT___TANDEM_SOURCE/g; s/(?<![A-Za-z0-9_])_EVENT___EXTENSIONS__(?![A-Za-z0-9_])/EVENT____EXTENSIONS__/g; s/(?<![A-Za-z0-9_])_EVENT___func__(?![A-Za-z0-9_])/EVENT____func__/g; s/(?<![A-Za-z0-9_])_EVENT_const(?![A-Za-z0-9_])/EVENT__const/g; s/(?<![A-Za-z0-9_])_EVENT_inline(?![A-Za-z0-9_])/EVENT__inline/g; s/(?<![A-Za-z0-9_])_EVENT_pid_t(?![A-Za-z0-9_])/EVENT__pid_t/g; s/(?<![A-Za-z0-9_])_EVENT_size_t(?![A-Za-z0-9_])/EVENT__size_t/g; s/(?<![A-Za-z0-9_])_EVENT_socklen_t(?![A-Za-z0-9_])/EVENT__socklen_t/g; s/(?<![A-Za-z0-9_])_EVENT_ssize_t(?![A-Za-z0-9_])/EVENT__ssize_t/g;
594 lines
19 KiB
C
594 lines
19 KiB
C
/*
|
|
* Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
|
|
* Copyright 2007-2012 Niels Provos, Nick Mathewson
|
|
*
|
|
* 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.
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
* derived from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
|
*/
|
|
#include "event2/event-config.h"
|
|
#include "evconfig-private.h"
|
|
|
|
#ifdef EVENT__HAVE_EPOLL
|
|
|
|
#include <stdint.h>
|
|
#include <sys/types.h>
|
|
#include <sys/resource.h>
|
|
#ifdef EVENT__HAVE_SYS_TIME_H
|
|
#include <sys/time.h>
|
|
#endif
|
|
#include <sys/queue.h>
|
|
#include <sys/epoll.h>
|
|
#include <signal.h>
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#ifdef EVENT__HAVE_FCNTL_H
|
|
#include <fcntl.h>
|
|
#endif
|
|
|
|
#include "event-internal.h"
|
|
#include "evsignal-internal.h"
|
|
#include "event2/thread.h"
|
|
#include "evthread-internal.h"
|
|
#include "log-internal.h"
|
|
#include "evmap-internal.h"
|
|
#include "changelist-internal.h"
|
|
|
|
struct epollop {
|
|
struct epoll_event *events;
|
|
int nevents;
|
|
int epfd;
|
|
};
|
|
|
|
static void *epoll_init(struct event_base *);
|
|
static int epoll_dispatch(struct event_base *, struct timeval *);
|
|
static void epoll_dealloc(struct event_base *);
|
|
|
|
static const struct eventop epollops_changelist = {
|
|
"epoll (with changelist)",
|
|
epoll_init,
|
|
event_changelist_add,
|
|
event_changelist_del,
|
|
epoll_dispatch,
|
|
epoll_dealloc,
|
|
1, /* need reinit */
|
|
EV_FEATURE_ET|EV_FEATURE_O1,
|
|
EVENT_CHANGELIST_FDINFO_SIZE
|
|
};
|
|
|
|
|
|
static int epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
|
|
short old, short events, void *p);
|
|
static int epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
|
|
short old, short events, void *p);
|
|
|
|
const struct eventop epollops = {
|
|
"epoll",
|
|
epoll_init,
|
|
epoll_nochangelist_add,
|
|
epoll_nochangelist_del,
|
|
epoll_dispatch,
|
|
epoll_dealloc,
|
|
1, /* need reinit */
|
|
EV_FEATURE_ET|EV_FEATURE_O1,
|
|
0
|
|
};
|
|
|
|
#define INITIAL_NEVENT 32
|
|
#define MAX_NEVENT 4096
|
|
|
|
/* On Linux kernels at least up to 2.6.24.4, epoll can't handle timeout
|
|
* values bigger than (LONG_MAX - 999ULL)/HZ. HZ in the wild can be
|
|
* as big as 1000, and LONG_MAX can be as small as (1<<31)-1, so the
|
|
* largest number of msec we can support here is 2147482. Let's
|
|
* round that down by 47 seconds.
|
|
*/
|
|
#define MAX_EPOLL_TIMEOUT_MSEC (35*60*1000)
|
|
|
|
static void *
|
|
epoll_init(struct event_base *base)
|
|
{
|
|
int epfd = -1;
|
|
struct epollop *epollop;
|
|
|
|
#ifdef EVENT__HAVE_EPOLL_CREATE1
|
|
/* First, try the shiny new epoll_create1 interface, if we have it. */
|
|
epfd = epoll_create1(EPOLL_CLOEXEC);
|
|
#endif
|
|
if (epfd == -1) {
|
|
/* Initialize the kernel queue using the old interface. (The
|
|
size field is ignored since 2.6.8.) */
|
|
if ((epfd = epoll_create(32000)) == -1) {
|
|
if (errno != ENOSYS)
|
|
event_warn("epoll_create");
|
|
return (NULL);
|
|
}
|
|
evutil_make_socket_closeonexec(epfd);
|
|
}
|
|
|
|
if (!(epollop = mm_calloc(1, sizeof(struct epollop)))) {
|
|
close(epfd);
|
|
return (NULL);
|
|
}
|
|
|
|
epollop->epfd = epfd;
|
|
|
|
/* Initialize fields */
|
|
epollop->events = mm_calloc(INITIAL_NEVENT, sizeof(struct epoll_event));
|
|
if (epollop->events == NULL) {
|
|
mm_free(epollop);
|
|
close(epfd);
|
|
return (NULL);
|
|
}
|
|
epollop->nevents = INITIAL_NEVENT;
|
|
|
|
if ((base->flags & EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST) != 0 ||
|
|
((base->flags & EVENT_BASE_FLAG_IGNORE_ENV) == 0 &&
|
|
evutil_getenv("EVENT_EPOLL_USE_CHANGELIST") != NULL))
|
|
base->evsel = &epollops_changelist;
|
|
|
|
evsig_init(base);
|
|
|
|
return (epollop);
|
|
}
|
|
|
|
static const char *
|
|
change_to_string(int change)
|
|
{
|
|
change &= (EV_CHANGE_ADD|EV_CHANGE_DEL);
|
|
if (change == EV_CHANGE_ADD) {
|
|
return "add";
|
|
} else if (change == EV_CHANGE_DEL) {
|
|
return "del";
|
|
} else if (change == 0) {
|
|
return "none";
|
|
} else {
|
|
return "???";
|
|
}
|
|
}
|
|
|
|
static const char *
|
|
epoll_op_to_string(int op)
|
|
{
|
|
return op == EPOLL_CTL_ADD?"ADD":
|
|
op == EPOLL_CTL_DEL?"DEL":
|
|
op == EPOLL_CTL_MOD?"MOD":
|
|
"???";
|
|
}
|
|
|
|
/*
|
|
Here are the values we're masking off to decide what operations to do.
|
|
Note that since EV_READ|EV_WRITE.
|
|
|
|
Note also that this table is a little sparse, since ADD+DEL is
|
|
nonsensical ("xxx" in the list below.)
|
|
|
|
Note also also that we are shifting old_events by only 3 bits, since
|
|
EV_READ is 2 and EV_WRITE is 4.
|
|
|
|
The table was auto-generated with a python script, according to this
|
|
pseudocode:
|
|
|
|
If either the read or the write change is add+del:
|
|
This is impossible; Set op==-1, events=0.
|
|
Else, if either the read or the write change is add:
|
|
Set events to 0.
|
|
If the read change is add, or
|
|
(the read change is not del, and ev_read is in old_events):
|
|
Add EPOLLIN to events.
|
|
If the write change is add, or
|
|
(the write change is not del, and ev_write is in old_events):
|
|
Add EPOLLOUT to events.
|
|
|
|
If old_events is set:
|
|
Set op to EPOLL_CTL_MOD [*1,*2]
|
|
Else:
|
|
Set op to EPOLL_CTL_ADD [*3]
|
|
|
|
Else, if the read or the write change is del:
|
|
Set op to EPOLL_CTL_DEL.
|
|
If the read change is del:
|
|
If the write change is del:
|
|
Set events to EPOLLIN|EPOLLOUT
|
|
Else if ev_write is in old_events:
|
|
Set events to EPOLLOUT
|
|
Set op to EPOLL_CTL_MOD
|
|
Else
|
|
Set events to EPOLLIN
|
|
Else:
|
|
{The write change is del.}
|
|
If ev_read is in old_events:
|
|
Set events to EPOLLIN
|
|
Set op to EPOLL_CTL_MOD
|
|
Else:
|
|
Set the events to EPOLLOUT
|
|
|
|
Else:
|
|
There is no read or write change; set op to 0 and events to 0.
|
|
|
|
The logic is a little tricky, since we had no events set on the fd before,
|
|
we need to set op="ADD" and set events=the events we want to add. If we
|
|
had any events set on the fd before, and we want any events to remain on
|
|
the fd, we need to say op="MOD" and set events=the events we want to
|
|
remain. But if we want to delete the last event, we say op="DEL" and
|
|
set events=(any non-null pointer).
|
|
|
|
[*1] This MOD is only a guess. MOD might fail with ENOENT if the file was
|
|
closed and a new file was opened with the same fd. If so, we'll retry
|
|
with ADD.
|
|
|
|
[*2] We can't replace this with a no-op even if old_events is the same as
|
|
the new events: if the file was closed and reopened, we need to retry
|
|
with an ADD. (We do a MOD in this case since "no change" is more
|
|
common than "close and reopen", so we'll usually wind up doing 1
|
|
syscalls instead of 2.)
|
|
|
|
[*3] This ADD is only a guess. There is a fun Linux kernel issue where if
|
|
you have two fds for the same file (via dup) and you ADD one to an
|
|
epfd, then close it, then re-create it with the same fd (via dup2 or an
|
|
unlucky dup), then try to ADD it again, you'll get an EEXIST, since the
|
|
struct epitem is not actually removed from the struct eventpoll until
|
|
the file itself is closed.
|
|
|
|
EV_CHANGE_ADD==1
|
|
EV_CHANGE_DEL==2
|
|
EV_READ ==2
|
|
EV_WRITE ==4
|
|
Bit 0: read change is add
|
|
Bit 1: read change is del
|
|
Bit 2: write change is add
|
|
Bit 3: write change is del
|
|
Bit 4: old events had EV_READ
|
|
Bit 5: old events had EV_WRITE
|
|
*/
|
|
|
|
#define INDEX(c) \
|
|
( (((c)->read_change&(EV_CHANGE_ADD|EV_CHANGE_DEL))) | \
|
|
(((c)->write_change&(EV_CHANGE_ADD|EV_CHANGE_DEL)) << 2) | \
|
|
(((c)->old_events&(EV_READ|EV_WRITE)) << 3) )
|
|
|
|
#if EV_READ != 2 || EV_WRITE != 4 || EV_CHANGE_ADD != 1 || EV_CHANGE_DEL != 2
|
|
#error "Libevent's internals changed! Regenerate the op_table in epoll.c"
|
|
#endif
|
|
|
|
static const struct operation {
|
|
int events;
|
|
int op;
|
|
} op_table[] = {
|
|
{ 0, 0 }, /* old= 0, write: 0, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_ADD }, /* old= 0, write: 0, read:add */
|
|
{ EPOLLIN, EPOLL_CTL_DEL }, /* old= 0, write: 0, read:del */
|
|
{ 0, -1 }, /* old= 0, write: 0, read:xxx */
|
|
{ EPOLLOUT, EPOLL_CTL_ADD }, /* old= 0, write:add, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_ADD },/* old= 0, write:add, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_ADD }, /* old= 0, write:add, read:del */
|
|
{ 0, -1 }, /* old= 0, write:add, read:xxx */
|
|
{ EPOLLOUT, EPOLL_CTL_DEL }, /* old= 0, write:del, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_ADD }, /* old= 0, write:del, read:add */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },/* old= 0, write:del, read:del */
|
|
{ 0, -1 }, /* old= 0, write:del, read:xxx */
|
|
{ 0, -1 }, /* old= 0, write:xxx, read: 0 */
|
|
{ 0, -1 }, /* old= 0, write:xxx, read:add */
|
|
{ 0, -1 }, /* old= 0, write:xxx, read:del */
|
|
{ 0, -1 }, /* old= 0, write:xxx, read:xxx */
|
|
{ 0, 0 }, /* old= r, write: 0, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old= r, write: 0, read:add */
|
|
{ EPOLLIN, EPOLL_CTL_DEL }, /* old= r, write: 0, read:del */
|
|
{ 0, -1 }, /* old= r, write: 0, read:xxx */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old= r, write:add, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old= r, write:add, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old= r, write:add, read:del */
|
|
{ 0, -1 }, /* old= r, write:add, read:xxx */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old= r, write:del, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old= r, write:del, read:add */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },/* old= r, write:del, read:del */
|
|
{ 0, -1 }, /* old= r, write:del, read:xxx */
|
|
{ 0, -1 }, /* old= r, write:xxx, read: 0 */
|
|
{ 0, -1 }, /* old= r, write:xxx, read:add */
|
|
{ 0, -1 }, /* old= r, write:xxx, read:del */
|
|
{ 0, -1 }, /* old= r, write:xxx, read:xxx */
|
|
{ 0, 0 }, /* old= w, write: 0, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old= w, write: 0, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old= w, write: 0, read:del */
|
|
{ 0, -1 }, /* old= w, write: 0, read:xxx */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old= w, write:add, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old= w, write:add, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old= w, write:add, read:del */
|
|
{ 0, -1 }, /* old= w, write:add, read:xxx */
|
|
{ EPOLLOUT, EPOLL_CTL_DEL }, /* old= w, write:del, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old= w, write:del, read:add */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },/* old= w, write:del, read:del */
|
|
{ 0, -1 }, /* old= w, write:del, read:xxx */
|
|
{ 0, -1 }, /* old= w, write:xxx, read: 0 */
|
|
{ 0, -1 }, /* old= w, write:xxx, read:add */
|
|
{ 0, -1 }, /* old= w, write:xxx, read:del */
|
|
{ 0, -1 }, /* old= w, write:xxx, read:xxx */
|
|
{ 0, 0 }, /* old=rw, write: 0, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old=rw, write: 0, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old=rw, write: 0, read:del */
|
|
{ 0, -1 }, /* old=rw, write: 0, read:xxx */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old=rw, write:add, read: 0 */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_MOD },/* old=rw, write:add, read:add */
|
|
{ EPOLLOUT, EPOLL_CTL_MOD }, /* old=rw, write:add, read:del */
|
|
{ 0, -1 }, /* old=rw, write:add, read:xxx */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old=rw, write:del, read: 0 */
|
|
{ EPOLLIN, EPOLL_CTL_MOD }, /* old=rw, write:del, read:add */
|
|
{ EPOLLIN|EPOLLOUT, EPOLL_CTL_DEL },/* old=rw, write:del, read:del */
|
|
{ 0, -1 }, /* old=rw, write:del, read:xxx */
|
|
{ 0, -1 }, /* old=rw, write:xxx, read: 0 */
|
|
{ 0, -1 }, /* old=rw, write:xxx, read:add */
|
|
{ 0, -1 }, /* old=rw, write:xxx, read:del */
|
|
{ 0, -1 }, /* old=rw, write:xxx, read:xxx */
|
|
};
|
|
|
|
static int
|
|
epoll_apply_one_change(struct event_base *base,
|
|
struct epollop *epollop,
|
|
const struct event_change *ch)
|
|
{
|
|
struct epoll_event epev;
|
|
int op, events = 0;
|
|
int idx;
|
|
|
|
idx = INDEX(ch);
|
|
op = op_table[idx].op;
|
|
events = op_table[idx].events;
|
|
|
|
if (!events) {
|
|
EVUTIL_ASSERT(op == 0);
|
|
return 0;
|
|
}
|
|
|
|
if ((ch->read_change|ch->write_change) & EV_CHANGE_ET)
|
|
events |= EPOLLET;
|
|
|
|
memset(&epev, 0, sizeof(epev));
|
|
epev.data.fd = ch->fd;
|
|
epev.events = events;
|
|
if (epoll_ctl(epollop->epfd, op, ch->fd, &epev) == 0) {
|
|
event_debug(("Epoll %s(%d) on fd %d okay. [old events were %d; read change was %d; write change was %d]",
|
|
epoll_op_to_string(op),
|
|
(int)epev.events,
|
|
(int)ch->fd,
|
|
ch->old_events,
|
|
ch->read_change,
|
|
ch->write_change));
|
|
return 0;
|
|
}
|
|
|
|
switch (op) {
|
|
case EPOLL_CTL_MOD:
|
|
if (errno == ENOENT) {
|
|
/* If a MOD operation fails with ENOENT, the
|
|
* fd was probably closed and re-opened. We
|
|
* should retry the operation as an ADD.
|
|
*/
|
|
if (epoll_ctl(epollop->epfd, EPOLL_CTL_ADD, ch->fd, &epev) == -1) {
|
|
event_warn("Epoll MOD(%d) on %d retried as ADD; that failed too",
|
|
(int)epev.events, ch->fd);
|
|
return -1;
|
|
} else {
|
|
event_debug(("Epoll MOD(%d) on %d retried as ADD; succeeded.",
|
|
(int)epev.events,
|
|
ch->fd));
|
|
return 0;
|
|
}
|
|
}
|
|
break;
|
|
case EPOLL_CTL_ADD:
|
|
if (errno == EEXIST) {
|
|
/* If an ADD operation fails with EEXIST,
|
|
* either the operation was redundant (as with a
|
|
* precautionary add), or we ran into a fun
|
|
* kernel bug where using dup*() to duplicate the
|
|
* same file into the same fd gives you the same epitem
|
|
* rather than a fresh one. For the second case,
|
|
* we must retry with MOD. */
|
|
if (epoll_ctl(epollop->epfd, EPOLL_CTL_MOD, ch->fd, &epev) == -1) {
|
|
event_warn("Epoll ADD(%d) on %d retried as MOD; that failed too",
|
|
(int)epev.events, ch->fd);
|
|
return -1;
|
|
} else {
|
|
event_debug(("Epoll ADD(%d) on %d retried as MOD; succeeded.",
|
|
(int)epev.events,
|
|
ch->fd));
|
|
return 0;
|
|
}
|
|
}
|
|
break;
|
|
case EPOLL_CTL_DEL:
|
|
if (errno == ENOENT || errno == EBADF || errno == EPERM) {
|
|
/* If a delete fails with one of these errors,
|
|
* that's fine too: we closed the fd before we
|
|
* got around to calling epoll_dispatch. */
|
|
event_debug(("Epoll DEL(%d) on fd %d gave %s: DEL was unnecessary.",
|
|
(int)epev.events,
|
|
ch->fd,
|
|
strerror(errno)));
|
|
return 0;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
event_warn("Epoll %s(%d) on fd %d failed. Old events were %d; read change was %d (%s); write change was %d (%s)",
|
|
epoll_op_to_string(op),
|
|
(int)epev.events,
|
|
ch->fd,
|
|
ch->old_events,
|
|
ch->read_change,
|
|
change_to_string(ch->read_change),
|
|
ch->write_change,
|
|
change_to_string(ch->write_change));
|
|
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
epoll_apply_changes(struct event_base *base)
|
|
{
|
|
struct event_changelist *changelist = &base->changelist;
|
|
struct epollop *epollop = base->evbase;
|
|
struct event_change *ch;
|
|
|
|
int r = 0;
|
|
int i;
|
|
|
|
for (i = 0; i < changelist->n_changes; ++i) {
|
|
ch = &changelist->changes[i];
|
|
if (epoll_apply_one_change(base, epollop, ch) < 0)
|
|
r = -1;
|
|
}
|
|
|
|
return (r);
|
|
}
|
|
|
|
static int
|
|
epoll_nochangelist_add(struct event_base *base, evutil_socket_t fd,
|
|
short old, short events, void *p)
|
|
{
|
|
struct event_change ch;
|
|
ch.fd = fd;
|
|
ch.old_events = old;
|
|
ch.read_change = ch.write_change = 0;
|
|
if (events & EV_WRITE)
|
|
ch.write_change = EV_CHANGE_ADD |
|
|
(events & EV_ET);
|
|
if (events & EV_READ)
|
|
ch.read_change = EV_CHANGE_ADD |
|
|
(events & EV_ET);
|
|
|
|
return epoll_apply_one_change(base, base->evbase, &ch);
|
|
}
|
|
|
|
static int
|
|
epoll_nochangelist_del(struct event_base *base, evutil_socket_t fd,
|
|
short old, short events, void *p)
|
|
{
|
|
struct event_change ch;
|
|
ch.fd = fd;
|
|
ch.old_events = old;
|
|
ch.read_change = ch.write_change = 0;
|
|
if (events & EV_WRITE)
|
|
ch.write_change = EV_CHANGE_DEL;
|
|
if (events & EV_READ)
|
|
ch.read_change = EV_CHANGE_DEL;
|
|
|
|
return epoll_apply_one_change(base, base->evbase, &ch);
|
|
}
|
|
|
|
static int
|
|
epoll_dispatch(struct event_base *base, struct timeval *tv)
|
|
{
|
|
struct epollop *epollop = base->evbase;
|
|
struct epoll_event *events = epollop->events;
|
|
int i, res;
|
|
long timeout = -1;
|
|
|
|
if (tv != NULL) {
|
|
timeout = evutil_tv_to_msec(tv);
|
|
if (timeout < 0 || timeout > MAX_EPOLL_TIMEOUT_MSEC) {
|
|
/* Linux kernels can wait forever if the timeout is
|
|
* too big; see comment on MAX_EPOLL_TIMEOUT_MSEC. */
|
|
timeout = MAX_EPOLL_TIMEOUT_MSEC;
|
|
}
|
|
}
|
|
|
|
epoll_apply_changes(base);
|
|
event_changelist_remove_all(&base->changelist, base);
|
|
|
|
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
|
|
|
res = epoll_wait(epollop->epfd, events, epollop->nevents, timeout);
|
|
|
|
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
|
|
|
if (res == -1) {
|
|
if (errno != EINTR) {
|
|
event_warn("epoll_wait");
|
|
return (-1);
|
|
}
|
|
|
|
return (0);
|
|
}
|
|
|
|
event_debug(("%s: epoll_wait reports %d", __func__, res));
|
|
EVUTIL_ASSERT(res <= epollop->nevents);
|
|
|
|
for (i = 0; i < res; i++) {
|
|
int what = events[i].events;
|
|
short ev = 0;
|
|
|
|
if (what & (EPOLLHUP|EPOLLERR)) {
|
|
ev = EV_READ | EV_WRITE;
|
|
} else {
|
|
if (what & EPOLLIN)
|
|
ev |= EV_READ;
|
|
if (what & EPOLLOUT)
|
|
ev |= EV_WRITE;
|
|
}
|
|
|
|
if (!ev)
|
|
continue;
|
|
|
|
evmap_io_active(base, events[i].data.fd, ev | EV_ET);
|
|
}
|
|
|
|
if (res == epollop->nevents && epollop->nevents < MAX_NEVENT) {
|
|
/* We used all of the event space this time. We should
|
|
be ready for more events next time. */
|
|
int new_nevents = epollop->nevents * 2;
|
|
struct epoll_event *new_events;
|
|
|
|
new_events = mm_realloc(epollop->events,
|
|
new_nevents * sizeof(struct epoll_event));
|
|
if (new_events) {
|
|
epollop->events = new_events;
|
|
epollop->nevents = new_nevents;
|
|
}
|
|
}
|
|
|
|
return (0);
|
|
}
|
|
|
|
|
|
static void
|
|
epoll_dealloc(struct event_base *base)
|
|
{
|
|
struct epollop *epollop = base->evbase;
|
|
|
|
evsig_dealloc(base);
|
|
if (epollop->events)
|
|
mm_free(epollop->events);
|
|
if (epollop->epfd >= 0)
|
|
close(epollop->epfd);
|
|
|
|
memset(epollop, 0, sizeof(struct epollop));
|
|
mm_free(epollop);
|
|
}
|
|
|
|
#endif /* EVENT__HAVE_EPOLL */
|