summaryrefslogtreecommitdiff
path: root/os_windows/os_errno.c
blob: 35b540b75e04f5ba319ff994e08f3950e3400ea0 (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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1999-2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"

/*
 * __os_get_errno_ret_zero --
 *	Return the last system error, including an error of zero.
 */
int
__os_get_errno_ret_zero()
{
	/* This routine must be able to return the same value repeatedly. */
	return (errno);
}

/*
 * We've seen cases where system calls failed but errno was never set.  For
 * that reason, __os_get_errno() and __os_get_syserr set errno to EAGAIN if
 * it's not already set, to work around the problem.  For obvious reasons,
 * we can only call this function if we know an error has occurred, that
 * is, we can't test the return for a non-zero value after the get call.
 *
 * __os_get_errno --
 *	Return the last ANSI C "errno" value or EAGAIN if the last error
 *	is zero.
 */
int
__os_get_errno()
{
	/* This routine must be able to return the same value repeatedly. */
	if (errno == 0)
		__os_set_errno(EAGAIN);
	return (errno);
}

#ifdef HAVE_REPLICATION_THREADS
/*
 * __os_get_neterr --
 *	Return the last networking error or EAGAIN if the last error is zero.
 *
 * PUBLIC: #ifdef HAVE_REPLICATION_THREADS
 * PUBLIC: int __os_get_neterr __P((void));
 * PUBLIC: #endif
 */
int
__os_get_neterr()
{
	int err;

	/* This routine must be able to return the same value repeatedly. */
	err = WSAGetLastError();
	if (err == 0)
		WSASetLastError(err = ERROR_RETRY);
	return (err);
}
#endif

/*
 * __os_get_syserr --
 *	Return the last system error or EAGAIN if the last error is zero.
 */
int
__os_get_syserr()
{
	int err;

	/* This routine must be able to return the same value repeatedly. */
	err = GetLastError();
	if (err == 0)
		SetLastError(err = ERROR_RETRY);
	return (err);
}

/*
 * __os_set_errno --
 *	Set the value of errno.
 */
void
__os_set_errno(evalue)
	int evalue;
{
	/*
	 * This routine is called by the compatibility interfaces (DB 1.85,
	 * dbm and hsearch).  Force values > 0, that is, not one of DB 2.X
	 * and later's public error returns.  If something bad has happened,
	 * default to EFAULT -- a nasty return.  Otherwise, default to EINVAL.
	 * As the compatibility APIs aren't included on Windows, the Windows
	 * version of this routine doesn't need this behavior.
	 */
	errno =
	    evalue >= 0 ? evalue : (evalue == DB_RUNRECOVERY ? EFAULT : EINVAL);
}

/*
 * __os_strerror --
 *	Return a string associated with the system error.
 */
char *
__os_strerror(error, buf, len)
	int error;
	char *buf;
	size_t len;
{
#ifdef DB_WINCE
#define	MAX_TMPBUF_LEN 512
	_TCHAR tbuf[MAX_TMPBUF_LEN];
	size_t  maxlen;

	DB_ASSERT(NULL, error != 0);

	memset(tbuf, 0, sizeof(_TCHAR)*MAX_TMPBUF_LEN);
	maxlen = (len > MAX_TMPBUF_LEN ? MAX_TMPBUF_LEN : len);
	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, (DWORD)error,
		0, tbuf, maxlen-1, NULL);

	if (WideCharToMultiByte(CP_UTF8, 0, tbuf, -1,
		buf, len, 0, NULL) == 0)
		strncpy(buf, "Error message translation failed.", len);
#else
	DB_ASSERT(NULL, error != 0);
	/*
	 * Explicitly call FormatMessageA, since we want to receive a char
	 * string back, not a tchar string.
	 */
	FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
	    0, (DWORD)error, 0, buf, (DWORD)(len - 1), NULL);
	buf[len - 1] = '\0';
#endif

	return (buf);
}

/*
 * __os_posix_err --
 *	Convert a system error to a POSIX error.
 */
