* @brief Reads data from a file descriptor into a buffer. * * This system call reads up to `nbyte` bytes of data from the file descriptor * specified by `fd` into the buffer pointed to by `buf`. * * @param fd The file descriptor to read from. This should be a valid file * descriptor obtained through system calls like `open()` or `socket()`. * @param buf A pointer to the buffer whe
| 421 | * of bytes, as failing to do so may result in undefined behavior. |
| 422 | */ |
| 423 | ssize_t sys_read(int fd, void *buf, size_t nbyte) |
| 424 | { |
| 425 | #ifdef ARCH_MM_MMU |
| 426 | void *kmem = RT_NULL; |
| 427 | ssize_t ret = -1; |
| 428 | |
| 429 | if (!nbyte) |
| 430 | { |
| 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | if (!lwp_user_accessable((void *)buf, nbyte)) |
| 435 | { |
| 436 | return -EFAULT; |
| 437 | } |
| 438 | |
| 439 | kmem = kmem_get(nbyte); |
| 440 | if (!kmem) |
| 441 | { |
| 442 | return -ENOMEM; |
| 443 | } |
| 444 | |
| 445 | ret = read(fd, kmem, nbyte); |
| 446 | if (ret > 0) |
| 447 | { |
| 448 | if (ret != lwp_put_to_user(buf, kmem, ret)) |
| 449 | return -EFAULT; |
| 450 | } |
| 451 | |
| 452 | if (ret < 0) |
| 453 | { |
| 454 | ret = GET_ERRNO(); |
| 455 | } |
| 456 | |
| 457 | kmem_put(kmem); |
| 458 | |
| 459 | return ret; |
| 460 | #else |
| 461 | if (!lwp_user_accessable((void *)buf, nbyte)) |
| 462 | { |
| 463 | return -EFAULT; |
| 464 | } |
| 465 | ssize_t ret = read(fd, buf, nbyte); |
| 466 | return (ret < 0 ? GET_ERRNO() : ret); |
| 467 | #endif |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * @brief Writes data from a buffer to a file descriptor. |
nothing calls this directly
no test coverage detected