fs_syscall.c 61.9 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

W
wangchen 已提交
32
#include "syscall_pub.h"
W
wenjun 已提交
33 34 35 36 37
#ifdef LOSCFG_FS_VFS
#include "errno.h"
#include "unistd.h"
#include "fs/fd_table.h"
#include "fs/file.h"
W
wangchen 已提交
38
#include "fs/fs.h"
W
wenjun 已提交
39 40 41 42 43 44 45
#include "fs/fs_operation.h"
#include "sys/mount.h"
#include "los_task_pri.h"
#include "sys/utsname.h"
#include "sys/uio.h"
#include "poll.h"
#include "sys/prctl.h"
L
lnlan 已提交
46
#include "epoll.h"
47
#ifdef LOSCFG_KERNEL_DYNLOAD
W
wenjun 已提交
48
#include "los_exec_elf.h"
49
#endif
W
wenjun 已提交
50 51 52 53 54 55 56 57
#include "los_syscall.h"
#include "dirent.h"
#include "user_copy.h"
#include "los_vm_map.h"
#include "los_memory.h"
#include "los_strncpy_from_user.h"
#include "capability_type.h"
#include "capability_api.h"
M
mucor 已提交
58
#include "sys/statfs.h"
W
wenjun 已提交
59

M
mamingshuai 已提交
60
#define HIGH_SHIFT_BIT 32
W
wangchen 已提交
61
#define TIMESPEC_TIMES_NUM  2
M
mamingshuai 已提交
62

W
wangchen 已提交
63
static int CheckNewAttrTime(struct IATTR *attr, struct timespec times[TIMESPEC_TIMES_NUM])
W
wenjun 已提交
64
{
W
wangchen 已提交
65 66
    int ret = ENOERR;
    struct timespec stp = {0};
W
wenjun 已提交
67

W
wangchen 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (times) {
        if (times[0].tv_nsec == UTIME_OMIT) {
            attr->attr_chg_valid &= ~CHG_ATIME;
        } else if (times[0].tv_nsec == UTIME_NOW) {
            ret = clock_gettime(CLOCK_REALTIME, &stp);
            if (ret < 0) {
                return -get_errno();
            }
            attr->attr_chg_atime = (unsigned int)stp.tv_sec;
            attr->attr_chg_valid |= CHG_ATIME;
        } else {
            attr->attr_chg_atime = (unsigned int)times[0].tv_sec;
            attr->attr_chg_valid |= CHG_ATIME;
        }

        if (times[1].tv_nsec == UTIME_OMIT) {
            attr->attr_chg_valid &= ~CHG_MTIME;
        } else if (times[1].tv_nsec == UTIME_NOW) {
            ret = clock_gettime(CLOCK_REALTIME, &stp);
            if (ret < 0) {
                return -get_errno();
            }
            attr->attr_chg_mtime = (unsigned int)stp.tv_sec;
            attr->attr_chg_valid |= CHG_MTIME;
        } else {
            attr->attr_chg_mtime = (unsigned int)times[1].tv_sec;
            attr->attr_chg_valid |= CHG_MTIME;
        }
    } else {
        ret = clock_gettime(CLOCK_REALTIME, &stp);
        if (ret < 0) {
            return -get_errno();
        }
        attr->attr_chg_atime = (unsigned int)stp.tv_sec;
        attr->attr_chg_mtime = (unsigned int)stp.tv_sec;
        attr->attr_chg_valid |= CHG_ATIME;
        attr->attr_chg_valid |= CHG_MTIME;
W
wenjun 已提交
105 106
    }

W
wangchen 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    return ret;
}

static int GetFullpathNull(int fd, const char *path, char **filePath)
{
    int ret;
    char *fullPath = NULL;
    struct file *file = NULL;

    if ((fd != AT_FDCWD) && (path == NULL)) {
        fd = GetAssociatedSystemFd(fd);
        ret = fs_getfilep(fd, &file);
        if (ret < 0) {
            return -get_errno();
        }
F
Far 已提交
122 123 124 125
        fullPath = strdup(file->f_path);
        if (fullPath == NULL) {
            ret = -ENOMEM;
        }
W
wangchen 已提交
126 127 128 129 130
    } else {
        ret = GetFullpath(fd, path, &fullPath);
        if (ret < 0) {
            return ret;
        }
W
wenjun 已提交
131 132
    }

W
wangchen 已提交
133 134
    *filePath = fullPath;
    return ret;
W
wenjun 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

static int UserIovItemCheck(const struct iovec *iov, const int iovcnt)
{
    int i;
    for (i = 0; i < iovcnt; ++i) {
        if (iov[i].iov_len == 0) {
            continue;
        }

        if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)iov[i].iov_base, iov[i].iov_len)) {
            return i;
        }
    }
    return iovcnt;
}

static int UserIovCopy(struct iovec **iovBuf, const struct iovec *iov, const int iovcnt, int *valid_iovcnt)
{
    int ret;
    int bufLen = iovcnt * sizeof(struct iovec);
156
    if (bufLen < 0) {
W
wenjun 已提交
157 158 159 160 161 162 163 164 165
        return -EINVAL;
    }

    *iovBuf = (struct iovec*)LOS_MemAlloc(OS_SYS_MEM_ADDR, bufLen);
    if (*iovBuf == NULL) {
        return -ENOMEM;
    }

    if (LOS_ArchCopyFromUser(*iovBuf, iov, bufLen) != 0) {
M
mamingshuai 已提交
166
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, *iovBuf);
W
wenjun 已提交
167 168 169 170 171
        return -EFAULT;
    }

    ret = UserIovItemCheck(*iovBuf, iovcnt);
    if (ret == 0) {
M
mamingshuai 已提交
172
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, *iovBuf);
W
wenjun 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        return -EFAULT;
    }

    *valid_iovcnt = ret;
    return 0;
}

static int PollfdToSystem(struct pollfd *fds, nfds_t nfds, int **pollFdsBak)
{
    if ((nfds != 0 && fds == NULL) || (pollFdsBak == NULL)) {
        set_errno(EINVAL);
        return -1;
    }
    if (nfds == 0) {
        return 0;
    }
    int *pollFds = (int *)malloc(sizeof(int) * nfds);
    if (pollFds == NULL) {
        set_errno(ENOMEM);
        return -1;
    }
    for (int i = 0; i < nfds; ++i) {
        struct pollfd *p_fds = &fds[i];
        pollFds[i] = p_fds->fd;
        if (p_fds->fd < 0) {
            set_errno(EBADF);
            free(pollFds);
            return -1;
        }
        p_fds->fd = GetAssociatedSystemFd(p_fds->fd);
    }
    *pollFdsBak = pollFds;
    return 0;
}

static void RestorePollfd(struct pollfd *fds, nfds_t nfds, const int *pollFds)
{
    if ((fds == NULL) || (pollFds == NULL)) {
        return;
    }
    for (int i = 0; i < nfds; ++i) {
        struct pollfd *p_fds = &fds[i];
        p_fds->fd = pollFds[i];
    }
}

static int UserPoll(struct pollfd *fds, nfds_t nfds, int timeout)
{
    int *pollFds = NULL;
    int ret = PollfdToSystem(fds, nfds, &pollFds);
    if (ret < 0) {
        return -1;
    }

    ret = poll(fds, nfds, timeout);

    RestorePollfd(fds, nfds, pollFds);

    free(pollFds);
    return ret;
}

int SysClose(int fd)
{
    int ret;

    /* Process fd convert to system global fd */
    int sysfd = DisassociateProcessFd(fd);

    ret = close(sysfd);
    if (ret < 0) {
        AssociateSystemFd(fd, sysfd);
        return -get_errno();
    }
    FreeProcessFd(fd);
    return ret;
}

ssize_t SysRead(int fd, void *buf, size_t nbytes)
{
    int ret;

    if (nbytes == 0) {
        return 0;
    }

    if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, nbytes)) {
        return -EFAULT;
    }

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = read(fd, buf, nbytes);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

ssize_t SysWrite(int fd, const void *buf, size_t nbytes)
{
    int ret;

    if (nbytes == 0) {
        return 0;
    }

    if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, nbytes)) {
        return -EFAULT;
    }

    /* Process fd convert to system global fd */
286 287
    int sysfd = GetAssociatedSystemFd(fd);
    ret = write(sysfd, buf, nbytes);
W
wenjun 已提交
288 289 290 291 292 293
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
// int vfs_normalize_path(const char *directory, const char *filename, char **pathname)
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
#define PROCESS_DIR_ROOT   "/proc"
static char *NextName(char *pos, uint8_t *len)
{
    char *name = NULL;
    while (*pos != 0 && *pos == '/') {
        pos++;
    }
    if (*pos == '\0') {
        return NULL;
    }
    name = (char *)pos;
    while (*pos != '\0' && *pos != '/') {
        pos++;
    }
    *len = pos - name;
    return name;
}

