//-------------------------------------------------------------------------------------------------- static void SetPriority ( const char* priorityPtr ///< [IN] Priority to set the process to. If this is NULL the /// default priority will be used. ) { const char* priorityStr = DEFAULT_PRIORITY; if (priorityPtr != NULL) { priorityStr = priorityPtr; } INTERNAL_ERR_IF(proc_SetPriority(priorityStr, 0) != LE_OK, "Could not set the priority level to '%s'.\n", priorityStr); }
//-------------------------------------------------------------------------------------------------- static void GetAppIds ( app_Ref_t appRef, ///< [IN] App ref. const char* appNamePtr, ///< [IN] App name. uid_t* uidPtr, ///< [OUT] Location to store the app's user ID. gid_t* gidPtr, ///< [OUT] Location to store the app's primary group ID. char* userNamePtr, ///< [OUT] Location to store the app user name size_t userNameLen ///< [IN] Size of userNamePtr ) { INTERNAL_ERR_IF(user_AppNameToUserName(appNamePtr, userNamePtr, userNameLen) != LE_OK, "userName buffer too small."); *uidPtr = app_GetUid(appRef); *gidPtr = app_GetGid(appRef); }
//-------------------------------------------------------------------------------------------------- static void AddImportFiles ( le_cfg_IteratorRef_t cfgIter, ///< [IN] Iterator to the application config. const ImportObj_t (*importsPtr)[], ///< [IN] Imports to include in the application. size_t numImports ///< [IN] Number of import elements. ) { // Find the last node under the 'files' or 'dirs' section. size_t nodeNum = 0; char nodePath[LIMIT_MAX_PATH_BYTES]; while (1) { int n = snprintf(nodePath, sizeof(nodePath), "%zd", nodeNum); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); if (!le_cfg_NodeExists(cfgIter, nodePath)) { break; } nodeNum++; } // Start adding files/directories at the end of the current list. int i; for (i = 0; i < numImports; i++) { // Add the source. int n = snprintf(nodePath, sizeof(nodePath), "%zd/src", nodeNum + i); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_SetString(cfgIter, nodePath, (*importsPtr)[i].src); // Add the destination. n = snprintf(nodePath, sizeof(nodePath), "%zd/dest", nodeNum + i); INTERNAL_ERR_IF(n >= sizeof(nodePath), "Node name is too long."); INTERNAL_ERR_IF(n < 0, "Format error. %m"); le_cfg_SetString(cfgIter, nodePath, (*importsPtr)[i].dest); } }
//-------------------------------------------------------------------------------------------------- static const char* GetOptionValue ( const char* option, ///< [IN] The option. size_t startIndex, ///< [IN] Index of the first argument to search from. size_t endIndex ///< [IN] Index of the last argument to search to. ) { // Make the string to search for. char searchStr[LIMIT_MAX_ARGS_STR_BYTES]; int n = snprintf(searchStr, sizeof(searchStr), "%s=", option); if ( (n < 0) || (n >= sizeof(searchStr)) ) { INTERNAL_ERR("Option string is too long."); } // Search the list of command line arguments for the specified option. size_t i = startIndex; for (; i <= endIndex; i++) { const char* argPtr = le_arg_GetArg(i); INTERNAL_ERR_IF(argPtr == NULL, "Wrong number of arguments."); char* subStr = strstr(argPtr, searchStr); if ( (subStr != NULL) && (subStr == argPtr) ) { // The argPtr begins with the searchStr. The remainder of the argBuf is the value string. const char* valueStr = argPtr + strlen(searchStr); return valueStr; } } return NULL; }
//-------------------------------------------------------------------------------------------------- static app_Ref_t GetAppRef ( const char * appNamePtr ///< [IN] App name ) { app_Ref_t appRef; char configPath[LIMIT_MAX_PATH_BYTES] = ""; INTERNAL_ERR_IF(le_path_Concat("/", configPath, LIMIT_MAX_PATH_BYTES, "apps", appNamePtr, (char*)NULL) != LE_OK, "Buffer size too small."); app_Init(); appRef = app_Create(configPath); LE_FATAL_IF(appRef == NULL, "There was an error when getting app info for '%s'.", appNamePtr); return appRef; }
//-------------------------------------------------------------------------------------------------- static bool GetFlagArg ( const char* flag, ///< [IN] The flag to find size_t startIndex, ///< [IN] Index of the first argument to search from. size_t endIndex ///< [IN] Index of the last argument to search to. ) { // Search the list of command line arguments for the specified option. size_t i = startIndex; for (; i <= endIndex; i++) { const char* argPtr = le_arg_GetArg(i); INTERNAL_ERR_IF(argPtr == NULL, "Wrong number of arguments."); if (strcmp(argPtr, flag) == 0) { return true; } } return false; }
//-------------------------------------------------------------------------------------------------- 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); }