+ return EV_VERSION_MAJOR;
+}
+
+int
+ev_version_minor (void)
+{
+ return EV_VERSION_MINOR;
+}
+
+/* return true if we are running with elevated privileges and should ignore env variables */
+static int
+enable_secure (void)
+{
+#ifdef _WIN32
+ return 0;
+#else
+ return getuid () != geteuid ()
+ || getgid () != getegid ();
+#endif
+}
+
+unsigned int
+ev_supported_backends (void)
+{
+}
+
+unsigned int
+ev_recommended_backends (void)
+{
+ unsigned int flags;
+
+ if (EV_USE_PORT ) flags |= EVBACKEND_PORT;
+ if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE;
+ if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL;
+ if (EV_USE_POLL ) flags |= EVBACKEND_POLL;
+ if (EV_USE_SELECT) flags |= EVBACKEND_SELECT;
+
+ return flags;
+}
+
+unsigned int
+ev_backend (EV_P)
+{
+ unsigned int flags = ev_recommended_backends ();
+
+#ifndef __NetBSD__
+ /* kqueue is borked on everything but netbsd apparently */
+ /* it usually doesn't work correctly on anything but sockets and pipes */
+ flags &= ~EVBACKEND_KQUEUE;
+#endif
+#ifdef __APPLE__
+ // flags &= ~EVBACKEND_KQUEUE; for documentation
+ flags &= ~EVBACKEND_POLL;
+#endif
+
+ return flags;
+}
+
+static void
+loop_init (EV_P_ unsigned int flags)
+{
+ if (!method)
+ {
+#if EV_USE_MONOTONIC
+ {
+ struct timespec ts;
+ if (!clock_gettime (CLOCK_MONOTONIC, &ts))
+ have_monotonic = 1;
+ }
+#endif
+
+ ev_rt_now = ev_time ();
+ mn_now = get_clock ();
+ now_floor = mn_now;
+ rtmn_diff = ev_rt_now - mn_now;
+
+ if (!(flags & EVFLAG_NOENV)
+ && !enable_secure ()
+ && getenv ("LIBEV_FLAGS"))
+ flags = atoi (getenv ("LIBEV_FLAGS"));
+
+ if (!(flags & 0x0000ffffUL))
+ flags |= ev_recommended_backends ();
+
+ method = 0;
+#if EV_USE_PORT
+ if (!method && (flags & EVBACKEND_PORT )) method = port_init (EV_A_ flags);
+#endif
+#if EV_USE_KQUEUE
+ if (!method && (flags & EVBACKEND_KQUEUE)) method = kqueue_init (EV_A_ flags);
+#endif
+#if EV_USE_EPOLL
+ if (!method && (flags & EVBACKEND_EPOLL )) method = epoll_init (EV_A_ flags);
+#endif
+#if EV_USE_POLL
+ if (!method && (flags & EVBACKEND_POLL )) method = poll_init (EV_A_ flags);
+#endif
+#if EV_USE_SELECT
+ if (!method && (flags & EVBACKEND_SELECT)) method = select_init (EV_A_ flags);
+#endif
+
+ ev_init (&sigev, sigcb);
+ ev_set_priority (&sigev, EV_MAXPRI);
+ }
+}
+
+static void
+loop_destroy (EV_P)
+{
+ int i;
+
+#if EV_USE_PORT
+ if (method == EVBACKEND_PORT ) port_destroy (EV_A);
+#endif
+#if EV_USE_KQUEUE
+ if (method == EVBACKEND_KQUEUE) kqueue_destroy (EV_A);
+#endif
+#if EV_USE_EPOLL
+ if (method == EVBACKEND_EPOLL ) epoll_destroy (EV_A);
+#endif
+#if EV_USE_POLL
+ if (method == EVBACKEND_POLL ) poll_destroy (EV_A);
+#endif
+#if EV_USE_SELECT
+ if (method == EVBACKEND_SELECT) select_destroy (EV_A);
+#endif
+
+ for (i = NUMPRI; i--; )
+ array_free (pending, [i]);
+
+ /* have to use the microsoft-never-gets-it-right macro */
+ array_free (fdchange, EMPTY0);
+ array_free (timer, EMPTY0);
+#if EV_PERIODICS
+ array_free (periodic, EMPTY0);
+#endif
+ array_free (idle, EMPTY0);
+ array_free (prepare, EMPTY0);
+ array_free (check, EMPTY0);
+
+ method = 0;
+}
+
+static void
+loop_fork (EV_P)
+{
+#if EV_USE_PORT
+ if (method == EVBACKEND_PORT ) port_fork (EV_A);
+#endif
+#if EV_USE_KQUEUE
+ if (method == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
+#endif
+#if EV_USE_EPOLL
+ if (method == EVBACKEND_EPOLL ) epoll_fork (EV_A);
+#endif
+
+ if (ev_is_active (&sigev))
+ {
+ /* default loop */
+
+ ev_ref (EV_A);
+ ev_io_stop (EV_A_ &sigev);
+ close (sigpipe [0]);
+ close (sigpipe [1]);
+
+ while (pipe (sigpipe))
+ syserr ("(libev) error creating pipe");
+
+ siginit (EV_A);
+ }
+
+ postfork = 0;
+}
+
+#if EV_MULTIPLICITY
+struct ev_loop *
+ev_loop_new (unsigned int flags)
+{
+ struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
+
+ memset (loop, 0, sizeof (struct ev_loop));
+
+ loop_init (EV_A_ flags);
+
+ if (ev_method (EV_A))
+ return loop;
+
+ return 0;
+}
+
+void
+ev_loop_destroy (EV_P)
+{
+ loop_destroy (EV_A);
+ ev_free (loop);
+}
+
+void
+ev_loop_fork (EV_P)
+{
+ postfork = 1;
+}
+
+#endif
+
+#if EV_MULTIPLICITY
+struct ev_loop *
+ev_default_loop_init (unsigned int flags)
+#else
+int
+ev_default_loop (unsigned int flags)
+#endif
+{
+ if (sigpipe [0] == sigpipe [1])
+ if (pipe (sigpipe))
+ return 0;
+
+ if (!ev_default_loop_ptr)
+ {
+#if EV_MULTIPLICITY
+ struct ev_loop *loop = ev_default_loop_ptr = &default_loop_struct;
+#else
+ ev_default_loop_ptr = 1;