Path: blob/main/crypto/krb5/src/util/verto/verto-k5ev.c
34907 views
/*1* Copyright 2011 Red Hat, Inc.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation files5* (the "Software"), to deal in the Software without restriction,6* including without limitation the rights to use, copy, modify, merge,7* publish, distribute, sublicense, and/or sell copies of the Software,8* and to permit persons to whom the Software is furnished to do so,9* subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324/*25* An edited version of verto-libev.c, using an embedded libev with renamed26* symbols. The corresponding version of verto-libev.c is stored in this27* directory for reference, although it is not built here.28*/2930#include "autoconf.h"31#include <stdlib.h>32#include <string.h>33#include <errno.h>3435#include <verto.h>36#include <verto-module.h>37#include "rename.h"3839/* Ignore some warnings generated by the libev code, which the libev maintainer40* isn't interested in avoiding. */41#ifdef __GNUC__42#pragma GCC diagnostic ignored "-Wunused-value"43#pragma GCC diagnostic ignored "-Wcomment"44#pragma GCC diagnostic ignored "-Wunused-result"45#ifdef __clang__46#pragma GCC diagnostic ignored "-Wbitwise-op-parentheses"47#endif48#endif4950#define EV_API_STATIC 151#define EV_STANDALONE 152/* Avoid using clock_gettime, which would create a dependency on librt. */53#define EV_USE_MONOTONIC 054#define EV_USE_REALTIME 055#define EV_FEATURES 0x4f /* No back ends or optional watchers */56/* Enable the optional watcher types we use. */57#define EV_IDLE_ENABLE 158#define EV_SIGNAL_ENABLE 159#define EV_CHILD_ENABLE 160/* Enable the back ends we want. */61#ifdef HAVE_POLL_H62#define EV_USE_POLL 163#endif64/* ev.c explicitly disables poll() on Mac or FreeBSD; fall back to select(). */65#define EV_USE_SELECT 166#include "ev.c"6768static verto_mod_ctx *69k5ev_ctx_new(void)70{71return ev_loop_new(EVFLAG_AUTO);72}7374static verto_mod_ctx *75k5ev_ctx_default(void)76{77return ev_default_loop(EVFLAG_AUTO);78}7980static void81k5ev_ctx_free(verto_mod_ctx *ctx)82{83if (ctx != EV_DEFAULT)84ev_loop_destroy(ctx);85}8687static void88k5ev_ctx_run(verto_mod_ctx *ctx)89{90ev_run(ctx, 0);91}9293static void94k5ev_ctx_run_once(verto_mod_ctx *ctx)95{96ev_run(ctx, EVRUN_ONCE);97}9899static void100k5ev_ctx_break(verto_mod_ctx *ctx)101{102ev_break(ctx, EVBREAK_ONE);103}104105static void106k5ev_ctx_reinitialize(verto_mod_ctx *ctx)107{108ev_loop_fork(ctx);109}110111static void112libev_callback(EV_P_ ev_watcher *w, int revents)113{114verto_ev_flag state = VERTO_EV_FLAG_NONE;115116#if EV_MULTIPLICITY117/* Match the check in ev.h, which doesn't mark this unused */118(void) EV_A;119#endif120121if (verto_get_type(w->data)== VERTO_EV_TYPE_CHILD)122verto_set_proc_status(w->data, ((ev_child*) w)->rstatus);123124if (revents & EV_READ)125state |= VERTO_EV_FLAG_IO_READ;126if (revents & EV_WRITE)127state |= VERTO_EV_FLAG_IO_WRITE;128if (revents & EV_ERROR)129state |= VERTO_EV_FLAG_IO_ERROR;130131verto_set_fd_state(w->data, state);132verto_fire(w->data);133}134135static void136k5ev_ctx_set_flags(verto_mod_ctx *ctx, const verto_ev *ev,137verto_mod_ev *evpriv)138{139if (verto_get_type(ev) == VERTO_EV_TYPE_IO) {140int events = EV_NONE;141142if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_READ)143events |= EV_READ;144if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_WRITE)145events |= EV_WRITE;146147ev_io_stop(ctx, (ev_io*) evpriv);148ev_io_set(((ev_io*) evpriv), verto_get_fd(ev), events);149ev_io_start(ctx, (ev_io*) evpriv);150}151}152153#define setuptype(type, ...) \154w.type = malloc(sizeof(ev_ ## type)); \155if (w.type) { \156ev_ ## type ## _init(w.type, (EV_CB(type, (*))) __VA_ARGS__); \157ev_ ## type ## _start(ctx, w.type); \158} \159break160161static verto_mod_ev *162k5ev_ctx_add(verto_mod_ctx *ctx, const verto_ev *ev, verto_ev_flag *flags)163{164union {165ev_watcher *watcher;166ev_io *io;167ev_timer *timer;168ev_idle *idle;169ev_signal *signal;170ev_child *child;171} w;172ev_tstamp interval;173174w.watcher = NULL;175*flags |= VERTO_EV_FLAG_PERSIST;176switch (verto_get_type(ev)) {177case VERTO_EV_TYPE_IO:178setuptype(io, libev_callback, verto_get_fd(ev), EV_NONE);179case VERTO_EV_TYPE_TIMEOUT:180interval = ((ev_tstamp) verto_get_interval(ev)) / 1000.0;181setuptype(timer, libev_callback, interval, interval);182case VERTO_EV_TYPE_IDLE:183setuptype(idle, libev_callback);184case VERTO_EV_TYPE_SIGNAL:185setuptype(signal, libev_callback, verto_get_signal(ev));186case VERTO_EV_TYPE_CHILD:187*flags &= ~VERTO_EV_FLAG_PERSIST; /* Child events don't persist */188setuptype(child, libev_callback, verto_get_proc(ev), 0);189default:190break; /* Not supported */191}192193if (w.watcher) {194w.watcher->data = (void*) ev;195k5ev_ctx_set_flags(ctx, ev, w.watcher);196}197return w.watcher;198}199200static void201k5ev_ctx_del(verto_mod_ctx *ctx, const verto_ev *ev, verto_mod_ev *evpriv)202{203switch (verto_get_type(ev)) {204case VERTO_EV_TYPE_IO:205ev_io_stop(ctx, (ev_io*) evpriv);206break;207case VERTO_EV_TYPE_TIMEOUT:208ev_timer_stop(ctx, (ev_timer*) evpriv);209break;210case VERTO_EV_TYPE_IDLE:211ev_idle_stop(ctx, (ev_idle*) evpriv);212break;213case VERTO_EV_TYPE_SIGNAL:214ev_signal_stop(ctx, (ev_signal*) evpriv);215break;216case VERTO_EV_TYPE_CHILD:217ev_child_stop(ctx, (ev_child*) evpriv);218break;219default:220break;221}222223free(evpriv);224}225226verto_ctx *verto_new_k5ev(void);227verto_ctx *verto_default_k5ev(void);228229VERTO_MODULE(k5ev, NULL,230VERTO_EV_TYPE_IO |231VERTO_EV_TYPE_TIMEOUT |232VERTO_EV_TYPE_IDLE |233VERTO_EV_TYPE_SIGNAL |234VERTO_EV_TYPE_CHILD);235236237