Ejemplo n.º 1
0
void _moddeinit(module_unload_intent_t intent)
{
    service_named_unbind_command("operserv", &os_connavg);
    hook_del_user_add(connavg_newuser);
    hook_del_event("user_add");
    del_conf_item("SAFE_CONNECTIONS", &conf_gi_table);
    event_delete(reset_connections, NULL);
}
/* event_got_response_handler -- called back when http response is received. */
static void event_got_response_handler(struct httpread *handle, void *cookie,
				       enum httpread_event en)
{
	struct wps_event_ *e = cookie;
	struct subscription *s = e->s;
	struct upnp_wps_device_sm *sm = s->sm;
	struct httpread *hread = e->hread;
	int reply_code = 0;

	assert(e == s->current_event);
	eloop_cancel_timeout(event_timeout_handler, NULL, e);

	if (en == HTTPREAD_EVENT_FILE_READY) {
		if (httpread_hdr_type_get(hread) == HTTPREAD_HDR_TYPE_REPLY) {
			reply_code = httpread_reply_code_get(hread);
			if (reply_code == HTTP_OK) {
				wpa_printf(MSG_DEBUG,
					   "WPS UPnP: Got event reply OK from "
					   "%s", e->addr->domain_and_port);
				event_delete(e);
				goto send_more;
			} else {
				wpa_printf(MSG_DEBUG, "WPS UPnP: Got event "
					   "error reply code %d from %s",
					   reply_code,
					   e->addr->domain_and_port);
				goto bad;
			}
		} else {
			wpa_printf(MSG_DEBUG, "WPS UPnP: Got bogus event "
				   "response %d from %s", en,
				   e->addr->domain_and_port);
		}
	} else {
		wpa_printf(MSG_DEBUG, "WPS UPnP: Event response timeout/fail "
			   "for %s", e->addr->domain_and_port);
		goto bad;
	}
	event_retry(e, 1);
	goto send_more;

send_more:
	/* Schedule sending more if there is more to send */
	if (s->event_queue)
		event_send_all_later(sm);
	return;

bad:
	/*
	 * If other side doesn't like what we say, forget about them.
	 * (There is no way to tell other side that we are dropping
	 * them...).
	 * Alternately, we could just do event_delete(e)
	 */
	wpa_printf(MSG_DEBUG, "WPS UPnP: Deleting subscription due to errors");
	subscription_unlink(s);
	subscription_destroy(s);
}
Ejemplo n.º 3
0
static void init()
{
	ENTER("event > init");
	
	while (event_delete( (espeak_EVENT*)pop() ))
	{}
	
	node_counter = 0;
}
Ejemplo n.º 4
0
uint8_t event_delete_list(event_ref * e_ptr) {
	if (!e_ptr) return FAILURE; //pointer is null
	event_ref e = *e_ptr;

	while(e) { //while an event still exists
		event_delete(&e);
	}

	return SUCCESS;
}
Ejemplo n.º 5
0
static void getEventsForLastHour_NoOne_countZero(void ** state){
    callback rc = reaction;
    turniket_t turniket = turniket_create(rc);
    worker_t worker1 = worker_new("Andrey Krysyuk", ALLOWED);
    worker_t worker2 = worker_new("Olga Yashan", ALLOWED);
    event_t event1 = event_new(worker1, 1000, INSIDE);
    event_t event2 = event_new(worker2, 1200, INSIDE);

    turniket_addEvent(turniket, event1);
    turniket_addEvent(turniket, event2);
    list_t * forLastHour = list_new();
    int count = turniket_getEventsForLastHour(turniket, forLastHour, 1500);
    assert_int_equal(count , 0);
    turniket_delete(turniket);
    event_delete(event1);
    event_delete(event2);
    worker_delete(worker1);
    worker_delete(worker2);
}
Ejemplo n.º 6
0
/* probe */
static int _probe(AppServerOptions options)
{
	Probe probe;
	AppServer * appserver;
	Event * event;
	struct timeval tv;

	memset(&probe, 0, sizeof(Probe));
	if(_probe_timeout(&probe) != 0)
	{
		free(probe.ifinfo);
		free(probe.volinfo);
		return 1;
	}
	if((event = event_new()) == NULL)
	{
		free(probe.ifinfo);
		free(probe.volinfo);
		return _probe_error(1);
	}
	if((appserver = appserver_new_event(&probe, options, "Probe", NULL,
					event)) == NULL)
	{
		free(probe.ifinfo);
		free(probe.volinfo);
		event_delete(event);
		return _probe_error(1);
	}
	tv.tv_sec = PROBE_REFRESH;
	tv.tv_usec = 0;
	if(event_register_timeout(event, &tv, (EventTimeoutFunc)_probe_timeout,
			&probe) != 0)
		_probe_error(0);
	else
		event_loop(event);
	appserver_delete(appserver);
	event_delete(event);
	free(probe.ifinfo);
	free(probe.volinfo);
	return 1;
}
Ejemplo n.º 7
0
espeak_ERROR event_declare (espeak_EVENT* event)
{
ENTER("event_declare");

	event_display(event);

	if (!event)
	{
		return EE_INTERNAL_ERROR;
	}

	int a_status = pthread_mutex_lock(&my_mutex); 
	espeak_ERROR a_error = EE_OK;

	if (!a_status)
	{
		SHOW_TIME("event_declare > locked\n");
		espeak_EVENT* a_event = event_copy(event);
		a_error = push(a_event);
		if (a_error != EE_OK)
		{
			event_delete(a_event);
		}
		SHOW_TIME("event_declare > unlocking\n");
		a_status = pthread_mutex_unlock(&my_mutex);
	}

  // TBD: remove the comment
  // reminder: code in comment.
  // This wait can lead to an underrun
  //
//   if (!a_status && !my_event_is_running && (a_error == EE_OK))
//   {
//       // quit when command is actually started 
//       // (for possible forthcoming 'end of command' checks)
	SHOW_TIME("event_declare > post my_sem_start_is_required\n");
	sem_post(&my_sem_start_is_required);
//       int val=1;
//       while (val)
// 	{
// 	  usleep(50000); // TBD: event?
// 	  sem_getvalue(&my_sem_start_is_required, &val);
// 	}
//     }

	if (a_status != 0)
	{
		a_error = EE_INTERNAL_ERROR;
	}

	return a_error;
}
Ejemplo n.º 8
0
static void getEventsForLastHour_fourEvents_countTwo(void ** state){
    callback rc = reaction;
    turniket_t turniket = turniket_create(rc);
    worker_t worker1 = worker_new("Andrey Krysyuk", ALLOWED);
    worker_t worker2 = worker_new("Olga Yashan", ALLOWED);
    event_t event1 = event_new(worker1, 1230, INSIDE);
    event_t event2 = event_new(worker2, 1200, INSIDE);
    event_t event3 = event_new(worker1, 1250, OUTSIDE);
    event_t event4 = event_new(worker2, 1255, OUTSIDE);
    turniket_addEvent(turniket, event1);
    turniket_addEvent(turniket, event2);
    turniket_addEvent(turniket, event3);
    turniket_addEvent(turniket, event4);
    list_t * forLastHour = list_new();
    int count = turniket_getEventsForLastHour(turniket, forLastHour, 1300);
    assert_int_equal(count , 2);
    turniket_delete(turniket);
    event_delete(event1);
    event_delete(event2);
    event_delete(event3);
    event_delete(event4);
}
Ejemplo n.º 9
0
Archivo: main.c Proyecto: kostya9/OP
int main(void)
{
    puts("Running unit tests for module stack");
    runTests();
    puts("Press anything to proceed to testing events...\n");
    getch();

    user_t * user_daniil = user_new("Daniil");
    user_t * user_vasya = user_new("Vasya");
    user_t * user_natasha = user_new("Natasha");
    user_t * user_katya = user_new("Katya");
    user_t * user_petya = user_new("Petya");
    user_t * user_sasha = user_new("Sasha");

    unsigned int seconds = 10;
    event_t * event = event_new(time(NULL) + seconds, "THIS IS A SUPER DUPER EVENT YEEE");
    event_add_alert(event, user_daniil, normal_alert);
    event_add_alert(event, user_vasya, normal_alert);
    event_add_alert(event, user_natasha, strange_alert);
    printf("Event 1 going on in %i seconds. Waiting...\n", seconds);

    unsigned int seconds_two = 12;
    printf("Setting up event 2 (group) which will be in %i seconds. Waiting ...\n ", seconds_two);
    event_t * event_two = event_new(time(NULL) + seconds_two, "THIS IS A SUPER DUPER EVENT TWO");
    user_t * users[6] = {user_daniil, user_vasya, user_natasha, user_katya, user_petya, user_sasha};
    event_add_alert_group(event_two, users, arr_len(users), normal_alert);

    Sleep(20 * 1000);

    event_delete(event);
    event_delete(event_two);

    for(unsigned int i = 0; i < arr_len(users); i++)
        user_delete(users[i]);

    return EXIT_SUCCESS;
}
Ejemplo n.º 10
0
/**
 * event_add - Add a new event to a queue
 * @s: Subscription
 * @data: Event data (is copied; caller retains ownership)
 * @probereq: Whether this is a Probe Request event
 * Returns: 0 on success, -1 on error, 1 on max event queue limit reached
 */
