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