Exemple #1
0
/* figure out when in the future to send our next ping broadcast message.
 * we do this based on a uniform[PING_INTERVAL_MIN .. PING_INTERVAL_MAX]
 * probability distribution, where the high end of the range is shorter
 * than the stp timeout window.  so, we have a fighting chance to have
 * other cloud boxes hear something from us before they time us out and
 * assume we are dead.
 */
void set_next_ping_alarm(void)
{
    int msec;

    if (times[6].tv_sec != -1) {
        if (db[28].d) { ddprintf("already set; returning.\n"); }
        return;
    }

    while (!checked_gettimeofday(&times[6]));
    msec = discrete_unif(PING_INTERVAL_MAX - PING_INTERVAL_MIN);
    msec += PING_INTERVAL_MIN;
    if (db[40].d) { msec *= 20; }

    if (db[7].d) {
        ddprintf("next time:  %d\n", msec);
    }

    usec_add_msecs(&times[6].tv_sec, &times[6].tv_usec, msec);

    if (db[7].d) {
        ptime("\nping_neighbor timer now", now);
        ptime("ping_neighbor interrupt", times[6]);
        ddprintf("\n");
    }

    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);
}
Exemple #2
0
/* note next time that a lockable resource will time out, and make sure
 * we get an interrupt for it.
 */
void reset_lock_timer()
{
    int i;
    long earliest_sec = -1, earliest_usec;

    for (i = 0; i < pending_request_count; i++) {
        lockable_resource_t *l = &pending_requests[i];
        update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
    }

    for (i = 0; i < locks_owned_count; i++) {
        lockable_resource_t *l = &locks_owned[i];
        update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
    }

    for (i = 0; i < locks_granted_count; i++) {
        lockable_resource_t *l = &locks_granted[i];
        update_time(&earliest_sec, &earliest_usec, l->sec, l->usec);
    }

    times[4].tv_sec = earliest_sec;
    times[4].tv_usec = earliest_usec;

    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);

} /* reset_lock_timer */
Exemple #3
0
/* Unix only provides a single ALRM signal, so we use the same handler for
 * frequent updates (every 5 seconds) and differential updates (every 30
 * seconds). We trigger an ALRM every 5 seconds and only write differential
 * updates every 6th ALRM. */
static void handle_signals(int sig) {
  if (sig == SIGINT || sig == SIGTERM) {
    write_update();
#ifdef ENABLE_FREQUENT_UPDATES
    write_frequent_update();
#endif
    exit(0);
  } else if (sig == SIGALRM) {
    alarm_count += 1;
    if (alarm_count % ALARMS_PER_UPDATE == 0) {
#ifndef DISABLE_FLOW_THRESHOLDING
      write_flow_log();
#endif
      write_update();
    }
#ifdef ENABLE_FREQUENT_UPDATES
    write_frequent_update();
#endif
    if (alarm_count % ALARMS_PER_UPDATE == 0
        && upload_failures_check(&upload_failures) > 0) {
      exit(0);
    }
    set_next_alarm();
  }
}
Exemple #4
0
INT32S cal_set_alarm(INT8U alarm_idx, TIME_T at, INT8U rpt_mode, void (*alarm_handler)())
{
	if (alarm_idx>ALARM_MAX_NUM) {
		return STATUS_FAIL;
	}
	
#if _OPERATING_SYSTEM == 1				// Soft Protect for critical section
//	OSSchedLock();
#endif
	alarm_event[alarm_idx].usage = TRUE;
	alarm_event[alarm_idx].alarm_time.tm_sec = at.tm_sec;
	alarm_event[alarm_idx].alarm_time.tm_min = at.tm_min;
	alarm_event[alarm_idx].alarm_time.tm_hour = at.tm_hour;
	alarm_event[alarm_idx].alarm_time.tm_mday = at.tm_mday;
	alarm_event[alarm_idx].alarm_time.tm_mon = at.tm_mon;
	alarm_event[alarm_idx].alarm_time.tm_year = at.tm_year;
	alarm_event[alarm_idx].repeat_mode = rpt_mode;
	alarm_event[alarm_idx].alarm_handler = alarm_handler;	
#if _OPERATING_SYSTEM == 1	
//	OSSchedUnlock();
#endif

	set_next_alarm();
	
	return STATUS_OK;	
}
Exemple #5
0
/* turn off ack timer */
void turn_off_ack_timer()
{
    if (awaiting_ack()) { return; }

    times[3].tv_sec = -1;
    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);
}
Exemple #6
0
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <interface> [whitelist]\n", argv[0]);
    return 1;
  }

  struct timeval start_timeval;
  gettimeofday(&start_timeval, NULL);
  start_timestamp_microseconds
      = start_timeval.tv_sec * NUM_MICROS_PER_SECOND + start_timeval.tv_usec;

  initialize_bismark_id();

  if (argc < 3 || initialize_domain_whitelist(argv[2])) {
    fprintf(stderr, "Error loading domain whitelist; whitelisting disabled.\n");
  }

