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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 1998 Vadim Zeitlin ( zeitlin@dptmaths.ens-cachan.fr )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "config.h"
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include "FileArea.h" // Interface declarations.
#include "FileAutoClose.h" // Needed for CFileAutoClose
#ifndef ENABLE_MMAP
# define ENABLE_MMAP 0
#endif
#if ENABLE_MMAP && defined(HAVE_MMAP) && defined(HAVE_SYSCONF) && (defined(HAVE__SC_PAGESIZE) || defined(HAVE__SC_PAGE_SIZE))
# define USE_MMAP
#endif
#ifdef USE_MMAP
#include <sys/mman.h>
#if defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGESIZE)
static const long gs_pageSize = sysconf(_SC_PAGESIZE);
#elif defined(HAVE_SYSCONF) && defined(HAVE__SC_PAGE_SIZE)
static const long gs_pageSize = sysconf(_SC_PAGE_SIZE);
#endif
#endif /* USE_MMAP */
#if !defined(HAVE_SIGACTION) || !defined(SA_SIGINFO) || !defined(USE_MMAP) || defined(__UCLIBC__)
class CFileAreaSigHandler
{
public:
static void Init() {};
static void Add(CFileArea&) {};
static void Remove(CFileArea&) {};
private:
CFileAreaSigHandler() {};
};
#else
class CFileAreaSigHandler
{
public:
static void Init();
static void Add(CFileArea& area);
static void Remove(CFileArea& area);
private:
CFileAreaSigHandler() {};
static wxMutex mutex;
static CFileArea *first;
static bool initialized;
static struct sigaction old_segv, old_bus;
static void Handler(int sig, siginfo_t *info, void *ctx);
};
wxMutex CFileAreaSigHandler::mutex;
CFileArea * CFileAreaSigHandler::first;
bool CFileAreaSigHandler::initialized = false;
struct sigaction CFileAreaSigHandler::old_segv;
struct sigaction CFileAreaSigHandler::old_bus;
/* define MAP_ANONYMOUS for Mac OS X */
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
#define MAP_ANONYMOUS MAP_ANON
#endif
// Handle signals.
// The idea is to replace faulted memory with zeroes and mark
// the error in proper CFileArea
void CFileAreaSigHandler::Handler(int sig, siginfo_t *info, void *ctx)
{
CFileArea *cur;
// find the mapped section where violation occurred (if any)
{
wxMutexLocker lock(mutex);
cur = first;
while (cur) {
if (cur->m_mmap_buffer && info->si_addr >= cur->m_mmap_buffer && info->si_addr < cur->m_mmap_buffer + cur->m_length)
break;
cur = cur->m_next;
}
}
// mark error if found
if (cur && gs_pageSize > 0) {
cur->m_error = true;
char *start_addr = ((char *) info->si_addr) - (((unsigned long) info->si_addr) % gs_pageSize);
if (mmap(start_addr, gs_pageSize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) != MAP_FAILED)
return;
}
// call old handler
struct sigaction* sa = (sig == SIGSEGV) ? &old_segv : &old_bus;
if (sa->sa_flags & SA_SIGINFO)
sa->sa_sigaction(sig, info, ctx);
else if (sa->sa_handler == SIG_DFL || sa->sa_handler == SIG_IGN)
abort();
else
sa->sa_handler(sig);
}
void CFileAreaSigHandler::Init()
{
// init error handler if needed
wxMutexLocker lock(mutex);
if (initialized)
return;
// Set our new signal handler.
// Note that we safe old handlers (probably wx ones) in order
// to be able to call them if signal not handled as desired.
// These handler will be removed by wx code when wx will restore
// old ones
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = Handler;
sa.sa_flags = SA_NODEFER|SA_SIGINFO;
if (sigaction(SIGSEGV, &sa, &old_segv))
return;
if (sigaction(SIGBUS, &sa, &old_bus)) {
sigaction(SIGSEGV, &old_segv, NULL);
return;
}
initialized = true;
}
void CFileAreaSigHandler::Add(CFileArea& area)
{
wxMutexLocker lock(mutex);
area.m_next = first;
first = &area;
}
void CFileAreaSigHandler::Remove(CFileArea& area)
{
wxMutexLocker lock(mutex);
CFileArea **cur = &first;
while (*cur) {
if (*cur == &area) {
*cur = area.m_next;
area.m_next = NULL;
break;
}
cur = &(*cur)->m_next;
}
}
#endif
CFileArea::CFileArea()
: m_buffer(NULL), m_mmap_buffer(NULL), m_length(0), m_next(NULL), m_file(NULL), m_error(false)
{
CFileAreaSigHandler::Init();
}
CFileArea::~CFileArea()
{
Close();
CheckError();<--- Exception thrown in function declared not to throw exceptions.
}
bool CFileArea::Close()
{
if (m_buffer != NULL && m_mmap_buffer == NULL)
{
delete[] m_buffer;
m_buffer = NULL;
}
#ifdef USE_MMAP
if (m_mmap_buffer)
{
munmap(m_mmap_buffer, m_length);
// remove from list
CFileAreaSigHandler::Remove(*this);
m_buffer = NULL;
m_mmap_buffer = NULL;
if (m_file) {
m_file->Unlock();
m_file = NULL;
}
}
#endif
return true;
}
void CFileArea::ReadAt(CFileAutoClose& file, uint64 offset, size_t count)
{
Close();
#ifdef USE_MMAP
uint64 offEnd = offset + count;
if (gs_pageSize > 0 && offEnd < 0x100000000ull) {
uint64 offStart = offset & (~((uint64)gs_pageSize-1));
m_length = offEnd - offStart;
void *p = mmap(NULL, m_length, PROT_READ, MAP_SHARED, file.fd(), offStart);
if (p != MAP_FAILED) {
m_file = &file;
m_mmap_buffer = (uint8_t*) p;
m_buffer = m_mmap_buffer + (offset - offStart);
// add to list to catch errors correctly
CFileAreaSigHandler::Add(*this);
return;
}
}
file.Unlock();
#endif
m_buffer = new uint8_t[count];
file.ReadAt(m_buffer, offset, count);
}
#ifdef USE_MMAP
void CFileArea::StartWriteAt(CFileAutoClose& file, uint64 offset, size_t count)
{
Close();
uint64 offEnd = offset + count;
if (file.GetLength() >= offEnd && gs_pageSize > 0 && offEnd < 0x100000000ull) {
uint64 offStart = offset & (~((uint64)gs_pageSize-1));
m_length = offEnd - offStart;
void *p = mmap(NULL, m_length, PROT_READ|PROT_WRITE, MAP_SHARED, file.fd(), offStart);
if (p != MAP_FAILED)
{
m_file = &file;
m_mmap_buffer = (uint8_t*) p;
m_buffer = m_mmap_buffer + (offset - offStart);
// add to list to catch errors correctly
CFileAreaSigHandler::Add(*this);
return;
}
file.Unlock();
}
m_buffer = new uint8_t[count];
}
#else
void CFileArea::StartWriteAt(CFileAutoClose&, uint64, size_t count)
{
Close();
m_buffer = new uint8_t[count];
}
#endif
bool CFileArea::FlushAt(CFileAutoClose& file, uint64 offset, size_t count)
{
if (!m_buffer)
return false;
#ifdef USE_MMAP
if (m_mmap_buffer) {
if (msync(m_mmap_buffer, m_length, MS_SYNC))
return false;
Close();
return true;
}
#endif
file.WriteAt(m_buffer, offset, count);
Close();
return true;
}
void CFileArea::CheckError()
{
bool err = m_error;
m_error = false;
if (err)
throw CIOFailureException(wxT("Read error, failed to read from file."));
}
|