io.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * tools/testing/selftests/kvm/lib/io.c
  3. *
  4. * Copyright (C) 2018, Google LLC.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2.
  7. */
  8. #include "test_util.h"
  9. /* Test Write
  10. *
  11. * A wrapper for write(2), that automatically handles the following
  12. * special conditions:
  13. *
  14. * + Interrupted system call (EINTR)
  15. * + Write of less than requested amount
  16. * + Non-block return (EAGAIN)
  17. *
  18. * For each of the above, an additional write is performed to automatically
  19. * continue writing the requested data.
  20. * There are also many cases where write(2) can return an unexpected
  21. * error (e.g. EIO). Such errors cause a TEST_ASSERT failure.
  22. *
  23. * Note, for function signature compatibility with write(2), this function
  24. * returns the number of bytes written, but that value will always be equal
  25. * to the number of requested bytes. All other conditions in this and
  26. * future enhancements to this function either automatically issue another
  27. * write(2) or cause a TEST_ASSERT failure.
  28. *
  29. * Args:
  30. * fd - Opened file descriptor to file to be written.
  31. * count - Number of bytes to write.
  32. *
  33. * Output:
  34. * buf - Starting address of data to be written.
  35. *
  36. * Return:
  37. * On success, number of bytes written.
  38. * On failure, a TEST_ASSERT failure is caused.
  39. */
  40. ssize_t test_write(int fd, const void *buf, size_t count)
  41. {
  42. ssize_t rc;
  43. ssize_t num_written = 0;
  44. size_t num_left = count;
  45. const char *ptr = buf;
  46. /* Note: Count of zero is allowed (see "RETURN VALUE" portion of
  47. * write(2) manpage for details.
  48. */
  49. TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
  50. do {
  51. rc = write(fd, ptr, num_left);
  52. switch (rc) {
  53. case -1:
  54. TEST_ASSERT(errno == EAGAIN || errno == EINTR,
  55. "Unexpected write failure,\n"
  56. " rc: %zi errno: %i", rc, errno);
  57. continue;
  58. case 0:
  59. TEST_ASSERT(false, "Unexpected EOF,\n"
  60. " rc: %zi num_written: %zi num_left: %zu",
  61. rc, num_written, num_left);
  62. break;
  63. default:
  64. TEST_ASSERT(rc >= 0, "Unexpected ret from write,\n"
  65. " rc: %zi errno: %i", rc, errno);
  66. num_written += rc;
  67. num_left -= rc;
  68. ptr += rc;
  69. break;
  70. }
  71. } while (num_written < count);
  72. return num_written;
  73. }
  74. /* Test Read
  75. *
  76. * A wrapper for read(2), that automatically handles the following
  77. * special conditions:
  78. *
  79. * + Interrupted system call (EINTR)
  80. * + Read of less than requested amount
  81. * + Non-block return (EAGAIN)
  82. *
  83. * For each of the above, an additional read is performed to automatically
  84. * continue reading the requested data.
  85. * There are also many cases where read(2) can return an unexpected
  86. * error (e.g. EIO). Such errors cause a TEST_ASSERT failure. Note,
  87. * it is expected that the file opened by fd at the current file position
  88. * contains at least the number of requested bytes to be read. A TEST_ASSERT
  89. * failure is produced if an End-Of-File condition occurs, before all the
  90. * data is read. It is the callers responsibility to assure that sufficient
  91. * data exists.
  92. *
  93. * Note, for function signature compatibility with read(2), this function
  94. * returns the number of bytes read, but that value will always be equal
  95. * to the number of requested bytes. All other conditions in this and
  96. * future enhancements to this function either automatically issue another
  97. * read(2) or cause a TEST_ASSERT failure.
  98. *
  99. * Args:
  100. * fd - Opened file descriptor to file to be read.
  101. * count - Number of bytes to read.
  102. *
  103. * Output:
  104. * buf - Starting address of where to write the bytes read.
  105. *
  106. * Return:
  107. * On success, number of bytes read.
  108. * On failure, a TEST_ASSERT failure is caused.
  109. */
  110. ssize_t test_read(int fd, void *buf, size_t count)
  111. {
  112. ssize_t rc;
  113. ssize_t num_read = 0;
  114. size_t num_left = count;
  115. char *ptr = buf;
  116. /* Note: Count of zero is allowed (see "If count is zero" portion of
  117. * read(2) manpage for details.
  118. */
  119. TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
  120. do {
  121. rc = read(fd, ptr, num_left);
  122. switch (rc) {
  123. case -1:
  124. TEST_ASSERT(errno == EAGAIN || errno == EINTR,
  125. "Unexpected read failure,\n"
  126. " rc: %zi errno: %i", rc, errno);
  127. break;
  128. case 0:
  129. TEST_ASSERT(false, "Unexpected EOF,\n"
  130. " rc: %zi num_read: %zi num_left: %zu",
  131. rc, num_read, num_left);
  132. break;
  133. default:
  134. TEST_ASSERT(rc > 0, "Unexpected ret from read,\n"
  135. " rc: %zi errno: %i", rc, errno);
  136. num_read += rc;
  137. num_left -= rc;
  138. ptr += rc;
  139. break;
  140. }
  141. } while (num_read < count);
  142. return num_read;
  143. }