提交 ab0b3090 编写于 作者: M Mupceet

decodeuidgid0622

Signed-off-by: NMupceet <laiguizhong@huawei.com>
上级 df7c96b8
......@@ -60,6 +60,7 @@ void WaitForFile(const char *source, unsigned int maxSecond);
size_t WriteAll(int fd, const char *buffer, size_t size);
char *GetRealPath(const char *source);
int StringToInt(const char *str, int defaultValue);
int StringToUint(const char *name, unsigned int *value);
int MakeDirRecursive(const char *dir, mode_t mode);
void CheckAndCreateDir(const char *fileName);
int CheckAndCreatFile(const char *file, mode_t mode);
......
......@@ -18,6 +18,7 @@
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdlib.h>
......@@ -48,43 +49,74 @@ float ConvertMicrosecondToSecond(int x)
return ((x / THOUSAND_UNIT_INT) / THOUSAND_UNIT_FLOAT);
}
static uid_t DecodeId(const char *name, bool isUid)
static bool CheckDigit(const char *name)
{
#ifndef __LITEOS_M__
INIT_CHECK_RETURN_VALUE(name != NULL, -1);
int digitFlag = 1;
size_t nameLen = strlen(name);
for (unsigned int i = 0; i < nameLen; ++i) {
if (isalpha(name[i])) {
digitFlag = 0;
break;
for (size_t i = 0; i < nameLen; ++i) {
if (!isdigit(name[i])) {
return false;
}
}
if (digitFlag) {
errno = 0;
uid_t result = strtoul(name, 0, DECIMAL_BASE);
INIT_CHECK_RETURN_VALUE(errno == 0, -1);
return result;
} else {
struct passwd *userInf = getpwnam(name);
if (userInf == NULL) {
return true;
}
int StringToUint(const char *name, unsigned int *value)
{
errno = 0;
*value = (unsigned int)strtoul(name, 0, DECIMAL_BASE);
INIT_CHECK_RETURN_VALUE(errno == 0, -1);
return 0;
}
uid_t DecodeUid(const char *name)
{
#ifndef __LITEOS_M__
INIT_CHECK_RETURN_VALUE(name != NULL, -1);
uid_t uid = -1;
if (CheckDigit(name)) {
if (!StringToUint(name, &uid)) {
return uid;
} else {
INIT_LOGE("Failed to decode uid");
return -1;
}
return isUid ? userInf->pw_uid : userInf->pw_gid;
}
struct passwd *p = getpwnam(name);
if (p == NULL) {
INIT_LOGE("Failed to decode uid");
return -1;
}
return p->pw_uid;
#else
return -1;
#endif
}
uid_t DecodeUid(const char *name)
{
return DecodeId(name, true);
}
gid_t DecodeGid(const char *name)
{
return DecodeId(name, false);
#ifndef __LITEOS_M__
INIT_CHECK_RETURN_VALUE(name != NULL, -1);
gid_t gid = -1;
if (CheckDigit(name)) {
if (!StringToUint(name, &gid)) {
return gid;
} else {
INIT_LOGE("Failed to decode gid");
return -1;
}
}
struct group *data = NULL;
while ((data = getgrent()) != NULL) {
if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) {
gid = data->gr_gid;
break;
}
}
endgrent();
return gid;
#else
return -1;
#endif
}
char *ReadFileToBuf(const char *configFile)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册