static unsigned int ProcRealProcessIDGet(unsigned int pid)
{
    unsigned int intSave;
    if (OS_PID_CHECK_INVALID(pid)) {
        return 0;
    }

    SCHEDULER_LOCK(intSave);
    LosProcessCB *pcb = OsGetPCBFromVpid(pid);
Z
zhushengle 已提交
324
    if (OsProcessIsInactive(pcb)) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
        SCHEDULER_UNLOCK(intSave);
        return 0;
    }

    int rootPid = OsGetRootPid(pcb);
    SCHEDULER_UNLOCK(intSave);
    if ((rootPid == OS_INVALID_VALUE) || (rootPid == pid)) {
        return 0;
    }

    return rootPid;
}

static int ProcRealProcessDirGet(char *path)
{
    char pidBuf[PATH_MAX] = {0};
    char *fullPath = NULL;
    uint8_t strLen = 0;
    int pid, rootPid;
    int ret = vfs_normalize_path(NULL, path, &fullPath);
    if (ret < 0) {
        return ret;
    }

    int procLen = strlen(PROCESS_DIR_ROOT);
    if (strncmp(fullPath, PROCESS_DIR_ROOT, procLen) != 0) {
        free(fullPath);
        return 0;
    }

    char *pidStr = NextName(fullPath + procLen, &strLen);
    if (pidStr == NULL) {
        free(fullPath);
        return 0;
    }

    if ((*pidStr <= '0') || (*pidStr > '9')) {
        free(fullPath);
        return 0;
    }

    if (memcpy_s(pidBuf, PATH_MAX, pidStr, strLen) != EOK) {
        free(fullPath);
        return 0;
    }
    pidBuf[strLen] = '\0';

    pid = atoi(pidBuf);
    if (pid == 0) {
        free(fullPath);
        return 0;
    }

    rootPid = ProcRealProcessIDGet((unsigned)pid);
    if (rootPid == 0) {
        free(fullPath);
        return 0;
    }

    if (snprintf_s(path, PATH_MAX + 1, PATH_MAX, "/proc/%d%s", rootPid, (pidStr + strLen)) < 0) {
        free(fullPath);
        return -EFAULT;
    }

    free(fullPath);
    return 0;
}
#endif
#endif

static int GetPath(const char *path, char **pathRet)
{
    int ret = UserPathCopy(path, pathRet);
    if (ret != 0) {
        return ret;
    }
#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
    ret = ProcRealProcessDirGet(*pathRet);
    if (ret != 0) {
        return ret;
    }
#endif
#endif
    return 0;
}

W
wenjun 已提交
412 413 414
int SysOpen(const char *path, int oflags, ...)
{
    int ret;
415
    int procFd = -1;
W
wangchenyang 已提交
416
    mode_t mode = DEFAULT_FILE_MODE; /* 0666: File read-write properties. */
W
wenjun 已提交
417 418
    char *pathRet = NULL;

C
chenwei 已提交
419
    if (path != NULL) {
420
        ret = GetPath(path, &pathRet);
C
chenwei 已提交
421
        if (ret != 0) {
422
            goto ERROUT;
C
chenwei 已提交
423
        }
W
wenjun 已提交
424 425 426
    }

    procFd = AllocProcessFd();
W
wangchenyang 已提交
427
    if (procFd < 0) {
W
wenjun 已提交
428
        ret = -EMFILE;
W
wangchenyang 已提交
429
        goto ERROUT;
W
wenjun 已提交
430 431
    }

432 433 434 435
    if (oflags & O_CLOEXEC) {
        SetCloexecFlag(procFd);
    }

W
wenjun 已提交
436 437
    if ((unsigned int)oflags & O_DIRECTORY) {
        ret = do_opendir(pathRet, oflags);
W
wangchenyang 已提交
438
    } else {
W
wenjun 已提交
439
#ifdef LOSCFG_FILE_MODE
W
wangchenyang 已提交
440 441 442 443
        va_list ap;
        va_start(ap, oflags);
        mode = va_arg(ap, int);
        va_end(ap);
W
wenjun 已提交
444 445
#endif

W
wangchenyang 已提交
446 447 448
        ret = do_open(AT_FDCWD, pathRet, oflags, mode);
    }

W
wenjun 已提交
449 450
    if (ret < 0) {
        ret = -get_errno();
W
wangchenyang 已提交
451
        goto ERROUT;
W
wenjun 已提交
452 453
    }

W
wangchenyang 已提交
454
    AssociateSystemFd(procFd, ret);
W
wenjun 已提交
455
    if (pathRet != NULL) {
W
wangchenyang 已提交
456 457 458 459 460
        LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    return procFd;

ERROUT:
C
chenwei 已提交
461
    if (pathRet != NULL) {
462 463 464 465
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    if (procFd >= 0) {
        FreeProcessFd(procFd);
C
chenwei 已提交
466
    }
W
wenjun 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    return ret;
}

int SysCreat(const char *pathname, mode_t mode)
{
    int ret = 0;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    int procFd = AllocProcessFd();
    if (procFd  < 0) {
        ret = -EMFILE;
        goto OUT;
    }

    ret = open((pathname ? pathRet : NULL), O_CREAT | O_TRUNC | O_WRONLY, mode);
    if (ret < 0) {
        FreeProcessFd(procFd);
        ret = -get_errno();
    } else {
        AssociateSystemFd(procFd, ret);
        ret = procFd;
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
499
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
500 501 502 503
    }
    return ret;
}

C
chenjing 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
int SysLink(const char *oldpath, const char *newpath)
{
    int ret;
    char *oldpathRet = NULL;
    char *newpathRet = NULL;

    if (oldpath != NULL) {
        ret = UserPathCopy(oldpath, &oldpathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (newpath != NULL) {
        ret = UserPathCopy(newpath, &newpathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = link(oldpathRet, newpathRet);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (oldpathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, oldpathRet);
    }
    if (newpathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, newpathRet);
    }
    return ret;
}

ssize_t SysReadlink(const char *pathname, char *buf, size_t bufsize)
{
    ssize_t ret;
    char *pathRet = NULL;

    if (bufsize == 0) {
        return -EINVAL;
    }

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
553 554 555 556 557 558 559 560 561

#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
        ret = ProcRealProcessDirGet(pathRet);
        if (ret != 0) {
            goto OUT;
        }
#endif
#endif
C
chenjing 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    }

    if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, bufsize)) {
        ret = -EFAULT;
        goto OUT;
    }

    ret = readlink(pathRet, buf, bufsize);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    return ret;
}

int SysSymlink(const char *target, const char *linkpath)
{
    int ret;
M
mucor 已提交
584
    char *targetRet = NULL;
C
chenjing 已提交
585 586
    char *pathRet = NULL;

M
mucor 已提交
587 588 589 590 591 592 593
    if (target != NULL) {
        ret = UserPathCopy(target, &targetRet);
        if (ret != 0) {
            goto OUT;
        }
    }

C
chenjing 已提交
594 595 596 597 598 599 600
    if (linkpath != NULL) {
        ret = UserPathCopy(linkpath, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

M
mucor 已提交
601
    ret = symlink(targetRet, pathRet);
C
chenjing 已提交
602 603 604 605 606 607 608 609
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
M
mucor 已提交
610 611 612 613

    if (targetRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, targetRet);
    }
C
chenjing 已提交
614 615 616
    return ret;
}

W
wenjun 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
int SysUnlink(const char *pathname)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = do_unlink(AT_FDCWD, (pathname ? pathRet : NULL));
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
636
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
637 638 639 640
    }
    return ret;
}

641
#ifdef LOSCFG_KERNEL_DYNLOAD
W
wenjun 已提交
642 643 644 645
int SysExecve(const char *fileName, char *const *argv, char *const *envp)
{
    return LOS_DoExecveFile(fileName, argv, envp);
}
646
#endif
W
wenjun 已提交
647

W
wcc0 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
int SysFchdir(int fd)
{
    int ret;
    int sysFd;
    struct file *file = NULL;

    sysFd = GetAssociatedSystemFd(fd);
    if (sysFd < 0) {
        return -EBADF;
    }

    ret = fs_getfilep(sysFd, &file);
    if (ret < 0) {
        return -get_errno();
    }

    ret = chdir(file->f_path);
    if (ret < 0) {
        ret = -get_errno();
    }

    return ret;
}

W
wenjun 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
int SysChdir(const char *path)
{
    int ret;
    char *pathRet = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = chdir(path ? pathRet : NULL);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
691
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
692 693 694 695 696 697 698 699 700
    }
    return ret;
}

off_t SysLseek(int fd, off_t offset, int whence)
{
    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

701
    return _lseek(fd, offset, whence);
W
wenjun 已提交
702 703 704 705 706
}

off64_t SysLseek64(int fd, int offsetHigh, int offsetLow, off64_t *result, int whence)
{
    off64_t ret;
707
    off64_t res;
W
wenjun 已提交
708 709 710 711 712
    int retVal;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

713 714 715
    ret = _lseek64(fd, offsetHigh, offsetLow, &res, whence);
    if (ret != 0) {
        return ret;
W
wenjun 已提交
716 717
    }

718
    retVal = LOS_ArchCopyToUser(result, &res, sizeof(off64_t));
W
wenjun 已提交
719 720 721 722 723 724 725
    if (retVal != 0) {
        return -EFAULT;
    }

    return 0;
}

