2006-02-16 23:18:11 +00:00
|
|
|
/*
|
2006-08-31 17:59:02 +00:00
|
|
|
* PROJECT: ReactOS Services
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: base/system/sc/sc.c
|
|
|
|
* PURPOSE: parse command line
|
|
|
|
* COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
|
2006-02-16 23:18:11 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sc.h"
|
|
|
|
|
|
|
|
SC_HANDLE hSCManager;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
VOID
|
|
|
|
ReportLastError(VOID)
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
DWORD RetVal;
|
|
|
|
|
|
|
|
DWORD ErrorCode = GetLastError();
|
2006-08-31 17:59:02 +00:00
|
|
|
if (ErrorCode != ERROR_SUCCESS)
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
|
|
|
RetVal = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
ErrorCode,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
|
|
|
|
(LPTSTR) &lpMsgBuf,
|
|
|
|
0,
|
|
|
|
NULL );
|
|
|
|
|
|
|
|
if (RetVal != 0)
|
|
|
|
{
|
|
|
|
_tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
|
|
|
|
LocalFree(lpMsgBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
static INT
|
|
|
|
ScControl(LPCTSTR Server, // remote machine name
|
|
|
|
LPCTSTR Command, // sc command
|
|
|
|
LPCTSTR *ServiceArgs, // any options
|
|
|
|
DWORD ArgCount) // argument counter
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2006-09-01 16:55:36 +00:00
|
|
|
LPCTSTR ServiceName = NULL;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
if (Server)
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
|
|
|
_tprintf(_T("Remote service control is not yet implemented\n"));
|
|
|
|
return 2;
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
|
|
|
|
if (!lstrcmpi(Command, _T("query")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2006-09-01 16:55:36 +00:00
|
|
|
Query(ServiceArgs,
|
|
|
|
ArgCount,
|
2006-08-31 17:59:02 +00:00
|
|
|
FALSE);
|
2006-02-16 23:18:11 +00:00
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("queryex")))
|
|
|
|
{
|
2006-09-01 16:55:36 +00:00
|
|
|
Query(ServiceArgs,
|
|
|
|
ArgCount,
|
2006-08-31 17:59:02 +00:00
|
|
|
TRUE);
|
|
|
|
}
|
|
|
|
else if (!lstrcmpi(Command, _T("start")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Start(ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
StartUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("pause")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Control(SERVICE_CONTROL_PAUSE,
|
|
|
|
ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
PauseUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("interrogate")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Control(SERVICE_CONTROL_INTERROGATE,
|
|
|
|
ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
InterrogateUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("stop")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Control(SERVICE_CONTROL_STOP,
|
|
|
|
ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
StopUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("continue")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Control(SERVICE_CONTROL_CONTINUE,
|
|
|
|
ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
ContinueUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("delete")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 0)
|
|
|
|
{
|
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
2006-09-01 16:55:36 +00:00
|
|
|
|
2006-02-16 23:18:11 +00:00
|
|
|
Delete(ServiceName);
|
2010-09-18 08:41:06 +00:00
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
else
|
|
|
|
DeleteUsage();
|
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("create")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2010-09-18 08:41:06 +00:00
|
|
|
Create(ServiceArgs, ArgCount);
|
2006-02-16 23:18:11 +00:00
|
|
|
}
|
2006-08-31 17:59:02 +00:00
|
|
|
else if (!lstrcmpi(Command, _T("control")))
|
2006-02-16 23:18:11 +00:00
|
|
|
{
|
2006-08-31 17:59:02 +00:00
|
|
|
INT CtlValue;
|
|
|
|
|
2010-09-18 08:41:06 +00:00
|
|
|
if (ArgCount > 1)
|
|
|
|
{
|
|
|
|
ServiceName = *ServiceArgs++;
|
|
|
|
ArgCount--;
|
2006-09-01 16:55:36 +00:00
|
|
|
|
2010-09-18 08:41:06 +00:00
|
|
|
CtlValue = _ttoi(ServiceArgs[0]);
|
|
|
|
ServiceArgs++;
|
|
|
|
ArgCount--;
|
2006-08-31 17:59:02 +00:00
|
|
|
|
2010-09-18 08:41:06 +00:00
|
|
|
if ((CtlValue >= 128) && (CtlValue <= 255))
|
2006-08-31 17:59:02 +00:00
|
|
|
Control(CtlValue,
|
|
|
|
ServiceName,
|
|
|
|
ServiceArgs,
|
|
|
|
ArgCount);
|
2010-09-18 08:41:06 +00:00
|
|
|
else
|
|
|
|
ControlUsage();
|
2006-08-31 17:59:02 +00:00
|
|
|
}
|
2010-09-18 08:41:06 +00:00
|
|
|
else
|
|
|
|
ControlUsage();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MainUsage();
|
2006-02-16 23:18:11 +00:00
|
|
|
}
|
2010-09-18 08:41:06 +00:00
|
|
|
|
2006-02-16 23:18:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _tmain(int argc, LPCTSTR argv[])
|
|
|
|
{
|
2006-09-01 16:55:36 +00:00
|
|
|
LPCTSTR Server = NULL; // remote machine
|
|
|
|
LPCTSTR Command = NULL; // sc command
|
|
|
|
LPCTSTR *Args = NULL; // Any remaining args
|
2006-02-16 23:18:11 +00:00
|
|
|
|
|
|
|
if (argc < 2)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
|
|
|
MainUsage();
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
|
|
|
|
/* get server name */
|
|
|
|
if ((argv[1][0] == '\\') && (argv[1][1] == '\\'))
|
|
|
|
{
|
|
|
|
if (argc < 3)
|
2006-08-31 17:59:02 +00:00
|
|
|
{
|
|
|
|
MainUsage();
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-16 23:18:11 +00:00
|
|
|
|
2006-08-31 17:59:02 +00:00
|
|
|
Server = argv[1];
|
2006-02-16 23:18:11 +00:00
|
|
|
Command = argv[2];
|
|
|
|
if (argc > 3)
|
2006-09-01 16:55:36 +00:00
|
|
|
Args = &argv[3];
|
2006-08-31 17:59:02 +00:00
|
|
|
|
|
|
|
return ScControl(Server,
|
|
|
|
Command,
|
2006-09-01 16:55:36 +00:00
|
|
|
Args,
|
|
|
|
argc-3);
|
2006-02-16 23:18:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Command = argv[1];
|
|
|
|
if (argc > 2)
|
2006-09-01 16:55:36 +00:00
|
|
|
Args = &argv[2];
|
2006-08-31 17:59:02 +00:00
|
|
|
|
|
|
|
return ScControl(Server,
|
|
|
|
Command,
|
2006-09-01 16:55:36 +00:00
|
|
|
Args,
|
|
|
|
argc-2);
|
2006-02-16 23:18:11 +00:00
|
|
|
}
|
|
|
|
}
|