uintptr_t CPROC read_ahead( PTHREAD thread ) { struct params * p = (struct params *)GetThreadParam( thread ); uint8_t* p_tmp = p->p; uintptr_t n; int a; for( n = 0; n <p->size; n += 4096 ) { a = p_tmp[0]; p_tmp += 4096; } return 0; }
static uintptr_t CPROC ConsoleInputThread( PTHREAD thread ) { PCONSOLE_INFO pdp = (PCONSOLE_INFO)GetThreadParam( thread ); INPUT_RECORD irInBuf[128]; //PTEXT pCommand; DWORD cNumRead, c; hInput = pdp->consolecon.hStdin; while( !gbExitThread ) { if (! ReadConsoleInput( pdp->consolecon.hStdin, // input buffer handle irInBuf, // buffer to read into 128, // size of read buffer &cNumRead) ) // number of records read { MessageBox( NULL, "Bad things happened on console input...", "ERROR", MB_OK ); } for( c = 0; c < cNumRead; c++ ) { switch(irInBuf[c].EventType) { case KEY_EVENT: // keyboard input // KeyboardState[irInBuf[c].Event.KeyEvent.wVirtualKeyCode] = // irInBuf[c].Event.KeyEvent.bKeyDown; KeyEventProc(pdp, irInBuf[c].Event.KeyEvent); break; /* case WINDOW_BUFFER_SIZE_EVENT: irInBuf[c].Event.WindowBufferSizeEvent.dwSize.Y = 50; SetConsoleScreenBufferSize( consolecon.hStdout, irInBuf[c].Event.WindowBufferSizeEvent.dwSize ); DebugBreak(); break; */ default: //DebugBreak(); break; } } } Close( pdp ); ghInputThread = NULL; ExitThread( 0xE0F ); return 0; }
static uintptr_t CPROC HandleTaskOutput(PTHREAD thread ) { PTASK_INFO task = (PTASK_INFO)GetThreadParam( thread ); { task->pOutputThread = thread; // read input from task, montiro close and dispatch TaskEnd Notification also. { PHANDLEINFO phi = &task->hStdOut; PTEXT pInput = SegCreate( 4096 ); int done, lastloop; Hold( task ); done = lastloop = FALSE; do { uint32_t dwRead, dwAvail; if( done ) lastloop = TRUE; #ifdef _WIN32 //while( ) { #else while( CanRead( phi->handle ) ) #endif { if( task->flags.log_input ) lprintf( WIDE( "Go to read task's stdout." ) ); #ifdef _WIN32 if( !task->flags.process_ended && ReadFile( phi->handle , GetText( pInput ), (DWORD)(GetTextSize( pInput ) - 1) , (LPDWORD)&dwRead, NULL ) ) //read the pipe { #else dwRead = read( phi->handle , GetText( pInput ) , GetTextSize( pInput ) - 1 ); if( !dwRead ) { #ifdef _DEBUG //lprintf( WIDE( "Ending system thread because of broke pipe! %d" ), errno ); #endif #ifdef WIN32 continue; #else //lprintf( WIDE( "0 read = pipe failure." ) ); break; #endif } #endif if( task->flags.log_input ) lprintf( WIDE( "got read on task's stdout: %d" ), dwRead ); if( task->flags.bSentIoTerminator ) { if( dwRead > 1 ) dwRead--; else { if( task->flags.log_input ) lprintf( WIDE( "Finished, no more data, task has ended; no need for once more around" ) ); lastloop = 1; break; // we're done; task ended, and we got an io terminator on XP } } //lprintf( WIDE( "result %d" ), dwRead ); GetText( pInput )[dwRead] = 0; pInput->data.size = dwRead; //LogBinary( GetText( pInput ), GetTextSize( pInput ) ); if( task->OutputEvent ) { task->OutputEvent( task->psvEnd, task, GetText( pInput ), GetTextSize( pInput ) ); } pInput->data.size = 4096; #ifdef _WIN32 } else { DWORD dwError = GetLastError(); if( ( dwError == ERROR_OPERATION_ABORTED ) && task->flags.process_ended ) { if( PeekNamedPipe( phi->handle, NULL, 0, NULL, (LPDWORD)&dwAvail, NULL ) ) { if( dwAvail > 0 ) { lprintf( WIDE( "caught data" ) ); // there is still data in the pipe, just that the process closed // and we got the sync even before getting the data. } else break; } } } #endif } #ifdef _WIN32 } #endif //allow a minor time for output to be gathered before sending // partial gathers... if( task->flags.process_ended ) #ifdef _WIN32 { // Ending system thread because of process exit! done = TRUE; } #else //if( !dwRead ) // break; if( task->pid == waitpid( task->pid, NULL, WNOHANG ) ) { Log( WIDE( "Setting done event on system reader." ) ); done = TRUE; // do one pass to make sure we completed read } else { //lprintf( WIDE( "process active..." ) ); Relinquish(); } #endif } while( !lastloop ); #ifdef _DEBUG if( lastloop ) { //DECLTEXT( msg, WIDE( "Ending system thread because of process exit!" ) ); //EnqueLink( phi->pdp->&ps->Command->Output, &msg ); } else { //DECLTEXT( msg, WIDE( "Guess we exited from broken pipe" ) ); //EnqueLink( phi->pdp->&ps->Command->Output, &msg ); } #endif LineRelease( pInput ); #ifdef _WIN32 CloseHandle( task->hReadIn ); CloseHandle( task->hReadOut ); CloseHandle( task->hWriteIn ); CloseHandle( task->hWriteOut ); //lprintf( WIDE( "Closing process handle %p" ), task->pi.hProcess ); phi->hThread = 0; #else //close( phi->handle ); close( task->hStdIn.pair[1] ); close( task->hStdOut.pair[0] ); //close( task->hStdErr.pair[0] ); #define INVALID_HANDLE_VALUE -1 #endif if( phi->handle == task->hStdIn.handle ) task->hStdIn.handle = INVALID_HANDLE_VALUE; phi->handle = INVALID_HANDLE_VALUE; task->pOutputThread = NULL; Release( task ); //WakeAThread( phi->pdp->common.Owner ); return 0xdead; } } }