Skip to content

Commit ab1e656

Browse files
Remove code handling cases where NSI API isn't supported (#49944)
1 parent f3b9d88 commit ab1e656

File tree

3 files changed

+87
-170
lines changed

3 files changed

+87
-170
lines changed

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/dllmain.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
DECLARE_DEBUG_PRINT_OBJECT("aspnetcorev2_outofprocess.dll");
88

9-
BOOL g_fNsiApiNotSupported = FALSE;
109
BOOL g_fWebSocketStaticInitialize = FALSE;
1110
BOOL g_fEnableReferenceCountTracing = FALSE;
1211
BOOL g_fGlobalInitialize = FALSE;
@@ -31,8 +30,6 @@ InitializeGlobalConfiguration(
3130
{
3231
HKEY hKey;
3332
BOOL fLocked = FALSE;
34-
DWORD dwSize = 0;
35-
DWORD dwResult = 0;
3633

3734
if (!g_fGlobalInitialize)
3835
{
@@ -90,19 +87,7 @@ InitializeGlobalConfiguration(
9087
}
9188
}
9289

93-
dwResult = GetExtendedTcpTable(NULL,
94-
&dwSize,
95-
FALSE,
96-
AF_INET,
97-
TCP_TABLE_OWNER_PID_LISTENER,
98-
0);
99-
if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
100-
{
101-
g_fNsiApiNotSupported = TRUE;
102-
}
103-
10490
g_fWebSocketStaticInitialize = IsWindows8OrGreater();
105-
10691
g_fGlobalInitialize = TRUE;
10792
}
10893
Finished:

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/serverprocess.cpp

Lines changed: 87 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#include "EventLog.h"
88
#include "file_utility.h"
99
#include "exceptions.h"
10-
//#include <share.h>
11-
12-
//extern BOOL g_fNsiApiNotSupported;
1310

1411
#define STARTUP_TIME_LIMIT_INCREMENT_IN_MILLISECONDS 5000
1512

@@ -114,33 +111,21 @@ SERVER_PROCESS::GetRandomPort
114111
DWORD dwActualProcessId = 0;
115112

116113
std::uniform_int_distribution<> dist(MIN_PORT_RANDOM, MAX_PORT);
117-
118-
if (g_fNsiApiNotSupported)
114+
DWORD cRetry = 0;
115+
do
119116
{
120117
//
121-
// the default value for optional parameter dwExcludedPort is 0 which is reserved
122-
// a random number between MIN_PORT_RANDOM and MAX_PORT
118+
// ignore dwActualProcessId because here we are
119+
// determing whether the randomly generated port is
120+
// in use by any other process.
123121
//
124122
while ((*pdwPickedPort = dist(m_randomGenerator)) == dwExcludedPort);
125-
}
126-
else
127-
{
128-
DWORD cRetry = 0;
129-
do
130-
{
131-
//
132-
// ignore dwActualProcessId because here we are
133-
// determing whether the randomly generated port is
134-
// in use by any other process.
135-
//
136-
while ((*pdwPickedPort = dist(m_randomGenerator)) == dwExcludedPort);
137-
hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
138-
} while (fPortInUse && ++cRetry < MAX_RETRY);
123+
hr = CheckIfServerIsUp(*pdwPickedPort, &dwActualProcessId, &fPortInUse);
124+
} while (fPortInUse && ++cRetry < MAX_RETRY);
139125

140-
if (cRetry >= MAX_RETRY)
141-
{
142-
hr = HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
143-
}
126+
if (cRetry >= MAX_RETRY)
127+
{
128+
hr = HRESULT_FROM_WIN32(ERROR_PORT_NOT_SET);
144129
}
145130

146131
return hr;
@@ -579,71 +564,68 @@ SERVER_PROCESS::PostStartCheck(
579564
fDebuggerAttached = FALSE;
580565
}
581566

582-
if (!g_fNsiApiNotSupported)
567+
//
568+
// NsiAPI(GetExtendedTcpTable) is supported. we should check whether processIds match
569+
//
570+
if (dwActualProcessId == m_dwProcessId)
583571
{
584-
//
585-
// NsiAPI(GetExtendedTcpTable) is supported. we should check whether processIds matche
586-
//
587-
if (dwActualProcessId == m_dwProcessId)
572+
m_dwListeningProcessId = m_dwProcessId;
573+
fProcessMatch = TRUE;
574+
}
575+
576+
if (!fProcessMatch)
577+
{
578+
// could be the scenario that backend creates child process
579+
if (FAILED_LOG(hr = GetChildProcessHandles()))
588580
{
589-
m_dwListeningProcessId = m_dwProcessId;
590-
fProcessMatch = TRUE;
581+
goto Finished;
591582
}
592583

593-
if (!fProcessMatch)
584+
for (DWORD i = 0; i < m_cChildProcess; ++i)
594585
{
595-
// could be the scenario that backend creates child process
596-
if (FAILED_LOG(hr = GetChildProcessHandles()))
586+
// a child process listen on the assigned port
587+
if (dwActualProcessId == m_dwChildProcessIds[i])
597588
{
598-
goto Finished;
599-
}
589+
m_dwListeningProcessId = m_dwChildProcessIds[i];
590+
fProcessMatch = TRUE;
600591

601-
for (DWORD i = 0; i < m_cChildProcess; ++i)
602-
{
603-
// a child process listen on the assigned port
604-
if (dwActualProcessId == m_dwChildProcessIds[i])
592+
if (m_hChildProcessHandles[i] != NULL)
605593
{
606-
m_dwListeningProcessId = m_dwChildProcessIds[i];
607-
fProcessMatch = TRUE;
594+
if (fDebuggerAttached == FALSE &&
595+
CheckRemoteDebuggerPresent(m_hChildProcessHandles[i], &fDebuggerAttached) == 0)
596+
{
597+
// some error occurred - assume debugger is not attached;
598+
fDebuggerAttached = FALSE;
599+
}
608600

609-
if (m_hChildProcessHandles[i] != NULL)
601+
if (FAILED_LOG(hr = RegisterProcessWait(&m_hChildProcessWaitHandles[i],
602+
m_hChildProcessHandles[i])))
610603
{
611-
if (fDebuggerAttached == FALSE &&
612-
CheckRemoteDebuggerPresent(m_hChildProcessHandles[i], &fDebuggerAttached) == 0)
613-
{
614-
// some error occurred - assume debugger is not attached;
615-
fDebuggerAttached = FALSE;
616-
}
617-
618-
if (FAILED_LOG(hr = RegisterProcessWait(&m_hChildProcessWaitHandles[i],
619-
m_hChildProcessHandles[i])))
620-
{
621-
goto Finished;
622-
}
623-
iChildProcessIndex = i;
604+
goto Finished;
624605
}
625-
break;
606+
iChildProcessIndex = i;
626607
}
608+
break;
627609
}
628610
}
611+
}
629612

630-
if(!fProcessMatch)
631-
{
632-
//
633-
// process that we created is not listening
634-
// on the port we specified.
635-
//
636-
fReady = FALSE;
637-
hr = HRESULT_FROM_WIN32(ERROR_CREATE_FAILED);
638-
strEventMsg.SafeSnwprintf(
639-
ASPNETCORE_EVENT_PROCESS_START_WRONGPORT_ERROR_MSG,
640-
m_struAppFullPath.QueryStr(),
641-
m_struPhysicalPath.QueryStr(),
642-
m_struCommandLine.QueryStr(),
643-
m_dwPort,
644-
hr);
645-
goto Finished;
646-
}
613+
if (!fProcessMatch)
614+
{
615+
//
616+
// process that we created is not listening
617+
// on the port we specified.
618+
//
619+
fReady = FALSE;
620+
hr = HRESULT_FROM_WIN32(ERROR_CREATE_FAILED);
621+
strEventMsg.SafeSnwprintf(
622+
ASPNETCORE_EVENT_PROCESS_START_WRONGPORT_ERROR_MSG,
623+
m_struAppFullPath.QueryStr(),
624+
m_struPhysicalPath.QueryStr(),
625+
m_struCommandLine.QueryStr(),
626+
m_dwPort,
627+
hr);
628+
goto Finished;
647629
}
648630

649631
if (!fReady)
@@ -709,12 +691,9 @@ SERVER_PROCESS::PostStartCheck(
709691
}
710692
}
711693

