예제 #1
0
static void
event_enable( tr_peerIo * io, short event )
{
    assert( tr_amInEventThread( io->session ) );
    assert( io->session != NULL );
    assert( io->session->events != NULL );
    assert( event_initialized( &io->event_read ) );
    assert( event_initialized( &io->event_write ) );

    if( io->socket < 0 )
        return;

    if( ( event & EV_READ ) && ! ( io->pendingEvents & EV_READ ) )
    {
        dbgmsg( io, "enabling libevent ready-to-read polling" );
        event_add( &io->event_read, NULL );
        io->pendingEvents |= EV_READ;
    }

    if( ( event & EV_WRITE ) && ! ( io->pendingEvents & EV_WRITE ) )
    {
        dbgmsg( io, "enabling libevent ready-to-write polling" );
        event_add( &io->event_write, NULL );
        io->pendingEvents |= EV_WRITE;
    }
}
예제 #2
0
static void php_http_client_curl_event_timer(CURLM *multi, long timeout_ms, void *timer_data)
{
	php_http_client_curl_event_context_t *context = timer_data;
	struct timeval timeout;

#if DBG_EVENTS
	fprintf(stderr, "(%ld)", timeout_ms);
#endif

	switch (timeout_ms) {
	case -1:
		if (event_initialized(context->timeout) && event_pending(context->timeout, EV_TIMEOUT, NULL)) {
			event_del(context->timeout);
		}
		break;
	case 0:
		php_http_client_curl_event_handler(context, CURL_SOCKET_TIMEOUT, 0);
		break;
	default:
		if (!event_initialized(context->timeout)) {
			event_assign(context->timeout, context->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_client_curl_event_timeout_callback, context);
		}

		timeout.tv_sec = timeout_ms / 1000;
		timeout.tv_usec = (timeout_ms % 1000) * 1000;

		if (!event_pending(context->timeout, EV_TIMEOUT, &timeout)) {
			event_add(context->timeout, &timeout);
		}
		break;
	}
}
예제 #3
0
파일: names.c 프로젝트: mosconi/openbsd
void
check_window_name(struct window *w)
{
	struct timeval	 tv, next;
	char		*name;
	int		 left;

	if (w->active == NULL)
		return;

	if (!options_get_number(w->options, "automatic-rename"))
		return;

	if (~w->active->flags & PANE_CHANGED) {
		log_debug("@%u active pane not changed", w->id);
		return;
	}
	log_debug("@%u active pane changed", w->id);

	gettimeofday(&tv, NULL);
	left = name_time_expired(w, &tv);
	if (left != 0) {
		if (!event_initialized(&w->name_event))
			evtimer_set(&w->name_event, name_time_callback, w);
		if (!evtimer_pending(&w->name_event, NULL)) {
			log_debug("@%u name timer queued (%d left)", w->id,
			    left);
			timerclear(&next);
			next.tv_usec = left;
			event_add(&w->name_event, &next);
		} else {
			log_debug("@%u name timer already queued (%d left)",
			    w->id, left);
		}
		return;
	}
	memcpy(&w->name_time, &tv, sizeof w->name_time);
	if (event_initialized(&w->name_event))
		evtimer_del(&w->name_event);

	w->active->flags &= ~PANE_CHANGED;

	name = format_window_name(w);
	if (strcmp(name, w->name) != 0) {
		log_debug("@%u new name %s (was %s)", w->id, name, w->name);
		window_set_name(w, name);
		server_status_window(w);
	} else
		log_debug("@%u name not changed (still %s)", w->id, w->name);

	free(name);
}
예제 #4
0
파일: tty.c 프로젝트: UNGLinux/Obase
void
tty_close(struct tty *tty)
{
	if (tty->log_fd != -1) {
		close(tty->log_fd);
		tty->log_fd = -1;
	}

	if (event_initialized(&tty->key_timer))
		evtimer_del(&tty->key_timer);
	tty_stop_tty(tty);

	if (tty->flags & TTY_OPENED) {
		bufferevent_free(tty->event);

		tty_term_free(tty->term);
		tty_keys_free(tty);

		tty->flags &= ~TTY_OPENED;
	}

	if (tty->fd != -1) {
		close(tty->fd);
		tty->fd = -1;
	}
}
예제 #5
0
파일: ioev.c 프로젝트: Vaelatern/OpenSMTPD
/* Set the requested event. */
void
io_reset(struct io *io, short events, void (*dispatch)(int, short, void*))
{
	struct timeval	tv, *ptv;

	io_debug("io_reset(%p, %s, %p) -> %s\n",
	    io, io_evstr(events), dispatch, io_strio(io));

	/*
	 * Indicate that the event has already been reset so that reload
	 * is not called on frame_leave.
	 */
	io->flags |= IO_RESET;

	if (event_initialized(&io->ev))
		event_del(&io->ev);

	/*
	 * The io is paused by the user, so we don't want the timeout to be
	 * effective.
	 */
	if (events == 0)
		return;

	event_set(&io->ev, io->sock, events, dispatch, io);
	if (io->timeout >= 0) {
		tv.tv_sec = io->timeout / 1000;
		tv.tv_usec = (io->timeout % 1000) * 1000;
		ptv = &tv;
	} else
		ptv = NULL;

	event_add(&io->ev, ptv);
}
예제 #6
0
파일: hub.c 프로젝트: xiserge/Shakespeer
void hub_set_idle_timeout(hub_t *hub)
{
    if(event_initialized(&hub->idle_timeout_event))
    {
        evtimer_del(&hub->idle_timeout_event);
    }
    else
    {
        evtimer_set(&hub->idle_timeout_event, hub_send_keep_alive, hub);
    }

    struct timeval tv = {.tv_sec = 300, .tv_usec = 0};
    evtimer_add(&hub->idle_timeout_event, &tv);
}