#ifndef DISABLE_ANONYMIZATION
  if (anonymization_init()) {
    fprintf(stderr, "Error initializing anonymizer\n");
    return 1;
  }
#endif
#ifdef ENABLE_FREQUENT_UPDATES
  if (anonymize_mac(bismark_mac, bismark_mac)) {
    fprintf(stderr, "Error anonymizing router MAC address\n");
  }
#endif
  packet_series_init(&packet_data);
  flow_table_init(&flow_table);
  dns_table_init(&dns_table, &domain_whitelist);
#ifdef ENABLE_HTTP_URL
  http_table_init(&http_table);
#endif
  address_table_init(&address_table);
  drop_statistics_init(&drop_statistics);
#ifdef ENABLE_FREQUENT_UPDATES
  device_throughput_table_init(&device_throughput_table);
#endif
  upload_failures_init(&upload_failures, UPLOAD_FAILURES_FILENAME);

  initialize_signal_handler();
  set_next_alarm();

  /* By default, pcap uses an internal buffer of 500 KB. Any packets that
   * overflow this buffer will be dropped. pcap_stats tells the number of
   * dropped packets.
   *
   * Because pcap does its own buffering, we don't need to run packet
   * processing in a separate thread. (It would be easier to just increase
   * the buffer size if we experience performance problems.) */
  pcap_handle = initialize_pcap(argv[1]);
  if (!pcap_handle) {
    return 1;
  }
  return pcap_loop(pcap_handle, -1, process_packet, NULL);
}
Exemple #7
0
/* set timer to generate alarm if we haven't heard an ack back to our
 * sequence message.
 */
void update_ack_timer()
{
    if (!db[22].d) { return; }

    if (times[3].tv_sec != -1) { return; }

    while (!checked_gettimeofday(&times[3]));
    usec_add_msecs(&times[3].tv_sec, &times[3].tv_usec, ACK_TIMEOUT_MSEC);
    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);
}
Exemple #8
0
/* if the 'display the cloud' option has been set for this box,
 * every CLOUD_PRINT_INTERVAL seconds (usually 5 seconds), we re-build an
 * ascii version of our model of the current cloud, viewable from our web
 * page.
 *
 * (if that option is set for at least one box in the cloud, all cloud boxes
 * are instructed to include their local cloud topology connectivity with
 * every stp beacon they send out, somewhat increasing protocol overhead.
 * but not too badly, since these messages only go out on average every
 * second or so.)
 */
