OpenSCManager waits for services.exe to initialize

svn path=/trunk/; revision=3295
This commit is contained in:
Jurgen van Gael 2002-07-23 08:16:05 +00:00
parent 57a74268f7
commit 3ce45b038f

View file

@ -1,4 +1,4 @@
/* $Id: scm.c,v 1.9 2002/07/20 13:31:34 ekohl Exp $ /* $Id: scm.c,v 1.10 2002/07/23 08:16:05 jvangael Exp $
* *
* COPYRIGHT: See COPYING in the top level directory * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries * PROJECT: ReactOS system libraries
@ -407,117 +407,116 @@ OpenSCManagerA(LPCSTR lpMachineName,
/********************************************************************** /**********************************************************************
* OpenSCManagerW * OpenSCManagerW
*/ */
SC_HANDLE STDCALL SC_HANDLE STDCALL OpenSCManagerW(LPCWSTR lpMachineName,
OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName,
LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
DWORD dwDesiredAccess)
{ {
HANDLE hPipe; HANDLE hPipe;
DWORD dwMode; DWORD dwMode;
BOOL fSuccess; DWORD dwWait;
LPWSTR lpszPipeName = L"\\\\.\\pipe\\Ntsvcs"; BOOL fSuccess;
HANDLE hStartEvent;
LPWSTR lpszPipeName = L"\\\\.\\pipe\\Ntsvcs";
if (lpMachineName == NULL || wcslen(lpMachineName) == 0) if(lpMachineName == NULL || wcslen(lpMachineName) == 0)
{ {
if (lpDatabaseName != NULL && wcscmp(lpDatabaseName, SERVICES_ACTIVE_DATABASEW) != 0) if(lpDatabaseName != NULL && wcscmp(lpDatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
{ { return(NULL); }
return(NULL);
}
// Try to open a named pipe; wait for it, if necessary // Only connect to scm when event "SvcctrlStartEvent_A3725DX" is signaled
while (1) hStartEvent = OpenEvent(SYNCHRONIZE, FALSE, _T("SvcctrlStartEvent_A3725DX"));
{ if(hStartEvent == NULL)
hPipe = CreateFileW( {
lpszPipeName, // pipe name SetLastError(ERROR_DATABASE_DOES_NOT_EXIST);
dwDesiredAccess, return (NULL);
0, // no sharing }
NULL, // no security attributes dwWait = WaitForSingleObject(hStartEvent, INFINITE);
OPEN_EXISTING, // opens existing pipe if(dwWait == WAIT_FAILED)
0, // default attributes {
NULL); // no template file SetLastError(ERROR_ACCESS_DENIED);
return (NULL);
}
CloseHandle(hStartEvent);
// Break if the pipe handle is valid // Try to open a named pipe; wait for it, if necessary
while(1)
{
hPipe = CreateFileW(lpszPipeName, // pipe name
dwDesiredAccess,
0, // no sharing
NULL, // no security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (hPipe != INVALID_HANDLE_VALUE) // Break if the pipe handle is valid
break; if(hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs // Exit if an error other than ERROR_PIPE_BUSY occurs
if(GetLastError()!= ERROR_PIPE_BUSY)
{ return(NULL); }
if (GetLastError() != ERROR_PIPE_BUSY) // All pipe instances are busy, so wait for 20 seconds
{ if(!WaitNamedPipeW(lpszPipeName, 20000))
return(NULL); { return(NULL); }
} }
// All pipe instances are busy, so wait for 20 seconds // The pipe connected; change to message-read mode
dwMode = PIPE_READMODE_MESSAGE;
if (!WaitNamedPipeW(lpszPipeName, 20000)) fSuccess = SetNamedPipeHandleState(
{ hPipe, // pipe handle
return(NULL); &dwMode, // new pipe mode
} NULL, // don't set maximum bytes
} NULL); // don't set maximum time
if(!fSuccess)
// The pipe connected; change to message-read mode {
CloseHandle(hPipe);
dwMode = PIPE_READMODE_MESSAGE; return(NULL);
fSuccess = SetNamedPipeHandleState( }
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if (!fSuccess)
{
CloseHandle(hPipe);
return(NULL);
}
#if 0 #if 0
// Send a message to the pipe server // Send a message to the pipe server
lpvMessage = (argc > 1) ? argv[1] : "default message";
lpvMessage = (argc > 1) ? argv[1] : "default message"; fSuccess = WriteFile(
hPipe, // pipe handle
lpvMessage, // message
strlen(lpvMessage) + 1, // message length
&cbWritten, // bytes written
NULL); // not overlapped
if(!fSuccess)
{
CloseHandle(hPipe);
return(NULL);
}
fSuccess = WriteFile( do
hPipe, // pipe handle {
lpvMessage, // message // Read from the pipe
strlen(lpvMessage) + 1, // message length fSuccess = ReadFile(
&cbWritten, // bytes written hPipe, // pipe handle
NULL); // not overlapped chBuf, // buffer to receive reply
if (!fSuccess) 512, // size of buffer
{ &cbRead, // number of bytes read
CloseHandle(hPipe); NULL); // not overlapped
return(NULL);
}
do if(! fSuccess && GetLastError() != ERROR_MORE_DATA)
{ break;
// Read from the pipe
fSuccess = ReadFile( // Reply from the pipe is written to STDOUT.
hPipe, // pipe handle if(!WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), chBuf, cbRead, &cbWritten, NULL))
chBuf, // buffer to receive reply break;
512, // size of buffer } while(!fSuccess); // repeat loop if ERROR_MORE_DATA
&cbRead, // number of bytes read
NULL); // not overlapped
if (! fSuccess && GetLastError() != ERROR_MORE_DATA) //CloseHandle(hPipe);
break;
// Reply from the pipe is written to STDOUT.
if (!WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
chBuf, cbRead, &cbWritten, NULL))
{
break;
}
} while (!fSuccess); // repeat loop if ERROR_MORE_DATA
//CloseHandle(hPipe);
#endif #endif
return(hPipe); return(hPipe);
} }
else else
{ {
/* FIXME: Connect to remote SCM */ /* FIXME: Connect to remote SCM */
return(NULL); return(NULL);
} }
} }