1 | /* |
---|
2 | * Fake write and writev functions for testing xwrite and xwritev. |
---|
3 | * |
---|
4 | * Copyright (c) 2004, 2005, 2006 |
---|
5 | * by Internet Systems Consortium, Inc. ("ISC") |
---|
6 | * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
---|
7 | * 2002, 2003 by The Internet Software Consortium and Rich Salz |
---|
8 | * |
---|
9 | * See LICENSE for licensing terms. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <config.h> |
---|
13 | #include <portable/system.h> |
---|
14 | #include <portable/uio.h> |
---|
15 | |
---|
16 | #include <errno.h> |
---|
17 | |
---|
18 | #include <util/util.h> |
---|
19 | |
---|
20 | ssize_t fake_write(int, const void *, size_t); |
---|
21 | ssize_t fake_pwrite(int, const void *, size_t, off_t); |
---|
22 | ssize_t fake_writev(int, const struct iovec *, int); |
---|
23 | |
---|
24 | /* |
---|
25 | * All the data is actually written into this buffer. We use write_offset |
---|
26 | * to track how far we've written. |
---|
27 | */ |
---|
28 | char write_buffer[256]; |
---|
29 | size_t write_offset = 0; |
---|
30 | |
---|
31 | /* |
---|
32 | * If write_interrupt is non-zero, then half of the calls to write or writev |
---|
33 | * will fail, returning -1 with errno set to EINTR. |
---|
34 | */ |
---|
35 | int write_interrupt = 0; |
---|
36 | |
---|
37 | /* |
---|
38 | * If write_fail is non-zero, all writes or writevs will return 0, indicating |
---|
39 | * no progress in writing out the buffer. |
---|
40 | */ |
---|
41 | int write_fail = 0; |
---|
42 | |
---|
43 | |
---|
44 | /* |
---|
45 | * Accept a write request and write only the first 32 bytes of it into |
---|
46 | * write_buffer (or as much as will fit), returning the amount written. |
---|
47 | */ |
---|
48 | ssize_t |
---|
49 | fake_write(int fd UNUSED, const void *data, size_t n) |
---|
50 | { |
---|
51 | size_t total; |
---|
52 | |
---|
53 | if (write_fail) |
---|
54 | return 0; |
---|
55 | if (write_interrupt && (write_interrupt++ % 2) == 0) { |
---|
56 | errno = EINTR; |
---|
57 | return -1; |
---|
58 | } |
---|
59 | total = (n < 32) ? n : 32; |
---|
60 | if (256 - write_offset < total) |
---|
61 | total = 256 - write_offset; |
---|
62 | memcpy(write_buffer + write_offset, data, total); |
---|
63 | write_offset += total; |
---|
64 | return total; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | /* |
---|
69 | * Accept a pwrite request and write only the first 32 bytes of it into |
---|
70 | * write_buffer at the specified offset (or as much as will fit), returning |
---|
71 | * the amount written. |
---|
72 | */ |
---|
73 | ssize_t |
---|
74 | fake_pwrite(int fd UNUSED, const void *data, size_t n, off_t offset) |
---|
75 | { |
---|
76 | size_t total; |
---|
77 | |
---|
78 | if (write_fail) |
---|
79 | return 0; |
---|
80 | if (write_interrupt && (write_interrupt++ % 2) == 0) { |
---|
81 | errno = EINTR; |
---|
82 | return -1; |
---|
83 | } |
---|
84 | total = (n < 32) ? n : 32; |
---|
85 | if (offset > 256) { |
---|
86 | errno = ENOSPC; |
---|
87 | return -1; |
---|
88 | } |
---|
89 | if ((size_t) (256 - offset) < total) |
---|
90 | total = 256 - offset; |
---|
91 | memcpy(write_buffer + offset, data, total); |
---|
92 | return total; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | /* |
---|
97 | * Accept an xwrite request and write only the first 32 bytes of it into |
---|
98 | * write_buffer (or as much as will fit), returning the amount written. |
---|
99 | */ |
---|
100 | ssize_t |
---|
101 | fake_writev(int fd UNUSED, const struct iovec *iov, int iovcnt) |
---|
102 | { |
---|
103 | int total, i; |
---|
104 | size_t left, n; |
---|
105 | |
---|
106 | if (write_fail) |
---|
107 | return 0; |
---|
108 | if (write_interrupt && (write_interrupt++ % 2) == 0) { |
---|
109 | errno = EINTR; |
---|
110 | return -1; |
---|
111 | } |
---|
112 | left = 256 - write_offset; |
---|
113 | if (left > 32) |
---|
114 | left = 32; |
---|
115 | total = 0; |
---|
116 | for (i = 0; i < iovcnt && left != 0; i++) { |
---|
117 | n = ((size_t) iov[i].iov_len < left) ? (size_t) iov[i].iov_len : left; |
---|
118 | memcpy(write_buffer + write_offset, iov[i].iov_base, n); |
---|
119 | write_offset += n; |
---|
120 | total += n; |
---|
121 | left -= n; |
---|
122 | } |
---|
123 | return total; |
---|
124 | } |
---|