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;
if (lpMachineName == NULL || wcslen(lpMachineName) == 0) LPWSTR lpszPipeName = L"\\\\.\\pipe\\Ntsvcs";
{
if (lpDatabaseName != NULL && wcscmp(lpDatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
{
return(NULL);
}
// Try to open a named pipe; wait for it, if necessary if(lpMachineName == NULL || wcslen(lpMachineName) == 0)
while (1) {
{ if(lpDatabaseName != NULL && wcscmp(lpDatabaseName, SERVICES_ACTIVE_DATABASEW) != 0)
hPipe = CreateFileW( { return(NULL); }
lpszPipeName, // pipe name
dwDesiredAccess,
0, // no sharing
NULL, // no security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid // Only connect to scm when event "SvcctrlStartEvent_A3725DX" is signaled
hStartEvent = OpenEvent(SYNCHRONIZE, FALSE, _T("SvcctrlStartEvent_A3725DX"));
if (hPipe != INVALID_HANDLE_VALUE) if(hStartEvent == NULL)
break; {
SetLastError(ERROR_DATABASE_DOES_NOT_EXIST);
// Exit if an error other than ERROR_PIPE_BUSY occurs return (NULL);
}
if (GetLastError() != ERROR_PIPE_BUSY) dwWait = WaitForSingleObject(hStartEvent, INFINITE);
{ if(dwWait == WAIT_FAILED)
return(NULL); {
} SetLastError(ERROR_ACCESS_DENIED);
return (NULL);
// All pipe instances are busy, so wait for 20 seconds }
CloseHandle(hStartEvent);
if (!WaitNamedPipeW(lpszPipeName, 20000))
{ // Try to open a named pipe; wait for it, if necessary
return(NULL); while(1)
} {
} hPipe = CreateFileW(lpszPipeName, // pipe name
dwDesiredAccess,
// The pipe connected; change to message-read mode 0, // no sharing
NULL, // no security attributes
dwMode = PIPE_READMODE_MESSAGE; OPEN_EXISTING, // opens existing pipe
fSuccess = SetNamedPipeHandleState( 0, // default attributes
hPipe, // pipe handle NULL); // no template file
&dwMode, // new pipe mode
NULL, // don't set maximum bytes // Break if the pipe handle is valid
NULL); // don't set maximum time if(hPipe != INVALID_HANDLE_VALUE)
if (!fSuccess) break;
{
CloseHandle(hPipe); // Exit if an error other than ERROR_PIPE_BUSY occurs
return(NULL); if(GetLastError()!= ERROR_PIPE_BUSY)
} { return(NULL); }
// All pipe instances are busy, so wait for 20 seconds
if(!WaitNamedPipeW(lpszPipeName, 20000))
{ return(NULL); }
}
// The pipe connected; change to message-read mode
dwMode = PIPE_READMODE_MESSAGE;
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(
fSuccess = WriteFile( hPipe, // pipe handle
hPipe, // pipe handle lpvMessage, // message
lpvMessage, // message strlen(lpvMessage) + 1, // message length
strlen(lpvMessage) + 1, // message length &cbWritten, // bytes written
&cbWritten, // bytes written NULL); // not overlapped
NULL); // not overlapped if(!fSuccess)
if (!fSuccess) {
{ CloseHandle(hPipe);
CloseHandle(hPipe); return(NULL);
return(NULL); }
}
do
do {
{ // Read from the pipe
// Read from the pipe fSuccess = ReadFile(
hPipe, // pipe handle
fSuccess = ReadFile( chBuf, // buffer to receive reply
hPipe, // pipe handle 512, // size of buffer
chBuf, // buffer to receive reply &cbRead, // number of bytes read
512, // size of buffer NULL); // not overlapped
&cbRead, // number of bytes read
NULL); // not overlapped if(! fSuccess && GetLastError() != ERROR_MORE_DATA)
break;
if (! fSuccess && GetLastError() != ERROR_MORE_DATA)
break; // Reply from the pipe is written to STDOUT.
if(!WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), chBuf, cbRead, &cbWritten, NULL))
// Reply from the pipe is written to STDOUT. break;
} while(!fSuccess); // repeat loop if ERROR_MORE_DATA
if (!WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),
chBuf, cbRead, &cbWritten, NULL)) //CloseHandle(hPipe);
{
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);
} }
} }