Exemple #1
0
/****** sge_client_ijs/stop_ijs_server() ***************************************
*  NAME
*     stop_ijs_server() -- stops the commlib server for the builtin
*                          interactive job support
*
*  SYNOPSIS
*     int stop_ijs_server(COMM_HANDLE **phandle, dstring *p_err_msg) 
*
*  FUNCTION
*     Stops the commlib server for the commlib connection between the shepherd
*     of the interactive job (qrsh/qlogin) and the qrsh/qlogin command.
*     Over this connectin the stdin/stdout/stderr input/output is transferred.
*
*  INPUTS
*     COMM_HANDLE **phandle - Pointer to the COMM server handle. Gets set to
*                             NULL in this function.
*     dstring *p_err_msg    - Contains the error reason in case of error.
*
*  RESULT
*     int - 0: OK
*           1: Invalid Parameter: phandle = NULL
*           2: General error shutting down the COMM server,
*              see p_err_msg for details
*
*  NOTES
*     MT-NOTE: stop_ijs_server() is not MT safe 
*
*  SEE ALSO
*     sge_client_ijs/start_ijs_server()
*     sge_client_ijs/run_ijs_server()
*     sge_client_ijs/force_ijs_server_shutdown()
*******************************************************************************/
int stop_ijs_server(COMM_HANDLE **phandle, dstring *p_err_msg)
{
   int ret = 0;

   DENTER(TOP_LAYER, "stop_ijs_server");

   if (phandle == NULL) {
      ret = 1;
   } else if (*phandle != NULL) {
      cl_com_set_error_func(NULL);
#if 0
      cl_log_list_set_log_level(cl_com_get_log_list(), CL_LOG_OFF);
#endif
      cl_com_ignore_timeouts(CL_TRUE);
      DPRINTF(("shut down the connection from our side\n"));
      ret = cl_commlib_shutdown_handle(*phandle, CL_FALSE);
      if (ret != CL_RETVAL_OK) {
         sge_dstring_sprintf(p_err_msg, "error shutting down the connection: %s",
            cl_get_error_text(ret));
         ret = 2;
      }
      *phandle = NULL;
   }
   DRETURN(ret);
}
Exemple #2
0
/*
 *  main()
 */