void set_next_cloud_print_alarm(void)
{
    struct timeval tv;

    if (!db[38].d && !db[30].d) {
        times[5].tv_sec = -1;
        times[7].tv_sec = -1;
        return;
    }

    if (times[5].tv_sec != -1 && times[7].tv_sec != -1) {
        if (db[28].d) { ddprintf("already set; returning.\n"); }
        return;
    }

    while (!checked_gettimeofday(&tv));

    if (times[5].tv_sec == -1) {
        times[5] = tv;
        usec_add_msecs(&times[5].tv_sec, &times[5].tv_usec,
                CLOUD_PRINT_INTERVAL);

        if (db[28].d) {
            ptime("\nprint_cloud timer now", now);
            ptime("print_cloud interrupt", times[5]);
            ddprintf("\n");
        }
    }

    if (times[7].tv_sec == -1) {
        int disable_time;
        times[7] = tv;

        disable_time = CLOUD_PRINT_DISABLE;
        if (stp_recv_beacon_count > 1) {
            disable_time *= stp_recv_beacon_count;
        }

        usec_add_msecs(&times[7].tv_sec, &times[7].tv_usec, disable_time);

        if (db[28].d) {
            ptime("\ndisable_print_cloud timer now", now);
            ptime("disable_print_cloud interrupt", times[7]);
            ddprintf("\n");
        }
    }

    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);
}
Exemple #9
0
extern "C" void RTC_IRQHandler(void){
//	RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
//	secondlyCheck();
//	return;
	uint32_t secval;
	// This is increment counter interrupt
	if (RTC_GetIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE)){
		// Clear pending interrupt
		RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
		_60th_SEC_COUNT=0;
		time2.second_inc=1;
		//run  checks at xx:xx:00
		if(!GetSS()){
			//run  checks at xx:00:00
			if(!GetMM()){
				//run daily checks at 00:00:00
				if(!GetHH()){
					//run weekly checks at Mon 00:00:00
					if(1 == GetDOW()){
						//run weekly checks at 1st Mon 00:00:00
						if(1 == GetDOM()){
							//run weekly checks at Jan 1st Mon 00:00:00
							if(1 == GetM()){
								yearlyCheck();
							}
							monthlyCheck();
						}
						weeklyCheck();
					}
					dailyCheck();
				}
				hourlyCheck();
			}
			minutelyCheck();
		}
		secondlyCheck();
	}

	// Continue to check the Alarm match
	if (RTC_GetIntPending(LPC_RTC, RTC_INT_ALARM)){
		// Clear pending interrupt
		RTC_ClearIntPending(LPC_RTC, RTC_INT_ALARM);
		set_next_alarm();
		sort_alarms();
		/* Send debug information */
//		_DBG_ ("ALARM 10s matched!");
	}
}
Exemple #10
0
/* figure out when in the future to send our next ping broadcast message.
 * we do this based on a uniform[PING_INTERVAL_MIN .. PING_INTERVAL_MAX]
 * probability distribution, where the high end of the range is shorter
 * than the stp timeout window.  so, we have a fighting chance to have
 * other cloud boxes hear something from us before they time us out and
 * assume we are dead.
 */
void set_next_scan_alarm(void)
{
    int msec;

    if (times[8].tv_sec != -1) {
        if (db[28].d) { ddprintf("already set; returning.\n"); }
        return;
    }

    while (!checked_gettimeofday(&times[8]));
    msec = discrete_unif(SCAN_INTERVAL_MAX - SCAN_INTERVAL_MIN);
    msec += PING_INTERVAL_MIN;

    if (db[7].d) {
        ddprintf("next time:  %d\n", msec);
    }

    usec_add_msecs(&times[8].tv_sec, &times[8].tv_usec, msec);

    block_timer_interrupts(SIG_BLOCK);
    set_next_alarm();
    block_timer_interrupts(SIG_UNBLOCK);
}
Exemple #11
0
static void alarm_callback(void)
{
	INT32S  i;
	t_rtc   alm_time;
	
	rtc_alarm_get(&alm_time);
	
	for (i=0;i<ALARM_MAX_NUM;i++) {
		if (!alarm_event[i].usage) {
			continue;
		}
		if ((alarm_event[i].alarm_time.tm_sec != alm_time.rtc_sec) ||
			(alarm_event[i].alarm_time.tm_min != alm_time.rtc_min) ||
			(alarm_event[i].alarm_time.tm_hour != alm_time.rtc_hour)) {
			continue;
		}
		
		if (alarm_event[i].en_flag && (alarm_event[i].repeat_mode==ALARM_DAY)) {
			if (alarm_event[i].alarm_handler) {
				(*alarm_event[i].alarm_handler)();
			}		
		}/*
		else if (alarm_event[i].en_flag && (alarm_event[i].repeat_mode==ALARM_ANNUAL)) {
			if ((alarm_event[i].alarm_time.tm_mday != tm.tm_mday) ||
			    (alarm_event[i].alarm_time.tm_mon != tm.tm_mon) ||
			    (alarm_event[i].alarm_time.tm_year != tm.tm_year)) {
			    	continue;
			}
			if (alarm_event[i].alarm_handler) {
				(*alarm_event[i].alarm_handler)();
			}
		}*/
	}
	set_next_alarm();	
	
}
Exemple #12
0
/* this is the routine that gets called when a timer interrupt takes place.
 * to avoid the complications of having to worry about messing with data
 * structures at the interrupt level when the mainline code might be
 * manipulating them, we simply notify that mainline code that a timer
 * interrupt took place, and let it handle it.  we do this by putting a
 * character into a pipe that is one of the things the mainline routine
 * selects on when it is looking for input.
 *
 * also, set the next timer alarm.
 */