static char *hub_make_tag(hub_t *hub)
{
    char *tag = 0;
    int num_returned_bytes = asprintf(&tag, "<%s V:%s,M:%c,H:%d/%d/%d,S:%d>",
                                      global_id_tag,
                                      global_id_version,
                                      hub->me->passive ? 'P' : 'A',
                                      hub_count_normal(), hub_count_registered(), hub_count_operator(),
                                      hub_slots_total());
    if (num_returned_bytes == -1)
        DEBUG("asprintf did not return anything");

    return tag;
}
예제 #7
0
int
main() {
        pipe(pp);
        struct event_base *base = event_base_new();
        evr = event_new(base, pp[0], EV_READ, read_cb, NULL);
        evw = event_new(base, pp[1], EV_WRITE  | EV_TIMEOUT, write_cb, NULL);

        event_add(evw, NULL);
        event_add(evr, NULL);

        struct event_base *outbase;
        int fdout;
        event_get_assignment(evw, NULL, &fdout, NULL, NULL, NULL);		/* Note fdout. */
        outbase = event_get_base(evw);		/* outbase == base */
        event_callback_fn cb = event_get_callback(evw);
        assert(write_cb == cb);
        assert(NULL == event_get_callback_arg(evw));

        assert(EV_WRITE|EV_TIMEOUT == event_get_events(evw));

        assert(pp[1] == event_get_fd(evw));
        assert(event_initialized(evw) == 1);

        test_num_events();
        event_base_dispatch(base);

        return 0;
}
예제 #8
0
void EventWatcher::Cancel() 
{
    if (event_initialized(&event_)) {
        event_del(&event_);
    }
    memset(&event_, 0, sizeof(event_));
}
예제 #9
0
static void php_http_client_curl_event_dtor(void **context)
{
	php_http_client_curl_event_context_t *ctx = *context;
	php_http_client_curl_t *curl;

#if DBG_EVENTS
	fprintf(stderr, "D");
#endif

	curl = ctx->client->ctx;

	curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, NULL);
	curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, NULL);
	curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, NULL);
	curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, NULL);

	if (event_initialized(ctx->timeout) && event_pending(ctx->timeout, EV_TIMEOUT, NULL)) {
		event_del(ctx->timeout);
	}
	efree(ctx->timeout);
	event_base_free(ctx->evbase);

	efree(ctx);
	*context = NULL;
}
예제 #10
0
static ZEND_RESULT_CODE php_http_client_curl_event_wait(void *context, struct timeval *custom_timeout)
{
	php_http_client_curl_event_context_t *ctx = context;
	struct timeval timeout;

#if DBG_EVENTS
	fprintf(stderr, "W");
#endif

	if (!event_initialized(ctx->timeout)) {
		if (0 > event_assign(ctx->timeout, ctx->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_client_curl_event_timeout_callback, ctx)) {
			return FAILURE;
		}
	} else if (custom_timeout && timerisset(custom_timeout)) {
		if (0 > event_add(ctx->timeout, custom_timeout)) {
			return FAILURE;
		}
	} else if (!event_pending(ctx->timeout, EV_TIMEOUT, NULL)) {
		php_http_client_curl_get_timeout(ctx->client->ctx, 1000, &timeout);
		if (0 > event_add(ctx->timeout, &timeout)) {
			return FAILURE;
		}
	}

	if (0 > event_base_loop(ctx->evbase, EVLOOP_ONCE)) {
		return FAILURE;
	}

	return SUCCESS;
}
예제 #11
0
파일: common.c 프로젝트: dex/capwap-tun
void remove_from_event_loop(struct tun_info *info)
{
    /* Clean event */
    if (event_initialized(&info->tun_ev) && 
        event_pending(&info->tun_ev, EV_READ, NULL))
        event_del(&info->tun_ev);
}
예제 #12
0
int
bufferevent_get_priority(const struct bufferevent *bufev)
{
	if (event_initialized(&bufev->ev_read)) {
		return event_get_priority(&bufev->ev_read);
	} else {
		return event_base_get_npriorities(bufev->ev_base) / 2;
	}
}
예제 #13
0
void
BeatBoard::ClientEventStatus::terminate()
{
  if (event_initialized(&ev))
  {
    event_del(&ev);
  }
  close(sockfd);
}
예제 #14
0
static void ss_client_fini(redudp_client *client)
{
    ss_client *ssclient = (void*)(client + 1);
    if (event_initialized(&ssclient->udprelay)) {
        close(event_get_fd(&ssclient->udprelay));
        if (event_del(&ssclient->udprelay) == -1)
            redudp_log_errno(client, LOG_ERR, "event_del");
    }
}
예제 #15
0
void
_bufferevent_decref_and_unlock(struct bufferevent *bufev)
{
	struct bufferevent_private *bufev_private =
	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
	struct bufferevent *underlying;

	if (--bufev_private->refcnt) {
		BEV_UNLOCK(bufev);
		return;
	}

	underlying = bufferevent_get_underlying(bufev);

	/* Clean up the shared info */
	if (bufev->be_ops->destruct)
		bufev->be_ops->destruct(bufev);

	/* XXX what happens if refcnt for these buffers is > 1?
	 * The buffers can share a lock with this bufferevent object,
	 * but the lock might be destroyed below. */
	/* evbuffer will free the callbacks */
	evbuffer_free(bufev->input);
	evbuffer_free(bufev->output);

	if (bufev_private->rate_limiting) {
		if (bufev_private->rate_limiting->group)
			bufferevent_remove_from_rate_limit_group(bufev);
		if (event_initialized(&bufev_private->rate_limiting->refill_bucket_event))
			event_del(&bufev_private->rate_limiting->refill_bucket_event);
		event_debug_unassign(&bufev_private->rate_limiting->refill_bucket_event);
		mm_free(bufev_private->rate_limiting);
		bufev_private->rate_limiting = NULL;
	}

	event_debug_unassign(&bufev->ev_read);
	event_debug_unassign(&bufev->ev_write);

	BEV_UNLOCK(bufev);
	if (bufev_private->own_lock)
		EVTHREAD_FREE_LOCK(bufev_private->lock,
		    EVTHREAD_LOCKTYPE_RECURSIVE);

	/* Free the actual allocated memory. */
	mm_free(bufev - bufev->be_ops->mem_offset);

	/* release the reference to underlying now that we no longer need
	 * the reference to it.  This is mainly in case our lock is shared
	 * with underlying.
	 * XXX Should we/can we just refcount evbuffer/bufferevent locks?
	 * It would probably save us some headaches.
	 */
	if (underlying)
		bufferevent_decref(underlying);
}
예제 #16
0
/* Start status timer for client. */
void
status_timer_start(struct client *c)
{
	struct session	*s = c->session;

	if (event_initialized(&c->status.timer))
		evtimer_del(&c->status.timer);
	else
		evtimer_set(&c->status.timer, status_timer_callback, c);

	if (s != NULL && options_get_number(s->options, "status"))
		status_timer_callback(-1, 0, c);
}
예제 #17
0
파일: names.c 프로젝트: goller/tmate-slave
void
queue_window_name(struct window *w)
{
	struct timeval	tv;

	tv.tv_sec = 0;
	tv.tv_usec = NAME_INTERVAL * 1000L;

	if (event_initialized(&w->name_timer))
		evtimer_del(&w->name_timer);
	evtimer_set(&w->name_timer, window_name_callback, w);
	evtimer_add(&w->name_timer, &tv);
}
예제 #18
0
void
snmp_init(struct relayd *x_env, struct imsgev *iev)
{
	env = x_env;
	iev_main = iev;

	if (event_initialized(&env->sc_snmpev))
		event_del(&env->sc_snmpev);
	if (event_initialized(&env->sc_snmpto))
		event_del(&env->sc_snmpto);
	if (env->sc_snmp != -1) {
		close(env->sc_snmp);
		env->sc_snmp = -1;
	}

	if ((env->sc_flags & F_TRAP) == 0) {
		iev_main = NULL;
		return;
	}

	snmp_sock(-1, -1, iev);
}
예제 #19
0
static void free_worker(struct Worker *w)
{
	if (!w)
		return;
	if (event_initialized(&w->ev))
		event_del(&w->ev);
	tls_free(w->ctx);
	tls_free(w->base);
	tls_config_free(w->config);
	if (w->socket > 0)
		close(w->socket);
	memset(w, 0, sizeof *w);
	free(w);
}
예제 #20
0
static void
event_disable( struct tr_peerIo * io, short event )
{
    assert( tr_amInEventThread( io->session ) );
    assert( io->session != NULL );
    assert( io->session->events != NULL );
    assert( event_initialized( &io->event_read ) );
    assert( event_initialized( &io->event_write ) );

    if( ( event & EV_READ ) && ( io->pendingEvents & EV_READ ) )
    {
        dbgmsg( io, "disabling libevent ready-to-read polling" );
        event_del( &io->event_read );
        io->pendingEvents &= ~EV_READ;
    }

    if( ( event & EV_WRITE ) && ( io->pendingEvents & EV_WRITE ) )
    {
        dbgmsg( io, "disabling libevent ready-to-write polling" );
        event_del( &io->event_write );
        io->pendingEvents &= ~EV_WRITE;
    }
}
예제 #21
0
tun_t *
tun_close(tun_t *tun)
{
	if (event_initialized(&tun->ev))
		event_del(&tun->ev);
	if (tun->if_fd > 0)
		close(tun->if_fd);
	if (tun->ip_fd > 0)
		close(tun->ip_fd);
	if (tun->fd > 0)
		close(tun->fd);
	free(tun);
	
	return (NULL);
}
예제 #22
0
파일: snmp.c 프로젝트: RobinGeuze/relayd
void
snmp_init(struct relayd *env, enum privsep_procid id)
{
	if (event_initialized(&env->sc_snmpev))
		event_del(&env->sc_snmpev);
	if (event_initialized(&env->sc_snmpto))
		event_del(&env->sc_snmpto);
	if (env->sc_snmp != -1) {
		if (snmp_agentx) {
			snmp_agentx_close(snmp_agentx, AGENTX_CLOSE_OTHER);
			snmp_agentx = NULL;
		}
		close(env->sc_snmp);
		env->sc_snmp = -1;
	}

	if ((env->sc_flags & F_SNMP) == 0)
		return;

	snmp_procid = id;

	proc_compose_imsg(env->sc_ps, snmp_procid, -1,
	    IMSG_SNMPSOCK, -1, NULL, 0);
}
예제 #23
0
파일: names.c 프로젝트: goller/tmate-slave
void
window_name_callback(unused int fd, unused short events, void *data)
{
	struct window	*w = data;
	char		*name, *wname;

	if (w->active == NULL)
		return;

	if (!options_get_number(&w->options, "automatic-rename")) {
		if (event_initialized(&w->name_timer))
			event_del(&w->name_timer);
		return;
	}
	queue_window_name(w);

	if (w->active->screen != &w->active->base)
		name = NULL;
	else
		name = osdep_get_name(w->active->fd, w->active->tty);
	if (name == NULL)
		wname = default_window_name(w);
	else {
		/*
		 * If tmux is using the default command, it will be a login
		 * shell and argv[0] may have a - prefix. Remove this if it is
		 * present. Ick.
		 */
		if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
		    name != NULL && name[0] == '-' && name[1] != '\0')
			wname = parse_window_name(name + 1);
		else
			wname = parse_window_name(name);
		free(name);
	}

	if (w->active->fd == -1) {
		xasprintf(&name, "%s[dead]", wname);
		free(wname);
		wname = name;
	}

	if (strcmp(wname, w->name)) {
		window_set_name(w, wname);
		server_status_window(w);
	}
	free(wname);
}
예제 #24
0
static void php_http_client_curl_event_callback(int socket, short action, void *event_data)
{
	php_http_client_curl_event_context_t *ctx = event_data;
	php_http_client_curl_t *curl = ctx->client->ctx;

#if DBG_EVENTS
	fprintf(stderr, "E");
#endif

	php_http_client_curl_event_handler(event_data, socket, etoca(action));

	/* remove timeout if there are no transfers left */
	if (!curl->unfinished && event_initialized(ctx->timeout) && event_pending(ctx->timeout, EV_TIMEOUT, NULL)) {
		event_del(ctx->timeout);
	}
}
예제 #25
0
int
bufferevent_decref_and_unlock_(struct bufferevent *bufev)
{
	struct bufferevent_private *bufev_private =
	    EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
	int n_cbs = 0;
#define MAX_CBS 16
	struct event_callback *cbs[MAX_CBS];

	EVUTIL_ASSERT(bufev_private->refcnt > 0);

	if (--bufev_private->refcnt) {
		BEV_UNLOCK(bufev);
		return 0;
	}

	if (bufev->be_ops->unlink)
		bufev->be_ops->unlink(bufev);

	/* Okay, we're out of references. Let's finalize this once all the
	 * callbacks are done running. */
	cbs[0] = &bufev->ev_read.ev_evcallback;
	cbs[1] = &bufev->ev_write.ev_evcallback;
	cbs[2] = &bufev_private->deferred;
	n_cbs = 3;
	if (bufev_private->rate_limiting) {
		struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
		if (event_initialized(e))
			cbs[n_cbs++] = &e->ev_evcallback;
	}
	n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
	n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);

	event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
	    bufferevent_finalize_cb_);

