Changeset 854 for branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c
- Timestamp:
- Nov 21, 2017, 10:24:14 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/prototype-v0/zoo-project/zoo-kernel/sshapi.c
r851 r854 29 29 30 30 #include "sshapi.h" 31 #include "service_internal.h" 31 32 32 33 SSHCON *sessions[MAX_PARALLEL_SSH_CON]; … … 94 95 int port=22; 95 96 96 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 97 fflush(stderr); 98 99 map* hpc_host=getMapFromMaps(conf,"HPC","host"); 100 map* hpc_port=getMapFromMaps(conf,"HPC","port"); 101 map* hpc_user=getMapFromMaps(conf,"HPC","user"); 102 map* hpc_password=getMapFromMaps(conf,"HPC","password"); 103 map* hpc_public_key=getMapFromMaps(conf,"HPC","key"); 104 105 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 106 fflush(stderr); 97 map* hpc_config=getMapFromMaps(conf,"lenv","configId"); 98 99 map* hpc_host=getMapFromMaps(conf,hpc_config->value,"ssh_host"); 100 map* hpc_port=getMapFromMaps(conf,hpc_config->value,"ssh_port"); 101 map* hpc_user=getMapFromMaps(conf,hpc_config->value,"ssh_user"); 102 map* hpc_password=getMapFromMaps(conf,hpc_config->value,"ssh_password"); 103 map* hpc_public_key=getMapFromMaps(conf,hpc_config->value,"ssh_key"); 104 105 char ip[100]; 106 struct hostent *my_hostent; 107 struct in_addr **addrs; 108 109 if (hpc_host != NULL) { 110 // Fetch ip address for the hostname 111 if ( (my_hostent = gethostbyname( hpc_host->value ) ) == NULL){ 112 herror("gethostbyname"); 113 setMapInMaps(conf,"lenv","message",_("Issue when invoking gethostbyname!")); 114 return NULL; 115 } 116 117 addrs = (struct in_addr **) my_hostent->h_addr_list; 118 119 for(i = 0; addrs[i] != NULL; i++) { 120 strcpy(ip , inet_ntoa(*addrs[i]) ); 121 break; 122 } 123 } 107 124 108 125 #ifdef WIN32 … … 117 134 #endif 118 135 119 // TODO: fetch ip address for the hostname120 136 if (hpc_host != NULL) { 121 hostaddr = inet_addr( hpc_host->value);137 hostaddr = inet_addr(ip); 122 138 } else { 123 139 setMapInMaps(conf,"lenv","message","No host parameter found in your main.cfg file!\n"); … … 235 251 int ssh_get_cnt(maps* conf){ 236 252 int result=0; 237 map* myMap=getMapFromMaps(conf,"lenv"," cnt_session");253 map* myMap=getMapFromMaps(conf,"lenv","nb_sessions"); 238 254 if(myMap!=NULL){ 239 255 result=atoi(myMap->value); … … 323 339 return false; 324 340 } 325 341 326 342 do { 327 343 sessions[cnt]->sftp_session = libssh2_sftp_init(sessions[cnt]->session); … … 391 407 * @param targetPath const char* defining the path for accessing the file on the 392 408 * remote host 393 * @return true in case of success, falseif failure occured409 * @return 0 in case of success, -1 if failure occured 394 410 */ 395 411 int ssh_fetch(maps* conf,const char* localPath,const char* targetPath,int cnt){ … … 414 430 415 431 fprintf(stderr, "Unable to init SFTP session\n"); 416 return false;432 return -1; 417 433 } 418 434 } while (!sessions[cnt]->sftp_session); 435 do { 436 sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath, 437 LIBSSH2_FXF_READ, 0); 438 if (!sftp_handle) { 439 if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) { 440 fprintf(stderr, " ** Unable to open file with SFTP\n"); 441 return -1; 442 } 443 else { 444 //non-blocking open 445 waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 446 } 447 } 448 } while (!sftp_handle); 449 450 int result=0; 451 do { 419 452 do { 420 sftp_handle = libssh2_sftp_open(sessions[cnt]->sftp_session, targetPath, 421 422 LIBSSH2_FXF_READ, 0); 423 424 if (!sftp_handle) { 425 if (libssh2_session_last_errno(sessions[cnt]->session) != LIBSSH2_ERROR_EAGAIN) { 426 fprintf(stderr, "Unable to open file with SFTP\n"); 427 return -1; 428 } 429 else { 430 fprintf(stderr, "non-blocking open\n"); 431 waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 432 } 433 } 434 } while (!sftp_handle); 435 436 int result=1; 437 do { 438 do { 439 rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); 440 /*fprintf(stderr, "libssh2_sftp_read returned %d\n", 441 rc);*/ 442 if(rc > 0) { 443 //write(2, mem, rc); 444 fwrite(mem, rc, 1, local); 445 } 446 } while (rc > 0); 447 448 if(rc != LIBSSH2_ERROR_EAGAIN) { 449 result=-1; 450 break; 451 } 452 453 struct timeval timeout; 454 fd_set fd; 455 timeout.tv_sec = 10; 456 timeout.tv_usec = 0; 457 458 FD_ZERO(&fd); 459 460 FD_SET(sessions[cnt]->sock_id, &fd); 461 462 rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout); 463 if(rc <= 0) { 464 if(rc==0) 465 fprintf(stderr, "SFTP download timed out: %d\n", rc); 466 else 467 fprintf(stderr, "SFTP download error: %d\n", rc); 468 result=-1; 469 break; 470 } 471 472 } while (1); 453 rc = libssh2_sftp_read(sftp_handle, mem, sizeof(mem)); 454 /*fprintf(stderr, "libssh2_sftp_read returned %d\n", 455 rc);*/ 456 if(rc > 0) { 457 //write(2, mem, rc); 458 fwrite(mem, rc, 1, local); 459 } 460 } while (rc > 0); 461 462 if(rc != LIBSSH2_ERROR_EAGAIN) { 463 result=-1; 464 break; 465 } 466 467 struct timeval timeout; 468 fd_set fd; 469 timeout.tv_sec = 10; 470 timeout.tv_usec = 0; 471 472 FD_ZERO(&fd); 473 474 FD_SET(sessions[cnt]->sock_id, &fd); 475 476 rc = select(sessions[cnt]->sock_id+1, &fd, &fd, NULL, &timeout); 477 if(rc <= 0) { 478 if(rc==0) 479 fprintf(stderr, "SFTP download timed out: %d\n", rc); 480 else 481 fprintf(stderr, "SFTP download error: %d\n", rc); 482 return -1; 483 } 484 485 } while (1); 473 486 duration = (int)(time(NULL)-start); 474 487 fclose(local); 475 488 libssh2_sftp_close_handle(sftp_handle); 476 477 489 libssh2_sftp_shutdown(sessions[cnt]->sftp_session); 478 490 return 0; … … 491 503 int exitcode; 492 504 char *exitsignal=(char *)"none"; 505 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 506 fflush(stderr); 493 507 while( (channel = libssh2_channel_open_session(sessions[cnt]->session)) == NULL && 494 508 libssh2_session_last_error(sessions[cnt]->session,NULL,NULL,0) == LIBSSH2_ERROR_EAGAIN ) { 509 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 510 fflush(stderr); 495 511 waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 496 512 } 513 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 514 fflush(stderr); 497 515 if( channel == NULL ){ 498 516 fprintf(stderr,"Error\n"); 499 return 1; 500 } 517 return -1; 518 } 519 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 520 fflush(stderr); 501 521 while( (rc = libssh2_channel_exec(channel, command)) == LIBSSH2_ERROR_EAGAIN ) { 522 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 523 fflush(stderr); 502 524 waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 503 525 } 526 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 527 fflush(stderr); 504 528 if( rc != 0 ) { 505 529 fprintf(stderr,"Error\n"); 506 530 return -1; 507 531 } 532 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 533 fflush(stderr); 508 534 509 535 map* tmpPath=getMapFromMaps(conf,"main","tmpPath"); … … 513 539 FILE* logFile=fopen(logPath,"wb"); 514 540 free(logPath); 541 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 542 fflush(stderr); 515 543 while(true){ 516 544 int rc; … … 535 563 break; 536 564 } 565 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 566 fflush(stderr); 537 567 fclose(logFile); 568 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 569 fflush(stderr); 538 570 exitcode = 127; 571 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 572 fflush(stderr); 539 573 while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN ) 540 574 waitsocket(sessions[cnt]->sock_id, sessions[cnt]->session); 575 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 576 fflush(stderr); 541 577 542 578 if( rc == 0 ) { … … 545 581 NULL, NULL, NULL, NULL, NULL); 546 582 } 583 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 584 fflush(stderr); 547 585 548 586 if (exitsignal) … … 550 588 else 551 589 fprintf(stderr, "\nEXIT: %d bytecount: %d\n", exitcode, bytecount); 590 fprintf(stderr,"%s %d\n",__FILE__,__LINE__); 591 fflush(stderr); 552 592 553 593 libssh2_channel_free(channel); … … 643 683 } 644 684 #ifdef DEBUG 645 fprintf(stderr," %s %d\n",__FILE__,__LINE__);685 fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__); 646 686 fflush(stderr); 647 687 dumpMaps(getMaps(*conf,"uploadQueue")); 648 fprintf(stderr," %s %d\n",__FILE__,__LINE__);688 fprintf(stderr,"*** %s %d\n",__FILE__,__LINE__); 649 689 fflush(stderr); 650 690 #endif … … 660 700 getMapArray(queueMaps->content,"targetPath",i) 661 701 }; 662 fprintf(stderr,"%s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value); 663 ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf)); 702 fprintf(stderr,"*** %s %d %s %s\n",__FILE__,__LINE__,argv[1]->value,argv[2]->value); 703 /**/zooLock* lck; 704 if((lck=lockFile(*conf,argv[1]->value,'w'))!=NULL){/**/ 705 if(ssh_copy(*conf,argv[1]->value,argv[2]->value,ssh_get_cnt(*conf))!=true){ 706 char* templateStr=_("Unable to copy over SSH the file requested for setting the value of %s."); 707 char *tmpMessage=(char*)malloc((strlen(templateStr)+strlen(argv[0]->value)+1)*sizeof(char)); 708 sprintf(tmpMessage,templateStr,argv[0]->value); 709 setMapInMaps(*conf,"lenv","message",tmpMessage); 710 free(tmpMessage); 711 unlockFile(*conf,lck); 712 return false; 713 } 714 /**/unlockFile(*conf,lck); 715 }else{ 716 setMapInMaps(*conf,"lenv","message",_("Unable to lock the file for upload!")); 717 return false; 718 }/**/ 664 719 } 665 720 }
Note: See TracChangeset
for help on using the changeset viewer.