712-
if (!g_fNsiApiNotSupported)
713-
{
714-
m_hListeningProcessHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE | PROCESS_DUP_HANDLE,
715-
FALSE,
716-
m_dwListeningProcessId);
717-
}
694+
m_hListeningProcessHandle = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE | PROCESS_DUP_HANDLE,
695+
FALSE,
696+
m_dwListeningProcessId);
718697

719698
//
720699
// mark server process as Ready
@@ -1200,7 +1179,6 @@ SERVER_PROCESS::CheckIfServerIsUp(
12001179
MIB_TCPROW_OWNER_PID *pOwner = NULL;
12011180
DWORD dwSize = 1000; // Initial size for pTCPInfo buffer
12021181
int iResult = 0;
1203-
SOCKADDR_IN sockAddr;
12041182
SOCKET socketCheck = INVALID_SOCKET;
12051183

12061184
DBG_ASSERT(pfReady);
@@ -1212,93 +1190,48 @@ SERVER_PROCESS::CheckIfServerIsUp(
12121190
//
12131191
*pdwProcessId = 0;
12141192

1215-
if (!g_fNsiApiNotSupported)
1193+
while (dwResult == ERROR_INSUFFICIENT_BUFFER)
12161194
{
1217-
while (dwResult == ERROR_INSUFFICIENT_BUFFER)
1218-
{
1219-
// Increase the buffer size with additional space, MIB_TCPROW 20 bytes
1220-
// New entries may be added by other processes before calling GetExtendedTcpTable
1221-
dwSize += 200;
1222-
1223-
if (pTCPInfo != NULL)
1224-
{
1225-
HeapFree(GetProcessHeap(), 0, pTCPInfo);
1226-
}
1195+
// Increase the buffer size with additional space, MIB_TCPROW 20 bytes
1196+
// New entries may be added by other processes before calling GetExtendedTcpTable
1197+
dwSize += 200;
12271198

1228-
pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize);
1229-
if (pTCPInfo == NULL)
1230-
{
1231-
hr = E_OUTOFMEMORY;
1232-
goto Finished;
1233-
}
1234-
1235-
dwResult = GetExtendedTcpTable(pTCPInfo,
1236-
&dwSize,
1237-
FALSE,
1238-
AF_INET,
1239-
TCP_TABLE_OWNER_PID_LISTENER,
1240-
0);
1241-
1242-
if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
1243-
{
1244-
hr = HRESULT_FROM_WIN32(dwResult);
1245-
goto Finished;
1246-
}
1247-
}
1248-
1249-
// iterate pTcpInfo struct to find PID/PORT entry
1250-
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
1199+
if (pTCPInfo != NULL)
12511200
{
1252-
pOwner = &pTCPInfo->table[dwLoop];
1253-
if (ntohs((USHORT)pOwner->dwLocalPort) == dwPort)
1254-
{
1255-
*pdwProcessId = pOwner->dwOwningPid;
1256-
*pfReady = TRUE;
1257-
break;
1258-
}
1201+
HeapFree(GetProcessHeap(), 0, pTCPInfo);
12591202
}
1260-
}
1261-
else
1262-
{
1263-
//
1264-
// We have to open socket to ping the service
1265-
//
1266-
socketCheck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
12671203

1268-
if (socketCheck == INVALID_SOCKET)
1204+
pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc(GetProcessHeap(), 0, dwSize);
1205+
if (pTCPInfo == NULL)
12691206
{
1270-
hr = HRESULT_FROM_WIN32(WSAGetLastError());
1207+
hr = E_OUTOFMEMORY;
12711208
goto Finished;
12721209
}
12731210

1274-
sockAddr.sin_family = AF_INET;
1275-
if (!inet_pton(AF_INET, LOCALHOST, &(sockAddr.sin_addr)))
1211+
dwResult = GetExtendedTcpTable(pTCPInfo,
1212+
&dwSize,
1213+
FALSE,
1214+
AF_INET,
1215+
TCP_TABLE_OWNER_PID_LISTENER,
1216+
0);
1217+
1218+
if (dwResult != NO_ERROR && dwResult != ERROR_INSUFFICIENT_BUFFER)
12761219
{
1277-
hr = HRESULT_FROM_WIN32(WSAGetLastError());
1220+
hr = HRESULT_FROM_WIN32(dwResult);
12781221
goto Finished;
12791222
}
1223+
}
12801224

1281-
//sockAddr.sin_addr.s_addr = inet_addr( LOCALHOST );
1282-
sockAddr.sin_port = htons((u_short)dwPort);
1283-
1284-
//
1285-
// Connect to server.
1286-
// if connection fails, socket is not closed, we reuse the same socket
1287-
// while retrying
1288-
//
1289-
iResult = connect(socketCheck, (SOCKADDR *)&sockAddr, sizeof(sockAddr));
1290-
if (iResult == SOCKET_ERROR)
1225+
// iterate pTcpInfo struct to find PID/PORT entry
1226+
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
1227+
{
1228+
pOwner = &pTCPInfo->table[dwLoop];
1229+
if (ntohs((USHORT)pOwner->dwLocalPort) == dwPort)
12911230
{
1292-
hr = HRESULT_FROM_WIN32(WSAGetLastError());
1293-
if (hr == HRESULT_FROM_WIN32(WSAECONNREFUSED))
1294-
{
1295-
// WSAECONNREFUSED means no application listen on the given port.
1296-
// This is not a failure. Reset the hresult to S_OK and return fReady to false
1297-
hr = S_OK;
1298-
}
1299-
goto Finished;
1231+
*pdwProcessId = pOwner->dwOwningPid;
1232+
*pfReady = TRUE;
1233+
break;
13001234
}
1301-
*pfReady = TRUE;
13021235
}
13031236

13041237
Finished:

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/stdafx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ inline bool IsSpace(char ch)
109109
extern BOOL g_fAsyncDisconnectAvailable;
110110
extern BOOL g_fWinHttpNonBlockingCallbackAvailable;
111111
extern BOOL g_fWebSocketStaticInitialize;
112-
extern BOOL g_fNsiApiNotSupported;
113112
extern BOOL g_fEnableReferenceCountTracing;
114113
extern BOOL g_fProcessDetach;
115114
extern DWORD g_dwActiveServerProcesses;

0 commit comments

Comments
 (0)