void repeated(int arg)
{
    send_interrupt_pipe_char();
    set_next_alarm();
}
Exemple #13
0
void sigalrm_handler ( int n ) {

  pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );

  static time_t last_charge_check, last_charge_worka;
  int batlevel = pnd_device_get_battery_gauge_perc();
  int uamps = 0;
  time_t now;

  pnd_device_get_charge_current ( &uamps );

  if ( batlevel < 0 ) {
#if 0
    // couldn't read the battery level, so just assume low and make blinks?
    batlevel = 4; // low, but not cause a shutdown
#else
    // couldn't read the battery level, so just assume ok!
    batlevel = 50;
#endif
  }

  // first -- are we critical yet? if so, shut down!
  if ( batlevel <= b_shutdown && b_shutdown_script) {

    if ( uamps > 100 ) {
        // critical battery, but charging, so relax.
        b_warned = 0;
    } else {
      if (b_warned == 0) {
          // Avoid warning again till re-powered
          b_warned = 1;
          int x;
          pnd_log ( pndn_error, "Battery Current: %d\n", uamps );
          pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
	      	b_shutdown_script );

          if ( ( x = fork() ) < 0 ) {
	        pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
    	    exit ( -3 );
          }

         if ( x == 0 ) {
           char value [ 100 ];
           sprintf ( value, "%d", b_shutdelay );
	   execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
	   pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
	   exit ( -4 );
         }
      }
    } // charging

  }

  // charge monitoring
  now = time(NULL);
  if ( bc_enable && bc_charge_device != NULL && (unsigned int)(now - last_charge_check) > 60 ) {

    int charge_enabled = pnd_device_get_charger_enable ( bc_charge_device );
    if ( charge_enabled < 0 )
      pnd_log ( pndn_error, "ERROR: Couldn't read charger enable control\n" );
    else {

      if ( charge_enabled && batlevel >= bc_stopcap && 0 < uamps && uamps < bc_stopcur ) {
        pnd_log ( pndn_debug, "Charge stop conditions reached, disabling charging\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 0 );
      }
      else if ( !charge_enabled && batlevel <= bc_startcap ) {
        pnd_log ( pndn_debug, "Charge start conditions reached, enabling charging\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 1 );
      }

      // for some unknown reason it just stops charging randomly (happens once per week or so),
      // and does not restart, resulting in a flat battery if machine is unattended.
      // What seems to help here is writing to chip registers, we can do it here indirectly
      // by writing to enable. Doing it occasionally should do no harm even with missing charger.
      if ( batlevel <= bc_startcap && (unsigned int)(now - last_charge_worka) > 20*60 ) {
        pnd_log ( pndn_debug, "Charge workaround trigger\n" );
        pnd_device_set_charger_enable ( bc_charge_device, 1 );
        last_charge_worka = now;
      }
    }
    last_charge_check = now;
  }

  // is battery warning already active?
  if ( b_active ) {
    // warning is on!

    // is user charging up? if so, stop blinking.
    // perhaps we shoudl check if charger is connected, and not blink at all in that case..
    if ( uamps > 0 ) {
      //Re-arm warning
      b_warned = 0;
      pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
      b_active = 0;
      set_next_alarm ( b_frequency, 0 );
      pnd_device_set_led_charger_brightness ( 250 );
      return;
    }

    if ( b_active == 1 ) {
      // turn LED on
      pnd_log ( pndn_debug, "Blink on\n" );
      pnd_device_set_led_charger_brightness ( 200 );
      // set timer to short duration
      b_active = 2;
      set_next_alarm ( 0, b_blinkdur );
    } else if ( b_active == 2 ) {
      // turn LED off
      pnd_log ( pndn_debug, "Blink off\n" );
      pnd_device_set_led_charger_brightness ( 10 );
      // back to longer duration
      b_active = 1;
      set_next_alarm ( b_blinkfreq, 0 );
    }

    return;
  }

  // warning is off..
  if ( batlevel <= b_threshold && uamps < 0 ) {
    // battery seems low, go to active mode
    pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
    b_active = 1;
    set_next_alarm ( b_blinkfreq, 0 );
  } // battery level

  return;
}
Exemple #14
0
int main ( int argc, char *argv[] ) {
  int i;
  int logall = -1; // -1 means normal logging rules; >=0 means log all!

  for ( i = 1; i < argc; i++ ) {

    if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
      //printf ( "Going daemon mode. Silent running.\n" );
      g_daemon_mode = 1;
    } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {

      if ( isdigit ( argv [ i ][ 2 ] ) ) {
	unsigned char x = atoi ( argv [ i ] + 2 );
	if ( x >= 0 &&
	     x < pndn_none )
	{
	  logall = x;
	}
      } else {
	logall = 0;
      }
    } else {
      usage ( argv );
      exit ( 0 );
    }

  } // for

  /* enable logging?
   */
  pnd_log_set_pretext ( "pndevmapperd" );
  pnd_log_set_flush ( 1 );

  if ( logall == -1 ) {
    // standard logging; non-daemon versus daemon

    if ( g_daemon_mode ) {
      // nada
    } else {
      pnd_log_set_filter ( pndn_rem );
      pnd_log_to_stdout();
    }

  } else {
    FILE *f;

    f = fopen ( "/tmp/pndevmapperd.log", "w" );

    if ( f ) {
      pnd_log_set_filter ( logall );
      pnd_log_to_stream ( f );
      pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
    }

    if ( logall == pndn_debug ) {
      pnd_log_set_buried_logging ( 1 ); // log the shit out of it
      pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
    }

  } // logall

  pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );

  pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );

  // basic daemon set up
  if ( g_daemon_mode ) {

    // set a CWD somewhere else
    chdir ( "/tmp" );

    // detach from terminal
    if ( ( i = fork() ) < 0 ) {
      pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
      exit ( i );
    }
    if ( i ) {
      exit ( 0 ); // exit parent
    }
    setsid();

    // umask
    umask ( 022 ); // emitted files can be rwxr-xr-x

  } // set up daemon

  /* hmm, seems to not like working right after boot.. do we depend on another daemon or
   * on giving kernel time to init something, or ... wtf?
   * -- lets give the system some time to wake up
   */
  { // delay

    // this one works for pndnotifyd, which actually needs INOTIFYH..
    //

    // check if inotify is awake yet; if not, try waiting for awhile to see if it does
    pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );

    if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
      pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
      return ( -1 );
    }

    pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );

    // pndnotifyd also waits for user to log in .. pretty excessive, especially since
    // what if user wants to close the lid while at the log in screen? for now play the
    // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
    //

    // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
    // log-out and back in again, with SDs popping in and out between..
    pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
    char tmp_username [ 128 ];
    while ( 1 ) {
      if ( pnd_check_login ( tmp_username, 127 ) ) {
	break;
      }
      pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
      sleep ( 2 );
    } // spin
    pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );

  } // delay

  /* inhale config or die trying
   */
  char *configpath;

  // attempt to fetch a sensible default searchpath for configs
  configpath = pnd_conf_query_searchpath();

  // attempt to fetch the apps config. since it finds us the runscript
  pnd_conf_handle evmaph;

  evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );

  if ( ! evmaph ) {
    // couldn't locate conf, just bail
    pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
    exit ( -1 );
  }

  /* iterate across conf, stocking the event map
   */
  void *n = pnd_box_get_head ( evmaph );

  while ( n ) {
    char *k = pnd_box_get_key ( n );
    //printf ( "key %s\n", k );

    if ( strncmp ( k, "keys.", 5 ) == 0 ) {
      k += 5;

      // keys should really push push generic-events onto the table, since they;'re just a special case of them
      // to make things easier to read

      // figure out which keycode we're talking about
      keycode_t *p = keycodes;
      while ( p -> keycode != -1 ) {
	if ( strcasecmp ( p -> keyname, k ) == 0 ) {
	  break;
	}
	p++;
      }

      if ( p -> keycode != -1 ) {
	g_evmap [ g_evmap_max ].key_p = 1;    // its a key, not an event
	g_evmap [ g_evmap_max ].reqs = p;     // note the keycode

	// note the script to activate in response
	if ( strchr ( n, ' ' ) ) {
	  char *foo = strdup ( n );
	  char *t = strchr ( foo, ' ' );
	  *t = '\0';
	  g_evmap [ g_evmap_max ].script = foo;
	  g_evmap [ g_evmap_max ].maxhold = atoi ( t + 1 );
	} else {
	  g_evmap [ g_evmap_max ].script = n;
	  g_evmap [ g_evmap_max ].maxhold = 0;
	}

	pnd_log ( pndn_rem, "Registered key %s [%d] to script %s with maxhold %d\n",
		  p -> keyname, p -> keycode, (char*) n, g_evmap [ g_evmap_max ].maxhold );

	g_evmap_max++;
      } else {
	pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
      }

    } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
      k += 7;

      // yes, key events could really be defined in this generic sense, and really we could just let people
      // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
      // add a 'generic' section to conf file and just let folks redefine random events that way
      // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
      // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
      // should just exist as scripts rather than daemons, but whose counting?

      // figure out which keycode we're talking about
      generic_event_t *p = generics;
      while ( p -> code != -1 ) {
	if ( strcasecmp ( p -> name, k ) == 0 ) {
	  break;
	}
	p++;
      }

      if ( p -> code != -1 ) {
	g_evmap [ g_evmap_max ].key_p = 0;    // its an event, not a key
	g_evmap [ g_evmap_max ].reqs = p;     // note the keycode
	g_evmap [ g_evmap_max ].script = n;   // note the script to activate in response
	pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
	g_evmap_max++;
      } else {
	pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
      }

    } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
      // not consumed here, skip silently

    } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
      // not consumed here, skip silently

    } else if ( strncmp ( k, "battery_charge.", 15 ) == 0 ) {
      // not consumed here, skip silently

    } else {
      // uhhh
      pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
    }

    n = pnd_box_get_next ( n );
  } // while

  if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
    pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
    pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
  }

  if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
    g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
    pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
  }

  // battery conf
  if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
    b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
    pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
    b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
    pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
    b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
    pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
    b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
    pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
  }
  b_active = 0;
  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
    b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
    pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
    b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
    pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
  }
  if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
    b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
    pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.enable" ) != PND_CONF_BADNUM ) {
    bc_enable = pnd_conf_get_as_int ( evmaph, "battery_charge.enable" );
    pnd_log ( pndn_rem, "Battery charge enable set to %u", bc_enable );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" ) != PND_CONF_BADNUM ) {
    bc_stopcap = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" );
    pnd_log ( pndn_rem, "Battery charge stop capacity set to %u", bc_stopcap );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" ) != PND_CONF_BADNUM ) {
    bc_stopcur = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" );
    pnd_log ( pndn_rem, "Battery charge stop current set to %u", bc_stopcur );
  }
  if ( pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" ) != PND_CONF_BADNUM ) {
    bc_startcap = pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" );
    pnd_log ( pndn_rem, "Battery charge start capacity set to %u", bc_startcap );
  }
  if ( pnd_conf_get_as_char ( evmaph, "battery_charge.device" ) != NULL ) {
    bc_charge_device = strdup ( pnd_conf_get_as_char ( evmaph, "battery_charge.device" ) );
    pnd_log ( pndn_rem, "Battery charge device set to %s", bc_charge_device );
  }

  /* do we have anything to do?
   */
  if ( ! g_evmap_max ) {
    // uuuh, nothing to do?
    pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
    exit ( -1 );
  } // spin

  /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
   */
  sigset_t ss;
  sigemptyset ( &ss );

  struct sigaction siggy;
  siggy.sa_handler = sigchld_handler;
  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
  siggy.sa_flags = SA_RESTART; /* don't need anything */
  sigaction ( SIGCHLD, &siggy, NULL );

  /* set up the battery level warning timers
   */
  siggy.sa_handler = sigalrm_handler;
  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
  siggy.sa_flags = SA_RESTART; /* don't need anything */
  sigaction ( SIGALRM, &siggy, NULL );

  if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
    pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
  } else {
    pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
  }

  /* actually try to do something useful
   */

  // stolen in part from notaz :)

  // try to locate the appropriate devices
  int id;
  int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
  int imaxfd = 0;

  for ( id = 0; ; id++ ) {
    char fname[64];
    char name[256] = { 0, };
    int fd;

    snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
    fd = open ( fname, O_RDONLY );

    if ( fd == -1 ) {
      break;
    }

    if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
      name [ 0 ] = '\0';
    }

    pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );

    if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
      fds [ 0 ] = fd;
    } else if ( strcmp ( name, "gpio-keys" ) == 0) {
      fds [ 1 ] = fd;
    } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
      fds [ 0 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
      fds [ 2 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
      fds [ 3 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
      fds [ 4 ] = fd;
    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
      fds [ 5 ] = fd;
    } else {
      pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
      //fds [ 6 ] = fd;
      close ( fd );
      fd = -1;
      continue;
    }

    if (imaxfd < fd) imaxfd = fd;

  } // for

  if ( fds [ 0 ] == -1 ) {
    pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
  }

  if ( fds [ 1 ] == -1 ) {
    pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
  }

  if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
    pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
    //exit ( -2 );
  }

  /* loop forever, watching for events
   */

  while ( 1 ) {
    struct input_event ev[64];

    unsigned int max_fd = 3; /* imaxfd */
    int fd = -1, rd, ret;
    fd_set fdset;

    // set up fd list
    FD_ZERO ( &fdset );

    imaxfd = 0;
    for (i = 0; i < max_fd /*imaxfd*/; i++) {
      if ( fds [ i ] != -1 ) {
	FD_SET( fds [ i ], &fdset );

	if ( fds [ i ] > imaxfd ) {
	  imaxfd = fds [ i ];
	}

      }
    }

    // figure out if we can block forever, or not
    unsigned char do_block = 1;
    struct timeval tv;
    tv.tv_usec = 0;
    tv.tv_sec = 1;

    for ( i = i; i < g_evmap_max; i++ ) {
      if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
	do_block = 0;
	break;
      }
    }

    // wait for fd's or timeout
    ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );

    if ( ret == -1 ) {
      pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
      continue; // retry!

    } else if ( ret == 0 ) { // select returned with timeout (no fd)

      // timeout occurred; should only happen when 1 or more keys are being held down and
      // they're "maxhold" keys, so we have to see if their timer has passed
      unsigned int now = time ( NULL );

      for ( i = i; i < g_evmap_max; i++ ) {

	if ( g_evmap [ i ].keydown_time &&
	     g_evmap [ i ].maxhold &&
	     now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
	{
	  keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
	  dispatch_key ( k -> keycode, 0 /* key up */ );
	}

      } // for

    } else { // an fd was fiddled with

      for ( i = 0; i < max_fd; i++ ) {
	if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
	  fd = fds [ i ];
	} // fd is set?
      } // for

      /* buttons or keypad */
      rd = read ( fd, ev, sizeof(struct input_event) * 64 );
      if ( rd < (int) sizeof(struct input_event) ) {
	pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
	break;
      }

      for (i = 0; i < rd / sizeof(struct input_event); i++ ) {

	if ( ev[i].type == EV_SYN ) {
	  continue;
	} else if ( ev[i].type == EV_KEY ) {

	  // do we even know about this key at all?
	  keycode_t *p = keycodes;
	  while ( p -> keycode != -1 ) {
	    if ( p -> keycode == ev [ i ].code ) {
	      break;
	    }
	    p++;
	  }

	  // if we do, hand it off to dispatcher to look up if we actually do something with it
	  if ( p -> keycode != -1 ) {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
	    }
	    dispatch_key ( p -> keycode, ev [ i ].value );
	  } else {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n",  ev [ i ].code, ev [ i ].value );
	    }
	  }

	} else if ( ev[i].type == EV_SW ) {

	  // do we even know about this event at all?
	  generic_event_t *p = generics;
	  while ( p -> code != -1 ) {
	    if ( p -> code == ev [ i ].code ) {
	      break;
	    }
	    p++;
	  }

	  // if we do, hand it off to dispatcher to look up if we actually do something with it
	  if ( p -> code != -1 ) {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
	    }
	    dispatch_event ( p -> code, ev [ i ].value );
	  } else {
	    if ( logall >= 0 ) {
	      pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n",  ev [ i ].code, ev [ i ].value );
	    }
	  }

	} else {
	  pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
	  continue;
	} // type?

      } // for

    } // an fd was touched

  } // while

  for (i = 0; i < 2; i++) {
    if ( i != 2 && fds [ i ] != -1 ) {
      close (fds [ i ] );
    }
  }

  return ( 0 );
} // main