void nuiSocket::SetNonBlocking(bool set) { if (mNonBlocking == set) return; int flags; mNonBlocking = set; /* If they have O_NONBLOCK, use the Posix way to do it */ #if defined(O_NONBLOCK) /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */ if (-1 == (flags = fcntl(mSocket, F_GETFL, 0))) flags = 0; if (set) flags |= O_NONBLOCK; else flags &= ~O_NONBLOCK; fcntl(mSocket, F_SETFL, flags); #else /* Otherwise, use the old way of doing it */ flags = set ? 1 : 0; int res = ioctl(mSocket, FIOBIO, &flags); DumpError(res, __FUNC__); #endif }
LeechCraft::Util::DBLock::~DBLock () { if (!Initialized_) return; if (Good_ ? !Database_.commit () : !Database_.rollback ()) DumpError (Database_.lastError ()); }
void LeechCraft::Util::DBLock::Init () { if (!Database_.transaction ()) { DumpError (Database_.lastError ()); throw std::runtime_error ("Could not start transaction"); } Initialized_ = true; }
bool nuiSocket::Init(int domain, int type, int protocol) { mSocket = socket(domain, type, protocol); #if (!defined _LINUX_) int n = 0; int res = setsockopt(mSocket, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n)); DumpError(res, __FUNC__); #endif return mSocket >= 0; }
void * __restrict FastAlloc::_Allocate(size_t dwSize) { OSEnterMutex(hAllocationMutex); //assert(dwSize); if(!dwSize) dwSize = 1; LPVOID lpMemory; Pool *pool; if(dwSize < 0x8001) { MemInfo *meminfo = GetMemInfo(dwSize); if(!meminfo->nextFree) //no pools have been created for this section { lpMemory = OSVirtualAlloc(0x10000); if (!lpMemory) DumpError(TEXT("Out of memory while trying to allocate %d bytes at %p"), dwSize, ReturnAddress()); Pool *&poollist = PoolList[PtrTo32(lpMemory)>>24]; if(!poollist) { poollist = (Pool*)OSVirtualAlloc(sizeof(Pool)*256); if (!poollist) DumpError(TEXT("Out of memory while trying to allocate %d bytes at %p"), dwSize, ReturnAddress()); zero(poollist, sizeof(Pool)*256); } pool = &poollist[(PtrTo32(lpMemory)>>16)&0xFF]; pool->lpMem = lpMemory; pool->bytesTotal = 0x10000; pool->meminfo = meminfo; pool->firstFreeMem = (FreeMemInfo*)lpMemory; pool->lastFreeMem = (FreeMemInfo*)lpMemory; meminfo->nextFree = (FreeMemInfo*)lpMemory; meminfo->nextFree->num = meminfo->maxBlocks; meminfo->nextFree->lpPool = pool; meminfo->nextFree->lpPrev = meminfo->nextFree->lpNext = NULL; }
bool nuiTCPServer::Bind(const nuiNetworkHost& rHost) { if (!Init(AF_INET, SOCK_STREAM, 0)) return false; int option = 1; int res = setsockopt(mSocket, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)); if (res) { printf("setsockopt error %d\n", errno); DumpError(this, res, __FUNC__); return false; } struct addrinfo* addr = nuiSocket::GetAddrInfo(rHost); res = bind(mSocket, addr->ai_addr, addr->ai_addrlen); if (res) DumpError(this, res, __FUNC__); freeaddrinfo(addr); return res == 0; }
bool nuiSocket::GetDistantHost(nuiNetworkHost& rHost) const { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int res = getpeername(mSocket, (struct sockaddr*)&addr, &addrlen); DumpError(res, __FUNC__); if (res != 0) return false; nuiNetworkHost h(ntohl(addr.sin_addr.s_addr), addr.sin_port, rHost.mProtocol); rHost = h; return true; }
nuiSocket::nuiSocket(nuiSocket::SocketType Socket) : mSocket(Socket), mpPool(NULL) { mNonBlocking = false; mMaxIdleTime = 0; // Default = do nothing about idling sockets #if (!defined _LINUX_) if (Socket != -1) { int n = 0; int res = setsockopt(mSocket, SOL_SOCKET, SO_NOSIGPIPE, &n, sizeof(n)); DumpError(res, __FUNC__); } #endif AddSocket(this); }
bool nuiTCPClient::Connect(const nuiNetworkHost& rHost) { if (!Init(AF_INET, SOCK_STREAM, 0)) return false; struct addrinfo* addr = nuiSocket::GetAddrInfo(rHost); int res = connect(mSocket, addr->ai_addr, addr->ai_addrlen); if (res) DumpError(this, res, __FUNC__); UpdateIdle(); freeaddrinfo(addr); mReadConnected = mWriteConnected = res == 0; return mReadConnected; }
void nuiSocket::Close() { if (mpPool) mpPool->Del(this); if (mSocket > 0) { int res = 0; // int res = shutdown(mSocket, SHUT_RDWR); // DumpError(res, __FUNC__); #ifdef WIN32 //DisconnectEx(mSocket, NULL, 0, 0); res = closesocket(mSocket); #else res = close(mSocket); #endif DumpError(res, __FUNC__); } mSocket = -1; }
int nuiTCPClient::Send(const uint8* pData, int len) { #ifdef WIN32 int res = send(mSocket, (const char*)pData, len, 0); #else int res = send(mSocket, pData, len, 0); #endif UpdateIdle(); if (res < 0) { if (errno == EWOULDBLOCK && mNonBlocking) return res; DumpError(this, res, __FUNC__); mWriteConnected = false; } else mSent += res; return res; }
// Run a program completely detached from the current process // it runs independantly. Program does not suspend until it completes. // No way at all to know if the program works or fails. SYSTEM_PROC( PTASK_INFO, LaunchPeerProgramExx )( CTEXTSTR program, CTEXTSTR path, PCTEXTSTR args , int flags , TaskOutput OutputHandler , TaskEnd EndNotice , uintptr_t psv DBG_PASS ) { PTASK_INFO task; if( program && program[0] ) { #ifdef WIN32 int launch_flags = ( ( flags & LPP_OPTION_NEW_CONSOLE ) ? CREATE_NEW_CONSOLE : 0 ) | ( ( flags & LPP_OPTION_NEW_GROUP ) ? CREATE_NEW_PROCESS_GROUP : 0 ) ; PVARTEXT pvt = VarTextCreateEx( DBG_VOIDRELAY ); PTEXT cmdline; PTEXT final_cmdline; LOGICAL needs_quotes; TEXTSTR expanded_path = ExpandPath( program ); char *new_path; TEXTSTR expanded_working_path = path?ExpandPath( path ):ExpandPath( WIDE(".") ); int first = TRUE; //TEXTCHAR saved_path[256]; task = (PTASK_INFO)AllocateEx( sizeof( TASK_INFO ) DBG_RELAY ); MemSet( task, 0, sizeof( TASK_INFO ) ); task->psvEnd = psv; task->EndNotice = EndNotice; if( l.ExternalFindProgram ) { new_path = l.ExternalFindProgram( expanded_path ); if( new_path ) { Release( expanded_path ); expanded_path = new_path; } } #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE("%s[%s]"), path, expanded_working_path ); #endif if( StrCmp( path, WIDE(".") ) == 0 ) { path = NULL; Release( expanded_working_path ); expanded_working_path = NULL; } if( expanded_path && StrChr( expanded_path, ' ' ) ) needs_quotes = TRUE; else needs_quotes = FALSE; if( needs_quotes ) vtprintf( pvt, WIDE( "\"" ) ); /* if( !IsAbsolutePath( expanded_path ) && expanded_working_path ) { //lprintf( "needs working path too" ); vtprintf( pvt, WIDE("%s/"), expanded_working_path ); } */ vtprintf( pvt, WIDE("%s"), expanded_path ); if( needs_quotes ) vtprintf( pvt, WIDE( "\"" ) ); if( flags & LPP_OPTION_FIRST_ARG_IS_ARG ) ; else { if( args && args[0] )// arg[0] is passed with linux programs, and implied with windows. args++; } while( args && args[0] ) { if( args[0][0] == 0 ) vtprintf( pvt, WIDE( " \"\"" ) ); else if( StrChr( args[0], ' ' ) ) vtprintf( pvt, WIDE(" \"%s\""), args[0] ); else vtprintf( pvt, WIDE(" %s"), args[0] ); first = FALSE; args++; } cmdline = VarTextGet( pvt ); vtprintf( pvt, WIDE( "cmd.exe /c %s" ), GetText( cmdline ) ); final_cmdline = VarTextGet( pvt ); VarTextDestroy( &pvt ); MemSet( &task->si, 0, sizeof( STARTUPINFO ) ); task->si.cb = sizeof( STARTUPINFO ); #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE( "quotes?%s path [%s] program [%s] [cmd.exe (%s)]"), needs_quotes?WIDE( "yes"):WIDE( "no"), expanded_working_path, expanded_path, GetText( final_cmdline ) ); #endif /* if( path ) { GetCurrentPath( saved_path, sizeof( saved_path ) ); SetCurrentPath( path ); } */ task->OutputEvent = OutputHandler; if( OutputHandler ) { SECURITY_ATTRIBUTES sa; sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof( sa ); CreatePipe( &task->hReadOut, &task->hWriteOut, &sa, 0 ); //CreatePipe( &hReadErr, &hWriteErr, &sa, 0 ); CreatePipe( &task->hReadIn, &task->hWriteIn, &sa, 0 ); task->si.hStdInput = task->hReadIn; task->si.hStdError = task->hWriteOut; task->si.hStdOutput = task->hWriteOut; task->si.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; if( !( flags & LPP_OPTION_DO_NOT_HIDE ) ) task->si.wShowWindow = SW_HIDE; else task->si.wShowWindow = SW_SHOW; } else { task->si.dwFlags |= STARTF_USESHOWWINDOW; if( !( flags & LPP_OPTION_DO_NOT_HIDE ) ) task->si.wShowWindow = SW_HIDE; else task->si.wShowWindow = SW_SHOW; } { HINSTANCE hShellProcess = 0; int success = 0; #ifdef WIN32 if( flags & LPP_OPTION_IMPERSONATE_EXPLORER ) { HANDLE hExplorer = GetImpersonationToken(); if( ( CreateProcessAsUser( hExplorer, NULL //program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, program , NULL // GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, WIDE( "cmd.exe" ) , GetText( final_cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) ) { success = 1; } CloseHandle( hExplorer ); } else #endif { if( ( CreateProcess( NULL //program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcess( program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcess( program , NULL // GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( TryShellExecute( task, expanded_working_path, program, cmdline ) ) || ( CreateProcess( NULL//WIDE( "cmd.exe" ) , GetText( final_cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || 0 ) { success = 1; } } if( success ) { //CloseHandle( task->hReadIn ); //CloseHandle( task->hWriteOut ); #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE("Success running %s[%s] in %s (%p): %d"), program, GetText( cmdline ), expanded_working_path, task->pi.hProcess, GetLastError() ); #endif if( OutputHandler ) { task->hStdIn.handle = task->hWriteIn; task->hStdIn.pLine = NULL; //task->hStdIn.pdp = pdp; task->hStdIn.hThread = 0; task->hStdIn.bNextNew = TRUE; task->hStdOut.handle = task->hReadOut; task->hStdOut.pLine = NULL; //task->hStdOut.pdp = pdp; task->hStdOut.bNextNew = TRUE; task->hStdOut.hThread = ThreadTo( HandleTaskOutput, (uintptr_t)task ); ThreadTo( WaitForTaskEnd, (uintptr_t)task ); } else { //task->hThread = ThreadTo( WaitForTaskEnd, (uintptr_t)task ); } } else { xlprintf(LOG_NOISE)( WIDE("Failed to run %s[%s]: %d"), program, GetText( cmdline ), GetLastError() ); CloseHandle( task->hWriteIn ); CloseHandle( task->hReadIn ); CloseHandle( task->hWriteOut ); CloseHandle( task->hReadOut ); CloseHandle( task->pi.hProcess ); CloseHandle( task->pi.hThread ); Release( task ); task = NULL; } } LineRelease( cmdline ); LineRelease( final_cmdline ); Release( expanded_working_path ); Release( expanded_path ); /* if( path ) SetCurrentPath( saved_path ); */ return task; #endif #ifdef __LINUX__ { pid_t newpid; TEXTCHAR saved_path[256]; xlprintf(LOG_ALWAYS)( WIDE("Expand Path was not implemented in linux code!") ); task = (PTASK_INFO)Allocate( sizeof( TASK_INFO ) ); MemSet( task, 0, sizeof( TASK_INFO ) ); task->psvEnd = psv; task->EndNotice = EndNotice; task->OutputEvent = OutputHandler; if( OutputHandler ) { pipe(task->hStdIn.pair); task->hStdIn.handle = task->hStdIn.pair[1]; pipe(task->hStdOut.pair); task->hStdOut.handle = task->hStdOut.pair[0]; } // always have to thread to taskend so waitpid can clean zombies. ThreadTo( WaitForTaskEnd, (uintptr_t)task ); if( path ) { GetCurrentPath( saved_path, sizeof( saved_path ) ); SetCurrentPath( path ); } if( !( newpid = fork() ) ) { char *_program = CStrDup( program ); // in case exec fails, we need to // drop any registered exit procs... //close( task->hStdIn.pair[1] ); //close( task->hStdOut.pair[0] ); //close( task->hStdErr.pair[0] ); if( OutputHandler ) { dup2( task->hStdIn.pair[0], 0 ); dup2( task->hStdOut.pair[1], 1 ); dup2( task->hStdOut.pair[1], 2 ); } DispelDeadstart(); //usleep( 100000 ); execve( _program, (char *const*)args, environ ); lprintf( WIDE( "Direct execute failed... trying along path..." ) ); { char *tmp = strdup( getenv( "PATH" ) ); char *tok; for( tok = strtok( tmp, ":" ); tok; tok = strtok( NULL, ":" ) ) { char fullname[256]; snprintf( fullname, sizeof( fullname ), "%s/%s", tok, _program ); lprintf( WIDE( "program:[%s]" ), fullname ); ((char**)args)[0] = fullname; execve( fullname, (char*const*)args, environ ); } Release( tmp ); } if( OutputHandler ) { close( task->hStdIn.pair[0] ); close( task->hStdOut.pair[1] ); } //close( task->hWriteErr ); close( 0 ); close( 1 ); close( 2 ); lprintf( WIDE( "exec failed - and this is ALLL bad... %d" ), errno ); //DebugBreak(); // well as long as this runs before // the other all will be well... task = NULL; // shit - what can I do now?! exit(0); // just in case exec fails... need to fault this. } else { if( OutputHandler ) { close( task->hStdIn.pair[0] ); close( task->hStdOut.pair[1] ); } } if( OutputHandler ) ThreadTo( HandleTaskOutput, (uintptr_t)task ); task->pid = newpid; lprintf( WIDE("Forked, and set the pid..") ); // how can I know if the command failed? // well I can't - but the user's callback will be invoked // when the above exits. if( path ) { // if path is NULL we didn't change the path... SetCurrentPath( saved_path ); } return task; } #endif } return FALSE; }
bool CContentDecoder::Open( sOpenVideoInfo *ovi ) { if (ovi == NULL) return false; boost::filesystem::path sys_name( ovi->m_Path ); const std::string &_filename = sys_name.string(); ovi->m_iCurrentFileFrameCount = 0; ovi->m_totalFrameCount = 0; struct stat fs; if ( !::stat( _filename.c_str(), &fs ) ) { ovi->m_CurrentFileatime = fs.st_atime; } g_Log->Info( "Opening: %s", _filename.c_str() ); //Destroy(); #ifdef USE_NEW_FFMPEG_API if( DumpError( avformat_open_input( &ovi->m_pFormatContext, _filename.c_str(), NULL, NULL ) ) < 0 ) #else if( DumpError( av_open_input_file( &ovi->m_pFormatContext, _filename.c_str(), NULL, 0, NULL ) ) < 0 ) #endif { g_Log->Warning( "Failed to open %s...", _filename.c_str() ); return false; } #ifdef USE_NEW_FFMPEG_API if( DumpError( avformat_find_stream_info( ovi->m_pFormatContext, NULL ) ) < 0 ) #else if( DumpError( av_find_stream_info( ovi->m_pFormatContext ) ) < 0 ) #endif { g_Log->Error( "av_find_stream_info failed with %s...", _filename.c_str() ); return false; } //dump_format( m_pFormatContext, 0, _filename.c_str(), false ); // Find video stream; ovi->m_VideoStreamID = -1; for( uint32 i=0; i<ovi->m_pFormatContext->nb_streams; i++ ) { if( ovi->m_pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ) { ovi->m_pVideoStream = ovi->m_pFormatContext->streams[i]; ovi->m_VideoStreamID = static_cast<int32>(i); break; } } if( ovi->m_VideoStreamID == -1 ) { g_Log->Error( "Could not find video stream in %s", _filename.c_str() ); return false; } // Find video codec. ovi->m_pVideoCodecContext = ovi->m_pFormatContext->streams[ ovi->m_VideoStreamID ]->codec; if( ovi->m_pVideoCodecContext == NULL ) { g_Log->Error( "Video CodecContext not found for %s", _filename.c_str() ); return false; } ovi->m_pVideoCodec = avcodec_find_decoder( ovi->m_pVideoCodecContext->codec_id ); if( ovi->m_pVideoCodec == NULL ) { ovi->m_pVideoCodecContext = NULL; g_Log->Error( "Video Codec not found for %s", _filename.c_str() ); return false; } //m_pVideoCodecContext->workaround_bugs = 1; //m_pFormatContext->flags |= AVFMT_FLAG_GENPTS; // Generate pts if missing even if it requires parsing future frames. ovi->m_pFormatContext->flags |= AVFMT_FLAG_IGNIDX; // Ignore index. //m_pFormatContext->flags |= AVFMT_FLAG_NONBLOCK; // Do not block when reading packets from input. #ifdef USE_NEW_FFMPEG_API if( DumpError( avcodec_open2( ovi->m_pVideoCodecContext, ovi->m_pVideoCodec, NULL ) ) < 0 ) #else if( DumpError( avcodec_open( ovi->m_pVideoCodecContext, ovi->m_pVideoCodec ) ) < 0 ) #endif { g_Log->Error( "avcodec_open failed for %s", _filename.c_str() ); return false; } #ifdef USE_NEW_FFMPEG_ALLOC_API ovi->m_pFrame = av_frame_alloc(); #else ovi->m_pFrame = avcodec_alloc_frame(); #endif if (ovi->m_pVideoStream->nb_frames > 0) ovi->m_totalFrameCount = static_cast<uint32>(ovi->m_pVideoStream->nb_frames); else ovi->m_totalFrameCount = uint32(((((double)ovi->m_pFormatContext->duration/(double)AV_TIME_BASE)) / av_q2d(ovi->m_pVideoStream->avg_frame_rate) + .5)); ovi->m_ReadingTrailingFrames = false; g_Log->Info( "Open done()" ); return true; }
void LeechCraft::Util::DBLock::DumpError (const QSqlQuery& lastQuery) { qWarning () << lastQuery.lastQuery (); DumpError (lastQuery.lastError ()); }
void nuiSocket::DumpError(int err) { DumpError(err, "?"); }