Moved environment functions to a new file.
authorIain Patterson <me@iain.cx>
Thu, 23 Jan 2014 20:05:25 +0000 (20:05 +0000)
committerIain Patterson <me@iain.cx>
Thu, 23 Jan 2014 20:05:25 +0000 (20:05 +0000)
Moved format_environment(), unformat_environment() and
test_environment() to env.cpp.

env.cpp [new file with mode: 0644]
env.h [new file with mode: 0644]
nssm.h
nssm.vcproj
process.cpp
process.h
registry.cpp
registry.h

diff --git a/env.cpp b/env.cpp
new file mode 100644 (file)
index 0000000..922c96f
--- /dev/null
+++ b/env.cpp
@@ -0,0 +1,96 @@
+#include "nssm.h"
+
+/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
+int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {
+  unsigned long i, j;
+  *newlen = envlen;
+
+  if (! *newlen) {
+    *formatted = 0;
+    return 0;
+  }
+
+  for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;
+
+  *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
+  if (! *formatted) {
+    *newlen = 0;
+    return 1;
+  }
+
+  for (i = 0, j = 0; i < envlen; i++) {
+    (*formatted)[j] = env[i];
+    if (! env[i]) {
+      if (env[i + 1]) {
+        (*formatted)[j] = _T('\r');
+        (*formatted)[++j] = _T('\n');
+      }
+    }
+    j++;
+  }
+
+  return 0;
+}
+
+/* Strip CR and replace LF with NULL. */
+int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {
+  unsigned long i, j;
+  *newlen = 0;
+
+  if (! envlen) {
+    *unformatted = 0;
+    return 0;
+  }
+
+  for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;
+  /* Must end with two NULLs. */
+  *newlen += 2;
+
+  *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
+  if (! *unformatted) return 1;
+
+  for (i = 0, j = 0; i < envlen; i++) {
+    if (env[i] == _T('\r')) continue;
+    if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');
+    else (*unformatted)[j] = env[i];
+    j++;
+  }
+
+  return 0;
+}
+
+/*
+  Verify an environment block.
+  Returns:  1 if environment is invalid.
+            0 if environment is OK.
+           -1 on error.
+*/
+int test_environment(TCHAR *env) {
+  TCHAR path[PATH_LENGTH];
+  GetModuleFileName(0, path, _countof(path));
+  STARTUPINFO si;
+  ZeroMemory(&si, sizeof(si));
+  si.cb = sizeof(si);
+  PROCESS_INFORMATION pi;
+  ZeroMemory(&pi, sizeof(pi));
+  unsigned long flags = CREATE_SUSPENDED;
+#ifdef UNICODE
+  flags |= CREATE_UNICODE_ENVIRONMENT;
+#endif
+
+  /*
+    Try to relaunch ourselves but with the candidate environment set.
+    Assuming no solar flare activity, the only reason this would fail is if
+    the environment were invalid.
+  */
+  if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
+    TerminateProcess(pi.hProcess, 0);
+  }
+  else {
+    unsigned long error = GetLastError();
+    if (error == ERROR_INVALID_PARAMETER) return 1;
+    else return -1;
+  }
+
+  return 0;
+}
diff --git a/env.h b/env.h
new file mode 100644 (file)
index 0000000..f23c592
--- /dev/null
+++ b/env.h
@@ -0,0 +1,8 @@
+#ifndef ENV_H
+#define ENV_H
+
+int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
+int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);
+int test_environment(TCHAR *);
+
+#endif
diff --git a/nssm.h b/nssm.h
index 1ad2b94..6d1f315 100644 (file)
--- a/nssm.h
+++ b/nssm.h
@@ -41,6 +41,7 @@
 #include <tchar.h>\r
 #include <windows.h>\r
 #include "service.h"\r
+#include "env.h"\r
 #include "event.h"\r
 #include "imports.h"\r
 #include "messages.h"\r
index a354922..a7947fc 100755 (executable)
                        Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
                        >\r
                        <File\r
+                               RelativePath="env.cpp"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath="event.cpp"\r
                                >\r
                                <FileConfiguration\r
                        Filter="h;hpp;hxx;hm;inl"\r
                        >\r
                        <File\r
