Beispiel #1
0
/* sequence of operations

   PE 0                      PE 1            .......     PE last

   accept                    accept                      NO-OP
   NO-OP                     connect(PE 0)               connect(PE last-1)
   NO-OP                     read(PE 0)                  read(PE last-1)
   write(PE 1)               write(PE 2)                 NO-OP
   read(PE 1)                read(PE 2)                  NO-OP
   NO-OP                     write(PE 0)                 write(PE last-1)
   NO-OP                     close(PE 0)                 close(PE last-1)
   close(PE 1)               close(PE 2)                 NO-OP

*/
int f77name(rpn_comm_softbarrier)(ftnword *comm)   /* perform a soft sync */
{
  char buf[1024];
  int fdesc_up=-1, fdesc_down=-1;
  int status=-1;
  struct set_of_ports *p=chain;
  
  while ( (p!=NULL) && (p->comm != *comm) ) p = p->next ;
  if ( p == NULL ) return(-1);
  
  f77name(save_openmp_state)();
#ifdef DEBUG
  printf("Entering rpn_comm_soft_sync, PE=%d\n",p->pe_me);
  fflush(stdout);
  if(p->pe_me == 0) sleep(1);
#endif
  if(p->pe_me != p->nprocs-1) fdesc_down=accept_from_sock(p->my_server);
#ifdef DEBUG
  if(p->pe_me != p->nprocs-1) printf("PE=%d, accept_from_sock,fdesc_down=%d\n",p->pe_me,fdesc_down);
  fflush(stdout);
#endif
  if(p->pe_me != 0){
#ifdef DEBUG
    printf("PE=%d,connect_to_port %d@%x\n",p->pe_me,p->list_port[p->pe_me-1],p->list_server[p->pe_me-1]);
#endif
    fdesc_up=connect_to_port(p->list_server[p->pe_me-1],p->list_port[p->pe_me-1]);
#ifdef DEBUG
    printf("PE=%d,connected_to_port %d@%x, desc=%d\n",p->pe_me,p->list_port[p->pe_me-1],p->list_server[p->pe_me-1],fdesc_up);
#endif
    read(fdesc_up,buf,4);
    }
  if(p->pe_me != p->nprocs-1) write(fdesc_down,buf,4);

  if(p->pe_me != p->nprocs-1) read(fdesc_down,buf,4);
  if(p->pe_me != 0) write(fdesc_up,buf,4);

  if(fdesc_up != -1) close(fdesc_up);
  if(fdesc_down != -1) close(fdesc_down);
  f77name(restore_openmp_state)();
  return(MPI_SUCCESS);
}
Beispiel #2
0
void gossip_thread_server(char *LOGFILE, void (*event_loop)(EXTENDED_CLIENT_SLOT *), int TIMEOUT, void *data)
{
  /* the first child is the actual server */
  
  int fclient;
  char *buf;
  int buflen;

  char *Auth_token = get_broker_Authorization();
  /* int chgbuflen = FALSE; */
  unsigned int Bauth_token = 0;

  int fserver;
  int server_port;
  int ipaddr;
  int b0, b1, b2, b3;
  int myuid = getuid();
  int client_uid, client_pid, client_auth;
  int PING_INTERVAL;
  
  unsigned char buffer[16];
  
  fd_set rfds;
  struct timeval tv = {0};


  /*   the second child will act as a watchdog and PINGs its parent periodically */
  /*   it will exit when parent has exited and therefore no longer accepts connections */
  
  /* miscellaneous initializations + get Authorization token */
  
  set_timeout_counter( TIMEOUT/get_ping_interval() ); /* set ping number */
  PING_INTERVAL = get_ping_interval();                /* get ping interval */
  
  if(Auth_token == NULL) 
    {
      fprintf(stderr, "Authorization token failure \n");
      exit(1);
    }

  sscanf( Auth_token, "%u", &Bauth_token );
  
  /*   get a socket */
  fserver = get_sock_net();
   
  /*   set buffer sizes for socket */
  set_sock_opt( fserver );
  
  /*   bind to a free port, get port number */
  server_port = bind_sock_to_port( fserver );
  
  /*   parent writes host:port and exits after disconnecting stdin, stdout, and stderr */
  ipaddr = get_own_ip_address();  /* get own IP address as 32 bit integer */

  b0 = ipaddr >> 24; /* split IP address */
  b1 = ipaddr >> 16;
  b2 = ipaddr >> 8;
  b3 = ipaddr;
  b0 &= 255;
  b1 &= 255;
  b2 &= 255;
  b3 &= 255;

  buf = (char *)malloc(1024);
  /* host_name = (char *)malloc(128); */

  if ( !buf )
    {
      fprintf(stderr, "channel buffer is NULL !!!, exiting\n");
      exit(1);
    }
 
  snprintf(buf, 1023, "%d.%d.%d.%d:%d", b0, b1, b2, b3, server_port);
  
  /* fprintf(stderr, "Launching Server at host : \"%s\" \n",  buf); */
  
  set_host_and_port(LOGFILE, buf);  /* write to channel info file */
                                    /* write as A.B.C.D:port */
    
  fprintf(stderr, "Launching Server at host: \"%s\" using port: \"%d\"\n", get_server_name(buf), server_port );
  
  sprintf(buf, "%s.LOG", LOGFILE);
  
  fclose(stdout); /* close and reopen STDOUT  */
  fclose(stderr); /* close and reopen STDERR */
  
  freopen(buf, "w", stdout);  
  freopen(buf, "w", stderr);
  
  if( fork() > 0 ) 
    {
      fprintf(stderr, "Exiting from parent process !!!\n");
      exit(0);  /* parent exits now */
    }
  
  setpgrp(); /* get into another process group */
  
  /* set NO BUFFERING for STDOUT and STDERR */
  setvbuf(stdout, NULL, _IONBF, 0);
  setvbuf(stderr, NULL, _IONBF, 0);

  client_auth = 0;
  client_pid = 0;
  client_uid = 0;
  
    
  listen(fserver, 32767);

  /* #ifdef NEVERTRUE */

  /* pinging part */
  if( (ping_pid = fork()) == 0 )
    {
      int watchdog_client;
      int ping_ord = 0;
      char *pingbuf;

      pingbuf = (char *)malloc(128);
      close(fserver);
            
      sleep(PING_INTERVAL);
      
      while(watchdog_client = connect_to_localport(server_port))
	{
	  /* send PING command with ping counter */

	  sprintf(pingbuf, "PING %d", ping_ord++);
	
	  if(watchdog_client > 0)
	    {
	      write(watchdog_client, pingbuf, strlen(pingbuf));
	      close(watchdog_client);
	    }
	  else
	    {
	      fprintf(stderr, "End of Pinging Time, Server not responding !!!\n");
	      exit(0);
	    }
	  sleep(PING_INTERVAL);
	  
	}
      

      if(pingbuf != NULL)
	free(pingbuf);

      exit(0);
    }
  else if (ping_pid < 0)
    {
      fprintf(stderr, "Error: cannot fork server process, pid = %d\n", ping_pid);
    }
  /* #endif */


  /* fd_set rfds; */
  /* struct timeval tv; */

  tv.tv_sec = 5;
  tv.tv_usec = 0;
 
  /* back to first child, loop on connection requests */
  while( fclient = accept_from_sock(fserver) )
    {
      /* timeout */

      FD_ZERO(&rfds);
      FD_SET(fclient, &rfds);

      if (select(fclient+1, &rfds, NULL, NULL, &tv))
	{
	  buflen = read(fclient, buf, NCARMAX); /* get LOGIN command */
	  
	}
      else
	{
	  exit(1);
	}
     
      buf[buflen > 0 ? buflen : 0] = '\0';
      
      if(get_active_channels() <= 0 && exit_flag)
	{
	  fprintf(stderr, "Exiting from server, active channels: %d\n", get_active_channels());
	  store_all_channels_data();
	  close(fclient);
	  fprintf(stderr, "Server EXITING !\n");
	  
	  if(ping_pid > 0)
	    { 
	      kill(ping_pid, 9);
	    }
	  
	  exit(1);
	}

      if(strncmp(buf, "PING", 4) == 0) /* PING ? */
	{
	  /* echo to logfile */
	  fprintf(stderr, "Server: PING %d (%d)\n",                                                                              TIMEOUT/get_ping_interval() - get_timeout_counter(),                                                  get_timeout_counter());
	  close(fclient);
	  if(get_timeout_counter() <= 0 || ( ( get_active_channels() <= 0) && exit_flag ) )
	    {
	      /* Before exiting save any pending data buffer(s) 
		 to corresponding subchannel file(s) */
	      store_all_channels_data();

	      /**********NEW*********/
	      /* if(get_last_channel()) */
	      /**********NEW*********/
	      fprintf(stderr, "TIMEOUT detected, EXITING \n");
	      
	      if(ping_pid > 0)
		{ 
		  kill(ping_pid, 9);
		}

	      exit(1);
	    }

	  decrement_timeout_counter();
	  continue;    /* go service next connection request */
	}
      
      /* validate login command, check client uid against server uid,
       verify that authorization token is the right one,
       register client pid as connected if connection accepted */
      



      if(strncmp(buf, "LOGIN", 5) == 0) 
	{
	  client_uid = -1 ;
	  client_pid = -1 ;
	  

	  /* sscanf(buf, "LOGIN %d %d %u", &client_uid, &client_pid, &client_auth); */
	  sscanf(buf, "LOGIN %d %d %u", &client_uid, &client_pid, &client_auth);

	  
	  /* reject connection if not my uid 
	     or bad authorization token or not
	     a LOGIN command */

	  if( client_uid != myuid || md5_ssh( buffer ) )
	    { 
	      printf("Client Authentication FAILED\n");
	      continue;
	    }


	  else /*  LOGIN accepted, process next command  */
	    {
	      send_ack_nack(fclient, IS_OK); /* Positive ACK, LOGIN  accepted  */
	      /* printf("SSH Digest: %x\n", buffer); */  
    
	      /*  start a new thread to handle user request  */
	      start_client_thread_2((void *)&user_server, client_uid, client_pid, fclient, buf, data , (void *)event_loop);

	      continue;
	    }
	}

      else
	{
	  fprintf(stderr, "LOGIN command expected: %s\n", buf);
	  send_ack_nack(fclient, NOT_OK);  /*  Negative ACK, LOGIN rejected  */
	}
      
    } /* end while accept is successful */

  printf("Accept from socket failed, exiting !!!, fserver = %d\n", fserver);

  if(ping_pid > 0)
    {
      kill(ping_pid, 9);
    }

  if(buf)
    free(buf);
  
  exit(1);
}