Added new Service Control utitlity.

svn path=/trunk/; revision=3890
This commit is contained in:
Robert Dickenson 2002-12-26 17:04:46 +00:00
parent ccf4643b4c
commit df6e777080
7 changed files with 702 additions and 0 deletions

View file

@ -0,0 +1,109 @@
/*
* ReactOS SC - service control console program
*
* command.c
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <windows.h>
#include <wchar.h>
#include <tchar.h>
#include "main.h"
int sc_Lock(SC_HANDLE hSCManager)
{
dprintf("sc_Lock(%x) - not implemented.\n", hSCManager);
return 0;
}
int sc_QueryLock(SC_HANDLE hSCManager)
{
QUERY_SERVICE_LOCK_STATUS LockStatus;
DWORD cbBufSize = sizeof(QUERY_SERVICE_LOCK_STATUS);
DWORD cbBytesNeeded;
dprintf("sc_QueryLock() - called.\n");
if (QueryServiceLockStatus(hSCManager, &LockStatus, cbBufSize, &cbBytesNeeded)) {
} else {
dprintf("Failed to Query Service Lock Status.\n");
ReportLastError();
}
if (!CloseServiceHandle(hSCManager)) {
dprintf("Failed to CLOSE handle to SCM.\n");
ReportLastError();
}
return 0;
}
int sc_control(SC_HANDLE hSCManager, char* service, DWORD dwControl)
{
int ret = 0;
SC_HANDLE schService;
SERVICE_STATUS serviceStatus;
dprintf("sc_control(%x, %s, %d) - called.\n", hSCManager, service, dwControl);
schService = OpenServiceA(hSCManager, service, SERVICE_ALL_ACCESS);
if (schService != NULL) {
ret = ControlService(schService, dwControl, &serviceStatus);
dprintf("ControlService(%x, %x, %x) returned %d\n", schService, dwControl, &serviceStatus, ret);
if (!ret) {
}
CloseServiceHandle(schService);
}
return ret;
}
int sc_command(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[])
{
// dprintf("sc_command(%x, %d, %s) - called.\n", hSCManager, sc_cmd, argv[]);
switch (sc_cmd) {
case SC_CMD_START:
//return sc_control(hSCManager, sc_cmd_arg, SERVICE_CONTROL_START);
dprintf(" - not implemented.\n");
break;
case SC_CMD_PAUSE:
return sc_control(hSCManager, argv[0], SERVICE_CONTROL_PAUSE);
case SC_CMD_INTERROGATE:
return sc_control(hSCManager, argv[0], SERVICE_CONTROL_INTERROGATE);
case SC_CMD_CONTINUE:
return sc_control(hSCManager, argv[0], SERVICE_CONTROL_CONTINUE);
case SC_CMD_STOP:
return sc_control(hSCManager, argv[0], SERVICE_CONTROL_STOP);
// case SC_CMD_CONFIG:
// case SC_CMD_DESCRIPTION:
// case SC_CMD_CONTROL:
case SC_CMD_LOCK:
return sc_Lock(hSCManager);
case SC_CMD_QUERYLOCK:
return sc_QueryLock(hSCManager);
default:
dprintf("sc_command(%x, %d, %s) - unknown command.\n", hSCManager, sc_cmd, argv[0]);
break;
}
return 0;
}

View file

@ -0,0 +1,75 @@
/*
* ReactOS SC - service control console program
*
* config.c
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <windows.h>
#include <wchar.h>
#include <tchar.h>
#include "main.h"
int sc_config(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[])
{
dprintf("sc_config(%x, %d, %s) - called.\n", hSCManager, sc_cmd, argv[0]);
switch (sc_cmd) {
case SC_CMD_CONFIG:
case SC_CMD_DESCRIPTION:
case SC_CMD_FAILURE:
case SC_CMD_SDSHOW:
case SC_CMD_SDSET:
dprintf(" - not implemented.\n");
break;
default:
dprintf("sc_config(%x, %d, %s) - unknown command.\n", hSCManager, sc_cmd, argv[0]);
break;
}
return 0;
}
/*
switch (sc_cmd) {
case SC_CMD_QUERY:
case SC_CMD_QUERYEX:
case SC_CMD_START:
case SC_CMD_PAUSE:
case SC_CMD_INTERROGATE:
case SC_CMD_CONTINUE:
case SC_CMD_STOP:
case SC_CMD_CONFIG:
case SC_CMD_DESCRIPTION:
case SC_CMD_FAILURE:
case SC_CMD_QC:
case SC_CMD_QDESCRIPTION:
case SC_CMD_QFAILURE:
case SC_CMD_DELETE:
case SC_CMD_CREATE:
case SC_CMD_CONTROL:
case SC_CMD_SDSHOW:
case SC_CMD_SDSET:
case SC_CMD_GETDISPLAYNAME:
case SC_CMD_GETKEYNAME:
case SC_CMD_ENUMDEPEND:
case SC_CMD_BOOT:
case SC_CMD_LOCK:
case SC_CMD_QUERYLOCK:
break;
}
*/

