1 /* errno.cc: errno-related functions
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2008, 2009 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
12 #define _sys_nerr FOO_sys_nerr
13 #define sys_nerr FOOsys_nerr
14 #define _sys_errlist FOO_sys_errlist
21 /* Table to map Windows error codes to Errno values. */
22 /* FIXME: Doing things this way is a little slow. It's trivial to change
23 this into a big case statement if necessary. Left as is for now. */
25 #define X(w, e) {ERROR_##w, #w, e}
29 DWORD w; /* windows version of error */
30 const char *s; /* text of windows version */
31 int e; /* errno version of error */
34 /* FIXME: Some of these choices are arbitrary! */
35 X (ACCESS_DENIED, EACCES),
36 X (ACTIVE_CONNECTIONS, EAGAIN),
37 X (ALREADY_EXISTS, EEXIST),
38 X (BAD_DEVICE, ENODEV),
39 X (BAD_NETPATH, ENOENT),
40 X (BAD_NET_NAME, ENOENT),
41 X (BAD_PATHNAME, ENOENT),
44 X (BAD_USERNAME, EINVAL),
45 X (BEGINNING_OF_MEDIA, EIO),
46 X (BROKEN_PIPE, EPIPE),
49 X (CALL_NOT_IMPLEMENTED, ENOSYS),
50 X (CANNOT_MAKE, EPERM),
51 X (CHILD_NOT_COMPLETE, EBUSY),
52 X (COMMITMENT_LIMIT, EAGAIN),
54 X (DEVICE_DOOR_OPEN, EIO),
55 X (DEVICE_IN_USE, EAGAIN),
56 X (DEVICE_REQUIRES_CLEANING, EIO),
57 X (DIRECTORY, ENOTDIR),
58 X (DIR_NOT_EMPTY, ENOTEMPTY),
59 X (DISK_CORRUPT, EIO),
60 X (DISK_FULL, ENOSPC),
61 X (DUP_NAME, ENOTUNIQ),
62 X (EAS_DIDNT_FIT, ENOSPC),
63 X (EAS_NOT_SUPPORTED, ENOTSUP),
64 X (EA_LIST_INCONSISTENT, EINVAL),
65 X (EA_TABLE_FULL, ENOSPC),
66 X (END_OF_MEDIA, ENOSPC),
67 X (EOM_OVERFLOW, EIO),
68 X (FILEMARK_DETECTED, EIO),
69 X (FILENAME_EXCED_RANGE, ENAMETOOLONG),
70 X (FILE_CORRUPT, EEXIST),
71 X (FILE_EXISTS, EEXIST),
72 X (FILE_INVALID, ENXIO),
73 X (FILE_NOT_FOUND, ENOENT),
74 X (HANDLE_DISK_FULL, ENOSPC),
75 X (HANDLE_EOF, ENODATA),
76 X (INVALID_ADDRESS, EINVAL),
77 X (INVALID_AT_INTERRUPT_TIME, EINTR),
78 X (INVALID_BLOCK_LENGTH, EIO),
79 X (INVALID_DATA, EINVAL),
80 X (INVALID_DRIVE, ENODEV),
81 X (INVALID_EA_NAME, EINVAL),
82 X (INVALID_FUNCTION, EBADRQC),
83 X (INVALID_HANDLE, EBADF),
84 X (INVALID_NAME, ENOENT),
85 X (INVALID_PARAMETER, EINVAL),
86 X (INVALID_SIGNAL_NUMBER, EINVAL),
88 X (IO_PENDING, EAGAIN),
89 X (LOCK_VIOLATION, EACCES),
90 X (MAX_THRDS_REACHED, EAGAIN),
91 X (META_EXPANSION_TOO_LONG, EINVAL),
92 X (MOD_NOT_FOUND, ENOENT),
93 X (MORE_DATA, EMSGSIZE),
94 X (NEGATIVE_SEEK, EINVAL),
95 X (NETNAME_DELETED, ENOENT),
97 X (NONPAGED_SYSTEM_RESOURCES, EAGAIN),
98 X (NONE_MAPPED, EINVAL),
99 X (NOT_CONNECTED, ENOLINK),
100 X (NOT_ENOUGH_MEMORY, ENOMEM),
101 X (NOT_OWNER, EPERM),
102 X (NOT_READY, ENOMEDIUM),
103 X (NOT_SAME_DEVICE, EXDEV),
104 X (NOT_SUPPORTED, ENOSYS),
106 X (NO_DATA_DETECTED, EIO),
107 X (NO_MEDIA_IN_DRIVE, ENOMEDIUM),
108 X (NO_MORE_FILES, ENMFILE),
109 X (NO_MORE_ITEMS, ENMFILE),
110 X (NO_MORE_SEARCH_HANDLES, ENFILE),
111 X (NO_PROC_SLOTS, EAGAIN),
112 X (NO_SIGNAL_SENT, EIO),
113 X (NO_SYSTEM_RESOURCES, EAGAIN),
114 X (NO_TOKEN, EINVAL),
115 X (OPEN_FAILED, EIO),
116 X (OPEN_FILES, EAGAIN),
117 X (OUTOFMEMORY, ENOMEM),
118 X (PAGED_SYSTEM_RESOURCES, EAGAIN),
119 X (PAGEFILE_QUOTA, EAGAIN),
120 X (PATH_NOT_FOUND, ENOENT),
121 X (PIPE_BUSY, EBUSY),
122 X (PIPE_CONNECTED, EBUSY),
123 X (PIPE_LISTENING, ECOMM),
124 X (PIPE_NOT_CONNECTED, ECOMM),
125 X (POSSIBLE_DEADLOCK, EDEADLOCK),
126 X (PROCESS_ABORTED, EFAULT),
127 X (PROC_NOT_FOUND, ESRCH),
128 X (REM_NOT_LIST, ENONET),
129 X (SETMARK_DETECTED, EIO),
130 X (SHARING_BUFFER_EXCEEDED, ENOLCK),
131 X (SHARING_VIOLATION, EBUSY),
132 X (SIGNAL_PENDING, EBUSY),
133 X (SIGNAL_REFUSED, EIO),
134 X (THREAD_1_INACTIVE, EINVAL),
135 X (TOO_MANY_LINKS, EMLINK),
136 X (TOO_MANY_OPEN_FILES, EMFILE),
137 X (WAIT_NO_CHILDREN, ECHILD),
138 X (WORKING_SET_QUOTA, EAGAIN),
139 X (WRITE_PROTECT, EROFS),
141 X (SECTOR_NOT_FOUND, EINVAL),
142 X (IO_INCOMPLETE, EAGAIN),
147 const char *_sys_errlist[] NO_COPY_INIT =
149 /* NOERROR 0 */ "No error",
150 /* EPERM 1 */ "Operation not permitted",
151 /* ENOENT 2 */ "No such file or directory",
152 /* ESRCH 3 */ "No such process",
153 /* EINTR 4 */ "Interrupted system call",
154 /* EIO 5 */ "Input/Output error",
155 /* ENXIO 6 */ "No such device or address",
156 /* E2BIG 7 */ "Argument list too long",
157 /* ENOEXEC 8 */ "Exec format error",
158 /* EBADF 9 */ "Bad file descriptor",
159 /* ECHILD 10 */ "No child processes",
160 /* EAGAIN 11 */ "Resource temporarily unavailable",
161 /* ENOMEM 12 */ "Cannot allocate memory",
162 /* EACCES 13 */ "Permission denied",
163 /* EFAULT 14 */ "Bad address",
164 /* ENOTBLK 15 */ "Block device required",
165 /* EBUSY 16 */ "Device or resource busy",
166 /* EEXIST 17 */ "File exists",
167 /* EXDEV 18 */ "Invalid cross-device link",
168 /* ENODEV 19 */ "No such device",
169 /* ENOTDIR 20 */ "Not a directory",
170 /* EISDIR 21 */ "Is a directory",
171 /* EINVAL 22 */ "Invalid argument",
172 /* ENFILE 23 */ "Too many open files in system",
173 /* EMFILE 24 */ "Too many open files",
174 /* ENOTTY 25 */ "Inappropriate ioctl for device",
175 /* ETXTBSY 26 */ "Text file busy",
176 /* EFBIG 27 */ "File too large",
177 /* ENOSPC 28 */ "No space left on device",
178 /* ESPIPE 29 */ "Illegal seek",
179 /* EROFS 30 */ "Read-only file system",
180 /* EMLINK 31 */ "Too many links",
181 /* EPIPE 32 */ "Broken pipe",
182 /* EDOM 33 */ "Numerical argument out of domain",
183 /* ERANGE 34 */ "Numerical result out of range",
184 /* ENOMSG 35 */ "No message of desired type",
185 /* EIDRM 36 */ "Identifier removed",
186 /* ECHRNG 37 */ "Channel number out of range",
187 /* EL2NSYNC 38 */ "Level 2 not synchronized",
188 /* EL3HLT 39 */ "Level 3 halted",
189 /* EL3RST 40 */ "Level 3 reset",
190 /* ELNRNG 41 */ "Link number out of range",
191 /* EUNATCH 42 */ "Protocol driver not attached",
192 /* ENOCSI 43 */ "No CSI structure available",
193 /* EL2HLT 44 */ "Level 2 halted",
194 /* EDEADLK 45 */ "Resource deadlock avoided",
195 /* ENOLCK 46 */ "No locks available",
199 /* EBADE 50 */ "Invalid exchange",
200 /* EBADR 51 */ "Invalid request descriptor",
201 /* EXFULL 52 */ "Exchange full",
202 /* ENOANO 53 */ "No anode",
203 /* EBADRQC 54 */ "Invalid request code",
204 /* EBADSLT 55 */ "Invalid slot",
205 /* EDEADLOCK 56 */ "File locking deadlock error",
206 /* EBFONT 57 */ "Bad font file format",
209 /* ENOSTR 60 */ "Device not a stream",
210 /* ENODATA 61 */ "No data available",
211 /* ETIME 62 */ "Timer expired",
212 /* ENOSR 63 */ "Out of streams resources",
213 /* ENONET 64 */ "Machine is not on the network",
214 /* ENOPKG 65 */ "Package not installed",
215 /* EREMOTE 66 */ "Object is remote",
216 /* ENOLINK 67 */ "Link has been severed",
217 /* EADV 68 */ "Advertise error",
218 /* ESRMNT 69 */ "Srmount error",
219 /* ECOMM 70 */ "Communication error on send",
220 /* EPROTO 71 */ "Protocol error",
223 /* EMULTIHOP 74 */ "Multihop attempted",
224 /* ELBIN 75 */ "Inode is remote (not really error)",
225 /* EDOTDOT 76 */ "RFS specific error",
226 /* EBADMSG 77 */ "Bad message",
228 /* EFTYPE 79 */ "Inappropriate file type or format",
229 /* ENOTUNIQ 80 */ "Name not unique on network",
230 /* EBADFD 81 */ "File descriptor in bad state",
231 /* EREMCHG 82 */ "Remote address changed",
232 /* ELIBACC 83 */ "Can not access a needed shared library",
233 /* ELIBBAD 84 */ "Accessing a corrupted shared library",
234 /* ELIBSCN 85 */ ".lib section in a.out corrupted",
235 /* ELIBMAX 86 */ "Attempting to link in too many shared libraries",
236 /* ELIBEXEC 87 */ "Cannot exec a shared library directly",
237 /* ENOSYS 88 */ "Function not implemented",
238 /* ENMFILE 89 */ "No more files",
239 /* ENOTEMPTY 90 */ "Directory not empty",
240 /* ENAMETOOLONG 91 */ "File name too long",
241 /* ELOOP 92 */ "Too many levels of symbolic links",
244 /* EOPNOTSUPP 95 */ "Operation not supported",
245 /* EPFNOSUPPORT 96 */ "Protocol family not supported",
253 /* ECONNRESET 104 */ "Connection reset by peer",
254 /* ENOBUFS 105 */ "No buffer space available",
255 /* EAFNOSUPPORT 106 */ "Address family not supported by protocol",
256 /* EPROTOTYPE 107 */ "Protocol wrong type for socket",
257 /* ENOTSOCK 108 */ "Socket operation on non-socket",
258 /* ENOPROTOOPT 109 */ "Protocol not available",
259 /* ESHUTDOWN 110 */ "Cannot send after transport endpoint shutdown",
260 /* ECONNREFUSED 111 */ "Connection refused",
261 /* EADDRINUSE 112 */ "Address already in use",
262 /* ECONNABORTED 113 */ "Software caused connection abort",
263 /* ENETUNREACH 114 */ "Network is unreachable",
264 /* ENETDOWN 115 */ "Network is down",
265 /* ETIMEDOUT 116 */ "Connection timed out",
266 /* EHOSTDOWN 117 */ "Host is down",
267 /* EHOSTUNREACH 118 */ "No route to host",
268 /* EINPROGRESS 119 */ "Operation now in progress",
269 /* EALREADY 120 */ "Operation already in progress",
270 /* EDESTADDRREQ 121 */ "Destination address required",
271 /* EMSGSIZE 122 */ "Message too long",
272 /* EPROTONOSUPPORT 123 */ "Protocol not supported",
273 /* ESOCKTNOSUPPORT 124 */ "Socket type not supported",
274 /* EADDRNOTAVAIL 125 */ "Cannot assign requested address",
275 /* ENETRESET 126 */ "Network dropped connection on reset",
276 /* EISCONN 127 */ "Transport endpoint is already connected",
277 /* ENOTCONN 128 */ "Transport endpoint is not connected",
278 /* ETOOMANYREFS 129 */ "Too many references: cannot splice",
279 /* EPROCLIM 130 */ "Too many processes",
280 /* EUSERS 131 */ "Too many users",
281 /* EDQUOT 132 */ "Disk quota exceeded",
282 /* ESTALE 133 */ "Stale NFS file handle",
283 /* ENOTSUP 134 */ "Not supported",
284 /* ENOMEDIUM 135 */ "No medium found",
285 /* ENOSHARE 136 */ "No such host or network path",
286 /* ECASECLASH 137 */ "Filename exists with different case",
287 /* EILSEQ 138 */ "Invalid or incomplete multibyte or wide character",
288 /* EOVERFLOW 139 */ "Value too large for defined data type",
289 /* ECANCELED 140 */ "Operation canceled",
290 /* ENOTRECOVERABLE 141 */ "State not recoverable",
291 /* EOWNERDEAD 142 */ "Previous owner died",
292 /* ESTRPIPE 143 */ "Streams pipe error"
295 int NO_COPY_INIT _sys_nerr = sizeof (_sys_errlist) / sizeof (_sys_errlist[0]);
299 geterrno_from_win_error (DWORD code, int deferrno)
301 for (int i = 0; errmap[i].w != 0; ++i)
302 if (code == errmap[i].w)
304 syscall_printf ("windows error %u == errno %d", code, errmap[i].e);
308 syscall_printf ("unknown windows error %u, setting errno to %d", code,
310 return deferrno; /* FIXME: what's so special about EACCESS? */
313 /* seterrno_from_win_error: Given a Windows error code, set errno
316 seterrno_from_win_error (const char *file, int line, DWORD code)
318 syscall_printf ("%s:%d windows error %d", file, line, code);
319 set_errno (geterrno_from_win_error (code, EACCES));
322 /* seterrno: Set `errno' based on GetLastError (). */
324 seterrno (const char *file, int line)
326 seterrno_from_win_error (file, line, GetLastError ());
329 extern char *_user_strerror _PARAMS ((int));
332 strerror_worker (int errnum)
335 if (errnum >= 0 && errnum < _sys_nerr)
336 res = (char *) _sys_errlist [errnum];
342 /* strerror: convert from errno values to error strings */
344 strerror (int errnum)
346 char *errstr = strerror_worker (errnum);
348 __small_sprintf (errstr = _my_tls.locals.strerror_buf, "Unknown error %u",
355 strerror_r (int errnum, char *buf, size_t n)
357 char *errstr = strerror_worker (errnum);
360 if (strlen (errstr) >= n)
362 strcpy (buf, errstr);