Z
zhangfanfan2 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
#ifdef LOSCFG_FS_NFS
static int NfsMountRef(const char *serverIpAndPath, const char *mountPath,
                       unsigned int uid, unsigned int gid) __attribute__((weakref("nfs_mount")));

static int NfsMount(const char *serverIpAndPath, const char *mountPath,
                    unsigned int uid, unsigned int gid)
{
    int ret;

    if ((serverIpAndPath == NULL) || (mountPath == NULL)) {
        return -EINVAL;
    }
    ret = NfsMountRef(serverIpAndPath, mountPath, uid, gid);
    if (ret < 0) {
        ret = -get_errno();
    }
    return ret;
}
#endif

W
wenjun 已提交
746 747 748 749 750 751
int SysMount(const char *source, const char *target, const char *filesystemtype, unsigned long mountflags,
             const void *data)
{
    int ret;
    char *sourceRet = NULL;
    char *targetRet = NULL;
M
mucor 已提交
752
    char *dataRet = NULL;
W
wenjun 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    char fstypeRet[FILESYSTEM_TYPE_MAX + 1] = {0};

    if (!IsCapPermit(CAP_FS_MOUNT)) {
        return -EPERM;
    }

    if (target != NULL) {
        ret = UserPathCopy(target, &targetRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (filesystemtype != NULL) {
        ret = LOS_StrncpyFromUser(fstypeRet, filesystemtype, FILESYSTEM_TYPE_MAX + 1);
        if (ret < 0) {
            goto OUT;
        } else if (ret > FILESYSTEM_TYPE_MAX) {
            ret = -ENODEV;
            goto OUT;
        }

        if (strcmp(fstypeRet, "ramfs") && (source != NULL)) {
            ret = UserPathCopy(source, &sourceRet);
            if (ret != 0) {
                goto OUT;
            }
        }
Z
zhangfanfan2 已提交
781 782 783 784 785 786
#ifdef LOSCFG_FS_NFS
        if (strcmp(fstypeRet, "nfs") == 0) {
            ret = NfsMount(sourceRet, targetRet, 0, 0);
            goto OUT;
        }
#endif
W
wenjun 已提交
787 788
    }

M
mucor 已提交
789 790 791 792 793 794 795 796
    if (data != NULL) {
        ret = UserPathCopy(data, &dataRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = mount(sourceRet, targetRet, (filesystemtype ? fstypeRet : NULL), mountflags, dataRet);
W
wenjun 已提交
797 798 799 800 801 802
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (sourceRet != NULL) {
M
mamingshuai 已提交
803
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, sourceRet);
W
wenjun 已提交
804 805
    }
    if (targetRet != NULL) {
M
mamingshuai 已提交
806
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, targetRet);
W
wenjun 已提交
807
    }
M
mucor 已提交
808 809 810
    if (dataRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, dataRet);
    }
W
wenjun 已提交
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
    return ret;
}

int SysUmount(const char *target)
{
    int ret;
    char *pathRet = NULL;

    if (!IsCapPermit(CAP_FS_MOUNT)) {
        return -EPERM;
    }

    if (target != NULL) {
        ret = UserPathCopy(target, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = umount(target ? pathRet : NULL);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
837
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
    }
    return ret;
}

int SysAccess(const char *path, int amode)
{
    int ret;
    char *pathRet = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

854 855
    ret = access(pathRet, amode);
    if (ret < 0) {
W
wenjun 已提交
856 857 858 859 860
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
861
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
    }

    return ret;
}

int SysRename(const char *oldpath, const char *newpath)
{
    int ret;
    char *pathOldRet = NULL;
    char *pathNewRet = NULL;

    if (oldpath != NULL) {
        ret = UserPathCopy(oldpath, &pathOldRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (newpath != NULL) {
        ret = UserPathCopy(newpath, &pathNewRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = do_rename(AT_FDCWD, (oldpath ? pathOldRet : NULL), AT_FDCWD,
                    (newpath ? pathNewRet : NULL));
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathOldRet != NULL) {
M
mamingshuai 已提交
895
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathOldRet);
W
wenjun 已提交
896 897
    }
    if (pathNewRet != NULL) {
M
mamingshuai 已提交
898
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathNewRet);
W
wenjun 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
    }
    return ret;
}

int SysMkdir(const char *pathname, mode_t mode)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = do_mkdir(AT_FDCWD, (pathname ? pathRet : NULL), mode);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
922
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    }
    return ret;
}

int SysRmdir(const char *pathname)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = do_rmdir(AT_FDCWD, (pathname ? pathRet : NULL));
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
946
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
947 948 949 950 951 952
    }
    return ret;
}

int SysDup(int fd)
{
G
Guangyao Ma 已提交
953 954 955 956
    int sysfd = GetAssociatedSystemFd(fd);
    /* Check if the param is valid, note that: socket fd is not support dup2 */
    if ((sysfd < 0) || (sysfd >= CONFIG_NFILE_DESCRIPTORS)) {
        return -EBADF;
W
wenjun 已提交
957 958
    }

G
Guangyao Ma 已提交
959 960 961
    int dupfd = AllocProcessFd();
    if (dupfd < 0) {
        return -EMFILE;
W
wenjun 已提交
962 963
    }

G
Guangyao Ma 已提交
964 965 966
    files_refer(sysfd);
    AssociateSystemFd(dupfd, sysfd);
    return dupfd;
W
wenjun 已提交
967 968 969 970 971 972 973 974 975 976
}

void SysSync(void)
{
    sync();
}

int SysUmount2(const char *target, int flags)
{
    if (flags != 0) {
M
mamingshuai 已提交
977
        return -EINVAL;
W
wenjun 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    }
    return SysUmount(target);
}

int SysIoctl(int fd, int req, void *arg)
{
    int ret;
    unsigned int size = _IOC_SIZE((unsigned int)req);
    unsigned int dir = _IOC_DIR((unsigned int)req);
    if ((size == 0) && (dir != _IOC_NONE)) {
        return -EINVAL;
    }

    if ((dir != _IOC_NONE) && (((void *)(uintptr_t)arg) == NULL)) {
        return -EINVAL;
    }

    if ((dir & _IOC_READ) || (dir & _IOC_WRITE)) {
        if (!LOS_IsUserAddressRange((uintptr_t)arg, size)) {
            return -EFAULT;
        }
    }

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = ioctl(fd, req, arg);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

int SysFcntl(int fd, int cmd, void *arg)
{
    /* Process fd convert to system global fd */
1014
    int sysfd = GetAssociatedSystemFd(fd);
W
wenjun 已提交
1015

1016 1017 1018
    int ret = VfsFcntl(fd, cmd, arg);
    if (ret == CONTINE_NUTTX_FCNTL) {
        ret = fcntl(sysfd, cmd, arg);
W
wenjun 已提交
1019
    }
G
Guangyao Ma 已提交
1020

W
wenjun 已提交
1021 1022 1023 1024 1025 1026
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

M
mamingshuai 已提交
1027
#ifdef LOSCFG_KERNEL_PIPE
W
wenjun 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
int SysPipe(int pipefd[2]) /* 2 : pipe fds for read and write */
{
    int ret;
    int pipeFdIntr[2] = {0}; /* 2 : pipe fds for read and write */

    int procFd0 = AllocProcessFd();
    if (procFd0 < 0) {
        return -EMFILE;
    }
    int procFd1 = AllocProcessFd();
    if (procFd1 < 0) {
        FreeProcessFd(procFd0);
        return -EMFILE;
    }

    ret = pipe(pipeFdIntr);
    if (ret < 0) {
        FreeProcessFd(procFd0);
        FreeProcessFd(procFd1);
        return -get_errno();
    }
    int sysPipeFd0 = pipeFdIntr[0];
    int sysPipeFd1 = pipeFdIntr[1];

    AssociateSystemFd(procFd0, sysPipeFd0);
    AssociateSystemFd(procFd1, sysPipeFd1);

    pipeFdIntr[0] = procFd0;
    pipeFdIntr[1] = procFd1;

    ret = LOS_ArchCopyToUser(pipefd, pipeFdIntr, sizeof(pipeFdIntr));
    if (ret != 0) {
        FreeProcessFd(procFd0);
        FreeProcessFd(procFd1);
        close(sysPipeFd0);
        close(sysPipeFd1);
        return -EFAULT;
    }
    return ret;
}
M
mamingshuai 已提交
1068
#endif
W
wenjun 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101

int SysDup2(int fd1, int fd2)
{
    int ret;
    int sysfd1 = GetAssociatedSystemFd(fd1);
    int sysfd2 = GetAssociatedSystemFd(fd2);

    /* Check if the param is valid, note that: socket fd is not support dup2 */
    if ((sysfd1 < 0) || (sysfd1 >= CONFIG_NFILE_DESCRIPTORS) || (CheckProcessFd(fd2) != OK)) {
        return -EBADF;
    }

    /* Handle special circumstances */
    if (fd1 == fd2) {
        return fd2;
    }

    ret = AllocSpecifiedProcessFd(fd2);
    if (ret != OK) {
        return ret;
    }

    /* close the sysfd2 in need */
    if (sysfd2 >= 0) {
        ret = close(sysfd2);
        if (ret < 0) {
            AssociateSystemFd(fd2, sysfd2);
            return -get_errno();
        }
    }

    files_refer(sysfd1);
    AssociateSystemFd(fd2, sysfd1);
1102 1103 1104

    /* if fd1 is not equal to fd2, the FD_CLOEXEC flag associated with fd2 shall be cleared */
    ClearCloexecFlag(fd2);
W
wenjun 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    return fd2;
}

static int SelectParamCheckCopy(fd_set *readfds, fd_set *writefds, fd_set *exceptfds, fd_set **fdsBuf)
{
    fd_set *readfdsRet = NULL;
    fd_set *writefdsRet = NULL;
    fd_set *exceptfdsRet = NULL;

    *fdsBuf = (fd_set *)LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(fd_set) * 3); /* 3: three param need check and copy */
    if (*fdsBuf == NULL) {
        return -ENOMEM;
    }

    readfdsRet = *fdsBuf;        /* LOS_MemAlloc 3 sizeof(fd_set) space,first use for readfds */
    writefdsRet = *fdsBuf + 1;   /* 1: LOS_MemAlloc 3 sizeof(fd_set) space,second use for writefds */
    exceptfdsRet = *fdsBuf + 2;  /* 2: LOS_MemAlloc 3 sizeof(fd_set) space,thired use for exceptfds */

    if (readfds != NULL) {
        if (LOS_ArchCopyFromUser(readfdsRet, readfds, sizeof(fd_set)) != 0) {
M
mamingshuai 已提交
1125
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, *fdsBuf);
W
wenjun 已提交
1126 1127 1128 1129 1130 1131
            return -EFAULT;
        }
    }

    if (writefds != NULL) {
        if (LOS_ArchCopyFromUser(writefdsRet, writefds, sizeof(fd_set)) != 0) {
M
mamingshuai 已提交
1132
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, *fdsBuf);
W
wenjun 已提交
1133 1134 1135 1136 1137 1138
            return -EFAULT;
        }
    }

    if (exceptfds != NULL) {
        if (LOS_ArchCopyFromUser(exceptfdsRet, exceptfds, sizeof(fd_set)) != 0) {
M
mamingshuai 已提交
1139
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, *fdsBuf);
W
wenjun 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
            return -EFAULT;
        }
    }

    return 0;
}

