//-------------------------------------------------------------------------------------------------- static void SetObject9InstanceForApp ( const char* appName, ///< The name of the application in question. assetData_InstanceDataRef_t instanceRef ///< The instance of object 9 to link to. Pass NULL if ///< the link is to be cleared. ) //-------------------------------------------------------------------------------------------------- { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(CFG_OBJECT_INFO_PATH); if (instanceRef != NULL) { int instanceId; LE_ASSERT(assetData_GetInstanceId(instanceRef, &instanceId) == LE_OK); le_cfg_GoToNode(iterRef, appName); le_cfg_SetInt(iterRef, "oiid", instanceId); LE_DEBUG("Application '%s' mapped to instance %d.", appName, instanceId); } else { le_cfg_DeleteNode(iterRef, appName); } le_cfg_CommitTxn(iterRef); }
//-------------------------------------------------------------------------------------------------- LE_SHARED void cfgInstall_Add ( const char* appName ) //-------------------------------------------------------------------------------------------------- { le_result_t result; char filePath[256] = "/opt/legato/apps/"; result = le_utf8_Append(filePath, appName, sizeof(filePath), NULL); LE_FATAL_IF(result != LE_OK, "App name '%s' is too long.", appName); result = le_utf8_Append(filePath, "/root.cfg", sizeof(filePath), NULL); LE_FATAL_IF(result != LE_OK, "App name '%s' is too long.", appName); LE_INFO("Importing configuration for application '%s' from '%s'.", appName, filePath); le_cfg_IteratorRef_t i = le_cfg_CreateWriteTxn("/apps"); result = le_cfgAdmin_ImportTree(i, filePath, appName); LE_FATAL_IF(result != LE_OK, "Failed to import configuration from '%s' to 'root:/apps/%s' (%s)", filePath, appName, LE_RESULT_TXT(result)); le_cfg_CommitTxn(i); }
//-------------------------------------------------------------------------------------------------- static void ResetApp ( void ) { le_cfg_ConnectService(); le_cfgAdmin_ConnectService(); // Get a write iterator to the application node. le_cfg_IteratorRef_t cfgIter = le_cfg_CreateWriteTxn("/apps"); le_cfg_GoToNode(cfgIter, AppName); // Check if this is a temporary configuration that was previously created by this or a similar // tool. if (le_cfg_IsEmpty(cfgIter, CFG_DEBUG_TOOL)) { fprintf(stderr, "This application already has its original configuration.\n"); exit(EXIT_FAILURE); } // Blow away what's in there now. le_cfg_GoToNode(cfgIter, "/apps"); le_cfg_DeleteNode(cfgIter, AppName); le_cfg_CommitTxn(cfgIter); // NOTE: Currently there is a bug in the config DB where deletions and imports cannot be done in // the same transaction so we must do it in two transactions. cfgInstall_Add(AppName); }
// ------------------------------------------------------------------------------------------------- static int HandleImport ( void ) // ------------------------------------------------------------------------------------------------- { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath); le_result_t result; // Check requested format format. if (UseJson) { result = HandleImportJSON(iterRef, FilePath); } else { result = le_cfgAdmin_ImportTree(iterRef, FilePath, ""); } if (result != LE_OK) { ReportImportExportFail(result, "Import", NodePath, FilePath); le_cfg_CancelTxn(iterRef); return EXIT_FAILURE; } le_cfg_CommitTxn(iterRef); return EXIT_SUCCESS; }
static void IncTestCount ( void ) { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn("/configTest/testCount"); le_cfg_SetInt(iterRef, "", le_cfg_GetInt(iterRef, "", 0) + 1); le_cfg_CommitTxn(iterRef); }
static void DeleteTest() { static char pathBuffer[STR_SIZE] = { 0 }; snprintf(pathBuffer, STR_SIZE, "%s/deleteTest/", TestRootDir); le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_SetString(iterRef, "valueA", "aNewValue"); le_cfg_SetString(iterRef, "valueB", "aNewValue"); le_cfg_SetString(iterRef, "valueC", "aNewValue"); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", "aNewValue"); TestValue(iterRef, "valueC", "aNewValue"); le_cfg_CommitTxn(iterRef); iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_DeleteNode(iterRef, "valueB"); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", ""); TestValue(iterRef, "valueC", "aNewValue"); le_cfg_CommitTxn(iterRef); iterRef = le_cfg_CreateReadTxn(pathBuffer); TestValue(iterRef, "valueA", "aNewValue"); TestValue(iterRef, "valueB", ""); TestValue(iterRef, "valueC", "aNewValue"); DumpTree(iterRef, 0); le_cfg_CancelTxn(iterRef); }
//-------------------------------------------------------------------------------------------------- static int UpdateRebootCount ( void ) { int bootCount = GetRebootCount(); le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(BOOT_COUNT_CFG); bootCount++; le_cfg_SetInt(iterRef, BOOT_COUNT_CFG_VAR, bootCount); le_cfg_CommitTxn(iterRef); return bootCount; }
static void ClearTree() { LE_INFO("---- Clearing Out Current Tree -----------------------------------------------------"); le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(TestRootDir); LE_FATAL_IF(iterRef == NULL, "Test: %s - Could not create iterator.", TestRootDir); DumpTree(iterRef, 0); le_cfg_DeleteNode(iterRef, ""); le_cfg_CommitTxn(iterRef); iterRef = le_cfg_CreateReadTxn(TestRootDir); DumpTree(iterRef, 0); le_cfg_CancelTxn(iterRef); }
//-------------------------------------------------------------------------------------------------- LE_SHARED void cfgInstall_Remove ( const char* appName ) //-------------------------------------------------------------------------------------------------- { LE_INFO("Removing configuration for application '%s'.", appName); // Remove the app configuration from the system tree. le_cfg_IteratorRef_t i = le_cfg_CreateWriteTxn("/apps"); le_cfg_DeleteNode(i, appName); le_cfg_CommitTxn(i); // Now delete the app specific tree. le_cfgAdmin_DeleteTree(appName); }
static void ExistAndEmptyTest() { static char pathBuffer[STR_SIZE] = { 0 }; snprintf(pathBuffer, STR_SIZE, "/%s/existAndEmptyTest/", TestRootDir); { le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(pathBuffer); le_cfg_SetEmpty(iterRef, ""); LE_TEST(le_cfg_IsEmpty(iterRef, "") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == false); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == true); le_cfg_SetString(iterRef, "valueA", "aNewValue"); le_cfg_SetInt(iterRef, "valueB", 10); le_cfg_SetBool(iterRef, "valueC", true); le_cfg_SetFloat(iterRef, "valueD", 10.24); LE_TEST(le_cfg_NodeExists(iterRef, "valueA") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueB") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueC") == true); LE_TEST(le_cfg_NodeExists(iterRef, "valueD") == true); LE_TEST(le_cfg_IsEmpty(iterRef, "valueA") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueB") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueC") == false); LE_TEST(le_cfg_IsEmpty(iterRef, "valueD") == false); le_cfg_CommitTxn(iterRef); } }
//-------------------------------------------------------------------------------------------------- static void ConfigureGdb ( void ) { le_cfg_ConnectService(); le_cfgAdmin_ConnectService(); // Get a write iterator to the application node. le_cfg_IteratorRef_t cfgIter = le_cfg_CreateWriteTxn("/apps"); le_cfg_GoToNode(cfgIter, AppName); // Check if this is a temporary configuration that was previously created by this or a similar // tool. if (!le_cfg_IsEmpty(cfgIter, CFG_DEBUG_TOOL)) { char debugTool[LIMIT_MAX_PATH_BYTES]; // Don't need to check return code because the value is just informative and does not matter // if it is truncated. le_cfg_GetString(cfgIter, CFG_DEBUG_TOOL, debugTool, sizeof(debugTool), ""); fprintf(stderr, "This application has already been configured for %s debug mode.\n", debugTool); exit(EXIT_FAILURE); } // Write into the config's debug tool node to indicate that this configuration has been modified. le_cfg_SetString(cfgIter, CFG_DEBUG_TOOL, "gdb"); // Add 512K to the maxFileSytemBytes so that we can debug this app in sandboxed mode uint32_t maxBytes; maxBytes = le_cfg_GetInt(cfgIter, "maxFileSystemBytes", DEFAULT_LIMIT_MAX_FILE_SYSTEM_BYTES); maxBytes += ADD_FILE_SYSTEM_BYTES; // add an additional 512KBytes LE_INFO("Resetting maxFileSystemBytes to %d bytes", maxBytes); le_cfg_SetInt(cfgIter, "maxFileSystemBytes", maxBytes); // Add gdbserver and libs to the app's 'requires/files' section. le_cfg_GoToNode(cfgIter, "requires/files"); AddImportFiles(cfgIter, &GdbFilesImports, NUM_ARRAY_MEMBERS(GdbFilesImports)); // Add /proc to the app's dirs section. le_cfg_GoToParent(cfgIter); le_cfg_GoToNode(cfgIter, "dirs"); AddImportFiles(cfgIter, &GdbDirsImports, NUM_ARRAY_MEMBERS(GdbDirsImports)); // Delete the list of processes. le_cfg_GoToParent(cfgIter); le_cfg_GoToParent(cfgIter); int i; for (i = 0; i < NumProcs; i++) { char nodePath[LIMIT_MAX_PATH_BYTES]; int n = snprintf(nodePath, sizeof(nodePath), "procs/%s", ProcNames[i]); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_DeleteNode(cfgIter, nodePath); } le_cfg_CommitTxn(cfgIter); }
//-------------------------------------------------------------------------------------------------- le_result_t le_cellnet_SetSimPinCode ( le_sim_Id_t simId, ///< [IN] ///< SIM identifier. const char* pinCodePtr ///< [IN] ///< PIN code to insert in the config tree. ) { le_result_t result=LE_OK; size_t pinCodeLength = strlen(pinCodePtr); LE_DEBUG("simId= %d, pinCode= %s",simId,pinCodePtr); if (simId >= LE_SIM_ID_MAX) { LE_ERROR("Invalid simId (%d) provided!", simId); result = LE_OUT_OF_RANGE; } else { //void entry is taken into account if (strncmp(pinCodePtr,"",LE_SIM_PIN_MAX_LEN)!=0) { if (pinCodeLength > LE_SIM_PIN_MAX_LEN) { LE_KILL_CLIENT("PIN code exceeds %d", LE_SIM_PIN_MAX_LEN); return LE_FAULT; } else if (pinCodeLength < LE_SIM_PIN_MIN_LEN) { LE_ERROR("SIM PIN code is not long enough (min 4 digits)"); result = LE_UNDERFLOW; } else { // test SIM pincode format int i; bool test_ok = true; for (i=0; ((i<pinCodeLength) && test_ok); i++) { if ((pinCodePtr[i] < 0x30) || (pinCodePtr[i] > 0x39)) { test_ok = false; break; } } if (false == test_ok) { LE_ERROR("SIM PIN code format error"); result = LE_FORMAT_ERROR; } } } } if (LE_OK == result) { // Set the configuration path for the SIM. char configPath[LIMIT_MAX_PATH_BYTES]; snprintf(configPath, sizeof(configPath), "%s/%d", CFG_MODEMSERVICE_SIM_PATH, simId); le_cfg_IteratorRef_t simCfgRef = le_cfg_CreateWriteTxn(configPath); le_cfg_SetString(simCfgRef, CFG_NODE_PIN, pinCodePtr); le_cfg_CommitTxn(simCfgRef); LE_DEBUG("SIM PIN code (%s) inserted OK in config Tree",pinCodePtr); // New SIM pincode is taken into account LoadSimFromConfigDb(simId); SendCellNetStateEvent(); } return result; }
// ------------------------------------------------------------------------------------------------- static int HandleSet ( void ) // ------------------------------------------------------------------------------------------------- { // Looks like we're trying to write a value to a node. Get the node's current type and then // write the requested value to that node. le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath); le_cfg_nodeType_t originalType = le_cfg_GetNodeType(iterRef, ""); le_cfg_nodeType_t newType = DataType; if ( (newType != originalType) && (originalType != LE_CFG_TYPE_DOESNT_EXIST)) { printf("Converting node '%s' type from %s to %s.\n", NodePath, NodeTypeStr(originalType), NodeTypeStr(newType)); } int result = EXIT_SUCCESS; switch (newType) { case LE_CFG_TYPE_STRING: le_cfg_SetString(iterRef, "", NodeValue); break; case LE_CFG_TYPE_BOOL: if (strcmp(NodeValue, "false") == 0) { le_cfg_SetBool(iterRef, "", false); } else if (strcmp(NodeValue, "true") == 0) { le_cfg_SetBool(iterRef, "", true); } else { fprintf(stderr, "Bad boolean value '%s'.\n", NodeValue); } break; case LE_CFG_TYPE_INT: { char *endIntp; errno = 0; int32_t value = strtol(NodeValue, &endIntp, 10); if (errno != 0) { fprintf(stderr, "Integer '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endIntp != '\0') { fprintf(stderr, "Invalid character in integer '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetInt(iterRef, "", value); } break; } case LE_CFG_TYPE_FLOAT: { char *endFloatp; errno = 0; double floatVal = strtod(NodeValue, &endFloatp); if (errno != 0) { fprintf(stderr, "Float value '%s' out of range\n", NodeValue); result = EXIT_FAILURE; } else if (*endFloatp != '\0') { fprintf(stderr, "Invalid character in float value '%s'\n", NodeValue); result = EXIT_FAILURE; } else { le_cfg_SetFloat(iterRef, "", floatVal); } break; } case LE_CFG_TYPE_DOESNT_EXIST: result = EXIT_FAILURE; break; default: fprintf(stderr, "Unexpected node type specified, %s.\n", NodeTypeStr(newType)); result = EXIT_FAILURE; break; } // Finally, commit the value update, if the set was successful. if (result != EXIT_FAILURE) { le_cfg_CommitTxn(iterRef); } else { le_cfg_CancelTxn(iterRef); } return result; }
// ------------------------------------------------------------------------------------------------- static int HandleCopy ( void ) // ------------------------------------------------------------------------------------------------- { // Create a temp file to export the tree to. char tempFilePath[] = "/tmp/configExport-XXXXXX"; int tempFd; do { tempFd = mkstemp(tempFilePath); } while ((tempFd == -1) && (errno == EINTR)); if (tempFd == -1) { fprintf(stderr, "Could not create temp file. Reason, %s (%d).", strerror(errno), errno); return 1; } // Unlink the file now so that we can make sure that it will end up being deleted, no matter how // we exit. if (unlink(tempFilePath) == -1) { printf("Could not unlink temporary file. Reason, %s (%d).", strerror(errno), errno); } // Create a transaction and export the data from the config tree. le_cfg_IteratorRef_t iterRef = le_cfg_CreateWriteTxn(NodePath); le_result_t result = le_cfgAdmin_ExportTree(iterRef, tempFilePath, ""); if (result != LE_OK) { fprintf(stderr, "An I/O error occurred while updating the config tree. " "Tree has been left untouched.\n"); goto txnDone; } if (DeleteAfterCopy != false) { // Since this is a rename, then delete the node at the original location. le_cfg_DeleteNode(iterRef, ""); } // Now, move the iterator to the node's new name, then attempt to reload the data. le_cfg_GoToNode(iterRef, ".."); result = le_cfgAdmin_ImportTree(iterRef, tempFilePath, NodeDestPath); if (result != LE_OK) { switch (result) { case LE_FAULT: fprintf(stderr, "An I/O error occurred while updating the config tree. " "Tree has been left untouched.\n"); break; case LE_FORMAT_ERROR: fprintf(stderr, "Import/export corruption detected. Tree has been left untouched.\n"); break; default: fprintf(stderr, "An unexpected error has occurred: %s, (%d).\n", LE_RESULT_TXT(result), result); break; } } txnDone: // Make sure that the change was successful, and either commit or discard any changes that were // made. if (result == LE_OK) { le_cfg_CommitTxn(iterRef); } else { le_cfg_CancelTxn(iterRef); } // Was the operation successful? int exitResult = (result == LE_OK) ? EXIT_SUCCESS : EXIT_FAILURE; // Finally, clean up our temp file and report our results. int closeRetVal; do { closeRetVal = close(tempFd); } while ((closeRetVal == -1) && (errno == EINTR)); if (closeRetVal == -1) { fprintf(stderr, "Could not close temp file. Reason, %s (%d).", strerror(errno), errno); exitResult = EXIT_FAILURE; } return exitResult; }