#define NSSM_DATE "2010-04-04"\r
#define NSSM_RUN "run"\r
\r
+/*\r
+ MSDN says the commandline in CreateProcess() is limited to 32768 characters\r
+ and the application name to MAX_PATH.\r
+ A registry key is limited to 255 characters.\r
+ A registry value is limited to 16383 characters.\r
+ Therefore we limit the service name to accommodate the path under HKLM.\r
+*/\r
+#define EXE_LENGTH MAX_PATH\r
+#define CMD_LENGTH 32768\r
+#define KEY_LENGTH 255\r
+#define VALUE_LENGTH 16383\r
+#define SERVICE_NAME_LENGTH KEY_LENGTH - 55\r
+\r
#endif\r
int create_messages() {\r
HKEY key;\r
\r
- char registry[MAX_PATH];\r
+ char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), "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
int create_parameters(char *service_name, char *exe, char *flags, char *dir) {\r
/* Get registry */\r
- char registry[MAX_PATH];\r
+ char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), 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
int create_exit_action(char *service_name, const char *action_string) {\r
/* Get registry */\r
- char registry[MAX_PATH];\r
+ char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), 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
int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {\r
/* Get registry */\r
- char registry[MAX_PATH];\r
+ char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), 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
int get_exit_action(char *service_name, unsigned long *ret, unsigned char *action) {\r
/* Get registry */\r
- char registry[MAX_PATH];\r
+ char registry[KEY_LENGTH];\r
if (_snprintf(registry, sizeof(registry), 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
SERVICE_STATUS_HANDLE service_handle;\r
HANDLE wait_handle;\r
HANDLE pid;\r
-static char service_name[MAX_PATH];\r
-char exe[MAX_PATH];\r
-char flags[MAX_PATH];\r
+static char service_name[SERVICE_NAME_LENGTH];\r
+char exe[EXE_LENGTH];\r
+char flags[CMD_LENGTH];\r
char dir[MAX_PATH];\r
\r
static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY } exit_actions;\r
GetModuleFileName(0, path, MAX_PATH);\r
\r
/* Construct command */\r
- char command[MAX_PATH];\r
+ char command[CMD_LENGTH];\r
size_t runlen = strlen(NSSM_RUN);\r
size_t pathlen = strlen(path);\r
- if (pathlen + runlen + 2 >= MAX_PATH) {\r
+ if (pathlen + runlen + 2 >= VALUE_LENGTH) {\r
fprintf(stderr, "The full path to " NSSM " is too long!\n");\r
return 3;\r
}\r
ZeroMemory(&pi, sizeof(pi));\r
\r
/* Launch executable with arguments */\r
- char cmd[MAX_PATH];\r
+ char cmd[CMD_LENGTH];\r
if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);\r
return stop_service(2);\r