Esempio n. 1
0
int EsifLogFile_Open(EsifLogType type, const char *filename, int append)
{
	int rc=0;
	char fullpath[MAX_PATH]={0};
	char mode[3] = {(append ? 'a' : 'w'), 0, 0};

	esif_ccb_write_lock(&g_EsifLogFile[type].lock);
	if (g_EsifLogFile[type].handle != NULL)
		esif_ccb_fclose(g_EsifLogFile[type].handle);

	EsifLogFile_GetFullPath(fullpath, sizeof(fullpath), filename);
#ifdef ESIF_ATTR_OS_WINDOWS
	mode[1] = 'c';
	g_EsifLogFile[type].handle = _fsopen(fullpath, mode, _SH_DENYWR);
	if (g_EsifLogFile[type].handle == NULL)
		rc = errno;
#else
	rc = esif_ccb_fopen(&g_EsifLogFile[type].handle, fullpath, mode);
#endif
	if (rc == 0) {
		esif_ccb_free(g_EsifLogFile[type].filename);
		g_EsifLogFile[type].filename = esif_ccb_strdup((char *)fullpath);
	}
	esif_ccb_write_unlock(&g_EsifLogFile[type].lock);
	return rc;
}
Esempio n. 2
0
// Find Next path in nameSpace
eEsifError EsifConfigFindNext (
	EsifDataPtr nameSpace,
	EsifDataPtr path,
	EsifDataPtr value,
	UInt32 *context
	)
{
	eEsifError rc   = ESIF_E_NOT_FOUND;
	DataVaultPtr DB = 0;
	ASSERT(nameSpace && path && value && context);
	DB = DataBank_GetNameSpace(g_DataBankMgr, (StringPtr)(nameSpace->buf_ptr));
	if (DB) {
		UInt32 item = *context;
		if (item >= DB->cache->size) {
			return rc;
		}
		(*context)++;

		if (path->buf_len && path->buf_len < DB->cache->elements[item].value.data_len) {
			return ESIF_E_NEED_LARGER_BUFFER;
		}
		EsifData_Set(path, ESIF_DATA_STRING, esif_ccb_strdup(
						 (char*)DB->cache->elements[item].key.buf_ptr), DB->cache->elements[item].key.data_len, DB->cache->elements[item].key.data_len);
		return EsifConfigGet(nameSpace, path, value);
	}
	return rc;
}
Esempio n. 3
0
eEsifError EsifLogMgrInit(void)
{
	int j;

	ESIF_TRACE_ENTRY_INFO();

	esif_ccb_memset(g_EsifLogFile, 0, sizeof(g_EsifLogFile));
	for (j=0; j < MAX_ESIFLOG; j++) {
		esif_ccb_lock_init(&g_EsifLogFile[j].lock);
	}
	g_EsifLogFile[ESIF_LOG_EVENTLOG].name = esif_ccb_strdup("event");
	g_EsifLogFile[ESIF_LOG_DEBUGGER].name = esif_ccb_strdup("debug");
	g_EsifLogFile[ESIF_LOG_SHELL].name    = esif_ccb_strdup("shell");
	g_EsifLogFile[ESIF_LOG_TRACE].name    = esif_ccb_strdup("trace");
	g_EsifLogFile[ESIF_LOG_UI].name       = esif_ccb_strdup("ui");

	ESIF_TRACE_EXIT_INFO();
	return ESIF_OK;
}
Esempio n. 4
0
// Read a Registry Key Value into a specified memory buffer
static eEsifError DataVault_ReadRegistry (
	DataVaultPtr self,
	esif_string keyname,
	void * *buffer,
	UInt32 *buf_size
	)
{
	eEsifError rc   = ESIF_E_UNSPECIFIED;
	HKEY hKey       = 0;
	DWORD dwFlags   = RRF_RT_ANY;
	DWORD dwError   = 0;
	DWORD dwSize    = 0;
	char *regkey    = 0;
	char *valuename = 0;
	u32 j;

	UNREFERENCED_PARAMETER(self);

	// Format: HKLM\Subkey\Path\ValueName
	for (j = 0; HKeyList[j].name; j++) {
		size_t namelen = esif_ccb_strlen(HKeyList[j].name, 30);
		if (esif_ccb_strnicmp(HKeyList[j].name, keyname, namelen) == 0 && keyname[namelen] == '\\') {
			hKey     = HKeyList[j].hkey;
			keyname += esif_ccb_strlen(HKeyList[j].name, 30) + 1;
			break;
		}
	}
	if (!hKey) {
		return rc;
	}

	regkey = esif_ccb_strdup(keyname);
	if ((valuename = strrchr(regkey, '\\')) == 0) {
		esif_ccb_free(regkey);
		return rc;
	}
	*valuename++ = 0;

	// Get Data Size from Registry and copy into buffer if found
	if ((dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, NULL, 0, &dwSize)) == 0) {
		void *reg_buf = esif_ccb_malloc(dwSize);
		dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, NULL, reg_buf, &dwSize);
		if (dwError == 0) {
			*buffer   = reg_buf;
			*buf_size = dwSize;
			rc = ESIF_OK;
		} else {
			esif_ccb_free(reg_buf);
		}
	} else {
		rc = ESIF_E_NOT_FOUND;
	}
	esif_ccb_free(regkey);
	return rc;
}
Esempio n. 5
0
// Set a Pathname
enum esif_rc  esif_pathlist_set(esif_pathtype type, esif_string value)
{
	enum esif_rc rc = ESIF_E_PARAMETER_IS_NULL;
	if (value != NULL && g_pathlist.pathlist != NULL && type < g_pathlist.num_paths) {
		esif_string pathvalue = esif_ccb_strdup(value);
		if (pathvalue == NULL) {
			rc = ESIF_E_NO_MEMORY;
		}
		else {
			esif_ccb_free(g_pathlist.pathlist[type]);
			g_pathlist.pathlist[type] = pathvalue;
			rc = ESIF_OK;
		}
	}
	return rc;
}
Esempio n. 6
0
static eEsifError EsifActMgr_CreatePossActList_Locked()
{
	eEsifError rc = ESIF_OK;
	struct esif_ccb_file curFile = {0};
	esif_ccb_file_enum_t fileIter = {0};
	char libPath[ESIF_LIBPATH_LEN];
	char *dotPtr = NULL;
	EsifActMgrEntryPtr newPossPtr = NULL;
	EsifString filePattern = ESIF_UPE_FILE_PREFIX "*" ESIF_LIB_EXT;

	g_actMgr.possibleActions = esif_link_list_create();
	if (NULL == g_actMgr.possibleActions) {
		rc = ESIF_E_NO_MEMORY;
		goto exit;
	}

	/* Get the loadable action directory path */
	esif_build_path(libPath, sizeof(libPath), ESIF_PATHTYPE_DLL, NULL, NULL);

	fileIter = esif_ccb_file_enum_first(libPath, filePattern, &curFile);
	if (INVALID_HANDLE_VALUE == fileIter) {
		goto exit;
	}

	do {
		newPossPtr = esif_ccb_malloc(sizeof(*newPossPtr));
		if(NULL == newPossPtr) {
			break;
		}
		dotPtr = esif_ccb_strchr(curFile.filename, '.');
		if (dotPtr != NULL) {
			*dotPtr = '\0';
			newPossPtr->libName = (esif_string)esif_ccb_strdup(curFile.filename);

			esif_link_list_add_at_back(g_actMgr.possibleActions, (void *)newPossPtr);
		}
	} while (esif_ccb_file_enum_next(fileIter, filePattern, &curFile));
	esif_ccb_file_enum_close(fileIter);
exit:
	return rc;
}
Esempio n. 7
0
static eEsifError EsifSetActionDelegateRset(
	const EsifUpDomainPtr domainPtr,
	const EsifDataPtr requestPtr)
{
	eEsifError rc = ESIF_E_PRIMITIVE_ACTION_FAILURE;
	EsifPrimitiveTupleParameter parameters = { 0 };
	EsifPrimitiveTuple tuple = { 0 };
	Bool signal_event = ESIF_FALSE;
	char domain_str[8] = { 0 };
	int j = 0;

	ESIF_ASSERT(domainPtr != NULL);
	ESIF_ASSERT(requestPtr != NULL);
	
	if (requestPtr->buf_ptr == NULL) {
		rc = ESIF_E_PARAMETER_IS_NULL;
		goto exit;
	}
	if (requestPtr->data_len != sizeof(parameters)) {
		rc = ESIF_E_REQUEST_DATA_OUT_OF_BOUNDS;
		goto exit;
	}
	
	// Convert BINARY Parameters to Primitive Tuple
	esif_ccb_memcpy(&parameters, requestPtr->buf_ptr, sizeof(parameters));
	
	ESIF_TRACE_DEBUG("CONFIG RESET: { %s (%hd), %s, %hd }\n",
		esif_primitive_str(parameters.id.integer.value),
		(u16)parameters.id.integer.value,
		esif_primitive_domain_str((u16)parameters.domain.integer.value, domain_str, sizeof(domain_str)),
		(u16)parameters.instance.integer.value
		);

	// Look up Primitive Tuple in the DSP and verify it is a valid SET primtive
	EsifDspPtr dspPtr = EsifUp_GetDsp(domainPtr->upPtr);
	if (dspPtr == NULL) {
		rc = ESIF_E_NEED_DSP;
		goto exit;
	}
	tuple.id = (u16) parameters.id.integer.value;
	tuple.domain = (u16) parameters.domain.integer.value;
	tuple.instance = (u16) parameters.instance.integer.value;
	EsifFpcPrimitivePtr primitivePtr = dspPtr->get_primitive(dspPtr, &tuple);
	if (primitivePtr == NULL) {
		rc = ESIF_E_PRIMITIVE_NOT_FOUND_IN_DSP;
		goto exit;
	}
	if (primitivePtr->operation != ESIF_PRIMITIVE_OP_SET) {
		rc = ESIF_E_INVALID_REQUEST_TYPE;
		goto exit;
	}

	// Find first CONFIG Action and Delete its Key from its DataVault
	for (j = 0; j < (int)primitivePtr->num_actions; j++) {
		EsifFpcActionPtr fpcActionPtr = dspPtr->get_action(dspPtr, primitivePtr, (u8)j);
		DataItemPtr paramDataVault = EsifFpcAction_GetParam(fpcActionPtr, (const UInt8)0);
		DataItemPtr paramKeyName = EsifFpcAction_GetParam(fpcActionPtr, (const UInt8)1);
		EsifString expandedKeyName = NULL;
		if (fpcActionPtr->type != ESIF_ACTION_CONFIG) {
			continue;
		}
		if (paramDataVault == NULL || paramKeyName == NULL || paramDataVault->data_type != ESIF_DSP_PARAMETER_TYPE_STRING || paramKeyName->data_type != ESIF_DSP_PARAMETER_TYPE_STRING) {
			rc = ESIF_E_PARAMETER_IS_OUT_OF_BOUNDS;
			goto exit;
		}
		
		// Replace "%nm%" tokens in the key name or make a copy of the key name for static keys
		expandedKeyName = EsifUp_CreateTokenReplacedParamString(domainPtr->upPtr, primitivePtr, (StringPtr)paramKeyName->data);
		if (expandedKeyName == NULL) {
			expandedKeyName = esif_ccb_strdup((StringPtr)paramKeyName->data);
			if (expandedKeyName == NULL) {
				rc = ESIF_E_NO_MEMORY;
				goto exit;
			}
		}

		// Valid SET CONFIG Primitive found with valid DV/Key Name; Delete the associated Key from the DataVault
		EsifDataPtr data_nspace = EsifData_CreateAs(ESIF_DATA_STRING, (StringPtr)paramDataVault->data, 0, ESIFAUTOLEN);
		EsifDataPtr data_key    = EsifData_CreateAs(ESIF_DATA_STRING, expandedKeyName, 0, ESIFAUTOLEN);

		// Do not signal an Event if Key does not exist in DataVault
		if (DataBank_KeyExists(g_DataBankMgr, (StringPtr)paramDataVault->data, expandedKeyName) == ESIF_FALSE) {
			rc = ESIF_OK;
		}
		else if (data_nspace == NULL || data_key == NULL) {
			rc = ESIF_E_NO_MEMORY;
		}
		else {
			// Delete Existing Key from DataVault
			rc = EsifConfigDelete(data_nspace, data_key);
			if (rc == ESIF_OK) {
				signal_event = ESIF_TRUE;
			}

			ESIF_TRACE_DEBUG("CONFIG RESET: config delete @%s %s [rc=%s (%d)]\n",
				(StringPtr)data_nspace->buf_ptr,
				(StringPtr)data_key->buf_ptr,
				esif_rc_str(rc),
				rc
				);
		}

		// Signal any Event(s) associated with this SET Primitive
		if (signal_event) {
			EsifActConfigSignalChangeEvents(domainPtr->upPtr, tuple, NULL);
		}

		EsifData_Destroy(data_nspace);
		EsifData_Destroy(data_key);
		esif_ccb_free(expandedKeyName);
		break;
	}
	if (j >= (int)primitivePtr->num_actions) {
		rc = ESIF_E_UNSUPPORTED_ACTION_TYPE;
	}

exit:
	return rc;
}
Esempio n. 8
0
void esif_ws_server_execute_rest_cmd(
	const char *dataPtr,
	const size_t dataSize
	)
{
	char *command_buf = NULL;

	if (atomic_read(&g_ws_quit))
		return;

	command_buf = strchr(dataPtr, ':');
	if (NULL != command_buf) {
		u32 msg_id = atoi(dataPtr);
		*command_buf = 0;
		command_buf++;

		// Ad-Hoc UI Shell commands begin with "0:", so verify ESIF shell is enabled and command is valid
		if (msg_id == 0 || g_ws_restricted) {
			char *response = NULL;
			if (msg_id == 0 && !g_shell_enabled) {
				response = "Shell Disabled";
			}
			else {
				static char *whitelist[] = { "status", "participants", NULL };
				static char *blacklist[] = { "shell", "web", "exit", "quit", NULL };
				Bool blocked = ESIF_FALSE;
				int j = 0;
				if (g_ws_restricted) {
					for (j = 0; whitelist[j] != NULL; j++) {
						if (esif_ccb_strnicmp(command_buf, whitelist[j], esif_ccb_strlen(whitelist[j], MAX_PATH)) == 0) {
							break;
						}
					}
					if (whitelist[j] == NULL) {
						blocked = ESIF_TRUE;
					}
				}
				else {
					for (j = 0; blacklist[j] != NULL; j++) {
						if (esif_ccb_strnicmp(command_buf, blacklist[j], esif_ccb_strlen(blacklist[j], MAX_PATH)) == 0) {
							blocked = ESIF_TRUE;
							break;
						}
					}
				}
				if (blocked) {
					response = "Unsupported Command";
				}
			}
			// Exit if shell or command unavailable
			if (response) {
				char buffer[MAX_PATH] = { 0 };
				esif_ccb_sprintf(sizeof(buffer), buffer, "%d:%s", msg_id, response);
				esif_ccb_free(g_rest_out);
				g_rest_out = esif_ccb_strdup(buffer);
				goto exit;
			}
		}

		// Lock Shell so we can capture output before another thread executes another command
#ifdef ESIF_ATTR_SHELL_LOCK
		esif_ccb_mutex_lock(&g_shellLock);
#endif
		if (!atomic_read(&g_ws_quit)) {
			EsifString cmd_results = esif_shell_exec_command(command_buf, dataSize, ESIF_TRUE);
			if (NULL != cmd_results) {
				size_t out_len = esif_ccb_strlen(cmd_results, OUT_BUF_LEN) + 12;
				esif_ccb_free(g_rest_out);
				g_rest_out = (EsifString) esif_ccb_malloc(out_len);
				if (g_rest_out) {
					esif_ccb_sprintf(out_len, g_rest_out, "%u:%s", msg_id, cmd_results);
				}
			}
		}
#ifdef ESIF_ATTR_SHELL_LOCK
		esif_ccb_mutex_unlock(&g_shellLock);
#endif
	}

exit:
	return;
}
Esempio n. 9
0
/* Loads a pluggable UPE action by library name */
eEsifError EsifActMgr_StartUpe(
	EsifString upeName,
	UInt8 upInstance
	)
{
	eEsifError rc = ESIF_OK;
	EsifActMgrEntryPtr entryPtr = NULL;
	GetIfaceFuncPtr getIfacePtr = NULL;
	EsifActIface iface = {0};

	if (NULL == upeName) {
		rc = ESIF_E_PARAMETER_IS_NULL;
		goto exit;
	}

	/* Check to see if the action is already running only one instance per action allowed */
	esif_ccb_write_lock(&g_actMgr.mgrLock);
	entryPtr = EsifActMgr_GetActEntryByLibname_Locked(upeName);
	esif_ccb_write_unlock(&g_actMgr.mgrLock);
	if (entryPtr != NULL) {
		entryPtr = NULL; /* Keep from being destroy on exit for this failure type */
		rc = ESIF_E_ACTION_ALREADY_STARTED;
		goto exit;
	}

	ESIF_TRACE_DEBUG("Adding new UPE action %s\n", upeName);

	rc = EsifActMgr_CreateEntry(&entryPtr);
	if (rc != ESIF_OK) {
		goto exit;
	}
	entryPtr->upInstance = upInstance;
	entryPtr->libName = (esif_string)esif_ccb_strdup(upeName);

	rc = EsifActMgr_LoadAction(entryPtr, &getIfacePtr);
	if (ESIF_OK != rc) {
		goto exit;
	}

	iface.hdr.fIfaceType = eIfaceTypeAction;
	iface.hdr.fIfaceVersion = ESIF_INTERFACE_VERSION;
	iface.hdr.fIfaceSize = sizeof(iface);

	rc = getIfacePtr(&iface);
	if (ESIF_OK != rc) {
		goto exit;
	}

	rc = EsifActMgr_CreateAction(entryPtr, &iface);
	if (ESIF_OK != rc) {
		goto exit;
	}

	EsifAct_MarkAsPlugin(entryPtr->actPtr);

	rc = EsifActMgr_AddEntry(entryPtr);
	if (rc != ESIF_OK) {
		goto exit;
	}

	ESIF_TRACE_DEBUG("Added action %s\n", upeName);
exit:
	if (rc != ESIF_OK) {
		ESIF_TRACE_ERROR("Failure adding action %s\n", esif_rc_str(rc));
		EsifActMgr_DestroyEntry(entryPtr);
	}
	return rc;
}
Esempio n. 10
0
//
// PUBLIC INTERFACE---------------------------------------------------------------------
//
char *EsifShellCmdDataLog(EsifShellCmdPtr shell)
{
	eEsifError rc = ESIF_OK;
	int argc = shell->argc;
	char **argv = shell->argv;
	char *output = shell->outbuf;
	char participantList[MAX_LOG_LINE] = { 0 };

	/*
	 * Serialize access to the datalog state.
	 */
	while (atomic_set(&g_dataLogLock, ESIF_TRUE)) {
		esif_ccb_sleep_msec(ESIF_DATALOG_LOCK_SLEEP_MS);
	}

	if (argc < 2) {
		esif_ccb_sprintf(OUT_BUF_LEN, output, "Data logging is: %s\n", (EsifDataIsLogStarted() ? "started" : "stopped"));
	}
	else if (esif_ccb_stricmp(argv[1], "start") == 0) {

		if (EsifDataIsLogStarted()){
			esif_ccb_sprintf(OUT_BUF_LEN, output, "Data logging is already started.\n");
			goto exit;
		}

		g_dataLogInterval = DEFAULT_STATUS_LOG_INTERVAL;
		if (argc > 2) {
			if ((int)esif_atoi(argv[2]) >= MIN_STATUS_LOG_INTERVAL) {
				g_dataLogInterval = (esif_ccb_time_t)esif_atoi(argv[2]);
			}
			else {
				esif_ccb_sprintf(OUT_BUF_LEN, output, "Invalid sampling period specified (minimum is %d ms).\n", MIN_STATUS_LOG_INTERVAL);
				goto exit;
			}
		}
		if (argc > 3) {
			esif_ccb_sprintf(sizeof(participantList), participantList, "%s", argv[3]);
		}

		rc = EsifDataLogValidateParticipantList(participantList);
		if (rc != ESIF_OK) {
			esif_ccb_sprintf(OUT_BUF_LEN, output, "There was a problem with your participant list. You may have selected invalid participants, or too many participants. \n");
			goto exit;
		}
		rc = EsifDataLogOpenLogFile();
		if (rc != ESIF_OK) {
			esif_ccb_sprintf(OUT_BUF_LEN, output, "Error opening log file... \n");
			goto exit;
		}
		esif_ccb_sprintf(OUT_BUF_LEN, output, "Data logging starting... \n");

		EsifDataLogStart();
	}
	else if (esif_ccb_stricmp(argv[1], "schedule") == 0) {
		UInt32 startTime = 1000;

		/* initialize */
		if (dataLogContextPtr == NULL) {
			dataLogContextPtr = (struct dataLogContext *)esif_ccb_malloc(sizeof(struct dataLogContext));
			if (dataLogContextPtr == NULL) {
				rc = ESIF_E_NO_MEMORY;
				goto exit;
			}
			dataLogContextPtr->dataLogScheduleTimer = (esif_ccb_timer_t *) esif_ccb_malloc(sizeof(esif_ccb_timer_t));
			if (dataLogContextPtr->dataLogScheduleTimer == NULL) {
				rc = ESIF_E_NO_MEMORY;
				goto exit;
			}
			dataLogContextPtr->dataLogParticipantList = NULL;
		}

		dataLogContextPtr->dataLogInterval = DEFAULT_STATUS_LOG_INTERVAL;
		/* start time (in ms from now) */
		if (argc > 2) {
			if ((int) esif_atoi(argv[2]) >= MIN_STATUS_LOG_SCHEDULE) {
				startTime = (esif_ccb_time_t) esif_atoi(argv[2]);
			}
			else {
				esif_ccb_sprintf(OUT_BUF_LEN, output, "Invalid schedule time specified (minimum is %d ms).\n", MIN_STATUS_LOG_INTERVAL);
				goto exit;
			}
		}
		/* log interval */
		if (argc > 3) {
			if ((int) esif_atoi(argv[3]) >= MIN_STATUS_LOG_INTERVAL) {
				dataLogContextPtr->dataLogInterval = (esif_ccb_time_t) esif_atoi(argv[3]);
			}
			else {
				esif_ccb_sprintf(OUT_BUF_LEN, output, "Invalid sampling period specified (minimum is %d ms).\n", MIN_STATUS_LOG_INTERVAL);
				goto exit;
			}
		}
		if (argc > 4) {
			dataLogContextPtr->dataLogParticipantList = esif_ccb_strdup(argv[4]);
			if (dataLogContextPtr->dataLogParticipantList == NULL) {
				rc = ESIF_E_NO_MEMORY;
				goto exit;
			}
		}

		rc = esif_ccb_timer_init(dataLogContextPtr->dataLogScheduleTimer, (esif_ccb_timer_cb) EsifDataLogSchedule, NULL);
		if (ESIF_OK != rc) {
			esif_ccb_sprintf(OUT_BUF_LEN, output, "Error starting timer... \n");
			goto exit;
		}
		rc = esif_ccb_timer_set_msec(dataLogContextPtr->dataLogScheduleTimer, startTime);
		esif_ccb_sprintf(OUT_BUF_LEN, output, "Data logging scheduled for start in %d ms at an interval of %d... \n", startTime, dataLogContextPtr->dataLogInterval);
		
	}
	else if (esif_ccb_stricmp(argv[1], "stop") == 0) {
		EsifDataLogStop();
		esif_ccb_sprintf(OUT_BUF_LEN, output, "Data logging stopped...\n");
		g_dataLogActive = ESIF_FALSE;
	}
	else {
		esif_ccb_sprintf(OUT_BUF_LEN, output, "Invalid parameter specified\n");
	}
exit:
	atomic_set(&g_dataLogLock, ESIF_FALSE);
	return output;
}
Esempio n. 11
0
/* Copy/Merge Keys from one NameSpace to Another */
eEsifError EsifConfigCopy(
	EsifDataPtr nameSpaceFrom,	// Source DV
	EsifDataPtr nameSpaceTo,	// Target DV
	EsifDataPtr keyspecs,		// Tab-separated Keyspec List (wildcards OK)
	esif_flags_t flags,			// Item Flags
	Bool replaceKeys,			// TRUE=COPY Keys (Replace if exists), FALSE=MERGE Keys (Do Not Replace)
	UInt32 *keycount)			// Optional pointer to variable to hold Key Count copied/merged
{
	eEsifError rc = ESIF_OK;
	EsifConfigFindContext context = NULL;
	EsifDataPtr data_key = NULL;
	EsifDataPtr data_value = NULL;
	esif_string keylist = NULL;
	esif_string keyspec = NULL;
	esif_string keyspec_context = NULL;
	char **keyset = NULL;
	size_t keyset_count = 0;
	UInt32 exported = 0;
	esif_context_t qsort_ctx = 0;
	size_t key = 0;

	ESIF_ASSERT(nameSpaceFrom && nameSpaceTo && keyspecs && nameSpaceFrom->buf_ptr && nameSpaceTo->buf_ptr && keyspecs->buf_ptr);

	// Parse Key List (optionally Tab-separated)
	keylist = esif_ccb_strdup((esif_string)keyspecs->buf_ptr);
	if (keylist == NULL) {
		rc = ESIF_E_NO_MEMORY;
		goto exit;
	}

	// Create sorted keyset with exclude keyspecs ("!keyspec") listed first
	keyspec = esif_ccb_strtok(keylist, "\t", &keyspec_context);
	while (keyspec != NULL) {
		char **new_keyset = (char **)esif_ccb_realloc(keyset, sizeof(char *) * (keyset_count + 1));
		if (new_keyset == NULL) {
			rc = ESIF_E_NO_MEMORY;
			goto exit;
		}
		keyset = new_keyset;
		keyset[keyset_count++] = keyspec;
		keyspec = esif_ccb_strtok(NULL, "\t", &keyspec_context);
	}
	esif_ccb_qsort(keyset, keyset_count, sizeof(char *), esif_ccb_qsort_stricmp, qsort_ctx);

	// Enumerate Each Matching keyspec
	for (key = 0; (rc == ESIF_OK && key < keyset_count); key++) {

		// Skip excludes for now so we can compare to each maching keyspec later
		if (keyset[key][0] == '!') {
			continue;
		}

		EsifData_Destroy(data_key);
		data_key = EsifData_CreateAs(ESIF_DATA_STRING, keyset[key], 0, ESIFAUTOLEN);
		if (data_key == NULL) {
			rc = ESIF_E_NO_MEMORY;
			goto exit;
		}
		if ((rc = EsifConfigFindFirst(nameSpaceFrom, data_key, NULL, &context)) == ESIF_OK) {
			do {
				// Skip if matching key matches any exclude keyspecs
				Bool skip_key = ESIF_FALSE;
				size_t ex = 0;
				for (ex = 0; (ex < key && keyset[ex][0] == '!'); ex++) {
					if (esif_ccb_strmatch((esif_string)data_key->buf_ptr, &keyset[ex][1])) {
						skip_key = ESIF_TRUE;
						break;
					}
				}

				// copy  = always replace existing key in target if it already exists
				// merge = never replace existing key in target if it already exists
				if ((skip_key == ESIF_FALSE) &&
					(replaceKeys == ESIF_TRUE || DataBank_KeyExists(g_DataBankMgr, (esif_string)nameSpaceTo->buf_ptr, (esif_string)data_key->buf_ptr) == ESIF_FALSE)) {

					EsifData_Destroy(data_value);
					data_value = EsifData_CreateAs(ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);
					if (data_value == NULL) {
						rc = ESIF_E_NO_MEMORY;
						break;
					}
					rc = EsifConfigGet(nameSpaceFrom, data_key, data_value);
					if (rc != ESIF_OK) {
						break;
					}
					rc = EsifConfigSet(nameSpaceTo, data_key, flags, data_value);
					if (rc != ESIF_OK) {
						break;
					}

					ESIF_TRACE_DEBUG("DV %s: @%s => @%s [%s] {%s, %u bytes}\n",
						(replaceKeys ? "Copy" : "Merge"),
						(esif_string)nameSpaceFrom->buf_ptr,
						(esif_string)nameSpaceTo->buf_ptr,
						(esif_string)data_key->buf_ptr,
						esif_data_type_str(data_value->type),
						data_value->data_len);

					exported++;
				}

				// Reset Key for next search
				EsifData_Set(data_key, ESIF_DATA_STRING, keyset[key], 0, ESIFAUTOLEN);
			} while ((rc = EsifConfigFindNext(nameSpaceFrom, data_key, NULL, &context)) == ESIF_OK);

			EsifConfigFindClose(&context);
		}
		if (rc == ESIF_E_ITERATION_DONE || rc == ESIF_E_NOT_FOUND) {
			rc = ESIF_OK;
		}
	}

exit:
	if (rc == ESIF_OK && keycount != NULL) {
		*keycount = exported;
	}
	EsifData_Destroy(data_key);
	EsifData_Destroy(data_value);
	esif_ccb_free(keylist);
	esif_ccb_free(keyset);
	return rc;
}
Esempio n. 12
0
// Find Next path in nameSpace
eEsifError EsifConfigFindNext(
	EsifDataPtr nameSpace,
	EsifDataPtr path,
	EsifDataPtr value,
	EsifConfigFindContextPtr context
	)
{
	eEsifError rc = ESIF_E_NOT_FOUND;
	DataVaultPtr DB = 0;

	ESIF_ASSERT(nameSpace && path && context);
	DB = DataBank_GetNameSpace(g_DataBankMgr, (StringPtr)(nameSpace->buf_ptr));
	if (DB) {
		UInt32 item = 0;

		esif_ccb_read_lock(&DB->lock);

		for (item = 0; item < DB->cache->size; item++) {
			if (*context == NULL || esif_ccb_stricmp(DB->cache->elements[item].key.buf_ptr, *context) > 0) {
				break;
			}
		}

		// Find next matching key
		while (item < DB->cache->size && EsifConfigKeyMatch(path, &DB->cache->elements[item].key) != ESIF_TRUE) {
			item++;
		}

		// Return matching item, if any
		if (item < DB->cache->size) {
			esif_ccb_free(*context);
			*context = esif_ccb_strdup(DB->cache->elements[item].key.buf_ptr);
			
			if (path->buf_len && path->buf_len < DB->cache->elements[item].key.data_len) {
				rc = ESIF_E_NEED_LARGER_BUFFER;
			}
			else {
				EsifData_Set(path, 
							 ESIF_DATA_STRING,
							 esif_ccb_strdup((char*)DB->cache->elements[item].key.buf_ptr),
							 DB->cache->elements[item].key.data_len,
							 DB->cache->elements[item].key.data_len);

				// If no value buffer for result, just return next matching key
				if (NULL == value)
					rc = ESIF_OK;
				else
					rc = EsifConfigGet(nameSpace, path, value);
			}
		}
		else if (*context != NULL) {
			rc = ESIF_E_ITERATION_DONE;
		}
		esif_ccb_read_unlock(&DB->lock);
	}

	if (rc != ESIF_OK) {
		EsifConfigFindClose(context);
	}
	return rc;
}
Esempio n. 13
0
/* Set */
eEsifError DataVault_SetValue (
	DataVaultPtr self,
	EsifDataPtr path,
	EsifDataPtr value,
	esif_flags_t flags
	)
{
	eEsifError rc = ESIF_OK;
	DataCacheEntryPtr keypair;

	if (FLAGS_TEST(self->flags, ESIF_SERVICE_CONFIG_STATIC | ESIF_SERVICE_CONFIG_READONLY)) {
		return ESIF_E_READONLY;
	}

	// Ignore value for DELETEs
	if (FLAGS_TEST(flags, ESIF_SERVICE_CONFIG_DELETE)) {
		value = NULL;
	}

	// TODO: Locking

	// AUTO data types or AUTO_ALLOCATE are not allowed for SET
	if (value && (value->type == ESIF_DATA_AUTO || value->buf_len == ESIF_DATA_ALLOCATE)) {
		return ESIF_E_UNSUPPORTED_RESULT_DATA_TYPE;
	}

	// Write to Log
	DataVault_WriteLog(self, (flags & ESIF_SERVICE_CONFIG_DELETE ? "DELETE" : "SET"), self->name, path, flags, value);

	// Delete entire DataVault?
	if (strcmp((esif_string)path->buf_ptr, "*") == 0) {
		if (flags & ESIF_SERVICE_CONFIG_DELETE) {
			DataCache_Destroy(self->cache);
			if ((self->cache = DataCache_Create()) == NULL) {
				return ESIF_E_NO_MEMORY;
			}
			DataVault_WriteVault(self);
			return ESIF_OK;
		}
		return ESIF_E_NOT_SUPPORTED;// "*" is an invalid key value
	}

	// Read data from File
	// TODO: Change Parser Logic and Syntax instead
	if (value && value->buf_ptr && esif_ccb_strncmp((char*)value->buf_ptr, "<<", 2) == 0) {
		void *buffer  = 0;
		UInt32 buflen = 0;
		if (DataVault_ReadFile(self, (char*)value->buf_ptr + 2, &buffer, &buflen) == ESIF_OK) {
			value->buf_ptr = buffer;
			if (value->type == ESIF_DATA_STRING) {
				buflen++;	// Include Null Terminator
			}
			value->buf_len = value->data_len = buflen;
		} else {
			return ESIF_E_UNSPECIFIED;	// TODO: File Not Found
		}
	}

	// Get the Data Row or create it if it does not exist
	keypair = DataCache_GetValue(self->cache, (esif_string)path->buf_ptr);
	if (keypair) {	// Match Found
		// READONLY?
		if (keypair->flags & ESIF_SERVICE_CONFIG_READONLY) {
			rc = ESIF_E_READONLY;
		}
		// DELETE?
		else if (flags & ESIF_SERVICE_CONFIG_DELETE) {
			flags |= keypair->flags;
			DataCache_Delete(self->cache, (esif_string)path->buf_ptr);
		} else if (value && value->buf_ptr) {
			// UPDATE
#ifdef ESIF_ATTR_OS_WINDOWS
			// Update Registry Value for REGKEY values if REGLINK is *NOT* specified in SET command
			if ((keypair->flags & ESIF_SERVICE_CONFIG_REGLINK) && !(flags & ESIF_SERVICE_CONFIG_REGLINK)) {
				keypair->value.type = value->type;
				rc = DataVault_WriteRegistry(self, (esif_string)keypair->value.buf_ptr, value);
			} else	// ...
#endif
			if (keypair->value.buf_len && value->data_len > keypair->value.buf_len) {
				rc = ESIF_E_NEED_LARGER_BUFFER;
			} else {
				keypair->flags = flags;
				keypair->value.type     = value->type;
				keypair->value.data_len = value->data_len;

				// Replace the File Offset stored in buf_ptr with a copy of the data for updated NOCACHE values
				if (keypair->flags & ESIF_SERVICE_CONFIG_NOCACHE && keypair->value.buf_len == 0) {
					keypair->value.buf_ptr = esif_ccb_malloc(value->data_len);
					keypair->value.buf_len = value->data_len;
				}
				esif_ccb_memcpy(keypair->value.buf_ptr, value->buf_ptr, value->data_len);
				rc = ESIF_OK;
			}
		}
	} else if (value && value->buf_ptr && !(flags & ESIF_SERVICE_CONFIG_DELETE)) {
		// Copy Key/Value Pair to new Data Row
		EsifData newkey   = {ESIF_DATA_STRING, esif_ccb_strdup((esif_string)path->buf_ptr), path->data_len, path->data_len};
		EsifData newvalue = {value->type, esif_ccb_malloc(esif_ccb_max(value->buf_len, value->data_len)), value->data_len, value->data_len};
		esif_ccb_memcpy(newvalue.buf_ptr, value->buf_ptr, value->data_len);
		DataCache_SetValue(self->cache, (esif_string)newkey.buf_ptr, newvalue, flags);
		esif_ccb_free(newkey.buf_ptr);
	}

	// If Persisted, Flush to disk
	if (rc == ESIF_OK && FLAGS_TEST(flags, ESIF_SERVICE_CONFIG_PERSIST)) {
		DataVault_WriteVault(self);
	}
	return rc;
}
Esempio n. 14
0
// Retrieve a single value from a DataVault
eEsifError DataVault_GetValue (
	DataVaultPtr self,
	EsifDataPtr path,
	EsifDataPtr value
	)
{
	eEsifError rc = ESIF_E_NOT_FOUND;
	DataCacheEntryPtr keypair = NULL;

	if (!self)
		return ESIF_E_PARAMETER_IS_NULL;

	// TODO: Locking

	// Debug: Dump Entire Contents of DataVault if path="*"
	if (strcmp((esif_string)path->buf_ptr, "*") == 0) {
		UInt32 context = 0;
		EsifDataPtr nameSpace = EsifData_CreateAs(ESIF_DATA_STRING, esif_ccb_strdup(self->name), ESIFAUTOLEN, ESIFAUTOLEN);
		EsifDataPtr key       = EsifData_CreateAs(ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);
		EsifDataPtr value     = EsifData_CreateAs(ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);

#ifdef _DEBUG
		// Dump DataCache if Debug Mode
		UInt32 i;
		for (i = 0; i < self->cache->size; i++) {
			DataCacheEntryPtr keypair = &self->cache->elements[i];
			CMD_DEBUG("\n"
					  "id=%d\n"
					  "key=%s\n"
					  "flags=%08X\n"
					  "value.type = %s (%d)\n"
					  "value.buf_ptr = %p %s\n"
					  "value.buf_len = %d\n"
					  "value.data_len= %d\n",
					  i,
					  (char*)keypair->key.buf_ptr,
					  keypair->flags,
					  esif_data_type_str(keypair->value.type), keypair->value.type,
					  keypair->value.buf_ptr,
					  ((keypair->flags & (ESIF_SERVICE_CONFIG_FILELINK | ESIF_SERVICE_CONFIG_REGLINK)) ? (esif_string)keypair->value.buf_ptr : ""),
					  keypair->value.buf_len,
					  keypair->value.data_len
					  );
		}
		if (i > 0) {
			CMD_DEBUG("*****************\n");
		}
#endif
		// Iterate the Contents of the Data Cache
		if ((rc = EsifConfigFindFirst(nameSpace, key, value, &context)) == ESIF_OK) {
			do {
				StringPtr valuestr = EsifData_ToString(value);
				CMD_DEBUG("\n"
						  "EsifFind%s(\"%s\",%d):\n"
						  "  path = %s\n"
						  "  value= %s%s%s\n"
						  "  type = %s, len=%d\n",
						  (context <= 1 ? "First" : "Next"), (char*)nameSpace->buf_ptr, context - 1,
						  (char*)key->buf_ptr,
						  (value->type == ESIF_DATA_STRING ? "\"" : ""), (valuestr ? valuestr : ""), (value->type == ESIF_DATA_STRING ? "\"" : ""),
						  esif_data_type_str(value->type), value->data_len
						  );
				esif_ccb_free(valuestr);
				EsifData_Set(key, ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);
				EsifData_Set(value, ESIF_DATA_AUTO, NULL, ESIF_DATA_ALLOCATE, 0);
			} while ((rc = EsifConfigFindNext(nameSpace, key, value, &context)) == ESIF_OK);
			if (rc == ESIF_E_NOT_FOUND) {
				rc = ESIF_OK;
			}
		}
		EsifData_Destroy(nameSpace);
		EsifData_Destroy(key);
		EsifData_Destroy(value);
		return ESIF_OK;
	}

	// Write to Log before retrieval if AUTO
	if (value->type == ESIF_DATA_AUTO || value->buf_len == ESIF_DATA_ALLOCATE) {
		DataVault_WriteLog(self, "AUTO", (esif_string)(self->name), path, 0, value);
	}

	keypair = DataCache_GetValue(self->cache, (esif_string)path->buf_ptr);
	
	if (NULL != keypair) {
		UInt32 data_len = keypair->value.data_len;
		void *buf_ptr   = keypair->value.buf_ptr;

		// File Redirect?
		if (keypair->flags & ESIF_SERVICE_CONFIG_FILELINK) {
			if (DataVault_ReadFile(self, (esif_string)buf_ptr, &buf_ptr, &data_len) != ESIF_OK) {
				value->data_len = 0;
				if (value->type == ESIF_DATA_AUTO) {
					value->type = keypair->value.type;
				}
				if (value->buf_len == ESIF_DATA_ALLOCATE) {
					value->buf_len = 0;
					value->buf_ptr = 0;
				}
				return ESIF_OK;	// return OK and a blank buffer if file not found/error
			}
		}

#ifdef ESIF_ATTR_OS_WINDOWS
		// Registry Redirect?
		else if (keypair->flags & ESIF_SERVICE_CONFIG_REGLINK) {
			if (DataVault_ReadRegistry(self, (esif_string)buf_ptr, &buf_ptr, &data_len) != ESIF_OK) {
				value->data_len = 0;
				if (value->type == ESIF_DATA_AUTO) {
					value->type = keypair->value.type;
				}
				if (value->buf_len == ESIF_DATA_ALLOCATE) {
					value->buf_len = 0;
					value->buf_ptr = 0;
				}
				return ESIF_OK;	// return OK and a blank buffer if Registry value not found/error
			}
		}
#endif
		// Match Found. Verify Data Type matches unless AUTO
		if (value->type != keypair->value.type && value->type != ESIF_DATA_AUTO) {
			rc = ESIF_E_UNSUPPORTED_RESULT_DATA_TYPE;	// TODO: ESIF_E_INVALID_DATA_TYPE
		}
		// Verify Data Buffer is large enough unless Auto-Allocate
		else if (value->buf_len < data_len && value->buf_len != ESIF_DATA_ALLOCATE) {
			value->buf_len = data_len;
			rc = ESIF_E_NEED_LARGER_BUFFER;
		}
		// Return pointer to static contents if this is a static vault
		else if ((self->flags & ESIF_SERVICE_CONFIG_STATIC) &&
				 (value->type == ESIF_DATA_AUTO) &&
				 (value->buf_len == ESIF_DATA_ALLOCATE)) {
			value->type     = keypair->value.type;
			value->data_len = data_len;
			value->buf_len  = 0;	// Caller MUST NOT Free!
			value->buf_ptr  = buf_ptr;
			rc = ESIF_OK;
		} else {
			// Set Data Type and Auto-Allocate Buffer?
			if (value->type == ESIF_DATA_AUTO) {
				value->type = keypair->value.type;
			}
			if (ESIF_DATA_ALLOCATE == value->buf_len) {
				value->buf_len = data_len;
				value->buf_ptr = esif_ccb_malloc(value->buf_len);
				if (!value->buf_ptr) {
					return ESIF_E_NO_MEMORY;
				}
			}

			// Read from file if NOCACHE option
			if ((keypair->flags & ESIF_SERVICE_CONFIG_NOCACHE) && keypair->value.buf_len == 0) {
				size_t offset = (size_t)keypair->value.buf_ptr;
				if (DataVault_ReadBlock(self, (esif_string)value->buf_ptr, data_len, offset) != ESIF_OK) {
					data_len = 0;
					return ESIF_E_NOT_FOUND;
				}
				// Decrypt?
				if (keypair->flags & ESIF_SERVICE_CONFIG_ENCRYPT) {
					UInt32 byte;
					for (byte = 0; byte < data_len; byte++)
						((UInt8*)(value->buf_ptr))[byte] = ~((UInt8*)(value->buf_ptr))[byte];
				}
			} else {
				esif_ccb_memcpy(value->buf_ptr, buf_ptr, data_len);
			}
			value->data_len = data_len;
			rc = ESIF_OK;
		}
	}
	// Write to Log
	DataVault_WriteLog(self, "GET", (esif_string)self->name, path, 0, value);
	return rc;
}
Esempio n. 15
0
// Write a Registry Key Value from an EsifData value
static eEsifError DataVault_WriteRegistry (
	DataVaultPtr self,
	esif_string keyname,
	EsifDataPtr value
	)
{
	eEsifError rc   = ESIF_E_UNSPECIFIED;
	HKEY hKey       = 0;
	DWORD dwFlags   = RRF_RT_ANY;
	DWORD dwError   = 0;
	DWORD dwSize    = 0;
	DWORD dwType    = REG_NONE;
	char *regkey    = 0;
	char *valuename = 0;
	u32 j;

	UNREFERENCED_PARAMETER(self);

	// Keyname Format: "HKLM\Subkey\Path\ValueName"
	for (j = 0; HKeyList[j].name; j++) {
		size_t namelen = esif_ccb_strlen(HKeyList[j].name, 30);
		if (esif_ccb_strnicmp(HKeyList[j].name, keyname, namelen) == 0 && keyname[namelen] == '\\') {
			hKey     = HKeyList[j].hkey;
			keyname += esif_ccb_strlen(HKeyList[j].name, 30) + 1;
			break;
		}
	}
	if (!hKey) {
		return rc;
	}

	regkey = esif_ccb_strdup(keyname);
	if ((valuename = strrchr(regkey, '\\')) == 0) {
		esif_ccb_free(regkey);
		return rc;
	}
	*valuename++ = 0;

	// Get Data Type from Registry if value already exists, otherwise deduce it from ESIF Data Type
	if ((dwError = RegGetValueA(hKey, regkey, valuename, dwFlags, &dwType, 0, 0)) != 0) {
		switch (value->type) {
		case ESIF_DATA_INT32:
		case ESIF_DATA_UINT32:
		case ESIF_DATA_TEMPERATURE:
			dwType = REG_DWORD;
			dwSize = value->data_len;
			break;

		case ESIF_DATA_INT64:
		case ESIF_DATA_UINT64:
			dwType = REG_QWORD;
			dwSize = value->data_len;
			break;

		case ESIF_DATA_STRING:
			dwType = REG_SZ;
			dwSize = value->data_len;
			break;

		case ESIF_DATA_BINARY:
		case ESIF_DATA_BLOB:
			dwType = REG_BINARY;
			dwSize = value->data_len;
			break;

		default:
			rc = ESIF_E_NOT_SUPPORTED;
			break;
		}
	}
	// Write Registry Value
	if (dwType != REG_NONE) {
		dwError = RegSetKeyValueA(hKey, regkey, valuename, dwType, value->buf_ptr, value->data_len);
		if (dwError == 0) {
			rc = ESIF_OK;
		}
	}
	esif_ccb_free(regkey);
	return rc;
}
Esempio n. 16
0
// Initialize Pathname List
enum esif_rc esif_pathlist_init(esif_string paths)
{
	static esif_pathmap pathmap[] = {
		{ "HOME",	ESIF_PATHTYPE_HOME },
		{ "TEMP",	ESIF_PATHTYPE_TEMP },
		{ "DV",		ESIF_PATHTYPE_DV },
		{ "LOG",	ESIF_PATHTYPE_LOG },
		{ "BIN",	ESIF_PATHTYPE_BIN },
		{ "LOCK",	ESIF_PATHTYPE_LOCK },
		{ "EXE",	ESIF_PATHTYPE_EXE },
		{ "DLL",	ESIF_PATHTYPE_DLL },
		{ "DPTF",	ESIF_PATHTYPE_DPTF },
		{ "DSP",	ESIF_PATHTYPE_DSP },
		{ "CMD",	ESIF_PATHTYPE_CMD },
		{ "UI",		ESIF_PATHTYPE_UI  },
		{ NULL }
	};
	static int num_paths = (sizeof(pathmap)/sizeof(*pathmap)) - 1;
	esif_string *pathlist = NULL;
	enum esif_rc rc = ESIF_OK;

