--- projects/cms/source/ihost/ihost.c 2002/05/12 14:10:45 1.5 +++ projects/cms/source/ihost/ihost.c 2002/05/12 17:33:21 1.6 @@ -44,7 +44,6 @@ int ihost_configure(ihost_state_t *ihost_state){ char *reply_ptr; /* Check to see if anything needs to be free'd */ - if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); @@ -92,20 +91,24 @@ int ihost_configure(ihost_state_t *ihost_state){ errf("Server error (%m)"); return -1; } - if((ihost_state->last_modified=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + if((ihost_state->last_modified=malloc((strlen(reply))+1)) == NULL){ + errf("malloc failed (%m)"); return -1; } + strcpy(ihost_state->last_modified, reply); + ihost_state->last_modified[(strlen(ihost_state->last_modified))]='\n'; reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST\n"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ errf("Server error (%m)"); return -1; } - if((ihost_state->files_list=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); - return -1; - } + if((ihost_state->files_list=malloc((strlen(reply))+1)) == NULL){ + errf("malloc failed (%m)"); + return -1; + } + strcpy(ihost_state->files_list, reply); + ihost_state->files_list[(strlen(ihost_state->files_list))]='\n'; reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN\n"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ @@ -229,17 +232,18 @@ int heartbeat(ihost_state_t *ihost_state){ } reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT\n"); - if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { errf("Server error"); return -1; } - + if (ihost_state->fm_host!=NULL) free(ihost_state->fm_host); reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG\n"); - if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { errf("Server error"); return -1; } + printf("filelist %s\n",ihost_state->files_list); reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { errf("Server error"); @@ -251,35 +255,49 @@ int heartbeat(ihost_state_t *ihost_state){ errf("Server error"); return -1; } - if (strncasecmp(reply, "ERROR", 2) == 0){ + if (strncasecmp(reply, "ERROR", 5) == 0){ /* Means the config has changed */ exitcode=RECONFIGURE_RETURN_CODE; } reply=sock_comm(fm_fd_r, fm_fd_w, "KEY\n"); - if ((reply==NULL) || (strncasecmp(reply, "ERROR", 2) == 0) ) { + if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { errf("Server error"); return -1; } if (ihost_state->key!=NULL) free(ihost_state->key); - if((ihost_state->key=strdup(reply)) == NULL){ - errf("strdup failed (%m)"); + if((ihost_state->key=malloc(strlen(reply)+1)) == NULL){ + errf("malloc failed (%m)"); return -1; } + strcpy(ihost_state->key, reply); + ihost_state->key[(strlen(ihost_state->key))]='\n'; - reply=sock_comm(fm_fd_r, fm_fd_w, "END\n"); + reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT\n"); if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ errf("Server error (%m)"); return -1; } + if(fclose(fm_fd_r) !=0){ + errf("Failed to close FD (%m)"); + return -1; + } + if(fclose(fm_fd_w) !=0){ + errf("Failed to close FD (%m)"); + return -1; + } + return exitcode; } int main(int argc, char **argv){ ihost_state_t ihost_state; + int heartbeat_exit; + int counter=0; + /* NULL'ify so i can tell if i need to free it or not */ ihost_state.fm_host=NULL; ihost_state.my_fqdn=NULL; @@ -299,9 +317,26 @@ int main(int argc, char **argv){ if(ihost_configure(&ihost_state)!=0){ errf("configure failed"); + /* Ok, ideally we prob should have 2 copies of the structure and carry on if this + happens.. But we dont :) (at the moment) */ + exit(1); } + while(TRUE){ + heartbeat_exit=heartbeat(&ihost_state); + if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ + errf("heartbeat needs to be reconfigured"); + ihost_configure(&ihost_state); + } + if(heartbeat_exit==-1){ + errf("ah crap"); + exit(1); + } + printf("Count : %d\n",counter++); + printf("waiting %d\n",ihost_state.tcp_update_time); + sleep(ihost_state.tcp_update_time); + } return 0; }