env.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "nssm.h"
  2. /* Find the length in characters of an environment block. */
  3. size_t environment_length(TCHAR *env) {
  4. size_t len = 0;
  5. TCHAR *s;
  6. for (s = env; ; s++) {
  7. len++;
  8. if (*s == _T('\0')) {
  9. if (*(s + 1) == _T('\0')) {
  10. len++;
  11. break;
  12. }
  13. }
  14. }
  15. return len;
  16. }
  17. /* Copy an environment block. */
  18. TCHAR *copy_environment_block(TCHAR *env) {
  19. TCHAR *newenv;
  20. if (copy_double_null(env, (unsigned long) environment_length(env), &newenv)) return 0;
  21. return newenv;
  22. }
  23. /*
  24. The environment block starts with variables of the form
  25. =C:=C:\Windows\System32 which we ignore.
  26. */
  27. TCHAR *useful_environment(TCHAR *rawenv) {
  28. TCHAR *env = rawenv;
  29. if (env) {
  30. while (*env == _T('=')) {
  31. for ( ; *env; env++);
  32. env++;
  33. }
  34. }
  35. return env;
  36. }
  37. /* Expand an environment variable. Must call HeapFree() on the result. */
  38. TCHAR *expand_environment_string(TCHAR *string) {
  39. unsigned long len;
  40. len = ExpandEnvironmentStrings(string, 0, 0);
  41. if (! len) {
  42. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, string, error_string(GetLastError()), 0);
  43. return 0;
  44. }
  45. TCHAR *ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  46. if (! ret) {
  47. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("ExpandEnvironmentStrings()"), _T("expand_environment_string"), 0);
  48. return 0;
  49. }
  50. if (! ExpandEnvironmentStrings(string, ret, len)) {
  51. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, string, error_string(GetLastError()), 0);
  52. HeapFree(GetProcessHeap(), 0, ret);
  53. return 0;
  54. }
  55. return ret;
  56. }
  57. /*
  58. Set all the environment variables from an environment block in the current
  59. environment or remove all the variables in the block from the current
  60. environment.
  61. */
  62. static int set_environment_block(TCHAR *env, bool set) {
  63. int ret = 0;
  64. TCHAR *s, *t;
  65. for (s = env; *s; s++) {
  66. for (t = s; *t && *t != _T('='); t++);
  67. if (*t == _T('=')) {
  68. *t = _T('\0');
  69. if (set) {
  70. TCHAR *expanded = expand_environment_string(++t);
  71. if (expanded) {
  72. if (! SetEnvironmentVariable(s, expanded)) ret++;
  73. HeapFree(GetProcessHeap(), 0, expanded);
  74. }
  75. else {
  76. if (! SetEnvironmentVariable(s, t)) ret++;
  77. }
  78. }
  79. else {
  80. if (! SetEnvironmentVariable(s, NULL)) ret++;
  81. }
  82. for (t++; *t; t++);
  83. }
  84. s = t;
  85. }
  86. return ret;
  87. }
  88. int set_environment_block(TCHAR *env) {
  89. return set_environment_block(env, true);
  90. }
  91. static int unset_environment_block(TCHAR *env) {
  92. return set_environment_block(env, false);
  93. }
  94. /* Remove all variables from the process environment. */
  95. int clear_environment() {
  96. TCHAR *rawenv = GetEnvironmentStrings();
  97. TCHAR *env = useful_environment(rawenv);
  98. int ret = unset_environment_block(env);
  99. if (rawenv) FreeEnvironmentStrings(rawenv);
  100. return ret;
  101. }
  102. /* Set the current environment to exactly duplicate an environment block. */
  103. int duplicate_environment(TCHAR *rawenv) {
  104. int ret = clear_environment();
  105. TCHAR *env = useful_environment(rawenv);
  106. ret += set_environment_block(env);
  107. return ret;
  108. }
  109. /*
  110. Verify an environment block.
  111. Returns: 1 if environment is invalid.
  112. 0 if environment is OK.
  113. -1 on error.
  114. */
  115. int test_environment(TCHAR *env) {
  116. TCHAR *path = (TCHAR *) nssm_imagepath();
  117. STARTUPINFO si;
  118. ZeroMemory(&si, sizeof(si));
  119. si.cb = sizeof(si);
  120. PROCESS_INFORMATION pi;
  121. ZeroMemory(&pi, sizeof(pi));
  122. unsigned long flags = CREATE_SUSPENDED;
  123. #ifdef UNICODE
  124. flags |= CREATE_UNICODE_ENVIRONMENT;
  125. #endif
  126. /*
  127. Try to relaunch ourselves but with the candidate environment set.
  128. Assuming no solar flare activity, the only reason this would fail is if
  129. the environment were invalid.
  130. */
  131. if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
  132. TerminateProcess(pi.hProcess, 0);
  133. }
  134. else {
  135. unsigned long error = GetLastError();
  136. if (error == ERROR_INVALID_PARAMETER) return 1;
  137. else return -1;
  138. }
  139. return 0;
  140. }
  141. /*
  142. Duplicate an environment block returned by GetEnvironmentStrings().
  143. Since such a block is by definition readonly, and duplicate_environment()
  144. modifies its inputs, this function takes a copy of the input and operates
  145. on that.
  146. */
  147. void duplicate_environment_strings(TCHAR *env) {
  148. TCHAR *newenv = copy_environment_block(env);
  149. if (! newenv) return;
  150. duplicate_environment(newenv);
  151. HeapFree(GetProcessHeap(), 0, newenv);
  152. }
  153. /* Safely get a copy of the current environment. */
  154. TCHAR *copy_environment() {
  155. TCHAR *rawenv = GetEnvironmentStrings();
  156. if (! rawenv) return NULL;
  157. TCHAR *env = copy_environment_block(rawenv);
  158. FreeEnvironmentStrings(rawenv);
  159. return env;
  160. }
  161. /*
  162. Create a new block with all the strings of the first block plus a new string.
  163. If the key is already present its value will be overwritten in place.
  164. If the key is blank or empty the new block will still be allocated and have
  165. non-zero length.
  166. */
  167. int append_to_environment_block(TCHAR *env, unsigned long envlen, TCHAR *string, TCHAR **newenv, unsigned long *newlen) {
  168. size_t keylen = 0;
  169. if (string && string[0]) {
  170. for (; string[keylen]; keylen++) {
  171. if (string[keylen] == _T('=')) {
  172. keylen++;
  173. break;
  174. }
  175. }
  176. }
  177. return append_to_double_null(env, envlen, newenv, newlen, string, keylen, false);
  178. }
  179. /*
  180. Create a new block with all the strings of the first block minus the given
  181. string.
  182. If the key is not present the new block will be a copy of the original.
  183. If the string is KEY=VALUE the key will only be removed if its value is
  184. VALUE.
  185. If the string is just KEY the key will unconditionally be removed.
  186. If removing the string results in an empty list the new block will still be
  187. allocated and have non-zero length.
  188. */
  189. int remove_from_environment_block(TCHAR *env, unsigned long envlen, TCHAR *string, TCHAR **newenv, unsigned long *newlen) {
  190. if (! string || ! string[0] || string[0] == _T('=')) return 1;
  191. TCHAR *key = 0;
  192. size_t len = _tcslen(string);
  193. size_t i;
  194. for (i = 0; i < len; i++) if (string[i] == _T('=')) break;
  195. /* Rewrite KEY to KEY= but leave KEY=VALUE alone. */
  196. size_t keylen = len;
  197. if (i == len) keylen++;
  198. key = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (keylen + 1) * sizeof(TCHAR));
  199. if (! key) {
  200. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("key"), _T("remove_from_environment_block()"), 0);
  201. return 2;
  202. }
  203. memmove(key, string, len * sizeof(TCHAR));
  204. if (keylen > len) key[keylen - 1] = _T('=');
  205. key[keylen] = _T('\0');
  206. int ret = remove_from_double_null(env, envlen, newenv, newlen, key, keylen, false);
  207. HeapFree(GetProcessHeap(), 0, key);
  208. return ret;
  209. }