}\r
\r
if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) error_message, NSSM_ERROR_BUFSIZE, 0)) {\r
- if (_snprintf(error_message, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0;\r
+ if (_snprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, "system error %lu", error) < 0) return 0;\r
}\r
return error_message;\r
}\r
char *ret;\r
if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {\r
ret = (char *) HeapAlloc(GetProcessHeap(), 0, 32);\r
- if (_snprintf(ret, NSSM_ERROR_BUFSIZE, "system error %lu", error) < 0) return 0;\r
+ if (_snprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, "system error %lu", error) < 0) return 0;\r
}\r
return ret;\r
}\r
\r
char blurb[256];\r
va_start(arg, id);\r
- if (vsnprintf(blurb, sizeof(blurb), format, arg) < 0) {\r
+ if (vsnprintf_s(blurb, sizeof(blurb), _TRUNCATE, format, arg) < 0) {\r
va_end(arg);\r
LocalFree(format);\r
return MessageBox(0, "Message %lu was supposed to go here!", NSSM, MB_OK | MB_ICONEXCLAMATION);\r
void browse(HWND window) {\r
if (! window) return;\r
\r
- unsigned long bufsize = 256;\r
- unsigned long len = bufsize;\r
+ size_t bufsize = 256;\r
+ size_t len = bufsize;\r
OPENFILENAME ofn;\r
ZeroMemory(&ofn, sizeof(ofn));\r
ofn.lStructSize = sizeof(ofn);\r
if (ofn.lpstrFilter) {\r
ZeroMemory((void *) ofn.lpstrFilter, bufsize);\r
char *localised = message_string(NSSM_GUI_BROWSE_FILTER_APPLICATIONS);\r
- _snprintf((char *) ofn.lpstrFilter, bufsize, localised);\r
+ _snprintf_s((char *) ofn.lpstrFilter, bufsize, _TRUNCATE, localised);\r
/* "Applications" + NULL + "*.exe" + NULL */\r
len = strlen(localised) + 1;\r
LocalFree(localised);\r
- _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, "*.exe");\r
+ _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.exe");\r
/* "All files" + NULL + "*.*" + NULL */\r
len += 6;\r
localised = message_string(NSSM_GUI_BROWSE_FILTER_ALL_FILES);\r
- _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, localised);\r
+ _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, localised);\r
len += strlen(localised) + 1;\r
LocalFree(localised);\r
- _snprintf((char *) ofn.lpstrFilter + len, bufsize - len, "*.*");\r
+ _snprintf_s((char *) ofn.lpstrFilter + len, bufsize - len, _TRUNCATE, "*.*");\r
/* Remainder of the buffer is already zeroed */\r
}\r
ofn.lpstrFile = new char[MAX_PATH];\r
char value[NSSM_STDIO_LENGTH];\r
\r
/* Path. */\r
- if (_snprintf(value, sizeof(value), "%s", prefix) < 0) {\r
+ if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s", prefix) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, prefix, "get_createfile_parameters()", 0);\r
return 1;\r
}\r
}\r
\r
/* ShareMode. */\r
- if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_SHARING) < 0) {\r
+ if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_SHARING) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_SHARING, "get_createfile_parameters()", 0);\r
return 3;\r
}\r
}\r
\r
/* CreationDisposition. */\r
- if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {\r
+ if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_DISPOSITION) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_DISPOSITION, "get_createfile_parameters()", 0);\r
return 5;\r
}\r
}\r
\r
/* Flags. */\r
- if (_snprintf(value, sizeof(value), "%s%s", prefix, NSSM_REG_STDIO_FLAGS) < 0) {\r
+ if (_snprintf_s(value, sizeof(value), _TRUNCATE, "%s%s", prefix, NSSM_REG_STDIO_FLAGS) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, NSSM_REG_STDIO_FLAGS, "get_createfile_parameters()", 0);\r
return 7;\r
}\r
if (get_createfile_parameters(key, NSSM_REG_STDOUT, path, &sharing, FILE_SHARE_READ | FILE_SHARE_WRITE, &disposition, OPEN_ALWAYS, &flags, FILE_ATTRIBUTE_NORMAL)) return 3;\r
if (path[0]) {\r
/* Remember path for comparison with stderr. */\r
- if (_snprintf(stdout_path, sizeof(stdout_path), "%s", path) < 0) {\r
+ if (_snprintf_s(stdout_path, sizeof(stdout_path), _TRUNCATE, "%s", path) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "stdout_path", "get_output_handles", 0);\r
return 4;\r
}\r
HANDLE process_handle = OpenProcess(PROCESS_QUERY_INFORMATION, false, pe->th32ProcessID);
if (! process_handle) {
char pid_string[16];
- _snprintf(pid_string, sizeof(pid_string), "%d", pe->th32ProcessID);
+ _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%d", pe->th32ProcessID);
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENPROCESS_FAILED, pid_string, service_name, error_string(GetLastError()), 0);
return 2;
}
if (! pid) return;
char pid_string[16], code[16];
- _snprintf(pid_string, sizeof(pid_string), "%d", pid);
- _snprintf(code, sizeof(code), "%d", exitcode);
+ _snprintf_s(pid_string, sizeof(pid_string), _TRUNCATE, "%d", pid);
+ _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode);
log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILLING, service_name, pid_string, code, 0);
/* Get a snapshot of all processes in the system. */
}
char ppid_string[16];
- _snprintf(ppid_string, sizeof(ppid_string), "%d", ppid);
+ _snprintf_s(ppid_string, sizeof(ppid_string), _TRUNCATE, "%d", ppid);
log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, service_name, 0);
if (! kill_process(service_name, stop_method, process_handle, pid, exitcode)) {
/* Maybe it already died. */
HKEY key;\r
\r
char registry[KEY_LENGTH];\r
- if (_snprintf(registry, sizeof(registry), "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {\r
+ if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "eventlog registry", "create_messages()", 0);\r
return 1;\r
}\r
GetModuleFileName(0, path, MAX_PATH);\r
\r
/* Try to register the module but don't worry so much on failure */\r
- RegSetValueEx(key, "EventMessageFile", 0, REG_SZ, (const unsigned char *) path, strlen(path) + 1);\r
+ RegSetValueEx(key, "EventMessageFile", 0, REG_SZ, (const unsigned char *) path, (unsigned long) strlen(path) + 1);\r
unsigned long types = EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;\r
RegSetValueEx(key, "TypesSupported", 0, REG_DWORD, /*XXX*/(PBYTE) &types, sizeof(types));\r
\r
int create_parameters(char *service_name, char *exe, char *flags, char *dir) {\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
- if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
+ if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY, service_name) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "create_parameters()", 0);\r
return 1;\r
}\r
}\r
\r
/* Try to create the parameters */\r
- if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_EXPAND_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_EXPAND_SZ, (const unsigned char *) exe, (unsigned long) strlen(exe) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXE, error_string(GetLastError()), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
return 3;\r
}\r
- if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_EXPAND_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_EXPAND_SZ, (const unsigned char *) flags, (unsigned long) strlen(flags) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_FLAGS, error_string(GetLastError()), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
return 4;\r
}\r
- if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_EXPAND_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_EXPAND_SZ, (const unsigned char *) dir, (unsigned long) strlen(dir) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_DIR, error_string(GetLastError()), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
int create_exit_action(char *service_name, const char *action_string) {\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
- if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
+ if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "create_exit_action()", 0);\r
return 1;\r
}\r
}\r
\r
/* Create the default value */\r
- if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, strlen(action_string) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, (unsigned long) strlen(action_string) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXIT, error_string(GetLastError()), 0);\r
RegCloseKey(key);\r
return 3;\r
return get_number(key, value, number, true);\r
}\r
\r
-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 *stop_method, STARTUPINFO *si) {\r
+int get_parameters(char *service_name, char *exe, unsigned long exelen, char *flags, unsigned long flagslen, char *dir, unsigned long dirlen, char **env, unsigned long *throttle_delay, unsigned long *stop_method, STARTUPINFO *si) {\r
unsigned long ret;\r
\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
- if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
+ if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY, service_name) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REGISTRY", "get_parameters()", 0);\r
return 1;\r
}\r
if (ret != ERROR_FILE_NOT_FOUND) {\r
if (type != REG_DWORD) {\r
char milliseconds[16];\r
- _snprintf(milliseconds, sizeof(milliseconds), "%lu", NSSM_RESET_THROTTLE_RESTART);\r
+ _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%lu", NSSM_RESET_THROTTLE_RESTART);\r
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_THROTTLE, service_name, NSSM_REG_THROTTLE, milliseconds, 0);\r
}\r
else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_THROTTLE, error_string(GetLastError()), 0);\r
\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
- if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
+ if (_snprintf_s(registry, sizeof(registry), _TRUNCATE, NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "NSSM_REG_EXIT", "get_exit_action()", 0);\r
return 1;\r
}\r
\r
char code[64];\r
if (! ret) code[0] = '\0';\r
- else if (_snprintf(code, sizeof(code), "%lu", *ret) < 0) {\r
+ else if (_snprintf_s(code, sizeof(code), _TRUNCATE, "%lu", *ret) < 0) {\r
RegCloseKey(key);\r
return get_exit_action(service_name, 0, action, default_action);\r
}\r
int expand_parameter(HKEY, char *, char *, unsigned long, bool);\r
int get_number(HKEY, char *, unsigned long *, bool);\r
int get_number(HKEY, char *, unsigned long *);\r
-int get_parameters(char *, char *, int, char *, int, char *, int, char **, unsigned long *, unsigned long *, STARTUPINFO *);\r
+int get_parameters(char *, char *, unsigned long, char *, unsigned long, char *, unsigned long, char **, unsigned long *, unsigned long *, STARTUPINFO *);\r
int get_exit_action(char *, unsigned long *, unsigned char *, bool *);\r
\r
#endif\r
print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM);\r
return 3;\r
}\r
- if (_snprintf(command, sizeof(command), "\"%s\"", path) < 0) {\r
+ if (_snprintf_s(command, sizeof(command), _TRUNCATE, "\"%s\"", path) < 0) {\r
print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH);\r
return 4;\r
}\r
\r
/* Service initialisation */\r
void WINAPI service_main(unsigned long argc, char **argv) {\r
- if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {\r
+ if (_snprintf_s(service_name, sizeof(service_name), _TRUNCATE, "%s", argv[0]) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "service_name", "service_main()", 0);\r
return;\r
}\r
int ret = start_service();\r
if (ret) {\r
char code[16];\r
- _snprintf(code, sizeof(code), "%d", ret);\r
+ _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", ret);\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_START_SERVICE_FAILED, exe, service_name, ret, 0);\r
return ret;\r
}\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);\r
return;\r
}\r
- if (_snprintf(text, 11, "0x%08x", control) < 0) {\r
+ if (_snprintf_s(text, 11, _TRUNCATE, "0x%08x", control) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "control code", "log_service_control", 0);\r
HeapFree(GetProcessHeap(), 0, text);\r
return;\r
\r
/* Launch executable with arguments */\r
char cmd[CMD_LENGTH];\r
- if (_snprintf(cmd, sizeof(cmd), "\"%s\" %s", exe, flags) < 0) {\r
+ if (_snprintf_s(cmd, sizeof(cmd), _TRUNCATE, "\"%s\" %s", exe, flags) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);\r
close_output_handles(&si);\r
return stop_service(2, true, true);\r
\r
throttle_restart();\r
\r
- bool inherit_handles = (si.dwFlags & STARTF_USESTDHANDLES);\r
+ bool inherit_handles = false;\r
+ if (si.dwFlags & STARTF_USESTDHANDLES) inherit_handles = true;\r
if (! CreateProcess(0, cmd, 0, 0, inherit_handles, 0, env, dir, &si, &pi)) {\r
unsigned long error = GetLastError();\r
if (error == ERROR_INVALID_PARAMETER && env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service_name, exe, NSSM_REG_ENV, 0);\r
tree. See below for the possible values of the why argument.\r
*/\r
if (! why) {\r
- _snprintf(code, sizeof(code), "%d", exitcode);\r
+ _snprintf_s(code, sizeof(code), _TRUNCATE, "%d", exitcode);\r
log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_ENDED_SERVICE, exe, service_name, code, 0);\r
}\r
\r
if (throttle > 7) throttle = 8;\r
\r
char threshold[8], milliseconds[8];\r
- _snprintf(threshold, sizeof(threshold), "%d", throttle_delay);\r
- _snprintf(milliseconds, sizeof(milliseconds), "%d", ms);\r
+ _snprintf_s(threshold, sizeof(threshold), _TRUNCATE, "%d", throttle_delay);\r
+ _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%d", ms);\r
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);\r
\r
if (throttle_timer) {\r