www.libssh2.org | Daily snapshots | Mailing list archive | Docs | Examples

Archive Index This month's Index

Subject: Re: [libssh2] #196: using libssh2_channel_read crushes

Re: [libssh2] #196: using libssh2_channel_read crushes

From: libssh2 Trac <trac_at_libssh2.stuge.se>
Date: Thu, 11 Nov 2010 09:31:39 -0000

#196: using libssh2_channel_read crushes
---------------------------------------------------------------------------------------+
  Reporter: www.google.com/accounts/o8/id?id=aitoawk50cbnsiuuqgylsi_oyi14_ymtafn1cjc | Owner: bagder
      Type: defect | Status: accepted
  Priority: high | Milestone: 1.2.7
 Component: API | Version: 1.2.7
Resolution: | Keywords:
    Blocks: | Blocked By:
---------------------------------------------------------------------------------------+

Comment (by www.google.com/accounts/o8/id?id=aitoawk50cbnsiuuqgylsi_oyi14_ymtafn1cjc):

 i did not added all the code in order to not confuse with my code,
 so below is code that relevant to Libssh2, other code initialize 5
 Threads,
 and invokes this below code (note every thread has a seperate connection
 and libssh2 vars) :
  ....
  // Here we are transfering 40 files to a target machine via SFTP
  for(int i=0;i<40;i++)
    {

        cout<<"Transfering File:"<<i<<endl;
        string number = static_cast<ostringstream*>( &(ostringstream() <<
 i) )->str();
        //fprintf(stderr,"Line 1 ");
        buf_size = strlen(buffer_to_send);
        //fprintf(stderr,"Line 2 ");
        //file = fopen((file_name+number).c_str(),"rb");
        ifstream file;
        file.open((file_name+number).c_str(),ifstream::out);
        if(!file.good())
        {
           cout<<"Error , Could not open file"<<endl;
           return NULL;
        }
        cout<<"File Opened Sucessfully"<<endl;
        sftp_handle =
         libssh2_sftp_open(sftp_session, (string(sftppath) + file_name +
 number + string("__") + thread_id_str).c_str(),
 LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
                       LIBSSH2_SFTP_S_IRUSR|LIBSSH2_SFTP_S_IWUSR|
                       LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IROTH);
        if(sftp_handle == NULL)
        {
          char *errmsg;
          int errlen;
          int err = libssh2_session_last_error(session, &errmsg, &errlen,
 0);
          fprintf(stdout, "Unable to open FILE on a session: (%d) %s\n",
 err, errmsg);
          return NULL;
        }

        char mem[32500];
        size_t nread;
        char *ptr;
        int rc;

        cout<<"Handling file :"<<file_name+number<<endl;
        do {
         if(file.eof())
         {
             cout<<"File Sent!! Reached End of File"<<endl;
             break;
         }
         else if(!file.good())
         {
             cout<<"Error reading the source file"<<endl;
             break;
         }
         else
         {
            file.read(mem,sizeof(mem));
            nread = file.gcount();
            cout<<"Sucessfully Read :"<<nread<<" Bytes"<<endl;
         }
         ptr = mem;

         do {
             /* write data in a loop until we block */
             rc = libssh2_sftp_write(sftp_handle, ptr, nread);
             if(rc < 0)
             {
                 cout<<"Error Sending a file"<<endl;
                 //cout<<"Error Code: "<<rc<<endl;
                 char *errmsg;
                 int errlen;
                 int err = libssh2_session_last_error(session, &errmsg,
 &errlen, 0);
                 fprintf(stdout, "Unable to open a session: (%d) %s\n",
 err, errmsg);
             }
             else
             {
                 cout<<" Bytes Sent:"<<rc<<endl;
             }
             ptr += rc;
             nread -= rc;
         } while (nread);

         } while (rc > 0);

        file.close();
    }
 // Ok we finished transfering 40 files now lets go to invoke some command
 // and then we will call libssh2_channel_read , NOTE that this code causes
 the corruption
 // as described above

 pthread_mutex_lock(my_mutex);

    LIBSSH2_CHANNEL *channel = NULL;
    if((channel = libssh2_channel_open_session(session)) == NULL )
    {
        cout<<"Error Allocating a new channel"<<endl;
    }
    else
    {
        cout<<"Sucess Allocating new channel for the curren Session"<<endl;
        int rc = libssh2_channel_exec(channel, "mkdir
 /var/cti/smsc/TestTheCommmand");
        if(rc != 0)
        {
            cout<<"Command sent to Server failed"<<endl;
        }
        else
        {
            cout<<"Command sent to Server Suceeded"<<endl;
        }
        libssh2_channel_close(channel);
        libssh2_channel_free(channel);
    }
    int rc;
    int bytecount;
    do
    {
        char buffer[0x4000];
        cout<<"Before Block"<<endl;
        rc = libssh2_channel_read( channel, buffer, sizeof(buffer) );
        cout<<"After Block"<<endl;
        if( rc > 0 )
        {
             int i;
             bytecount += rc;
             fprintf(stdout, "We read:\n");
             for( i=0; i < rc; ++i )
                  fputc( buffer[i], stdout);
               fprintf(stdout, "\n");
        }
  else
        {
             fprintf(stdout, "libssh2_channel_read returned %d\n", rc);
        }

    }while( rc > 0);
    pthread_mutex_unlock(my_mutex);

    finalize_connection(session,sftp_session,sftp_handle,sock);

-- 
Ticket URL: <http://trac.libssh2.org/ticket/196#comment:2>
libssh2 <http://trac.libssh2.org/>
C library for writing portable SSH2 clients
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-11-11

the libssh2 team