View file

@ -0,0 +1,247 @@
/*
* ReactOS SC - service control console program
*
* main.c
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <windows.h>
#include <tchar.h>
#include "main.h"
#define VERSION 1
#ifdef UNICODE
#define TARGET "UNICODE"
#else
#define TARGET "MBCS"
#endif
BOOL verbose_flagged = 0;
BOOL status_flagged = 0;
HANDLE OutputHandle;
HANDLE InputHandle;
void dprintf(char* fmt, ...)
{
va_list args;
char buffer[255];
va_start(args, fmt);
wvsprintfA(buffer, fmt, args);
WriteConsoleA(OutputHandle, buffer, lstrlenA(buffer), NULL, NULL);
va_end(args);
}
long getinput(char* buf, int buflen)
{
DWORD result;
ReadConsoleA(InputHandle, buf, buflen, &result, NULL);
return (long)result;
}
DWORD ReportLastError(void)
{
DWORD dwError = GetLastError();
if (dwError != ERROR_SUCCESS) {
PTSTR msg = NULL;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
0, dwError, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (PTSTR)&msg, 0, NULL)) {
if (msg != NULL) {
dprintf("ReportLastError() %d - %s\n", dwError, msg);
} else {
dprintf("ERROR: ReportLastError() %d - returned TRUE but with no msg string!\n", dwError);
}
} else {
dprintf("ReportLastError() %d - unknown error\n", dwError);
}
if (msg != NULL) {
LocalFree(msg);
}
}
return dwError;
}
int usage(char* argv0)
{
dprintf("DESCRIPTION:\n");
dprintf("\tSC is a command line program used for communicating with\n");
dprintf("\tthe Service Control Manager and its services.\n");
dprintf("USAGE:\n");
dprintf("\tsc <server> [command] [service name] <option1> <option2>...\n");
dprintf("\tThe optional parameter <server> has the form \"\\ServerName\"\n");
dprintf("\tFurther help on commands can be obtained by typing: \"sc [command]\"\n");
dprintf("\tService Commands:\n");
dprintf("\t query : Queries the status for a service, or\n");
dprintf("\t enumerates the status for types of services.\n");
dprintf("\t queryex : Queries the extended status for a service, or\n");
dprintf("\t enumerates the status for types of services.\n");
dprintf("\t start : Starts a service.\n");
dprintf("\t pause : Sends a PAUSE control request to a service.\n");
dprintf("\t interrogate : Sends a INTERROGATE control request to a service.\n");
dprintf("\t continue : Sends a CONTINUE control request to a service.\n");
dprintf("\t stop : Sends a STOP request to a service.\n");
dprintf("\t config : Changes the configuration of a service (persistant).\n");
dprintf("\t description : Changes the description of a service.\n");
dprintf("\t failure : Changes the actions taken by a service upon failure.\n");
dprintf("\t qc : Queries the configuration information for a service.\n");
dprintf("\t qdescription : Queries the description for a service.\n");
dprintf("\t qfailure : Queries the actions taken by a service upon failure.\n");
dprintf("\t delete : Deletes a service (from the registry).\n");
dprintf("\t create : Creates a service. (adds it to the registry).\n");
dprintf("\t control : Sends a control to a service.\n");
// dprintf("\t sdshow : Displays a service's security descriptor.\n");
// dprintf("\t sdset : Sets a service's security descriptor.\n");
dprintf("\t GetDisplayName : Gets the DisplayName for a service.\n");
dprintf("\t GetKeyName : Gets the ServiceKeyName for a service.\n");
dprintf("\t EnumDepend : Enumerates Service Dependencies.\n");
dprintf("\n");
dprintf("\tService Name Independant Commands:\n");
dprintf("\t boot : (ok | bad) Indicates whether the last boot should\n");
dprintf("\t be saved as the last-known-good boot configuration\n");
dprintf("\t Lock : Locks the SCM Database\n");
dprintf("\t QueryLock : Queries the LockStatus for the SCM Database\n");
return 0;
}
typedef int (*sc_cmd_proc)(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[]);
typedef struct {
SC_CMDS sc_cmd;
const char* cmd_str;
sc_cmd_proc funcptr;
DWORD dwDesiredAccess;
} sc_cmd_entry;
sc_cmd_entry sc_cmds_table[] = {
{ SC_CMD_QUERY, "query", sc_query, SC_MANAGER_ALL_ACCESS },
{ SC_CMD_QUERYEX, "queryex", sc_query, SC_MANAGER_ALL_ACCESS },
{ SC_CMD_START, "start", sc_command, 0L },
{ SC_CMD_PAUSE, "pause", sc_command, 0L },
{ SC_CMD_INTERROGATE, "interrogate", sc_command, 0L },
{ SC_CMD_CONTINUE, "continue", sc_command, 0L },
{ SC_CMD_STOP, "stop", sc_command, 0L },
{ SC_CMD_CONFIG, "config", sc_setup, 0L },
{ SC_CMD_DESCRIPTION, "description", sc_setup, 0L },
{ SC_CMD_FAILURE, "failure", sc_setup, 0L },
{ SC_CMD_QC, "qc", sc_query, 0L },
{ SC_CMD_QDESCRIPTION, "qdescription", sc_query, 0L },
{ SC_CMD_QFAILURE, "qfailure", sc_query, 0L },
{ SC_CMD_DELETE, "delete", sc_setup, 0L },
{ SC_CMD_CREATE, "create", sc_setup, 0L },
{ SC_CMD_CONTROL, "control", sc_setup, 0L },
{ SC_CMD_SDSHOW, "sdshow", sc_query, 0L },
{ SC_CMD_SDSET, "sdset", sc_setup, 0L },
{ SC_CMD_GETDISPLAYNAME, "GetDisplayName", sc_query, 0L },
{ SC_CMD_GETKEYNAME, "GetKeyName", sc_query, 0L },
{ SC_CMD_ENUMDEPEND, "EnumDepend", sc_query, 0L },
{ SC_CMD_BOOT, "boot", sc_config, 0L },
{ SC_CMD_LOCK, "Lock", sc_config, 0L },
{ SC_CMD_QUERYLOCK, "QueryLock", sc_config, 0L }
};
int sc_main(const sc_cmd_entry* cmd_entry,
const char* sc_machine_name,
char* sc_cmd_arg[])
{
int result = 1;
SC_HANDLE hSCManager;
DWORD dwDesiredAccess;
dprintf("sc_main(%s) - called.\n", cmd_entry->cmd_str);
if (sc_machine_name) {
dprintf("remote service control not yet implemented.\n");
return 2;
}
dwDesiredAccess = cmd_entry->dwDesiredAccess;
if (!dwDesiredAccess) dwDesiredAccess = SC_MANAGER_CONNECT + GENERIC_READ;
hSCManager = OpenSCManagerA(sc_machine_name, NULL, dwDesiredAccess);
if (hSCManager != NULL) {
result = cmd_entry->funcptr(hSCManager, cmd_entry->sc_cmd, sc_cmd_arg);
if (!CloseServiceHandle(hSCManager)) {
dprintf("Failed to CLOSE handle to SCM.\n");
ReportLastError();
}
} else {
dprintf("Failed to open Service Control Manager.\n");
ReportLastError();
}
return result;
}
int __cdecl main(int argc, char* argv[])
{
int i;
const char* sc_machine_name = NULL;
const char* sc_cmd_str = argv[1];
char** sc_cmd_arg = NULL;
AllocConsole();
InputHandle = GetStdHandle(STD_INPUT_HANDLE);
OutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
dprintf("%s application - build %03d (default: %s)\n", argv[0], VERSION, TARGET);
if (argc < 2) {
return usage(argv[0]);
}
if ((argv[1][0] == '\\') && (argv[1][1] == '\\')) {
if (argc < 3) {
return usage(argv[0]);
}
sc_machine_name = argv[1];
sc_cmd_str = argv[2];
sc_cmd_arg = &argv[3];
} else {
sc_machine_name = NULL;
sc_cmd_str = argv[1];
sc_cmd_arg = &argv[2];
}
for (i = 0; i < sizeof(sc_cmds_table)/sizeof(sc_cmd_entry); i++) {
sc_cmd_entry* cmd_entry = &(sc_cmds_table[i]);
if (lstrcmpiA(sc_cmd_str, cmd_entry->cmd_str) == 0) {
return sc_main(cmd_entry, sc_machine_name, sc_cmd_arg);
}
}
return usage(argv[0]);
}
#ifdef _NOCRT
//char* args[] = { "sc.exe", "pause", "smplserv" };
char* args[] = { "sc.exe", "EnumDepend", "" };
int __cdecl mainCRTStartup(void)
{
main(3, args);
return 0;
}
#endif /*_NOCRT*/

