* @brief Opens or creates a file, returning a file descriptor. * * This system call opens the file specified by `name` with the specified * access mode and flags. If the file does not exist and the `O_CREAT` flag * is provided, it will create the file with the specified mode. * * @param name The path to the file to be opened. This can be an absolute or * relative path. * @param
| 594 | * @see sys_close(), sys_read(), sys_write(), open() |
| 595 | */ |
| 596 | sysret_t sys_open(const char *name, int flag, ...) |
| 597 | { |
| 598 | #ifdef ARCH_MM_MMU |
| 599 | int ret = -1; |
| 600 | rt_size_t len = 0; |
| 601 | char *kname = RT_NULL; |
| 602 | mode_t mode = 0; |
| 603 | |
| 604 | if (!lwp_user_accessable((void *)name, 1)) |
| 605 | { |
| 606 | return -EFAULT; |
| 607 | } |
| 608 | |
| 609 | len = lwp_user_strlen(name); |
| 610 | if (!len) |
| 611 | { |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | kname = (char *)kmem_get(len + 1); |
| 616 | if (!kname) |
| 617 | { |
| 618 | return -ENOMEM; |
| 619 | } |
| 620 | |
| 621 | if ((flag & O_CREAT) || (flag & O_TMPFILE) == O_TMPFILE) |
| 622 | { |
| 623 | va_list ap; |
| 624 | va_start(ap, flag); |
| 625 | mode = va_arg(ap, mode_t); |
| 626 | va_end(ap); |
| 627 | } |
| 628 | |
| 629 | if (lwp_get_from_user(kname, (void *)name, len + 1) != (len + 1)) |
| 630 | { |
| 631 | kmem_put(kname); |
| 632 | return -EINVAL; |
| 633 | } |
| 634 | |
| 635 | ret = open(kname, flag, mode); |
| 636 | if (ret < 0) |
| 637 | { |
| 638 | ret = GET_ERRNO(); |
| 639 | } |
| 640 | |
| 641 | kmem_put(kname); |
| 642 | |
| 643 | return ret; |
| 644 | #else |
| 645 | int ret; |
| 646 | mode_t mode = 0; |
| 647 | |
| 648 | if (!lwp_user_accessable((void *)name, 1)) |
| 649 | { |
| 650 | return -EFAULT; |
| 651 | } |
| 652 | |
| 653 | if ((flag & O_CREAT) || (flag & O_TMPFILE) == O_TMPFILE) |
nothing calls this directly
no test coverage detected