int SysSelect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
    int ret;
    fd_set *fdsRet = NULL;
    fd_set *readfdsRet = NULL;
    fd_set *writefdsRet = NULL;
    fd_set *exceptfdsRet = NULL;
    struct timeval timeoutRet = {0};

    ret = SelectParamCheckCopy(readfds, writefds, exceptfds, &fdsRet);
    if (ret != 0) {
        return ret;
    }

    readfdsRet = fdsRet;        /* LOS_MemAlloc 3 sizeof(fd_set) space,first use for readfds */
    writefdsRet = fdsRet + 1;   /* 1: LOS_MemAlloc 3 sizeof(fd_set) space,second use for writefds */
    exceptfdsRet = fdsRet + 2;  /* 2: LOS_MemAlloc 3 sizeof(fd_set) space,thired use for exceptfds */

    if (timeout != NULL) {
        if (LOS_ArchCopyFromUser(&timeoutRet, timeout, sizeof(struct timeval)) != 0) {
            goto ERROUT;
        }
    }

    ret = do_select(nfds, (readfds ? readfdsRet : NULL), (writefds ? writefdsRet : NULL),
                 (exceptfds ? exceptfdsRet : NULL), (timeout ? (&timeoutRet) : NULL), UserPoll);
    if (ret < 0) {
M
mamingshuai 已提交
1174
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, fdsRet);
W
wenjun 已提交
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
        return -get_errno();
    }

    if (readfds != NULL) {
        if (LOS_ArchCopyToUser(readfds, readfdsRet, sizeof(fd_set)) != 0) {
            goto ERROUT;
        }
    }

    if (writefds != NULL) {
        if (LOS_ArchCopyToUser(writefds, writefdsRet, sizeof(fd_set)) != 0) {
            goto ERROUT;
        }
    }

    if (exceptfds != 0) {
        if (LOS_ArchCopyToUser(exceptfds, exceptfdsRet, sizeof(fd_set)) != 0) {
            goto ERROUT;
        }
    }

M
mamingshuai 已提交
1196
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, fdsRet);
W
wenjun 已提交
1197 1198 1199
    return ret;

ERROUT:
M
mamingshuai 已提交
1200
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, fdsRet);
W
wenjun 已提交
1201 1202 1203 1204 1205 1206
    return -EFAULT;
}

int SysTruncate(const char *path, off_t length)
{
    int ret;
M
mamingshuai 已提交
1207
    int fd = -1;
W
wenjun 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    char *pathRet = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    fd = open((path ? pathRet : NULL), O_RDWR);
    if (fd < 0) {
        /* The errno value has already been set */
        ret = -get_errno();
        goto OUT;
    }

    ret = ftruncate(fd, length);
    close(fd);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1232
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1233 1234 1235 1236 1237 1238 1239
    }
    return ret;
}

int SysTruncate64(const char *path, off64_t length)
{
    int ret;
M
mamingshuai 已提交
1240
    int fd = -1;
W
wenjun 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    char *pathRet = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    fd = open((path ? pathRet : NULL), O_RDWR);
    if (fd < 0) {
        /* The errno value has already been set */
        ret = -get_errno();
        goto OUT;
    }

    ret = ftruncate64(fd, length);
    close(fd);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1265
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
    }
    return ret;
}

int SysFtruncate(int fd, off_t length)
{
    int ret;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = ftruncate(fd, length);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

int SysStatfs(const char *path, struct statfs *buf)
{
    int ret;
    char *pathRet = NULL;
    struct statfs bufRet = {0};

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = statfs((path ? pathRet : NULL), (buf ? (&bufRet) : NULL));
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct statfs));
    if (ret != 0) {
        ret = -EFAULT;
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1310
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
    }
    return ret;
}

int SysStatfs64(const char *path, size_t sz, struct statfs *buf)
{
    int ret;
    char *pathRet = NULL;
    struct statfs bufRet = {0};

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (sz != sizeof(*buf)) {
        ret = -EINVAL;
        goto OUT;
    }

    ret = statfs((path ? pathRet : NULL), (buf ? (&bufRet) : NULL));
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct statfs));
    if (ret != 0) {
        ret = -EFAULT;
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1346
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1347 1348 1349 1350
    }
    return ret;
}

Yansira's avatar
Yansira 已提交
1351
int SysStat(const char *path, struct kstat *buf)
W
wenjun 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
{
    int ret;
    char *pathRet = NULL;
    struct stat bufRet = {0};

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = stat((path ? pathRet : NULL), (buf ? (&bufRet) : NULL));
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

Yansira's avatar
Yansira 已提交
1370
    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct kstat));
W
wenjun 已提交
1371 1372 1373 1374 1375 1376
    if (ret != 0) {
        ret = -EFAULT;
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1377
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1378 1379 1380 1381
    }
    return ret;
}

Yansira's avatar
Yansira 已提交
1382
int SysLstat(const char *path, struct kstat *buffer)
W
wenjun 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
{
    int ret;
    char *pathRet = NULL;
    struct stat bufRet = {0};

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = stat((path ? pathRet : NULL), (buffer ? (&bufRet) : NULL));
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

Yansira's avatar
Yansira 已提交
1401
    ret = LOS_ArchCopyToUser(buffer, &bufRet, sizeof(struct kstat));
W
wenjun 已提交
1402 1403 1404 1405 1406 1407
    if (ret != 0) {
        ret = -EFAULT;
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1408
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1409 1410 1411 1412
    }
    return ret;
}

1413
int SysFstat(int fd, struct kstat *buf)
W
wenjun 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
{
    int ret;
    struct stat bufRet = {0};
    struct file *filep = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = fs_getfilep(fd, &filep);
    if (ret < 0) {
        return -get_errno();
    }

    if (filep->f_oflags & O_DIRECTORY) {
        return -EBADF;
    }

    ret = stat(filep->f_path, (buf ? (&bufRet) : NULL));
    if (ret < 0) {
        return -get_errno();
    }

1436
    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct kstat));
W
wenjun 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
    if (ret != 0) {
        return -EFAULT;
    }

    return ret;
}

int SysStatx(int fd, const char *restrict path, int flag, unsigned mask, struct statx *restrict stx)
{
    return -ENOSYS;
}

