launcher, launchctl: refactoring

This commit is contained in:
Bill Zissimopoulos 2016-05-13 09:28:56 -07:00
parent 26a9bb714b
commit 35d2b3f626
2 changed files with 66 additions and 66 deletions

View File

@ -61,7 +61,7 @@ static void usage(void)
PROGNAME); PROGNAME);
} }
static int callpipe_and_report(PWSTR PipeBuf, ULONG SendSize, ULONG RecvSize) static int call_pipe_and_report(PWSTR PipeBuf, ULONG SendSize, ULONG RecvSize)
{ {
DWORD LastError, BytesTransferred; DWORD LastError, BytesTransferred;
@ -131,7 +131,7 @@ int start(PWSTR PipeBuf, ULONG PipeBufSize,
memcpy(P, Argv[Argi], ArgvSize * sizeof(WCHAR)); P += ArgvSize; memcpy(P, Argv[Argi], ArgvSize * sizeof(WCHAR)); P += ArgvSize;
} }
return callpipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize); return call_pipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize);
} }
int stop(PWSTR PipeBuf, ULONG PipeBufSize, int stop(PWSTR PipeBuf, ULONG PipeBufSize,
@ -151,20 +151,7 @@ int stop(PWSTR PipeBuf, ULONG PipeBufSize,
memcpy(P, ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize; memcpy(P, ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize;
memcpy(P, InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize; memcpy(P, InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize;
return callpipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize); return call_pipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize);
}
int list(PWSTR PipeBuf, ULONG PipeBufSize)
{
PWSTR P;
if (PipeBufSize < 1 * sizeof(WCHAR))
return ERROR_INVALID_PARAMETER;
P = PipeBuf;
*P++ = LauncherSvcInstanceList;
return callpipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize);
} }
int getinfo(PWSTR PipeBuf, ULONG PipeBufSize, int getinfo(PWSTR PipeBuf, ULONG PipeBufSize,
@ -184,7 +171,20 @@ int getinfo(PWSTR PipeBuf, ULONG PipeBufSize,
memcpy(P, ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize; memcpy(P, ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize;
memcpy(P, InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize; memcpy(P, InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize;
return callpipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize); return call_pipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize);
}
int list(PWSTR PipeBuf, ULONG PipeBufSize)
{
PWSTR P;
if (PipeBufSize < 1 * sizeof(WCHAR))
return ERROR_INVALID_PARAMETER;
P = PipeBuf;
*P++ = LauncherSvcInstanceList;
return call_pipe_and_report(PipeBuf, (ULONG)((P - PipeBuf) * sizeof(WCHAR)), PipeBufSize);
} }
int wmain(int argc, wchar_t **argv) int wmain(int argc, wchar_t **argv)
@ -218,14 +218,6 @@ int wmain(int argc, wchar_t **argv)
return stop(PipeBuf, PIPE_BUFFER_SIZE, argv[1], argv[2]); return stop(PipeBuf, PIPE_BUFFER_SIZE, argv[1], argv[2]);
} }
else else
if (0 == lstrcmpW(L"list", argv[0]))
{
if (1 != argc)
usage();
return list(PipeBuf, PIPE_BUFFER_SIZE);
}
else
if (0 == lstrcmpW(L"info", argv[0])) if (0 == lstrcmpW(L"info", argv[0]))
{ {
if (3 != argc) if (3 != argc)
@ -233,6 +225,14 @@ int wmain(int argc, wchar_t **argv)
return getinfo(PipeBuf, PIPE_BUFFER_SIZE, argv[1], argv[2]); return getinfo(PipeBuf, PIPE_BUFFER_SIZE, argv[1], argv[2]);
} }
else
if (0 == lstrcmpW(L"list", argv[0]))
{
if (1 != argc)
usage();
return list(PipeBuf, PIPE_BUFFER_SIZE);
}
else else
usage(); usage();

View File

@ -374,40 +374,6 @@ exit:
return Result; return Result;
} }
NTSTATUS SvcInstanceGetNameList(HANDLE ClientToken,
PWSTR Buffer, PULONG PSize)
{
SVC_INSTANCE *SvcInstance;
PLIST_ENTRY ListEntry;
PWSTR P = Buffer, BufferEnd = P + *PSize / sizeof(WCHAR);
ULONG ClassNameSize, InstanceNameSize;
EnterCriticalSection(&SvcInstanceLock);
for (ListEntry = SvcInstanceList.Flink;
&SvcInstanceList != ListEntry;
ListEntry = ListEntry->Flink)
{
SvcInstance = CONTAINING_RECORD(ListEntry, SVC_INSTANCE, ListEntry);
ClassNameSize = lstrlenW(SvcInstance->ClassName) + 1;
InstanceNameSize = lstrlenW(SvcInstance->InstanceName) + 1;
if (BufferEnd < P + ClassNameSize + InstanceNameSize)
break;
memcpy(P, SvcInstance->ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize;
*Buffer++ = L' ';
memcpy(P, SvcInstance->InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize;
}
LeaveCriticalSection(&SvcInstanceLock);
*PSize = (ULONG)(P - Buffer);
return STATUS_SUCCESS;
}
NTSTATUS SvcInstanceGetInfo(HANDLE ClientToken, NTSTATUS SvcInstanceGetInfo(HANDLE ClientToken,
PWSTR ClassName, PWSTR InstanceName, PWSTR Buffer, PULONG PSize) PWSTR ClassName, PWSTR InstanceName, PWSTR Buffer, PULONG PSize)
{ {
@ -453,6 +419,40 @@ exit:
return Result; return Result;
} }
NTSTATUS SvcInstanceGetNameList(HANDLE ClientToken,
PWSTR Buffer, PULONG PSize)
{
SVC_INSTANCE *SvcInstance;
PLIST_ENTRY ListEntry;
PWSTR P = Buffer, BufferEnd = P + *PSize / sizeof(WCHAR);
ULONG ClassNameSize, InstanceNameSize;
EnterCriticalSection(&SvcInstanceLock);
for (ListEntry = SvcInstanceList.Flink;
&SvcInstanceList != ListEntry;
ListEntry = ListEntry->Flink)
{
SvcInstance = CONTAINING_RECORD(ListEntry, SVC_INSTANCE, ListEntry);
ClassNameSize = lstrlenW(SvcInstance->ClassName) + 1;
InstanceNameSize = lstrlenW(SvcInstance->InstanceName) + 1;
if (BufferEnd < P + ClassNameSize + InstanceNameSize)
break;
memcpy(P, SvcInstance->ClassName, ClassNameSize * sizeof(WCHAR)); P += ClassNameSize;
*Buffer++ = L' ';
memcpy(P, SvcInstance->InstanceName, InstanceNameSize * sizeof(WCHAR)); P += InstanceNameSize;
}
LeaveCriticalSection(&SvcInstanceLock);
*PSize = (ULONG)(P - Buffer);
return STATUS_SUCCESS;
}
static HANDLE SvcThread, SvcEvent; static HANDLE SvcThread, SvcEvent;
static HANDLE SvcPipe = INVALID_HANDLE_VALUE; static HANDLE SvcPipe = INVALID_HANDLE_VALUE;
static OVERLAPPED SvcOverlapped; static OVERLAPPED SvcOverlapped;
@ -735,13 +735,6 @@ static VOID SvcPipeTransact(HANDLE ClientToken, PWSTR PipeBuf, PULONG PSize)
SvcPipeTransactResult(Result, PipeBuf, PSize); SvcPipeTransactResult(Result, PipeBuf, PSize);
break; break;
case LauncherSvcInstanceList:
*PSize = PIPE_BUFFER_SIZE - 1;
Result = SvcInstanceGetNameList(ClientToken, PipeBuf + 1, PSize);
SvcPipeTransactResult(Result, PipeBuf, PSize);
break;
case LauncherSvcInstanceInfo: case LauncherSvcInstanceInfo:
ClassName = SvcPipeTransactGetPart(&P, PipeBufEnd); ClassName = SvcPipeTransactGetPart(&P, PipeBufEnd);
InstanceName = SvcPipeTransactGetPart(&P, PipeBufEnd); InstanceName = SvcPipeTransactGetPart(&P, PipeBufEnd);
@ -756,6 +749,13 @@ static VOID SvcPipeTransact(HANDLE ClientToken, PWSTR PipeBuf, PULONG PSize)
SvcPipeTransactResult(Result, PipeBuf, PSize); SvcPipeTransactResult(Result, PipeBuf, PSize);
break; break;
case LauncherSvcInstanceList:
*PSize = PIPE_BUFFER_SIZE - 1;
Result = SvcInstanceGetNameList(ClientToken, PipeBuf + 1, PSize);
SvcPipeTransactResult(Result, PipeBuf, PSize);
break;
default: default:
SvcPipeTransactResult(STATUS_INVALID_PARAMETER, PipeBuf, PSize); SvcPipeTransactResult(STATUS_INVALID_PARAMETER, PipeBuf, PSize);
break; break;