extern int main(int argc, char** argv) {
   cl_com_handle_t*    handle        = NULL; 
   cl_com_message_t*   message       = NULL;
   cl_com_endpoint_t*  sender        = NULL;
   cl_ssl_setup_t      ssl_config;
   cl_framework_t      framework     = CL_CT_TCP;
   int                 ret_val       = CL_RETVAL_OK;
   const int           fd_in         = 0;
   const int           fd_out        = 1;
   char                fd_in_data[255];
   char                fd_out_data[255];
   char*               reg           = NULL; 
   int                 error         = CL_RETVAL_OK;   

   reg = "reg";

   fd_in_data[0] = '\0';
   fd_out_data[0] = '\0';
 
   /* check command line argument count */
   if (argc <= 3) {
      usage();
      exit(1);
   }

   /* get service host and port as also the connection mode from command line */
   server_host = argv[1];
   handle_port = atoi(argv[2]);
   if (handle_port <= 0) {
      fprintf(stderr,"need a port number > 0\n");
      usage();
      exit(1);
   }

   if (argv[3]) {
      if (strcmp(argv[3], "SSL") == 0) {
         framework=CL_CT_SSL;
         printf("using SSL framework\n");
      }
   }else{
      printf("using TCP framework\n");
   }
 
   /* set the ssl environment */
   if(framework == CL_CT_SSL){
      memset(&ssl_config, 0, sizeof(ssl_config));
      ssl_config.ssl_method           = CL_SSL_v23;                 /*  v23 method                                  */
      ssl_config.ssl_CA_cert_pem_file = getenv("SSL_CA_CERT_FILE"); /*  CA certificate file                         */
      ssl_config.ssl_CA_key_pem_file  = NULL;                       /*  private certificate file of CA (not used)   */
      ssl_config.ssl_cert_pem_file    = getenv("SSL_CERT_FILE");    /*  certificates file                           */
      ssl_config.ssl_key_pem_file     = getenv("SSL_KEY_FILE");     /*  key file                                    */
      ssl_config.ssl_rand_file        = getenv("SSL_RAND_FILE");    /*  rand file (if RAND_status() not ok)         */
      ssl_config.ssl_crl_file         = getenv("SSL_CRL_FILE");     /*  revocation list file                        */
      ssl_config.ssl_reconnect_file   = NULL;                       /*  file for reconnect data    (not used)       */
      ssl_config.ssl_refresh_time     = 0;                          /*  key alive time for connections (not used)   */
      ssl_config.ssl_password         = NULL;                       /*  password for encrypted keyfiles (not used)  */
      ssl_config.ssl_verify_func      = my_ssl_verify_func;         /*  function callback for peer user/name check  */

      if (ssl_config.ssl_CA_cert_pem_file == NULL ||
          ssl_config.ssl_cert_pem_file    == NULL ||
          ssl_config.ssl_key_pem_file     == NULL ||
          ssl_config.ssl_rand_file        == NULL) {
         printf("please set the following environment variables:\n");
         printf("SSL_CA_CERT_FILE         = CA certificate file\n");
         printf("SSL_CERT_FILE            = certificates file\n");
         printf("SSL_KEY_FILE             = key file\n");
         printf("(optional) SSL_RAND_FILE = rand file (if RAND_status() not ok)\n");
      }
   }

   /* setup signalhandling */
   setup_signal_handler();
 
   /* setup commlib */
   error = cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_OFF, NULL);
 
   if(error != CL_RETVAL_OK){
      fprintf(stderr, "cl_com_setup_commlib failed with: %s\n", cl_get_error_text(error));
   }
   /* setup commlib error function callback */
   cl_com_set_error_func(on_communication_error);
 

   if (framework == CL_CT_SSL) {
      cl_com_specify_ssl_configuration(&ssl_config);
   }

   /* create communication handle */
   handle=cl_com_create_handle(&error,
                               framework,
                               CL_CM_CT_MESSAGE,
                               false,
                               handle_port,
                               CL_TCP_DEFAULT,
                               "client", 0,
                               5, 0 );
   if (handle == NULL) {
      fprintf(stderr, "could not create communication handle with error: %s\n", cl_get_error_text(error));
      cl_com_cleanup_commlib();
      exit(1);
   }
 
   /* print out some info output */
   printf("client running:\n");
   printf("host: \"%s\"\n",  handle->local->comp_host);
   printf("name: \"%s\"\n",  handle->local->comp_name);
   printf("id:   \"%ld\"\n", handle->local->comp_id);
    
   ret_val = cl_com_external_fd_register(handle, fd_in, fd_in_cb, CL_R_SELECT, fd_in_data);
   if (ret_val != CL_RETVAL_OK) {
      printf("could not register stdin-fd: %s\n", cl_get_error_text(ret_val));
      do_shutdown = 1;
   }else{
      printf("stdin-fd was registered\n");
   }

   ret_val = cl_com_external_fd_register(handle, fd_out, fd_out_cb, CL_W_SELECT, fd_out_data);
   if (ret_val != CL_RETVAL_OK) {
      printf("could not register stdout-fd: %s\n", cl_get_error_text(ret_val));
      do_shutdown = 1;
   }else{
      printf("stdout-fd was registered\n");
   }

   /* register at the server */
   ret_val = cl_commlib_send_message(handle,
                                     server_host, "server", 1, 
                                     CL_MIH_MAT_NAK, 
                                     (cl_byte_t**)&reg, strlen(reg)+1,
                                     NULL, 0, 0,
                                     true, false);
                                     
   if (ret_val != CL_RETVAL_OK) {
      do_shutdown = 1;
   }
   /* application main loop */
   while ( do_shutdown == 0 ) {
      CL_LOG(CL_LOG_WARNING, "App: start looping");
      cl_commlib_trigger(handle, 1);
      CL_LOG(CL_LOG_WARNING, "App: trigger was done");
      ret_val = cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);

      if (message != NULL) {
         CL_LOG(CL_LOG_WARNING, "App: std_out received");
         pthread_mutex_lock(&data_mutex);
         CL_LOG(CL_LOG_WARNING, "App: std_out got mutex");
         strcat(fd_out_data, (const char*)message->message);
         pthread_mutex_unlock(&data_mutex);
         CL_LOG(CL_LOG_WARNING, "App: std_out released mutex");
         cl_com_external_fd_set_write_ready(handle, fd_out);
         CL_LOG(CL_LOG_WARNING, "App: std_out set write ready flag");

         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
      }
      pthread_mutex_lock(&data_in_mutex);
      if(ready == 1) {
         cl_byte_t* bp = NULL; 
         CL_LOG(CL_LOG_WARNING, "App: std_in send");
         bp = (cl_byte_t*) fd_in_data;
         cl_commlib_send_message(handle,
                              server_host, "server", 1,
                              CL_MIH_MAT_NAK,
                              &bp, strlen(fd_in_data)+1,
                              NULL, 0, 0,
                              true, false);
         ready = 0;
         fd_in_data[0] = '\0';
      }
      pthread_mutex_unlock(&data_in_mutex);
   }
 
  
   printf("shutting down ...\n");
   cl_com_external_fd_unregister(handle, fd_in);
   cl_com_external_fd_unregister(handle, fd_out);
 
   /* here the application goes down - shutdown communication lib */
   while ( cl_commlib_shutdown_handle(handle, true) == CL_RETVAL_MESSAGE_IN_BUFFER) {
      message = NULL;
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         printf("ignoring message from \"%s\"\n", sender->comp_host); 
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }

   /* cleanup commlib */
   cl_com_cleanup_commlib();
   
   return 0;
 }