int SysFsync(int fd)
{
    int ret;
    struct file *filep = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    /* Get the file structure corresponding to the file descriptor. */
    ret = fs_getfilep(fd, &filep);
    if (ret < 0) {
        /* The errno value has already been set */
        return -get_errno();
    }

    if (filep->f_oflags & O_DIRECTORY) {
        return -EBADF;
    }

    /* Perform the fsync operation */
    ret = file_fsync(filep);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

ssize_t SysReadv(int fd, const struct iovec *iov, int iovcnt)
{
    int ret;
    int valid_iovcnt = -1;
    struct iovec *iovRet = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);
M
mucor 已提交
1484 1485 1486 1487 1488 1489
    if ((iov == NULL) || (iovcnt < 0) || (iovcnt > IOV_MAX)) {
        return -EINVAL;
    }

    if (iovcnt == 0) {
        return 0;
W
wenjun 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    }

    ret = UserIovCopy(&iovRet, iov, iovcnt, &valid_iovcnt);
    if (ret != 0) {
        return ret;
    }

    if (valid_iovcnt <= 0) {
        ret = -EFAULT;
        goto OUT;
    }

    ret = vfs_readv(fd, iovRet, valid_iovcnt, NULL);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, iovRet);
    return ret;
}

ssize_t SysWritev(int fd, const struct iovec *iov, int iovcnt)
{
    int ret;
    int valid_iovcnt = -1;
    struct iovec *iovRet = NULL;

    /* Process fd convert to system global fd */
1519
    int sysfd = GetAssociatedSystemFd(fd);
1520 1521 1522
    if ((iovcnt < 0) || (iovcnt > IOV_MAX)) {
        return -EINVAL;
    }
M
mucor 已提交
1523 1524 1525 1526 1527

    if (iovcnt == 0) {
        return 0;
    }

1528 1529
    if (iov == NULL) {
        return -EFAULT;
W
wenjun 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    }

    ret = UserIovCopy(&iovRet, iov, iovcnt, &valid_iovcnt);
    if (ret != 0) {
        return ret;
    }

    if (valid_iovcnt != iovcnt) {
        ret = -EFAULT;
        goto OUT_FREE;
    }

1542
    ret = writev(sysfd, iovRet, valid_iovcnt);
W
wenjun 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    if (ret < 0) {
        ret = -get_errno();
    }

OUT_FREE:
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, iovRet);
    return ret;
}

int SysPoll(struct pollfd *fds, nfds_t nfds, int timeout)
{
    int ret;
    struct pollfd *kfds = NULL;

    if ((nfds >= MAX_POLL_NFDS) || (nfds == 0) || (fds == NULL)) {
        return -EINVAL;
    }

    kfds = (struct pollfd *)malloc(sizeof(struct pollfd) * nfds);
    if (kfds != NULL) {
        if (LOS_ArchCopyFromUser(kfds, fds, sizeof(struct pollfd) * nfds) != 0) {
            ret = -EFAULT;
            goto OUT_KFD;
        }
    }

    int *pollFds = NULL;
    ret = PollfdToSystem(kfds, nfds, &pollFds);
    if (ret < 0) {
        ret = -get_errno();
        goto OUT_KFD;
    }

    ret = poll(kfds, nfds, timeout);
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

    if (kfds != NULL) {
        RestorePollfd(kfds, nfds, pollFds);
        if (LOS_ArchCopyToUser(fds, kfds, sizeof(struct pollfd) * nfds) != 0) {
            ret = -EFAULT;
            goto OUT;
        }
    }

OUT:
    free(pollFds);
OUT_KFD:
    free(kfds);
    return ret;
}

int SysPrctl(int option, ...)
{
    unsigned long name;
    va_list ap;
    errno_t err;

    va_start(ap, option);
    if (option != PR_SET_NAME) {
        PRINT_ERR("%s: %d, no support option : 0x%x\n", __FUNCTION__, __LINE__, option);
        err = EOPNOTSUPP;
        goto ERROR;
    }

    name = va_arg(ap, unsigned long);
1611 1612 1613 1614 1615 1616
    if (!LOS_IsUserAddress(name)) {
        err = EFAULT;
        goto ERROR;
    }

    err = OsSetTaskName(OsCurrTaskGet(), (const char *)(uintptr_t)name, TRUE);
W
wenjun 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
    if (err != LOS_OK) {
        goto ERROR;
    }

    va_end(ap);
    return ENOERR;

ERROR:
    va_end(ap);
    return -err;
}

ssize_t SysPread64(int fd, void *buf, size_t nbytes, off64_t offset)
{
    int ret, retVal;
    char *bufRet = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    if (nbytes == 0) {
        ret = pread64(fd, buf, nbytes, offset);
        if (ret < 0) {
            return -get_errno();
        } else {
            return ret;
        }
    }

    bufRet = (char *)LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
    if (bufRet == NULL) {
        return -ENOMEM;
    }
1650
    (void)memset_s(bufRet, nbytes, 0, nbytes);
W
wenjun 已提交
1651 1652
    ret = pread64(fd, (buf ? bufRet : NULL), nbytes, offset);
    if (ret < 0) {
M
mamingshuai 已提交
1653
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1654 1655 1656 1657 1658
        return -get_errno();
    }

    retVal = LOS_ArchCopyToUser(buf, bufRet, ret);
    if (retVal != 0) {
M
mamingshuai 已提交
1659
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1660 1661 1662
        return -EFAULT;
    }

M
mamingshuai 已提交
1663
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
    return ret;
}

ssize_t SysPwrite64(int fd, const void *buf, size_t nbytes, off64_t offset)
{
    int ret;
    char *bufRet = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    if (nbytes == 0) {
        ret = pwrite64(fd, buf, nbytes, offset);
        if (ret < 0) {
            return -get_errno();
        }
        return ret;
    }

    bufRet = (char *)LOS_MemAlloc(OS_SYS_MEM_ADDR, nbytes);
    if (bufRet == NULL) {
        return -ENOMEM;
    }

    if (buf != NULL) {
        ret = LOS_ArchCopyFromUser(bufRet, buf, nbytes);
        if (ret != 0) {
M
mamingshuai 已提交
1691
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1692 1693 1694 1695 1696 1697
            return -EFAULT;
        }
    }

    ret = pwrite64(fd, (buf ? bufRet : NULL), nbytes, offset);
    if (ret < 0) {
M
mamingshuai 已提交
1698
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1699 1700 1701
        return -get_errno();
    }

M
mamingshuai 已提交
1702
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1703 1704 1705 1706 1707 1708 1709
    return ret;
}

char *SysGetcwd(char *buf, size_t n)
{
    char *ret = NULL;
    char *bufRet = NULL;
M
mucor 已提交
1710
    size_t bufLen = n;
W
wenjun 已提交
1711 1712
    int retVal;

M
mucor 已提交
1713 1714 1715 1716 1717
    if (bufLen > PATH_MAX) {
        bufLen = PATH_MAX;
    }

    bufRet = (char *)LOS_MemAlloc(OS_SYS_MEM_ADDR, bufLen);
W
wenjun 已提交
1718 1719 1720
    if (bufRet == NULL) {
        return (char *)(intptr_t)-ENOMEM;
    }
M
mucor 已提交
1721
    (void)memset_s(bufRet, bufLen, 0, bufLen);
W
wenjun 已提交
1722

M
mucor 已提交
1723
    ret = getcwd((buf ? bufRet : NULL), bufLen);
W
wenjun 已提交
1724
    if (ret == NULL) {
M
mamingshuai 已提交
1725
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1726 1727 1728
        return (char *)(intptr_t)-get_errno();
    }

M
mucor 已提交
1729
    retVal = LOS_ArchCopyToUser(buf, bufRet, bufLen);
W
wenjun 已提交
1730
    if (retVal != 0) {
M
mamingshuai 已提交
1731
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1732 1733 1734 1735
        return (char *)(intptr_t)-EFAULT;
    }
    ret = buf;

M
mamingshuai 已提交
1736
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, bufRet);
W
wenjun 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
    return ret;
}

ssize_t SysSendFile(int outfd, int infd, off_t *offset, size_t count)
{
    int ret, retVal;
    off_t offsetRet;

    retVal = LOS_ArchCopyFromUser(&offsetRet, offset, sizeof(off_t));
    if (retVal != 0) {
        return -EFAULT;
    }

    /* Process fd convert to system global fd */
    outfd = GetAssociatedSystemFd(outfd);
    infd = GetAssociatedSystemFd(infd);

    ret = sendfile(outfd, infd, (offset ? (&offsetRet) : NULL), count);
    if (ret < 0) {
        return -get_errno();
    }

    retVal = LOS_ArchCopyToUser(offset, &offsetRet, sizeof(off_t));
    if (retVal != 0) {
        return -EFAULT;
    }

    return ret;
}