int
__os_posix_err(error)
	int error;
{
	/* Handle calls on successful returns. */
	if (error == 0)
		return (0);

	/*
	 * Translate the Windows error codes we care about.
	 */
	switch (error) {
	case ERROR_INVALID_PARAMETER:
		return (EINVAL);

	case ERROR_FILE_NOT_FOUND:
	case ERROR_INVALID_DRIVE:
	case ERROR_PATH_NOT_FOUND:
		return (ENOENT);

	case ERROR_NO_MORE_FILES:
	case ERROR_TOO_MANY_OPEN_FILES:
		return (EMFILE);

	case ERROR_ACCESS_DENIED:
		return (EPERM);

	case ERROR_INVALID_HANDLE:
		return (EBADF);

	case ERROR_NOT_ENOUGH_MEMORY:
		return (ENOMEM);

	case ERROR_DISK_FULL:
		return (ENOSPC);

	case ERROR_ARENA_TRASHED:
	case ERROR_BAD_COMMAND:
	case ERROR_BAD_ENVIRONMENT:
	case ERROR_BAD_FORMAT:
	case ERROR_GEN_FAILURE:
	case ERROR_INVALID_ACCESS:
	case ERROR_INVALID_BLOCK:
	case ERROR_INVALID_DATA:
	case ERROR_READ_FAULT:
	case ERROR_WRITE_FAULT:
		return (EFAULT);

	case ERROR_ALREADY_EXISTS:
	case ERROR_FILE_EXISTS:
		return (EEXIST);

	case ERROR_NOT_SAME_DEVICE:
		return (EXDEV);

	case ERROR_WRITE_PROTECT:
		return (EACCES);

	case ERROR_LOCK_FAILED:
	case ERROR_LOCK_VIOLATION:
	case ERROR_NOT_READY:
	case ERROR_SHARING_VIOLATION:
		return (EBUSY);

	case ERROR_RETRY:
		return (EINTR);
	}

	/*
	 * Translate the Windows socket error codes.
	 */
	switch (error) {
	case WSAEADDRINUSE:
#ifdef EADDRINUSE
		return (EADDRINUSE);
#else
		break;
#endif
	case WSAEADDRNOTAVAIL:
#ifdef EADDRNOTAVAIL
		return (EADDRNOTAVAIL);
#else
		break;
#endif
	case WSAEAFNOSUPPORT:
#ifdef EAFNOSUPPORT
		return (EAFNOSUPPORT);
#else
		break;
#endif
	case WSAEALREADY:
#ifdef EALREADY
		return (EALREADY);
#else
		break;
#endif
	case WSAEBADF:
		return (EBADF);
	case WSAECONNABORTED:
#ifdef ECONNABORTED
		return (ECONNABORTED);
#else
		break;
#endif
	case WSAECONNREFUSED:
#ifdef ECONNREFUSED
		return (ECONNREFUSED);
#else
		break;
#endif
	case WSAECONNRESET:
#ifdef ECONNRESET
		return (ECONNRESET);
#else
		break;
#endif
	case WSAEDESTADDRREQ:
#ifdef EDESTADDRREQ
		return (EDESTADDRREQ);
#else
		break;
#endif
	case WSAEFAULT:
		return (EFAULT);
	case WSAEHOSTDOWN:
#ifdef EHOSTDOWN
		return (EHOSTDOWN);
#else
		break;
#endif
	case WSAEHOSTUNREACH:
#ifdef EHOSTUNREACH
		return (EHOSTUNREACH);
#else
		break;
#endif
	case WSAEINPROGRESS:
#ifdef EINPROGRESS
		return (EINPROGRESS);
#else
		break;
#endif
	case WSAEINTR:
		return (EINTR);
	case WSAEINVAL:
		return (EINVAL);
	case WSAEISCONN:
#ifdef EISCONN
		return (EISCONN);
#else
		break;
#endif
	case WSAELOOP:
#ifdef ELOOP
		return (ELOOP);
#else
		break;
#endif
	case WSAEMFILE:
		return (EMFILE);
	case WSAEMSGSIZE:
#ifdef EMSGSIZE
		return (EMSGSIZE);
#else
		break;
#endif
	case WSAENAMETOOLONG:
		return (ENAMETOOLONG);
	case WSAENETDOWN:
#ifdef ENETDOWN
		return (ENETDOWN);
#else
		break;
#endif
	case WSAENETRESET:
#ifdef ENETRESET
		return (ENETRESET);
#else
		break;
#endif
	case WSAENETUNREACH:
#ifdef ENETUNREACH
		return (ENETUNREACH);
#else
		break;
#endif
	case WSAENOBUFS:
#ifdef ENOBUFS
		return (ENOBUFS);
#else
		break;
#endif
	case WSAENOPROTOOPT:
#ifdef ENOPROTOOPT
		return (ENOPROTOOPT);
#else
		break;
#endif
	case WSAENOTCONN:
#ifdef ENOTCONN
		return (ENOTCONN);
#else
		break;
#endif
	case WSANOTINITIALISED:
		return (EAGAIN);
	case WSAENOTSOCK:
#ifdef ENOTSOCK
		return (ENOTSOCK);
#else
		break;
#endif
	case WSAEOPNOTSUPP:
		return (DB_OPNOTSUP);
	case WSAEPFNOSUPPORT:
#ifdef EPFNOSUPPORT
		return (EPFNOSUPPORT);
#else
		break;
#endif
	case WSAEPROTONOSUPPORT:
#ifdef EPROTONOSUPPORT
		return (EPROTONOSUPPORT);
#else
		break;
#endif
	case WSAEPROTOTYPE:
#ifdef EPROTOTYPE
		return (EPROTOTYPE);
#else
		break;
#endif
	case WSAESHUTDOWN:
#ifdef ESHUTDOWN
		return (ESHUTDOWN);
#else
		break;
#endif
	case WSAESOCKTNOSUPPORT:
#ifdef ESOCKTNOSUPPORT
		return (ESOCKTNOSUPPORT);
#else
		break;
#endif
	case WSAETIMEDOUT:
#ifdef ETIMEDOUT
		return (ETIMEDOUT);
#else
		break;
#endif
	case WSAETOOMANYREFS:
#ifdef ETOOMANYREFS
		return (ETOOMANYREFS);
#else
		break;
#endif
	case WSAEWOULDBLOCK:
#ifdef EWOULDBLOCK
		return (EWOULDBLOCK);
#else
		return (EAGAIN);
#endif
	case WSAHOST_NOT_FOUND:
#ifdef EHOSTUNREACH
		return (EHOSTUNREACH);
#else
		break;
#endif
	case WSASYSNOTREADY:
		return (EAGAIN);
	case WSATRY_AGAIN:
		return (EAGAIN);
	case WSAVERNOTSUPPORTED:
		return (DB_OPNOTSUP);
	case WSAEACCES:
		return (EACCES);
	}

	/*
	 * EFAULT is the default if we don't have a translation.
	 */
	return (EFAULT);
}