+                               RelativePath="env.h"\r
+                               >\r
+                       </File>\r
+                       <File\r
                                RelativePath="event.h"\r
                                >\r
                        </File>\r
index 75956de..59707c4 100644 (file)
@@ -313,39 +313,3 @@ void kill_process_tree(nssm_service_t *service, unsigned long pid, unsigned long
 
   CloseHandle(process_handle);
 }
-
-/*
-  Verify an environment block.
-  Returns:  1 if environment is invalid.
-            0 if environment is OK.
-           -1 on error.
-*/
-int test_environment(TCHAR *env) {
-  TCHAR path[PATH_LENGTH];
-  GetModuleFileName(0, path, _countof(path));
-  STARTUPINFO si;
-  ZeroMemory(&si, sizeof(si));
-  si.cb = sizeof(si);
-  PROCESS_INFORMATION pi;
-  ZeroMemory(&pi, sizeof(pi));
-  unsigned long flags = CREATE_SUSPENDED;
-#ifdef UNICODE
-  flags |= CREATE_UNICODE_ENVIRONMENT;
-#endif
-
-  /*
-    Try to relaunch ourselves but with the candidate environment set.
-    Assuming no solar flare activity, the only reason this would fail is if
-    the environment were invalid.
-  */
-  if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
-    TerminateProcess(pi.hProcess, 0);
-  }
-  else {
-    unsigned long error = GetLastError();
-    if (error == ERROR_INVALID_PARAMETER) return 1;
-    else return -1;
-  }
-
-  return 0;
-}
index a8f2989..3489f54 100644 (file)
--- a/process.h
+++ b/process.h
@@ -17,6 +17,5 @@ int kill_threads(nssm_service_t *, kill_t *);
 int kill_console(nssm_service_t *);
 int kill_process(nssm_service_t *, HANDLE, unsigned long, unsigned long);
 void kill_process_tree(nssm_service_t *, unsigned long, unsigned long, unsigned long);
-int test_environment(TCHAR *);
 
 #endif
index 158158d..c794ed1 100644 (file)
@@ -222,64 +222,6 @@ int set_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, un
   return 0;\r
 }\r
 \r
-/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */\r
-int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {\r
-  unsigned long i, j;\r
-  *newlen = envlen;\r
-\r
-  if (! *newlen) {\r
-    *formatted = 0;\r
-    return 0;\r
-  }\r
-\r
-  for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;\r
-\r
-  *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));\r
-  if (! *formatted) {\r
-    *newlen = 0;\r
-    return 1;\r
-  }\r
-\r
-  for (i = 0, j = 0; i < envlen; i++) {\r
-    (*formatted)[j] = env[i];\r
-    if (! env[i]) {\r
-      if (env[i + 1]) {\r
-        (*formatted)[j] = _T('\r');\r
-        (*formatted)[++j] = _T('\n');\r
-      }\r
-    }\r
-    j++;\r
-  }\r
-\r
-  return 0;\r
-}\r
-\r
-/* Strip CR and replace LF with NULL. */\r
-int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {\r
-  unsigned long i, j;\r
-  *newlen = 0;\r
-\r
-  if (! envlen) {\r
-    *unformatted = 0;\r
-    return 0;\r
-  }\r
-\r
-  for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;\r
-  /* Must end with two NULLs. */\r
-  *newlen += 2;\r
-\r
-  *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));\r
-  if (! *unformatted) return 1;\r
-\r
-  for (i = 0, j = 0; i < envlen; i++) {\r
-    if (env[i] == _T('\r')) continue;\r
-    if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');\r
-    else (*unformatted)[j] = env[i];\r
-    j++;\r
-  }\r
-\r
-  return 0;\r
-}\r
 \r
 int get_string(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool expand, bool sanitise, bool must_exist) {\r
   TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);\r
index da3c8b8..390e032 100644 (file)
@@ -35,8 +35,6 @@ int create_messages();
 int create_parameters(nssm_service_t *, bool);\r
 int create_exit_action(TCHAR *, const TCHAR *, bool);\r
 int set_environment(TCHAR *, HKEY, TCHAR *, TCHAR **, unsigned long *);\r
-int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);\r
-int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);\r
 int get_string(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool, bool);\r
 int get_string(HKEY, TCHAR *, TCHAR *, unsigned long, bool);\r
 int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool);\r