int main(int argc, char *argv[])
{
  struct hostent *he = NULL;
  char* resolved_name = NULL;
  int retval = CL_RETVAL_OK;
  char* ip_string = NULL;
  char** tp = NULL;
  char** tp2 = NULL;
  int name_only = 0;
  int sge_aliasing = 0;
  int all_option = 0;
  int system_error = 0;

#if defined(CRAY)  
  struct sockaddr_in  addr;
#else
  struct in_addr addr;
#endif

  if (argc < 2) {
    usage();
  }

  ip_string = argv[1];
  if (!strcmp(argv[1], "-help")) {
     usage();
  }
  if (!strcmp(argv[1], "-name")) {
     if (argc != 3) {
        usage(); 
     }
     name_only = 1;
     ip_string = argv[2];
  }   
  if (!strcmp(argv[1], "-aname")) {
     if (argc != 3) {
        usage(); 
     }
     name_only = 1;
     sge_aliasing = 1;
     ip_string = argv[2];
  }   
  if (!strcmp(argv[1], "-all")) {
     if (argc != 3) {
        usage(); 
     }
     name_only = 0;
     sge_aliasing = 1;
     all_option = 1;
     ip_string = argv[2];
  }

  
  retval = cl_com_setup_commlib(CL_NO_THREAD ,CL_LOG_OFF, NULL);
  if (retval != CL_RETVAL_OK) {
     fprintf(stderr,"%s\n",cl_get_error_text(retval));
     exit(1);
  }

  if (sge_aliasing ) {
     cl_com_set_alias_file(sge_get_alias_path());
  }

  retval = cl_com_set_error_func(gethostbyaddr_communication_error);
  if (retval != CL_RETVAL_OK) {
     fprintf(stderr,"%s\n",cl_get_error_text(retval));
     exit(1);
  }
  

#if defined(CRAY)
  addr.sin_addr.s_addr = inet_addr(ip_string);
#else
  addr.s_addr = inet_addr(ip_string);
#endif

  retval = cl_com_cached_gethostbyaddr(&addr, &resolved_name, &he, &system_error);
  if (retval != CL_RETVAL_OK) {
     cl_com_cleanup_commlib();
     if (error_reported == 0) {
        char* err_text = cl_com_get_h_error_string(system_error);

        if (err_text == NULL) {
           err_text = strdup(strerror(system_error));
           if (err_text == NULL) {
              err_text = strdup("unexpected error");
           }
        }
        if ( ip_string == NULL) {
            ip_string = "NULL";
        }

        fprintf(stderr,"error resolving ip "SFQ": %s (%s)\n",ip_string, cl_get_error_text(retval), err_text); 

        free(err_text); 
        err_text = NULL;
     }
     exit(1);
  }

  if (he == NULL) {
     fprintf(stderr,"%s\n","could not get hostent struct");
  }

  if (name_only) {
     if (sge_aliasing) {
        if (resolved_name != NULL) {
           printf("%s\n",resolved_name);
        } else {
           fprintf(stderr,"%s\n","unexpected error");
           exit(1);
        }
     } else {
        if (he != NULL) {
           printf("%s\n",he->h_name);
        } else {
           fprintf(stderr,"%s\n","could not get hostent struct");
           exit(1);
        }
     }
  } else {
     if (he != NULL) {
        printf(MSG_SYSTEM_HOSTNAMEIS_S,he->h_name);
        printf("\n");
     }
   
     if (resolved_name != NULL && all_option) {
        printf("SGE name: %s\n",resolved_name);
        free(resolved_name);
        resolved_name = NULL;
     } 
   
     if (he != NULL) {
        printf(MSG_SYSTEM_ALIASES );
   
        for (tp = he->h_aliases; *tp; tp++)
          printf("%s ", *tp);
        printf("\n");
     
        printf(MSG_SYSTEM_ADDRESSES );
   
        for (tp2 = he->h_addr_list; *tp2; tp2++)
           printf("%s ", inet_ntoa(* (struct in_addr *) *tp2));  /* inet_ntoa() is not MT save */
        printf("\n");    
     }
  }
  sge_free_hostent(&he);
  if (resolved_name != NULL) {
     free(resolved_name);
     resolved_name = NULL;
  }
  retval = cl_com_cleanup_commlib();
  if (retval != CL_RETVAL_OK) {
     fprintf(stderr,"%s\n",cl_get_error_text(retval));
     exit(1);
  }
  return 0;
}
Exemple #4
0
/****** commlib_to_tty() *******************************************************
*  NAME
*     commlib_to_tty() -- commlib_to_tty thread entry point and main loop
*
*  SYNOPSIS
*     void* commlib_to_tty(void *t_conf)
*
*  FUNCTION
*     Entry point and main loop of the commlib_to_tty thread.
*     Reads data from the commlib and writes it to the tty.
*
*  INPUTS
*     void *t_conf - pointer to cl_thread_settings_t struct of the thread
*
*  RESULT
*     void* - always NULL
*
*  NOTES
*     MT-NOTE: commlib_to_tty is MT-safe ?
*
*  SEE ALSO
*******************************************************************************/
void* commlib_to_tty(void *t_conf)
{
   recv_message_t       recv_mess;
   dstring              err_msg = DSTRING_INIT;
   int                  ret = 0, do_exit = 0;

   DENTER(TOP_LAYER, "commlib_to_tty");
   thread_func_startup(t_conf);

   while (do_exit == 0) {
      /*
       * wait blocking for a message from commlib
       */
      recv_mess.cl_message = NULL;
      recv_mess.data = NULL;

      DPRINTF(("commlib_to_tty: recv_message()\n"));
      ret = comm_recv_message(g_comm_handle, CL_TRUE, &recv_mess, &err_msg);
      if (ret != COMM_RETVAL_OK) {
         /* check if we are still connected to anybody. */
         /* if not - exit. */
         DPRINTF(("commlib_to_tty: error receiving message: %s\n", 
                  sge_dstring_get_string(&err_msg)));
         if (comm_get_connection_count(g_comm_handle, &err_msg) == 0) {
            DPRINTF(("commlib_to_tty: no endpoint found\n"));
            do_exit = 1;
            continue;
         }
      }
      DPRINTF(("commlib_to_tty: received a message\n"));

      thread_testcancel(t_conf);
      client_check_window_change(g_comm_handle);

      if (received_signal == SIGHUP ||
          received_signal == SIGINT ||
          received_signal == SIGQUIT ||
          received_signal == SIGTERM) {
         /* If we receive one of these signals, we must terminate */
         DPRINTF(("commlib_to_tty: shutting down because of signal %d\n",
                 received_signal));
         do_exit = 1;
         continue;
      }

      DPRINTF(("'parsing' message\n"));
      /*
       * 'parse' message
       * A 1 byte prefix tells us what kind of message it is.
       * See sge_ijs_comm.h for message types.
       */
      if (recv_mess.cl_message != NULL) {
         char buf[100];
         switch (recv_mess.type) {
            case STDOUT_DATA_MSG:
               /* copy recv_mess.data to buf to append '\0' */
               memcpy(buf, recv_mess.data, MIN(99, recv_mess.cl_message->message_length - 1));
               buf[MIN(99, recv_mess.cl_message->message_length - 1)] = 0;
               DPRINTF(("commlib_to_tty: received stdout message, writing to tty.\n"));
               DPRINTF(("commlib_to_tty: message is: %s\n", buf));
/* TODO: If it's not possible to write all data to the tty, retry blocking
 *       until all data was written. The commlib must block then, too.
 */
               if (sge_writenbytes(STDOUT_FILENO, recv_mess.data,
                          (int)(recv_mess.cl_message->message_length-1))
                       != (int)(recv_mess.cl_message->message_length-1)) {
                  DPRINTF(("commlib_to_tty: sge_writenbytes() error\n"));
               }
               break;
            case STDERR_DATA_MSG:
               DPRINTF(("commlib_to_tty: received stderr message, writing to tty.\n"));
/* TODO: If it's not possible to write all data to the tty, retry blocking
 *       until all data was written. The commlib must block then, too.
 */
               if (sge_writenbytes(STDERR_FILENO, recv_mess.data,
                          (int)(recv_mess.cl_message->message_length-1))
                       != (int)(recv_mess.cl_message->message_length-1)) {
                  DPRINTF(("commlib_to_tty: sge_writenbytes() error\n"));
               }
               break;
            case WINDOW_SIZE_CTRL_MSG:
               /* control message */
               /* we don't expect a control message */
               DPRINTF(("commlib_to_tty: received window size message! "
                        "This was unexpected!\n"));
               break;
            case REGISTER_CTRL_MSG:
               /* control message */
               /* a client registered with us. With the next loop, the 
                * cl_commlib_trigger function will send the WINDOW_SIZE_CTRL_MSG
                * (and perhaps some data messages),  which is already in the 
                * send_messages list of the connection, to the client.
                */
               DPRINTF(("commlib_to_tty: received register message!\n"));
               /* Send the settings in response */
               sprintf(buf, "noshell = %d", g_noshell);
               ret = (int)comm_write_message(g_comm_handle, g_hostname, COMM_CLIENT, 1,
                  (unsigned char*)buf, strlen(buf)+1, SETTINGS_CTRL_MSG, &err_msg);
               DPRINTF(("commlib_to_tty: sent SETTINGS_CTRL_MSG, ret = %d\n", ret));
               break;
            case UNREGISTER_CTRL_MSG:
               /* control message */
               /* the client wants to quit, as this is the last message the client
                * sends, we can be sure to have received all messages from the 
                * client. We answer with a UNREGISTER_RESPONSE_CTRL_MSG so
                * the client knows that it can quit now. We can quit, also.
                */
               DPRINTF(("commlib_to_tty: received unregister message!\n"));
               DPRINTF(("commlib_to_tty: writing UNREGISTER_RESPONSE_CTRL_MSG\n"));

               /* copy recv_mess.data to buf to append '\0' */
               memcpy(buf, recv_mess.data, MIN(99, recv_mess.cl_message->message_length - 1));
               buf[MIN(99, recv_mess.cl_message->message_length - 1)] = 0;

               /* the UNREGISTER_CTRL_MSG contains the exit status of the
                * qrsh_starter in case of qrsh <command> and the exit status
                * of the shell for qlogin/qrsh <no command>.
                * If the job was signalled, the exit code is 128+signal.
                */
               sscanf(buf, "%d", &g_exit_status);
               comm_write_message(g_comm_handle, g_hostname, COMM_CLIENT, 1, 
                  (unsigned char*)" ", 1, UNREGISTER_RESPONSE_CTRL_MSG, &err_msg);

               DPRINTF(("commlib_to_tty: received exit_status from shepherd: %d\n", 
                        g_exit_status));
               comm_flush_write_messages(g_comm_handle, &err_msg);
               do_exit = 1;
#if 0
               cl_log_list_set_log_level(cl_com_get_log_list(), CL_LOG_OFF);
               cl_com_set_error_func(NULL);
#endif
               break;
         }
      }
      comm_free_message(&recv_mess, &err_msg);
   }

   thread_func_cleanup(t_conf);
   DPRINTF(("commlib_to_tty: exiting commlib_to_tty thread!\n"));
   sge_dstring_free(&err_msg);
   DEXIT;
   return NULL;
}
Exemple #5
0
/*
 *  main()
 */
