1 /* Copyright (c) 2008 MySQL AB, 2009 Sun Microsystems, Inc.
2    Use is subject to license terms.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7 
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation.  The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License, version 2.0, for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
23 
24 /*
25   Convert Windows API error (GetLastError() to Posix equivalent (errno)
26   The exported function  my_osmaperr() is modelled after and borrows
27   heavily from undocumented _dosmaperr()(found of the static Microsoft C runtime).
28 */
29 
30 #include <my_global.h>
31 #include <my_sys.h>
32 
33 
34 struct errentry
35 {
36   unsigned long oscode;   /* OS return value */
37   int sysv_errno;  /* System V error code */
38 };
39 
40 static struct errentry errtable[]= {
41   {  ERROR_INVALID_FUNCTION,       EINVAL    },  /* 1 */
42   {  ERROR_FILE_NOT_FOUND,         ENOENT    },  /* 2 */
43   {  ERROR_PATH_NOT_FOUND,         ENOENT    },  /* 3 */
44   {  ERROR_TOO_MANY_OPEN_FILES,    EMFILE    },  /* 4 */
45   {  ERROR_ACCESS_DENIED,          EACCES    },  /* 5 */
46   {  ERROR_INVALID_HANDLE,         EBADF     },  /* 6 */
47   {  ERROR_ARENA_TRASHED,          ENOMEM    },  /* 7 */
48   {  ERROR_NOT_ENOUGH_MEMORY,      ENOMEM    },  /* 8 */
49   {  ERROR_INVALID_BLOCK,          ENOMEM    },  /* 9 */
50   {  ERROR_BAD_ENVIRONMENT,        E2BIG     },  /* 10 */
51   {  ERROR_BAD_FORMAT,             ENOEXEC   },  /* 11 */
52   {  ERROR_INVALID_ACCESS,         EINVAL    },  /* 12 */
53   {  ERROR_INVALID_DATA,           EINVAL    },  /* 13 */
54   {  ERROR_INVALID_DRIVE,          ENOENT    },  /* 15 */
55   {  ERROR_CURRENT_DIRECTORY,      EACCES    },  /* 16 */
56   {  ERROR_NOT_SAME_DEVICE,        EXDEV     },  /* 17 */
57   {  ERROR_NO_MORE_FILES,          ENOENT    },  /* 18 */
58   {  ERROR_LOCK_VIOLATION,         EACCES    },  /* 33 */
59   {  ERROR_BAD_NETPATH,            ENOENT    },  /* 53 */
60   {  ERROR_NETWORK_ACCESS_DENIED,  EACCES    },  /* 65 */
61   {  ERROR_BAD_NET_NAME,           ENOENT    },  /* 67 */
62   {  ERROR_FILE_EXISTS,            EEXIST    },  /* 80 */
63   {  ERROR_CANNOT_MAKE,            EACCES    },  /* 82 */
64   {  ERROR_FAIL_I24,               EACCES    },  /* 83 */
65   {  ERROR_INVALID_PARAMETER,      EINVAL    },  /* 87 */
66   {  ERROR_NO_PROC_SLOTS,          EAGAIN    },  /* 89 */
67   {  ERROR_DRIVE_LOCKED,           EACCES    },  /* 108 */
68   {  ERROR_BROKEN_PIPE,            EPIPE     },  /* 109 */
69   {  ERROR_DISK_FULL,              ENOSPC    },  /* 112 */
70   {  ERROR_INVALID_TARGET_HANDLE,  EBADF     },  /* 114 */
71   {  ERROR_INVALID_HANDLE,         EINVAL    },  /* 124 */
72   {  ERROR_WAIT_NO_CHILDREN,       ECHILD    },  /* 128 */
73   {  ERROR_CHILD_NOT_COMPLETE,     ECHILD    },  /* 129 */
74   {  ERROR_DIRECT_ACCESS_HANDLE,   EBADF     },  /* 130 */
75   {  ERROR_NEGATIVE_SEEK,          EINVAL    },  /* 131 */
76   {  ERROR_SEEK_ON_DEVICE,         EACCES    },  /* 132 */
77   {  ERROR_DIR_NOT_EMPTY,          ENOTEMPTY },  /* 145 */
78   {  ERROR_NOT_LOCKED,             EACCES    },  /* 158 */
79   {  ERROR_BAD_PATHNAME,           ENOENT    },  /* 161 */
80   {  ERROR_MAX_THRDS_REACHED,      EAGAIN    },  /* 164 */
81   {  ERROR_LOCK_FAILED,            EACCES    },  /* 167 */
82   {  ERROR_ALREADY_EXISTS,         EEXIST    },  /* 183 */
83   {  ERROR_FILENAME_EXCED_RANGE,   ENOENT    },  /* 206 */
84   {  ERROR_NESTING_NOT_ALLOWED,    EAGAIN    },  /* 215 */
85   {  ERROR_NOT_ENOUGH_QUOTA,       ENOMEM    }    /* 1816 */
86 };
87 
88 /* size of the table */
89 #define ERRTABLESIZE (sizeof(errtable)/sizeof(errtable[0]))
90 
91 /* The following two constants must be the minimum and maximum
92 values in the (contiguous) range of Exec Failure errors. */
93 #define MIN_EXEC_ERROR ERROR_INVALID_STARTING_CODESEG
94 #define MAX_EXEC_ERROR ERROR_INFLOOP_IN_RELOC_CHAIN
95 
96 /* These are the low and high value in the range of errors that are
97 access violations */
98 #define MIN_EACCES_RANGE ERROR_WRITE_PROTECT
99 #define MAX_EACCES_RANGE ERROR_SHARING_BUFFER_EXCEEDED
100 
101 
get_errno_from_oserr(unsigned long oserrno)102 static int get_errno_from_oserr(unsigned long oserrno)
103 {
104   int i;
105 
106   /* check the table for the OS error code */
107   for (i= 0; i < ERRTABLESIZE; ++i)
108   {
109     if (oserrno == errtable[i].oscode)
110     {
111       return  errtable[i].sysv_errno;
112     }
113   }
114 
115   /* The error code wasn't in the table.  We check for a range of */
116   /* EACCES errors or exec failure errors (ENOEXEC).  Otherwise   */
117   /* EINVAL is returned.                                          */
118 
119   if (oserrno >= MIN_EACCES_RANGE && oserrno <= MAX_EACCES_RANGE)
120     return EACCES;
121   else if (oserrno >= MIN_EXEC_ERROR && oserrno <= MAX_EXEC_ERROR)
122     return ENOEXEC;
123   else
124     return EINVAL;
125 }
126 
127 /* Set errno corresponsing to GetLastError() value */
my_osmaperr(unsigned long oserrno)128 void my_osmaperr ( unsigned long oserrno)
129 {
130     errno= get_errno_from_oserr(oserrno);
131 }
132