Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Stefan Krah | 6df5cae | 2012-11-12 20:14:36 +0100 | [diff] [blame] | 2 | #include "osdefs.h" |
Stefan Krah | 6c01e38 | 2014-01-20 15:31:08 +0100 | [diff] [blame] | 3 | #include <locale.h> |
| 4 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 5 | #ifdef MS_WINDOWS |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 6 | # include <malloc.h> |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 7 | # include <windows.h> |
| 8 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 9 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 10 | #ifdef HAVE_LANGINFO_H |
| 11 | #include <langinfo.h> |
| 12 | #endif |
| 13 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 14 | #ifdef HAVE_SYS_IOCTL_H |
| 15 | #include <sys/ioctl.h> |
| 16 | #endif |
| 17 | |
| 18 | #ifdef HAVE_FCNTL_H |
| 19 | #include <fcntl.h> |
| 20 | #endif /* HAVE_FCNTL_H */ |
| 21 | |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 22 | #ifdef __APPLE__ |
| 23 | extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size); |
| 24 | #endif |
| 25 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 26 | #ifdef O_CLOEXEC |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 27 | /* Does open() support the O_CLOEXEC flag? Possible values: |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 28 | |
| 29 | -1: unknown |
| 30 | 0: open() ignores O_CLOEXEC flag, ex: Linux kernel older than 2.6.23 |
| 31 | 1: open() supports O_CLOEXEC flag, close-on-exec is set |
| 32 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 33 | The flag is used by _Py_open(), _Py_open_noraise(), io.FileIO |
| 34 | and os.open(). */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 35 | int _Py_open_cloexec_works = -1; |
| 36 | #endif |
| 37 | |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 38 | PyObject * |
| 39 | _Py_device_encoding(int fd) |
| 40 | { |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 41 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 42 | UINT cp; |
| 43 | #endif |
| 44 | if (!_PyVerify_fd(fd) || !isatty(fd)) { |
| 45 | Py_RETURN_NONE; |
| 46 | } |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 47 | #if defined(MS_WINDOWS) |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 48 | if (fd == 0) |
| 49 | cp = GetConsoleCP(); |
| 50 | else if (fd == 1 || fd == 2) |
| 51 | cp = GetConsoleOutputCP(); |
| 52 | else |
| 53 | cp = 0; |
| 54 | /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application |
| 55 | has no console */ |
| 56 | if (cp != 0) |
| 57 | return PyUnicode_FromFormat("cp%u", (unsigned int)cp); |
| 58 | #elif defined(CODESET) |
| 59 | { |
| 60 | char *codeset = nl_langinfo(CODESET); |
| 61 | if (codeset != NULL && codeset[0] != 0) |
| 62 | return PyUnicode_FromString(codeset); |
| 63 | } |
| 64 | #endif |
| 65 | Py_RETURN_NONE; |
| 66 | } |
| 67 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 68 | #if !defined(__APPLE__) && !defined(MS_WINDOWS) |
| 69 | extern int _Py_normalize_encoding(const char *, char *, size_t); |
| 70 | |
| 71 | /* Workaround FreeBSD and OpenIndiana locale encoding issue with the C locale. |
| 72 | On these operating systems, nl_langinfo(CODESET) announces an alias of the |
| 73 | ASCII encoding, whereas mbstowcs() and wcstombs() functions use the |
| 74 | ISO-8859-1 encoding. The problem is that os.fsencode() and os.fsdecode() use |
| 75 | locale.getpreferredencoding() codec. For example, if command line arguments |
| 76 | are decoded by mbstowcs() and encoded back by os.fsencode(), we get a |
| 77 | UnicodeEncodeError instead of retrieving the original byte string. |
| 78 | |
| 79 | The workaround is enabled if setlocale(LC_CTYPE, NULL) returns "C", |
| 80 | nl_langinfo(CODESET) announces "ascii" (or an alias to ASCII), and at least |
| 81 | one byte in range 0x80-0xff can be decoded from the locale encoding. The |
| 82 | workaround is also enabled on error, for example if getting the locale |
| 83 | failed. |
| 84 | |
Philip Jenvey | 215c49a | 2013-01-15 13:24:12 -0800 | [diff] [blame] | 85 | Values of force_ascii: |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 86 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 87 | 1: the workaround is used: Py_EncodeLocale() uses |
| 88 | encode_ascii_surrogateescape() and Py_DecodeLocale() uses |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 89 | decode_ascii_surrogateescape() |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 90 | 0: the workaround is not used: Py_EncodeLocale() uses wcstombs() and |
| 91 | Py_DecodeLocale() uses mbstowcs() |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 92 | -1: unknown, need to call check_force_ascii() to get the value |
| 93 | */ |
| 94 | static int force_ascii = -1; |
| 95 | |
| 96 | static int |
| 97 | check_force_ascii(void) |
| 98 | { |
| 99 | char *loc; |
| 100 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 101 | char *codeset, **alias; |
| 102 | char encoding[100]; |
| 103 | int is_ascii; |
| 104 | unsigned int i; |
| 105 | char* ascii_aliases[] = { |
| 106 | "ascii", |
| 107 | "646", |
| 108 | "ansi-x3.4-1968", |
| 109 | "ansi-x3-4-1968", |
| 110 | "ansi-x3.4-1986", |
| 111 | "cp367", |
| 112 | "csascii", |
| 113 | "ibm367", |
| 114 | "iso646-us", |
| 115 | "iso-646.irv-1991", |
| 116 | "iso-ir-6", |
| 117 | "us", |
| 118 | "us-ascii", |
| 119 | NULL |
| 120 | }; |
| 121 | #endif |
| 122 | |
| 123 | loc = setlocale(LC_CTYPE, NULL); |
| 124 | if (loc == NULL) |
| 125 | goto error; |
| 126 | if (strcmp(loc, "C") != 0) { |
| 127 | /* the LC_CTYPE locale is different than C */ |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | #if defined(HAVE_LANGINFO_H) && defined(CODESET) |
| 132 | codeset = nl_langinfo(CODESET); |
| 133 | if (!codeset || codeset[0] == '\0') { |
| 134 | /* CODESET is not set or empty */ |
| 135 | goto error; |
| 136 | } |
| 137 | if (!_Py_normalize_encoding(codeset, encoding, sizeof(encoding))) |
| 138 | goto error; |
| 139 | |
| 140 | is_ascii = 0; |
| 141 | for (alias=ascii_aliases; *alias != NULL; alias++) { |
| 142 | if (strcmp(encoding, *alias) == 0) { |
| 143 | is_ascii = 1; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | if (!is_ascii) { |
| 148 | /* nl_langinfo(CODESET) is not "ascii" or an alias of ASCII */ |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | for (i=0x80; i<0xff; i++) { |
| 153 | unsigned char ch; |
| 154 | wchar_t wch; |
| 155 | size_t res; |
| 156 | |
| 157 | ch = (unsigned char)i; |
| 158 | res = mbstowcs(&wch, (char*)&ch, 1); |
| 159 | if (res != (size_t)-1) { |
| 160 | /* decoding a non-ASCII character from the locale encoding succeed: |
| 161 | the locale encoding is not ASCII, force ASCII */ |
| 162 | return 1; |
| 163 | } |
| 164 | } |
| 165 | /* None of the bytes in the range 0x80-0xff can be decoded from the locale |
| 166 | encoding: the locale encoding is really ASCII */ |
| 167 | return 0; |
| 168 | #else |
| 169 | /* nl_langinfo(CODESET) is not available: always force ASCII */ |
| 170 | return 1; |
| 171 | #endif |
| 172 | |
| 173 | error: |
| 174 | /* if an error occured, force the ASCII encoding */ |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | static char* |
| 179 | encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos) |
| 180 | { |
| 181 | char *result = NULL, *out; |
| 182 | size_t len, i; |
| 183 | wchar_t ch; |
| 184 | |
| 185 | if (error_pos != NULL) |
| 186 | *error_pos = (size_t)-1; |
| 187 | |
| 188 | len = wcslen(text); |
| 189 | |
| 190 | result = PyMem_Malloc(len + 1); /* +1 for NUL byte */ |
| 191 | if (result == NULL) |
| 192 | return NULL; |
| 193 | |
| 194 | out = result; |
| 195 | for (i=0; i<len; i++) { |
| 196 | ch = text[i]; |
| 197 | |
| 198 | if (ch <= 0x7f) { |
| 199 | /* ASCII character */ |
| 200 | *out++ = (char)ch; |
| 201 | } |
| 202 | else if (0xdc80 <= ch && ch <= 0xdcff) { |
| 203 | /* UTF-8b surrogate */ |
| 204 | *out++ = (char)(ch - 0xdc00); |
| 205 | } |
| 206 | else { |
| 207 | if (error_pos != NULL) |
| 208 | *error_pos = i; |
| 209 | PyMem_Free(result); |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | *out = '\0'; |
| 214 | return result; |
| 215 | } |
| 216 | #endif /* !defined(__APPLE__) && !defined(MS_WINDOWS) */ |
| 217 | |
| 218 | #if !defined(__APPLE__) && (!defined(MS_WINDOWS) || !defined(HAVE_MBRTOWC)) |
| 219 | static wchar_t* |
| 220 | decode_ascii_surrogateescape(const char *arg, size_t *size) |
| 221 | { |
| 222 | wchar_t *res; |
| 223 | unsigned char *in; |
| 224 | wchar_t *out; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 225 | size_t argsize = strlen(arg) + 1; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 226 | |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 227 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 228 | return NULL; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 229 | res = PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 230 | if (!res) |
| 231 | return NULL; |
| 232 | |
| 233 | in = (unsigned char*)arg; |
| 234 | out = res; |
| 235 | while(*in) |
| 236 | if(*in < 128) |
| 237 | *out++ = *in++; |
| 238 | else |
| 239 | *out++ = 0xdc00 + *in++; |
| 240 | *out = 0; |
| 241 | if (size != NULL) |
| 242 | *size = out - res; |
| 243 | return res; |
| 244 | } |
| 245 | #endif |
| 246 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 247 | |
| 248 | /* Decode a byte string from the locale encoding with the |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 249 | surrogateescape error handler: undecodable bytes are decoded as characters |
| 250 | in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 251 | character, escape the bytes using the surrogateescape error handler instead |
| 252 | of decoding them. |
| 253 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 254 | Return a pointer to a newly allocated wide character string, use |
| 255 | PyMem_RawFree() to free the memory. If size is not NULL, write the number of |
| 256 | wide characters excluding the null character into *size |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 257 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 258 | Return NULL on decoding error or memory allocation error. If *size* is not |
| 259 | NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on |
| 260 | decoding error. |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 261 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 262 | Decoding errors should never happen, unless there is a bug in the C |
| 263 | library. |
| 264 | |
| 265 | Use the Py_EncodeLocale() function to encode the character string back to a |
| 266 | byte string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 267 | wchar_t* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 268 | Py_DecodeLocale(const char* arg, size_t *size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 269 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 270 | #ifdef __APPLE__ |
| 271 | wchar_t *wstr; |
| 272 | wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 273 | if (size != NULL) { |
| 274 | if (wstr != NULL) |
| 275 | *size = wcslen(wstr); |
| 276 | else |
| 277 | *size = (size_t)-1; |
| 278 | } |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 279 | return wstr; |
| 280 | #else |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 281 | wchar_t *res; |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 282 | size_t argsize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 283 | size_t count; |
Victor Stinner | 313f10c | 2013-05-07 23:48:56 +0200 | [diff] [blame] | 284 | #ifdef HAVE_MBRTOWC |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 285 | unsigned char *in; |
| 286 | wchar_t *out; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 287 | mbstate_t mbs; |
| 288 | #endif |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 289 | |
| 290 | #ifndef MS_WINDOWS |
| 291 | if (force_ascii == -1) |
| 292 | force_ascii = check_force_ascii(); |
| 293 | |
| 294 | if (force_ascii) { |
| 295 | /* force ASCII encoding to workaround mbstowcs() issue */ |
| 296 | res = decode_ascii_surrogateescape(arg, size); |
| 297 | if (res == NULL) |
| 298 | goto oom; |
| 299 | return res; |
| 300 | } |
| 301 | #endif |
| 302 | |
| 303 | #ifdef HAVE_BROKEN_MBSTOWCS |
| 304 | /* Some platforms have a broken implementation of |
| 305 | * mbstowcs which does not count the characters that |
| 306 | * would result from conversion. Use an upper bound. |
| 307 | */ |
| 308 | argsize = strlen(arg); |
| 309 | #else |
| 310 | argsize = mbstowcs(NULL, arg, 0); |
| 311 | #endif |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 312 | if (argsize != (size_t)-1) { |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 313 | if (argsize == PY_SSIZE_T_MAX) |
| 314 | goto oom; |
| 315 | argsize += 1; |
| 316 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 317 | goto oom; |
Benjamin Peterson | 10ecaa2 | 2015-01-04 16:05:39 -0600 | [diff] [blame] | 318 | res = (wchar_t *)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 319 | if (!res) |
| 320 | goto oom; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 321 | count = mbstowcs(res, arg, argsize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 322 | if (count != (size_t)-1) { |
| 323 | wchar_t *tmp; |
| 324 | /* Only use the result if it contains no |
| 325 | surrogate characters. */ |
| 326 | for (tmp = res; *tmp != 0 && |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 327 | !Py_UNICODE_IS_SURROGATE(*tmp); tmp++) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 328 | ; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 329 | if (*tmp == 0) { |
| 330 | if (size != NULL) |
| 331 | *size = count; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 332 | return res; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 333 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 334 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 335 | PyMem_RawFree(res); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 336 | } |
| 337 | /* Conversion failed. Fall back to escaping with surrogateescape. */ |
| 338 | #ifdef HAVE_MBRTOWC |
| 339 | /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */ |
| 340 | |
| 341 | /* Overallocate; as multi-byte characters are in the argument, the |
| 342 | actual output could use less memory. */ |
| 343 | argsize = strlen(arg) + 1; |
Benjamin Peterson | f18bf6f | 2015-01-04 16:03:17 -0600 | [diff] [blame] | 344 | if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) |
| 345 | goto oom; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 346 | res = (wchar_t*)PyMem_RawMalloc(argsize*sizeof(wchar_t)); |
Victor Stinner | 19de4c3 | 2010-11-08 23:30:46 +0000 | [diff] [blame] | 347 | if (!res) |
| 348 | goto oom; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 349 | in = (unsigned char*)arg; |
| 350 | out = res; |
| 351 | memset(&mbs, 0, sizeof mbs); |
| 352 | while (argsize) { |
| 353 | size_t converted = mbrtowc(out, (char*)in, argsize, &mbs); |
| 354 | if (converted == 0) |
| 355 | /* Reached end of string; null char stored. */ |
| 356 | break; |
| 357 | if (converted == (size_t)-2) { |
| 358 | /* Incomplete character. This should never happen, |
| 359 | since we provide everything that we have - |
| 360 | unless there is a bug in the C library, or I |
| 361 | misunderstood how mbrtowc works. */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 362 | PyMem_RawFree(res); |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 363 | if (size != NULL) |
| 364 | *size = (size_t)-2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 365 | return NULL; |
| 366 | } |
| 367 | if (converted == (size_t)-1) { |
| 368 | /* Conversion error. Escape as UTF-8b, and start over |
| 369 | in the initial shift state. */ |
| 370 | *out++ = 0xdc00 + *in++; |
| 371 | argsize--; |
| 372 | memset(&mbs, 0, sizeof mbs); |
| 373 | continue; |
| 374 | } |
Victor Stinner | 76df43d | 2012-10-30 01:42:39 +0100 | [diff] [blame] | 375 | if (Py_UNICODE_IS_SURROGATE(*out)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 376 | /* Surrogate character. Escape the original |
| 377 | byte sequence with surrogateescape. */ |
| 378 | argsize -= converted; |
| 379 | while (converted--) |
| 380 | *out++ = 0xdc00 + *in++; |
| 381 | continue; |
| 382 | } |
| 383 | /* successfully converted some bytes */ |
| 384 | in += converted; |
| 385 | argsize -= converted; |
| 386 | out++; |
| 387 | } |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 388 | if (size != NULL) |
| 389 | *size = out - res; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 390 | #else /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 391 | /* Cannot use C locale for escaping; manually escape as if charset |
| 392 | is ASCII (i.e. escape all bytes > 128. This will still roundtrip |
| 393 | correctly in the locale's charset, which must be an ASCII superset. */ |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 394 | res = decode_ascii_surrogateescape(arg, size); |
| 395 | if (res == NULL) |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 396 | goto oom; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 397 | #endif /* HAVE_MBRTOWC */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 398 | return res; |
| 399 | oom: |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 400 | if (size != NULL) |
| 401 | *size = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 402 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 403 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 406 | /* Encode a wide character string to the locale encoding with the |
| 407 | surrogateescape error handler: surrogate characters in the range |
| 408 | U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 409 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 410 | Return a pointer to a newly allocated byte string, use PyMem_Free() to free |
| 411 | the memory. Return NULL on encoding or memory allocation error. |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 412 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 413 | If error_pos is not NULL, *error_pos is set to the index of the invalid |
| 414 | character on encoding error, or set to (size_t)-1 otherwise. |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 415 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 416 | Use the Py_DecodeLocale() function to decode the bytes string back to a wide |
| 417 | character string. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 418 | char* |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 419 | Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 420 | { |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 421 | #ifdef __APPLE__ |
| 422 | Py_ssize_t len; |
| 423 | PyObject *unicode, *bytes = NULL; |
| 424 | char *cpath; |
| 425 | |
| 426 | unicode = PyUnicode_FromWideChar(text, wcslen(text)); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 427 | if (unicode == NULL) |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 428 | return NULL; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 429 | |
| 430 | bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); |
| 431 | Py_DECREF(unicode); |
| 432 | if (bytes == NULL) { |
| 433 | PyErr_Clear(); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 434 | if (error_pos != NULL) |
| 435 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
| 438 | |
| 439 | len = PyBytes_GET_SIZE(bytes); |
| 440 | cpath = PyMem_Malloc(len+1); |
| 441 | if (cpath == NULL) { |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 442 | PyErr_Clear(); |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 443 | Py_DECREF(bytes); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 444 | if (error_pos != NULL) |
| 445 | *error_pos = (size_t)-1; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 446 | return NULL; |
| 447 | } |
| 448 | memcpy(cpath, PyBytes_AsString(bytes), len + 1); |
| 449 | Py_DECREF(bytes); |
| 450 | return cpath; |
| 451 | #else /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 452 | const size_t len = wcslen(text); |
| 453 | char *result = NULL, *bytes = NULL; |
| 454 | size_t i, size, converted; |
| 455 | wchar_t c, buf[2]; |
| 456 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 457 | #ifndef MS_WINDOWS |
| 458 | if (force_ascii == -1) |
| 459 | force_ascii = check_force_ascii(); |
| 460 | |
| 461 | if (force_ascii) |
| 462 | return encode_ascii_surrogateescape(text, error_pos); |
| 463 | #endif |
| 464 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 465 | /* The function works in two steps: |
| 466 | 1. compute the length of the output buffer in bytes (size) |
| 467 | 2. outputs the bytes */ |
| 468 | size = 0; |
| 469 | buf[1] = 0; |
| 470 | while (1) { |
| 471 | for (i=0; i < len; i++) { |
| 472 | c = text[i]; |
| 473 | if (c >= 0xdc80 && c <= 0xdcff) { |
| 474 | /* UTF-8b surrogate */ |
| 475 | if (bytes != NULL) { |
| 476 | *bytes++ = c - 0xdc00; |
| 477 | size--; |
| 478 | } |
| 479 | else |
| 480 | size++; |
| 481 | continue; |
| 482 | } |
| 483 | else { |
| 484 | buf[0] = c; |
| 485 | if (bytes != NULL) |
| 486 | converted = wcstombs(bytes, buf, size); |
| 487 | else |
| 488 | converted = wcstombs(NULL, buf, 0); |
| 489 | if (converted == (size_t)-1) { |
| 490 | if (result != NULL) |
| 491 | PyMem_Free(result); |
Victor Stinner | 2f02a51 | 2010-11-08 22:43:46 +0000 | [diff] [blame] | 492 | if (error_pos != NULL) |
| 493 | *error_pos = i; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
| 496 | if (bytes != NULL) { |
| 497 | bytes += converted; |
| 498 | size -= converted; |
| 499 | } |
| 500 | else |
| 501 | size += converted; |
| 502 | } |
| 503 | } |
| 504 | if (result != NULL) { |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 505 | *bytes = '\0'; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | |
| 509 | size += 1; /* nul byte at the end */ |
| 510 | result = PyMem_Malloc(size); |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 511 | if (result == NULL) { |
| 512 | if (error_pos != NULL) |
| 513 | *error_pos = (size_t)-1; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 514 | return NULL; |
Victor Stinner | 0d92c4f | 2012-11-12 23:32:21 +0100 | [diff] [blame] | 515 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 516 | bytes = result; |
| 517 | } |
| 518 | return result; |
Victor Stinner | e262377 | 2012-11-12 23:04:02 +0100 | [diff] [blame] | 519 | #endif /* __APPLE__ */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 522 | /* In principle, this should use HAVE__WSTAT, and _wstat |
| 523 | should be detected by autoconf. However, no current |
| 524 | POSIX system provides that function, so testing for |
| 525 | it is pointless. |
| 526 | Not sure whether the MS_WINDOWS guards are necessary: |
| 527 | perhaps for cygwin/mingw builds? |
| 528 | */ |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 529 | #if defined(HAVE_STAT) && !defined(MS_WINDOWS) |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 530 | |
| 531 | /* Get file status. Encode the path to the locale encoding. */ |
| 532 | |
Victor Stinner | b306d75 | 2010-10-07 22:09:40 +0000 | [diff] [blame] | 533 | int |
| 534 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 535 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 536 | int err; |
| 537 | char *fname; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 538 | fname = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 539 | if (fname == NULL) { |
| 540 | errno = EINVAL; |
| 541 | return -1; |
| 542 | } |
| 543 | err = stat(fname, buf); |
| 544 | PyMem_Free(fname); |
| 545 | return err; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 546 | } |
| 547 | #endif |
| 548 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 549 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 550 | #if defined(HAVE_FSTAT) || defined(MS_WINDOWS) |
| 551 | |
| 552 | #ifdef MS_WINDOWS |
| 553 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 554 | |
| 555 | static void |
| 556 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
| 557 | { |
| 558 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 559 | /* Cannot simply cast and dereference in_ptr, |
| 560 | since it might not be aligned properly */ |
| 561 | __int64 in; |
| 562 | memcpy(&in, in_ptr, sizeof(in)); |
| 563 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 564 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
| 565 | } |
| 566 | |
| 567 | void |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 568 | _Py_time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 569 | { |
| 570 | /* XXX endianness */ |
| 571 | __int64 out; |
| 572 | out = time_in + secs_between_epochs; |
| 573 | out = out * 10000000 + nsec_in / 100; |
| 574 | memcpy(out_ptr, &out, sizeof(out)); |
| 575 | } |
| 576 | |
| 577 | /* Below, we *know* that ugo+r is 0444 */ |
| 578 | #if _S_IREAD != 0400 |
| 579 | #error Unsupported C library |
| 580 | #endif |
| 581 | static int |
| 582 | attributes_to_mode(DWORD attr) |
| 583 | { |
| 584 | int m = 0; |
| 585 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 586 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 587 | else |
| 588 | m |= _S_IFREG; |
| 589 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 590 | m |= 0444; |
| 591 | else |
| 592 | m |= 0666; |
| 593 | return m; |
| 594 | } |
| 595 | |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 596 | void |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 597 | _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 598 | { |
| 599 | memset(result, 0, sizeof(*result)); |
| 600 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 601 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 602 | result->st_dev = info->dwVolumeSerialNumber; |
| 603 | result->st_rdev = result->st_dev; |
| 604 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 605 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 606 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 607 | result->st_nlink = info->nNumberOfLinks; |
| 608 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
| 609 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 610 | /* first clear the S_IFMT bits */ |
| 611 | result->st_mode ^= (result->st_mode & S_IFMT); |
| 612 | /* now set the bits that make this a symlink */ |
| 613 | result->st_mode |= S_IFLNK; |
| 614 | } |
| 615 | result->st_file_attributes = info->dwFileAttributes; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 616 | } |
| 617 | #endif |
| 618 | |
| 619 | /* Return information about a file. |
| 620 | |
| 621 | On POSIX, use fstat(). |
| 622 | |
| 623 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
| 624 | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
| 625 | than 2 GB because the file size type is an signed 32-bit integer: see issue |
| 626 | #23152. |
| 627 | */ |
| 628 | int |
| 629 | _Py_fstat(int fd, struct _Py_stat_struct *result) |
| 630 | { |
| 631 | #ifdef MS_WINDOWS |
| 632 | BY_HANDLE_FILE_INFORMATION info; |
| 633 | HANDLE h; |
| 634 | int type; |
| 635 | |
| 636 | if (!_PyVerify_fd(fd)) |
| 637 | h = INVALID_HANDLE_VALUE; |
| 638 | else |
| 639 | h = (HANDLE)_get_osfhandle(fd); |
| 640 | |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 641 | /* Protocol violation: we explicitly clear errno, instead of |
| 642 | setting it to a POSIX error. Callers should use GetLastError. */ |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 643 | errno = 0; |
| 644 | |
| 645 | if (h == INVALID_HANDLE_VALUE) { |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 646 | /* This is really a C library error (invalid file handle). |
| 647 | We set the Win32 error to the closes one matching. */ |
| 648 | SetLastError(ERROR_INVALID_HANDLE); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 649 | return -1; |
| 650 | } |
| 651 | memset(result, 0, sizeof(*result)); |
| 652 | |
| 653 | type = GetFileType(h); |
| 654 | if (type == FILE_TYPE_UNKNOWN) { |
| 655 | DWORD error = GetLastError(); |
| 656 | if (error != 0) { |
| 657 | return -1; |
| 658 | } |
| 659 | /* else: valid but unknown file */ |
| 660 | } |
| 661 | |
| 662 | if (type != FILE_TYPE_DISK) { |
| 663 | if (type == FILE_TYPE_CHAR) |
| 664 | result->st_mode = _S_IFCHR; |
| 665 | else if (type == FILE_TYPE_PIPE) |
| 666 | result->st_mode = _S_IFIFO; |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | if (!GetFileInformationByHandle(h, &info)) { |
| 671 | return -1; |
| 672 | } |
| 673 | |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 674 | _Py_attribute_data_to_stat(&info, 0, result); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 675 | /* specific to fstat() */ |
| 676 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 677 | return 0; |
| 678 | #else |
| 679 | return fstat(fd, result); |
| 680 | #endif |
| 681 | } |
| 682 | #endif /* HAVE_FSTAT || MS_WINDOWS */ |
| 683 | |
| 684 | |
| 685 | #ifdef HAVE_STAT |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 686 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 687 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 688 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 689 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 690 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 691 | |
| 692 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 693 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 694 | { |
| 695 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 696 | int err; |
| 697 | struct _stat wstatbuf; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 698 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 699 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 700 | wpath = PyUnicode_AsUnicode(path); |
| 701 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 702 | return -2; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 703 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 704 | if (!err) |
| 705 | statbuf->st_mode = wstatbuf.st_mode; |
| 706 | return err; |
| 707 | #else |
| 708 | int ret; |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 709 | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 710 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 711 | return -2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 712 | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
| 713 | Py_DECREF(bytes); |
| 714 | return ret; |
| 715 | #endif |
| 716 | } |
| 717 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 718 | #endif /* HAVE_STAT */ |
| 719 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 720 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 721 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 722 | get_inheritable(int fd, int raise) |
| 723 | { |
| 724 | #ifdef MS_WINDOWS |
| 725 | HANDLE handle; |
| 726 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 727 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 728 | if (!_PyVerify_fd(fd)) { |
| 729 | if (raise) |
| 730 | PyErr_SetFromErrno(PyExc_OSError); |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | handle = (HANDLE)_get_osfhandle(fd); |
| 735 | if (handle == INVALID_HANDLE_VALUE) { |
| 736 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 737 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | if (!GetHandleInformation(handle, &flags)) { |
| 742 | if (raise) |
| 743 | PyErr_SetFromWindowsErr(0); |
| 744 | return -1; |
| 745 | } |
| 746 | |
| 747 | return (flags & HANDLE_FLAG_INHERIT); |
| 748 | #else |
| 749 | int flags; |
| 750 | |
| 751 | flags = fcntl(fd, F_GETFD, 0); |
| 752 | if (flags == -1) { |
| 753 | if (raise) |
| 754 | PyErr_SetFromErrno(PyExc_OSError); |
| 755 | return -1; |
| 756 | } |
| 757 | return !(flags & FD_CLOEXEC); |
| 758 | #endif |
| 759 | } |
| 760 | |
| 761 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 762 | Return 1 if the file descriptor can be inherited, 0 if it cannot, |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 763 | raise an exception and return -1 on error. */ |
| 764 | int |
| 765 | _Py_get_inheritable(int fd) |
| 766 | { |
| 767 | return get_inheritable(fd, 1); |
| 768 | } |
| 769 | |
| 770 | static int |
| 771 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 772 | { |
| 773 | #ifdef MS_WINDOWS |
| 774 | HANDLE handle; |
| 775 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 776 | #else |
| 777 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 778 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 779 | int request; |
| 780 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 781 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 782 | int flags; |
| 783 | int res; |
| 784 | #endif |
| 785 | |
| 786 | /* atomic_flag_works can only be used to make the file descriptor |
| 787 | non-inheritable */ |
| 788 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 789 | |
| 790 | if (atomic_flag_works != NULL && !inheritable) { |
| 791 | if (*atomic_flag_works == -1) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 792 | int isInheritable = get_inheritable(fd, raise); |
| 793 | if (isInheritable == -1) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 794 | return -1; |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 795 | *atomic_flag_works = !isInheritable; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (*atomic_flag_works) |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | #ifdef MS_WINDOWS |
| 803 | if (!_PyVerify_fd(fd)) { |
| 804 | if (raise) |
| 805 | PyErr_SetFromErrno(PyExc_OSError); |
| 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | handle = (HANDLE)_get_osfhandle(fd); |
| 810 | if (handle == INVALID_HANDLE_VALUE) { |
| 811 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 812 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 813 | return -1; |
| 814 | } |
| 815 | |
| 816 | if (inheritable) |
| 817 | flags = HANDLE_FLAG_INHERIT; |
| 818 | else |
| 819 | flags = 0; |
| 820 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 821 | if (raise) |
| 822 | PyErr_SetFromWindowsErr(0); |
| 823 | return -1; |
| 824 | } |
| 825 | return 0; |
| 826 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 827 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 828 | |
| 829 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 830 | if (ioctl_works != 0) { |
| 831 | /* fast-path: ioctl() only requires one syscall */ |
| 832 | if (inheritable) |
| 833 | request = FIONCLEX; |
| 834 | else |
| 835 | request = FIOCLEX; |
| 836 | err = ioctl(fd, request, NULL); |
| 837 | if (!err) { |
| 838 | ioctl_works = 1; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | if (errno != ENOTTY) { |
| 843 | if (raise) |
| 844 | PyErr_SetFromErrno(PyExc_OSError); |
| 845 | return -1; |
| 846 | } |
| 847 | else { |
| 848 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 849 | device". The ioctl is declared but not supported by the kernel. |
| 850 | Remember that ioctl() doesn't work. It is the case on |
| 851 | Illumos-based OS for example. */ |
| 852 | ioctl_works = 0; |
| 853 | } |
| 854 | /* fallback to fcntl() if ioctl() does not work */ |
| 855 | } |
| 856 | #endif |
| 857 | |
| 858 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 859 | flags = fcntl(fd, F_GETFD); |
| 860 | if (flags < 0) { |
| 861 | if (raise) |
| 862 | PyErr_SetFromErrno(PyExc_OSError); |
| 863 | return -1; |
| 864 | } |
| 865 | |
| 866 | if (inheritable) |
| 867 | flags &= ~FD_CLOEXEC; |
| 868 | else |
| 869 | flags |= FD_CLOEXEC; |
| 870 | res = fcntl(fd, F_SETFD, flags); |
| 871 | if (res < 0) { |
| 872 | if (raise) |
| 873 | PyErr_SetFromErrno(PyExc_OSError); |
| 874 | return -1; |
| 875 | } |
| 876 | return 0; |
| 877 | #endif |
| 878 | } |
| 879 | |
| 880 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 881 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 882 | static int |
| 883 | make_non_inheritable(int fd) |
| 884 | { |
| 885 | return set_inheritable(fd, 0, 0, NULL); |
| 886 | } |
| 887 | |
| 888 | /* Set the inheritable flag of the specified file descriptor. |
| 889 | On success: return 0, on error: raise an exception if raise is nonzero |
| 890 | and return -1. |
| 891 | |
| 892 | If atomic_flag_works is not NULL: |
| 893 | |
| 894 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 895 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 896 | set the inheritable flag |
| 897 | * if *atomic_flag_works==1: do nothing |
| 898 | * if *atomic_flag_works==0: set inheritable flag to False |
| 899 | |
| 900 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 901 | file descriptor. |
| 902 | |
| 903 | atomic_flag_works can only be used to make a file descriptor |
| 904 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 905 | int |
| 906 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 907 | { |
| 908 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 909 | } |
| 910 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 911 | static int |
| 912 | _Py_open_impl(const char *pathname, int flags, int gil_held) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 913 | { |
| 914 | int fd; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 915 | int async_err = 0; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 916 | #ifndef MS_WINDOWS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 917 | int *atomic_flag_works; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 918 | #endif |
| 919 | |
| 920 | #ifdef MS_WINDOWS |
| 921 | flags |= O_NOINHERIT; |
| 922 | #elif defined(O_CLOEXEC) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 923 | atomic_flag_works = &_Py_open_cloexec_works; |
| 924 | flags |= O_CLOEXEC; |
| 925 | #else |
| 926 | atomic_flag_works = NULL; |
| 927 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 928 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 929 | if (gil_held) { |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 930 | do { |
| 931 | Py_BEGIN_ALLOW_THREADS |
| 932 | fd = open(pathname, flags); |
| 933 | Py_END_ALLOW_THREADS |
| 934 | } while (fd < 0 |
| 935 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 936 | if (async_err) |
| 937 | return -1; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 938 | if (fd < 0) { |
| 939 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); |
| 940 | return -1; |
| 941 | } |
| 942 | } |
| 943 | else { |
| 944 | fd = open(pathname, flags); |
| 945 | if (fd < 0) |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | #ifndef MS_WINDOWS |
| 950 | if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 951 | close(fd); |
| 952 | return -1; |
| 953 | } |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 954 | #endif |
| 955 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 956 | return fd; |
| 957 | } |
| 958 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 959 | /* Open a file with the specified flags (wrapper to open() function). |
| 960 | Return a file descriptor on success. Raise an exception and return -1 on |
| 961 | error. |
| 962 | |
| 963 | The file descriptor is created non-inheritable. |
| 964 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 965 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 966 | except if the Python signal handler raises an exception. |
| 967 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 968 | The GIL must be held. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 969 | int |
| 970 | _Py_open(const char *pathname, int flags) |
| 971 | { |
| 972 | /* _Py_open() must be called with the GIL held. */ |
| 973 | assert(PyGILState_Check()); |
| 974 | return _Py_open_impl(pathname, flags, 1); |
| 975 | } |
| 976 | |
| 977 | /* Open a file with the specified flags (wrapper to open() function). |
| 978 | Return a file descriptor on success. Set errno and return -1 on error. |
| 979 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 980 | The file descriptor is created non-inheritable. |
| 981 | |
| 982 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 983 | int |
| 984 | _Py_open_noraise(const char *pathname, int flags) |
| 985 | { |
| 986 | return _Py_open_impl(pathname, flags, 0); |
| 987 | } |
| 988 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 989 | /* Open a file. Use _wfopen() on Windows, encode the path to the locale |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 990 | encoding and use fopen() otherwise. |
| 991 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 992 | The file descriptor is created non-inheritable. |
| 993 | |
| 994 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 995 | FILE * |
| 996 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 997 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 998 | FILE *f; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 999 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1000 | char *cpath; |
| 1001 | char cmode[10]; |
| 1002 | size_t r; |
| 1003 | r = wcstombs(cmode, mode, 10); |
| 1004 | if (r == (size_t)-1 || r >= 10) { |
| 1005 | errno = EINVAL; |
| 1006 | return NULL; |
| 1007 | } |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1008 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1009 | if (cpath == NULL) |
| 1010 | return NULL; |
| 1011 | f = fopen(cpath, cmode); |
| 1012 | PyMem_Free(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1013 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1014 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1015 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1016 | if (f == NULL) |
| 1017 | return NULL; |
| 1018 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1019 | fclose(f); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1025 | /* Wrapper to fopen(). |
| 1026 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1027 | The file descriptor is created non-inheritable. |
| 1028 | |
| 1029 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1030 | FILE* |
| 1031 | _Py_fopen(const char *pathname, const char *mode) |
| 1032 | { |
| 1033 | FILE *f = fopen(pathname, mode); |
| 1034 | if (f == NULL) |
| 1035 | return NULL; |
| 1036 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1037 | fclose(f); |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | return f; |
| 1041 | } |
| 1042 | |
| 1043 | /* Open a file. Call _wfopen() on Windows, or encode the path to the filesystem |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1044 | encoding and call fopen() otherwise. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1045 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1046 | Return the new file object on success. Raise an exception and return NULL |
| 1047 | on error. |
| 1048 | |
| 1049 | The file descriptor is created non-inheritable. |
| 1050 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1051 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 1052 | except if the Python signal handler raises an exception. |
| 1053 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1054 | The GIL must be held. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1055 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1056 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1057 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1058 | FILE *f; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1059 | int async_err = 0; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1060 | #ifdef MS_WINDOWS |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1061 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1062 | wchar_t wmode[10]; |
| 1063 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1064 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1065 | assert(PyGILState_Check()); |
| 1066 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1067 | if (!PyUnicode_Check(path)) { |
| 1068 | PyErr_Format(PyExc_TypeError, |
| 1069 | "str file path expected under Windows, got %R", |
| 1070 | Py_TYPE(path)); |
| 1071 | return NULL; |
| 1072 | } |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1073 | wpath = PyUnicode_AsUnicode(path); |
| 1074 | if (wpath == NULL) |
| 1075 | return NULL; |
| 1076 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1077 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1078 | if (usize == 0) { |
| 1079 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1080 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1081 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1082 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1083 | do { |
| 1084 | Py_BEGIN_ALLOW_THREADS |
| 1085 | f = _wfopen(wpath, wmode); |
| 1086 | Py_END_ALLOW_THREADS |
| 1087 | } while (f == NULL |
| 1088 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1089 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1090 | PyObject *bytes; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1091 | char *path_bytes; |
| 1092 | |
| 1093 | assert(PyGILState_Check()); |
| 1094 | |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1095 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1096 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1097 | path_bytes = PyBytes_AS_STRING(bytes); |
| 1098 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1099 | do { |
| 1100 | Py_BEGIN_ALLOW_THREADS |
| 1101 | f = fopen(path_bytes, mode); |
| 1102 | Py_END_ALLOW_THREADS |
| 1103 | } while (f == NULL |
| 1104 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1105 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1106 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1107 | #endif |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1108 | if (async_err) |
| 1109 | return NULL; |
| 1110 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1111 | if (f == NULL) { |
| 1112 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1113 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1117 | fclose(f); |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame^] | 1123 | /* Read count bytes from fd into buf. |
| 1124 | * |
| 1125 | * On success, return the number of read bytes, it can be lower than count. |
| 1126 | * If the current file offset is at or past the end of file, no bytes are read, |
| 1127 | * and read() returns zero. |
| 1128 | * |
| 1129 | * On error, raise an exception, set errno and return -1. |
| 1130 | * |
| 1131 | * When interrupted by a signal (read() fails with EINTR), retry the syscall. |
| 1132 | * If the Python signal handler raises an exception, the function returns -1 |
| 1133 | * (the syscall is not retried). |
| 1134 | * |
| 1135 | * The GIL must be held. */ |
| 1136 | Py_ssize_t |
| 1137 | _Py_read(int fd, void *buf, size_t count) |
| 1138 | { |
| 1139 | Py_ssize_t n; |
| 1140 | int async_err = 0; |
| 1141 | |
| 1142 | /* _Py_read() must not be called with an exception set, otherwise the |
| 1143 | * caller may think that read() was interrupted by a signal and the signal |
| 1144 | * handler raised an exception. */ |
| 1145 | assert(!PyErr_Occurred()); |
| 1146 | |
| 1147 | if (!_PyVerify_fd(self->fd)) { |
| 1148 | PyErr_SetFromErrno(PyExc_OSError); |
| 1149 | assert(errno == EBADF); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | #ifdef MS_WINDOWS |
| 1154 | if (count > INT_MAX) { |
| 1155 | /* On Windows, the count parameter of read() is an int */ |
| 1156 | count = INT_MAX; |
| 1157 | } |
| 1158 | #else |
| 1159 | if (count > PY_SSIZE_T_MAX) { |
| 1160 | /* if count is greater than PY_SSIZE_T_MAX, |
| 1161 | * read() result is undefined */ |
| 1162 | count = PY_SSIZE_T_MAX; |
| 1163 | } |
| 1164 | #endif |
| 1165 | |
| 1166 | do { |
| 1167 | Py_BEGIN_ALLOW_THREADS |
| 1168 | errno = 0; |
| 1169 | #ifdef MS_WINDOWS |
| 1170 | n = read(fd, buf, (int)count); |
| 1171 | #else |
| 1172 | n = read(fd, buf, count); |
| 1173 | #endif |
| 1174 | Py_END_ALLOW_THREADS |
| 1175 | } while (n < 0 && errno == EINTR && |
| 1176 | !(async_err = PyErr_CheckSignals())); |
| 1177 | |
| 1178 | if (async_err) { |
| 1179 | /* read() was interrupted by a signal (failed with EINTR) |
| 1180 | * and the Python signal handler raised an exception */ |
| 1181 | assert(errno == EINTR && PyErr_Occurred()); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | if (n < 0) { |
| 1185 | #ifndef NDEBUG |
| 1186 | int err = errno; |
| 1187 | #endif |
| 1188 | PyErr_SetFromErrno(PyExc_OSError); |
| 1189 | assert(errno == err); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
| 1193 | return n; |
| 1194 | } |
| 1195 | |
| 1196 | /* Write count bytes of buf into fd. |
| 1197 | * |
| 1198 | * -On success, return the number of written bytes, it can be lower than count |
| 1199 | * including 0 |
| 1200 | * - On error, raise an exception, set errno and return -1. |
| 1201 | * |
| 1202 | * When interrupted by a signal (write() fails with EINTR), retry the syscall. |
| 1203 | * If the Python signal handler raises an exception, the function returns -1 |
| 1204 | * (the syscall is not retried). |
| 1205 | * |
| 1206 | * The GIL must be held. */ |
| 1207 | Py_ssize_t |
| 1208 | _Py_write(int fd, const void *buf, size_t count) |
| 1209 | { |
| 1210 | Py_ssize_t n; |
| 1211 | int async_err = 0; |
| 1212 | |
| 1213 | /* _Py_write() must not be called with an exception set, otherwise the |
| 1214 | * caller may think that write() was interrupted by a signal and the signal |
| 1215 | * handler raised an exception. */ |
| 1216 | assert(!PyErr_Occurred()); |
| 1217 | |
| 1218 | if (!_PyVerify_fd(fd)) { |
| 1219 | PyErr_SetFromErrno(PyExc_OSError); |
| 1220 | assert(errno == EBADF); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | #ifdef MS_WINDOWS |
| 1225 | if (count > 32767 && isatty(fd)) { |
| 1226 | /* Issue #11395: the Windows console returns an error (12: not |
| 1227 | enough space error) on writing into stdout if stdout mode is |
| 1228 | binary and the length is greater than 66,000 bytes (or less, |
| 1229 | depending on heap usage). */ |
| 1230 | count = 32767; |
| 1231 | } |
| 1232 | else if (count > INT_MAX) |
| 1233 | count = INT_MAX; |
| 1234 | #else |
| 1235 | if (count > PY_SSIZE_T_MAX) { |
| 1236 | /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer |
| 1237 | * to do it ourself to have a portable behaviour. */ |
| 1238 | count = PY_SSIZE_T_MAX; |
| 1239 | } |
| 1240 | #endif |
| 1241 | |
| 1242 | do { |
| 1243 | Py_BEGIN_ALLOW_THREADS |
| 1244 | errno = 0; |
| 1245 | #ifdef MS_WINDOWS |
| 1246 | n = write(fd, buf, (int)count); |
| 1247 | #else |
| 1248 | n = write(fd, buf, count); |
| 1249 | #endif |
| 1250 | Py_END_ALLOW_THREADS |
| 1251 | } while (n < 0 && errno == EINTR && |
| 1252 | !(async_err = PyErr_CheckSignals())); |
| 1253 | |
| 1254 | if (async_err) { |
| 1255 | /* write() was interrupted by a signal (failed with EINTR) |
| 1256 | * and the Python signal handler raised an exception */ |
| 1257 | assert(errno == EINTR && PyErr_Occurred()); |
| 1258 | return -1; |
| 1259 | } |
| 1260 | if (n < 0) { |
| 1261 | #ifndef NDEBUG |
| 1262 | int err = errno; |
| 1263 | #endif |
| 1264 | PyErr_SetFromErrno(PyExc_OSError); |
| 1265 | assert(errno == err); |
| 1266 | return -1; |
| 1267 | } |
| 1268 | |
| 1269 | return n; |
| 1270 | } |
| 1271 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1272 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1273 | |
| 1274 | /* Read value of symbolic link. Encode the path to the locale encoding, decode |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1275 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1276 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1277 | int |
| 1278 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 1279 | { |
| 1280 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1281 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1282 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1283 | int res; |
| 1284 | size_t r1; |
| 1285 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1286 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1287 | if (cpath == NULL) { |
| 1288 | errno = EINVAL; |
| 1289 | return -1; |
| 1290 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1291 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1292 | PyMem_Free(cpath); |
| 1293 | if (res == -1) |
| 1294 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1295 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1296 | errno = EINVAL; |
| 1297 | return -1; |
| 1298 | } |
| 1299 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1300 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 1301 | if (wbuf == NULL) { |
| 1302 | errno = EINVAL; |
| 1303 | return -1; |
| 1304 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1305 | if (bufsiz <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1306 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1307 | errno = EINVAL; |
| 1308 | return -1; |
| 1309 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1310 | wcsncpy(buf, wbuf, bufsiz); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1311 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1312 | return (int)r1; |
| 1313 | } |
| 1314 | #endif |
| 1315 | |
| 1316 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1317 | |
| 1318 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1319 | encoding, decode the result from the locale encoding. |
| 1320 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1321 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1322 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1323 | _Py_wrealpath(const wchar_t *path, |
| 1324 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1325 | { |
| 1326 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1327 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1328 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1329 | char *res; |
| 1330 | size_t r; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1331 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1332 | if (cpath == NULL) { |
| 1333 | errno = EINVAL; |
| 1334 | return NULL; |
| 1335 | } |
| 1336 | res = realpath(cpath, cresolved_path); |
| 1337 | PyMem_Free(cpath); |
| 1338 | if (res == NULL) |
| 1339 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1340 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1341 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1342 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1343 | errno = EINVAL; |
| 1344 | return NULL; |
| 1345 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1346 | if (resolved_path_size <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1347 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1348 | errno = EINVAL; |
| 1349 | return NULL; |
| 1350 | } |
| 1351 | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1352 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1353 | return resolved_path; |
| 1354 | } |
| 1355 | #endif |
| 1356 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1357 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1358 | including the null character. Decode the path from the locale encoding. |
| 1359 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1360 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1361 | wchar_t* |
| 1362 | _Py_wgetcwd(wchar_t *buf, size_t size) |
| 1363 | { |
| 1364 | #ifdef MS_WINDOWS |
Victor Stinner | 56785ea | 2013-06-05 00:46:29 +0200 | [diff] [blame] | 1365 | int isize = (int)Py_MIN(size, INT_MAX); |
| 1366 | return _wgetcwd(buf, isize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1367 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1368 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1369 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1370 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1371 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1372 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1373 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1374 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1375 | if (wname == NULL) |
| 1376 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1377 | if (size <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1378 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1379 | return NULL; |
| 1380 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1381 | wcsncpy(buf, wname, size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1382 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1383 | return buf; |
| 1384 | #endif |
| 1385 | } |
| 1386 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1387 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1388 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1389 | exception and return -1 on error. |
| 1390 | |
| 1391 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1392 | int |
| 1393 | _Py_dup(int fd) |
| 1394 | { |
| 1395 | #ifdef MS_WINDOWS |
| 1396 | HANDLE handle; |
| 1397 | DWORD ftype; |
| 1398 | #endif |
| 1399 | |
| 1400 | if (!_PyVerify_fd(fd)) { |
| 1401 | PyErr_SetFromErrno(PyExc_OSError); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | |
| 1405 | #ifdef MS_WINDOWS |
| 1406 | handle = (HANDLE)_get_osfhandle(fd); |
| 1407 | if (handle == INVALID_HANDLE_VALUE) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1408 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1409 | return -1; |
| 1410 | } |
| 1411 | |
| 1412 | /* get the file type, ignore the error if it failed */ |
| 1413 | ftype = GetFileType(handle); |
| 1414 | |
| 1415 | Py_BEGIN_ALLOW_THREADS |
| 1416 | fd = dup(fd); |
| 1417 | Py_END_ALLOW_THREADS |
| 1418 | if (fd < 0) { |
| 1419 | PyErr_SetFromErrno(PyExc_OSError); |
| 1420 | return -1; |
| 1421 | } |
| 1422 | |
| 1423 | /* Character files like console cannot be make non-inheritable */ |
| 1424 | if (ftype != FILE_TYPE_CHAR) { |
| 1425 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1426 | close(fd); |
| 1427 | return -1; |
| 1428 | } |
| 1429 | } |
| 1430 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1431 | Py_BEGIN_ALLOW_THREADS |
| 1432 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 1433 | Py_END_ALLOW_THREADS |
| 1434 | if (fd < 0) { |
| 1435 | PyErr_SetFromErrno(PyExc_OSError); |
| 1436 | return -1; |
| 1437 | } |
| 1438 | |
| 1439 | #else |
| 1440 | Py_BEGIN_ALLOW_THREADS |
| 1441 | fd = dup(fd); |
| 1442 | Py_END_ALLOW_THREADS |
| 1443 | if (fd < 0) { |
| 1444 | PyErr_SetFromErrno(PyExc_OSError); |
| 1445 | return -1; |
| 1446 | } |
| 1447 | |
| 1448 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1449 | close(fd); |
| 1450 | return -1; |
| 1451 | } |
| 1452 | #endif |
| 1453 | return fd; |
| 1454 | } |
| 1455 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1456 | #ifndef MS_WINDOWS |
| 1457 | /* Get the blocking mode of the file descriptor. |
| 1458 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1459 | raise an exception and return -1 on error. */ |
| 1460 | int |
| 1461 | _Py_get_blocking(int fd) |
| 1462 | { |
| 1463 | int flags = fcntl(fd, F_GETFL, 0); |
| 1464 | if (flags < 0) { |
| 1465 | PyErr_SetFromErrno(PyExc_OSError); |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
| 1469 | return !(flags & O_NONBLOCK); |
| 1470 | } |
| 1471 | |
| 1472 | /* Set the blocking mode of the specified file descriptor. |
| 1473 | |
| 1474 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1475 | otherwise. |
| 1476 | |
| 1477 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1478 | int |
| 1479 | _Py_set_blocking(int fd, int blocking) |
| 1480 | { |
| 1481 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1482 | int arg = !blocking; |
| 1483 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1484 | goto error; |
| 1485 | #else |
| 1486 | int flags, res; |
| 1487 | |
| 1488 | flags = fcntl(fd, F_GETFL, 0); |
| 1489 | if (flags < 0) |
| 1490 | goto error; |
| 1491 | |
| 1492 | if (blocking) |
| 1493 | flags = flags & (~O_NONBLOCK); |
| 1494 | else |
| 1495 | flags = flags | O_NONBLOCK; |
| 1496 | |
| 1497 | res = fcntl(fd, F_SETFL, flags); |
| 1498 | if (res < 0) |
| 1499 | goto error; |
| 1500 | #endif |
| 1501 | return 0; |
| 1502 | |
| 1503 | error: |
| 1504 | PyErr_SetFromErrno(PyExc_OSError); |
| 1505 | return -1; |
| 1506 | } |
| 1507 | #endif |
| 1508 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1509 | #ifdef _MSC_VER |
| 1510 | #if _MSC_VER >= 1900 |
| 1511 | |
| 1512 | /* This function lets the Windows CRT validate the file handle without |
| 1513 | terminating the process if it's invalid. */ |
| 1514 | int |
| 1515 | _PyVerify_fd(int fd) |
| 1516 | { |
| 1517 | intptr_t osh; |
| 1518 | /* Fast check for the only condition we know */ |
| 1519 | if (fd < 0) { |
| 1520 | _set_errno(EBADF); |
| 1521 | return 0; |
| 1522 | } |
| 1523 | osh = _get_osfhandle(fd); |
| 1524 | return osh != (intptr_t)-1; |
| 1525 | } |
| 1526 | |
| 1527 | #elif _MSC_VER >= 1400 |
| 1528 | /* Legacy implementation of _PyVerify_fd while transitioning to |
| 1529 | * MSVC 14.0. This should eventually be removed. (issue23524) |
| 1530 | */ |
| 1531 | |
| 1532 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 1533 | * valid and raise an assertion if it isn't. |
| 1534 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1535 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1536 | * which for write(2) states: |
| 1537 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1538 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1539 | * writing." |
| 1540 | * Furthermore, python allows the user to enter any old integer |
| 1541 | * as a fd and should merely raise a python exception on error. |
| 1542 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1543 | * validity of a file descriptor, but we can emulate its internal behaviour |
| 1544 | * by using the exported __pinfo data member and knowledge of the |
| 1545 | * internal structures involved. |
| 1546 | * The structures below must be updated for each version of visual studio |
| 1547 | * according to the file internal.h in the CRT source, until MS comes |
| 1548 | * up with a less hacky way to do this. |
| 1549 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1550 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1551 | */ |
| 1552 | /* The actual size of the structure is determined at runtime. |
| 1553 | * Only the first items must be present. |
| 1554 | */ |
| 1555 | typedef struct { |
| 1556 | intptr_t osfhnd; |
| 1557 | char osfile; |
| 1558 | } my_ioinfo; |
| 1559 | |
| 1560 | extern __declspec(dllimport) char * __pioinfo[]; |
| 1561 | #define IOINFO_L2E 5 |
| 1562 | #define IOINFO_ARRAYS 64 |
| 1563 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1564 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1565 | #define FOPEN 0x01 |
| 1566 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1567 | |
| 1568 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1569 | int |
| 1570 | _PyVerify_fd(int fd) |
| 1571 | { |
| 1572 | const int i1 = fd >> IOINFO_L2E; |
| 1573 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
| 1574 | |
| 1575 | static size_t sizeof_ioinfo = 0; |
| 1576 | |
| 1577 | /* Determine the actual size of the ioinfo structure, |
| 1578 | * as used by the CRT loaded in memory |
| 1579 | */ |
| 1580 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1581 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1582 | } |
| 1583 | if (sizeof_ioinfo == 0) { |
| 1584 | /* This should not happen... */ |
| 1585 | goto fail; |
| 1586 | } |
| 1587 | |
| 1588 | /* See that it isn't a special CLEAR fileno */ |
| 1589 | if (fd != _NO_CONSOLE_FILENO) { |
| 1590 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1591 | * we check pointer validity and other info |
| 1592 | */ |
| 1593 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1594 | /* finally, check that the file is open */ |
| 1595 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1596 | if (info->osfile & FOPEN) { |
| 1597 | return 1; |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | fail: |
| 1602 | errno = EBADF; |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | #endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */ |
| 1607 | #endif /* defined _MSC_VER */ |