!301 修复SysOpenat返回的文件句柄不正确的问题

Merge pull request !301 from JING/openat
This commit is contained in:
openharmony_ci 2021-06-09 13:50:40 +08:00 committed by Gitee
commit 3bf8cbc728
1 changed files with 19 additions and 2 deletions

View File

@ -1556,6 +1556,7 @@ int SysFtruncate64(int fd, off64_t length)
int SysOpenat(int dirfd, const char *path, int oflags, ...) int SysOpenat(int dirfd, const char *path, int oflags, ...)
{ {
int ret; int ret;
int procFd;
char *pathRet = NULL; char *pathRet = NULL;
mode_t mode; mode_t mode;
#ifdef LOSCFG_FILE_MODE #ifdef LOSCFG_FILE_MODE
@ -1571,10 +1572,16 @@ int SysOpenat(int dirfd, const char *path, int oflags, ...)
if (path != NULL) { if (path != NULL) {
ret = UserPathCopy(path, &pathRet); ret = UserPathCopy(path, &pathRet);
if (ret != 0) { if (ret != 0) {
goto OUT; return ret;
} }
} }
procFd = AllocProcessFd();
if (procFd < 0) {
ret = -EMFILE;
goto ERROUT;
}
if (dirfd != AT_FDCWD) { if (dirfd != AT_FDCWD) {
/* Process fd convert to system global fd */ /* Process fd convert to system global fd */
dirfd = GetAssociatedSystemFd(dirfd); dirfd = GetAssociatedSystemFd(dirfd);
@ -1583,12 +1590,22 @@ int SysOpenat(int dirfd, const char *path, int oflags, ...)
ret = do_open(dirfd, (path ? pathRet : NULL), oflags, mode); ret = do_open(dirfd, (path ? pathRet : NULL), oflags, mode);
if (ret < 0) { if (ret < 0) {
ret = -get_errno(); ret = -get_errno();
goto ERROUT;
} }
OUT: AssociateSystemFd(procFd, ret);
if (pathRet != NULL) { if (pathRet != NULL) {
(void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet); (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
} }
return procFd;
ERROUT:
if (pathRet != NULL) {
(void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
}
if (procFd >= 0) {
FreeProcessFd(procFd);
}
return ret; return ret;
} }