![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0-only 0002 /* 0003 * tools/testing/selftests/kvm/lib/io.c 0004 * 0005 * Copyright (C) 2018, Google LLC. 0006 */ 0007 0008 #include "test_util.h" 0009 0010 /* Test Write 0011 * 0012 * A wrapper for write(2), that automatically handles the following 0013 * special conditions: 0014 * 0015 * + Interrupted system call (EINTR) 0016 * + Write of less than requested amount 0017 * + Non-block return (EAGAIN) 0018 * 0019 * For each of the above, an additional write is performed to automatically 0020 * continue writing the requested data. 0021 * There are also many cases where write(2) can return an unexpected 0022 * error (e.g. EIO). Such errors cause a TEST_ASSERT failure. 0023 * 0024 * Note, for function signature compatibility with write(2), this function 0025 * returns the number of bytes written, but that value will always be equal 0026 * to the number of requested bytes. All other conditions in this and 0027 * future enhancements to this function either automatically issue another 0028 * write(2) or cause a TEST_ASSERT failure. 0029 * 0030 * Args: 0031 * fd - Opened file descriptor to file to be written. 0032 * count - Number of bytes to write. 0033 * 0034 * Output: 0035 * buf - Starting address of data to be written. 0036 * 0037 * Return: 0038 * On success, number of bytes written. 0039 * On failure, a TEST_ASSERT failure is caused. 0040 */ 0041 ssize_t test_write(int fd, const void *buf, size_t count) 0042 { 0043 ssize_t rc; 0044 ssize_t num_written = 0; 0045 size_t num_left = count; 0046 const char *ptr = buf; 0047 0048 /* Note: Count of zero is allowed (see "RETURN VALUE" portion of 0049 * write(2) manpage for details. 0050 */ 0051 TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count); 0052 0053 do { 0054 rc = write(fd, ptr, num_left); 0055 0056 switch (rc) { 0057 case -1: 0058 TEST_ASSERT(errno == EAGAIN || errno == EINTR, 0059 "Unexpected write failure,\n" 0060 " rc: %zi errno: %i", rc, errno); 0061 continue; 0062 0063 case 0: 0064 TEST_FAIL("Unexpected EOF,\n" 0065 " rc: %zi num_written: %zi num_left: %zu", 0066 rc, num_written, num_left); 0067 break; 0068 0069 default: 0070 TEST_ASSERT(rc >= 0, "Unexpected ret from write,\n" 0071 " rc: %zi errno: %i", rc, errno); 0072 num_written += rc; 0073 num_left -= rc; 0074 ptr += rc; 0075 break; 0076 } 0077 } while (num_written < count); 0078 0079 return num_written; 0080 } 0081 0082 /* Test Read 0083 * 0084 * A wrapper for read(2), that automatically handles the following 0085 * special conditions: 0086 * 0087 * + Interrupted system call (EINTR) 0088 * + Read of less than requested amount 0089 * + Non-block return (EAGAIN) 0090 * 0091 * For each of the above, an additional read is performed to automatically 0092 * continue reading the requested data. 0093 * There are also many cases where read(2) can return an unexpected 0094 * error (e.g. EIO). Such errors cause a TEST_ASSERT failure. Note, 0095 * it is expected that the file opened by fd at the current file position 0096 * contains at least the number of requested bytes to be read. A TEST_ASSERT 0097 * failure is produced if an End-Of-File condition occurs, before all the 0098 * data is read. It is the callers responsibility to assure that sufficient 0099 * data exists. 0100 * 0101 * Note, for function signature compatibility with read(2), this function 0102 * returns the number of bytes read, but that value will always be equal 0103 * to the number of requested bytes. All other conditions in this and 0104 * future enhancements to this function either automatically issue another 0105 * read(2) or cause a TEST_ASSERT failure. 0106 * 0107 * Args: 0108 * fd - Opened file descriptor to file to be read. 0109 * count - Number of bytes to read. 0110 * 0111 * Output: 0112 * buf - Starting address of where to write the bytes read. 0113 * 0114 * Return: 0115 * On success, number of bytes read. 0116 * On failure, a TEST_ASSERT failure is caused. 0117 */ 0118 ssize_t test_read(int fd, void *buf, size_t count) 0119 { 0120 ssize_t rc; 0121 ssize_t num_read = 0; 0122 size_t num_left = count; 0123 char *ptr = buf; 0124 0125 /* Note: Count of zero is allowed (see "If count is zero" portion of 0126 * read(2) manpage for details. 0127 */ 0128 TEST_ASSERT(count >= 0, "Unexpected count, count: %li", count); 0129 0130 do { 0131 rc = read(fd, ptr, num_left); 0132 0133 switch (rc) { 0134 case -1: 0135 TEST_ASSERT(errno == EAGAIN || errno == EINTR, 0136 "Unexpected read failure,\n" 0137 " rc: %zi errno: %i", rc, errno); 0138 break; 0139 0140 case 0: 0141 TEST_FAIL("Unexpected EOF,\n" 0142 " rc: %zi num_read: %zi num_left: %zu", 0143 rc, num_read, num_left); 0144 break; 0145 0146 default: 0147 TEST_ASSERT(rc > 0, "Unexpected ret from read,\n" 0148 " rc: %zi errno: %i", rc, errno); 0149 num_read += rc; 0150 num_left -= rc; 0151 ptr += rc; 0152 break; 0153 } 0154 } while (num_read < count); 0155 0156 return num_read; 0157 }
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |