Пример #1
0
void *child(void * arg) {
    extern int active_threads;
    int host_id = *(int *) arg;
    char logmessage[LOGSIZE];

    if (set.verbose == POLLER_VERBOSITY_DEBUG) {
        snprintf(logmessage, LOGSIZE, "DEBUG: In Poller, About to Start Polling of Host\n");
        cacti_log(logmessage);
    }

    if (active_threads == 1) {
        if (set.verbose == POLLER_VERBOSITY_DEBUG) {
            snprintf(logmessage, LOGSIZE, "DEBUG: This is where popen DEADLOCKs errors occur\n");
        }
    }

    poll_host(host_id);

    thread_mutex_lock(LOCK_THREAD);
    active_threads = active_threads - 1;
    thread_mutex_unlock(LOCK_THREAD);

    if (set.verbose == POLLER_VERBOSITY_DEBUG) {
        snprintf(logmessage, LOGSIZE, "DEBUG: The Value of Active Threads is %i\n" ,active_threads);
        cacti_log(logmessage);
    }

    pthread_exit(0);
}
int main(int argc, char **argv)
{
   int status;
   int nrx_fd;
   int host_rfd;
   int host_wfd;
   int use_stdin = 0;
   char * ser_dev = NULL;
   int port = 12345;
   char * filename = default_filename;
   void * handle = NULL;
   int host_flash = 0;
   int input_baudrate = 115200;
   speed_t baudrate = B115200;
   int opt;

   struct ifreq ifr;
   struct nanoioctl nr;
   
   while((opt = getopt(argc, argv, "d:s:p:f:b:i")) != -1) {
      switch(opt) {
         case 'b':
            input_baudrate = atoi(optarg);
            switch(input_baudrate) {
               case 9600: baudrate = B9600; break;
               case 19200: baudrate = B19200; break;
               case 38400: baudrate = B38400; break;
               case 57600: baudrate = B57600; break;
               case 115200: baudrate = B115200; break;
               default: usage();
            }
            break;
         case 'd':
            debug = atoi(optarg);
            break;
         case 'f':
            filename = optarg;
            break;
         case 'i':
            use_stdin = 1;
            break;
         case 'p':
            port = atoi(optarg);
            break;
         case 's':
            ser_dev = optarg;
            break;
         default:
            break;
      }
   }
   if(optind == argc)
      usage();

   strcpy(ifr.ifr_name, argv[optind]);
   ifr.ifr_data = (char *)&nr;
   memset(&nr,0,sizeof(nr));
   nr.magic = NR_MAGIC;

   APP_INFO("Nanoradio network interface: %s\n", ifr.ifr_name);
   APP_INFO("Saving persistent MIB data of type HOST to: %s\n", filename);

   if(use_stdin) {
      APP_INFO("Using stdin/stdout as client interface\n");
      host_wfd = host_rfd = open_terminal();
      redirect_stdout("/tmp/hic_proxy.log");
   } else if(ser_dev) 
      {
	APP_INFO("Using %s (baudrate = %d) as serial client interface\n", ser_dev,input_baudrate);
	host_wfd = host_rfd = open_serial(ser_dev, baudrate);
      }
   else 
      {
         APP_INFO("Using ethernet as client interface\n");
         host_wfd = host_rfd = open_socket(port);
      }
    
   nrx_fd = socket(AF_INET, SOCK_DGRAM, 0);
   if(nrx_fd < 0) err(1, "socket");

   for(;;) {      
      status = poll_host(host_rfd, &ifr);
      if(status) {
	
         
         if((nr.data[TYPE_INDEX] ==  HIC_MESSAGE_TYPE_FLASH_PRG))
            {
               APP_DEBUG("flash cmd recieved\n");

               //examine flash cmd
               handle = flash_cmd(&ifr,handle,filename);
 
               if(handle)
                  {
                     //send local host flash cmd reply
                     host_write(host_wfd, nr.data, nr.length);
                  }
               else
                  {
                     //forward to target
                     nrx_write(nrx_fd, &ifr);
                  }
            }
         else
            {
               //forward to target
               nrx_write(nrx_fd, &ifr);
            }
      }
	
      status = poll_target(nrx_fd, &ifr);
  
      if(status)
         host_write(host_wfd, nr.data, nr.length);

      usleep(5000);
   }

   if(!use_stdin) {
      close(host_rfd);
   }
   close(nrx_fd);
}