7
7
#include " EventLog.h"
8
8
#include " file_utility.h"
9
9
#include " exceptions.h"
10
- // #include <share.h>
11
-
12
- // extern BOOL g_fNsiApiNotSupported;
13
10
14
11
#define STARTUP_TIME_LIMIT_INCREMENT_IN_MILLISECONDS 5000
15
12
@@ -114,33 +111,21 @@ SERVER_PROCESS::GetRandomPort
114
111
DWORD dwActualProcessId = 0 ;
115
112
116
113
std::uniform_int_distribution<> dist (MIN_PORT_RANDOM, MAX_PORT);
117
-
118
- if (g_fNsiApiNotSupported)
114
+ DWORD cRetry = 0 ;
115
+ do
119
116
{
120
117
//
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.
123
121
//
124
122
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);
139
125
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);
144
129
}
145
130
146
131
return hr;
@@ -579,71 +564,68 @@ SERVER_PROCESS::PostStartCheck(
579
564
fDebuggerAttached = FALSE ;
580
565
}
581
566
582
- if (!g_fNsiApiNotSupported)
567
+ //
568
+ // NsiAPI(GetExtendedTcpTable) is supported. we should check whether processIds match
569
+ //
570
+ if (dwActualProcessId == m_dwProcessId)
583
571
{
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 ()))
588
580
{
589
- m_dwListeningProcessId = m_dwProcessId;
590
- fProcessMatch = TRUE ;
581
+ goto Finished;
591
582
}
592
583
593
- if (! fProcessMatch )
584
+ for (DWORD i = 0 ; i < m_cChildProcess; ++i )
594
585
{
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] )
597
588
{
598
- goto Finished ;
599
- }
589
+ m_dwListeningProcessId = m_dwChildProcessIds[i] ;
590
+ fProcessMatch = TRUE ;
600
591
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 )
605
593
{
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
+ }
608
600
609
- if (m_hChildProcessHandles[i] != NULL )
601
+ if (FAILED_LOG (hr = RegisterProcessWait (&m_hChildProcessWaitHandles[i],
602
+ m_hChildProcessHandles[i])))
610
603
{
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;
624
605
}
625
- break ;
606
+ iChildProcessIndex = i ;
626
607
}
608
+ break ;
627
609
}
628
610
}
611
+ }
629
612
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;
647
629
}
648
630
649
631
if (!fReady )
@@ -709,12 +691,9 @@ SERVER_PROCESS::PostStartCheck(
709
691
}
710
692
}
711
693
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);
718
697
719
698
//
720
699
// mark server process as Ready
@@ -1200,7 +1179,6 @@ SERVER_PROCESS::CheckIfServerIsUp(
1200
1179
MIB_TCPROW_OWNER_PID *pOwner = NULL ;
1201
1180
DWORD dwSize = 1000 ; // Initial size for pTCPInfo buffer
1202
1181
int iResult = 0 ;
1203
- SOCKADDR_IN sockAddr;
1204
1182
SOCKET socketCheck = INVALID_SOCKET;
1205
1183
1206
1184
DBG_ASSERT (pfReady);
@@ -1212,93 +1190,48 @@ SERVER_PROCESS::CheckIfServerIsUp(
1212
1190
//
1213
1191
*pdwProcessId = 0 ;
1214
1192
1215
- if (!g_fNsiApiNotSupported )
1193
+ while (dwResult == ERROR_INSUFFICIENT_BUFFER )
1216
1194
{
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 ;
1227
1198
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 )
1251
1200
{
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);
1259
1202
}
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);
1267
1203
1268
- if (socketCheck == INVALID_SOCKET)
1204
+ pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)HeapAlloc (GetProcessHeap (), 0 , dwSize);
1205
+ if (pTCPInfo == NULL )
1269
1206
{
1270
- hr = HRESULT_FROM_WIN32 ( WSAGetLastError ()) ;
1207
+ hr = E_OUTOFMEMORY ;
1271
1208
goto Finished;
1272
1209
}
1273
1210
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)
1276
1219
{
1277
- hr = HRESULT_FROM_WIN32 (WSAGetLastError () );
1220
+ hr = HRESULT_FROM_WIN32 (dwResult );
1278
1221
goto Finished;
1279
1222
}
1223
+ }
1280
1224
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)
1291
1230
{
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 ;
1300
1234
}
1301
- *pfReady = TRUE ;
1302
1235
}
1303
1236
1304
1237
Finished:
0 commit comments