Strip quotes from directory name.
authorIain Patterson <me@iain.cx>
Wed, 16 Feb 2011 23:46:27 +0000 (23:46 +0000)
committerIain Patterson <me@iain.cx>
Wed, 16 Feb 2011 23:46:27 +0000 (23:46 +0000)
Windows directories aren't allowed to contain quotes so CreateProcess()
will fail if the AppDirectory is quoted.  Note that it succeeds even if
Application itself is quoted as the application plus parameters are
interpreted as a command line.

nssm.h
nssm.vcproj
registry.cpp
registry.h

diff --git a/nssm.h b/nssm.h
index af8380e..602c0c9 100644 (file)
--- a/nssm.h
+++ b/nssm.h
@@ -2,6 +2,7 @@
 #define NSSM_H\r
 \r
 #define _WIN32_WINNT 0x0500\r
+#include <shlwapi.h>\r
 #include <stdarg.h>\r
 #include <stdio.h>\r
 #include <windows.h>\r
index 4130d2e..b85c1e7 100755 (executable)
@@ -76,7 +76,7 @@
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
-                               OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
+                               AdditionalDependencies="shlwapi.lib"\r
                                LinkIncremental="2"\r
                                SuppressStartupBanner="true"\r
                                GenerateDebugInformation="true"\r
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
-                               OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
+                               AdditionalDependencies="shlwapi.lib"\r
                                LinkIncremental="2"\r
                                SuppressStartupBanner="true"\r
                                GenerateDebugInformation="true"\r
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
-                               OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
+                               AdditionalDependencies="shlwapi.lib"\r
                                LinkIncremental="1"\r
                                SuppressStartupBanner="true"\r
                                ProgramDatabaseFile=".\Release/nssm.pdb"\r
                        />\r
                        <Tool\r
                                Name="VCLinkerTool"\r
-                               OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
+                               AdditionalDependencies="shlwapi.lib"\r
                                LinkIncremental="1"\r
                                SuppressStartupBanner="true"\r
                                ProgramDatabaseFile=".\Release/nssm.pdb"\r
index 0a74bd8..0471ca7 100644 (file)
@@ -102,7 +102,7 @@ int create_exit_action(char *service_name, const char *action_string) {
   return 0;\r
 }\r
 \r
-int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen) {\r
+int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) {\r
   unsigned char *buffer = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, datalen);\r
   if (! buffer) {\r
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, "expand_parameter()", 0);\r
@@ -119,6 +119,9 @@ int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen) {
     return 2;\r
   }\r
 \r
+  /* Paths aren't allowed to contain quotes. */\r
+  if (sanitise) PathUnquoteSpaces((LPSTR) buffer);\r
+\r
   ZeroMemory(data, datalen);\r
 \r
   /* Technically we shouldn't expand environment strings from REG_SZ values */\r
@@ -155,19 +158,19 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
   }\r
 \r
   /* Try to get executable file - MUST succeed */\r
-  if (expand_parameter(key, NSSM_REG_EXE, exe, exelen)) {\r
+  if (expand_parameter(key, NSSM_REG_EXE, exe, exelen, false)) {\r
     RegCloseKey(key);\r
     return 3;\r
   }\r
 \r
   /* Try to get flags - may fail and we don't care */\r
-  if (expand_parameter(key, NSSM_REG_FLAGS, flags, flagslen)) {\r
+  if (expand_parameter(key, NSSM_REG_FLAGS, flags, flagslen, false)) {\r
     log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service_name, exe, 0);\r
     ZeroMemory(flags, flagslen);\r
   }\r
 \r
   /* Try to get startup directory - may fail and we fall back to a default */\r
-  if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen) || ! dir[0]) {\r
+  if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen, true) || ! dir[0]) {\r
     /* Our buffers are defined to be long enough for this to be safe */\r
     size_t i;\r
     for (i = strlen(exe); i && exe[i] != '\\' && exe[i] != '/'; i--);\r
index f814e42..9f5eaf1 100644 (file)
@@ -10,7 +10,7 @@
 int create_messages();\r
 int create_parameters(char *, char *, char *, char *);\r
 int create_exit_action(char *, const char *);\r
-int expand_parameter(HKEY, char *, char *, unsigned long);\r
+int expand_parameter(HKEY, char *, char *, unsigned long, bool);\r
 int get_parameters(char *, char *, int, char *, int, char *, int);\r
 int get_exit_action(char *, unsigned long *, unsigned char *, bool *);\r
 \r