	if (!g_pathlist.initialized) {
		char *buffer = NULL;
		char *filename = ESIF_PATHLIST_FILENAME;
		FILE *fp = NULL;
		struct stat st={0};

		// Use pathlist file, if it exists
		if (esif_ccb_stat(filename, &st) == 0 && esif_ccb_fopen(&fp, filename, "rb") == 0) {
			if ((buffer = (char *) esif_ccb_malloc(st.st_size + 1)) != NULL) {
				if (esif_ccb_fread(buffer, st.st_size, sizeof(char), st.st_size, fp) != (size_t)st.st_size) {
					rc = ESIF_E_IO_ERROR;
				}
			}
			esif_ccb_fclose(fp);
		}
		// Otherwise, use default pathlist
		else if (buffer == NULL && paths != NULL) {
			buffer = esif_ccb_strdup(paths);
		}

		if ((buffer == NULL) || ((pathlist = (esif_string *)esif_ccb_malloc(num_paths * sizeof(esif_string))) == NULL)) {
			esif_ccb_free(buffer);
			buffer = NULL;
			rc = ESIF_E_NO_MEMORY;
		}

		// Parse key/value pairs into pathlist
		if (rc == ESIF_OK && buffer != NULL) {
			char *ctxt = 0;
				char *keypair = esif_ccb_strtok(buffer, "\r\n", &ctxt);
				char *value = 0;
				int  id=0;

				g_pathlist.initialized = 1;
				g_pathlist.pathmap = pathmap;
				g_pathlist.num_paths = num_paths;
				g_pathlist.pathlist = pathlist;

				while (keypair != NULL) {
					value = esif_ccb_strchr(keypair, '=');
					if (value) {
						*value++ = 0;
						for (id = 0; id < g_pathlist.num_paths && g_pathlist.pathmap[id].name; id++) {
							if (esif_ccb_stricmp(keypair, g_pathlist.pathmap[id].name) == 0) {
								esif_pathlist_set(g_pathlist.pathmap[id].type, value);
								break;
							}
						}
					}
					keypair = esif_ccb_strtok(NULL, "\r\n", &ctxt);
				}
		}
		esif_ccb_free(buffer);
	}
	return rc;
}
Esempio n. 17
0
// Retrieve a single value from a DataVault
eEsifError DataVault_GetValue(
	DataVaultPtr self,
	EsifDataPtr path,
	EsifDataPtr value,
	esif_flags_t *flagsPtr
	)
{
	eEsifError rc = ESIF_E_NOT_FOUND;
	DataCacheEntryPtr keypair = NULL;

	if (!self)
		return ESIF_E_PARAMETER_IS_NULL;

	if (flagsPtr)
		*flagsPtr = 0;

	// Return "keyname1|keyname2|..." if path contains "*" or "?"
	if (esif_ccb_strpbrk((esif_string)path->buf_ptr, "*?") != NULL) {
		EsifDataPtr nameSpace = EsifData_CreateAs(ESIF_DATA_STRING, esif_ccb_strdup(self->name), ESIFAUTOLEN, ESIFAUTOLEN);
		EsifDataPtr key		  = EsifData_CreateAs(ESIF_DATA_STRING, path->buf_ptr, 0, ESIFAUTOLEN);
		EsifConfigFindContext context = NULL;
		esif_string keylist = NULL;
		u32 data_len = 0;
		
		// Verify valid Data Type and Data Buffer size
		if (value->type != ESIF_DATA_STRING && value->type != ESIF_DATA_AUTO) {
			rc = ESIF_E_UNSUPPORTED_RESULT_DATA_TYPE;
		}
		
		if (rc == ESIF_E_NOT_FOUND && nameSpace != NULL && key != NULL && (rc = EsifConfigFindFirst(nameSpace, key, NULL, &context)) == ESIF_OK) {
			do {
				data_len += (u32)key->data_len;
				esif_string newlist = esif_ccb_realloc(keylist, data_len);
				if (newlist == NULL) {
					EsifData_Set(key, ESIF_DATA_STRING, "", 0, ESIFAUTOLEN);
					rc = ESIF_E_NO_MEMORY;
					break;
				}
				keylist = newlist;
				esif_ccb_sprintf_concat(data_len, keylist, "%s%s", (*keylist ? "|" : ""), (char *)key->buf_ptr);
				EsifData_Set(key, ESIF_DATA_STRING, path->buf_ptr, 0, ESIFAUTOLEN);
			} while ((rc = EsifConfigFindNext(nameSpace, key, NULL, &context)) == ESIF_OK);
		
			EsifConfigFindClose(&context);
			if (rc == ESIF_E_ITERATION_DONE) {
				rc = ESIF_OK;
			}
		}
		EsifData_Destroy(nameSpace);
		EsifData_Destroy(key);
		if (!keylist || rc != ESIF_OK) {
			esif_ccb_free(keylist);
			return rc;
		}

		// Return keylist value and data type
		if (value->type == ESIF_DATA_AUTO) {
			value->type = ESIF_DATA_STRING;
		}
		if (value->buf_len == ESIF_DATA_ALLOCATE) {
			esif_ccb_free(value->buf_ptr);
			value->buf_ptr = esif_ccb_strdup(keylist);
			value->buf_len = data_len;
			value->data_len = data_len;
		}
		else if (value->buf_len < data_len) {
			rc = ESIF_E_NEED_LARGER_BUFFER;
			value->data_len = data_len;
		}
		else if (value->buf_ptr) {
			esif_ccb_strcpy(value->buf_ptr, keylist, value->buf_len);
			value->data_len = data_len;
		}
		esif_ccb_free(keylist);
		return rc;
	}

	// Write to Log before retrieval if AUTO
	if (value->type == ESIF_DATA_AUTO || value->buf_len == ESIF_DATA_ALLOCATE) {
		DataVault_WriteLog(self, "AUTO", (esif_string)(self->name), path, 0, value);
	}

	keypair = DataCache_GetValue(self->cache, (esif_string)path->buf_ptr);
	
	if (NULL != keypair) {
		UInt32 data_len = keypair->value.data_len;
		void *buf_ptr   = keypair->value.buf_ptr;
		UInt32 buf_len  = 0;
		Bool buf_alloc = ESIF_FALSE;

		// File Redirect?
		if (keypair->flags & ESIF_SERVICE_CONFIG_FILELINK) {
			if (ReadFileIntoBuffer((esif_string)buf_ptr, &buf_ptr, &data_len) != ESIF_OK) {
				value->data_len = 0;
				if (value->type == ESIF_DATA_AUTO) {
					value->type = keypair->value.type;
				}
				if (value->buf_len == ESIF_DATA_ALLOCATE) {
					value->buf_len = 0;
					value->buf_ptr = 0;
				}
				return ESIF_OK;	// return OK and a blank buffer if file not found/error
			}
			// Include Null Terminator if result is STRING
			if (value->buf_len == ESIF_DATA_ALLOCATE && (value->type == ESIF_DATA_STRING || (value->type == ESIF_DATA_AUTO && keypair->value.type == ESIF_DATA_STRING))) {
				data_len++;
			}
			buf_len = data_len;
			buf_alloc = ESIF_TRUE;
		}

		// Match Found. Verify Data Type matches unless AUTO
		if (value->type != keypair->value.type && value->type != ESIF_DATA_AUTO) {
			rc = ESIF_E_UNSUPPORTED_RESULT_DATA_TYPE;	// TODO: ESIF_E_INVALID_DATA_TYPE
		}
		// Verify Data Buffer is large enough unless Auto-Allocate
		else if (value->buf_len < data_len && value->buf_len != ESIF_DATA_ALLOCATE) {
			value->data_len = data_len;
			rc = ESIF_E_NEED_LARGER_BUFFER;
		}
		// Return pointer to static contents if this is a static vault
		else if ((self->flags & ESIF_SERVICE_CONFIG_STATIC) &&
				 (value->type == ESIF_DATA_AUTO) &&
				 (value->buf_len == ESIF_DATA_ALLOCATE)) {
			value->type     = keypair->value.type;
			value->data_len = data_len;
			value->buf_len  = 0;	// Caller MUST NOT Free!
			value->buf_ptr  = buf_ptr;
			rc = ESIF_OK;
		} 
		else {
			// Set Data Type and Auto-Allocate Buffer?
			if (value->type == ESIF_DATA_AUTO) {
				value->type = keypair->value.type;
			}
			if (ESIF_DATA_ALLOCATE == value->buf_len) {
				value->buf_len = esif_ccb_max(1, data_len);
				value->buf_ptr = esif_ccb_malloc(value->buf_len);
				if (!value->buf_ptr) {
					if (buf_alloc) {
						esif_ccb_free(buf_ptr);
					}
					return ESIF_E_NO_MEMORY;
				}
			}

			// Read from file if NOCACHE option
			if ((keypair->flags & ESIF_SERVICE_CONFIG_NOCACHE) && keypair->value.buf_len == 0) {
				size_t offset = (size_t)keypair->value.buf_ptr;
				if (DataVault_GetFromSource(self, (esif_string)value->buf_ptr, data_len, offset) != ESIF_OK) {
					if (buf_alloc) {
						esif_ccb_free(buf_ptr);
					}
					return ESIF_E_NOT_FOUND;
				}
				// Unscramble Data?
				if (keypair->flags & ESIF_SERVICE_CONFIG_SCRAMBLE) {
					UInt32 byte;
					for (byte = 0; byte < data_len; byte++)
						((UInt8*)(value->buf_ptr))[byte] = ~((UInt8*)(value->buf_ptr))[byte];
				}
			} else {
				esif_ccb_memcpy(value->buf_ptr, buf_ptr, data_len);
			}
			value->data_len = data_len;
			rc = ESIF_OK;
		}

		// Return flags
		if (rc == ESIF_OK) {
			if (flagsPtr != NULL)
				*flagsPtr = keypair->flags;
		}

		// Destroy Dynamically copied data, such as FILELINK contents
		if (buf_alloc) {
			esif_ccb_free(buf_ptr);
		}
	}
	// Write to Log
	DataVault_WriteLog(self, "GET", (esif_string)self->name, path, 0, value);
	return rc;
}
Esempio n. 18
0
static eEsifError EsifDataLogValidateParticipantList(char *inParticipantList)
{
	int i = 0;
	char *participantList = NULL;
	char *participantSelect = NULL;
	char colDelims [] = ",";
	char *partTok = NULL;
	UInt8 participantId = 0;
	int participantCounter = 0;
	int totalFields = 0;
	eEsifError rc = ESIF_OK;

	for (i = 0; i < MAX_PARTICIPANT_ENTRY; i++){
		g_dataLogParticipants[i].participantId = 0;
		g_dataLogParticipants[i].participantNumFields = 0;
	}
	
	if (inParticipantList == NULL) {
		for (i = 0; i < MAX_PARTICIPANT_ENTRY; i++) {
			EsifUpPtr upPtr = NULL;

			participantId = (UInt8) i;
			upPtr = EsifUpPm_GetAvailableParticipantByInstance(participantId);

			if (NULL != upPtr) {
				int j = 0;
				int fieldCounter = 0;
				struct esif_fpc_domain *domainPtr = NULL;
				UInt8 domainCount = (u8)upPtr->fDspPtr->get_domain_count(upPtr->fDspPtr);
				DataLogParticipant nextParticipant = { 0 };

				for (j = 0; j < domainCount; j++) {
					domainPtr = upPtr->fDspPtr->get_domain(upPtr->fDspPtr, j + 1);
					if (NULL == domainPtr) {
						continue;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_TEMP_STATUS) {
						fieldCounter += 4;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_POWER_CONTROL) {
						fieldCounter += 2;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_ACTIVE_CONTROL) {
						fieldCounter += 1;
					}
				}
				nextParticipant.participantId = participantId;
				nextParticipant.participantNumFields = fieldCounter;
				g_dataLogParticipants[participantCounter] = nextParticipant;

				totalFields += fieldCounter;
				EsifUp_PutRef(upPtr);
				participantCounter++;
			}
		}
		goto exit;
	}

	if (esif_ccb_strlen(inParticipantList, MAX_LOG_LINE) >= 1) {

		participantList = esif_ccb_strdup(inParticipantList);
		participantSelect = esif_ccb_strtok(participantList, colDelims, &partTok);

		while (participantSelect != NULL) {
			EsifUpPtr upPtr = { 0 };

			if (participantCounter >= (sizeof(g_dataLogParticipants)/sizeof(*g_dataLogParticipants))) {
					rc = ESIF_E_NOT_SUPPORTED;
					goto exit;
			}

			if ((int)esif_atoi(participantSelect) > 0 || esif_ccb_strcmp(participantSelect, "0") == 0) {
				participantId = (UInt8) esif_atoi(participantSelect);
				upPtr = EsifUpPm_GetAvailableParticipantByInstance(participantId);
			}
			else {
				upPtr = EsifUpPm_GetAvailableParticipantByName(participantSelect);
			}
			if (NULL == upPtr) {
				rc = ESIF_E_PARTICIPANT_NOT_FOUND;
				goto exit;
			}
			else {
				int j = 0;
				int fieldCounter = 0;
				struct esif_fpc_domain *domainPtr = NULL;
				UInt8 domainCount = (u8) upPtr->fDspPtr->get_domain_count(upPtr->fDspPtr);
				DataLogParticipant nextParticipant = { 0 };
				participantId = (UInt8) upPtr->fInstance; /* redundant in the case of an id passed in, but needed for name*/

				for (j = 0; j < domainCount; j++) {
					domainPtr = upPtr->fDspPtr->get_domain(upPtr->fDspPtr, j + 1);
					if (NULL == domainPtr) {
						continue;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_TEMP_STATUS) {
						fieldCounter += 4;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_POWER_CONTROL) {
						fieldCounter += 2;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_ACTIVE_CONTROL) {
						fieldCounter += 1;
					}
				}
				nextParticipant.participantId = participantId;
				nextParticipant.participantNumFields = fieldCounter;
				g_dataLogParticipants[participantCounter] = nextParticipant;

				totalFields += fieldCounter;
				EsifUp_PutRef(upPtr);
				participantCounter++;
			}
			participantSelect = esif_ccb_strtok(NULL, colDelims, &partTok);
		}
	}
	else {
		for (i = 0; i < MAX_PARTICIPANT_ENTRY; i++) {
			EsifUpPtr upPtr = NULL;

			participantId = (UInt8) i;
			upPtr = EsifUpPm_GetAvailableParticipantByInstance(participantId);

			if (NULL != upPtr) {
				int j = 0;
				int fieldCounter = 0;
				struct esif_fpc_domain *domainPtr = NULL;
				UInt8 domainCount = (u8)upPtr->fDspPtr->get_domain_count(upPtr->fDspPtr);
				DataLogParticipant nextParticipant = { 0 };

				for (j = 0; j < domainCount; j++) {
					domainPtr = upPtr->fDspPtr->get_domain(upPtr->fDspPtr, j + 1);
					if (NULL == domainPtr) {
						continue;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_TEMP_STATUS) {
						fieldCounter += 4;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_POWER_CONTROL) {
						fieldCounter += 2;
					}
					if (domainPtr->capability_for_domain.capability_flags & ESIF_CAPABILITY_ACTIVE_CONTROL) {
						fieldCounter += 1;
					}
				}
				nextParticipant.participantId = participantId;
				nextParticipant.participantNumFields = fieldCounter;
				g_dataLogParticipants[participantCounter] = nextParticipant;

				totalFields += fieldCounter;
				EsifUp_PutRef(upPtr);
				participantCounter++;
			}
		}
	}
exit:
	esif_ccb_free(participantList);

	if (totalFields == 0) {
		rc = ESIF_E_NOT_SUPPORTED;
	}
	return rc;
}