Skip to content

Commit

Permalink
Backport fix for bug CORE-2356 : On Windows listener process of Class…
Browse files Browse the repository at this point in the history
…ic Server can't create necessary resources after restart if any worker process was present
  • Loading branch information
hvlad committed Sep 11, 2010
1 parent c1ab5a8 commit 807c777
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/remote/inet.cpp
Expand Up @@ -2005,7 +2005,8 @@ static int fork( SOCKET old_handle, USHORT flag)
}

Firebird::string cmdLine;
cmdLine.printf("%s -i -h %"SLONGFORMAT, name, (SLONG) new_handle);
cmdLine.printf("%s -i -h %"SLONGFORMAT"@%"SLONGFORMAT, name, (SLONG) new_handle,
GetCurrentProcessId());

STARTUPINFO start_crud;
start_crud.cb = sizeof(STARTUPINFO);
Expand All @@ -2017,12 +2018,13 @@ static int fork( SOCKET old_handle, USHORT flag)
start_crud.dwFlags = STARTF_FORCEOFFFEEDBACK;

PROCESS_INFORMATION pi;
if (CreateProcess(NULL, cmdLine.begin(), NULL, NULL, TRUE,
if (CreateProcess(NULL, cmdLine.begin(), NULL, NULL, FALSE,
(flag & SRVR_high_priority ?
HIGH_PRIORITY_CLASS | DETACHED_PROCESS :
NORMAL_PRIORITY_CLASS | DETACHED_PROCESS),
NULL, NULL, &start_crud, &pi))
{
// hvlad: child process will close our handle of just accepted socket
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 1;
Expand Down
44 changes: 39 additions & 5 deletions src/remote/os/win32/srvr_w32.cpp
Expand Up @@ -587,13 +587,47 @@ static HANDLE parse_args( LPCSTR lpszArgs, USHORT * pserver_flag)
case 'H':
while (*p && *p == ' ')
p++;
if (*p) {
char *pp = buffer;
while (*p && *p != ' ') {
*pp++ = *p++;
if (*p)
{
TEXT buffer[32];
char* pp = buffer;
while (*p && *p != ' ' && (pp - buffer < sizeof(buffer) - 1))
{
if (*p == '@')
{
p++;
*pp++ = '\0';
connection_handle = (HANDLE) _atoi64(buffer);
pp = buffer;
}
else
*pp++ = *p++;
}
*pp++ = '\0';
connection_handle = (HANDLE) atol(buffer);

if (connection_handle == INVALID_HANDLE_VALUE)
{
connection_handle = (HANDLE) _atoi64(buffer);
}
else
{
const DWORD parent_id = atol(buffer);
const HANDLE parent_handle = OpenProcess(PROCESS_DUP_HANDLE, FALSE, parent_id);
if (!parent_handle)
{
gds__log("SERVER: OpenProcess failed. Errno = %d, parent PID = %d", GetLastError(), parent_id);
exit(FINI_ERROR);
}

if (!DuplicateHandle(parent_handle, connection_handle, GetCurrentProcess(), &connection_handle,
0, FALSE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
{
gds__log("SERVER: DuplicateHandle failed. Errno = %d, parent PID = %d", GetLastError(), parent_id);
exit(FINI_ERROR);
}

CloseHandle(parent_handle);
}
}
break;
#endif
Expand Down
12 changes: 9 additions & 3 deletions src/remote/os/win32/wnet.cpp
Expand Up @@ -416,7 +416,8 @@ rem_port* WNET_connect(const TEXT* name,
GetModuleFileName(NULL, name, sizeof(name));

Firebird::string cmdLine;
cmdLine.printf("%s -w -h %"SLONGFORMAT, name, (SLONG) port->port_handle);
cmdLine.printf("%s -w -h %"SLONGFORMAT"@%"SLONGFORMAT, name, (SLONG) port->port_handle,
GetCurrentProcessId());

STARTUPINFO start_crud;
PROCESS_INFORMATION pi;
Expand All @@ -428,16 +429,21 @@ rem_port* WNET_connect(const TEXT* name,
start_crud.lpTitle = NULL;
start_crud.dwFlags = STARTF_FORCEOFFFEEDBACK;

if (CreateProcess(NULL, cmdLine.begin(), NULL, NULL, TRUE,
if (CreateProcess(NULL, cmdLine.begin(), NULL, NULL, FALSE,
(flag & SRVR_high_priority ?
HIGH_PRIORITY_CLASS | DETACHED_PROCESS :
NORMAL_PRIORITY_CLASS | DETACHED_PROCESS),
NULL, NULL, &start_crud, &pi))
{
// hvlad: child process will close our handle of client pipe
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
CloseHandle(port->port_handle);
else
{
gds__log("WNET/inet_error: fork/CreateProcess errno = %d", GetLastError());
CloseHandle(port->port_handle);
}
}
#endif /* REQUESTER */
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote/xnet.cpp
Expand Up @@ -2438,7 +2438,7 @@ static bool fork(ULONG client_pid, USHORT flag, ULONG* forked_pid)
PROCESS_INFORMATION pi;

const bool cp_result =
CreateProcess(NULL, cmdLine.begin(), NULL, NULL, TRUE,
CreateProcess(NULL, cmdLine.begin(), NULL, NULL, FALSE,
(flag & SRVR_high_priority ? HIGH_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS)
| DETACHED_PROCESS | CREATE_SUSPENDED,
NULL, NULL, &start_crud, &pi);
Expand Down

0 comments on commit 807c777

Please sign in to comment.