- Reorganized some commands in order to add erros and success messages.
- Added error and success messages.
- Enabled usage text for the failure command.

svn path=/trunk/; revision=71645
This commit is contained in:
Eric Kohl 2016-06-15 21:28:33 +00:00
parent 3320cc95a2
commit ffe9e79d41
7 changed files with 153 additions and 104 deletions

View file

@ -16,9 +16,11 @@ Control(DWORD Control,
INT ArgCount) INT ArgCount)
{ {
SC_HANDLE hSCManager = NULL; SC_HANDLE hSCManager = NULL;
SC_HANDLE hSc = NULL; SC_HANDLE hService = NULL;
SERVICE_STATUS Status; SERVICE_STATUS Status;
DWORD dwDesiredAccess = 0; DWORD dwDesiredAccess = 0;
BOOL bResult = TRUE;
SERVICE_STATUS_PROCESS StatusEx;
#ifdef SCDBG #ifdef SCDBG
LPCTSTR *TmpArgs = Args; LPCTSTR *TmpArgs = Args;
@ -62,40 +64,50 @@ Control(DWORD Control,
hSCManager = OpenSCManager(NULL, hSCManager = OpenSCManager(NULL,
NULL, NULL,
SC_MANAGER_CONNECT); SC_MANAGER_CONNECT);
if (hSCManager != NULL) if (hSCManager == NULL)
{ {
hSc = OpenService(hSCManager, _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
ServiceName, bResult = FALSE;
dwDesiredAccess); goto done;
if (hSc != NULL)
{
if (ControlService(hSc,
Control,
&Status))
{
SERVICE_STATUS_PROCESS StatusEx;
/* FIXME: lazy hack ;) */
CopyMemory(&StatusEx, &Status, sizeof(Status));
StatusEx.dwProcessId = 0;
StatusEx.dwServiceFlags = 0;
PrintService(ServiceName,
&StatusEx,
FALSE);
CloseServiceHandle(hSc);
CloseServiceHandle(hSCManager);
return TRUE;
}
}
else
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
} }
ReportLastError(); hService = OpenService(hSCManager,
if (hSc) CloseServiceHandle(hSc); ServiceName,
if (hSCManager) CloseServiceHandle(hSCManager); dwDesiredAccess);
return FALSE; if (hService == NULL)
{
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
bResult = FALSE;
goto done;
}
if (!ControlService(hService,
Control,
&Status))
{
_tprintf(_T("[SC] ControlService FAILED %lu:\n\n"), GetLastError());
bResult = FALSE;
goto done;
}
/* FIXME: lazy hack ;) */
CopyMemory(&StatusEx, &Status, sizeof(Status));
StatusEx.dwProcessId = 0;
StatusEx.dwServiceFlags = 0;
PrintService(ServiceName,
&StatusEx,
FALSE);
done:
if (!bResult)
ReportLastError();
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
return bResult;
} }

View file

@ -12,9 +12,9 @@
BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount) BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount)
{ {
SC_HANDLE hSCManager; SC_HANDLE hSCManager = NULL;
SC_HANDLE hSc; SC_HANDLE hService = NULL;
BOOL bRet = FALSE; BOOL bRet = TRUE;
INT i; INT i;
INT Length; INT Length;
@ -71,38 +71,45 @@ BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount)
#endif #endif
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (hSCManager == NULL)
if (hSCManager != NULL)
{ {
hSc = CreateService(hSCManager, _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
ServiceInfo.lpServiceName, bRet = FALSE;
ServiceInfo.lpDisplayName, goto done;
SERVICE_ALL_ACCESS,
ServiceInfo.dwServiceType,
ServiceInfo.dwStartType,
ServiceInfo.dwErrorControl,
ServiceInfo.lpBinaryPathName,
ServiceInfo.lpLoadOrderGroup,
ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
ServiceInfo.lpDependencies,
ServiceInfo.lpServiceStartName,
ServiceInfo.lpPassword);
if (hSc != NULL)
{
_tprintf(_T("[SC] CreateService SUCCESS\n"));
CloseServiceHandle(hSc);
bRet = TRUE;
}
else
ReportLastError();
CloseServiceHandle(hSCManager);
} }
else
hService = CreateService(hSCManager,
ServiceInfo.lpServiceName,
ServiceInfo.lpDisplayName,
SERVICE_ALL_ACCESS,
ServiceInfo.dwServiceType,
ServiceInfo.dwStartType,
ServiceInfo.dwErrorControl,
ServiceInfo.lpBinaryPathName,
ServiceInfo.lpLoadOrderGroup,
ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
ServiceInfo.lpDependencies,
ServiceInfo.lpServiceStartName,
ServiceInfo.lpPassword);
if (hService == NULL)
{
_tprintf(_T("[SC] CreateService FAILED %lu:\n\n"), GetLastError());
bRet = FALSE;
goto done;
}
_tprintf(_T("[SC] CreateService SUCCESS\n\n"));
done:
if (bRet == FALSE)
ReportLastError(); ReportLastError();
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
if (lpBuffer != NULL) if (lpBuffer != NULL)
HeapFree(GetProcessHeap(), 0, lpBuffer); HeapFree(GetProcessHeap(), 0, lpBuffer);

View file

@ -13,6 +13,7 @@ BOOL Delete(LPCTSTR ServiceName)
{ {
SC_HANDLE hSCManager = NULL; SC_HANDLE hSCManager = NULL;
SC_HANDLE hSc = NULL; SC_HANDLE hSc = NULL;
BOOL bRet = TRUE;
#ifdef SCDBG #ifdef SCDBG
_tprintf(_T("service to delete - %s\n\n"), ServiceName); _tprintf(_T("service to delete - %s\n\n"), ServiceName);
@ -21,27 +22,39 @@ BOOL Delete(LPCTSTR ServiceName)
hSCManager = OpenSCManager(NULL, hSCManager = OpenSCManager(NULL,
NULL, NULL,
SC_MANAGER_CONNECT); SC_MANAGER_CONNECT);
if (hSCManager != NULL) if (hSCManager == NULL)
{ {
hSc = OpenService(hSCManager, ServiceName, DELETE); _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
if (hSc != NULL) bRet = FALSE;
{ goto done;
if (DeleteService(hSc))
{
_tprintf(_T("[SC] DeleteService SUCCESS\n"));
CloseServiceHandle(hSc);
CloseServiceHandle(hSCManager);
return TRUE;
}
}
} }
ReportLastError(); hSc = OpenService(hSCManager, ServiceName, DELETE);
if (hSc == NULL)
{
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
bRet = FALSE;
goto done;
}
if (hSc) CloseServiceHandle(hSc); if (!DeleteService(hSc))
if (hSCManager) CloseServiceHandle(hSCManager); {
_tprintf(_T("[SC] DeleteService FAILED %lu:\n\n"), GetLastError());
bRet = FALSE;
goto done;
}
return FALSE; _tprintf(_T("[SC] DeleteService SUCCESS\n\n"));
done:
if (bRet == FALSE)
ReportLastError();
if (hSc)
CloseServiceHandle(hSc);
if (hSCManager)
CloseServiceHandle(hSCManager);
return bRet;
} }

View file

@ -26,6 +26,7 @@ BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
SC_MANAGER_CONNECT); SC_MANAGER_CONNECT);
if (hManager == NULL) if (hManager == NULL)
{ {
_tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -33,6 +34,7 @@ BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
hService = OpenService(hManager, ServiceName, WRITE_DAC); hService = OpenService(hManager, ServiceName, WRITE_DAC);
if (hService == NULL) if (hService == NULL)
{ {
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -42,6 +44,7 @@ BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
&pSecurityDescriptor, &pSecurityDescriptor,
&ulSecurityDescriptorSize)) &ulSecurityDescriptorSize))
{ {
_tprintf(_T("[SC] ConvertStringSecurityDescriptorToSecurityDescriptor FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -50,6 +53,7 @@ BOOL SdSet(LPCTSTR ServiceName, LPCTSTR StringSecurityDescriptor)
DACL_SECURITY_INFORMATION, DACL_SECURITY_INFORMATION,
pSecurityDescriptor)) pSecurityDescriptor))
{ {
_tprintf(_T("[SC] SetServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }

View file

@ -27,6 +27,7 @@ BOOL SdShow(LPCTSTR ServiceName)
SC_MANAGER_CONNECT); SC_MANAGER_CONNECT);
if (hManager == NULL) if (hManager == NULL)
{ {
_tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -34,6 +35,7 @@ BOOL SdShow(LPCTSTR ServiceName)
hService = OpenService(hManager, ServiceName, READ_CONTROL); hService = OpenService(hManager, ServiceName, READ_CONTROL);
if (hService == NULL) if (hService == NULL)
{ {
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -46,6 +48,7 @@ BOOL SdShow(LPCTSTR ServiceName)
{ {
if (cbBytesNeeded == 0) if (cbBytesNeeded == 0)
{ {
_tprintf(_T("[SC] QueryServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -55,6 +58,7 @@ BOOL SdShow(LPCTSTR ServiceName)
if (pSecurityDescriptor == NULL) if (pSecurityDescriptor == NULL)
{ {
SetLastError(ERROR_OUTOFMEMORY); SetLastError(ERROR_OUTOFMEMORY);
_tprintf(_T("[SC] HeapAlloc FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -65,6 +69,7 @@ BOOL SdShow(LPCTSTR ServiceName)
cbBytesNeeded, cbBytesNeeded,
&cbBytesNeeded)) &cbBytesNeeded))
{ {
_tprintf(_T("[SC] QueryServiceObjectSecurity FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }
@ -75,6 +80,7 @@ BOOL SdShow(LPCTSTR ServiceName)
&pStringBuffer, &pStringBuffer,
NULL)) NULL))
{ {
_tprintf(_T("[SC] ConvertSecurityDescriptorToStringSecurityDescriptor FAILED %lu:\n\n"), GetLastError());
bResult = FALSE; bResult = FALSE;
goto done; goto done;
} }

View file

@ -14,6 +14,7 @@ BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
SC_HANDLE hSCManager = NULL; SC_HANDLE hSCManager = NULL;
SC_HANDLE hSc = NULL; SC_HANDLE hSc = NULL;
LPSERVICE_STATUS_PROCESS pServiceInfo = NULL; LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;
BOOL bResult = TRUE;
#ifdef SCDBG #ifdef SCDBG
LPCTSTR *TmpArgs = ServiceArgs; LPCTSTR *TmpArgs = ServiceArgs;
@ -34,28 +35,33 @@ BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
SC_MANAGER_CONNECT); SC_MANAGER_CONNECT);
if (hSCManager == NULL) if (hSCManager == NULL)
{ {
ReportLastError(); _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
return FALSE; bResult = FALSE;
goto done;
} }
hSc = OpenService(hSCManager, hSc = OpenService(hSCManager,
ServiceName, ServiceName,
SERVICE_START | SERVICE_QUERY_STATUS); SERVICE_START | SERVICE_QUERY_STATUS);
if (hSc == NULL) if (hSc == NULL)
goto fail; {
_tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
bResult = FALSE;
goto done;
}
if (!ArgCount) if (!ArgCount)
{ {
ServiceArgs = NULL; ServiceArgs = NULL;
} }
if (! StartService(hSc, if (!StartService(hSc,
ArgCount, ArgCount,
ServiceArgs)) ServiceArgs))
{ {
_tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError()); _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
goto fail; bResult = FALSE;
goto done;
} }
pServiceInfo = QueryService(ServiceName); pServiceInfo = QueryService(ServiceName);
@ -64,18 +70,19 @@ BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
PrintService(ServiceName, PrintService(ServiceName,
pServiceInfo, pServiceInfo,
TRUE); TRUE);
HeapFree(GetProcessHeap(), 0, pServiceInfo);
} }
HeapFree(GetProcessHeap(), 0, pServiceInfo); done:
CloseServiceHandle(hSc); if (bResult == FALSE)
CloseServiceHandle(hSCManager); ReportLastError();
return TRUE; if (hSc)
CloseServiceHandle(hSc);
fail: if (hSCManager)
ReportLastError(); CloseServiceHandle(hSCManager);
if (hSc) CloseServiceHandle(hSc);
if (hSCManager) CloseServiceHandle(hSCManager);
return FALSE;
return bResult;
} }

View file

@ -35,7 +35,7 @@ VOID MainUsage(VOID)
_T("\t stop : Sends a STOP request to a service.\n") _T("\t stop : Sends a STOP request to a service.\n")
_T("\t config : Changes the configuration of a service (persistant).\n") _T("\t config : Changes the configuration of a service (persistant).\n")
_T("\t description : Changes the description of a service.\n") _T("\t description : Changes the description of a service.\n")
// "\t failure : Changes the actions taken by a service upon failure.\n" _T("\t failure : Changes the actions taken by a service upon failure.\n")
_T("\t qc : Queries the configuration information for a service.\n") _T("\t qc : Queries the configuration information for a service.\n")
_T("\t qdescription : Queries the description for a service.\n") _T("\t qdescription : Queries the description for a service.\n")
_T("\t qfailure : Queries the actions taken by a service upon failure.\n") _T("\t qfailure : Queries the actions taken by a service upon failure.\n")
@ -171,9 +171,9 @@ VOID CreateUsage(VOID)
VOID ControlUsage(VOID) VOID ControlUsage(VOID)
{ {
_tprintf(_T("DESCRIPTION:\n") _tprintf(_T("DESCRIPTION:\n")
_T(" Sends a CONTROL control request to a service.\n") _T(" Sends a CONTROL control request to a service.\n")
_T("USAGE:\n") _T("USAGE:\n")
_T(" sc <server> control [service name] <value>\n")); _T(" sc <server> control [service name] <value>\n"));
} }
VOID SdShowUsage(VOID) VOID SdShowUsage(VOID)