int SysFtruncate64(int fd, off64_t length)
{
    int ret;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = ftruncate64(fd, length);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

int SysOpenat(int dirfd, const char *path, int oflags, ...)
{
    int ret;
1784
    int procFd;
W
wenjun 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
    char *pathRet = NULL;
    mode_t mode;
#ifdef LOSCFG_FILE_MODE
    va_list ap;

    va_start(ap, oflags);
    mode = va_arg(ap, int);
    va_end(ap);
#else
    mode = 0666; /* 0666: File read-write properties. */
#endif

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
1800
            return ret;
W
wenjun 已提交
1801 1802 1803
        }
    }

1804 1805 1806 1807 1808 1809
    procFd = AllocProcessFd();
    if (procFd < 0) {
        ret = -EMFILE;
        goto ERROUT;
    }

1810 1811 1812 1813
    if (oflags & O_CLOEXEC) {
        SetCloexecFlag(procFd);
    }

W
wenjun 已提交
1814 1815 1816 1817 1818 1819 1820 1821
    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

    ret = do_open(dirfd, (path ? pathRet : NULL), oflags, mode);
    if (ret < 0) {
        ret = -get_errno();
1822
        goto ERROUT;
W
wenjun 已提交
1823 1824
    }

1825 1826 1827 1828 1829 1830 1831
    AssociateSystemFd(procFd, ret);
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    return procFd;

ERROUT:
W
wenjun 已提交
1832
    if (pathRet != NULL) {
M
mamingshuai 已提交
1833
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1834
    }
1835 1836 1837
    if (procFd >= 0) {
        FreeProcessFd(procFd);
    }
W
wenjun 已提交
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
    return ret;
}

int SysMkdirat(int dirfd, const char *pathname, mode_t mode)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

    ret = do_mkdir(dirfd, (pathname ? pathRet : NULL), mode);
    if (ret < 0) {
        ret = -get_errno();
    }

C
chenjing 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
OUT:
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    return ret;
}

int SysLinkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags)
{
    int ret;
    char *oldpathRet = NULL;
    char *newpathRet = NULL;

    if (oldpath != NULL) {
        ret = UserPathCopy(oldpath, &oldpathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (newpath != NULL) {
        ret = UserPathCopy(newpath, &newpathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (olddirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        olddirfd = GetAssociatedSystemFd(olddirfd);
    }

    if (newdirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        newdirfd = GetAssociatedSystemFd(newdirfd);
    }

    ret = linkat(olddirfd, oldpathRet, newdirfd, newpathRet, flags);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (oldpathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, oldpathRet);
    }
    if (newpathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, newpathRet);
    }
    return ret;
}

int SysSymlinkat(const char *target, int dirfd, const char *linkpath)
{
    int ret;
    char *pathRet = NULL;
M
mucor 已提交
1919 1920 1921 1922 1923 1924 1925 1926
    char *targetRet = NULL;

    if (target != NULL) {
        ret = UserPathCopy(target, &targetRet);
        if (ret != 0) {
            goto OUT;
        }
    }
C
chenjing 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939

    if (linkpath != NULL) {
        ret = UserPathCopy(linkpath, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

M
mucor 已提交
1940
    ret = symlinkat(targetRet, dirfd, pathRet);
C
chenjing 已提交
1941 1942 1943 1944 1945 1946 1947 1948
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
M
mucor 已提交
1949 1950 1951 1952

    if (targetRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, targetRet);
    }
C
chenjing 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
    return ret;
}

ssize_t SysReadlinkat(int dirfd, const char *pathname, char *buf, size_t bufsize)
{
    ssize_t ret;
    char *pathRet = NULL;

    if (bufsize == 0) {
        return -EINVAL;
    }

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
1970 1971 1972 1973 1974 1975 1976 1977 1978

#ifdef LOSCFG_PID_CONTAINER
#ifdef LOSCFG_PROC_PROCESS_DIR
        ret = ProcRealProcessDirGet(pathRet);
        if (ret != 0) {
            goto OUT;
        }
#endif
#endif
C
chenjing 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
    }

    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

    if (!LOS_IsUserAddressRange((vaddr_t)(UINTPTR)buf, bufsize)) {
        ret = -EFAULT;
        goto OUT;
    }

    ret = readlinkat(dirfd, pathRet, buf, bufsize);
    if (ret < 0) {
        ret = -get_errno();
    }

W
wenjun 已提交
1996 1997
OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
1998
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
    }
    return ret;
}

int SysUnlinkat(int dirfd, const char *pathname, int flag)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

    ret = unlinkat(dirfd, (pathname ? pathRet : NULL), flag);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
2027
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    }
    return ret;
}

int SysRenameat(int oldfd, const char *oldpath, int newdfd, const char *newpath)
{
    int ret;
    char *pathOldRet = NULL;
    char *pathNewRet = NULL;

    if (oldpath != NULL) {
        ret = UserPathCopy(oldpath, &pathOldRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (newpath != NULL) {
        ret = UserPathCopy(newpath, &pathNewRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (oldfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        oldfd = GetAssociatedSystemFd(oldfd);
    }
    if (newdfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        newdfd = GetAssociatedSystemFd(newdfd);
    }

    ret = do_rename(oldfd, (oldpath ? pathOldRet : NULL), newdfd, (newpath ? pathNewRet : NULL));
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathOldRet != NULL) {
M
mamingshuai 已提交
2068
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathOldRet);
W
wenjun 已提交
2069 2070
    }
    if (pathNewRet != NULL) {
M
mamingshuai 已提交
2071
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathNewRet);
W
wenjun 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
    }
    return ret;
}

int SysFallocate(int fd, int mode, off_t offset, off_t len)
{
    int ret;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = fallocate(fd, mode, offset, len);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

int SysFallocate64(int fd, int mode, off64_t offset, off64_t len)
{
    int ret;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = fallocate64(fd, mode, offset, len);
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

M
mamingshuai 已提交
2104
ssize_t SysPreadv(int fd, const struct iovec *iov, int iovcnt, long loffset, long hoffset)
W
wenjun 已提交
2105
{
M
mamingshuai 已提交
2106 2107 2108
    off_t offsetflag;
    offsetflag = (off_t)((unsigned long long)loffset | (((unsigned long long)hoffset) << HIGH_SHIFT_BIT));

W
wenjun 已提交
2109 2110 2111 2112 2113 2114
    int ret;
    int valid_iovcnt = -1;
    struct iovec *iovRet = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);
M
mucor 已提交
2115 2116 2117 2118 2119 2120
    if ((iov == NULL) || (iovcnt < 0) || (iovcnt > IOV_MAX)) {
        return -EINVAL;
    }

    if (iovcnt == 0) {
        return 0;
W
wenjun 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    }

    ret = UserIovCopy(&iovRet, iov, iovcnt, &valid_iovcnt);
    if (ret != 0) {
        return ret;
    }

    if (valid_iovcnt <= 0) {
        ret = -EFAULT;
        goto OUT_FREE;
    }

M
mamingshuai 已提交
2133
    ret = preadv(fd, iovRet, valid_iovcnt, offsetflag);
W
wenjun 已提交
2134 2135 2136 2137 2138
    if (ret < 0) {
        ret = -get_errno();
    }

OUT_FREE:
M
mamingshuai 已提交
2139
    (void)(void)LOS_MemFree(OS_SYS_MEM_ADDR, iovRet);
W
wenjun 已提交
2140 2141 2142
    return ret;
}

M
mamingshuai 已提交
2143
ssize_t SysPwritev(int fd, const struct iovec *iov, int iovcnt, long loffset, long hoffset)
W
wenjun 已提交
2144
{
M
mamingshuai 已提交
2145 2146
    off_t offsetflag;
    offsetflag = (off_t)((unsigned long long)loffset | (((unsigned long long)hoffset) << HIGH_SHIFT_BIT));
W
wenjun 已提交
2147 2148 2149 2150 2151 2152
    int ret;
    int valid_iovcnt = -1;
    struct iovec *iovRet = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);
M
mucor 已提交
2153 2154 2155 2156 2157 2158
    if ((iov == NULL) || (iovcnt < 0) || (iovcnt > IOV_MAX)) {
        return -EINVAL;
    }

    if (iovcnt == 0) {
        return 0;
W
wenjun 已提交
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    }

    ret = UserIovCopy(&iovRet, iov, iovcnt, &valid_iovcnt);
    if (ret != 0) {
        return ret;
    }

    if (valid_iovcnt != iovcnt) {
        ret = -EFAULT;
        goto OUT_FREE;
    }

M
mamingshuai 已提交
2171
    ret = pwritev(fd, iovRet, valid_iovcnt, offsetflag);
W
wenjun 已提交
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
    if (ret < 0) {
        ret = -get_errno();
    }

OUT_FREE:
    (void)LOS_MemFree(OS_SYS_MEM_ADDR, iovRet);
    return ret;
}

#ifdef LOSCFG_FS_FAT
int SysFormat(const char *dev, int sectors, int option)
{
    int ret;
    char *devRet = NULL;

    if (!IsCapPermit(CAP_FS_FORMAT)) {
        return -EPERM;
    }

    if (dev != NULL) {
        ret = UserPathCopy(dev, &devRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = format((dev ? devRet : NULL), sectors, option);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (devRet != NULL) {
M
mamingshuai 已提交
2205
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, devRet);
W
wenjun 已提交
2206 2207 2208 2209 2210
    }
    return ret;
}
#endif

2211
int SysFstat64(int fd, struct kstat *buf)
W
wenjun 已提交
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
{
    int ret;
    struct stat64 bufRet = {0};

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = fstat64(fd, (buf ? (&bufRet) : NULL));
    if (ret < 0) {
        return -get_errno();
    }

2224
    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct kstat));
W
wenjun 已提交
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
    if (ret != 0) {
        return -EFAULT;
    }

    return ret;
}

