Functions

gs_tools_common.c File Reference

#include "utility.h"
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <stdio.h>
#include "comm_data.h"
#include "comm_basics.h"
#include "comm_encode.h"
#include "general.h"
#include "gs_tools.h"
Include dependency graph for gs_tools_common.c:

Go to the source code of this file.

Functions

gs_server_t ** gs_tools_get_all_servers (char *agenthost, int agentport, int *num)
int gs_kill_server (char *agent_name, int agentport, char *server_name)
char * gs_get_password (char *prompt)
int gs_kill_agent (char *name, int agentport)

Detailed Description

Common routines for the GridSolve command line tools.

Definition in file gs_tools_common.c.


Function Documentation

char* gs_get_password ( char *  prompt  ) 

Turns off echo to the terminal and gets a password from the user. Resets the terminal before returning.

Parameters:
prompt -- The prompt that you would like displayed before reading the password (e.g. "Password:"). If NULL, no prompt is displayed.
Returns:
allocated string containing the password entered.

Definition at line 278 of file gs_tools_common.c.

{
  struct termios initialrsettings, newrsettings;
  char *password;

  if(prompt)
    printf("%s", prompt);

  tcgetattr(fileno(stdin), &initialrsettings);
  newrsettings = initialrsettings;
  newrsettings.c_lflag &= ~ECHO;

  if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0)
    return NULL;
  else {
    password = gs_get_line(stdin);

    tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);

    if(!password)
      return NULL;

    if(strlen(password) > 0)
      password[strlen(password)-1] = 0;
  }

  printf("\n");

  return password;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gs_kill_agent ( char *  name,
int  agentport 
)

Kills the specified agent.

Parameters:
name -- hostname of the agent
agentport -- port number of the agent
Returns:
0 on success, -1 on failure

Definition at line 319 of file gs_tools_common.c.

{
  int sock, tag;
  char *password;

  if((sock = gs_connect_direct(name, agentport)) == INVALID_SOCKET) {
    fprintf(stderr,"%s cannot be contacted\n", name);
    return(-1);
  }

  if((gs_send_tag(sock, GS_PROT_KILL_AGENT) < 0) ||
     (gs_send_string(sock, VERSION) < 0)) {
    fprintf(stderr,"Error communicating with agent.\n");
    return(-1);
  }

  if(gs_recv_tag(sock, &tag) < 0) {
    fprintf(stderr,"Error communicating with agent.\n");
    return(-1);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_VERSION_MISMATCH)
      fprintf(stderr, "Error: Agent is an incompatible version\n");
    else
      fprintf(stderr, "Error: Agent refused with code %d\n", tag);
    return(-1);
  }

  password = gs_get_password("GridSolve Agent Password: ");

  if(!password) {
    fprintf(stderr,"Error reading password.\n");
    return(-1);
  }

  if(gs_send_string(sock, password) < 0) {
    fprintf(stderr,"Error sending password\n");
    return(-1);
  }

  if(gs_recv_tag(sock, &tag) < 0) {
    fprintf(stderr,"Error receving confirmation tag\n");
    return(-1);
  }

  if(tag != GS_PROT_OK) {
    fprintf(stderr, "Not Killed, error code = %d\n", tag);
    return(-1);
  }

  printf("Successfully Killed\n");

  return(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int gs_kill_server ( char *  agent_name,
int  agentport,
char *  server_name 
)

Kills the specified server.

Parameters:
server_name -- hostname or CID of server to kill. if CID, must prefix "-c" to the CID string.

Definition at line 107 of file gs_tools_common.c.

{
  int sock, server_sock, tag, by_cid;
  gs_server_t *gs_server = NULL;
  char *msg, *password;

  by_cid = strncmp(server_name, "-c", 2) == 0;

  printf("GridSolve Server password for %s", by_cid ? server_name+2 : server_name);

  password = gs_get_password(": ");

  if(!password) {
    fprintf(stderr, "Error reading password.\n");
    return(-1);
  }

  if((sock = gs_connect_direct(agent_name, agentport)) == INVALID_SOCKET) {
    fprintf(stderr, "%s cannot be contacted\n", agent_name);
    return(-1);
  }

  if((gs_send_tag(sock, GS_PROT_KILL_SERVER) < 0) ||
     (gs_send_string(sock, VERSION) < 0)) {
    fprintf(stderr, "Error communicating with agent.\n");
    return(-1);
  }

  if(gs_recv_tag(sock, &tag) < 0) {
    fprintf(stderr, "Error communicating with agent.\n");
    return(-1);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_VERSION_MISMATCH)
      fprintf(stderr, "Error: Agent is an incompatible version\n");
    else
      fprintf(stderr, "Error: Agent refused with code %d\n", tag);
    return(-1);
  }

  if(gs_send_string(sock, server_name) < 0) {
    fprintf(stderr, "Unsuccessful (Sending Server)\n");
    return(-1);
  }

  if(gs_recv_tag(sock,&tag) < 0) {
    fprintf(stderr, "Unsuccessful (receiving response)\n");
    return(-1);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_UNKNOWN_SERVER) {
      if(by_cid) {
        fprintf(stderr, "No Server with component id '%s'.\n", server_name+2);
        fprintf(stderr, "Try specifying the component id exactly as it is ");
        fprintf(stderr, "displayed by GS_config\n");
      }
      else {
        fprintf(stderr, "No Server named '%s'.\n", server_name);
        fprintf(stderr, "Try specifying the hostname exactly as it is ");
        fprintf(stderr, "displayed by GS_config\n");
      }
    }
    else if(tag == GS_PROT_MULTIPLE_SERVER) {
      fprintf(stderr, "Multiple servers matched name '%s'.\n\n", server_name);
      fprintf(stderr, "Disambiguate by specifying the component id (cid) ");
      fprintf(stderr, "using\nthe -c flag to GS_killserver.  For example:\n\n");
      fprintf(stderr, "   GS_killserver -c agent_name 20206a66691be13f\n");
      fprintf(stderr, "\nGS_config will display the component ids.\n");
    }
    else
      fprintf(stderr, "Unspecified error killing server '%s'.\n", 
         by_cid ? (server_name + 2) : server_name);

    return(-1);
  }

  gs_server = (gs_server_t *)calloc(1, sizeof(gs_server_t));
  if(!gs_server) {
    fprintf(stderr, "Unsuccessful (Allocating Server Memory)\n");
    return(-1);
  }

  if(gs_recv_string(sock, &msg) < 0) {
    fprintf(stderr, "Unsuccessful receiving server structure.\n");
    return(-1);
  }

  if(gs_decode_server(msg, gs_server) <0){
    fprintf(stderr, "Unsuccessful (Decoding Server)\n");
    return(-1);
  }

  server_sock = gs_connect_to_host(gs_server->componentid, gs_server->ipaddress,
     gs_server->port, gs_server->proxyip, gs_server->proxyport);

  if(server_sock < 0) {
    fprintf(stderr, "Unsuccessful (connecting server)\n");
    gs_server_free(gs_server);
    return(-1);
  }

  if((gs_send_tag(server_sock, GS_PROT_KILL_SERVER) < 0) ||
     (gs_send_string(server_sock, VERSION) < 0)) {
    fprintf(stderr, "Unsuccessful (sending tag to Server)\n");
    gs_server_free(gs_server);
    return(-1);
  }

  if(gs_recv_tag(server_sock, &tag) < 0) {
    fprintf(stderr, "Error communicating with server.\n");
    return(-1);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_VERSION_MISMATCH)
      fprintf(stderr, "Error: Server is an incompatible version\n");
    else
      fprintf(stderr, "Error: Server refused with code %d\n", tag);
    return(-1);
  }

  if(gs_send_string(server_sock, password) < 0) {
    fprintf(stderr, "Unsuccessful (Sending Password)\n");
    gs_server_free(gs_server);
    return(-1);
  }

  if(gs_recv_tag(server_sock,&tag) < 0) {
    fprintf(stderr, "Unsuccessful (receiving response)\n");
    gs_server_free(gs_server);
    return(-1);
  }

  if(tag == GS_PROT_OK) {
    printf("Password Accepted and Successfully Killed\n");

    if(gs_send_tag(sock, GS_PROT_OK) < 0) {
      fprintf(stderr, "Unsuccessful (Sending Confirmation to Agent)\n");
      gs_server_free(gs_server);
      return(-1);
    }
  }
  else {
    printf("Password Not Accepted and Not Killed\n");

    if(gs_send_tag(sock, GS_PROT_ERROR) < 0) {
      fprintf(stderr, "Unsuccessful (Sending Confirmation to Agent)\n");
      gs_server_free(gs_server);
      return(-1);
    }
  }

  gs_server_free(gs_server);

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

