Abstracted open_registry().

Added service_registry_path() to construct a registry path for a service
which may contain the Parameters subkey and optional sub-subkey.  If no
Parameters subkey is needed the constructed path references the native
service parameters.

The open_registry() function is now a wrapper around the new
open_registry_key() function which opens an arbitrary key.

The new open_service_resgistry() function also wraps open_registry_key()
to access native parameters.
master
Iain Patterson 10 years ago
parent fb96938cf9
commit 66861c12e7

@ -456,18 +456,20 @@ void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned
if (! ok) *buffer = default_value;
}
HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool must_exist) {
/* Get registry */
TCHAR registry[KEY_LENGTH];
HKEY key;
static int service_registry_path(const TCHAR *service_name, bool parameters, const TCHAR *sub, TCHAR *buffer, unsigned long buflen) {
int ret;
if (sub) ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY _T("\\%s"), service_name, sub);
else ret = _sntprintf_s(registry, _countof(registry), _TRUNCATE, NSSM_REGISTRY, service_name);
if (ret < 0) {
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("NSSM_REGISTRY"), _T("open_registry()"), 0);
return 0;
if (parameters) {
if (sub) ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS _T("\\%s"), service_name, sub);
else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY _T("\\") NSSM_REG_PARAMETERS, service_name);
}
else ret = _sntprintf_s(buffer, buflen, _TRUNCATE, NSSM_REGISTRY, service_name);
return ret;
}
static HKEY open_registry_key(const TCHAR *registry, REGSAM sam, bool must_exist) {
HKEY key;
if (sam & KEY_SET_VALUE) {
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, sam, 0, &key, 0) != ERROR_SUCCESS) {
@ -487,6 +489,28 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool
return key;
}
HKEY open_service_registry(const TCHAR *service_name, REGSAM sam, bool must_exist) {
/* Get registry */
TCHAR registry[KEY_LENGTH];
if (service_registry_path(service_name, false, 0, registry, _countof(registry)) < 0) {
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REGISTRY, _T("open_service_registry()"), 0);
return 0;
}
return open_registry_key(registry, sam, must_exist);
}
HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool must_exist) {
/* Get registry */
TCHAR registry[KEY_LENGTH];
if (service_registry_path(service_name, true, sub, registry, _countof(registry)) < 0) {
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REGISTRY, _T("open_registry()"), 0);
return 0;
}
return open_registry_key(registry, sam, must_exist);
}
HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
return open_registry(service_name, sub, sam, true);
}

@ -1,7 +1,8 @@
#ifndef REGISTRY_H
#define REGISTRY_H
#define NSSM_REGISTRY _T("SYSTEM\\CurrentControlSet\\Services\\%s\\Parameters")
#define NSSM_REGISTRY _T("SYSTEM\\CurrentControlSet\\Services\\%s")
#define NSSM_REG_PARAMETERS _T("Parameters")
#define NSSM_REGISTRY_GROUPS _T("SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder")
#define NSSM_REG_GROUPS _T("List")
#define NSSM_REG_EXE _T("Application")
@ -36,6 +37,7 @@
#define NSSM_REG_HOOK _T("AppEvents")
#define NSSM_STDIO_LENGTH 29
HKEY open_service_registry(const TCHAR *, REGSAM sam, bool);
HKEY open_registry(const TCHAR *, const TCHAR *, REGSAM sam, bool);
HKEY open_registry(const TCHAR *, const TCHAR *, REGSAM sam);
HKEY open_registry(const TCHAR *, REGSAM sam);

@ -726,6 +726,25 @@ int native_get_displayname(const TCHAR *service_name, void *param, const TCHAR *
return ret;
}
int native_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = open_service_registry(service_name, KEY_SET_VALUE, false);
if (! key) return -1;
int ret = setting_set_environment(service_name, (void *) key, NSSM_NATIVE_ENVIRONMENT, default_value, value, additional);
RegCloseKey(key);
return ret;
}
int native_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
HKEY key = open_service_registry(service_name, KEY_READ, false);
if (! key) return -1;
ZeroMemory(value, sizeof(value_t));
int ret = setting_get_environment(service_name, (void *) key, NSSM_NATIVE_ENVIRONMENT, default_value, value, additional);
RegCloseKey(key);
return ret;
}
int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
SC_HANDLE service_handle = (SC_HANDLE) param;
if (! service_handle) return -1;
@ -1101,6 +1120,7 @@ settings_t settings[] = {
{ NSSM_NATIVE_DEPENDONSERVICE, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_dependonservice, native_get_dependonservice },
{ NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description },
{ NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname },
{ NSSM_NATIVE_ENVIRONMENT, REG_MULTI_SZ, NULL, true, ADDITIONAL_CRLF, native_set_environment, native_get_environment },
{ NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath },
{ NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, 0, native_set_objectname, native_get_objectname },
{ NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name },

@ -5,6 +5,7 @@
#define NSSM_NATIVE_DEPENDONSERVICE _T("DependOnService")
#define NSSM_NATIVE_DESCRIPTION _T("Description")
#define NSSM_NATIVE_DISPLAYNAME _T("DisplayName")
#define NSSM_NATIVE_ENVIRONMENT _T("Environment")
#define NSSM_NATIVE_IMAGEPATH _T("ImagePath")
#define NSSM_NATIVE_NAME _T("Name")
#define NSSM_NATIVE_OBJECTNAME _T("ObjectName")

Loading…
Cancel
Save