int SysFcntl64(int fd, int cmd, void *arg)
{
    /* Process fd convert to system global fd */
2235
    int sysfd = GetAssociatedSystemFd(fd);
W
wenjun 已提交
2236

2237 2238 2239
    int ret = VfsFcntl(fd, cmd, arg);
    if (ret == CONTINE_NUTTX_FCNTL) {
        ret = fcntl64(sysfd, cmd, arg);
W
wenjun 已提交
2240
    }
G
Guangyao Ma 已提交
2241

W
wenjun 已提交
2242 2243 2244 2245 2246 2247 2248 2249
    if (ret < 0) {
        return -get_errno();
    }
    return ret;
}

int SysGetdents64(int fd, struct dirent *de_user, unsigned int count)
{
M
mamingshuai 已提交
2250
    if (!LOS_IsUserAddressRange((VADDR_T)(UINTPTR)de_user, count)) {
W
wenjun 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
        return -EFAULT;
    }

    struct dirent *de_knl = NULL;

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    int ret = do_readdir(fd, &de_knl, count);
    if (ret < 0) {
        return ret;
    }
    if (de_knl != NULL) {
M
mamingshuai 已提交
2264
        int cpy_ret = LOS_ArchCopyToUser(de_user, de_knl, ret);
W
wenjun 已提交
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
        if (cpy_ret != 0)
        {
            return -EFAULT;
        }
    }
    return ret;
}

char *SysRealpath(const char *path, char *resolved_path)
{
    char *pathRet = NULL;
    char *resolved_pathRet = NULL;
    char *result = NULL;
    int ret;

    if (resolved_path == NULL) {
        return (char *)(intptr_t)-EINVAL;
    }

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            result = (char *)(intptr_t)ret;
            goto OUT;
        }
    }

    resolved_pathRet = realpath((path ? pathRet : NULL), NULL);
    if (resolved_pathRet == NULL) {
        result = (char *)(intptr_t)-get_errno();
        goto OUT;
    }

    ret = LOS_ArchCopyToUser(resolved_path, resolved_pathRet, strlen(resolved_pathRet) + 1);
    if (ret != 0) {
        result = (char *)(intptr_t)-EFAULT;
        goto OUT;
    }
    result = resolved_path;

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
2307
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
2308 2309
    }
    if (resolved_pathRet != NULL) {
M
mamingshuai 已提交
2310
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, resolved_pathRet);
W
wenjun 已提交
2311 2312 2313 2314
    }
    return result;
}

W
wangchen 已提交
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
int SysUtimensat(int fd, const char *path, struct timespec times[TIMESPEC_TIMES_NUM], int flag)
{
    int ret;
    int timeLen;
    struct IATTR attr = {0};
    char *filePath = NULL;

    timeLen = TIMESPEC_TIMES_NUM * sizeof(struct timespec);
    CHECK_ASPACE(times, timeLen);
    DUP_FROM_USER(times, timeLen);
    ret = CheckNewAttrTime(&attr, times);
    FREE_DUP(times);
    if (ret < 0) {
        goto OUT;
    }

    ret = GetFullpathNull(fd, path, &filePath);
    if (ret < 0) {
        goto OUT;
    }

    ret = chattr(filePath, &attr);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    PointerFree(filePath);
    return ret;
}

W
wenjun 已提交
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
int SysChmod(const char *pathname, mode_t mode)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

2358
    ret = chmod(pathRet, mode);
W
wenjun 已提交
2359 2360 2361 2362 2363 2364
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
2365
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
2366 2367 2368 2369
    }
    return ret;
}

W
wangchen 已提交
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
int SysFchmodat(int fd, const char *path, mode_t mode, int flag)
{
    int ret;
    char *pathRet = NULL;
    char *fullpath = NULL;
    struct IATTR attr = {
        .attr_chg_mode = mode,
        .attr_chg_valid = CHG_MODE,
    };

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (fd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        fd = GetAssociatedSystemFd(fd);
    }

    ret = vfs_normalize_pathat(fd, pathRet, &fullpath);
    if (ret < 0) {
        goto OUT;
    }

    ret = chattr(fullpath, &attr);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    PointerFree(pathRet);
    PointerFree(fullpath);

    return ret;
}

W
wcc0 已提交
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
int SysFchmod(int fd, mode_t mode)
{
    int ret;
    int sysFd;
    struct IATTR attr = {
        .attr_chg_mode = mode,
        .attr_chg_valid = CHG_MODE, /* change mode */
    };
    struct file *file = NULL;

    sysFd = GetAssociatedSystemFd(fd);
    if (sysFd < 0) {
        return -EBADF;
    }

    ret = fs_getfilep(sysFd, &file);
    if (ret < 0) {
        return -get_errno();
    }

    ret = chattr(file->f_path, &attr);
    if (ret < 0) {
        return -get_errno();
    }

    return ret;
}

W
wangchen 已提交
2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
int SysFchownat(int fd, const char *path, uid_t owner, gid_t group, int flag)
{
    int ret;
    char *fullpath = NULL;
    struct IATTR attr = {
        .attr_chg_valid = 0,
    };

    ret = GetFullpath(fd, path, &fullpath);
    if (ret < 0) {
        goto OUT;
    }

    if (owner != (uid_t)-1) {
        attr.attr_chg_uid = owner;
        attr.attr_chg_valid |= CHG_UID;
    }
    if (group != (gid_t)-1) {
        attr.attr_chg_gid = group;
        attr.attr_chg_valid |= CHG_GID;
    }

    ret = chattr(fullpath, &attr);
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    PointerFree(fullpath);

    return ret;
}

int SysFchown(int fd, uid_t owner, gid_t group)
{
    int ret;
    int sysFd;
    struct IATTR attr = {0};
    attr.attr_chg_valid = 0;
    struct file *file = NULL;

    sysFd = GetAssociatedSystemFd(fd);
    if (sysFd < 0) {
        return -EBADF;
    }

    ret = fs_getfilep(sysFd, &file);
    if (ret < 0) {
        return -get_errno();
    }

    if (owner != (uid_t)-1) {
        attr.attr_chg_uid = owner;
        attr.attr_chg_valid |= CHG_UID;
    }
    if (group != (gid_t)-1) {
        attr.attr_chg_gid = group;
        attr.attr_chg_valid |= CHG_GID;
    }
    ret = chattr(file->f_path, &attr);
    if (ret < 0) {
        ret = -get_errno();
    }

    return ret;
}

W
wenjun 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
int SysChown(const char *pathname, uid_t owner, gid_t group)
{
    int ret;
    char *pathRet = NULL;

    if (pathname != NULL) {
        ret = UserPathCopy(pathname, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

2516
    ret = chown(pathRet, owner, group);
W
wenjun 已提交
2517 2518 2519 2520 2521 2522
    if (ret < 0) {
        ret = -get_errno();
    }

OUT:
    if (pathRet != NULL) {
M
mamingshuai 已提交
2523
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
W
wenjun 已提交
2524 2525 2526
    }
    return ret;
}
G
Guangyao Ma 已提交
2527

2528
int SysFstatat64(int dirfd, const char *restrict path, struct kstat *restrict buf, int flag)
G
Guangyao Ma 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
{
    int ret;
    struct stat bufRet = {0};
    char *pathRet = NULL;
    char *fullpath = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    if (dirfd != AT_FDCWD) {
        /* Process fd convert to system global fd */
        dirfd = GetAssociatedSystemFd(dirfd);
    }

    ret = vfs_normalize_pathat(dirfd, pathRet, &fullpath);
    if (ret < 0) {
        goto OUT;
    }

    ret = stat(fullpath, &bufRet);
    if (ret < 0) {
        ret = -get_errno();
        goto OUT;
    }

2558
    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct kstat));
G
Guangyao Ma 已提交
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
    if (ret != 0) {
        ret = -EFAULT;
        goto OUT;
    }

OUT:
    if (pathRet != NULL) {
        LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }

    if (fullpath != NULL) {
        free(fullpath);
    }
    return ret;
}
W
wangchen 已提交
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612

