1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/io.c
5 * Copyright (C) 2018, Google LLC.
12 * A wrapper for write(2), that automatically handles the following
15 * + Interrupted system call (EINTR)
16 * + Write of less than requested amount
17 * + Non-block return (EAGAIN)
19 * For each of the above, an additional write is performed to automatically
20 * continue writing the requested data.
21 * There are also many cases where write(2) can return an unexpected
22 * error (e.g. EIO). Such errors cause a TEST_ASSERT failure.
24 * Note, for function signature compatibility with write(2), this function
25 * returns the number of bytes written, but that value will always be equal
26 * to the number of requested bytes. All other conditions in this and
27 * future enhancements to this function either automatically issue another
28 * write(2) or cause a TEST_ASSERT failure.
31 * fd - Opened file descriptor to file to be written.
32 * count - Number of bytes to write.
35 * buf - Starting address of data to be written.
38 * On success, number of bytes written.
39 * On failure, a TEST_ASSERT failure is caused.
41 ssize_t test_write(int fd, const void *buf, size_t count)
44 ssize_t num_written = 0;
45 size_t num_left = count;
46 const char *ptr = buf;
48 /* Note: Count of zero is allowed (see "RETURN VALUE" portion of
49 * write(2) manpage for details.
51 TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
54 rc = write(fd, ptr, num_left);
58 TEST_ASSERT(errno == EAGAIN || errno == EINTR,
59 "Unexpected write failure,\n"
60 " rc: %zi errno: %i", rc, errno);
64 TEST_FAIL("Unexpected EOF,\n"
65 " rc: %zi num_written: %zi num_left: %zu",
66 rc, num_written, num_left);
70 TEST_ASSERT(rc >= 0, "Unexpected ret from write,\n"
71 " rc: %zi errno: %i", rc, errno);
77 } while (num_written < count);
84 * A wrapper for read(2), that automatically handles the following
87 * + Interrupted system call (EINTR)
88 * + Read of less than requested amount
89 * + Non-block return (EAGAIN)
91 * For each of the above, an additional read is performed to automatically
92 * continue reading the requested data.
93 * There are also many cases where read(2) can return an unexpected
94 * error (e.g. EIO). Such errors cause a TEST_ASSERT failure. Note,
95 * it is expected that the file opened by fd at the current file position
96 * contains at least the number of requested bytes to be read. A TEST_ASSERT
97 * failure is produced if an End-Of-File condition occurs, before all the
98 * data is read. It is the callers responsibility to assure that sufficient
101 * Note, for function signature compatibility with read(2), this function
102 * returns the number of bytes read, but that value will always be equal
103 * to the number of requested bytes. All other conditions in this and
104 * future enhancements to this function either automatically issue another
105 * read(2) or cause a TEST_ASSERT failure.
108 * fd - Opened file descriptor to file to be read.
109 * count - Number of bytes to read.
112 * buf - Starting address of where to write the bytes read.
115 * On success, number of bytes read.
116 * On failure, a TEST_ASSERT failure is caused.
118 ssize_t test_read(int fd, void *buf, size_t count)
121 ssize_t num_read = 0;
122 size_t num_left = count;
125 /* Note: Count of zero is allowed (see "If count is zero" portion of
126 * read(2) manpage for details.
128 TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count);
131 rc = read(fd, ptr, num_left);
135 TEST_ASSERT(errno == EAGAIN || errno == EINTR,
136 "Unexpected read failure,\n"
137 " rc: %zi errno: %i", rc, errno);
141 TEST_FAIL("Unexpected EOF,\n"
142 " rc: %zi num_read: %zi num_left: %zu",
143 rc, num_read, num_left);
147 TEST_ASSERT(rc > 0, "Unexpected ret from read,\n"
148 " rc: %zi errno: %i", rc, errno);
154 } while (num_read < count);