summaryrefslogtreecommitdiff
path: root/test/raidfile/intercept.cpp
blob: 60c0ca1e024558d18eca3ca9a3d0a18ac79ec43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// --------------------------------------------------------------------------
//
// File
//		Name:    intercept.cpp
//		Purpose: Syscall interception code for the raidfile test
//		Created: 2003/07/22
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/uio.h>
#include <errno.h>

#ifndef PLATFORM_CLIB_FNS_INTERCEPTION_IMPOSSIBLE

#ifdef PLATFORM_DARWIN
	// For some reason, __syscall just doesn't work on Darwin
	// so instead, we build functions using assembler in a varient
	// of the technique used in the Darwin Libc
	extern "C" int
	TEST_open(const char *path, int flags, mode_t mode);
	extern "C" int
	TEST_close(int d);
	extern "C" ssize_t
	TEST_write(int d, const void *buf, size_t nbytes);
	extern "C" ssize_t
	TEST_read(int d, void *buf, size_t nbytes);
	extern "C" ssize_t
	TEST_readv(int d, const struct iovec *iov, int iovcnt);
	extern "C" off_t
	TEST_lseek(int fildes, off_t offset, int whence);
#else
	#if defined(PLATFORM_LINUX) || defined(PLATFORM_SUNOS)
		#undef __syscall
		#define __syscall syscall
	#else
		// Need this, not declared in syscall.h
		extern "C" off_t __syscall(quad_t number, ...);
	#endif
#endif

#include <string.h>
#include <stdio.h>

#include "MemLeakFindOn.h"

bool intercept_enabled = false;
const char *intercept_filename = 0;
int intercept_filedes = -1;
off_t intercept_errorafter = 0;
int intercept_errno = 0;
int intercept_syscall = 0;
off_t intercept_filepos = 0;

#define SIZE_ALWAYS_ERROR	-773

void intercept_clear_setup()
{
	intercept_enabled = false;
	intercept_filename = 0;
	intercept_filedes = -1;
	intercept_errorafter = 0;
	intercept_syscall = 0;
	intercept_filepos = 0;
}

bool intercept_triggered()
{
	return !intercept_enabled;
}

void intercept_setup_error(const char *filename, unsigned int errorafter, int errortoreturn, int syscalltoerror)
{
	TRACE4("Setup for error: %s, after %d, err %d, syscall %d\n", filename, errorafter, errortoreturn, syscalltoerror);
	intercept_enabled = true;
	intercept_filename = filename;
	intercept_filedes = -1;
	intercept_errorafter = errorafter;
	intercept_syscall = syscalltoerror;
	intercept_errno = errortoreturn;
	intercept_filepos = 0;
}

bool intercept_errornow(int d, int size, int syscallnum)
{
	if(intercept_filedes != -1 && d == intercept_filedes && syscallnum == intercept_syscall)
	{
		//printf("Checking for err, %d, %d, %d\n", d, size, syscallnum);
		if(size == SIZE_ALWAYS_ERROR)
		{
			// Looks good for an error!
			TRACE2("Returning error %d for syscall %d\n", intercept_errno, syscallnum);
			return true;
		}
		// where are we in the file?
		if(intercept_filepos >= intercept_errorafter || intercept_filepos >= ((off_t)intercept_errorafter - size))
		{
			TRACE3("Returning error %d for syscall %d, file pos %d\n", intercept_errno, syscallnum, (int)intercept_filepos);
			return true;
		}
	}
	return false;	// no error please!
}

int intercept_reterr()
{
	intercept_enabled = false;
	intercept_filename = 0;
	intercept_filedes = -1;
	intercept_errorafter = 0;
	intercept_syscall = 0;
	return intercept_errno;
}

#define CHECK_FOR_FAKE_ERROR_COND(D, S, CALL, FAILRES)	\
	if(intercept_enabled)					\
	{										\
		if(intercept_errornow(D, S, CALL))	\
		{									\
			errno = intercept_reterr();		\
			return FAILRES;					\
		}									\
	}

extern "C" int
open(const char *path, int flags, mode_t mode)
{
	if(intercept_enabled)
	{
		if(intercept_syscall == SYS_open && strcmp(path, intercept_filename) == 0)
		{
			errno = intercept_reterr();
			return -1;
		}
	}
#ifdef PLATFORM_DARWIN
	int r = TEST_open(path, flags, mode);
#else
	int r = __syscall(SYS_open, path, flags, mode);
#endif
	if(intercept_enabled && intercept_filedes == -1)
	{
		// Right file?
		if(strcmp(intercept_filename, path) == 0)
		{
			intercept_filedes = r;
			//printf("Found file to intercept, h = %d\n", r);
		}
	}
	return r;
}

extern "C" int
close(int d)
{
	CHECK_FOR_FAKE_ERROR_COND(d, SIZE_ALWAYS_ERROR, SYS_close, -1);
#ifdef PLATFORM_DARWIN
	int r = TEST_close(d);
#else
	int r = __syscall(SYS_close, d);
#endif
	if(r == 0)
	{
		if(d == intercept_filedes)
		{
			intercept_filedes = -1;
		}
	}
	return r;
}

extern "C" ssize_t
write(int d, const void *buf, size_t nbytes)
{
	CHECK_FOR_FAKE_ERROR_COND(d, nbytes, SYS_write, -1);
#ifdef PLATFORM_DARWIN
	int r = TEST_write(d, buf, nbytes);
#else
	int r = __syscall(SYS_write, d, buf, nbytes);
#endif
	if(r != -1)
	{
		intercept_filepos += r;
	}
	return r;
}

extern "C" ssize_t
read(int d, void *buf, size_t nbytes)
{
	CHECK_FOR_FAKE_ERROR_COND(d, nbytes, SYS_read, -1);
#ifdef PLATFORM_DARWIN
	int r = TEST_read(d, buf, nbytes);
#else
	int r = __syscall(SYS_read, d, buf, nbytes);
#endif
	if(r != -1)
	{
		intercept_filepos += r;
	}
	return r;
}

extern "C" ssize_t
readv(int d, const struct iovec *iov, int iovcnt)
{
	// how many bytes?
	int nbytes = 0;
	for(int b = 0; b < iovcnt; ++b)
	{
		nbytes += iov[b].iov_len;
	}

	CHECK_FOR_FAKE_ERROR_COND(d, nbytes, SYS_readv, -1);
#ifdef PLATFORM_DARWIN
	int r = TEST_readv(d, iov, iovcnt);
#else
	int r = __syscall(SYS_readv, d, iov, iovcnt);
#endif
	if(r != -1)
	{
		intercept_filepos += r;
	}
	return r;
}

extern "C" off_t
lseek(int fildes, off_t offset, int whence)
{
	// random magic for lseek syscall, see /usr/src/lib/libc/sys/lseek.c
	CHECK_FOR_FAKE_ERROR_COND(fildes, 0, SYS_lseek, -1);
#ifdef PLATFORM_DARWIN
	int r = TEST_lseek(fildes, offset, whence);
#else
	#if defined(PLATFORM_LINUX) || defined(PLATFORM_SUNOS)
		off_t r = __syscall(SYS_lseek, fildes, offset, whence);
	#else
		// Should swap this condition round. No reason to assume that most OS
		// do this syscall wierdness, default should be the sensible way
		off_t r = __syscall(SYS_lseek, fildes, 0 /* extra 0 required here! */, offset, whence);
	#endif
#endif
	if(r != -1)
	{
		intercept_filepos = r;
	}
	return r;
}

#endif // n PLATFORM_CLIB_FNS_INTERCEPTION_IMPOSSIBLE