int event_add(struct subscription *s, const struct wpabuf *data, int probereq)
{
	struct wps_event_ *e;
	unsigned int len;

	len = dl_list_len(&s->event_queue);
	if (len >= MAX_EVENTS_QUEUED) {
		wpa_printf(MSG_DEBUG, "WPS UPnP: Too many events queued for "
			   "subscriber %p", s);
		if (probereq)
			return 1;

		/* Drop oldest entry to allow EAP event to be stored. */
		e = event_dequeue(s);
		if (!e)
			return 1;
		event_delete(e);
	}

	if (s->last_event_failed && probereq && len > 0) {
		/*
		 * Avoid queuing frames for subscribers that may have left
		 * without unsubscribing.
		 */
		wpa_printf(MSG_DEBUG, "WPS UPnP: Do not queue more Probe "
			   "Request frames for subscription %p since last "
			   "delivery failed", s);
		return -1;
	}

	e = os_zalloc(sizeof(*e));
	if (e == NULL)
		return -1;
	dl_list_init(&e->list);
	e->s = s;
	e->data = wpabuf_dup(data);
	if (e->data == NULL) {
		os_free(e);
		return -1;
	}
	e->subscriber_sequence = s->next_subscriber_sequence++;
	if (s->next_subscriber_sequence == 0)
		s->next_subscriber_sequence++;
	wpa_printf(MSG_DEBUG, "WPS UPnP: Queue event %p for subscriber %p "
		   "(queue len %u)", e, s, len + 1);
	dl_list_add_tail(&s->event_queue, &e->list);
	event_send_all_later(s->sm);
	return 0;
}
Ejemplo n.º 11
0
int conn_listening_del_event(event_base_t *base, array_t *listening)
{
    conn_t        *c = NULL;
    uint32_t       i = 0;
    listening_t   *ls = NULL;

    ls = listening->elts;
    for (i = 0; i < listening->nelts; i++) {
        c = ls[i].connection;
        if (event_delete(base, c->read, EVENT_READ_EVENT, 0) == FAST_ERROR) {
            return FAST_ERROR;
        }
    }

    return FAST_OK;
}
Ejemplo n.º 12
0
espeak_ng_STATUS event_declare(espeak_EVENT *event)
{
	if (!event)
		return EINVAL;

	espeak_ng_STATUS status;
	if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) {
		my_start_is_required = 1;
		return status;
	}

	espeak_EVENT *a_event = event_copy(event);
	if ((status = push(a_event)) != ENS_OK) {
		event_delete(a_event);
		pthread_mutex_unlock(&my_mutex);
	} else {
		my_start_is_required = 1;
		pthread_cond_signal(&my_cond_start_is_required);
		status = pthread_mutex_unlock(&my_mutex);
	}


	return status;
}
Ejemplo n.º 13
0
/* Adjust various statistics, dispatches the event handler (if notify is
 * nonzero) and then deletes the event.  This function does NOT delete the event
 * from any lists it might be on (eg nsp->read_list etc.) nse->event_done
 * MUST be true when you call this */
