X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=registry.cpp;h=d6cde2392d0ef1b6a927398809489488029fd60b;hb=9727e8ada22d4da8a492b7da0a19d88e4394d59e;hp=5172c50e91750fc26d66a5c201a785102281f8a4;hpb=530bd80072effe809a6d3a2fc5fd847e51342021;p=nssm.git diff --git a/registry.cpp b/registry.cpp index 5172c50..d6cde23 100644 --- a/registry.cpp +++ b/registry.cpp @@ -141,28 +141,34 @@ int set_environment(char *service_name, HKEY key, char **env) { return 0; } -int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) { +int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise, bool must_exist) { unsigned char *buffer = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, datalen); if (! buffer) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, "expand_parameter()", 0); return 1; } + ZeroMemory(data, datalen); + unsigned long type = REG_EXPAND_SZ; unsigned long buflen = datalen; unsigned long ret = RegQueryValueEx(key, value, 0, &type, buffer, &buflen); if (ret != ERROR_SUCCESS) { - if (ret != ERROR_FILE_NOT_FOUND) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); + unsigned long error = GetLastError(); HeapFree(GetProcessHeap(), 0, buffer); + + if (ret == ERROR_FILE_NOT_FOUND) { + if (! must_exist) return 0; + } + + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0); return 2; } /* Paths aren't allowed to contain quotes. */ if (sanitise) PathUnquoteSpaces((LPSTR) buffer); - ZeroMemory(data, datalen); - /* Technically we shouldn't expand environment strings from REG_SZ values */ if (type != REG_EXPAND_SZ) { memmove(data, buffer, buflen); @@ -181,6 +187,38 @@ int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, b return 0; } +int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) { + return expand_parameter(key, value, data, datalen, sanitise, true); +} + +/* + Query an unsigned long from the registry. + Returns: 1 if a number was retrieved. + 0 if none was found and must_exist is false. + -1 if none was found and must_exist is true. + -2 otherwise. +*/ +int get_number(HKEY key, char *value, unsigned long *number, bool must_exist) { + unsigned long type = REG_DWORD; + unsigned long number_len = sizeof(unsigned long); + + int ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) number, &number_len); + if (ret == ERROR_SUCCESS) return 1; + + if (ret == ERROR_FILE_NOT_FOUND) { + if (! must_exist) return 0; + } + + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); + if (ret == ERROR_FILE_NOT_FOUND) return -1; + + return -2; +} + +int get_number(HKEY key, char *value, unsigned long *number) { + return get_number(key, value, number, true); +} + int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen, char **env, unsigned long *throttle_delay) { unsigned long ret;