io.c 4.3 KB

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