diff --git a/src/coreclr/pal/src/handlemgr/handleapi.cpp b/src/coreclr/pal/src/handlemgr/handleapi.cpp index 5f08f2fae51423..adda2aa2110c1e 100644 --- a/src/coreclr/pal/src/handlemgr/handleapi.cpp +++ b/src/coreclr/pal/src/handlemgr/handleapi.cpp @@ -107,31 +107,9 @@ CorUnix::InternalDuplicateHandle( PAL_ERROR palError = NO_ERROR; IPalObject *pobjSource = NULL; - DWORD source_process_id; - DWORD target_process_id; - DWORD cur_process_id; - - cur_process_id = GetCurrentProcessId(); - source_process_id = PROCGetProcessIDFromHandle(hSourceProcess); - target_process_id = PROCGetProcessIDFromHandle(hTargetProcess); - - /* Check validity of process handles */ - if (0 == source_process_id || 0 == target_process_id) - { - ASSERT("Can't duplicate handle: invalid source or destination process"); - palError = ERROR_INVALID_PARAMETER; - goto InternalDuplicateHandleExit; - } - - /* At least source or target process should be the current process. */ - if (source_process_id != cur_process_id - && target_process_id != cur_process_id) - { - ASSERT("Can't duplicate handle : neither source or destination" - "processes are from current process"); - palError = ERROR_INVALID_PARAMETER; - goto InternalDuplicateHandleExit; - } + /* We do not support other process in PAL */ + _ASSERTE(hSourceProcess == hPseudoCurrentProcess); + _ASSERTE(hTargetProcess == hPseudoCurrentProcess); if (FALSE != bInheritHandle) { @@ -169,16 +147,6 @@ CorUnix::InternalDuplicateHandle( goto InternalDuplicateHandleExit; } - // Handles can't be remoted cross-process. - // Just return the same handle. - if (source_process_id != cur_process_id - || target_process_id != cur_process_id) - { - *phDuplicate = hSource; - palError = NO_ERROR; - goto InternalDuplicateHandleExit; - } - // // Obtain the source IPalObject // @@ -200,10 +168,10 @@ CorUnix::InternalDuplicateHandle( } else if (hPseudoCurrentProcess == hSource) { + /* The only pseudo handle is invariant */ TRACE("Duplicating process pseudo handle(%p)\n", hSource); - - pobjSource = g_pobjProcess; - pobjSource->AddReference(); + *phDuplicate = hPseudoCurrentProcess; + goto InternalDuplicateHandleExit; } else if (hPseudoCurrentThread == hSource) { diff --git a/src/coreclr/pal/src/include/pal/corunix.hpp b/src/coreclr/pal/src/include/pal/corunix.hpp index f5d9ae6560a858..82ab89a1626e59 100644 --- a/src/coreclr/pal/src/include/pal/corunix.hpp +++ b/src/coreclr/pal/src/include/pal/corunix.hpp @@ -163,7 +163,6 @@ namespace CorUnix otiFile, otiFileMapping, otiSocket, - otiProcess, otiThread, otiIOCompletionPort, ObjectTypeIdCount // This entry must come last in the enumeration diff --git a/src/coreclr/pal/src/include/pal/palinternal.h b/src/coreclr/pal/src/include/pal/palinternal.h index 8c610758a998d5..871fb86ed65ddf 100644 --- a/src/coreclr/pal/src/include/pal/palinternal.h +++ b/src/coreclr/pal/src/include/pal/palinternal.h @@ -332,6 +332,4 @@ const char StackOverflowMessage[] = "Stack overflow.\n"; #endif // __cplusplus -DWORD PALAPI GetCurrentSessionId(); - #endif /* _PAL_INTERNAL_H_ */ diff --git a/src/coreclr/pal/src/include/pal/process.h b/src/coreclr/pal/src/include/pal/process.h index e3f26bde875a03..b15d2ab837a696 100644 --- a/src/coreclr/pal/src/include/pal/process.h +++ b/src/coreclr/pal/src/include/pal/process.h @@ -37,9 +37,8 @@ extern "C" */ extern Volatile terminator; -// The process and session ID of this process, so we can avoid excessive calls to getpid() and getsid(). +// The process ID of this process, so we can avoid excessive calls to getpid(). extern DWORD gPID; -extern DWORD gSID; extern LPWSTR pAppDir; @@ -50,15 +49,6 @@ extern int gApplicationGroupIdLength; #endif // __APPLE__ extern PathCharString *gSharedFilesPath; -/*++ -Function: - PROCGetProcessIDFromHandle - -Abstract - Return the process ID from a process handle ---*/ -DWORD PROCGetProcessIDFromHandle(HANDLE hProcess); - /*++ Function: PROCCreateInitialProcess diff --git a/src/coreclr/pal/src/include/pal/procobj.hpp b/src/coreclr/pal/src/include/pal/procobj.hpp index c1e10df2b44166..a53551f03d0234 100644 --- a/src/coreclr/pal/src/include/pal/procobj.hpp +++ b/src/coreclr/pal/src/include/pal/procobj.hpp @@ -23,43 +23,6 @@ Module Name: namespace CorUnix { - extern CObjectType otProcess; - - // - // Ideally dwProcessId would be part of the process object's immutable - // data. Doing so, though, creates complications in CreateProcess. The - // contents of the immutable data for a new object must be set before - // that object is registered with the object manager (as the object - // manager may make a copy of the immutable data). The PID for a new - // process, though, is not known until after creation. Registering the - // process object after process creation creates an undesirable error path - // -- if we are not able to register the process object (say, because of - // a low resource condition) we would be forced to return an error to - // the caller of CreateProcess, even though the new process was actually - // created... - // - // Note: we could work around this by effectively always going down - // the create suspended path. That is, the new process would not exec until - // the parent process released it. It's unclear how much benefit this would - // provide us. - // - - class CProcProcessLocalData - { - public: - CProcProcessLocalData() - : - dwProcessId(0) - { - }; - - ~CProcProcessLocalData() - { - }; - - DWORD dwProcessId; - }; - PAL_ERROR InitializeProcessCommandLine( LPWSTR lpwstrCmdLine, @@ -70,8 +33,6 @@ namespace CorUnix CreateInitialProcessAndThreadObjects( CPalThread *pThread ); - - extern IPalObject *g_pobjProcess; } #endif // _PAL_PROCOBJ_HPP_ diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index d261ce80c898a7..6c5c9989a2bb3f 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -331,9 +331,8 @@ Initialize( if (init_count == 0) { - // Set our pid and sid. + // Set our pid. gPID = getpid(); - gSID = getsid(gPID); // Initialize the thread local storage if (FALSE == TLSInitialize()) diff --git a/src/coreclr/pal/src/thread/process.cpp b/src/coreclr/pal/src/thread/process.cpp index 25902fcea08b8c..94ff751db5dac8 100644 --- a/src/coreclr/pal/src/thread/process.cpp +++ b/src/coreclr/pal/src/thread/process.cpp @@ -120,26 +120,6 @@ extern bool g_running_in_exe; using namespace CorUnix; -CObjectType CorUnix::otProcess( - otiProcess, - NULL, // No cleanup routine - 0, // No immutable data - NULL, // No immutable data copy routine - NULL, // No immutable data cleanup routine - sizeof(CProcProcessLocalData), - NULL, // No process local data cleanup routine - CObjectType::WaitableObject, - CObjectType::SingleTransitionObject, - CObjectType::ThreadReleaseHasNoSideEffects - ); - -CAllowedObjectTypes aotProcess(otiProcess); - -// -// The representative IPalObject for this process -// -IPalObject* CorUnix::g_pobjProcess; - // // The command line and app name for the process // @@ -152,9 +132,8 @@ Volatile terminator = 0; // Id of thread generating a core dump Volatile g_crashingThreadId = 0; -// Process and session ID of this process. +// Process ID of this process. DWORD gPID = (DWORD) -1; -DWORD gSID = (DWORD) -1; // Application group ID for this process #ifdef __APPLE__ @@ -252,7 +231,7 @@ CreateSemaphoreName( const UnambiguousProcessDescriptor& unambiguousProcessDescriptor, LPCSTR applicationGroupId); -static BOOL PROCEndProcess(HANDLE hProcess, UINT uExitCode, BOOL bTerminateUnconditionally); +static BOOL PROCEndProcess(UINT uExitCode, BOOL bTerminateUnconditionally); /*++ Function: @@ -274,26 +253,6 @@ GetCurrentProcessId( } -/*++ -Function: - GetCurrentSessionId - -See MSDN doc. ---*/ -DWORD -PALAPI -GetCurrentSessionId( - VOID) -{ - PERF_ENTRY(GetCurrentSessionId); - ENTRY("GetCurrentSessionId()\n" ); - - LOGEXIT("GetCurrentSessionId returns DWORD %#x\n", gSID); - PERF_EXIT(GetCurrentSessionId); - return gSID; -} - - /*++ Function: GetCurrentProcess @@ -352,7 +311,7 @@ ExitProcess( else { WARN("thread re-called ExitProcess\n"); - PROCEndProcess(GetCurrentProcess(), uExitCode, FALSE); + PROCEndProcess(uExitCode, FALSE); } } else if (0 != old_terminator) @@ -376,7 +335,7 @@ ExitProcess( */ if (PALInitLock() && PALIsInitialized()) { - PROCEndProcess(GetCurrentProcess(), uExitCode, FALSE); + PROCEndProcess(uExitCode, FALSE); /* Should not get here, because we terminate the current process */ ASSERT("PROCEndProcess has returned\n"); @@ -411,10 +370,12 @@ TerminateProcess( { BOOL ret; + _ASSERTE(hProcess == hPseudoCurrentProcess); + PERF_ENTRY(TerminateProcess); ENTRY("TerminateProcess(hProcess=%p, uExitCode=%u)\n",hProcess, uExitCode ); - ret = PROCEndProcess(hProcess, uExitCode, TRUE); + ret = PROCEndProcess(uExitCode, TRUE); LOGEXIT("TerminateProcess returns BOOL %d\n", ret); PERF_EXIT(TerminateProcess); @@ -455,77 +416,44 @@ RaiseFailFastException( little extra work before exiting. Most importantly, it won't shut down any DLLs that are loaded. + Only terminating current process is supported. + --*/ -static BOOL PROCEndProcess(HANDLE hProcess, UINT uExitCode, BOOL bTerminateUnconditionally) +static BOOL PROCEndProcess(UINT uExitCode, BOOL bTerminateUnconditionally) { - DWORD dwProcessId; BOOL ret = FALSE; - dwProcessId = PROCGetProcessIDFromHandle(hProcess); - if (dwProcessId == 0) + // WARN/ERROR before starting the termination process and/or leaving the PAL. + if (bTerminateUnconditionally) { - SetLastError(ERROR_INVALID_HANDLE); + WARN("exit code 0x%x ignored for terminate.\n", uExitCode); } - else if(dwProcessId != GetCurrentProcessId()) + else if ((uExitCode & 0xff) != uExitCode) { - if (uExitCode != 0) - WARN("exit code 0x%x ignored for external process.\n", uExitCode); - - if (kill(dwProcessId, SIGKILL) == 0) - { - ret = TRUE; - } - else - { - switch (errno) { - case ESRCH: - SetLastError(ERROR_INVALID_HANDLE); - break; - case EPERM: - SetLastError(ERROR_ACCESS_DENIED); - break; - default: - // Unexpected failure. - ASSERT(FALSE); - SetLastError(ERROR_INTERNAL_ERROR); - break; - } - } + // TODO: Convert uExitCodes into sysexits(3)? + ERROR("exit() only supports the lower 8-bits of an exit code. " + "status will only see error 0x%x instead of 0x%x.\n", uExitCode & 0xff, uExitCode); } - else - { - // WARN/ERROR before starting the termination process and/or leaving the PAL. - if (bTerminateUnconditionally) - { - WARN("exit code 0x%x ignored for terminate.\n", uExitCode); - } - else if ((uExitCode & 0xff) != uExitCode) - { - // TODO: Convert uExitCodes into sysexits(3)? - ERROR("exit() only supports the lower 8-bits of an exit code. " - "status will only see error 0x%x instead of 0x%x.\n", uExitCode & 0xff, uExitCode); - } - - TerminateCurrentProcessNoExit(bTerminateUnconditionally); - LOGEXIT("PROCEndProcess will not return\n"); + TerminateCurrentProcessNoExit(bTerminateUnconditionally); - if (bTerminateUnconditionally) - { - // abort() has the semantics that - // (1) it doesn't run atexit handlers - // (2) can invoke CrashReporter or produce a coredump, which is appropriate for TerminateProcess calls - // TerminationRequestHandlingRoutine in synchmanager.cpp sets the exit code to this special value. The - // Watson analyzer needs to know that the process was terminated with a SIGTERM. - PROCAbort(uExitCode == (128 + SIGTERM) ? SIGTERM : SIGABRT); - } - else - { - exit(uExitCode); - } + LOGEXIT("PROCEndProcess will not return\n"); - ASSERT(FALSE); // we shouldn't get here + if (bTerminateUnconditionally) + { + // abort() has the semantics that + // (1) it doesn't run atexit handlers + // (2) can invoke CrashReporter or produce a coredump, which is appropriate for TerminateProcess calls + // TerminationRequestHandlingRoutine in synchmanager.cpp sets the exit code to this special value. The + // Watson analyzer needs to know that the process was terminated with a SIGTERM. + PROCAbort(uExitCode == (128 + SIGTERM) ? SIGTERM : SIGABRT); } + else + { + exit(uExitCode); + } + + ASSERT(FALSE); // we shouldn't get here return ret; } @@ -2050,69 +1978,6 @@ PROCAbort(int signal, siginfo_t* siginfo, void* context) } \ while(0) -/*++ -Function: - PROCGetProcessIDFromHandle - -Abstract - Return the process ID from a process handle - -Parameter - hProcess: process handle - -Return - Return the process ID, or 0 if it's not a valid handle ---*/ -DWORD -PROCGetProcessIDFromHandle( - HANDLE hProcess) -{ - PAL_ERROR palError; - IPalObject *pobjProcess = NULL; - CPalThread *pThread = InternalGetCurrentThread(); - - DWORD dwProcessId = 0; - - if (hPseudoCurrentProcess == hProcess) - { - dwProcessId = gPID; - goto PROCGetProcessIDFromHandleExit; - } - - - palError = g_pObjectManager->ReferenceObjectByHandle( - pThread, - hProcess, - &aotProcess, - &pobjProcess - ); - - if (NO_ERROR == palError) - { - IDataLock *pDataLock; - CProcProcessLocalData *pLocalData; - - palError = pobjProcess->GetProcessLocalData( - pThread, - ReadLock, - &pDataLock, - reinterpret_cast(&pLocalData) - ); - - if (NO_ERROR == palError) - { - dwProcessId = pLocalData->dwProcessId; - pDataLock->ReleaseLock(pThread, FALSE); - } - - pobjProcess->ReleaseReference(pThread); - } - -PROCGetProcessIDFromHandleExit: - - return dwProcessId; -} - /*++ Function InitializeProcessCommandLine @@ -2209,11 +2074,7 @@ CorUnix::CreateInitialProcessAndThreadObjects( { PAL_ERROR palError = NO_ERROR; HANDLE hThread; - IPalObject *pobjProcess = NULL; - IDataLock *pDataLock; - CProcProcessLocalData *pLocalData; CObjectAttributes oa; - HANDLE hProcess; // // Create initial thread object @@ -2231,74 +2092,8 @@ CorUnix::CreateInitialProcessAndThreadObjects( (void) g_pObjectManager->RevokeHandle(pThread, hThread); - // - // Create and initialize process object - // - - palError = g_pObjectManager->AllocateObject( - pThread, - &otProcess, - &oa, - &pobjProcess - ); - - if (NO_ERROR != palError) - { - ERROR("Unable to allocate process object"); - goto CreateInitialProcessAndThreadObjectsExit; - } - - palError = pobjProcess->GetProcessLocalData( - pThread, - WriteLock, - &pDataLock, - reinterpret_cast(&pLocalData) - ); - - if (NO_ERROR != palError) - { - ASSERT("Unable to access local data"); - goto CreateInitialProcessAndThreadObjectsExit; - } - - pLocalData->dwProcessId = gPID; - pDataLock->ReleaseLock(pThread, TRUE); - - palError = g_pObjectManager->RegisterObject( - pThread, - pobjProcess, - &aotProcess, - &hProcess, - &g_pobjProcess - ); - - // - // pobjProcess is invalidated by the call to RegisterObject, so - // NULL it out here to prevent it from being released later - // - - pobjProcess = NULL; - - if (NO_ERROR != palError) - { - ASSERT("Failure registering process object"); - goto CreateInitialProcessAndThreadObjectsExit; - } - - // - // There's no need to keep this handle around, so revoke - // it now - // - - g_pObjectManager->RevokeHandle(pThread, hProcess); - CreateInitialProcessAndThreadObjectsExit: - if (NULL != pobjProcess) - { - pobjProcess->ReleaseReference(pThread); - } - return palError; }