MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / sys_read

Function sys_read

components/lwp/lwp_syscall.c:423–468  ·  view source on GitHub ↗

* @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

Source from the content-addressed store, hash-verified

421 * of bytes, as failing to do so may result in undefined behavior.
422 */
423ssize_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.

Callers

nothing calls this directly

Calls 5

lwp_user_accessableFunction · 0.85
kmem_getFunction · 0.85
lwp_put_to_userFunction · 0.85
kmem_putFunction · 0.85
readFunction · 0.50

Tested by

no test coverage detected