int SysFaccessat(int fd, const char *filename, int amode, int flag)
{
    int ret;
    struct stat buf;
    struct statfs fsBuf;
    char *fullDirectory = NULL;

    ret = GetFullpath(fd, filename, &fullDirectory);
    if (ret < 0) {
        goto OUT;
    }

    ret = statfs(fullDirectory, &fsBuf);
    if (ret != 0) {
        ret = -get_errno();
        goto OUT;
    }

    if ((fsBuf.f_flags & MS_RDONLY) && ((unsigned int)amode & W_OK)) {
        ret = -EROFS;
        goto OUT;
    }

    ret = stat(fullDirectory, &buf);
    if (ret != 0) {
        ret = -get_errno();
        goto OUT;
    }

    if (VfsPermissionCheck(buf.st_uid, buf.st_gid, buf.st_mode, amode)) {
        ret = -EACCES;
    }

OUT:
    PointerFree(fullDirectory);

    return ret;
}
W
wcc0 已提交
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644

int SysFstatfs(int fd, struct statfs *buf)
{
    int ret;
    struct file *filep = NULL;
    struct statfs bufRet = {0};

    /* Process fd convert to system global fd */
    fd = GetAssociatedSystemFd(fd);

    ret = fs_getfilep(fd, &filep);
    if (ret < 0) {
        ret = -get_errno();
        return ret;
    }

    ret = statfs(filep->f_path, &bufRet);
    if (ret < 0) {
        ret = -get_errno();
        return ret;
    }

    ret = LOS_ArchCopyToUser(buf, &bufRet, sizeof(struct statfs));
    if (ret != 0) {
        ret = -EFAULT;
    }

    return ret;
}

int SysFstatfs64(int fd, size_t sz, struct statfs *buf)
{
L
lnlan 已提交
2645
    int ret;
W
wcc0 已提交
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655

    if (sz != sizeof(struct statfs)) {
        ret = -EINVAL;
        return ret;
    }

    ret = SysFstatfs(fd, buf);

    return ret;
}
2656 2657 2658

int SysPpoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigMask, int nsig)
{
2659 2660 2661
    int timeout, retVal;
    sigset_t_l origMask = {0};
    sigset_t_l set = {0};
2662 2663

    CHECK_ASPACE(tmo_p, sizeof(struct timespec));
2664
    CPY_FROM_USER(tmo_p);
T
teamol 已提交
2665 2666 2667

    if (tmo_p != NULL) {
        timeout = tmo_p->tv_sec * OS_SYS_US_PER_MS + tmo_p->tv_nsec / OS_SYS_NS_PER_MS;
2668 2669
        if (timeout < 0) {
            return -EINVAL;
T
teamol 已提交
2670 2671 2672 2673
        }
    } else {
        timeout = -1;
    }
2674

T
teamol 已提交
2675
    if (sigMask != NULL) {
2676 2677 2678 2679 2680 2681 2682
        retVal = LOS_ArchCopyFromUser(&set, sigMask, sizeof(sigset_t));
        if (retVal != 0) {
            return -EFAULT;
        }
        (VOID)OsSigprocMask(SIG_SETMASK, &set, &origMask);
    } else {
        (VOID)OsSigprocMask(SIG_SETMASK, NULL, &origMask);
2683 2684
    }

2685 2686 2687
    retVal = SysPoll(fds, nfds, timeout);
    (VOID)OsSigprocMask(SIG_SETMASK, &origMask, NULL);

2688
    return retVal;
2689
}
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708

int SysPselect6(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    const struct timespec *timeout, const long data[2])
{
    int ret;
    int retVal;
    sigset_t_l origMask;
    sigset_t_l setl;

    CHECK_ASPACE(readfds, sizeof(fd_set));
    CHECK_ASPACE(writefds, sizeof(fd_set));
    CHECK_ASPACE(exceptfds, sizeof(fd_set));
    CHECK_ASPACE(timeout, sizeof(struct timeval));

    CPY_FROM_USER(readfds);
    CPY_FROM_USER(writefds);
    CPY_FROM_USER(exceptfds);
    DUP_FROM_USER(timeout, sizeof(struct timeval));

W
wangchen 已提交
2709 2710 2711
    if (timeout != NULL) {
        ((struct timeval *)timeout)->tv_usec = timeout->tv_nsec / 1000; /* 1000, convert ns to us */
    }
2712 2713

    if (data != NULL) {
X
x-xiny 已提交
2714
        retVal = LOS_ArchCopyFromUser(&(setl.sig[0]), (int *)((UINTPTR)data[0]), sizeof(sigset_t));
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
        if (retVal != 0) {
            ret = -EFAULT;
            FREE_DUP(timeout);
            return ret;
        }
    }

    OsSigprocMask(SIG_SETMASK, &setl, &origMask);
    ret = do_select(nfds, readfds, writefds, exceptfds, (struct timeval *)timeout, UserPoll);
    if (ret < 0) {
        /* do not copy parameter back to user mode if do_select failed */
        ret = -get_errno();
        FREE_DUP(timeout);
        return ret;
    }
    OsSigprocMask(SIG_SETMASK, &origMask, NULL);

    CPY_TO_USER(readfds);
    CPY_TO_USER(writefds);
    CPY_TO_USER(exceptfds);
    FREE_DUP(timeout);

    return ret;
}
L
lnlan 已提交
2739

2740
static int DoEpollCreate1(int flags)
L
lnlan 已提交
2741 2742 2743 2744
{
    int ret;
    int procFd;

2745
    ret = epoll_create1(flags);
L
lnlan 已提交
2746 2747
    if (ret < 0) {
        ret = -get_errno();
2748
        return ret;
L
lnlan 已提交
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
    }

    procFd = AllocAndAssocProcessFd((INTPTR)(ret), MIN_START_FD);
    if (procFd == -1) {
        epoll_close(ret);
        return -EMFILE;
    }

    return procFd;
}

2760
int SysEpollCreate(int size)
L
lnlan 已提交
2761
{
2762 2763 2764
    (void)size;
    return DoEpollCreate1(0);
}
L
lnlan 已提交
2765

2766 2767 2768
int SysEpollCreate1(int flags)
{
    return DoEpollCreate1(flags);
L
lnlan 已提交
2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
}

int SysEpollCtl(int epfd, int op, int fd, struct epoll_event *ev)
{
    int ret;

    CHECK_ASPACE(ev, sizeof(struct epoll_event));
    CPY_FROM_USER(ev);

    fd = GetAssociatedSystemFd(fd);
    epfd = GetAssociatedSystemFd(epfd);
    if ((fd < 0) || (epfd < 0)) {
        ret = -EBADF;
        goto OUT;
    }

    ret = epoll_ctl(epfd, op, fd, ev);
    if (ret < 0) {
L
lnlan 已提交
2787
        ret = -EBADF;
L
lnlan 已提交
2788 2789 2790 2791 2792
        goto OUT;
    }

    CPY_TO_USER(ev);
OUT:
L
lnlan 已提交
2793
    return (ret == -1) ? -get_errno() : ret;
L
lnlan 已提交
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855
}

int SysEpollWait(int epfd, struct epoll_event *evs, int maxevents, int timeout)
{
    int ret = 0;

    CHECK_ASPACE(evs, sizeof(struct epoll_event));
    CPY_FROM_USER(evs);

    epfd = GetAssociatedSystemFd(epfd);
    if  (epfd < 0) {
        ret = -EBADF;
        goto OUT;
    }

    ret = epoll_wait(epfd, evs, maxevents, timeout);
    if (ret < 0) {
        ret = -get_errno();
    }

    CPY_TO_USER(evs);
OUT:
    return (ret == -1) ? -get_errno() : ret;
}

int SysEpollPwait(int epfd, struct epoll_event *evs, int maxevents, int timeout, const sigset_t *mask)
{
    sigset_t_l origMask;
    sigset_t_l setl;
    int ret = 0;

    CHECK_ASPACE(mask, sizeof(sigset_t));

    if (mask != NULL) {
        ret = LOS_ArchCopyFromUser(&setl, mask, sizeof(sigset_t));
        if (ret != 0) {
            return -EFAULT;
        }
    }

    CHECK_ASPACE(evs, sizeof(struct epoll_event));
    CPY_FROM_USER(evs);

    epfd = GetAssociatedSystemFd(epfd);
    if (epfd < 0) {
        ret = -EBADF;
        goto OUT;
    }

    OsSigprocMask(SIG_SETMASK, &setl, &origMask);
    ret = epoll_wait(epfd, evs, maxevents, timeout);
    if (ret < 0) {
        ret = -get_errno();
    }

    OsSigprocMask(SIG_SETMASK, &origMask, NULL);

    CPY_TO_USER(evs);
OUT:
    return (ret == -1) ? -get_errno() : ret;
}

2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
#ifdef LOSCFG_CHROOT
int SysChroot(const char *path)
{
    int ret;
    char *pathRet = NULL;

    if (path != NULL) {
        ret = UserPathCopy(path, &pathRet);
        if (ret != 0) {
            goto OUT;
        }
    }

    ret = chroot(path ? pathRet : NULL);
    if (ret < 0) {
        ret = -get_errno();
    }
OUT:
    if (pathRet != NULL) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, pathRet);
    }
    return ret;
}
#endif
W
wenjun 已提交
2880
#endif