#include "nssm.h"\r
\r
-static char error_message[65535];\r
+#define NSSM_ERROR_BUFSIZE 65535\r
+unsigned long tls_index;\r
\r
/* Convert error code to error string - must call LocalFree() on return value */\r
char *error_string(unsigned long error) {\r
- if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &error_message, sizeof(error_message), 0)) {\r
- if (_snprintf(error_message, sizeof(error_message), "system error %lu", error) < 0) return 0;\r
+ /* Thread-safe buffer */\r
+ char *error_message = (char *) TlsGetValue(tls_index);\r
+ if (! error_message) {\r
+ error_message = (char *) LocalAlloc(LPTR, NSSM_ERROR_BUFSIZE);\r
+ if (! error_message) return "<out of memory for error message>";\r
+ TlsSetValue(tls_index, (void *) error_message);\r
+ }\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
}\r
return error_message;\r
}\r
#include "nssm.h"\r
\r
+extern unsigned long tls_index;\r
+\r
/* String function */\r
int str_equiv(const char *a, const char *b) {\r
int i;\r
/* Undocumented: "run" is used to actually do service stuff */\r
if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));\r
\r
+ /* Thread local storage for error message buffer */\r
+ tls_index = TlsAlloc();\r
+\r
/* Register messages */\r
create_messages();\r
\r