gs_server_t** gs_tools_get_all_servers ( char *  agenthost,
int  agentport,
int *  num 
)

Gets a list of the servers from the specified agent.

Parameters:
agenthost -- the host name of the agent
agentport -- the port number of the agent
num -- the number of servers returned (set upon return)
Returns:
the server list on success, NULL on error.

Definition at line 34 of file gs_tools_common.c.

{
  int i, sock, num_servers, tag;
  gs_server_t **server_list;
  char *msg;

  *num = -1;

  if((sock = gs_connect_direct(agenthost, agentport)) == INVALID_SOCKET) {
    fprintf(stderr,"%s cannot be contacted\n", agenthost);
    return(NULL);
  }

  if((gs_send_tag(sock, GS_PROT_SERVER_LIST) < 0) ||
     (gs_send_string(sock, VERSION) < 0)) {
    fprintf(stderr,"Cannot communicate with %s\n", agenthost);
    fprintf(stderr,"Error sending tag GS_PROT_SERVER_LIST\n");
    return(NULL);
  }

  if(gs_recv_tag(sock, &tag) < 0) {
    fprintf(stderr,"Error communicating with agent.\n");
    return(NULL);
  }

  if(tag != GS_PROT_OK) {
    if(tag == GS_PROT_VERSION_MISMATCH)
      fprintf(stderr, "Error: Agent is an incompatible version\n");
    else
      fprintf(stderr, "Error: Agent refused with code %d\n", tag);
    return(NULL);
  }

  if(gs_recv_int(sock, &num_servers) < 0) {
    fprintf(stderr,"Error communicating with agent.\n");
    return(NULL);
  }


  *num = num_servers;
  if(num_servers <= 0)
    return(NULL);

  server_list = (gs_server_t **) CALLOC(num_servers, sizeof(gs_server_t *));
  if(!server_list) {
    fprintf(stderr,"Failed to allocate memory for the server list.\n");
    return(NULL);
  }

  for(i=0;i<num_servers;i++) {
    server_list[i] = (gs_server_t *) CALLOC(1,sizeof(gs_server_t));
    if(gs_recv_string(sock, &msg) < 0) {
      fprintf(stderr,"Error communicating with agent.\n");
      return(NULL);
    }
    if(gs_decode_server(msg, server_list[i]) < 0) {
      fprintf(stderr,"Error decoding server information.\n");
      return(NULL);
    }
    FREE(msg);
  }

  return server_list;
}

Here is the call graph for this function:

Here is the caller graph for this function: