Пример #1
0
int main(int argc, char* argv[])
{
    signal(SIGINT,  signal_term);
    signal(SIGTERM, signal_term);

    int daemon_mode = (argc > 1 && strcmp(argv[1], "-d") == 0) ? 1 : 0;
    if (daemon_mode) {
        if (file_exists(file_pid)) {
            perror("already running");
            exit(1);
        }

        pid_t pid = fork();

        // fork failed
        if (pid < 0) {
            return -1;
        } else if (pid != 0) {
            // fork successed, parent exit
            return 0;
        }

        write_pid();
        setsid();
        chdir("/");
        umask(0755);
        close(0);
        close(1);
        close(2);
    }

    lcd_connect();
    lcd_reset();
    lcd_clear();
    lcd_toggle_backlight();
    sleep(1);

    do {

        show_hostname();
        show_time();

        show_uptime();
        show_mems();
        show_loads();

        show_diskspace(DEFAULT_DISKSPACE_DIR);
        show_ip(DEFAULT_NETWORK_INTERFACE);
        show_traffic(DEFAULT_NETWORK_INTERFACE);

        sleep(10);
    } while (!signal_term_received);

    lcd_clear();
    lcd_reset();
    lcd_disconnect();
    unlink(file_pid);

    return 0;
}
Пример #2
0
// TODO: handle pthread_create return code
int
main (int argc, char **argv)
{
  pthread_t thread1, thread2;
  double tm;
//    struct timeval tv;
  struct timespec tv;
  struct timespec ts;

  argv_global = argv;

  //signal(SIGALRM,sig_alarm);
  signal (SIGHUP, sig_hup);

  if (argc != 6)
    {
      fprintf (stdout,
	       "Usage: %s interface1 \"interface1 pcap filter\" interface2 \"interface2 pcap filter\" timer\n",
	       argv[0]);
      return 0;
    }

  timer = atoi (argv[5]);
  if (timer == 0)
    {
      fprintf (stdout, "Wrong timer!\n");
      return (0);
    }

//    gettimeofday(&tv,NULL);
//    tm=tv.tv_sec+(double)tv.tv_usec/1000000;

  clock_gettime (CLOCK_MONOTONIC, &tv);
  tm = tv.tv_sec + (double) tv.tv_nsec / (double) BILLION;


  oldtm = tm;

  //motherthread = pthread_self ();
  pthread_mutex_lock(&mutex[0]);
  pthread_create (&thread1, NULL, (void *) dump_interface, (void *) 0);
  pthread_cond_wait(&condition, &mutex[0]);
  pthread_mutex_unlock(&mutex[0]);

  pthread_create (&thread2, NULL, (void *) dump_interface, (void *) 1);
  

  while (1)
    {
      ts.tv_sec = timer;
      ts.tv_nsec = 0;
      nanosleep (&ts, NULL);
      show_traffic ();
      sig_hup (0);
    }
  fprintf (stdout, "\nExiting!\n");
  return 0;
}