mirror of
https://gitee.com/openharmony/startup_init
synced 2025-02-25 23:15:38 +00:00
系统参数支持类型定义
Signed-off-by: Mupceet <laiguizhong@huawei.com>
This commit is contained in:
parent
c08d446780
commit
188053b6a3
@ -84,46 +84,6 @@ INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder
|
||||
return *paramHolder;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int StringToLL(const char *str, long long int *out)
|
||||
{
|
||||
const char* s = str;
|
||||
while (isspace(*s)) {
|
||||
s++;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
int positiveHex = (len > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'));
|
||||
int negativeHex = (len > 2 && s[0] == '-' && s[1] == '0' && (s[2] == 'x' || s[2] == 'X')); // 2: shorttest
|
||||
int base = (positiveHex || negativeHex) ? HEX : DECIMAL;
|
||||
char* end = NULL;
|
||||
errno = 0;
|
||||
*out = strtoll(s, &end, base);
|
||||
if (errno != 0) {
|
||||
return -1;
|
||||
}
|
||||
BEGET_CHECK(!(s == end || *end != '\0'), return -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out)
|
||||
{
|
||||
const char* s = str;
|
||||
while (isspace(*s)) {
|
||||
s++;
|
||||
}
|
||||
BEGET_CHECK(s[0] != '-', return -1);
|
||||
int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? HEX : DECIMAL;
|
||||
char* end = NULL;
|
||||
errno = 0;
|
||||
*out = strtoull(s, &end, base);
|
||||
if (errno != 0) {
|
||||
return -1;
|
||||
}
|
||||
BEGET_CHECK(end != s, return -1);
|
||||
BEGET_CHECK(*end == '\0', return -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API const char *GetProductModel_(void)
|
||||
{
|
||||
static const char *productModel = NULL;
|
||||
|
@ -29,13 +29,8 @@ extern "C" {
|
||||
#define HASH_LENGTH 32
|
||||
#define DEV_BUF_LENGTH 3
|
||||
#define DEV_BUF_MAX_LENGTH 1024
|
||||
#define DECIMAL 10
|
||||
#define HEX 16
|
||||
|
||||
INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder);
|
||||
|
||||
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out);
|
||||
INIT_LOCAL_API int StringToLL(const char *str, long long int *out);
|
||||
INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t len);
|
||||
|
||||
INIT_LOCAL_API const char *GetProductModel_(void);
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "beget_ext.h"
|
||||
#include "param_comm.h"
|
||||
#include "init_param.h"
|
||||
#include "init_utils.h"
|
||||
#include "sysparam_errno.h"
|
||||
#include "securec.h"
|
||||
#include "parameter.h"
|
||||
|
@ -20,6 +20,7 @@
|
||||
|
||||
#include "param_comm.h"
|
||||
#include "init_param.h"
|
||||
#include "init_utils.h"
|
||||
#include "sysparam_errno.h"
|
||||
#include "securec.h"
|
||||
#include "beget_ext.h"
|
||||
|
@ -21,6 +21,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "beget_ext.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
@ -32,6 +34,7 @@ typedef struct {
|
||||
int value;
|
||||
} InitArgInfo;
|
||||
|
||||
#define HEX_BASE 16
|
||||
#define BINARY_BASE 2
|
||||
#define OCTAL_BASE 8
|
||||
#define DECIMAL_BASE 10
|
||||
@ -76,6 +79,10 @@ int GetMapValue(const char *name, const InitArgInfo *infos, int argNum, int defV
|
||||
const InitArgInfo *GetServieStatusMap(int *size);
|
||||
uint32_t GetRandom(void);
|
||||
void OpenConsole(void);
|
||||
|
||||
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out);
|
||||
INIT_LOCAL_API int StringToLL(const char *str, long long int *out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <pwd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "param_manager.h"
|
||||
#include "param_security.h"
|
||||
@ -60,6 +61,15 @@ static void GetGroupIdByName(gid_t *gid, const char *name, uint32_t nameLen)
|
||||
// user:group:r|w
|
||||
static int GetParamDacData(ParamDacData *dacData, const char *value)
|
||||
{
|
||||
static const struct {
|
||||
const char *name;
|
||||
int value;
|
||||
} paramTypes[] = {
|
||||
{ "int", PARAM_TYPE_INT },
|
||||
{ "string", PARAM_TYPE_STRING },
|
||||
{ "bool", PARAM_TYPE_BOOL },
|
||||
};
|
||||
|
||||
if (dacData == NULL) {
|
||||
return -1;
|
||||
}
|
||||
@ -73,7 +83,19 @@ static int GetParamDacData(ParamDacData *dacData, const char *value)
|
||||
}
|
||||
GetUserIdByName(&dacData->uid, value, groupName - value);
|
||||
GetGroupIdByName(&dacData->gid, groupName + 1, mode - groupName - 1);
|
||||
dacData->mode = strtol(mode + 1, NULL, OCT_BASE);
|
||||
|
||||
dacData->paramType = PARAM_TYPE_STRING;
|
||||
char *type = strstr(mode + 1, ":");
|
||||
if (type != NULL) {
|
||||
*type = '\0';
|
||||
type++;
|
||||
for (size_t i = 0; (type != NULL) && (i < ARRAY_LENGTH(paramTypes)); i++) {
|
||||
if (strcmp(paramTypes[i].name, type) == 0) {
|
||||
dacData->paramType = paramTypes[i].value;
|
||||
}
|
||||
}
|
||||
}
|
||||
dacData->mode = (uint16_t)strtol(mode + 1, NULL, OCT_BASE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -155,6 +155,7 @@ INIT_PUBLIC_API int InitParamWorkSpace(int onlyRead)
|
||||
auditData.dacData.gid = DAC_DEFAULT_GROUP; // 2000 for shell
|
||||
auditData.dacData.uid = DAC_DEFAULT_USER; // for root
|
||||
auditData.dacData.mode = DAC_DEFAULT_MODE; // 0774 default mode
|
||||
auditData.dacData.paramType = PARAM_TYPE_STRING;
|
||||
ret = AddSecurityLabel(&auditData);
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to add default dac label");
|
||||
} else {
|
||||
|
@ -129,7 +129,53 @@ INIT_LOCAL_API int ReadParamWithCheck(const char *name, uint32_t op, ParamHandle
|
||||
return PARAM_CODE_NOT_FOUND;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value)
|
||||
INIT_LOCAL_API uint8_t GetParamValueType(const char *name)
|
||||
{
|
||||
uint32_t labelIndex = 0;
|
||||
WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
|
||||
if (space == NULL) {
|
||||
return PARAM_TYPE_STRING;
|
||||
}
|
||||
(void)FindTrieNode(space, name, strlen(name), &labelIndex);
|
||||
ParamSecurityNode *securityNode = (ParamSecurityNode *)GetTrieNode(space, labelIndex);
|
||||
if (securityNode == NULL) {
|
||||
return PARAM_TYPE_STRING;
|
||||
}
|
||||
return securityNode->type;
|
||||
}
|
||||
|
||||
static int CheckParamValueType(const char *name, const char *value, uint8_t paramType)
|
||||
{
|
||||
if (paramType == PARAM_TYPE_INT) {
|
||||
long long int temp1 = 0;
|
||||
if (strlen(value) > 1 && value[0] == '-' && StringToLL(value, &temp1) != 0) {
|
||||
PARAM_LOGE("Illegal param value %s for int", value);
|
||||
return PARAM_CODE_INVALID_VALUE;
|
||||
}
|
||||
unsigned long long int temp2 = 0;
|
||||
if (StringToULL(value, &temp2) != 0) {
|
||||
PARAM_LOGE("Illegal param value %s for int", value);
|
||||
return PARAM_CODE_INVALID_VALUE;
|
||||
}
|
||||
} else if (paramType == PARAM_TYPE_BOOL) {
|
||||
static const char *validValue[] = {
|
||||
"1", "0", "true", "false", "y", "yes", "on", "off", "n", "no"
|
||||
};
|
||||
size_t i = 0;
|
||||
for (; i < ARRAY_LENGTH(validValue); i++) {
|
||||
if (strcasecmp(validValue[i], value) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= ARRAY_LENGTH(validValue)) {
|
||||
PARAM_LOGE("Illegal param value %s for bool", value);
|
||||
return PARAM_CODE_INVALID_VALUE;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType)
|
||||
{
|
||||
if (IS_READY_ONLY(name)) {
|
||||
PARAM_CHECK(strlen(value) < PARAM_CONST_VALUE_LEN_MAX,
|
||||
@ -139,10 +185,10 @@ INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name,
|
||||
return PARAM_CODE_READ_ONLY;
|
||||
}
|
||||
} else {
|
||||
PARAM_CHECK(strlen(value) < PARAM_VALUE_LEN_MAX,
|
||||
return PARAM_CODE_INVALID_VALUE, "Illegal param value %s", value);
|
||||
PARAM_CHECK(strlen(value) < GetParamMaxLen(paramType),
|
||||
return PARAM_CODE_INVALID_VALUE, "Illegal param value %s length", value);
|
||||
}
|
||||
return 0;
|
||||
return CheckParamValueType(name, value, paramType);
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int CheckParamName(const char *name, int info)
|
||||
@ -181,13 +227,13 @@ INIT_LOCAL_API int CheckParamName(const char *name, int info)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int AddParam(WorkSpace *workSpace, const char *name, const char *value, uint32_t *dataIndex)
|
||||
static int AddParam(WorkSpace *workSpace, uint8_t type, const char *name, const char *value, uint32_t *dataIndex)
|
||||
{
|
||||
ParamTrieNode *node = AddTrieNode(workSpace, name, strlen(name));
|
||||
PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node");
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
|
||||
if (entry == NULL) {
|
||||
uint32_t offset = AddParamNode(workSpace, name, strlen(name), value, strlen(value));
|
||||
uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value));
|
||||
PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX, "Failed to allocate name %s", name);
|
||||
SaveIndex(&node->dataIndex, offset);
|
||||
long long globalCommitId = ATOMIC_LOAD_EXPLICIT(&workSpace->area->commitId, memory_order_relaxed);
|
||||
@ -255,16 +301,21 @@ INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dat
|
||||
if ((mode & LOAD_PARAM_ONLY_ADD) == LOAD_PARAM_ONLY_ADD) {
|
||||
return PARAM_CODE_READ_ONLY;
|
||||
}
|
||||
ret = CheckParamValue(node, name, value);
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
|
||||
PARAM_CHECK(entry != NULL, return PARAM_CODE_REACHED_MAX,
|
||||
"Failed to update param value %s %u", name, node->dataIndex);
|
||||
// use save type to check value
|
||||
ret = CheckParamValue(node, name, value, entry->type);
|
||||
PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
|
||||
PARAMSPACE_AREA_RW_LOCK(workSpace);
|
||||
ret = UpdateParam(workSpace, &node->dataIndex, name, value);
|
||||
PARAMSPACE_AREA_RW_UNLOCK(workSpace);
|
||||
} else {
|
||||
ret = CheckParamValue(node, name, value);
|
||||
uint8_t type = GetParamValueType(name);
|
||||
ret = CheckParamValue(node, name, value, type);
|
||||
PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
|
||||
PARAMSPACE_AREA_RW_LOCK(workSpace);
|
||||
ret = AddParam((WorkSpace *)workSpace, name, value, dataIndex);
|
||||
ret = AddParam((WorkSpace *)workSpace, type, name, value, dataIndex);
|
||||
PARAMSPACE_AREA_RW_UNLOCK(workSpace);
|
||||
}
|
||||
return ret;
|
||||
@ -288,7 +339,7 @@ INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData)
|
||||
PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node %s", auditData->name);
|
||||
uint32_t offset = node->labelIndex;
|
||||
if (node->labelIndex == 0) { // can not support update for label
|
||||
offset = AddParamSecruityNode(workSpace, auditData);
|
||||
offset = AddParamSecurityNode(workSpace, auditData);
|
||||
PARAM_CHECK(offset != 0, return PARAM_CODE_REACHED_MAX, "Failed to add label");
|
||||
SaveIndex(&node->labelIndex, offset);
|
||||
} else {
|
||||
@ -297,11 +348,13 @@ INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData)
|
||||
label->mode = auditData->dacData.mode;
|
||||
label->uid = auditData->dacData.uid;
|
||||
label->gid = auditData->dacData.gid;
|
||||
label->type = auditData->dacData.paramType & PARAM_TYPE_MASK;
|
||||
#endif
|
||||
PARAM_LOGE("Error, repeat to add label for name %s", auditData->name);
|
||||
}
|
||||
PARAM_LOGV("AddSecurityLabel label %d gid %d uid %d mode %o name: %s", offset,
|
||||
auditData->dacData.gid, auditData->dacData.uid, auditData->dacData.mode, auditData->name);
|
||||
PARAM_LOGV("AddSecurityLabel label %d gid %d uid %d mode %o type:%d name: %s", offset,
|
||||
auditData->dacData.gid, auditData->dacData.uid, auditData->dacData.mode,
|
||||
auditData->dacData.paramType, auditData->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -323,16 +323,11 @@ INIT_LOCAL_API int TraversalTrieNode(const WorkSpace *workSpace,
|
||||
return 0;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData)
|
||||
INIT_LOCAL_API uint32_t AddParamSecurityNode(WorkSpace *workSpace, const ParamAuditData *auditData)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
|
||||
PARAM_CHECK(auditData != NULL && auditData->name != NULL, return 0, "Invalid auditData");
|
||||
#ifdef PARAM_SUPPORT_SELINUX
|
||||
const uint32_t labelLen = strlen(auditData->label);
|
||||
uint32_t realLen = sizeof(ParamSecurityNode) + PARAM_ALIGN(labelLen + 1);
|
||||
#else
|
||||
uint32_t realLen = sizeof(ParamSecurityNode);
|
||||
#endif
|
||||
PARAM_CHECK((workSpace->area->currOffset + realLen) < workSpace->area->dataSize, return 0,
|
||||
"Failed to allocate currOffset %u, dataSize %u datalen %u",
|
||||
workSpace->area->currOffset, workSpace->area->dataSize, realLen);
|
||||
@ -340,22 +335,14 @@ INIT_LOCAL_API uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAu
|
||||
node->uid = auditData->dacData.uid;
|
||||
node->gid = auditData->dacData.gid;
|
||||
node->mode = auditData->dacData.mode;
|
||||
node->length = 0;
|
||||
#ifdef PARAM_SUPPORT_SELINUX
|
||||
if (labelLen != 0) {
|
||||
int ret = ParamMemcpy(node->data, labelLen, auditData->label, labelLen);
|
||||
PARAM_CHECK(ret == 0, return 0, "Failed to copy key");
|
||||
node->data[labelLen] = '\0';
|
||||
node->length = labelLen;
|
||||
}
|
||||
#endif
|
||||
node->type = auditData->dacData.paramType & PARAM_TYPE_MASK;
|
||||
uint32_t offset = workSpace->area->currOffset;
|
||||
workSpace->area->currOffset += realLen;
|
||||
workSpace->area->securityNodeCount++;
|
||||
return offset;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace,
|
||||
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type,
|
||||
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
|
||||
@ -366,7 +353,7 @@ INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace,
|
||||
if ((valueLen > PARAM_VALUE_LEN_MAX) || IS_READY_ONLY(key)) {
|
||||
realLen += keyLen + valueLen;
|
||||
} else {
|
||||
realLen += keyLen + PARAM_VALUE_LEN_MAX;
|
||||
realLen += keyLen + GetParamMaxLen(type);
|
||||
}
|
||||
realLen = PARAM_ALIGN(realLen);
|
||||
PARAM_CHECK((workSpace->area->currOffset + realLen) < workSpace->area->dataSize, return 0,
|
||||
@ -376,6 +363,7 @@ INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace,
|
||||
ParamNode *node = (ParamNode *)(workSpace->area->data + workSpace->area->currOffset);
|
||||
ATOMIC_INIT(&node->commitId, 0);
|
||||
|
||||
node->type = type;
|
||||
node->keyLength = keyLen;
|
||||
node->valueLength = valueLen;
|
||||
int ret = ParamSprintf(node->data, realLen, "%s=%s", key, value);
|
||||
@ -417,4 +405,15 @@ INIT_LOCAL_API ParamTrieNode *FindTrieNode(WorkSpace *workSpace,
|
||||
return NULL;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
uint32_t GetParamMaxLen(uint8_t type)
|
||||
{
|
||||
static const uint32_t typeLengths[] = {
|
||||
PARAM_VALUE_LEN_MAX, 32, 8 // 8 max bool length 32 max int length
|
||||
};
|
||||
if (type >= ARRAY_LENGTH(typeLengths)) {
|
||||
return PARAM_VALUE_LEN_MAX;
|
||||
}
|
||||
return typeLengths[type];
|
||||
}
|
@ -84,8 +84,9 @@ INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr);
|
||||
INIT_LOCAL_API WorkSpace *GetWorkSpace(const char *name);
|
||||
INIT_LOCAL_API ParamTrieNode *GetTrieNodeByHandle(ParamHandle handle);
|
||||
INIT_LOCAL_API int ReadParamWithCheck(const char *name, uint32_t op, ParamHandle *handle);
|
||||
INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value);
|
||||
INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType);
|
||||
INIT_LOCAL_API int CheckParamName(const char *name, int paramInfo);
|
||||
INIT_LOCAL_API uint8_t GetParamValueType(const char *name);
|
||||
|
||||
INIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value);
|
||||
INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd);
|
||||
|
@ -74,7 +74,8 @@ typedef struct {
|
||||
pid_t pid;
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
uint32_t mode;
|
||||
uint16_t mode;
|
||||
uint8_t paramType;
|
||||
} ParamDacData;
|
||||
|
||||
typedef struct {
|
||||
|
@ -50,9 +50,15 @@ typedef struct {
|
||||
#define PARAM_FLAGS_WAITED 0x20000000
|
||||
#define PARAM_FLAGS_COMMITID 0x0000ffff
|
||||
|
||||
#define PARAM_TYPE_MASK 0x0f
|
||||
#define PARAM_TYPE_STRING 0x00
|
||||
#define PARAM_TYPE_INT 0x01
|
||||
#define PARAM_TYPE_BOOL 0x02
|
||||
|
||||
typedef struct {
|
||||
ATOMIC_UINT32 commitId;
|
||||
uint16_t keyLength;
|
||||
uint8_t type;
|
||||
uint8_t keyLength;
|
||||
uint16_t valueLength;
|
||||
char data[0];
|
||||
} ParamNode;
|
||||
@ -61,7 +67,8 @@ typedef struct {
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
uint16_t mode;
|
||||
uint16_t length;
|
||||
uint8_t type;
|
||||
uint8_t length;
|
||||
char data[0];
|
||||
} ParamSecurityNode;
|
||||
|
||||
@ -107,9 +114,12 @@ typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieN
|
||||
INIT_LOCAL_API int TraversalTrieNode(const WorkSpace *workSpace,
|
||||
const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, const void *cookie);
|
||||
|
||||
INIT_LOCAL_API uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
|
||||
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace,
|
||||
INIT_LOCAL_API uint32_t AddParamSecurityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
|
||||
INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type,
|
||||
const char *key, uint32_t keyLen, const char *value, uint32_t valueLen);
|
||||
|
||||
uint32_t GetParamMaxLen(uint8_t type);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
|
@ -306,7 +306,7 @@ INIT_INNER_API int CheckParameterSet(const char *name,
|
||||
PARAM_CHECK(srcLabel != NULL && ctrlService != NULL, return -1, "Invalid param ");
|
||||
int ret = CheckParamName(name, 0);
|
||||
PARAM_CHECK(ret == 0, return ret, "Illegal param name %s", name);
|
||||
ret = CheckParamValue(NULL, name, value);
|
||||
ret = CheckParamValue(NULL, name, value, GetParamValueType(name));
|
||||
PARAM_CHECK(ret == 0, return ret, "Illegal param value %s", value);
|
||||
*ctrlService = 0;
|
||||
|
||||
|
@ -586,3 +586,47 @@ void OpenConsole(void)
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int StringToLL(const char *str, long long int *out)
|
||||
{
|
||||
INIT_ERROR_CHECK(str != NULL && out != NULL, return -1, "Invalid parament");
|
||||
const char *s = str;
|
||||
while (isspace(*s)) {
|
||||
s++;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
int positiveHex = (len > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'));
|
||||
int negativeHex = (len > 2 && s[0] == '-' && s[1] == '0' && (s[2] == 'x' || s[2] == 'X')); // 2: shorttest
|
||||
int base = (positiveHex || negativeHex) ? HEX_BASE : DECIMAL_BASE;
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
*out = strtoll(s, &end, base);
|
||||
if (errno != 0) {
|
||||
INIT_LOGE("StringToLL %s err = %d", str, errno);
|
||||
return -1;
|
||||
}
|
||||
BEGET_CHECK(!(s == end || *end != '\0'), return -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out)
|
||||
{
|
||||
INIT_ERROR_CHECK(str != NULL && out != NULL, return -1, "Invalid parament");
|
||||
const char *s = str;
|
||||
while (isspace(*s)) {
|
||||
s++;
|
||||
}
|
||||
BEGET_CHECK(s[0] != '-', return -1);
|
||||
int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? HEX_BASE : DECIMAL_BASE;
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
*out = strtoull(s, &end, base);
|
||||
if (errno != 0) {
|
||||
INIT_LOGE("StringToULL %s err = %d", str, errno);
|
||||
return -1;
|
||||
}
|
||||
BEGET_CHECK(end != s, return -1);
|
||||
BEGET_CHECK(*end == '\0', return -1);
|
||||
return 0;
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "init_param.h"
|
||||
#include "init_utils.h"
|
||||
#include "parameter.h"
|
||||
#include "sysparam_errno.h"
|
||||
#include "param_comm.h"
|
||||
|
Loading…
x
Reference in New Issue
Block a user