Process::Process(DWORD id) { _id = id; _ownerDomain = NULL; _ownerName = NULL; _pName = NULL; _handle = OpenProcess(PROCESS_ALL_ACCESS,FALSE, _id ); if (_handle == INVALID_HANDLE_VALUE){ throw(ProcessException("handle error",GetLastError())); } DWORD dwRtnCode = GetSecurityInfo(//получаем DACL,owner для процесса _handle, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, &_ownerSID, &_ownerGroupSID, &_dacl, NULL, &_psd ); if (dwRtnCode != ERROR_SUCCESS) { throw(ProcessException("GetSecurityInfo error ", GetLastError())); } ownerNameDomain(); //сразу же получаем все имена processNameEx(); fillAceList(); // и заполняем массив ACE }
void Process::ownerNameDomain(){ DWORD dwRtnCode = 0; BOOL bRtnBool = TRUE; SID_NAME_USE eUse = SidTypeUnknown; DWORD size = 1024; // Reallocate memory for the buffers. _ownerName = (LPTSTR)GlobalAlloc( GMEM_FIXED, size); // Check GetLastError for GlobalAlloc error condition. if (_ownerName == NULL) throw(ProcessException("GlobalAlloc error = ",GetLastError())); _ownerDomain = (LPTSTR)GlobalAlloc( GMEM_FIXED, size); // Check GetLastError for GlobalAlloc error condition. if (_ownerDomain == NULL) throw(ProcessException("GlobalAlloc error = ", GetLastError())); // Second call to LookupAccountSid to get the account name. bRtnBool = LookupAccountSid( NULL, // name of local or remote computer _ownerSID, // security identifier _ownerName, // account name buffer (LPDWORD)&size, // size of account name buffer _ownerDomain, // domain name (LPDWORD)&size, // size of domain name buffer &eUse); // SID type // Check GetLastError for LookupAccountSid error condition. if (bRtnBool == FALSE) { DWORD dwErrorCode = 0; dwErrorCode = GetLastError(); if (dwErrorCode == ERROR_NONE_MAPPED) throw(ProcessException("Account owner not found for specified SID = ",dwErrorCode)); else throw(ProcessException("Error in LookupAccountSid = ",dwErrorCode)); }; _ownerRID = parseRID(_ownerSID); return; }
void Process::fillAceList(){ ACL_SIZE_INFORMATION aclSizeInfo; ZeroMemory(&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION)); aclSizeInfo.AclBytesInUse = sizeof(ACL); ACL_REVISION_INFORMATION aclRevisionInformation; ZeroMemory(&aclRevisionInformation,sizeof(ACL_REVISION_INFORMATION)); GetAclInformation( _dacl, (LPVOID)&aclRevisionInformation, sizeof(ACL_REVISION_INFORMATION), AclRevisionInformation); DWORD dwRtnCode = GetAclInformation(//Узнаем всю информацию о ACL. - Кол-во ACE в ней _dacl, (LPVOID)&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION), AclSizeInformation); if (!dwRtnCode) throw(ProcessException("GetAclInformation error =",GetLastError())); PVOID pAce; for (DWORD i=0;i<aclSizeInfo.AceCount;++i){//По всем ACE if (GetAce(_dacl,i,&pAce)){ switch (((ACE_HEADER*)pAce)->AceType) { case ACCESS_ALLOWED_ACE_TYPE: _allowedACEs.push_back((ACCESS_ALLOWED_ACE*)pAce); break; case ACCESS_DENIED_ACE_TYPE: _deniedACEs.push_back((ACCESS_DENIED_ACE*)pAce); break; default: printf("Undefined type\n"); break; } }else throw(ProcessException("GetAce ERROR = ", GetLastError())); } }
LONG WINAPI DwExceptionFilter(LPEXCEPTION_POINTERS pep) { EXCEPTION_RECORD *per; HANDLE hFileMap; DWSharedMem *pdwsm; SECURITY_ATTRIBUTES sa; //------------------------------------------------------------------------------------------------------------ // we keep local copies of these in case another thread is trashing memory // it much more likely to trash the heap than our stack HANDLE hEventDone; // event DW signals when done HANDLE hEventAlive; // heartbeat event DW signals per EVENT_TIMEOUT HANDLE hMutex; // to protect the signaling of EventDone char szCommandLine[MAX_PATH * 2]; DWORD dw; BOOL fDwRunning; STARTUPINFO si; PROCESS_INFORMATION pi; //------------------------------------------------------------------------------------------------------------ // init - Check if we just hit a breakpoint. If so, continue execution. Debugger will take care of itself. per = pep->ExceptionRecord; if (EXCEPTION_BREAKPOINT == per->ExceptionCode) return 0; //------------------------------------------------------------------------------------------------------------ // create shared memory memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle = TRUE; hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, sizeof(DWSharedMem), NULL); if (hFileMap == NULL) { //At this point, call the GameOS exception handler and convert the pep to the data they need! ProcessException(pep); return 1; } pdwsm = (DWSharedMem *) MapViewOfFile(hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0); if (pdwsm == NULL) { //At this point, call the GameOS exception handler and convert the pep to the data they need! ProcessException(pep); return 1; } memset(pdwsm, 0, sizeof(DWSharedMem)); hEventAlive = CreateEvent(&sa, FALSE, FALSE, NULL); hEventDone = CreateEvent(&sa, FALSE, FALSE, NULL); hMutex = CreateMutex(&sa, FALSE, NULL); if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), &pdwsm->hProc, PROCESS_ALL_ACCESS, TRUE, 0)) { //At this point, call the GameOS exception handler and convert the pep to the data they need! ProcessException(pep); return 1; } if (hEventAlive == NULL || hEventDone == NULL || hMutex == NULL || pdwsm->hProc == NULL) { //At this point, call the GameOS exception handler and convert the pep to the data they need! ProcessException(pep); return 1; } //------------------------------------------------------------------------------------------------------------ // setup interface structure pdwsm->pid = GetCurrentProcessId(); pdwsm->tid = GetCurrentThreadId(); pdwsm->hEventAlive = hEventAlive; pdwsm->hEventDone = hEventDone; pdwsm->hMutex = hMutex; pdwsm->dwSize = sizeof(DWSharedMem); pdwsm->pep = pep; pdwsm->eip = (DWORD) pep->ExceptionRecord->ExceptionAddress; pdwsm->bfmsoctdsOffer = msoctdsQuit; pdwsm->bfmsoctdsLetRun = msoctdsQuit; pdwsm->bfDWBehaviorFlags = fDwCheckSig; strcpy(pdwsm->szFormalAppName, Environment.applicationName); strcpy(pdwsm->szInformalAppName, "MechCommander 2"); strcpy(pdwsm->szRegSubPath, "Software\\Microsoft\\Microsoft Games\\"); strcat(pdwsm->szRegSubPath, Environment.applicationName); // strcpy(pdwsm->szLCIDKeyValue, ""); // strcpy(pdwsm->szPIDRegKey, "HKLM\\Software\\Microsoft\\Internet Explorer\\Registration\\DigitalProductID"); #if defined(FINAL) || defined(EXTERNAL) strcpy(pdwsm->szServer, "watson.microsoft.com"); #else strcpy(pdwsm->szServer, "officewatson"); #endif wcscpy(pdwsm->wzErrorMessage, WatsonCrashMessageUnicode); //Leave this alone? No idea what it does. Not in Docs. // OK, I kinda know now. These are DLLs that Watson can check for goodness at crash time. // COOL, because the end user might have mucked with the EXE or data and this allows us to // report that information back to the server. memcpy(pdwsm->wzDotDataDlls, L"mc2res.dll\0editores.dll\0", 24 * sizeof(WCHAR)); GetModuleFileNameA(NULL, pdwsm->szModuleFileName, DW_MAX_PATH); //Additional Files for MechCommander? Should there be any? Log files, etc. // ok, now we don't want to accidently change this memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); memset(&pi, 0, sizeof(PROCESS_INFORMATION)); wsprintfA(szCommandLine, "dw -x -s %u", (DWORD) hFileMap); //Check if we are in fullScreen mode. If so, switch back to WindowMode to insure DW screen comes up. if(Environment.fullScreen && hWindow ) EnterWindowMode(); if (CreateProcessA(NULL, szCommandLine, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { fDwRunning = TRUE; while (fDwRunning) { if (WaitForSingleObject(hEventAlive, DW_TIMEOUT_VALUE * 100) == WAIT_OBJECT_0) { if (WaitForSingleObject(hEventDone, 1) == WAIT_OBJECT_0) { fDwRunning = FALSE; } continue; } // we timed-out waiting for DW to respond, try to quit dw = WaitForSingleObject(hMutex, DW_TIMEOUT_VALUE); if (dw == WAIT_TIMEOUT) { fDwRunning = FALSE; // either DW's hung or crashed, we must carry on } else if (dw == WAIT_ABANDONED) { fDwRunning = FALSE; ReleaseMutex(hMutex); } else { // DW has not woken up? if (WaitForSingleObject(hEventAlive, 1) != WAIT_OBJECT_0) // tell DW we're through waiting for it's sorry self { SetEvent(hEventDone); fDwRunning = FALSE; } else { // are we done if (WaitForSingleObject(hEventDone, 1) == WAIT_OBJECT_0) fDwRunning = FALSE; } ReleaseMutex(hMutex); } } #if 0 // did we get attached? // Again, do NOT term the current APP. Late GameOS have its shot at the exception. if (WaitForSingleObject(hEventDBAttach, 1) == WAIT_OBJECT_0) { // yes, die MessageBox(NULL, "DB Attach ", "out", MB_OK); CloseHandle(hEventAlive); CloseHandle(hEventDone); CloseHandle(hMutex); TerminateProcess(GetCurrentProcess(), 0); } #endif // no, clean up CloseHandle(hEventAlive); CloseHandle(hEventDone); CloseHandle(hMutex); } // end if CreateProcess succeeded UnmapViewOfFile(pdwsm); CloseHandle(hFileMap); //At this point, call the GameOS exception handler and convert the pep to the data they need! ProcessException(pep); return 1; }
static LONG WINAPI HerculesUnhandledExceptionFilter( EXCEPTION_POINTERS* pExceptionPtrs ) { static BOOL bDidThis = FALSE; // (if we did this once already) if (bDidThis) return EXCEPTION_EXECUTE_HANDLER; // (quick exit to prevent loop) bDidThis = TRUE; SetErrorMode( 0 ); // (reset back to default handling) if (sysblk.daemon_mode) // (is an external GUI in control?) { fflush( stdout ); fflush( stderr ); _ftprintf( stderr, _T("]!OOPS!\n") ); // (external GUI pre-processing...) fflush( stdout ); fflush( stderr ); Sleep( 10 ); } else { // Normal panel mode: reset console mode and clear the screen... DWORD dwCellsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; HANDLE hStdIn, hStdErr; COORD ptConsole = { 0, 0 }; EnableMenuItem( GetSystemMenu( FindConsoleHandle(), FALSE ), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED ); hStdIn = GetStdHandle( STD_INPUT_HANDLE ); hStdErr = GetStdHandle( STD_ERROR_HANDLE ); #define DEFAULT_CONSOLE_ATTRIBUTES (0 \ | FOREGROUND_RED \ | FOREGROUND_GREEN \ | FOREGROUND_BLUE \ ) /* FIXME these are defined in SDK V6+ */ #ifndef ENABLE_INSERT_MODE #define ENABLE_INSERT_MODE 0 #endif #ifndef ENABLE_QUICK_EDIT_MODE #define ENABLE_QUICK_EDIT_MODE 0 #endif #define DEFAULT_CONSOLE_INPUT_MODE (0 \ | ENABLE_ECHO_INPUT \ | ENABLE_INSERT_MODE \ | ENABLE_LINE_INPUT \ | ENABLE_MOUSE_INPUT \ | ENABLE_PROCESSED_INPUT \ | ENABLE_QUICK_EDIT_MODE \ ) #define DEFAULT_CONSOLE_OUTPUT_MODE (0 \ | ENABLE_PROCESSED_OUTPUT \ | ENABLE_WRAP_AT_EOL_OUTPUT \ ) SetConsoleTextAttribute( hStdErr, DEFAULT_CONSOLE_ATTRIBUTES ); SetConsoleMode ( hStdIn, DEFAULT_CONSOLE_INPUT_MODE ); SetConsoleMode ( hStdErr, DEFAULT_CONSOLE_OUTPUT_MODE ); GetConsoleScreenBufferInfo( hStdErr, &csbi ); FillConsoleOutputCharacter( hStdErr, ' ', csbi.dwSize.X * csbi.dwSize.Y, ptConsole, &dwCellsWritten ); GetConsoleScreenBufferInfo( hStdErr, &csbi ); FillConsoleOutputAttribute( hStdErr, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, ptConsole, &dwCellsWritten ); SetConsoleCursorPosition ( hStdErr, ptConsole ); fflush( stdout ); fflush( stderr ); Sleep( 10 ); } _tprintf( _T("\n\n") ); _tprintf( _T(" ***************\n") ); _tprintf( _T(" * OOPS! *\n") ); _tprintf( _T(" ***************\n") ); _tprintf( _T("\n") ); _tprintf( _T(" Hercules has crashed!\n") ); _tprintf( _T("\n") ); _tprintf( _T("(you may or may not need to press ENTER if no 'oops!' dialog-box appears)\n") ); _tprintf( _T("\n") ); fflush( stdout ); fflush( stderr ); Sleep( 10 ); ProcessException( pExceptionPtrs ); // (create a minidump, if possible) fflush( stdout ); fflush( stderr ); Sleep( 10 ); timeEndPeriod( 1 ); // (reset to default time interval) return EXCEPTION_EXECUTE_HANDLER; // (quite likely exits the process) }
// Execute the next instruction std::string m68000::ExecuteInstruction(std::string &traceRecord, bool tracing) { unsigned int opcode; int status; // Add instruction address to the trace record if (tracing) { traceRecord = "{InstructionAddress "; traceRecord += IntToString(register_value[PC_INDEX], 8); traceRecord += "} "; } // Make sure the CPU hasn't been halted if (myState != HALT_STATE) { // Service any pending interrupts bool serviceFlag; status = ServiceInterrupts(serviceFlag); // Only execute an instruction if we didn't service an interrupt if (!serviceFlag && status == EXECUTE_OK) { // Make sure the CPU isn't stopped waiting for exceptions if (myState != STOP_STATE) { // Fetch the next instruction status = Peek(register_value[PC_INDEX], opcode, WORD); if (status == EXECUTE_OK) { register_value[PC_INDEX] += 2; // Execute the instruction ExecutionPointer executeMethod = DecodeInstruction(opcode); status = (this->*executeMethod)(opcode, traceRecord, tracing); // If the last instruction was not priviledged then check for trace if ((status == EXECUTE_OK) && (register_value[SR_INDEX] & T_FLAG)) status = ProcessException(9); } } else { if (tracing) traceRecord += "{Mnemonic {CPU is stopped}} "; } } if (status == EXECUTE_BUS_ERROR) { if (ExecuteBusError(opcode, traceRecord, tracing) != EXECUTE_OK) { // Oh, no the cpu has fallen and it can't get up! myState = HALT_STATE; if (tracing) traceRecord += "{Mnemonic {Double Bus/Address Error CPU halted}} "; } } else if (status == EXECUTE_ADDRESS_ERROR) { if (ExecuteAddressError(opcode, traceRecord, tracing) != EXECUTE_OK) { // Now, where's that reset button??? myState = HALT_STATE; if (tracing) traceRecord += "{Mnemonic {Double Bus/Address Error CPU halted}} "; } } } else { if (tracing) traceRecord += "{Mnemonic {CPU has halted}} "; } // Check the event list myEventHandler.Check(); // Signal if the processor is in a wierd state if (myState == HALT_STATE) { return "CPU has halted"; } else if (myState == BREAK_STATE) { myState = NORMAL_STATE; if (tracing) return ""; else return "BREAK instruction"; } return ""; }
LONG WINAPI CrashExceptionFilter( struct _EXCEPTION_POINTERS *ExceptionInfo ) { return ProcessException(ExceptionInfo); }
void Process::processNameEx(){ DWORD dwRtnCode = 0; DWORD size = 1024; //Надеемся что влезет _pName = (LPSTR)GlobalAlloc( GMEM_FIXED, size); if (_pName == NULL) { DWORD dwErrorCode = 0; dwErrorCode = GetLastError(); throw(ProcessException("GlobalAlloc error = ",dwErrorCode)); } dwRtnCode = GetProcessImageFileNameA(_handle,_pName,size); if (dwRtnCode == 0){ DWORD error = GetLastError(); throw(ProcessException("GetProcesImageFileName error ",error)); } }