uio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. uio.c
  9. Abstract:
  10. This module implements the user I/O vector read and write routines.
  11. Author:
  12. Evan Green 23-Jan-2015
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <sys/uio.h>
  23. #include <unistd.h>
  24. //
  25. // ---------------------------------------------------------------- Definitions
  26. //
  27. //
  28. // ------------------------------------------------------ Data Type Definitions
  29. //
  30. //
  31. // ----------------------------------------------- Internal Function Prototypes
  32. //
  33. //
  34. // -------------------------------------------------------------------- Globals
  35. //
  36. //
  37. // ------------------------------------------------------------------ Functions
  38. //
  39. LIBC_API
  40. ssize_t
  41. readv (
  42. int FileDescriptor,
  43. const struct iovec *IoVector,
  44. int IoVectorCount
  45. )
  46. /*++
  47. Routine Description:
  48. This routine is equivalent to the read function, except that it places data
  49. into the buffers specified by the given I/O vector array.
  50. Arguments:
  51. FileDescriptor - Supplies the file descriptor to read from.
  52. IoVector - Supplies a pointer to an array of I/O vectors.
  53. IoVectorCount - Supplies the number of elements in the I/O vector array.
  54. That is, IoVector[IoVectorCount - 1] will be the last array element
  55. accessed.
  56. Return Value:
  57. Returns the same values a read would (the number of bytes read on success,
  58. or -1 on error with errno set to contain more information).
  59. --*/
  60. {
  61. UINTN BytesCompleted;
  62. UINTN Index;
  63. UINTN Size;
  64. KSTATUS Status;
  65. Size = 0;
  66. for (Index = 0; Index < IoVectorCount; Index += 1) {
  67. Size += IoVector[Index].iov_len;
  68. }
  69. Status = OsPerformVectoredIo((HANDLE)(INTN)FileDescriptor,
  70. IO_OFFSET_NONE,
  71. Size,
  72. 0,
  73. SYS_WAIT_TIME_INDEFINITE,
  74. (PIO_VECTOR)IoVector,
  75. IoVectorCount,
  76. &BytesCompleted);
  77. if (Status == STATUS_TIMEOUT) {
  78. errno = EAGAIN;
  79. return -1;
  80. } else if ((!KSUCCESS(Status)) && (Status != STATUS_END_OF_FILE)) {
  81. errno = ClConvertKstatusToErrorNumber(Status);
  82. if (BytesCompleted == 0) {
  83. BytesCompleted = -1;
  84. }
  85. }
  86. return (ssize_t)BytesCompleted;
  87. }
  88. LIBC_API
  89. ssize_t
  90. writev (
  91. int FileDescriptor,
  92. const struct iovec *IoVector,
  93. int IoVectorCount
  94. )
  95. /*++
  96. Routine Description:
  97. This routine is equivalent to the write function, except that it reads data
  98. from the buffers specified by the given I/O vector array.
  99. Arguments:
  100. FileDescriptor - Supplies the file descriptor to write to.
  101. IoVector - Supplies a pointer to an array of I/O vectors.
  102. IoVectorCount - Supplies the number of elements in the I/O vector array.
  103. That is, IoVector[IoVectorCount - 1] will be the last array element
  104. accessed.
  105. Return Value:
  106. Returns the same values a write would (the number of bytes written on
  107. success, or -1 on error with errno set to contain more information).
  108. --*/
  109. {
  110. UINTN BytesCompleted;
  111. UINTN Index;
  112. UINTN Size;
  113. KSTATUS Status;
  114. Size = 0;
  115. for (Index = 0; Index < IoVectorCount; Index += 1) {
  116. Size += IoVector[Index].iov_len;
  117. }
  118. Status = OsPerformVectoredIo((HANDLE)(INTN)FileDescriptor,
  119. IO_OFFSET_NONE,
  120. Size,
  121. SYS_IO_FLAG_WRITE,
  122. SYS_WAIT_TIME_INDEFINITE,
  123. (PIO_VECTOR)IoVector,
  124. IoVectorCount,
  125. &BytesCompleted);
  126. if (Status == STATUS_TIMEOUT) {
  127. errno = EAGAIN;
  128. return -1;
  129. } else if ((!KSUCCESS(Status)) && (Status != STATUS_END_OF_FILE)) {
  130. errno = ClConvertKstatusToErrorNumber(Status);
  131. if (BytesCompleted == 0) {
  132. BytesCompleted = -1;
  133. }
  134. }
  135. return (ssize_t)BytesCompleted;
  136. }
  137. //
  138. // --------------------------------------------------------- Internal Functions
  139. //