View file

@ -0,0 +1,72 @@
/*
* ReactOS test program -
*
* main.h
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#ifdef __cplusplus
extern "C" {
#endif
DWORD ReportLastError(void);
long getinput(char* Buffer, int buflen);
void dprintf(char* fmt, ...);
typedef enum tag_SC_CMDS {
SC_CMD_QUERY,
SC_CMD_QUERYEX,
SC_CMD_START,
SC_CMD_PAUSE,
SC_CMD_INTERROGATE,
SC_CMD_CONTINUE,
SC_CMD_STOP,
SC_CMD_CONFIG,
SC_CMD_DESCRIPTION,
SC_CMD_FAILURE,
SC_CMD_QC,
SC_CMD_QDESCRIPTION,
SC_CMD_QFAILURE,
SC_CMD_DELETE,
SC_CMD_CREATE,
SC_CMD_CONTROL,
SC_CMD_SDSHOW,
SC_CMD_SDSET,
SC_CMD_GETDISPLAYNAME,
SC_CMD_GETKEYNAME,
SC_CMD_ENUMDEPEND,
SC_CMD_BOOT,
SC_CMD_LOCK,
SC_CMD_QUERYLOCK
} SC_CMDS;
int sc_query(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[]);
int sc_setup(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[]);
int sc_config(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[]);
int sc_command(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[]);
#ifdef __cplusplus
};
#endif
#endif // __MAIN_H__

View file

@ -0,0 +1,24 @@
#
# $Id: makefile,v 1.0
PATH_TO_TOP = ../../..
TARGET_NORC = yes
TARGET_TYPE = program
TARGET_APPTYPE = console
TARGET_NAME = sc
TARGET_CFLAGS = -DDBG
TARGET_SDKLIBS = kernel32.a ntdll.a advapi32.a
TARGET_OBJECTS = command.o config.o main.o query.o setup.o
include $(PATH_TO_TOP)/rules.mak
include $(TOOLS_PATH)/helper.mk
# EOF

View file

@ -0,0 +1,113 @@
/*
* ReactOS SC - service control console program
*
* query.c
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <windows.h>
#include <wchar.h>
#include <tchar.h>
#include "main.h"
int DisplayServiceInfo(BOOL display_ex)
{
const char* type_str = "WIN32_SHARE_PROCESS";
const char* state_str = "RUNNING";
const char* flags_str = "RUNS_IN_SYSTEM_PROCESS";
const char* features_str = "NOT_STOPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN";
const char* ServiceName = "none";
const char* DisplayName = "none";
int Type;
int State;
int Win32ExitCode;
int ServiceExitCode;
int CheckPoint;
int WaitHint;
int Pid;
int Flags;
Type = 20;
State = 4;
Win32ExitCode = 0;
ServiceExitCode = 0;
CheckPoint = 0;
WaitHint = 0;
Pid = 0;
Flags = 0;
dprintf("\nSERVICE_NAME: %s\n", ServiceName);
dprintf("DISPLAY_NAME: %s\n", DisplayName);
dprintf("\tTYPE : %d %s\n", Type, type_str);
dprintf("\tSTATE : %d %s\n", State, state_str);
dprintf("\t : (%s)\n", features_str);
dprintf("\tWIN32_EXIT_CODE : %d (0x%01x)\n", Win32ExitCode, Win32ExitCode);
dprintf("\tSERVICE_EXIT_CODE : %d (0x%01x)\n", ServiceExitCode, ServiceExitCode);
dprintf("\tCHECKPOINT : 0x%01x\n", CheckPoint);
dprintf("\tWAIT_HINT : 0x%01x\n", WaitHint);
if (display_ex) {
dprintf("\tPID : %d\n", Pid);
dprintf("\tFLAGS : %s\n", flags_str);
}
return 0;
}
int EnumServicesInfo(void)
{
int entriesRead = 0;
int resumeIndex = 0;
dprintf("Enum: entriesRead = %d\n", entriesRead);
dprintf("Enum: resumeIndex = %d\n", resumeIndex);
DisplayServiceInfo(1);
return 0;
}
int sc_query(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[])
{
dprintf("sc_query(%x, %d, %s) - called.\n", hSCManager, sc_cmd, argv[0]);
switch (sc_cmd) {
case SC_CMD_QUERY:
case SC_CMD_QUERYEX:
case SC_CMD_QC:
case SC_CMD_QDESCRIPTION:
case SC_CMD_QFAILURE:
case SC_CMD_SDSHOW:
case SC_CMD_GETDISPLAYNAME:
case SC_CMD_GETKEYNAME:
dprintf(" - not implemented.\n");
break;
case SC_CMD_ENUMDEPEND:
return EnumServicesInfo();
// case SC_CMD_QUERYLOCK:
// return sc_QueryLock(hSCManager);
default:
dprintf("sc_query(%x, %d, %s) - unknown command.\n", hSCManager, sc_cmd, argv[0]);
break;
}
return 0;
}

View file

@ -0,0 +1,62 @@
/*
* ReactOS SC - service control console program
*
* setup.c
*
* Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <windows.h>
#include <wchar.h>
#include <tchar.h>
#include "main.h"
int sc_delete(char* argv[])
{
dprintf("sc_delete(%s) - not implemented.\n", argv[0]);
return 0;
}
int sc_boot(char* arg)
{
dprintf("sc_boot(%s) - not implemented.\n", arg);
return 0;
}
int sc_setup(SC_HANDLE hSCManager, SC_CMDS sc_cmd, char* argv[])
{
dprintf("sc_setup(%x, %d, %s) - called.\n", hSCManager, sc_cmd, argv[0]);
switch (sc_cmd) {
// case SC_CMD_DESCRIPTION:
// case SC_CMD_FAILURE:
case SC_CMD_DELETE:
return sc_delete(argv);
case SC_CMD_CREATE:
dprintf(" - not implemented.\n");
break;
// case SC_CMD_SDSHOW:
// case SC_CMD_SDSET:
case SC_CMD_BOOT:
return sc_boot(argv[0]);
default:
dprintf("sc_setup(%x, %d, %s) - unknown command.\n", hSCManager, sc_cmd, argv[0]);
break;
}
return 0;
}