gs_tools_common.c

Go to the documentation of this file.
00001 
00007 /* $Id: gs_tools_common.c,v 1.2 2008/10/28 19:50:40 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include "utility.h"
00011 
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <termios.h>
00015 #include <stdio.h>
00016 
00017 #include "comm_data.h"
00018 #include "comm_basics.h"
00019 #include "comm_encode.h"
00020 #include "general.h"
00021 #include "gs_tools.h"
00022 
00033 gs_server_t **
00034 gs_tools_get_all_servers(char *agenthost, int agentport, int *num)
00035 {
00036   int i, sock, num_servers, tag;
00037   gs_server_t **server_list;
00038   char *msg;
00039 
00040   *num = -1;
00041 
00042   if((sock = gs_connect_direct(agenthost, agentport)) == INVALID_SOCKET) {
00043     fprintf(stderr,"%s cannot be contacted\n", agenthost);
00044     return(NULL);
00045   }
00046 
00047   if((gs_send_tag(sock, GS_PROT_SERVER_LIST) < 0) ||
00048      (gs_send_string(sock, VERSION) < 0)) {
00049     fprintf(stderr,"Cannot communicate with %s\n", agenthost);
00050     fprintf(stderr,"Error sending tag GS_PROT_SERVER_LIST\n");
00051     return(NULL);
00052   }
00053 
00054   if(gs_recv_tag(sock, &tag) < 0) {
00055     fprintf(stderr,"Error communicating with agent.\n");
00056     return(NULL);
00057   }
00058 
00059   if(tag != GS_PROT_OK) {
00060     if(tag == GS_PROT_VERSION_MISMATCH)
00061       fprintf(stderr, "Error: Agent is an incompatible version\n");
00062     else
00063       fprintf(stderr, "Error: Agent refused with code %d\n", tag);
00064     return(NULL);
00065   }
00066 
00067   if(gs_recv_int(sock, &num_servers) < 0) {
00068     fprintf(stderr,"Error communicating with agent.\n");
00069     return(NULL);
00070   }
00071 
00072 
00073   *num = num_servers;
00074   if(num_servers <= 0)
00075     return(NULL);
00076 
00077   server_list = (gs_server_t **) CALLOC(num_servers, sizeof(gs_server_t *));
00078   if(!server_list) {
00079     fprintf(stderr,"Failed to allocate memory for the server list.\n");
00080     return(NULL);
00081   }
00082 
00083   for(i=0;i<num_servers;i++) {
00084     server_list[i] = (gs_server_t *) CALLOC(1,sizeof(gs_server_t));
00085     if(gs_recv_string(sock, &msg) < 0) {
00086       fprintf(stderr,"Error communicating with agent.\n");
00087       return(NULL);
00088     }
00089     if(gs_decode_server(msg, server_list[i]) < 0) {
00090       fprintf(stderr,"Error decoding server information.\n");
00091       return(NULL);
00092     }
00093     FREE(msg);
00094   }
00095 
00096   return server_list;
00097 }
00098 
00106 int
00107 gs_kill_server(char *agent_name, int agentport, char *server_name)
00108 {
00109   int sock, server_sock, tag, by_cid;
00110   gs_server_t *gs_server = NULL;
00111   char *msg, *password;
00112 
00113   by_cid = strncmp(server_name, "-c", 2) == 0;
00114 
00115   printf("GridSolve Server password for %s", by_cid ? server_name+2 : server_name);
00116 
00117   password = gs_get_password(": ");
00118 
00119   if(!password) {
00120     fprintf(stderr, "Error reading password.\n");
00121     return(-1);
00122   }
00123 
00124   if((sock = gs_connect_direct(agent_name, agentport)) == INVALID_SOCKET) {
00125     fprintf(stderr, "%s cannot be contacted\n", agent_name);
00126     return(-1);
00127   }
00128 
00129   if((gs_send_tag(sock, GS_PROT_KILL_SERVER) < 0) ||
00130      (gs_send_string(sock, VERSION) < 0)) {
00131     fprintf(stderr, "Error communicating with agent.\n");
00132     return(-1);
00133   }
00134 
00135   if(gs_recv_tag(sock, &tag) < 0) {
00136     fprintf(stderr, "Error communicating with agent.\n");
00137     return(-1);
00138   }
00139 
00140   if(tag != GS_PROT_OK) {
00141     if(tag == GS_PROT_VERSION_MISMATCH)
00142       fprintf(stderr, "Error: Agent is an incompatible version\n");
00143     else
00144       fprintf(stderr, "Error: Agent refused with code %d\n", tag);
00145     return(-1);
00146   }
00147 
00148   if(gs_send_string(sock, server_name) < 0) {
00149     fprintf(stderr, "Unsuccessful (Sending Server)\n");
00150     return(-1);
00151   }
00152 
00153   if(gs_recv_tag(sock,&tag) < 0) {
00154     fprintf(stderr, "Unsuccessful (receiving response)\n");
00155     return(-1);
00156   }
00157 
00158   if(tag != GS_PROT_OK) {
00159     if(tag == GS_PROT_UNKNOWN_SERVER) {
00160       if(by_cid) {
00161         fprintf(stderr, "No Server with component id '%s'.\n", server_name+2);
00162         fprintf(stderr, "Try specifying the component id exactly as it is ");
00163         fprintf(stderr, "displayed by GS_config\n");
00164       }
00165       else {
00166         fprintf(stderr, "No Server named '%s'.\n", server_name);
00167         fprintf(stderr, "Try specifying the hostname exactly as it is ");
00168         fprintf(stderr, "displayed by GS_config\n");
00169       }
00170     }
00171     else if(tag == GS_PROT_MULTIPLE_SERVER) {
00172       fprintf(stderr, "Multiple servers matched name '%s'.\n\n", server_name);
00173       fprintf(stderr, "Disambiguate by specifying the component id (cid) ");
00174       fprintf(stderr, "using\nthe -c flag to GS_killserver.  For example:\n\n");
00175       fprintf(stderr, "   GS_killserver -c agent_name 20206a66691be13f\n");
00176       fprintf(stderr, "\nGS_config will display the component ids.\n");
00177     }
00178     else
00179       fprintf(stderr, "Unspecified error killing server '%s'.\n", 
00180          by_cid ? (server_name + 2) : server_name);
00181 
00182     return(-1);
00183   }
00184 
00185   gs_server = (gs_server_t *)calloc(1, sizeof(gs_server_t));
00186   if(!gs_server) {
00187     fprintf(stderr, "Unsuccessful (Allocating Server Memory)\n");
00188     return(-1);
00189   }
00190 
00191   if(gs_recv_string(sock, &msg) < 0) {
00192     fprintf(stderr, "Unsuccessful receiving server structure.\n");
00193     return(-1);
00194   }
00195 
00196   if(gs_decode_server(msg, gs_server) <0){
00197     fprintf(stderr, "Unsuccessful (Decoding Server)\n");
00198     return(-1);
00199   }
00200 
00201   server_sock = gs_connect_to_host(gs_server->componentid, gs_server->ipaddress,
00202      gs_server->port, gs_server->proxyip, gs_server->proxyport);
00203 
00204   if(server_sock < 0) {
00205     fprintf(stderr, "Unsuccessful (connecting server)\n");
00206     gs_server_free(gs_server);
00207     return(-1);
00208   }
00209 
00210   if((gs_send_tag(server_sock, GS_PROT_KILL_SERVER) < 0) ||
00211      (gs_send_string(server_sock, VERSION) < 0)) {
00212     fprintf(stderr, "Unsuccessful (sending tag to Server)\n");
00213     gs_server_free(gs_server);
00214     return(-1);
00215   }
00216 
00217   if(gs_recv_tag(server_sock, &tag) < 0) {
00218     fprintf(stderr, "Error communicating with server.\n");
00219     return(-1);
00220   }
00221 
00222   if(tag != GS_PROT_OK) {
00223     if(tag == GS_PROT_VERSION_MISMATCH)
00224       fprintf(stderr, "Error: Server is an incompatible version\n");
00225     else
00226       fprintf(stderr, "Error: Server refused with code %d\n", tag);
00227     return(-1);
00228   }
00229 
00230   if(gs_send_string(server_sock, password) < 0) {
00231     fprintf(stderr, "Unsuccessful (Sending Password)\n");
00232     gs_server_free(gs_server);
00233     return(-1);
00234   }
00235 
00236   if(gs_recv_tag(server_sock,&tag) < 0) {
00237     fprintf(stderr, "Unsuccessful (receiving response)\n");
00238     gs_server_free(gs_server);
00239     return(-1);
00240   }
00241 
00242   if(tag == GS_PROT_OK) {
00243     printf("Password Accepted and Successfully Killed\n");
00244 
00245     if(gs_send_tag(sock, GS_PROT_OK) < 0) {
00246       fprintf(stderr, "Unsuccessful (Sending Confirmation to Agent)\n");
00247       gs_server_free(gs_server);
00248       return(-1);
00249     }
00250   }
00251   else {
00252     printf("Password Not Accepted and Not Killed\n");
00253 
00254     if(gs_send_tag(sock, GS_PROT_ERROR) < 0) {
00255       fprintf(stderr, "Unsuccessful (Sending Confirmation to Agent)\n");
00256       gs_server_free(gs_server);
00257       return(-1);
00258     }
00259   }
00260 
00261   gs_server_free(gs_server);
00262 
00263   return 0;
00264 }
00265 
00277 char *
00278 gs_get_password(char *prompt)
00279 {
00280   struct termios initialrsettings, newrsettings;
00281   char *password;
00282 
00283   if(prompt)
00284     printf("%s", prompt);
00285 
00286   tcgetattr(fileno(stdin), &initialrsettings);
00287   newrsettings = initialrsettings;
00288   newrsettings.c_lflag &= ~ECHO;
00289 
00290   if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0)
00291     return NULL;
00292   else {
00293     password = gs_get_line(stdin);
00294 
00295     tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
00296 
00297     if(!password)
00298       return NULL;
00299 
00300     if(strlen(password) > 0)
00301       password[strlen(password)-1] = 0;
00302   }
00303 
00304   printf("\n");
00305 
00306   return password;
00307 }
00308 
00318 int
00319 gs_kill_agent(char *name, int agentport)
00320 {
00321   int sock, tag;
00322   char *password;
00323 
00324   if((sock = gs_connect_direct(name, agentport)) == INVALID_SOCKET) {
00325     fprintf(stderr,"%s cannot be contacted\n", name);
00326     return(-1);
00327   }
00328 
00329   if((gs_send_tag(sock, GS_PROT_KILL_AGENT) < 0) ||
00330      (gs_send_string(sock, VERSION) < 0)) {
00331     fprintf(stderr,"Error communicating with agent.\n");
00332     return(-1);
00333   }
00334 
00335   if(gs_recv_tag(sock, &tag) < 0) {
00336     fprintf(stderr,"Error communicating with agent.\n");
00337     return(-1);
00338   }
00339 
00340   if(tag != GS_PROT_OK) {
00341     if(tag == GS_PROT_VERSION_MISMATCH)
00342       fprintf(stderr, "Error: Agent is an incompatible version\n");
00343     else
00344       fprintf(stderr, "Error: Agent refused with code %d\n", tag);
00345     return(-1);
00346   }
00347 
00348   password = gs_get_password("GridSolve Agent Password: ");
00349 
00350   if(!password) {
00351     fprintf(stderr,"Error reading password.\n");
00352     return(-1);
00353   }
00354 
00355   if(gs_send_string(sock, password) < 0) {
00356     fprintf(stderr,"Error sending password\n");
00357     return(-1);
00358   }
00359 
00360   if(gs_recv_tag(sock, &tag) < 0) {
00361     fprintf(stderr,"Error receving confirmation tag\n");
00362     return(-1);
00363   }
00364 
00365   if(tag != GS_PROT_OK) {
00366     fprintf(stderr, "Not Killed, error code = %d\n", tag);
00367     return(-1);
00368   }
00369 
00370   printf("Successfully Killed\n");
00371 
00372   return(0);
00373 }