#undef MAX_CBS
	BEV_UNLOCK(bufev);

	return 1;
}
예제 #26
0
파일: bench.c 프로젝트: AllenShi/h-store
static struct timeval *
run_once(void)
{
	int *cp, space;
	long i;
	static struct timeval ts, te;

	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
		if (event_initialized(&events[i]))
			event_del(&events[i]);
		event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *) i);
		event_add(&events[i], NULL);
	}

	event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);

	fired = 0;
	space = num_pipes / num_active;
	space = space * 2;
	for (i = 0; i < num_active; i++, fired++)
		send(pipes[i * space + 1], "e", 1, 0);

	count = 0;
	writes = num_writes;
	{ int xcount = 0;
	gettimeofday(&ts, NULL);
	do {
		event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
		xcount++;
	} while (count != fired);
	gettimeofday(&te, NULL);

	if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
	}

	evutil_timersub(&te, &ts, &te);

	return (&te);
}
예제 #27
0
파일: ioev.c 프로젝트: Vaelatern/OpenSMTPD
void
io_clear(struct io *io)
{
	io_debug("io_clear(%p)\n", io);

	/* the current io is virtually dead */
	if (io == current)
		current = NULL;

#ifdef IO_SSL
	if (io->ssl) {
		SSL_free(io->ssl);
		io->ssl = NULL;
	}
#endif

	if (event_initialized(&io->ev))
		event_del(&io->ev);
	if (io->sock != -1) {
		close(io->sock);
		io->sock = -1;
	}
}
예제 #28
0
void
pptpd_stop(pptpd *_this)
{
	int nctrl;
	pptp_ctrl *ctrl;
	struct timeval tv;

	if (event_initialized(&_this->ev_timer))
		evtimer_del(&_this->ev_timer);
	pptpd_close_1723(_this);

	/* XXX: use common procedure with l2tpd_stop */

	if (pptpd_is_stopped(_this))
		return;
	if (pptpd_is_shutting_down(_this)) {
		pptpd_stop_immediatly(_this);
		return;
	}
	_this->state = PPTPD_STATE_SHUTTING_DOWN;
	nctrl = 0;
	for (slist_itr_first(&_this->ctrl_list);
	    (ctrl = slist_itr_next(&_this->ctrl_list)) != NULL;) {
		pptp_ctrl_stop(ctrl, PPTP_CDN_RESULT_ADMIN_SHUTDOWN);
		nctrl++;
	}
	if (nctrl > 0) {
		tv.tv_sec = PPTPD_SHUTDOWN_TIMEOUT;
		tv.tv_usec = 0;

		evtimer_set(&_this->ev_timer, pptpd_stop_timeout, _this);
		evtimer_add(&_this->ev_timer, &tv);

		return;
	}
	pptpd_stop_immediatly(_this);
}
예제 #29
0
static void
be_async_destruct(struct bufferevent *bev)
{
	struct bufferevent_async *bev_async = upcast(bev);
	struct bufferevent_private *bev_p = BEV_UPCAST(bev);
	evutil_socket_t fd;

	EVUTIL_ASSERT(!upcast(bev)->write_in_progress &&
			!upcast(bev)->read_in_progress);

	bev_async_del_read(bev_async);
	bev_async_del_write(bev_async);

	fd = _evbuffer_overlapped_get_fd(bev->input);
	if (bev_p->options & BEV_OPT_CLOSE_ON_FREE) {
		/* XXXX possible double-close */
		evutil_closesocket(fd);
	}
	/* delete this in case non-blocking connect was used */
	if (event_initialized(&bev->ev_write)) {
		event_del(&bev->ev_write);
		_bufferevent_del_generic_timeout_cbs(bev);
	}
}
예제 #30
0
void
pptpd_stop_immediatly(pptpd *_this)
{
	pptp_ctrl *ctrl;

	if (event_initialized(&_this->ev_timer))
		evtimer_del(&_this->ev_timer);
	if (_this->state != PPTPD_STATE_STOPPED) {
		/* lock, to avoid multiple call from pptp_ctrl_stop() */
		_this->state = PPTPD_STATE_STOPPED;

		pptpd_close_1723(_this);
		for (slist_itr_first(&_this->ctrl_list);
		    (ctrl = slist_itr_next(&_this->ctrl_list)) != NULL;) {
			pptp_ctrl_stop(ctrl, 0);
		}
		pptpd_close_gre(_this);
		slist_fini(&_this->ctrl_list);
		slist_fini(&_this->call_free_list);
		pptpd_log(_this, LOG_NOTICE, "Stopped");
	} else {
		PPTPD_DBG((_this, LOG_DEBUG, "(Already) Stopped"));
	}
}