Skip to content

Remove code handling cases where NSI API isn't supported #49944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

DECLARE_DEBUG_PRINT_OBJECT("aspnetcorev2_outofprocess.dll");

BOOL g_fNsiApiNotSupported = FALSE;
BOOL g_fWebSocketStaticInitialize = FALSE;
BOOL g_fEnableReferenceCountTracing = FALSE;
BOOL g_fGlobalInitialize = FALSE;
Expand All @@ -31,8 +30,6 @@ InitializeGlobalConfiguration(
{
HKEY hKey;
BOOL fLocked = FALSE;
DWORD dwSize = 0;
DWORD dwResult = 0;

if (!g_fGlobalInitialize)
{
Expand Down Expand Up @@ -90,19 +87,7 @@ InitializeGlobalConfiguration(
}
}

dwResult = GetExtendedTcpTable(NULL,
&dwSize,
FALSE,
AF_INET,
TCP_TABLE_OWNER_PID_LISTENER,
0);
if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
{
g_fNsiApiNotSupported = TRUE;
}

g_fWebSocketStaticInitialize = IsWindows8OrGreater();

g_fGlobalInitialize = TRUE;
}
Finished:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#include "EventLog.h"
#include "file_utility.h"
#include "exceptions.h"
//#include <share.h>

//extern BOOL g_fNsiApiNotSupported;

#define STARTUP_TIME_LIMIT_INCREMENT_IN_MILLISECONDS 5000

Expand Down Expand Up @@ -114,33 +111,21 @@ SERVER_PROCESS::GetRandomPort
DWORD dwActualProcessId = 0;

std::uniform_int_distribution<> dist(MIN_PORT_RANDOM, MAX_PORT);

if (g_fNsiApiNotSupported)
DWORD cRetry = 0;
do
{
//
// the default value for optional parameter dwExcludedPort is 0 which is reserved
// a random number between MIN_PORT_RANDOM and MAX_PORT
// ignore dwActualProcessId because here we are
// determing whether the randomly generated port is
// in use by any other process.
//
while ((*pdwPickedPort = dist(m_randomGenerator)) == dwExcludedPort);
}
else
{
DWORD cRetry = 0;
do
{
//
// ignore dwActualProcessId because here we are
// determing whether the randomly generated port is
// in use by any other process.
//
while ((*pdwPickedPort = dist(m_randomGenerator)) == dwExcludedPort);
hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
} while (fPortInUse && ++cRetry < MAX_RETRY);
hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
} while (fPortInUse && ++cRetry < MAX_RETRY);

if (cRetry >= MAX_RETRY)
{
hr = HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
}
if (cRetry >= MAX_RETRY)
{
hr = HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
}

return hr;
Expand Down Expand Up @@ -579,71 +564,68 @@ SERVER_PROCESS::PostStartCheck(
fDebuggerAttached = FALSE;
}

if (!g_fNsiApiNotSupported)
//
// NsiAPI(GetExtendedTcpTable) is supported. we should check whether processIds match
//
if (dwActualProcessId == m_dwProcessId)
{
//
// NsiAPI(GetExtendedTcpTable) is supported. we should check whether processIds matche
//
if (dwActualProcessId == m_dwProcessId)
m_dwListeningProcessId = m_dwProcessId;
fProcessMatch = TRUE;
}

if (!fProcessMatch)
{
// could be the scenario that backend creates child process
if (FAILED_LOG(hr = GetChildProcessHandles()))
{
m_dwListeningProcessId = m_dwProcessId;
fProcessMatch = TRUE;
goto Finished;
}

if (!fProcessMatch)
for (DWORD i = 0; i < m_cChildProcess; ++i)
{
// could be the scenario that backend creates child process
if (FAILED_LOG(hr = GetChildProcessHandles()))
// a child process listen on the assigned port
if (dwActualProcessId == m_dwChildProcessIds[i])
{
goto Finished;
}
m_dwListeningProcessId = m_dwChildProcessIds[i];
fProcessMatch = TRUE;

for (DWORD i = 0; i < m_cChildProcess; ++i)
{
// a child process listen on the assigned port
if (dwActualProcessId == m_dwChildProcessIds[i])
if (m_hChildProcessHandles[i] != NULL)
{
m_dwListeningProcessId = m_dwChildProcessIds[i];
fProcessMatch = TRUE;
if (fDebuggerAttached == FALSE &&
CheckRemoteDebuggerPresent(m_hChildProcessHandles[i], &fDebuggerAttached) == 0)
{
// some error occurred - assume debugger is not attached;
fDebuggerAttached = FALSE;
}

if (m_hChildProcessHandles[i] != NULL)
if (FAILED_LOG(hr = RegisterProcessWait(&m_hChildProcessWaitHandles[i],
m_hChildProcessHandles[i])))
{
if (fDebuggerAttached == FALSE &&
CheckRemoteDebuggerPresent(m_hChildProcessHandles[i], &fDebuggerAttached) == 0)
{
// some error occurred - assume debugger is not attached;
fDebuggerAttached = FALSE;
}

if (FAILED_LOG(hr = RegisterProcessWait(&m_hChildProcessWaitHandles[i],
m_hChildProcessHandles[i])))
{
goto Finished;
}
iChildProcessIndex = i;
goto Finished;
}
break;
iChildProcessIndex = i;
}
break;
}
}
}

if(!fProcessMatch)
{
//
// process that we created is not listening
// on the port we specified.
//
fReady = FALSE;
hr = HRESULT_FROM_WIN32(ERROR_CREATE_FAILED);
strEventMsg.SafeSnwprintf(
ASPNETCORE_EVENT_PROCESS_START_WRONGPORT_ERROR_MSG,
m_struAppFullPath.QueryStr(),
m_struPhysicalPath.QueryStr(),
m_struCommandLine.QueryStr(),
m_dwPort,
hr);
goto Finished;
}
if (!fProcessMatch)
{
//
// process that we created is not listening
// on the port we specified.
//
fReady = FALSE;
hr = HRESULT_FROM_WIN32(ERROR_CREATE_FAILED);
strEventMsg.SafeSnwprintf(
ASPNETCORE_EVENT_PROCESS_START_WRONGPORT_ERROR_MSG,
m_struAppFullPath.QueryStr(),
m_struPhysicalPath.QueryStr(),
m_struCommandLine.QueryStr(),
m_dwPort,
hr);
goto Finished;
}

if (!fReady)
Expand Down Expand Up @@ -709,12 +691,9 @@ SERVER_PROCESS::PostStartCheck(
}
}

