提交 258f8f85 编写于 作者: M Mupceet

Merge branch 'master' of gitee.com:openharmony/startup_init_lite into initservice

Signed-off-by: NMupceet <laiguizhong@huawei.com>
......@@ -61,7 +61,7 @@ static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint3
BEGET_ERROR_CHECK(strl != NULL, return, "pty slave path %s is invaild", realPath);
int fd = open(realPath, O_RDWR);
free(realPath);
BEGET_ERROR_CHECK(fd >= 0, return, "Failed open %s, err=%d", realPath, errno);
BEGET_ERROR_CHECK(fd >= 0, return, "Failed open %s, err=%d", msg->ptyName, errno);
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO); // Redirect fd to 0, 1, 2
......
......@@ -22,8 +22,9 @@
#include "beget_ext.h"
#include "init_utils.h"
#include "securec.h"
#include "init_param.h"
#include "parameter.h"
#include "securec.h"
static int StartProcess(const char *name, const char *extArgv[], int extArgc)
{
......@@ -100,15 +101,8 @@ static int GetCurrentServiceStatus(const char *serviceName, ServiceStatus *statu
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
char paramValue[PARAM_VALUE_LEN_MAX] = {0};
unsigned int valueLen = PARAM_VALUE_LEN_MAX;
if (SystemGetParameter(paramName, paramValue, &valueLen) != 0) {
BEGET_LOGE("Failed get paramName.");
return -1;
}
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
*status = GetMapValue(paramValue, statusMap, size, SERVICE_IDLE);
uint32_t value = GetUintParameter(paramName, SERVICE_IDLE);
*status = (ServiceStatus)value;
return 0;
}
......@@ -184,13 +178,7 @@ int ServiceControl(const char *serviceName, int action)
int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int waitTimeout)
{
char *state = NULL;
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
if (((int)status < size) && (statusMap[status].value == (int)status)) {
state = statusMap[status].name;
}
if (serviceName == NULL || state == NULL || waitTimeout <= 0) {
if (serviceName == NULL || waitTimeout <= 0) {
BEGET_LOGE("Service wait failed, service name is null or status invalid %d", status);
return -1;
}
......@@ -200,7 +188,12 @@ int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int wait
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemWaitParameter(paramName, state, waitTimeout) != 0) {
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)status) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemWaitParameter(paramName, value, waitTimeout) != 0) {
BEGET_LOGE("Wait param for %s failed.", paramName);
return -1;
}
......@@ -220,7 +213,12 @@ int ServiceSetReady(const char *serviceName)
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemSetParameter(paramName, "ready") != 0) {
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)SERVICE_READY) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemSetParameter(paramName, value) != 0) {
BEGET_LOGE("Set param for %s failed.", paramName);
return -1;
}
......
......@@ -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"
......
......@@ -33,4 +33,4 @@ debug.bytrace. = root:system:0775
persist.distributed_hardware.device_manager. = system:system:0775
bootevent. = samgr:samgr:0777
hw_sc. = root:root:0777
startup.service.ctl. = root:root:0775:int
startup.service.ctl. = system:servicectrl:0775:int
......@@ -78,6 +78,10 @@ int StringReplaceChr(char *strl, char oldChr, char newChr);
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
}
......
......@@ -22,15 +22,28 @@
static void DoAsyncEvent_(const LoopHandle loopHandle, AsyncEventTask *asyncTask)
{
LE_CHECK(loopHandle != NULL && asyncTask != NULL, return, "Invalid parameters");
LE_Buffer *buffer = GetFirstBuffer(&asyncTask->stream);
while (buffer != NULL) {
ListNode tmpHdr;
OH_ListInit(&tmpHdr);
StreamTask *task = &asyncTask->stream;
LoopMutexLock(&task->mutex);
tmpHdr.next = task->buffHead.next;
tmpHdr.prev = task->buffHead.prev;
task->buffHead.next->prev = &tmpHdr;
task->buffHead.prev->next = &tmpHdr;
OH_ListInit(&task->buffHead);
LoopMutexUnlock(&task->mutex);
ListNode *node = tmpHdr.next;
while (node != &tmpHdr) {
LE_Buffer *buffer = ListEntry(node, LE_Buffer, node);
uint64_t eventId = *(uint64_t*)(buffer->data);
if (asyncTask->processAsyncEvent) {
asyncTask->processAsyncEvent((TaskHandle)asyncTask, eventId,
(uint8_t *)(buffer->data + sizeof(uint64_t)), buffer->dataSize);
}
FreeBuffer(loopHandle, &asyncTask->stream, buffer);
buffer = GetFirstBuffer(&asyncTask->stream);
OH_ListRemove(&buffer->node);
free(buffer);
node = tmpHdr.next;
}
}
#ifdef STARTUP_INIT_TEST
......
......@@ -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];
}
\ No newline at end of file
......@@ -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;
......
......@@ -552,3 +552,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;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册