Member Avatar for CathShadow
CathShadow

Well, to give some background on the app, it's a program (service) that runs in a loop, collects data about the PC its running on (users connected, CPU load, network traffic) etc. It then opens "plink" and sends the XML data accross to a perl file which processes and inserts it to a DB. the problem is that at times when I open a handle to say plink, the handle gets to "read", and stays there, which means that by the time it gets to the next run in the loop it's still trying to 'read', so the failsafe mechanism kicks in and collects data but doesn't send it, (and plink NEVER closes)...

I've been trying for 2 months to solve it, tried a few different things but I land up actually crashing the agent more than anything else:

string commandrun = "plink -ssh -C -2 -i id.ppk fwstats@";
	commandrun += IP;
	commandrun += " /usr/local/mss/server/mdf_transport.pl ";
	commandrun += to_string(timestamp);
	commandrun += " ";
	commandrun += hostname;
	testhandler::transportcommand = commandrun;

...

openexec * oe = new openexec(transportcommand.c_str());
	int filesize = 0;
	FILE *infilesize;

	infilesize=fopen(filename.c_str(),"rt");
	fseek(infilesize,0,SEEK_END);
	filesize=ftell(infilesize);
	fclose(infilesize);

	oe->printf("raw cc %d MDF\n",filesize);
	char tbuf[256];
	FILE *infile;

	infile=fopen(filename.c_str(),"rb");
	int size=fread(tbuf,1,256,infile);
	testhandler_log.log_write_line("Size: ");
	testhandler_log.log_write_line_nodate(to_string(size));
	testhandler_log.log_write_newline();

	while (size)
	{
		oe->write(tbuf,size);

	    size=fread(tbuf,1,256,infile);

		testhandler_log.log_write_line("Size: ");
		testhandler_log.log_write_line_nodate(to_string(size));
		testhandler_log.log_write_newline();

	}
	fclose(infile);


	oe->close();
	oe->readline(tbuf,140);
	testhandler_log.log_write_line(tbuf);
	testhandler_log.log_write_newline();
	if (strcmp("SUCCESS",tbuf)==0)
	{
		unlink(filename.c_str());
	}

	delete oe;
int openexec::readline(char * str,const int maxlen)
{
	openexec_log.log_write_line("Read from process\n");
    int count=0;
    char temp;

    //  fprintf(mssout,"readline started\n");

    while (count<maxlen)
    {
        if (chBufPos==chBufSize)
        {
            // Fill up the Buffer
            //      fprintf(mssout,"readFile\n");
            //      DWORD ec;

            //      GetExitCodeProcess(piProcInfo.hProcess,&ec);
            //      if (ec!=STILL_ACTIVE)
#ifdef __GNUC__
            CloseHandle(hChildStdoutWr);
#else

            __try
            {
                CloseHandle(hChildStdoutWr);
            }
            __except (true)
            {}
#endif

#ifndef __GNUC__

            __try {
#endif
                if (!HitEOF)
            {
                //        fprintf(logout,"1\n");
                if (!ReadFile(hChildStdoutRdDup, chBuf, BUFSIZE, &chBufSize, NULL) || chBufSize==0)
                    {
                        HitEOF=true;
                        chBufSize=0;
                    }
                }
                else
                    chBufSize=0;
#ifndef __GNUC__

                }
                __except(true)
            {
                HitEOF=true;
                chBufSize=0;
            }
#endif
            //      else chBufSize=0;

            //      fprintf(mssout,"Done\n");
            chBufPos=0;
            if (chBufSize==0)
            {
                //        fprintf(logout,"2\n");
                str[count]=0;
                if (count==0)
                    return -1;
                return count;
            }
        }
        temp=chBuf[chBufPos];

        if (temp=='\n')
        {
            str[count]=0;
            ++chBufPos;
            return count;
        }
        else
            str[count]=temp;
        ++count;
        ++chBufPos;
    }
    str[maxlen-1]=0;
    return maxlen-1;

}

The output of the app is as follows:

2008-10-23 11:25:31     openexec        Done Creating Child Process
2008-10-23 11:25:31     openexec        Write to process: raw cc 2497 MDF

2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 256
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 193
2008-10-23 11:25:31     openexec        Write to process
2008-10-23 11:25:31     testhandler     Size: 0
2008-10-23 11:25:31     openexec        Close Handle
2008-10-23 11:25:31     openexec        Read from process
2008-10-23 11:25:32     testhandler     SUCCESS

This is the successful ones; unsuccessful looks as follows:

2008-10-28 0:00:06 Info lock is on (aborting)

2008-10-28 0:00:06     openexec        Done Creating Child Process
2008-10-28 0:00:06     openexec        Write to process: raw cc 2497 MDF

2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 256
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 193
2008-10-28 0:00:06     openexec        Write to process
2008-10-28 0:00:06     testhandler     Size: 0
2008-10-28 0:00:06     openexec        Close Handle
2008-10-28 0:00:06     openexec        Read from process

So it looks like it gets stuck on the handle (so when the next thread tries to run it can't because its still open so it aborts:

2008-10-28 0:05:06	Info lock is on (aborting)

Do you have any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.