if (!g_fNsiApiNotSupported)
{
m_hListeningProcessHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE | PROCESS_DUP_HANDLE,
FALSE,
m_dwListeningProcessId);
}
m_hListeningProcessHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE | PROCESS_DUP_HANDLE,
FALSE,
m_dwListeningProcessId);

//
// mark server process as Ready
Expand Down Expand Up @@ -1200,7 +1179,6 @@ SERVER_PROCESS::CheckIfServerIsUp(
MIB_TCPROW_OWNER_PID *pOwner = NULL;
DWORD dwSize = 1000; // Initial size for pTCPInfo buffer
int iResult = 0;
SOCKADDR_IN sockAddr;
SOCKET socketCheck = INVALID_SOCKET;

DBG_ASSERT(pfReady);
Expand All @@ -1212,93 +1190,48 @@ SERVER_PROCESS::CheckIfServerIsUp(
//
*pdwProcessId = 0;

if (!g_fNsiApiNotSupported)
while (dwResult == ERROR_INSUFFICIENT_BUFFER)
{
while (dwResult == ERROR_INSUFFICIENT_BUFFER)
{
// Increase the buffer size with additional space, MIB_TCPROW 20 bytes
// New entries may be added by other processes before calling GetExtendedTcpTable
dwSize += 200;

if (pTCPInfo != NULL)
{
HeapFree(GetProcessHeap(), 0, pTCPInfo);
}
// Increase the buffer size with additional space, MIB_TCPROW 20 bytes
// New entries may be added by other processes before calling GetExtendedTcpTable
dwSize += 200;

pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize);
if (pTCPInfo == NULL)
{
hr = E_OUTOFMEMORY;
goto Finished;
}

dwResult = GetExtendedTcpTable(pTCPInfo,
&dwSize,
FALSE,
AF_INET,
TCP_TABLE_OWNER_PID_LISTENER,
0);

if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
{
hr = HRESULT_FROM_WIN32(dwResult);
goto Finished;
}
}

// iterate pTcpInfo struct to find PID/PORT entry
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
if (pTCPInfo != NULL)
{
pOwner = &pTCPInfo->table[dwLoop];
if (ntohs((USHORT)pOwner->dwLocalPort) == dwPort)
{
*pdwProcessId = pOwner->dwOwningPid;
*pfReady = TRUE;
break;
}
HeapFree(GetProcessHeap(), 0, pTCPInfo);
}
}
else
{
//
// We have to open socket to ping the service
//
socketCheck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (socketCheck == INVALID_SOCKET)
pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize);
if (pTCPInfo == NULL)
{
hr = HRESULT_FROM_WIN32(WSAGetLastError());
hr = E_OUTOFMEMORY;
goto Finished;
}

sockAddr.sin_family = AF_INET;
if (!inet_pton(AF_INET, LOCALHOST, &(sockAddr.sin_addr)))
dwResult = GetExtendedTcpTable(pTCPInfo,
&dwSize,
FALSE,
AF_INET,
TCP_TABLE_OWNER_PID_LISTENER,
0);

if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
{
hr = HRESULT_FROM_WIN32(WSAGetLastError());
hr = HRESULT_FROM_WIN32(dwResult);
goto Finished;
}
}

//sockAddr.sin_addr.s_addr = inet_addr( LOCALHOST );
sockAddr.sin_port = htons((u_short)dwPort);

//
// Connect to server.
// if connection fails, socket is not closed, we reuse the same socket
// while retrying
//
iResult = connect(socketCheck, (SOCKADDR *)&sockAddr, sizeof(sockAddr));
if (iResult == SOCKET_ERROR)
// iterate pTcpInfo struct to find PID/PORT entry
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
{
pOwner = &pTCPInfo->table[dwLoop];
if (ntohs((USHORT)pOwner->dwLocalPort) == dwPort)
{
hr = HRESULT_FROM_WIN32(WSAGetLastError());
if (hr == HRESULT_FROM_WIN32(WSAECONNREFUSED))
{
// WSAECONNREFUSED means no application listen on the given port.
// This is not a failure. Reset the hresult to S_OK and return fReady to false
hr = S_OK;
}
goto Finished;
*pdwProcessId = pOwner->dwOwningPid;
*pfReady = TRUE;
break;
}
*pfReady = TRUE;
}

Finished:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ inline bool IsSpace(char ch)
extern BOOL g_fAsyncDisconnectAvailable;
extern BOOL g_fWinHttpNonBlockingCallbackAvailable;
extern BOOL g_fWebSocketStaticInitialize;
extern BOOL g_fNsiApiNotSupported;
extern BOOL g_fEnableReferenceCountTracing;
extern BOOL g_fProcessDetach;
extern DWORD g_dwActiveServerProcesses;
Expand Down