mirror of
https://gitee.com/openharmony/startup_init
synced 2025-02-17 02:18:39 +00:00
!3349 提供cmdline精准匹配接口
Merge pull request !3349 from duxianzhi/cmdline1107
This commit is contained in:
commit
ddac4bcd1f
@ -23,14 +23,14 @@
|
||||
bool IsOverlayEnable(void)
|
||||
{
|
||||
char oemMode[MAX_BUFFER_LEN] = {0};
|
||||
int ret = GetParameterFromCmdLine("oemmode", oemMode, MAX_BUFFER_LEN);
|
||||
int ret = GetExactParameterFromCmdLine("oemmode", oemMode, MAX_BUFFER_LEN, EMPTY_VALUE);
|
||||
if (ret) {
|
||||
BEGET_LOGE("Failed get oenmode from cmdline.");
|
||||
return false;
|
||||
}
|
||||
|
||||
char buildvariant[MAX_BUFFER_LEN] = {0};
|
||||
ret = GetParameterFromCmdLine("buildvariant", buildvariant, MAX_BUFFER_LEN);
|
||||
ret = GetExactParameterFromCmdLine("buildvariant", buildvariant, MAX_BUFFER_LEN, EMPTY_VALUE);
|
||||
if (ret) {
|
||||
BEGET_LOGE("Failed get buildvariant from cmdline.");
|
||||
return false;
|
||||
|
@ -564,7 +564,7 @@ int FsHvbGetValueFromCmdLine(char *val, size_t size, const char *key)
|
||||
FS_HVB_RETURN_ERR_IF_NULL(val);
|
||||
FS_HVB_RETURN_ERR_IF_NULL(key);
|
||||
|
||||
return GetParameterFromCmdLine(key, val, size);
|
||||
return GetExactParameterFromCmdLine(key, val, size, EMPTY_VALUE);
|
||||
}
|
||||
|
||||
bool CheckAndGetExt4Size(const char *headerBuf, uint64_t *imageSize, const char* image)
|
||||
|
@ -58,6 +58,10 @@ typedef struct {
|
||||
#define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
|
||||
#define BOOT_CMD_LINE STARTUP_INIT_UT_PATH"/proc/cmdline"
|
||||
|
||||
#define FIRST_VALUE "First_Value" // 取第一个匹配值
|
||||
#define LAST_VALUE "Last_Value" // 取最后一个匹配值
|
||||
#define EMPTY_VALUE "EMPTY_VALUE" // 不取任何一个
|
||||
|
||||
uid_t DecodeUid(const char *name);
|
||||
gid_t DecodeGid(const char *name);
|
||||
char *ReadFileToBuf(const char *configFile);
|
||||
@ -110,6 +114,21 @@ void CloseStdio(void);
|
||||
int GetServiceGroupIdByPid(pid_t pid, gid_t *gids, uint32_t gidSize);
|
||||
int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen);
|
||||
|
||||
/**
|
||||
* @brief Get param value from /proc/cmdline by exact match
|
||||
*
|
||||
* @param paramName the name of param
|
||||
* @param value the array to receive the value of paramName
|
||||
* @param valueLen the length of value
|
||||
* @param conflictStrategy the strategy of conflicts, here are some options available
|
||||
* FIRST_VALUE: select the first value
|
||||
* LAST_VALUE: select the last value
|
||||
* EMPTY_VALUE: return empty value
|
||||
* "A<B...<C": assign priorities
|
||||
* @return return 0 if succeed; return -1 if not found; return 1 if conflicted.
|
||||
*/
|
||||
int GetExactParameterFromCmdLine(const char *paramName, char *value, size_t valueLen, const char *conflictStrategy);
|
||||
|
||||
/**
|
||||
* @brief Get string index from a string array
|
||||
*
|
||||
|
@ -721,6 +721,144 @@ char *TrimHead(char *str, char c)
|
||||
return head;
|
||||
}
|
||||
|
||||
static const char *GetCmdlineValueByName(const char *name, const char *buffer, const char *endData)
|
||||
{
|
||||
const char *tmp = buffer;
|
||||
do {
|
||||
tmp = strstr(tmp, name);
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (tmp > buffer && *(tmp - 1) != ' ') {
|
||||
tmp++;
|
||||
continue;
|
||||
}
|
||||
tmp = tmp + strlen(name);
|
||||
if (tmp >= endData) {
|
||||
return NULL;
|
||||
}
|
||||
if (*tmp == '=') {
|
||||
tmp++;
|
||||
return tmp;
|
||||
}
|
||||
} while (tmp < endData);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int GetValuePriority(const char *str, const char *value)
|
||||
{
|
||||
if (str == NULL || value == NULL || *value == '\0') {
|
||||
return -1;
|
||||
}
|
||||
const char *endData = str + strlen(str);
|
||||
const char *start = str;
|
||||
const char *tmp = str;
|
||||
int len = strlen(value);
|
||||
while (tmp < endData) {
|
||||
tmp = strstr(start, "<");
|
||||
if (tmp == NULL) {
|
||||
tmp = endData;
|
||||
}
|
||||
if (tmp - start == len && strncmp(start, value, len) == 0) {
|
||||
return tmp - str;
|
||||
}
|
||||
start = tmp + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int CopyCmdlineValue(const char *tmp, const char *endData, char *value, int length, int defaultValue)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < length && tmp < endData && *tmp != ' ') {
|
||||
value[i++] = *tmp;
|
||||
tmp++;
|
||||
}
|
||||
|
||||
if (i >= length) {
|
||||
BEGET_LOGE("value is too long");
|
||||
for (int j = 0; j < length; ++j) {
|
||||
value[j] = '\0';
|
||||
}
|
||||
return defaultValue ? defaultValue : -1;
|
||||
}
|
||||
value[i] = '\0';
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
static int DealConflictPriority(const char *name, const char *tmp, char *value, int length, const char *str)
|
||||
{
|
||||
if (length <= 0 || length > CMDLINE_VALUE_LEN_MAX) {
|
||||
BEGET_LOGE("length is invalid");
|
||||
return 1;
|
||||
}
|
||||
const char *endData = tmp + strlen(tmp);
|
||||
char *valueTmp = (char *)calloc(length, sizeof(char));
|
||||
INIT_ERROR_CHECK(valueTmp != NULL, return 1, "Failed to calloc valueTmp");
|
||||
while (CopyCmdlineValue(tmp, endData, valueTmp, length, 0) == 0) {
|
||||
if (GetValuePriority(str, valueTmp) <= GetValuePriority(str, value)) {
|
||||
tmp = GetCmdlineValueByName(name, tmp, endData);
|
||||
if (tmp == NULL) {
|
||||
free(valueTmp);
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
int i = 0;
|
||||
while (*(valueTmp + i) != '\0') {
|
||||
value[i] = *(valueTmp + i);
|
||||
i++;
|
||||
}
|
||||
for (int j = i; j < length; ++j) {
|
||||
value[j] = '\0';
|
||||
}
|
||||
tmp = GetCmdlineValueByName(name, tmp, endData);
|
||||
if (tmp == NULL) {
|
||||
free(valueTmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
free(valueTmp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int GetExactProcCmdlineValue(const char *name, const char *buffer, char *value, int length, const char *strategy)
|
||||
{
|
||||
INIT_ERROR_CHECK(name != NULL && buffer != NULL && value != NULL, return -1, "Failed get parameters");
|
||||
const char *endData = buffer + strlen(buffer);
|
||||
const char *tmp = GetCmdlineValueByName(name, buffer, endData);
|
||||
if (tmp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *temp = GetCmdlineValueByName(name, tmp, endData);
|
||||
if (temp == NULL) { // 无冲突
|
||||
return CopyCmdlineValue(tmp, endData, value, length, 0);
|
||||
} else if (strcmp(strategy, FIRST_VALUE) == 0) { // 取第一个值
|
||||
return CopyCmdlineValue(tmp, endData, value, length, 1);
|
||||
} else if (strcmp(strategy, LAST_VALUE) == 0) { // 取最后一个值
|
||||
do {
|
||||
tmp = temp;
|
||||
temp = GetCmdlineValueByName(name, tmp, endData);
|
||||
} while (temp != NULL);
|
||||
return CopyCmdlineValue(tmp, endData, value, length, 1);
|
||||
} else if (strcmp(strategy, EMPTY_VALUE) == 0) { // 不取任何一个值
|
||||
return 1;
|
||||
} else { // 处理自定义优先级
|
||||
return DealConflictPriority(name, tmp, value, length, strategy);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int GetExactParameterFromCmdLine(const char *paramName, char *value, size_t valueLen, const char *conflictStrategy)
|
||||
{
|
||||
char *buffer = ReadFileData(BOOT_CMD_LINE);
|
||||
BEGET_ERROR_CHECK(buffer != NULL, return -1, "Failed to read /proc/cmdline");
|
||||
int ret = GetExactProcCmdlineValue(paramName, buffer, value, valueLen, conflictStrategy);
|
||||
free(buffer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int GetParameterFromCmdLine(const char *paramName, char *value, size_t valueLen)
|
||||
{
|
||||
char *buffer = ReadFileData(BOOT_CMD_LINE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user