From 4b7c8eec508cc90d80355fad5df80163a181ddcb Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 1 Jan 2014 12:41:02 +0000 Subject: [PATCH] Allow service editing on the command line. Individual service parameters can now be queried or edited on the command line. The syntax is designed to be intuitive and is described in some detail in the README. Some examples follow: nssm get Selenium Application nssm get "My Batch Job" AppExit 0 nssm get Jenkins AppEnvironmentExtra CLASSPATH nssm get Netlogon ImagePath nssm set UT2004 Application C:\Games\UT2004\System\UCC.exe nssm set UT2004 AppParameters server nssm set UT2004 AppExit Default Exit nssm set Jenkins ObjectName DOMAIN\ci correct horse battery staple nssm set "My Batch Job" AppExit 0 Exit nssm set WowzaMediaServer362 Start SERVICE_DELAYED_START nssm set UT2004 Type SERVICE_INTERACTIVE_PROCESS nssm reset UT2004 AppEnvironment nssm reset UT2004 ObjectName --- ChangeLog.txt | 3 +- README.txt | 135 +++++++++- messages.mc | Bin 107400 -> 118256 bytes nssm.cpp | 9 +- nssm.dsp | 8 + nssm.h | 1 + nssm.vcproj | 8 + service.cpp | 219 ++++++++++++++-- service.h | 8 + settings.cpp | 672 ++++++++++++++++++++++++++++++++++++++++++++++++++ settings.h | 40 +++ 11 files changed, 1072 insertions(+), 31 deletions(-) create mode 100644 settings.cpp create mode 100644 settings.h diff --git a/ChangeLog.txt b/ChangeLog.txt index 004f597..bdc1aed 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,7 @@ Changes since 2.21 ------------------ - * Existing services can now be edited using the GUI. + * Existing services can now be edited using the GUI + or on the command line. * NSSM can now optionally rotate existing files when redirecting I/O. diff --git a/README.txt b/README.txt index 0a0c47c..a7655d9 100644 --- a/README.txt +++ b/README.txt @@ -58,7 +58,7 @@ Since version 2.22, NSSM can rotate existing output files when redirecting I/O. Since version 2.22, NSSM can set service display name, description, startup type and log on details. -Since version 2.22, NSSM can edit existing services with the GUI. +Since version 2.22, NSSM can edit existing services. Usage @@ -298,6 +298,131 @@ the App* registry settings described above, the GUI will allow editing only system settings such as the service display name and description. +Managing services using the command line +---------------------------------------- +NSSM can retrieve or set individual service parameters from the command line. +In general the syntax is as follows, though see below for exceptions. + + nssm get + + nssm set + +Parameters can also be reset to their default values. + + nssm reset + +The parameter names recognised by NSSM are the same as the registry entry +names described above, eg AppDirectory. + +NSSM offers limited editing capabilities for Services other than those which +run NSSM itself. The parameters recognised are as follows: + + Description: Service description. + DisplayName: Service display name. + ImagePath: Path to the service executable. + ObjectName: User account which runs the service. + Start: Service startup type. + Type: Service type. + +These correspond to the registry values under the service's key +HKLM\SYSTEM\CurrentControlSet\Services\. + + +Note that NSSM will concatenate all arguments passed on the command line +with spaces to form the value to set. Thus the following two invocations +would have the same effect. + + nssm set Description "NSSM managed service" + + nssm set Description NSSM managed service + + +Non-standard parameters +----------------------- +The AppEnvironment and AppEnvironmentExtra parameters recognise an +additional argument when querying the environment. The following syntax +will print all extra environment variables configured for a service + + nssm get AppEnvironmentExtra + +whereas the syntax below will print only the value of the CLASSPATH +variable if it is configured in the environment block, or the empty string +if it is not configured. + + nssm get AppEnvironmentExtra CLASSPATH + +When setting an environment block, each variable should be specified as a +KEY=VALUE pair in separate command line arguments. For example: + + nssm set AppEnvironment CLASSPATH=C:\Classes TEMP=C:\Temp + + +The AppExit parameter requires an additional argument specifying the exit +code to get or set. The default action can be specified with the string +Default. + +For example, to get the default exit action for a service you should run + + nssm get AppExit Default + +To get the exit action when the application exits with exit code 2, run + + nssm get AppExit 2 + +Note that if no explicit action is configured for a specified exit code, +NSSM will print the default exit action. + +To set configure the service to stop when the application exits with an +exit code of 2, run + + nssm set AppExit 2 Exit + + +The ObjectName parameter requires an additional argument only when setting +a username. The additional argument is the password of the user. + +To retrieve the username, run + + nssm get ObjectName + +To set the username and password, run + + nssm set ObjectName + +Note that the rules of argument concatenation still apply. The following +invocation is valid and will have the expected effect. + + nssm set ObjectName correct horse battery staple + + +The Start parameter is used to query or set the startup type of the service. +Valid service startup types are as follows: + + SERVICE_AUTO_START: Automatic startup at boot. + SERVICE_DELAYED_START: Delayed startup at boot. + SERVICE_DEMAND_START: Manual service startup. + SERVICE_DISABLED: The service is disabled. + +Note that SERVICE_DELAYED_START is not supported on versions of Windows prior +to Vista. NSSM will set the service to automatic startup if delayed start is +unavailable. + + +The Type parameter is used to query or set the service type. NSSM recognises +all currently documented service types but will only allow setting one of two +types: + + SERVICE_WIN32_OWN_PROCESS: A standalone service. This is the default. + SERVICE_INTERACTIVE_PROCESS: A service which can interact with the desktop. + +Note that a service may only be configured as interactive if it runs under +the LocalSystem account. The safe way to configure an interactive service +is in two stages as follows. + + nssm reset ObjectName + nssm set Type SERVICE_INTERACTIVE_PROCESS + + Removing services using the GUI ------------------------------- NSSM can also remove services. Run @@ -335,6 +460,14 @@ To install an Unreal Tournament server: nssm install UT2004 c:\games\ut2004\system\ucc.exe server +To run the server as the "games" user: + + nssm set UT2004 ObjectName games password + +To configure the server to log to a file: + + nssm set UT2004 AppStdout c:\games\ut2004\service.log + To remove the server: nssm remove UT2004 confirm diff --git a/messages.mc b/messages.mc index 40a7f1c9157cf8e7d2d9c6a7a97343db318530c7..aaaeb8c49c9c9ce45c6dc30b31de9ce1c41b3bd0 100644 GIT binary patch delta 4128 zcmd5<-A|KO7(a)$RJs@X9WBN3wgNJ#LJ<@zq6I4A6cIn>FtoH%1cHE2+)NfTV_c@$ z#n`juHPOwF>|$4of^nEEWO%V9yK#Eq#j-eAj9H9v{sGIf=Q;hb0^)2*wu`qt?|DAX z^Z9$u`_Yft&(3SFK33U=S%gK|FzdztWBABy5Z`1Lfg=M;p?-3iz6)u@bqM+T*>O0p za;gp^eOgtxL1wjVh=q_IVn>9g2(If;{vc9D*eDCnOYeX$RE4UMn3m?Wwct6mXmtqf z8pYG=AXi$E(7HZNt)S0ZIiXfRR>3h}34E9>!*?nPy0s3Cj_Ft{TsD@`t_DNEq7c%q z1XrWU_}t||G-ZUX24|yLRmX&*8g%qkyjJ8*aVLUWFp%WXzmbOM64S&_+6!tZ&{b&s ztdTXtpKojurazMM?ZR}-EGfm@GRkRGOc#~ z2E)Y1`M+ktrQ#*va+boBLjsd#mD1YAB^YwJQVB}<3a^>H33v4Y_@-Px}p)nPuIbHS2vL-wif-RgNG%nV5mGw#w!(ldYm&2F*3b<0z6 z;)8}f81jWF8tdTdb~PC44-kW~jrmy<*0P`Nz-KS>L*WAtJPWlb@_w#{C(U`vSMPC| zk0@hWZl_R}Ik89EuviYGSOj5#z%9b|Cn2Jgy~(^-TE%PxjG8i$)J4&zgs$)ni=qkc z=z}ojG$=R9xe0FNx5J4}12j&WY%+6^;rP1-U6A8$^|CJTP1>}C!V%^L9^CjE++*9y zA<&-p8WMN(A*~Oy(aiS4SpQP8(AX-lBxMG`jLU2uWCJuY(9#%2qotH7n{w+o&sR|j zW=dA4Bg>27H@hbtNpUI#NTd?P1U=AITCa>hz^>U$6f=02Gh$_#1A*lIUIJE2I0X@HIKe57|9Q5Gt`_sALuaEgEsIf4rP_tT?#8mE;`BIp zc@6<(>}vAb@-s!;^GbR9OV3A>uid03G!W9|`q=@jGipE|Yk<(HLj-v|0!PnUmnW-i zHt3;8#IY1Zn*x1lNW~#A4apc%JG4Qx{T;j;B5Vk-{$;meeH4Lj2m`#3{Rtk*PKpnZ ziF^XkGtohQ_4y6TmlwE6nYzfI(ri_(Ug9UTvxg_tg|n3pF7xekSZE@inX?<8ZVo;5 z@m|b#8~*OY*iGjj6tSzC%q(`9$R}c#i4Kb0XKx$eNqZl`!`dXgrBs3}M)gtb#Mq}5 zoQbSyebr;9|#GW#}BYc;X0|mWl=fnGDTX4!XcLYS(HU2beHFrVU#a zS9lA+w;=BB!nYt!&t14rQ*;lu1f|%|c7pF#2mJh2o<m^DYRr;aN}OSw5GB)(pI t*)eb$%c1Xdo|f3LLH|=*e2+Ys8^iw~Djw@0=yyw}?$v*Lubz*0{sUsh-30&u delta 501 zcmew`o4w;W+lE)nlVzBsraSCqWZC?V^%?VKFP;jP%|^o4cqT8}YcTmuuhHZ*1+mGO zRvJvsd1yBIo`S&SNh^XT2gnFaepkq`IYjXa6SFEq@nn4!)5$C=^rkDcGxAI}P*s8p znrt+fyg^+L&WGvFQ&yQQpf&?6zFy55Q!_{qSu==@zl zcJ_Htd6CH=Hj+7;vm8Aj3hy{=0V~?$yale{oXY{IBiJVIbJYRMi@6zr<$+$Dyu$4b pNPhZ 1) { - /* Valid commands are install, edit or remove */ + /* Valid commands are install, edit, get, set, reset, unset or remove */ if (str_equiv(argv[1], _T("install"))) { if (! is_admin) { print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL); @@ -87,12 +87,15 @@ int _tmain(int argc, TCHAR **argv) { } exit(pre_install_service(argc - 2, argv + 2)); } - if (str_equiv(argv[1], _T("edit"))) { + if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) { if (! is_admin) { print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT); exit(100); } - exit(pre_edit_service(argc - 2, argv + 2)); + int ret = pre_edit_service(argc - 1, argv + 1); + /* There might be a password here. */ + for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR)); + exit(ret); } if (str_equiv(argv[1], _T("remove"))) { if (! is_admin) { diff --git a/nssm.dsp b/nssm.dsp index 2ac2b34..05efb15 100644 --- a/nssm.dsp +++ b/nssm.dsp @@ -118,6 +118,10 @@ SOURCE=.\registry.cpp SOURCE=.\service.cpp # End Source File +# Begin Source File + +SOURCE=.\settings.cpp +# End Source File # End Group # Begin Group "Header Files" @@ -154,6 +158,10 @@ SOURCE=.\registry.h SOURCE=.\service.h # End Source File +# Begin Source File + +SOURCE=.\settings.h +# End Source File # End Group # Begin Group "Resource Files" diff --git a/nssm.h b/nssm.h index cf6133e..b83844a 100644 --- a/nssm.h +++ b/nssm.h @@ -13,6 +13,7 @@ #include "messages.h" #include "process.h" #include "registry.h" +#include "settings.h" #include "io.h" #include "gui.h" diff --git a/nssm.vcproj b/nssm.vcproj index dfac3e5..324e28f 100755 --- a/nssm.vcproj +++ b/nssm.vcproj @@ -626,6 +626,10 @@ /> + + + + 2) { + bool additional_mandatory = false; + + parameter = argv[2]; + for (i = 0; settings[i].name; i++) { + setting = &settings[i]; + if (! str_equiv(setting->name, parameter)) continue; + if (((setting->additional & ADDITIONAL_GETTING) && mode == MODE_GETTING) || ((setting->additional & ADDITIONAL_SETTING) && mode == MODE_SETTING) || ((setting->additional & ADDITIONAL_RESETTING) && mode == MODE_RESETTING)) { + additional_mandatory = true; + mandatory++; + } + break; + } + if (! settings[i].name) { + print_message(stderr, NSSM_MESSAGE_INVALID_PARAMETER, parameter); + for (i = 0; settings[i].name; i++) _ftprintf(stderr, _T("%s\n"), settings[i].name); + return 1; + } + if (argc < mandatory) return usage(1); + + additional = 0; + if (additional_mandatory) { + additional = argv[3]; + remainder = 4; + } + else additional = argv[remainder]; + } nssm_service_t *service = alloc_nssm_service(); - _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]); + _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), service_name); /* Open service manager */ SC_HANDLE services = open_service_manager(); @@ -440,7 +499,6 @@ int pre_edit_service(int argc, TCHAR **argv) { } /* Get system details. */ - unsigned long bufsize; QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle); if (! qsc) { CloseHandle(service->handle); @@ -450,31 +508,37 @@ int pre_edit_service(int argc, TCHAR **argv) { service->type = qsc->dwServiceType; if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) { - HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); - CloseServiceHandle(services); - print_message(stderr, NSSM_MESSAGE_CANNOT_EDIT, service->name, _T("SERVICE_WIN32_OWN_PROCESS"), 0); - return 3; + if (mode != MODE_GETTING) { + HeapFree(GetProcessHeap(), 0, qsc); + CloseHandle(service->handle); + CloseServiceHandle(services); + print_message(stderr, NSSM_MESSAGE_CANNOT_EDIT, service->name, NSSM_WIN32_OWN_PROCESS, 0); + return 3; + } } if (get_service_startup(service->name, service->handle, qsc, &service->startup)) { - HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); - CloseServiceHandle(services); - return 4; + if (mode != MODE_GETTING) { + HeapFree(GetProcessHeap(), 0, qsc); + CloseHandle(service->handle); + CloseServiceHandle(services); + return 4; + } } if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) { - HeapFree(GetProcessHeap(), 0, qsc); - CloseHandle(service->handle); - CloseServiceHandle(services); - return 5; + if (mode != MODE_GETTING) { + HeapFree(GetProcessHeap(), 0, qsc); + CloseHandle(service->handle); + CloseServiceHandle(services); + return 5; + } } _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), qsc->lpDisplayName); /* Get the canonical service name. We open it case insensitively. */ - bufsize = _countof(service->name); + unsigned long bufsize = _countof(service->name); GetServiceKeyName(services, service->displayname, service->name, &bufsize); /* Remember the executable in case it isn't NSSM. */ @@ -483,9 +547,11 @@ int pre_edit_service(int argc, TCHAR **argv) { /* Get extended system details. */ if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) { - CloseHandle(service->handle); - CloseServiceHandle(services); - return 6; + if (mode != MODE_GETTING) { + CloseHandle(service->handle); + CloseServiceHandle(services); + return 6; + } } /* Get NSSM details. */ @@ -494,11 +560,112 @@ int pre_edit_service(int argc, TCHAR **argv) { CloseServiceHandle(services); if (! service->exe[0]) { - print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image); service->native = true; + if (mode != MODE_GETTING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image); + } + + /* Editing with the GUI. */ + if (mode == MODE_EDITING) { + nssm_gui(IDD_EDIT, service); + return 0; + } + + /* Trying to manage App* parameters for a non-NSSM service. */ + if (! setting->native && service->native) { + CloseHandle(service->handle); + print_message(stderr, NSSM_MESSAGE_NATIVE_PARAMETER, setting->name, NSSM); + return 1; + } + + HKEY key; + value_t value; + int ret; + + if (mode == MODE_GETTING) { + if (! service->native) { + key = open_registry(service->name, KEY_READ); + if (! key) return 4; + } + + if (setting->native) ret = get_setting(service->name, service->handle, setting, &value, additional); + else ret = get_setting(service->name, key, setting, &value, additional); + if (ret < 0) { + CloseHandle(service->handle); + return 5; + } + + switch (setting->type) { + case REG_EXPAND_SZ: + case REG_MULTI_SZ: + case REG_SZ: + _tprintf(_T("%s\n"), value.string ? value.string : _T("")); + HeapFree(GetProcessHeap(), 0, value.string); + break; + + case REG_DWORD: + _tprintf(_T("%u\n"), value.numeric); + break; + } + + if (! service->native) RegCloseKey(key); + CloseHandle(service->handle); + return 0; + } + + /* Build the value. */ + if (mode == MODE_RESETTING) { + /* Unset the parameter. */ + value.string = 0; + } + else { + /* Set the parameter. */ + size_t len = 0; + size_t delimiterlen = (setting->additional & ADDITIONAL_CRLF) ? 2 : 1; + for (i = remainder; i < argc; i++) len += _tcslen(argv[i]) + delimiterlen; + len++; + + value.string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR)); + if (! value.string) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("value"), _T("edit_service()")); + CloseHandle(service->handle); + return 2; + } + + size_t s = 0; + for (i = remainder; i < argc; i++) { + size_t len = _tcslen(argv[i]); + memmove(value.string + s, argv[i], len * sizeof(TCHAR)); + s += len; + if (i < argc - 1) { + if (setting->additional & ADDITIONAL_CRLF) { + value.string[s++] = _T('\r'); + value.string[s++] = _T('\n'); + } + else value.string[s++] = _T(' '); + } + } + value.string[s] = _T('\0'); + } + + if (! service->native) { + key = open_registry(service->name, KEY_WRITE); + if (! key) { + if (value.string) HeapFree(GetProcessHeap(), 0, value.string); + return 4; + } + } + + if (setting->native) ret = set_setting(service->name, service->handle, setting, &value, additional); + else ret = set_setting(service->name, key, setting, &value, additional); + if (value.string) HeapFree(GetProcessHeap(), 0, value.string); + if (ret < 0) { + if (! service->native) RegCloseKey(key); + CloseHandle(service->handle); + return 6; } - nssm_gui(IDD_EDIT, service); + if (! service->native) RegCloseKey(key); + CloseHandle(service->handle); return 0; } @@ -581,7 +748,7 @@ int edit_service(nssm_service_t *service, bool editing) { /* Username must be NULL if we aren't changing or an account name. - We must explicitly user LOCALSYSTEM to change it when we are editing. + We must explicitly use LOCALSYSTEM to change it when we are editing. Password must be NULL if we aren't changing, a password or "". Empty passwords are valid but we won't allow them in the GUI. */ diff --git a/service.h b/service.h index a7914a1..515b108 100644 --- a/service.h +++ b/service.h @@ -20,6 +20,13 @@ #define ACTION_LEN 16 #define NSSM_LOCALSYSTEM_ACCOUNT _T("LocalSystem") +#define NSSM_KERNEL_DRIVER _T("SERVICE_KERNEL_DRIVER") +#define NSSM_FILE_SYSTEM_DRIVER _T("SERVICE_FILE_SYSTEM_DRIVER") +#define NSSM_WIN32_OWN_PROCESS _T("SERVICE_WIN32_OWN_PROCESS") +#define NSSM_WIN32_SHARE_PROCESS _T("SERVICE_WIN32_SHARE_PROCESS") +#define NSSM_INTERACTIVE_PROCESS _T("SERVICE_INTERACTIVE_PROCESS") +#define NSSM_SHARE_INTERACTIVE_PROCESS NSSM_WIN32_SHARE_PROCESS _T("|") NSSM_INTERACTIVE_PROCESS +#define NSSM_UNKNOWN _T("?") typedef struct { bool native; @@ -94,6 +101,7 @@ int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *); int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *); int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *); int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *); +int grant_logon_as_service(const TCHAR *); int pre_install_service(int, TCHAR **); int pre_remove_service(int, TCHAR **); int pre_edit_service(int, TCHAR **); diff --git a/settings.cpp b/settings.cpp new file mode 100644 index 0000000..e64fdc5 --- /dev/null +++ b/settings.cpp @@ -0,0 +1,672 @@ +#include "nssm.h" +/* XXX: (value && value->string) is probably bogus because value is probably never null */ + +extern const TCHAR *exit_action_strings[]; +extern const TCHAR *startup_strings[]; + +/* Does the parameter refer to the default value of the AppExit setting? */ +static inline int is_default_exit_action(const TCHAR *value) { + return (str_equiv(value, _T("default")) || str_equiv(value, _T("*")) || ! value[0]); +} + +static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) { + size_t len = _tcslen(string); + if (! len++) { + value->string = 0; + return 0; + } + + value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR)); + if (! value->string) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()")); + return -1; + } + + if (_sntprintf_s(value->string, len, _TRUNCATE, _T("%s"), string) < 0) { + HeapFree(GetProcessHeap(), 0, value->string); + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()")); + return -1; + } + + return 1; +} + +/* Functions to manage NSSM-specific settings in the registry. */ +static int setting_set_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + if (! key) return -1; + + unsigned long number; + long error; + + /* Resetting to default? */ + if (! value || ! value->string) { + error = RegDeleteValue(key, name); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error)); + return -1; + } + if (str_number(value->string, &number)) return -1; + + if (default_value && number == (unsigned long) default_value) { + error = RegDeleteValue(key, name); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error)); + return -1; + } + + if (set_number(key, (TCHAR *) name, number)) return -1; + + return 1; +} + +static int setting_get_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + return get_number(key, (TCHAR *) name, &value->numeric, false); +} + +static int setting_set_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + if (! key) return -1; + + long error; + + /* Resetting to default? */ + if (! value || ! value->string) { + if (default_value) value->string = (TCHAR *) default_value; + else { + error = RegDeleteValue(key, name); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error)); + return -1; + } + } + if (default_value && _tcslen((TCHAR *) default_value) && str_equiv(value->string, (TCHAR *) default_value)) { + error = RegDeleteValue(key, name); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error)); + return -1; + } + + if (set_expand_string(key, (TCHAR *) name, value->string)) return -1; + + return 1; +} + +static int setting_get_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + TCHAR buffer[VALUE_LENGTH]; + + if (expand_parameter(key, (TCHAR *) name, (TCHAR *) buffer, (unsigned long) sizeof(buffer), false, false)) return -1; + + return value_from_string(name, value, buffer); +} + +static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + unsigned long exitcode; + TCHAR *code; + TCHAR action_string[ACTION_LEN]; + + if (additional) { + /* Default action? */ + if (is_default_exit_action(additional)) code = 0; + else { + if (str_number(additional, &exitcode)) return -1; + code = (TCHAR *) additional; + } + } + + HKEY key = open_registry(service_name, name, KEY_WRITE); + if (! key) return -1; + + long error; + int ret = 1; + + /* Resetting to default? */ + if (value && value->string) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), value->string); + else { + if (code) { + /* Delete explicit action. */ + error = RegDeleteValue(key, code); + RegCloseKey(key); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(error)); + return -1; + } + else { + /* Explicitly keep the default action. */ + if (default_value) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), (TCHAR *) default_value); + ret = 0; + } + } + + /* Validate the string. */ + for (int i = 0; exit_action_strings[i]; i++) { + if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) { + if (default_value && str_equiv(action_string, (TCHAR *) default_value)) ret = 0; + if (RegSetValueEx(key, code, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) { + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(GetLastError())); + RegCloseKey(key); + return -1; + } + + RegCloseKey(key); + return ret; + } + } + + print_message(stderr, NSSM_MESSAGE_INVALID_EXIT_ACTION, action_string); + for (int i = 0; exit_action_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), exit_action_strings[i]); + + return -1; +} + +static int setting_get_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + unsigned long exitcode = 0; + unsigned long *code = 0; + + if (additional) { + if (! is_default_exit_action(additional)) { + if (str_number(additional, &exitcode)) return -1; + code = &exitcode; + } + } + + TCHAR action_string[ACTION_LEN]; + bool default_action; + if (get_exit_action(service_name, code, action_string, &default_action)) return -1; + + value_from_string(name, value, action_string); + + if (default_action && ! _tcsnicmp((const TCHAR *) action_string, (TCHAR *) default_value, ACTION_LEN)) return 0; + return 1; +} + +static int setting_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + if (! param) return -1; + + if (! value || ! value->string || ! value->string[0]) { + long error = RegDeleteValue(key, name); + if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0; + print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error)); + return -1; + } + + unsigned long envlen = (unsigned long) _tcslen(value->string) + 1; + TCHAR *unformatted = 0; + unsigned long newlen; + if (unformat_environment(value->string, envlen, &unformatted, &newlen)) return -1; + + if (test_environment(unformatted)) { + HeapFree(GetProcessHeap(), 0, unformatted); + print_message(stderr, NSSM_GUI_INVALID_ENVIRONMENT); + return -1; + } + + if (RegSetValueEx(key, name, 0, REG_MULTI_SZ, (const unsigned char *) unformatted, (unsigned long) newlen * sizeof(TCHAR)) != ERROR_SUCCESS) { + if (newlen) HeapFree(GetProcessHeap(), 0, unformatted); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0); + return -1; + } + + if (newlen) HeapFree(GetProcessHeap(), 0, unformatted); + return 1; +} + +static int setting_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) { + HKEY key = (HKEY) param; + if (! param) return -1; + + TCHAR *env = 0; + unsigned long envlen; + if (set_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1; + if (! envlen) return 0; + + TCHAR *formatted; + unsigned long newlen; + if (format_environment(env, envlen, &formatted, &newlen)) return -1; + + int ret; + if (additional) { + /* Find named environment variable. */ + TCHAR *s; + size_t len = _tcslen(additional); + for (s = env; *s; s++) { + /* Look for = NULL NULL */ + if (! _tcsnicmp(s, additional, len) && s[len] == _T('=')) { + /* Strip = */ + s += len + 1; + ret = value_from_string(name, value, s); + HeapFree(GetProcessHeap(), 0, env); + return ret; + } + + /* Skip this string. */ + for ( ; *s; s++); + } + HeapFree(GetProcessHeap(), 0, env); + return 0; + } + + HeapFree(GetProcessHeap(), 0, env); + + ret = value_from_string(name, value, formatted); + if (newlen) HeapFree(GetProcessHeap(), 0, formatted); + return ret; +} + +/* Functions to manage native service settings. */ +int native_set_description(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; + + TCHAR *description = 0; + if (value) description = value->string; + if (set_service_description(service_name, service_handle, description)) return -1; + + if (description && description[0]) return 1; + + return 0; +} + +int native_get_description(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; + + TCHAR buffer[VALUE_LENGTH]; + if (get_service_description(service_name, service_handle, _countof(buffer), buffer)) return -1; + + if (buffer[0]) return value_from_string(name, value, buffer); + value->string = 0; + + return 0; +} + +int native_set_displayname(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; + + TCHAR *displayname = 0; + if (value && value->string) displayname = value->string; + else displayname = (TCHAR *) service_name; + + if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, displayname)) { + print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); + return -1; + } + + /* + If the display name and service name differ only in case, + ChangeServiceConfig() will return success but the display name will be + set to the service name, NOT the value passed to the function. + This appears to be a quirk of Windows rather than a bug here. + */ + if (displayname != service_name && ! str_equiv(displayname, service_name)) return 1; + + return 0; +} + +int native_get_displayname(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; + + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + int ret = value_from_string(name, value, qsc->lpDisplayName); + HeapFree(GetProcessHeap(), 0, qsc); + + 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; + + /* It makes no sense to try to reset the image path. */ + if (! value || ! value->string) { + print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); + return -1; + } + + if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, value->string, 0, 0, 0, 0, 0, 0)) { + print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); + return -1; + } + + return 1; +} + +int native_get_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; + + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + int ret = value_from_string(name, value, qsc->lpBinaryPathName); + HeapFree(GetProcessHeap(), 0, qsc); + + return ret; +} + +int native_set_objectname(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; + + /* + Logical syntax is: nssm set ObjectName + That means the username is actually passed in the additional parameter. + */ + bool localsystem = true; + TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT; + TCHAR *password = 0; + if (additional) { + if (! str_equiv(additional, NSSM_LOCALSYSTEM_ACCOUNT)) { + localsystem = false; + username = (TCHAR *) additional; + if (value && value->string) password = value->string; + else { + /* We need a password if the account is not LOCALSYSTEM. */ + print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name); + return -1; + } + } + } + + /* + ChangeServiceConfig() will fail to set the username if the service is set + to interact with the desktop. + */ + unsigned long type = SERVICE_NO_CHANGE; + if (! localsystem) { + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) { + if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR)); + return -1; + } + + type = qsc->dwServiceType & ~SERVICE_INTERACTIVE_PROCESS; + HeapFree(GetProcessHeap(), 0, qsc); + } + + if (grant_logon_as_service(username)) { + if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR)); + print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username); + return -1; + } + + if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) { + if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR)); + print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); + return -1; + } + if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR)); + + if (localsystem) return 0; + + return 1; +} + +int native_get_objectname(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; + + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + int ret = value_from_string(name, value, qsc->lpServiceStartName); + HeapFree(GetProcessHeap(), 0, qsc); + + return ret; +} + +int native_set_startup(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; + + /* It makes no sense to try to reset the startup type. */ + if (! value || ! value->string) { + print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); + return -1; + } + + /* Map NSSM_STARTUP_* constant to Windows SERVICE_*_START constant. */ + int service_startup = -1; + int i; + for (i = 0; startup_strings[i]; i++) { + if (str_equiv(value->string, startup_strings[i])) { + service_startup = i; + break; + } + } + + if (service_startup < 0) { + print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_STARTUP, value->string); + for (i = 0; startup_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), startup_strings[i]); + return -1; + } + + unsigned long startup; + switch (service_startup) { + case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break; + case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break; + default: startup = SERVICE_AUTO_START; + } + + if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) { + print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); + return -1; + } + + SERVICE_DELAYED_AUTO_START_INFO delayed; + ZeroMemory(&delayed, sizeof(delayed)); + if (service_startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1; + else delayed.fDelayedAutostart = 0; + if (! ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) { + unsigned long error = GetLastError(); + /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */ + if (error != ERROR_INVALID_LEVEL) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service_name, error_string(error), 0); + } + } + + return 1; +} + +int native_get_startup(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; + + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + unsigned long startup; + int ret = get_service_startup(service_name, service_handle, qsc, &startup); + HeapFree(GetProcessHeap(), 0, qsc); + + if (ret) return -1; + + unsigned long i; + for (i = 0; startup_strings[i]; i++); + if (startup >= i) return -1; + + return value_from_string(name, value, startup_strings[startup]); +} + +int native_set_type(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; + + /* It makes no sense to try to reset the service type. */ + if (! value || ! value->string) { + print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name); + return -1; + } + + /* + We can only manage services of type SERVICE_WIN32_OWN_PROCESS + and SERVICE_INTERACTIVE_PROCESS. + */ + unsigned long type = SERVICE_WIN32_OWN_PROCESS; + if (str_equiv(value->string, NSSM_INTERACTIVE_PROCESS)) type |= SERVICE_INTERACTIVE_PROCESS; + else if (! str_equiv(value->string, NSSM_WIN32_OWN_PROCESS)) { + print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_TYPE, value->string); + _ftprintf(stderr, _T("%s\n"), NSSM_WIN32_OWN_PROCESS); + _ftprintf(stderr, _T("%s\n"), NSSM_INTERACTIVE_PROCESS); + return -1; + } + + /* + ChangeServiceConfig() will fail if the service runs under an account + other than LOCALSYSTEM and we try to make it interactive. + */ + if (type & SERVICE_INTERACTIVE_PROCESS) { + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) { + HeapFree(GetProcessHeap(), 0, qsc); + print_message(stderr, NSSM_MESSAGE_INTERACTIVE_NOT_LOCALSYSTEM, value->string, service_name, NSSM_LOCALSYSTEM_ACCOUNT); + return -1; + } + + HeapFree(GetProcessHeap(), 0, qsc); + } + + if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) { + print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError())); + return -1; + } + + return 1; +} + +int native_get_type(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; + + QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); + if (! qsc) return -1; + + value->numeric = qsc->dwServiceType; + HeapFree(GetProcessHeap(), 0, qsc); + + const TCHAR *string; + switch (value->numeric) { + case SERVICE_KERNEL_DRIVER: string = NSSM_KERNEL_DRIVER; break; + case SERVICE_FILE_SYSTEM_DRIVER: string = NSSM_FILE_SYSTEM_DRIVER; break; + case SERVICE_WIN32_OWN_PROCESS: string = NSSM_WIN32_OWN_PROCESS; break; + case SERVICE_WIN32_SHARE_PROCESS: string = NSSM_WIN32_SHARE_PROCESS; break; + case SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_INTERACTIVE_PROCESS; break; + case SERVICE_WIN32_SHARE_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_SHARE_INTERACTIVE_PROCESS; break; + default: string = NSSM_UNKNOWN; + } + + return value_from_string(name, value, string); +} + +int set_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) { + if (! key) return -1; + int ret; + + if (setting->set) ret = setting->set(service_name, (void *) key, setting->name, setting->default_value, value, additional); + else ret = -1; + + if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name); + else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name); + else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name); + + return ret; +} + +int set_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) { + if (! service_handle) return -1; + + int ret; + if (setting->set) ret = setting->set(service_name, service_handle, setting->name, setting->default_value, value, additional); + else ret = -1; + + if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name); + else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name); + else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name); + + return ret; +} + +/* + Returns: 1 if the value was retrieved. + 0 if the default value was retrieved. + -1 on error. +*/ +int get_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) { + if (! key) return -1; + int ret; + + switch (setting->type) { + case REG_EXPAND_SZ: + case REG_MULTI_SZ: + case REG_SZ: + value->string = (TCHAR *) setting->default_value; + if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional); + else ret = -1; + break; + + case REG_DWORD: + value->numeric = (unsigned long) setting->default_value; + if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional); + else ret = -1; + break; + + default: + ret = -1; + break; + } + + if (ret < 0) print_message(stderr, NSSM_MESSAGE_GET_SETTING_FAILED, setting->name, service_name); + + return ret; +} + +int get_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) { + if (! service_handle) return -1; + return setting->get(service_name, service_handle, setting->name, 0, value, additional); +} + +settings_t settings[] = { + { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action }, + { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment }, + { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment }, + { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string }, + { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number }, + { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number }, + { 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_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath }, + { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, ADDITIONAL_SETTING, native_set_objectname, native_get_objectname }, + { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup }, + { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type }, + { NULL, NULL, NULL, NULL, NULL } +}; diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..71e31d1 --- /dev/null +++ b/settings.h @@ -0,0 +1,40 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#define NSSM_NATIVE_DESCRIPTION _T("Description") +#define NSSM_NATIVE_DISPLAYNAME _T("DisplayName") +#define NSSM_NATIVE_IMAGEPATH _T("ImagePath") +#define NSSM_NATIVE_OBJECTNAME _T("ObjectName") +#define NSSM_NATIVE_STARTUP _T("Start") +#define NSSM_NATIVE_TYPE _T("Type") + +/* Are additional arguments needed? */ +#define ADDITIONAL_GETTING (1 << 0) +#define ADDITIONAL_SETTING (1 << 1) +#define ADDITIONAL_RESETTING (1 << 2) +#define ADDITIONAL_CRLF (1 << 3) +#define ADDITIONAL_MANDATORY ADDITIONAL_GETTING|ADDITIONAL_SETTING|ADDITIONAL_RESETTING + +typedef union { + unsigned long numeric; + TCHAR *string; +} value_t; + +typedef int (*setting_function_t)(const TCHAR *, void *, const TCHAR *, void *, value_t *, const TCHAR *); + +typedef struct { + const TCHAR *name; + unsigned long type; + void *default_value; + bool native; + int additional; + setting_function_t set; + setting_function_t get; +} settings_t; + +int set_setting(const TCHAR *, HKEY, settings_t *, value_t *, const TCHAR *); +int set_setting(const TCHAR *, SC_HANDLE, settings_t *, value_t *, const TCHAR *); +int get_setting(const TCHAR *, HKEY, settings_t *, value_t *, const TCHAR *); +int get_setting(const TCHAR *, SC_HANDLE, settings_t *, value_t *, const TCHAR *); + +#endif -- 2.20.1