extern int main(int argc, char** argv) {

   int                handle_port = 0;
   cl_com_handle_t*   handle      = NULL; 
   cl_com_message_t*  message     = NULL;
   cl_com_endpoint_t* sender      = NULL;
   int                i           = 0;
 
 
   /* check command line argument count */
   if (argc != 2) {
      usage();
      exit(1);
   }
 
   /* get server port from command line */
   handle_port = atoi(argv[1]);
   if (handle_port <= 0) {
      fprintf(stderr,"need a port number > 0\n");
      usage();
      exit(1);
   }
 
 
   /* setup signalhandling */
   setup_signal_handler();
 
   /* setup commlib */
   cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_WARNING, on_communication_log);
 
   /* setup commlib error function callback */
   cl_com_set_error_func(on_communication_error);
 
   /* create communication handle */
   handle=cl_com_create_handle(NULL, 
                               CL_CT_TCP, 
                               CL_CM_CT_MESSAGE,
                               true,
                               handle_port,
                               CL_TCP_DEFAULT,
                               "server", 1,
                               1, 0 );
 
   if (handle == NULL) {
      fprintf(stderr, "could not create communication handle\n");
      cl_com_cleanup_commlib();
      exit(1);
   }
 
   /* print out some info output */
   printf("server running:\n");
   printf("host: \"%s\"\n", handle->local->comp_host);
   cl_com_get_service_port(handle,&i);
   printf("port: %d\n",     i);
   printf("name: \"%s\"\n", handle->local->comp_name);
   printf("id:   %ld\n",    handle->local->comp_id);
     
   /* application main loop */
   while(do_shutdown != 1) {
 
      cl_commlib_trigger(handle, 1);
 
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         char response_message[1024];
         sprintf(response_message,"Welcome to demo service at host \"%s\"!", handle->local->comp_host);
         printf("received message from \"%s/%s/%ld\": %s\n", sender->comp_host, sender->comp_name, sender->comp_id, message->message);
 
         cl_commlib_send_message(handle, 
                                 sender->comp_host, 
                                 sender->comp_name, 
                                 sender->comp_id, CL_MIH_MAT_NAK,  
                                 (cl_byte_t**)&response_message, 
                                 strlen(response_message)+1, 
                                 NULL, 0, 0, 
                                 true,false);
         message->message = NULL;
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }

   printf("shutting down ...\n");

   /* here the application goes down - shutdown communication lib */
   while ( cl_commlib_shutdown_handle(handle, true) == CL_RETVAL_MESSAGE_IN_BUFFER) {
      message = NULL;
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         printf("ignoring message from \"%s\"\n", sender->comp_host); 
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }
 
   /* cleanup commlib */
   cl_com_cleanup_commlib();

  return 0;
}