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