#include "common.h"
#ifndef DACCESS_COMPILE
#include "CommonTypes.h"
-#include "daccess.h"
#include "CommonMacros.h"
#include "PalRedhawkCommon.h"
#include "PalRedhawk.h"
-#include "rhassert.h"
-#include "slist.h"
-#include "gcrhinterface.h"
-#include "varint.h"
-#include "regdisplay.h"
-#include "StackFrameIterator.h"
-#include "thread.h"
#include "holder.h"
-#include "Crst.h"
-#include "event.h"
-#include "threadstore.h"
-#include "RuntimeInstance.h"
-#include "shash.h"
#include "RhConfig.h"
#include <string.h>
-bool RhConfig::ReadConfigValue(_In_z_ const TCHAR *wszName, uint64_t* pValue, bool decimal)
+bool RhConfig::ReadConfigValue(_In_z_ const char *name, uint64_t* pValue, bool decimal)
{
- TCHAR wszBuffer[CONFIG_VAL_MAXLEN + 1]; // hex digits plus a nul terminator.
- const uint32_t cchBuffer = sizeof(wszBuffer) / sizeof(wszBuffer[0]);
+ TCHAR buffer[CONFIG_VAL_MAXLEN + 1]; // hex digits plus a nul terminator.
+ const uint32_t cchBuffer = ARRAY_SIZE(buffer);
uint32_t cchResult = 0;
-
-#ifdef FEATURE_ENVIRONMENT_VARIABLE_CONFIG
- TCHAR wszVariableName[64] = _T("DOTNET_");
- assert(_tcslen(wszVariableName) + _tcslen(wszName) < sizeof(wszVariableName) / sizeof(wszVariableName[0]));
- _tcscat(wszVariableName, wszName);
- cchResult = PalGetEnvironmentVariable(wszVariableName, wszBuffer, cchBuffer);
-#endif // FEATURE_ENVIRONMENT_VARIABLE_CONFIG
-
-#ifdef FEATURE_EMBEDDED_CONFIG
- // if the config key wasn't found in the ini file
- if ((cchResult == 0) || (cchResult >= cchBuffer))
- cchResult = GetEmbeddedVariable(wszName, wszBuffer, cchBuffer);
-#endif // FEATURE_EMBEDDED_CONFIG
-
- if ((cchResult == 0) || (cchResult >= cchBuffer))
- return false; // not found
-
- uint64_t uiResult = 0;
-
- for (uint32_t i = 0; i < cchResult; i++)
+ TCHAR variableName[64] = _T("DOTNET_");
+ assert(ARRAY_SIZE("DOTNET_") - 1 + strlen(name) < ARRAY_SIZE(variableName));
+#ifdef TARGET_WINDOWS
+ for (size_t i = 0; i < strlen(name); i++)
{
- TCHAR ch = wszBuffer[i];
+ variableName[ARRAY_SIZE("DOTNET_") - 1 + i] = name[i];
+ }
+#else
+ strcat(variableName, name);
+#endif
- if (decimal)
+ cchResult = PalGetEnvironmentVariable(variableName, buffer, cchBuffer);
+ if (cchResult != 0 && cchResult < cchBuffer)
+ {
+ // Environment variable was set. Convert it to an integer.
+ uint64_t uiResult = 0;
+ for (uint32_t i = 0; i < cchResult; i++)
{
- uiResult *= 10;
+ TCHAR ch = buffer[i];
- if ((ch >= _T('0')) && (ch <= _T('9')))
- uiResult += ch - _T('0');
- else
- return false; // parse error
- }
- else
- {
- uiResult *= 16;
-
- if ((ch >= _T('0')) && (ch <= _T('9')))
- uiResult += ch - _T('0');
- else if ((ch >= _T('a')) && (ch <= _T('f')))
- uiResult += (ch - _T('a')) + 10;
- else if ((ch >= _T('A')) && (ch <= _T('F')))
- uiResult += (ch - _T('A')) + 10;
+ if (decimal)
+ {
+ uiResult *= 10;
+
+ if ((ch >= '0') && (ch <= '9'))
+ uiResult += ch - '0';
+ else
+ return false; // parse error
+ }
else
- return false; // parse error
+ {
+ uiResult *= 16;
+
+ if ((ch >= '0') && (ch <= '9'))
+ uiResult += ch - '0';
+ else if ((ch >= 'a') && (ch <= 'f'))
+ uiResult += (ch - 'a') + 10;
+ else if ((ch >= 'A') && (ch <= 'F'))
+ uiResult += (ch - 'A') + 10;
+ else
+ return false; // parse error
+ }
}
- }
- *pValue = uiResult;
- return true;
-}
+ *pValue = uiResult;
+ return true;
+ }
-#ifdef FEATURE_EMBEDDED_CONFIG
-uint32_t RhConfig::GetEmbeddedVariable(_In_z_ const TCHAR* configName, _Out_writes_all_(cchOutputBuffer) TCHAR* outputBuffer, _In_ uint32_t cchOutputBuffer)
-{
- //the buffer needs to be big enough to read the value buffer + null terminator
- if (cchOutputBuffer < CONFIG_VAL_MAXLEN + 1)
+ // Check the embedded configuration
+ const char *embeddedValue = nullptr;
+ if (GetEmbeddedVariable(name, &embeddedValue))
{
- return 0;
+ *pValue = strtoull(embeddedValue, NULL, decimal ? 10 : 16);
+ return true;
}
- //if we haven't read the config yet try to read
+ return false;
+}
+
+bool RhConfig::GetEmbeddedVariable(_In_z_ const char* configName, _Out_ const char** configValue)
+{
+ // Read the config if we haven't yet
if (g_embeddedSettings == NULL)
{
ReadEmbeddedSettings();
}
- //if the config wasn't read or reading failed return 0 immediately
+ // Config wasn't read or reading failed
if (g_embeddedSettings == CONFIG_INI_NOT_AVAIL)
{
- return 0;
+ return false;
}
- return GetConfigVariable(configName, (ConfigPair*)g_embeddedSettings, outputBuffer, cchOutputBuffer);
-}
-#endif // FEATURE_EMBEDDED_CONFIG
+ const ConfigPair* configPairs = (const ConfigPair*)g_embeddedSettings;
-uint32_t RhConfig::GetConfigVariable(_In_z_ const TCHAR* configName, const ConfigPair* configPairs, _Out_writes_all_(cchOutputBuffer) TCHAR* outputBuffer, _In_ uint32_t cchOutputBuffer)
-{
- //find the first name which matches (case insensitive to be compat with environment variable counterpart)
+ // Find the first name which matches (case insensitive to be compat with environment variable counterpart)
for (int iSettings = 0; iSettings < RCV_Count; iSettings++)
{
- if (_tcsicmp(configName, configPairs[iSettings].Key) == 0)
+ if (_stricmp(configName, configPairs[iSettings].Key) == 0)
{
- bool nullTerm = FALSE;
-
- uint32_t iValue;
-
- for (iValue = 0; (iValue < CONFIG_VAL_MAXLEN + 1) && (iValue < cchOutputBuffer); iValue++)
- {
- outputBuffer[iValue] = configPairs[iSettings].Value[iValue];
-
- if (outputBuffer[iValue] == '\0')
- {
- nullTerm = true;
- break;
- }
- }
-
- //return the length of the config value if null terminated else return zero
- return nullTerm ? iValue : 0;
+ *configValue = configPairs[iSettings].Value;
+ return true;
}
}
- //if the config key was not found return 0
- return 0;
+ // Config key was not found
+ return false;
}
-#ifdef FEATURE_EMBEDDED_CONFIG
struct CompilerEmbeddedSettingsBlob
{
uint32_t Size;
return;
}
-#endif // FEATURE_EMBEDDED_CONFIG
//Parses one line of config and populates values in the passed in configPair
//returns: true if the parsing was successful, false if the parsing failed.
#ifndef DACCESS_COMPILE
-#define FEATURE_EMBEDDED_CONFIG
-#define FEATURE_ENVIRONMENT_VARIABLE_CONFIG
-
class RhConfig
{
struct ConfigPair
{
public:
- TCHAR Key[CONFIG_KEY_MAXLEN + 1]; //maxlen + null terminator
- TCHAR Value[CONFIG_VAL_MAXLEN + 1]; //maxlen + null terminator
+ char Key[CONFIG_KEY_MAXLEN + 1]; //maxlen + null terminator
+ char Value[CONFIG_VAL_MAXLEN + 1]; //maxlen + null terminator
};
-#ifdef FEATURE_EMBEDDED_CONFIG
// g_embeddedSettings is a buffer of ConfigPair structs embedded in the compiled binary.
//
//NOTE: g_embeddedSettings is only set in ReadEmbeddedSettings and must be set atomically only once
// using PalInterlockedCompareExchangePointer to avoid races when initializing
void* volatile g_embeddedSettings = NULL;
-#endif // FEATURE_EMBEDDED_CONFIG
public:
- bool ReadConfigValue(_In_z_ const TCHAR* wszName, uint64_t* pValue, bool decimal = false);
+ bool ReadConfigValue(_In_z_ const char* wszName, uint64_t* pValue, bool decimal = false);
#define DEFINE_VALUE_ACCESSOR(_name, defaultVal) \
uint64_t Get##_name() \
if (m_uiConfigValuesRead & (1 << RCV_##_name)) \
return m_uiConfigValues[RCV_##_name]; \
uint64_t uiValue; \
- m_uiConfigValues[RCV_##_name] = ReadConfigValue(_T(#_name), &uiValue) ? uiValue : defaultVal; \
+ m_uiConfigValues[RCV_##_name] = ReadConfigValue(#_name, &uiValue) ? uiValue : defaultVal; \
m_uiConfigValuesRead |= 1 << RCV_##_name; \
return m_uiConfigValues[RCV_##_name]; \
}
//NOTE: if the method fails configPair is left in an uninitialized state
bool ParseConfigLine(_Out_ ConfigPair* configPair, _In_z_ const char * line);
-#ifdef FEATURE_EMBEDDED_CONFIG
void ReadEmbeddedSettings();
- uint32_t GetEmbeddedVariable(_In_z_ const TCHAR* configName, _Out_writes_all_(cchOutputBuffer) TCHAR* outputBuffer, _In_ uint32_t cchOutputBuffer);
-#endif // FEATURE_EMBEDDED_CONFIG
-
- uint32_t GetConfigVariable(_In_z_ const TCHAR* configName, const ConfigPair* configPairs, _Out_writes_all_(cchOutputBuffer) TCHAR* outputBuffer, _In_ uint32_t cchOutputBuffer);
-
- static bool priv_isspace(char c)
- {
- return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
- }
-
+ // Gets a pointer to the embedded configuration value. Memory is held by the callee.
+ // Returns true if the variable was found, false otherwise
+ bool GetEmbeddedVariable(_In_z_ const char* configName, _Out_ const char** configValue);
uint32_t m_uiConfigValuesRead;
uint64_t m_uiConfigValues[RCV_Count];