void event_dispatch_and_delete(struct npool *nsp, struct nevent *nse, int notify) {
  assert(nsp);
  assert(nse);

  assert(nse->event_done);

  nsp->events_pending--;
  assert(nsp->events_pending >= 0);

  if (nse->iod) {
    nse->iod->events_pending--;
    assert(nse->iod->events_pending >= 0);
  }

  if (notify) {
    nsock_trace_handler_callback(nsp, nse);
    nse->handler(nsp, nse, nse->userdata);
  }

  /* FIXME: We should be updating stats here ... */

  /* Now we clobber the event ... */
  event_delete(nsp, nse);
}
Ejemplo n.º 14
0
int main(void)
{
	int sfd, epfd;
	struct connection main_c;
	struct epoll_event new_evts[MAX_CONNECTIONS];

	if (unittest_http_parse(0)) return -1;

	prep_signals();

	epfd = epoll_create(MAX_CONNECTIONS);
	sfd = connmgr_create_server(8000);
	main_c.evt_id = sfd;
	event_new(epfd, &main_c);

	cos_init(NULL);

	while (1) {
		int nevts, i, accept_event = 0;

		nevts = epoll_wait(epfd, new_evts, MAX_CONNECTIONS, -1);
		if (nevts < 0) {
			perror("waiting for events");
			return -1;
		}
		for (i = 0 ; i < nevts ; i++) {
			struct epoll_event *e = &new_evts[i];
			struct connection *c = (struct connection *)e->data.ptr;

			if (c == &main_c) {
				if (e->events & (EPOLLERR | EPOLLHUP)) {
					printf("errors on the listen fd\n");
					return -1;
				}
				accept_event = 1;
			} else if (e->events & (EPOLLERR | EPOLLHUP)) {
				event_delete(epfd, c);
				close(c->evt_id);
				http_close_connection(c->conn_id);
				/* FIXME: free requests for connection */
			} else {
				int ret;

				ret = connection_event(c);
				if (ret > 0) {
					event_delete(epfd, c);
					close(c->evt_id);
					http_close_connection(c->conn_id);
				} else if (ret < 0) {
					return -1;
				}
			}
		}

		if (accept_event) {
			struct connection *c;
			c = connmgr_accept(main_c.evt_id);
			if (NULL == c) {
				printf("Not a large enough connection namespace.");
			} else {
				event_new(epfd, c);
			}
		}
	}
	return 0;
}
Ejemplo n.º 15
0
event_ref new_timer_event(uint8_t timer, uint16_t period, uint8_t repeat, uint16_t repeat_count) {
	event_ref e = 0;
	action_ref check_action = 0;

	node_ref period_arg = 0;
	node_ref trigger_arg = 0;

	time_ref time_trigger = 0;
	time_ref time_period = 0;


	for(;;) {

		// Create a new time period
		time_period = new_time();
		if(!time_period) break;
		// Set up the period
		if(timer == CLOCK_TIME) {
			time_period->clock_time = period;
		} else if (timer == MILLISECONDS) {
			time_period->milliseconds = period;
		} else if (timer == SECONDS) {
			time_period->seconds = period;
		} else if (timer == MINUTES) {
			time_period->minutes = period;
		} else if (timer == HOURS) {
			time_period->hours = period;
		} else if (timer == DAYS) {
			time_period->days = period;
		}

		// Create a new time trigger
		time_trigger = new_time();
		if(!time_trigger) break;
		// Set up the trigger = global + period
		add_time_to_time(time_trigger, global_time());
		add_time_to_time(time_trigger, time_period);

		// Create a new event
		e = new_event();
		if(!e) break;

		// Create a new action
		check_action = new_action();
		if(!check_action) break;
		// Set its function
		action_set_func(check_action, &check_time_args);

		// Create a trigger arg
		trigger_arg = new_node(time_trigger, &time_delete);
		if(!trigger_arg) break;

		// Create the period arg
		period_arg = new_node(time_period, &time_delete);
		if(!period_arg) break;
		node_append(period_arg, trigger_arg);

		action_set_args(check_action, period_arg);
		event_set_check(e, check_action);
		event_set_repeat(e, repeat, repeat_count);

		return e;
	}

	event_delete(&e);

	node_delete(&period_arg);
	node_delete(&trigger_arg);

	action_delete(&check_action);

	time_delete(&time_period);
	time_delete(&time_trigger);

	return FAILURE;
}
Ejemplo n.º 16
0
void event_alarm_delete( context_t *ctx, int fd )
{
	event_delete( ctx, fd, EH_TIMER );
	alarm_delete( ctx, fd );
}
Ejemplo n.º 17
0
static void *polling_thread(void *p)
{
	(void)p; // unused

	while (1) {
		int a_stop_is_required = 0;

		(void)pthread_mutex_lock(&my_mutex);
		my_event_is_running = 0;

		while (my_start_is_required == 0) {
			while ((pthread_cond_wait(&my_cond_start_is_required, &my_mutex) == -1) && errno == EINTR)
				continue; // Restart when interrupted by handler
		}

		my_event_is_running = 1;
		a_stop_is_required = 0;
		my_start_is_required = 0;

		pthread_mutex_unlock(&my_mutex);

		// In this loop, my_event_is_running = 1
		while (head && (a_stop_is_required == 0)) {
			espeak_EVENT *event = (espeak_EVENT *)(head->data);
			assert(event);

			if (my_callback) {
				event_notify(event);
				// the user_data (and the type) are cleaned to be sure
				// that MSG_TERMINATED is called twice (at delete time too).
				event->type = espeakEVENT_LIST_TERMINATED;
				event->user_data = NULL;
			}

			(void)pthread_mutex_lock(&my_mutex);
			event_delete((espeak_EVENT *)pop());
			a_stop_is_required = my_stop_is_required;
			if (a_stop_is_required > 0)
				my_stop_is_required = 0;

			(void)pthread_mutex_unlock(&my_mutex);
		}

		(void)pthread_mutex_lock(&my_mutex);

		my_event_is_running = 0;

		if (a_stop_is_required == 0) {
			a_stop_is_required = my_stop_is_required;
			if (a_stop_is_required > 0)
				my_stop_is_required = 0;
		}

		(void)pthread_mutex_unlock(&my_mutex);

		if (a_stop_is_required > 0) {
			// no mutex required since the stop command is synchronous
			// and waiting for my_cond_stop_is_acknowledged
			init();

			// acknowledge the stop request
			(void)pthread_mutex_lock(&my_mutex);
			my_stop_is_acknowledged = 1;
			(void)pthread_cond_signal(&my_cond_stop_is_acknowledged);
			(void)pthread_mutex_unlock(&my_mutex);
		}
	}

	return NULL;
}
Ejemplo n.º 18
0
static void* polling_thread(void*)
{
ENTER("polling_thread");

	while(1)
	{
		int a_stop_is_required=0;

		SHOW_TIME("polling_thread > locking\n");
		int a_status = pthread_mutex_lock(&my_mutex);
		SHOW_TIME("polling_thread > locked (my_event_is_running = 0)\n");
		my_event_is_running = 0;
		pthread_mutex_unlock(&my_mutex);
		SHOW_TIME("polling_thread > unlocked\n");

		SHOW_TIME("polling_thread > wait for my_sem_start_is_required\n");

		while ((sem_wait(&my_sem_start_is_required) == -1) && errno == EINTR)
		{
			continue; // Restart when interrupted by handler
		}

		SHOW_TIME("polling_thread > get my_sem_start_is_required\n");

		a_status = pthread_mutex_lock(&my_mutex);
		SHOW_TIME("polling_thread > locked (my_event_is_running = 1)\n");
		my_event_is_running = 1;
		pthread_mutex_unlock(&my_mutex);
		SHOW_TIME("polling_thread > unlocked\n");

		a_stop_is_required=0;
		a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);  // NOTE: may set a_stop_is_required to -1
		if ((a_status==0) && (a_stop_is_required > 0))
		{
			SHOW("polling_thread > stop required (%d)\n", __LINE__);
			while(0 == sem_trywait(&my_sem_stop_is_required))
			{
			};
		}
		else
		{
			a_stop_is_required=0;
		}

		// In this loop, my_event_is_running = 1
		while (head && (a_stop_is_required <= 0))
		{
			SHOW_TIME("polling_thread > check head\n");
			while(0 == sem_trywait(&my_sem_start_is_required))
			{
			};
	
			espeak_EVENT* event = (espeak_EVENT*)(head->data);
			assert(event);
	
			uint32_t time_in_ms = 0;
	
			int err = get_remaining_time((uint32_t)event->sample, 
							&time_in_ms, 
							&a_stop_is_required);
			if (a_stop_is_required > 0)
			{
				break;
			}
			else if (err != 0)
			{ 
				// No available time: the event is deleted.
				SHOW("polling_thread > %s\n","audio device down");
				a_status = pthread_mutex_lock(&my_mutex);
				SHOW_TIME("polling_thread > locked\n");
				event_delete( (espeak_EVENT*)pop());
				a_status = pthread_mutex_unlock(&my_mutex);
				SHOW_TIME("polling_thread > unlocked\n");
			}
			else if (time_in_ms==0)
			{ // the event is already reached.
				if (my_callback)
				{
					event_notify(event);
					// the user_data (and the type) are cleaned to be sure 
					// that MSG_TERMINATED is called twice (at delete time too).
					event->type=espeakEVENT_LIST_TERMINATED;
					event->user_data=NULL;
				}
	
				a_status = pthread_mutex_lock(&my_mutex);
				SHOW_TIME("polling_thread > locked\n");
				event_delete( (espeak_EVENT*)pop());
				a_status = pthread_mutex_unlock(&my_mutex);
				SHOW_TIME("polling_thread > unlocked\n");
			
				a_stop_is_required=0;
				a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
	
				if ((a_status==0) && (a_stop_is_required > 0))
				{
					SHOW("polling_thread > stop required (%d)\n", __LINE__);
					while(0 == sem_trywait(&my_sem_stop_is_required))
					{
					};
				}
				else
				{
					a_stop_is_required=0;
				}
			}
			else
			{ // The event will be notified soon: sleep until timeout or stop request
				a_stop_is_required = sleep_until_timeout_or_stop_request(time_in_ms);
			}
		}
	
		a_status = pthread_mutex_lock(&my_mutex);
		SHOW_TIME("polling_thread > locked\n");
	
		SHOW_TIME("polling_thread > my_event_is_running = 0\n");
		my_event_is_running = 0;
	
		if(a_stop_is_required <= 0)
		{
			a_status = sem_getvalue(&my_sem_stop_is_required, &a_stop_is_required);
			if ((a_status==0) && (a_stop_is_required > 0))
			{
				SHOW("polling_thread > stop required (%d)\n", __LINE__);
				while(0 == sem_trywait(&my_sem_stop_is_required))
				{
				};
			}
			else
			{
				a_stop_is_required=0;
			}
		}

		a_status = pthread_mutex_unlock(&my_mutex);
		SHOW_TIME("polling_thread > unlocked\n");

		if (a_stop_is_required > 0)
		{ 
			SHOW("polling_thread > %s\n","stop required!");
			// no mutex required since the stop command is synchronous
			// and waiting for my_sem_stop_is_acknowledged
			init();

			// acknowledge the stop request
			SHOW_TIME("polling_thread > post my_sem_stop_is_acknowledged\n");
			a_status = sem_post(&my_sem_stop_is_acknowledged);
		}
	}

	return NULL;
}
Ejemplo n.º 19
0
ssize_t dns_handler(context_t *ctx, event_t event, driver_data_t *event_data )
{
	dns_config_t *cf = (dns_config_t *) ctx->data;

	x_printf(ctx, "<%s> Event = \"%s\" (%d)\n", ctx->name, event_map[event], event);

	switch( event ) {
		case EVENT_INIT:
			if( event_data->type == TYPE_CUSTOM && event_data->event_custom ) {
				dns_conf_t *init = (dns_conf_t *) (event_data->event_custom);
				dns_load_servers( ctx, init->dns_resolver );

				cf->dns_timeout = init->dns_timeout;
				cf->current_host = strdup( init->dns_host );
			} else {
				const char *resolver = config_get_item( ctx->config, "resolver" );
				const char *host = config_get_item( ctx->config, "host" );

				if( ! config_get_timeval( ctx->config, "timeout", & cf->dns_timeout ))
					cf->dns_timeout = DNS_DEFAULT_TIMEOUT;

				if( ! config_get_intval( ctx->config, "retry", & cf->dns_max_retry ))
					cf->dns_max_retry = 5;

				dns_load_servers(ctx, resolver);
				cf->current_host = strdup( host );
				if( cf->dns_max_retry < cf->dns_max_servers )
					cf->dns_max_retry = cf->dns_max_servers;
			}

			cf->sock_fd = dns_resolve_host( ctx, cf->current_host );

			if( cf->sock_fd >= 0 ) {
				cf->dns_retries = cf->dns_max_retry ;
				cf->dns_timer = event_alarm_add( ctx, cf->dns_timeout, ALARM_TIMER );
				event_add( ctx, cf->sock_fd, EH_READ );
				x_printf(ctx,"attempting to resolve hostname %s (%d attempts)\n",cf->current_host, cf->dns_retries );
			} else {
				x_printf(ctx,"Failed to send query to socket...\n");
			}

		case EVENT_START:
			cf->state = DNS_STATE_RUNNING;
			break;

		case EVENT_TERMINATE:
			context_terminate( ctx );
			break;

		case EVENT_ALARM:
			{
				if( event_data->event_alarm == cf->dns_timer ) {
#ifdef ENABLE_GETADDRINFO
					// This is entirely pointless, because it can't block!!
					if( (cf->flags & DNS_NETWORK_UP) && (cf->flags & DNS_DNS_ENABLE) ) {
						x_printf(ctx,"Attempting DNS resolver check for %s\n",cf->dns_host);
						struct addrinfo *addrinfo = NULL;
						int rc = getaddrinfo( cf->dns_host, NULL, NULL, &addrinfo );
						if( rc == 0 ) {
							x_printf(ctx,"Name resolver completed..\n");
							freeaddrinfo( addrinfo );
						} else {
							x_printf(ctx,"Failure performing DNS name resolution.   Disconnecting network\n");
							logger(ctx,"Failure performing DNS name resolution.   Disconnecting network");
						}
					}
#endif
					x_printf(ctx,"TIMEOUT: dns failed to respond, trying next server\n");
					if( cf->sock_fd >= 0 ) {
						close( cf->sock_fd );
						cf->sock_fd = -1;
						event_delete( ctx, cf->sock_fd, EH_NONE );
					}

					if( cf->dns_retries-- > 0 ) {
						cf->dns_current = ( cf->dns_current + 1 ) % cf->dns_max_servers;
						cf->sock_fd = dns_resolve_host( ctx, cf->current_host );

						if( cf->sock_fd >= 0 ) {
							cf->dns_timer = event_alarm_add( ctx, cf->dns_timeout, ALARM_TIMER );
							event_add( ctx, cf->sock_fd, EH_READ );
						}
					} else {
						x_printf(ctx,"DNS RETRIES EXHAUSTED. Terminating\n");
						context_owner_notify( ctx, CHILD_EVENT, 0 );
						context_terminate( ctx );
					}

				}
			}
			break;

		case EVENT_READ:
			if( event_data->event_request.fd == cf->sock_fd ) {
				x_printf(ctx,"Got a read event on file descriptor %ld\n",event_data->event_request.fd);
				in_addr_t rc = dns_handle_dns_response( ctx, &( event_data->event_request ));
				x_printf(ctx, "handle response returned %d (0x%08x) (%s)\n", ntohl(rc), ntohl(rc), inet_ntoa( *(struct in_addr *) &rc ));

				if( rc == (unsigned long) -1 ) {
					x_printf(ctx,"Error reading from socket, skipping\n");
				} else {
					event_alarm_delete( ctx, cf->dns_timer );
					event_delete( ctx, cf->sock_fd, EH_NONE );
					close( cf->sock_fd );
					cf->sock_fd = -1;

					context_owner_notify( ctx, CHILD_EVENT, rc );
					context_terminate( ctx );
				}
			}

			break;

        default:
            x_printf(ctx,"\n *\n *\n * Emitted some kind of event \"%s\" (%d)\n *\n *\n", event_map[event], event);
    }
    return 0;
}
Ejemplo n.º 20
0
int main (int argc, char *argv[])
{
  DWORD sp1, sp2;
  struct ll_initparms parms;
  void *mbi;
  struct timespec time1, time2, time3, time4, time5;
  struct timespec time6, time7, time8, time9;
  int par[10];
  int i, secs;
  DWORD oldt, t;

  sp1 = get_sp();
  cli();

#ifdef PERIODIC
  parms.mode = LL_PERIODIC;
  parms.tick = T;
#else
  parms.mode = LL_ONESHOT;
#endif

  mbi = ll_init();
  event_init(&parms);

  if (mbi == NULL) {
    message("Error in LowLevel initialization code...\n");
    sti();
    l1_exit(-1);
  }
  sti();

  message("LowLevel started...\n");

/*  cli(); */
  NULL_TIMESPEC(&time1);
  NULL_TIMESPEC(&time2);
  NULL_TIMESPEC(&time3);
  NULL_TIMESPEC(&time4);
  NULL_TIMESPEC(&time5);
  NULL_TIMESPEC(&time6);
  NULL_TIMESPEC(&time7);
  NULL_TIMESPEC(&time8);
  NULL_TIMESPEC(&time9);

  ADDNANO2TIMESPEC(1000000, &time1);        /* Time1: 1 ms */
  ADDNANO2TIMESPEC(5000000, &time2);        /* Time2: 5 ms */
  ADDNANO2TIMESPEC(10000000, &time3);        /* Time 3: 10 ms */
  ADDNANO2TIMESPEC(3000000, &time4);        /* Time 4: 3 ms */
  ADDNANO2TIMESPEC(7500000, &time5);        /* Time 5: 7.5 ms */
  ADDNANO2TIMESPEC(7000000, &time6);        /* Time 6: 7 ms */
  ADDNANO2TIMESPEC(500000000, &time7);
  ADDNANO2TIMESPEC(500000000, &time7);
  ADDNANO2TIMESPEC(500000000, &time7);
  ADDNANO2TIMESPEC(500000000, &time7);
  ADDNANO2TIMESPEC(500000000, &time7);
  ADDNANO2TIMESPEC(500000000, &time7);        /* Time 7: 6*500 ms = 3 Sec*/
  ADDNANO2TIMESPEC(51700000, &time8);        /* Time 8: 51.7 ms */
  ADDNANO2TIMESPEC(51500000, &time9);        /* Time 9: 51.5 ms */

cli();
t = ll_gettime(TIME_NEW, NULL);
sti();
  
  for (i = 0; i < 10; i++) {
    par[i] = i + 1;
  }
  canEnd = 0;

  cli();

  event_post(time1, evtHandler, &(par[0]));
  event_post(time2, evtHandler, &(par[1]));
  event_post(time3, evtHandler, &(par[2]));
  event_post(time4, evtHandler, &(par[3]));
  event_post(time5, evtHandler, &(par[4]));
  i = event_post(time6, evtHandler, &(par[5]));
  event_post(time7, evtHandler, &(par[6]));
  event_post(time8, evtHandler, &(par[7]));
  event_post(time9, evtHandler, &(par[8]));

  event_delete(i);
  event_post(time5, evtHandler, &(par[5]));
    
  message("Now time is %lu\n", t);
  
  secs = 0;
  oldt = 0;
  canEnd = 0;
  while((canEnd == 0) && (secs < 6)) {
    cli();
    t = ll_gettime(TIME_NEW, NULL);
    sti();
    if (t < oldt) {
      error("Time goes back???\n");
      message("ARGGGGG! %lu %lu\n", t, oldt);
      ll_abort(100);
    }
    oldt = t;
    if ((t  / 1000000) > secs) {
      secs++;
      message("           %d     %lu\n", secs, t);
    }
  }
  cli();
  message("Can End detected\n");
  ll_end();
  sp2 = get_sp();
  for (i = 1; i < 10; i++) {
    message("Event %d called at time %lu    ==    %lu\n", id[i],
	    t1[i], t2[i]);
  }
  message("End reached!\n");
  message("Actual stack : %lx - ", sp2);
  message("Begin stack : %lx\n", sp1);
  message("Check if same : %s\n",sp1 == sp2 ? "Ok :-)" : "No :-(");

  return 1;
}
Ejemplo n.º 21
0
void
fDns_poll(void)
{
again:

    fDns_result = 0;

    if (eventc == 0) {
//        fprintf(stderr, "(%s:%s:%i) epoll_wait\n", __FILE__, __func__, __LINE__);
        rc = epoll_wait (epoll_fd, events, MAXEVENTS, -1);
        if (rc < 1) {
            fDns_result = rc;
            return;
        }
        eventc = rc;
        ec = -1;
    }

//    fprintf(stderr, "(%s:%s:%i) eventc: %i \n", __FILE__, __func__, __LINE__, eventc);

    while (eventc) {
        ec++;

        e = events[ec].data.ptr;

//        fprintf(stderr, "(%s:%s:%i) e->fd: %i, ec: %i \n", __FILE__, __func__, __LINE__, e->fd, ec);

        if ((events[ec].events & EPOLLERR) || (events[ec].events & EPOLLHUP) || (!(events[ec].events & EPOLLIN))) {
            /* An error has occured on this fd, or the socket is not ready for reading (why were we notified then?) */
            if (e->fd == fDns_socket) {
                fprintf(stderr, "(%s:%s:%i) e->fd == fDns_socket \n", __FILE__, __func__, __LINE__);

            } else if (e->fd == fDns_notify_fd) {
                fprintf(stderr, "(%s:%s:%i) e->fd == fDns_notify_fd\n", __FILE__, __func__, __LINE__);
            } else {
//                fprintf(stderr, "(%s:%s:%i) fDns_socket: %i, fDns_inotify_fd %i, epoll_fd: %i, error on: %i\n", __FILE__, __func__, __LINE__, fDns_socket, fDns_inotify_fd, epoll_fd, e->fd);
                event_delete(e);
            }

            eventc--;

            continue;

        } else if (e->fd == fDns_notify_fd) {
            fprintf(stderr, "(%s:%s:%i) fDns_notify_fd... \n", __FILE__, __func__, __LINE__);

            fDns_notify();

            eventc--;
            continue;

        } else if (e->fd == fDns_socket) {
//            fprintf(stderr, "(%s:%s:%i) fDns_socket... \n", __FILE__, __func__, __LINE__);

            fDns_client_sa_len = sizeof(struct sockaddr_storage);
//            fprintf(stderr, "(%s:%s:%i) got an tcp connection... \n", __FILE__, __func__, __LINE__);
            /* We have a notification on the listening socket, which means one or more incoming connections. */
            while (1) {

                event_new->sas_len = sizeof(struct sockaddr_storage);

                fDns_socket_infd = accept(fDns_socket, (struct sockaddr *) &event_new->sas, &event_new->sas_len);
                if (fDns_socket_infd == -1) {
                    if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                        /* We have processed all incoming connections. */
//                        fprintf(stderr, "(%s:%s:%i) accepted all incomming connections...\n", __FILE__, __func__, __LINE__);
                        break;
                    } else {
                        fprintf(stderr, "(%s:%s:%i) accept: '%s' \n", __FILE__, __func__, __LINE__, strerror(errno));
                        break;
                    }
                }

                if (event_new->sas_len > sizeof(struct sockaddr_storage)) {
                    fprintf(stderr, "(%s:%s:%i) event_new->sas_len %i > sizeof(struct sockaddr_storage) %lu \n", __FILE__, __func__, __LINE__, event_new->sas_len, sizeof(struct sockaddr_storage));
                    close(fDns_socket_infd);
                    continue;
                }

                rc = fDns_tcp_socket_non_blocking(fDns_socket_infd);
                if (rc == -1) {
                    close(fDns_socket_infd);
                    continue;
                }

                event_new->fd = fDns_socket_infd;
                event.data.ptr = event_new;
                event.events = EPOLLIN | EPOLLET;

                rc = epoll_ctl (epoll_fd, EPOLL_CTL_ADD, fDns_socket_infd, &event);
                if (rc == -1) {
                    fprintf(stderr, "(%s:%s:%i) epoll_ctl ADD: '%s' \n", __FILE__, __func__, __LINE__, strerror(errno));
                    close(fDns_socket_infd);
                    continue;
                }

                e = event_new;
                event_new = calloc(1, sizeof(struct event_struct));
                if (event_new == NULL) {
                    fprintf(stderr, "(%s:%s:%i) calloc of struct event_struct: '%s' \n", __FILE__, __func__, __LINE__, strerror(errno));
                    close(fDns_socket_infd);
                    continue;
                }

                e->next = event_new;
                event_new->prev = e;

//                fprintf(stderr, "(%s:%s:%i) got an connection: %i \n", __FILE__, __func__, __LINE__, fDns_socket_infd);
            }

            eventc--;
            continue;
        } else {
//            fprintf(stderr, "(%s:%s:%i) ec: %i, socket: %i\n", __FILE__, __func__, __LINE__, ec, e->fd);

            done = 0;
            fDns_bl = 0;

            while(1) {
                rc = recv(e->fd, &fDns_br[fDns_bl], FDNS_BUFFER_SIZE - fDns_bl, 0);
                if (rc == -1) {
                    /* If errno == EAGAIN, that means we have read all data. So go back to the main loop. */
                    if (errno != EAGAIN) {
                        done = 1;
                    }
                    break;
                } else if (rc == 0) {
                      /* End of file. The remote has closed the connection. */
//                    fprintf(stderr, "(%s:%s:%i) rc: %i fDns_bl: %u\n", __FILE__, __func__, __LINE__, rc, fDns_bl);
                    done = 1;
                    break;
                } else if (rc >= FDNS_BUFFER_SIZE - fDns_bl) {
                    fprintf(stderr, "(%s:%s:%i) rc: %i fDns_bl: %u\n", __FILE__, __func__, __LINE__, rc, fDns_bl);
                    //hony, it is to big for me.
                    done = 1;
                    break;
                }
                fDns_bl += rc;
//                fprintf(stderr, "(%s:%s:%i) rc: %i fDns_bl: %u\n", __FILE__, __func__, __LINE__, rc, fDns_bl);
            }

            if (done) {
//                fprintf(stderr, "(%s:%s:%i) closeing: %i\n", __FILE__, __func__, __LINE__, e->fd);

                event_delete(e);

                eventc--;
                continue;
            } else {

                //we now have something in the buffer to process.
                fDns_result = fDns_bl;
                fDns_client_sa = &e->sas;
                fDns_client_sa_len = e->sas_len;

                fDns_socket_infd = e->fd;

                eventc--;

                //we break and will be returing to mainloop() again.
                break;
            }
        }
    }

    if (fDns_result == 0)
        goto again;
}