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 | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 522 | |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 523 | #ifdef MS_WINDOWS |
| 524 | static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */ |
| 525 | |
| 526 | static void |
| 527 | FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out) |
| 528 | { |
| 529 | /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */ |
| 530 | /* Cannot simply cast and dereference in_ptr, |
| 531 | since it might not be aligned properly */ |
| 532 | __int64 in; |
| 533 | memcpy(&in, in_ptr, sizeof(in)); |
| 534 | *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */ |
| 535 | *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t); |
| 536 | } |
| 537 | |
| 538 | void |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 539 | _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] | 540 | { |
| 541 | /* XXX endianness */ |
| 542 | __int64 out; |
| 543 | out = time_in + secs_between_epochs; |
| 544 | out = out * 10000000 + nsec_in / 100; |
| 545 | memcpy(out_ptr, &out, sizeof(out)); |
| 546 | } |
| 547 | |
| 548 | /* Below, we *know* that ugo+r is 0444 */ |
| 549 | #if _S_IREAD != 0400 |
| 550 | #error Unsupported C library |
| 551 | #endif |
| 552 | static int |
| 553 | attributes_to_mode(DWORD attr) |
| 554 | { |
| 555 | int m = 0; |
| 556 | if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 557 | m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */ |
| 558 | else |
| 559 | m |= _S_IFREG; |
| 560 | if (attr & FILE_ATTRIBUTE_READONLY) |
| 561 | m |= 0444; |
| 562 | else |
| 563 | m |= 0666; |
| 564 | return m; |
| 565 | } |
| 566 | |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 567 | void |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 568 | _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] | 569 | { |
| 570 | memset(result, 0, sizeof(*result)); |
| 571 | result->st_mode = attributes_to_mode(info->dwFileAttributes); |
| 572 | result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow; |
| 573 | result->st_dev = info->dwVolumeSerialNumber; |
| 574 | result->st_rdev = result->st_dev; |
| 575 | FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec); |
| 576 | FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec); |
| 577 | FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec); |
| 578 | result->st_nlink = info->nNumberOfLinks; |
| 579 | result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow; |
| 580 | if (reparse_tag == IO_REPARSE_TAG_SYMLINK) { |
| 581 | /* first clear the S_IFMT bits */ |
| 582 | result->st_mode ^= (result->st_mode & S_IFMT); |
| 583 | /* now set the bits that make this a symlink */ |
| 584 | result->st_mode |= S_IFLNK; |
| 585 | } |
| 586 | result->st_file_attributes = info->dwFileAttributes; |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 587 | } |
| 588 | #endif |
| 589 | |
| 590 | /* Return information about a file. |
| 591 | |
| 592 | On POSIX, use fstat(). |
| 593 | |
| 594 | On Windows, use GetFileType() and GetFileInformationByHandle() which support |
| 595 | files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger |
| 596 | than 2 GB because the file size type is an signed 32-bit integer: see issue |
| 597 | #23152. |
| 598 | */ |
| 599 | int |
| 600 | _Py_fstat(int fd, struct _Py_stat_struct *result) |
| 601 | { |
| 602 | #ifdef MS_WINDOWS |
| 603 | BY_HANDLE_FILE_INFORMATION info; |
| 604 | HANDLE h; |
| 605 | int type; |
| 606 | |
| 607 | if (!_PyVerify_fd(fd)) |
| 608 | h = INVALID_HANDLE_VALUE; |
| 609 | else |
| 610 | h = (HANDLE)_get_osfhandle(fd); |
| 611 | |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 612 | /* Protocol violation: we explicitly clear errno, instead of |
| 613 | setting it to a POSIX error. Callers should use GetLastError. */ |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 614 | errno = 0; |
| 615 | |
| 616 | if (h == INVALID_HANDLE_VALUE) { |
Steve Dower | 8acde7d | 2015-03-07 18:14:07 -0800 | [diff] [blame] | 617 | /* This is really a C library error (invalid file handle). |
| 618 | We set the Win32 error to the closes one matching. */ |
| 619 | SetLastError(ERROR_INVALID_HANDLE); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 620 | return -1; |
| 621 | } |
| 622 | memset(result, 0, sizeof(*result)); |
| 623 | |
| 624 | type = GetFileType(h); |
| 625 | if (type == FILE_TYPE_UNKNOWN) { |
| 626 | DWORD error = GetLastError(); |
| 627 | if (error != 0) { |
| 628 | return -1; |
| 629 | } |
| 630 | /* else: valid but unknown file */ |
| 631 | } |
| 632 | |
| 633 | if (type != FILE_TYPE_DISK) { |
| 634 | if (type == FILE_TYPE_CHAR) |
| 635 | result->st_mode = _S_IFCHR; |
| 636 | else if (type == FILE_TYPE_PIPE) |
| 637 | result->st_mode = _S_IFIFO; |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | if (!GetFileInformationByHandle(h, &info)) { |
| 642 | return -1; |
| 643 | } |
| 644 | |
Steve Dower | a2af1a5 | 2015-02-21 10:04:10 -0800 | [diff] [blame] | 645 | _Py_attribute_data_to_stat(&info, 0, result); |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 646 | /* specific to fstat() */ |
| 647 | result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; |
| 648 | return 0; |
| 649 | #else |
| 650 | return fstat(fd, result); |
| 651 | #endif |
| 652 | } |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 653 | |
| 654 | |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 655 | /* Call _wstat() on Windows, or encode the path to the filesystem encoding and |
| 656 | call stat() otherwise. Only fill st_mode attribute on Windows. |
| 657 | |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 658 | Return 0 on success, -1 on _wstat() / stat() error, -2 if an exception was |
| 659 | raised. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 660 | |
| 661 | int |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 662 | _Py_stat(PyObject *path, struct stat *statbuf) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 663 | { |
| 664 | #ifdef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 665 | int err; |
| 666 | struct _stat wstatbuf; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 667 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 668 | |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 669 | wpath = PyUnicode_AsUnicode(path); |
| 670 | if (wpath == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 671 | return -2; |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 672 | err = _wstat(wpath, &wstatbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 673 | if (!err) |
| 674 | statbuf->st_mode = wstatbuf.st_mode; |
| 675 | return err; |
| 676 | #else |
| 677 | int ret; |
Victor Stinner | a4a7595 | 2010-10-07 22:23:10 +0000 | [diff] [blame] | 678 | PyObject *bytes = PyUnicode_EncodeFSDefault(path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 679 | if (bytes == NULL) |
Victor Stinner | bd0850b | 2011-12-18 20:47:30 +0100 | [diff] [blame] | 680 | return -2; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 681 | ret = stat(PyBytes_AS_STRING(bytes), statbuf); |
| 682 | Py_DECREF(bytes); |
| 683 | return ret; |
| 684 | #endif |
| 685 | } |
| 686 | |
Victor Stinner | d45c7f8 | 2012-12-04 01:34:47 +0100 | [diff] [blame] | 687 | |
Antoine Pitrou | 409b538 | 2013-10-12 22:41:17 +0200 | [diff] [blame] | 688 | static int |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 689 | get_inheritable(int fd, int raise) |
| 690 | { |
| 691 | #ifdef MS_WINDOWS |
| 692 | HANDLE handle; |
| 693 | DWORD flags; |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 694 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 695 | if (!_PyVerify_fd(fd)) { |
| 696 | if (raise) |
| 697 | PyErr_SetFromErrno(PyExc_OSError); |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | handle = (HANDLE)_get_osfhandle(fd); |
| 702 | if (handle == INVALID_HANDLE_VALUE) { |
| 703 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 704 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 705 | return -1; |
| 706 | } |
| 707 | |
| 708 | if (!GetHandleInformation(handle, &flags)) { |
| 709 | if (raise) |
| 710 | PyErr_SetFromWindowsErr(0); |
| 711 | return -1; |
| 712 | } |
| 713 | |
| 714 | return (flags & HANDLE_FLAG_INHERIT); |
| 715 | #else |
| 716 | int flags; |
| 717 | |
| 718 | flags = fcntl(fd, F_GETFD, 0); |
| 719 | if (flags == -1) { |
| 720 | if (raise) |
| 721 | PyErr_SetFromErrno(PyExc_OSError); |
| 722 | return -1; |
| 723 | } |
| 724 | return !(flags & FD_CLOEXEC); |
| 725 | #endif |
| 726 | } |
| 727 | |
| 728 | /* Get the inheritable flag of the specified file descriptor. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 729 | 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] | 730 | raise an exception and return -1 on error. */ |
| 731 | int |
| 732 | _Py_get_inheritable(int fd) |
| 733 | { |
| 734 | return get_inheritable(fd, 1); |
| 735 | } |
| 736 | |
| 737 | static int |
| 738 | set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) |
| 739 | { |
| 740 | #ifdef MS_WINDOWS |
| 741 | HANDLE handle; |
| 742 | DWORD flags; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 743 | #else |
| 744 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 745 | static int ioctl_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 746 | int request; |
| 747 | int err; |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 748 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 749 | int flags; |
| 750 | int res; |
| 751 | #endif |
| 752 | |
| 753 | /* atomic_flag_works can only be used to make the file descriptor |
| 754 | non-inheritable */ |
| 755 | assert(!(atomic_flag_works != NULL && inheritable)); |
| 756 | |
| 757 | if (atomic_flag_works != NULL && !inheritable) { |
| 758 | if (*atomic_flag_works == -1) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 759 | int isInheritable = get_inheritable(fd, raise); |
| 760 | if (isInheritable == -1) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 761 | return -1; |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 762 | *atomic_flag_works = !isInheritable; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | if (*atomic_flag_works) |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | #ifdef MS_WINDOWS |
| 770 | if (!_PyVerify_fd(fd)) { |
| 771 | if (raise) |
| 772 | PyErr_SetFromErrno(PyExc_OSError); |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | handle = (HANDLE)_get_osfhandle(fd); |
| 777 | if (handle == INVALID_HANDLE_VALUE) { |
| 778 | if (raise) |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 779 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | if (inheritable) |
| 784 | flags = HANDLE_FLAG_INHERIT; |
| 785 | else |
| 786 | flags = 0; |
| 787 | if (!SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags)) { |
| 788 | if (raise) |
| 789 | PyErr_SetFromWindowsErr(0); |
| 790 | return -1; |
| 791 | } |
| 792 | return 0; |
| 793 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 794 | #else |
Victor Stinner | 282124b | 2014-09-02 11:41:04 +0200 | [diff] [blame] | 795 | |
| 796 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) |
| 797 | if (ioctl_works != 0) { |
| 798 | /* fast-path: ioctl() only requires one syscall */ |
| 799 | if (inheritable) |
| 800 | request = FIONCLEX; |
| 801 | else |
| 802 | request = FIOCLEX; |
| 803 | err = ioctl(fd, request, NULL); |
| 804 | if (!err) { |
| 805 | ioctl_works = 1; |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | if (errno != ENOTTY) { |
| 810 | if (raise) |
| 811 | PyErr_SetFromErrno(PyExc_OSError); |
| 812 | return -1; |
| 813 | } |
| 814 | else { |
| 815 | /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for |
| 816 | device". The ioctl is declared but not supported by the kernel. |
| 817 | Remember that ioctl() doesn't work. It is the case on |
| 818 | Illumos-based OS for example. */ |
| 819 | ioctl_works = 0; |
| 820 | } |
| 821 | /* fallback to fcntl() if ioctl() does not work */ |
| 822 | } |
| 823 | #endif |
| 824 | |
| 825 | /* slow-path: fcntl() requires two syscalls */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 826 | flags = fcntl(fd, F_GETFD); |
| 827 | if (flags < 0) { |
| 828 | if (raise) |
| 829 | PyErr_SetFromErrno(PyExc_OSError); |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | if (inheritable) |
| 834 | flags &= ~FD_CLOEXEC; |
| 835 | else |
| 836 | flags |= FD_CLOEXEC; |
| 837 | res = fcntl(fd, F_SETFD, flags); |
| 838 | if (res < 0) { |
| 839 | if (raise) |
| 840 | PyErr_SetFromErrno(PyExc_OSError); |
| 841 | return -1; |
| 842 | } |
| 843 | return 0; |
| 844 | #endif |
| 845 | } |
| 846 | |
| 847 | /* Make the file descriptor non-inheritable. |
Victor Stinner | b034eee | 2013-09-07 10:36:04 +0200 | [diff] [blame] | 848 | Return 0 on success, set errno and return -1 on error. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 849 | static int |
| 850 | make_non_inheritable(int fd) |
| 851 | { |
| 852 | return set_inheritable(fd, 0, 0, NULL); |
| 853 | } |
| 854 | |
| 855 | /* Set the inheritable flag of the specified file descriptor. |
| 856 | On success: return 0, on error: raise an exception if raise is nonzero |
| 857 | and return -1. |
| 858 | |
| 859 | If atomic_flag_works is not NULL: |
| 860 | |
| 861 | * if *atomic_flag_works==-1, check if the inheritable is set on the file |
| 862 | descriptor: if yes, set *atomic_flag_works to 1, otherwise set to 0 and |
| 863 | set the inheritable flag |
| 864 | * if *atomic_flag_works==1: do nothing |
| 865 | * if *atomic_flag_works==0: set inheritable flag to False |
| 866 | |
| 867 | Set atomic_flag_works to NULL if no atomic flag was used to create the |
| 868 | file descriptor. |
| 869 | |
| 870 | atomic_flag_works can only be used to make a file descriptor |
| 871 | non-inheritable: atomic_flag_works must be NULL if inheritable=1. */ |
| 872 | int |
| 873 | _Py_set_inheritable(int fd, int inheritable, int *atomic_flag_works) |
| 874 | { |
| 875 | return set_inheritable(fd, inheritable, 1, atomic_flag_works); |
| 876 | } |
| 877 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 878 | static int |
| 879 | _Py_open_impl(const char *pathname, int flags, int gil_held) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 880 | { |
| 881 | int fd; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 882 | int async_err = 0; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 883 | #ifndef MS_WINDOWS |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 884 | int *atomic_flag_works; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 885 | #endif |
| 886 | |
| 887 | #ifdef MS_WINDOWS |
| 888 | flags |= O_NOINHERIT; |
| 889 | #elif defined(O_CLOEXEC) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 890 | atomic_flag_works = &_Py_open_cloexec_works; |
| 891 | flags |= O_CLOEXEC; |
| 892 | #else |
| 893 | atomic_flag_works = NULL; |
| 894 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 895 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 896 | if (gil_held) { |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 897 | do { |
| 898 | Py_BEGIN_ALLOW_THREADS |
| 899 | fd = open(pathname, flags); |
| 900 | Py_END_ALLOW_THREADS |
| 901 | } while (fd < 0 |
| 902 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 903 | if (async_err) |
| 904 | return -1; |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 905 | if (fd < 0) { |
| 906 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, pathname); |
| 907 | return -1; |
| 908 | } |
| 909 | } |
| 910 | else { |
| 911 | fd = open(pathname, flags); |
| 912 | if (fd < 0) |
| 913 | return -1; |
| 914 | } |
| 915 | |
| 916 | #ifndef MS_WINDOWS |
| 917 | if (set_inheritable(fd, 0, gil_held, atomic_flag_works) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 918 | close(fd); |
| 919 | return -1; |
| 920 | } |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 921 | #endif |
| 922 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 923 | return fd; |
| 924 | } |
| 925 | |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 926 | /* Open a file with the specified flags (wrapper to open() function). |
| 927 | Return a file descriptor on success. Raise an exception and return -1 on |
| 928 | error. |
| 929 | |
| 930 | The file descriptor is created non-inheritable. |
| 931 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 932 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 933 | except if the Python signal handler raises an exception. |
| 934 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 935 | The GIL must be held. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 936 | int |
| 937 | _Py_open(const char *pathname, int flags) |
| 938 | { |
| 939 | /* _Py_open() must be called with the GIL held. */ |
| 940 | assert(PyGILState_Check()); |
| 941 | return _Py_open_impl(pathname, flags, 1); |
| 942 | } |
| 943 | |
| 944 | /* Open a file with the specified flags (wrapper to open() function). |
| 945 | Return a file descriptor on success. Set errno and return -1 on error. |
| 946 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 947 | The file descriptor is created non-inheritable. |
| 948 | |
| 949 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 950 | int |
| 951 | _Py_open_noraise(const char *pathname, int flags) |
| 952 | { |
| 953 | return _Py_open_impl(pathname, flags, 0); |
| 954 | } |
| 955 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 956 | /* 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] | 957 | encoding and use fopen() otherwise. |
| 958 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 959 | The file descriptor is created non-inheritable. |
| 960 | |
| 961 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 962 | FILE * |
| 963 | _Py_wfopen(const wchar_t *path, const wchar_t *mode) |
| 964 | { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 965 | FILE *f; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 966 | #ifndef MS_WINDOWS |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 967 | char *cpath; |
| 968 | char cmode[10]; |
| 969 | size_t r; |
| 970 | r = wcstombs(cmode, mode, 10); |
| 971 | if (r == (size_t)-1 || r >= 10) { |
| 972 | errno = EINVAL; |
| 973 | return NULL; |
| 974 | } |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 975 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 976 | if (cpath == NULL) |
| 977 | return NULL; |
| 978 | f = fopen(cpath, cmode); |
| 979 | PyMem_Free(cpath); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 980 | #else |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 981 | f = _wfopen(path, mode); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 982 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 983 | if (f == NULL) |
| 984 | return NULL; |
| 985 | if (make_non_inheritable(fileno(f)) < 0) { |
| 986 | fclose(f); |
| 987 | return NULL; |
| 988 | } |
| 989 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 992 | /* Wrapper to fopen(). |
| 993 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 994 | The file descriptor is created non-inheritable. |
| 995 | |
| 996 | If interrupted by a signal, fail with EINTR. */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 997 | FILE* |
| 998 | _Py_fopen(const char *pathname, const char *mode) |
| 999 | { |
| 1000 | FILE *f = fopen(pathname, mode); |
| 1001 | if (f == NULL) |
| 1002 | return NULL; |
| 1003 | if (make_non_inheritable(fileno(f)) < 0) { |
| 1004 | fclose(f); |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | return f; |
| 1008 | } |
| 1009 | |
| 1010 | /* 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] | 1011 | encoding and call fopen() otherwise. |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1012 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1013 | Return the new file object on success. Raise an exception and return NULL |
| 1014 | on error. |
| 1015 | |
| 1016 | The file descriptor is created non-inheritable. |
| 1017 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1018 | When interrupted by a signal (open() fails with EINTR), retry the syscall, |
| 1019 | except if the Python signal handler raises an exception. |
| 1020 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1021 | The GIL must be held. */ |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1022 | FILE* |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1023 | _Py_fopen_obj(PyObject *path, const char *mode) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1024 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1025 | FILE *f; |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1026 | int async_err = 0; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1027 | #ifdef MS_WINDOWS |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1028 | wchar_t *wpath; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1029 | wchar_t wmode[10]; |
| 1030 | int usize; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1031 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1032 | assert(PyGILState_Check()); |
| 1033 | |
Antoine Pitrou | 0e576f1 | 2011-12-22 10:03:38 +0100 | [diff] [blame] | 1034 | if (!PyUnicode_Check(path)) { |
| 1035 | PyErr_Format(PyExc_TypeError, |
| 1036 | "str file path expected under Windows, got %R", |
| 1037 | Py_TYPE(path)); |
| 1038 | return NULL; |
| 1039 | } |
Victor Stinner | ee587ea | 2011-11-17 00:51:38 +0100 | [diff] [blame] | 1040 | wpath = PyUnicode_AsUnicode(path); |
| 1041 | if (wpath == NULL) |
| 1042 | return NULL; |
| 1043 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1044 | usize = MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, sizeof(wmode)); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1045 | if (usize == 0) { |
| 1046 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1047 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1048 | } |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1049 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1050 | do { |
| 1051 | Py_BEGIN_ALLOW_THREADS |
| 1052 | f = _wfopen(wpath, wmode); |
| 1053 | Py_END_ALLOW_THREADS |
| 1054 | } while (f == NULL |
| 1055 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1056 | #else |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1057 | PyObject *bytes; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1058 | char *path_bytes; |
| 1059 | |
| 1060 | assert(PyGILState_Check()); |
| 1061 | |
Antoine Pitrou | 2b1cc89 | 2011-12-19 18:19:06 +0100 | [diff] [blame] | 1062 | if (!PyUnicode_FSConverter(path, &bytes)) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1063 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1064 | path_bytes = PyBytes_AS_STRING(bytes); |
| 1065 | |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1066 | do { |
| 1067 | Py_BEGIN_ALLOW_THREADS |
| 1068 | f = fopen(path_bytes, mode); |
| 1069 | Py_END_ALLOW_THREADS |
| 1070 | } while (f == NULL |
| 1071 | && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1072 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1073 | Py_DECREF(bytes); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1074 | #endif |
Victor Stinner | a47fc5c | 2015-03-18 09:52:54 +0100 | [diff] [blame] | 1075 | if (async_err) |
| 1076 | return NULL; |
| 1077 | |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1078 | if (f == NULL) { |
| 1079 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1080 | return NULL; |
Victor Stinner | e42ccd2 | 2015-03-18 01:39:23 +0100 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | if (set_inheritable(fileno(f), 0, 1, NULL) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1084 | fclose(f); |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | return f; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1090 | /* Read count bytes from fd into buf. |
| 1091 | * |
| 1092 | * On success, return the number of read bytes, it can be lower than count. |
| 1093 | * If the current file offset is at or past the end of file, no bytes are read, |
| 1094 | * and read() returns zero. |
| 1095 | * |
| 1096 | * On error, raise an exception, set errno and return -1. |
| 1097 | * |
| 1098 | * When interrupted by a signal (read() fails with EINTR), retry the syscall. |
| 1099 | * If the Python signal handler raises an exception, the function returns -1 |
| 1100 | * (the syscall is not retried). |
| 1101 | * |
| 1102 | * The GIL must be held. */ |
| 1103 | Py_ssize_t |
| 1104 | _Py_read(int fd, void *buf, size_t count) |
| 1105 | { |
| 1106 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1107 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1108 | int async_err = 0; |
| 1109 | |
| 1110 | /* _Py_read() must not be called with an exception set, otherwise the |
| 1111 | * caller may think that read() was interrupted by a signal and the signal |
| 1112 | * handler raised an exception. */ |
| 1113 | assert(!PyErr_Occurred()); |
| 1114 | |
Victor Stinner | c1cf4f7 | 2015-03-19 23:53:04 +0100 | [diff] [blame] | 1115 | if (!_PyVerify_fd(fd)) { |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1116 | /* save/restore errno because PyErr_SetFromErrno() can modify it */ |
| 1117 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1118 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1119 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1120 | return -1; |
| 1121 | } |
| 1122 | |
| 1123 | #ifdef MS_WINDOWS |
| 1124 | if (count > INT_MAX) { |
| 1125 | /* On Windows, the count parameter of read() is an int */ |
| 1126 | count = INT_MAX; |
| 1127 | } |
| 1128 | #else |
| 1129 | if (count > PY_SSIZE_T_MAX) { |
| 1130 | /* if count is greater than PY_SSIZE_T_MAX, |
| 1131 | * read() result is undefined */ |
| 1132 | count = PY_SSIZE_T_MAX; |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
| 1136 | do { |
| 1137 | Py_BEGIN_ALLOW_THREADS |
| 1138 | errno = 0; |
| 1139 | #ifdef MS_WINDOWS |
| 1140 | n = read(fd, buf, (int)count); |
| 1141 | #else |
| 1142 | n = read(fd, buf, count); |
| 1143 | #endif |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1144 | /* save/restore errno because PyErr_CheckSignals() |
| 1145 | * and PyErr_SetFromErrno() can modify it */ |
| 1146 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1147 | Py_END_ALLOW_THREADS |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1148 | } while (n < 0 && err == EINTR && |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1149 | !(async_err = PyErr_CheckSignals())); |
| 1150 | |
| 1151 | if (async_err) { |
| 1152 | /* read() was interrupted by a signal (failed with EINTR) |
| 1153 | * and the Python signal handler raised an exception */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1154 | errno = err; |
| 1155 | assert(errno == EINTR && PyErr_Occurred()); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1156 | return -1; |
| 1157 | } |
| 1158 | if (n < 0) { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1159 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1160 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
| 1163 | |
| 1164 | return n; |
| 1165 | } |
| 1166 | |
| 1167 | /* Write count bytes of buf into fd. |
| 1168 | * |
| 1169 | * -On success, return the number of written bytes, it can be lower than count |
| 1170 | * including 0 |
| 1171 | * - On error, raise an exception, set errno and return -1. |
| 1172 | * |
| 1173 | * When interrupted by a signal (write() fails with EINTR), retry the syscall. |
| 1174 | * If the Python signal handler raises an exception, the function returns -1 |
| 1175 | * (the syscall is not retried). |
| 1176 | * |
| 1177 | * The GIL must be held. */ |
| 1178 | Py_ssize_t |
| 1179 | _Py_write(int fd, const void *buf, size_t count) |
| 1180 | { |
| 1181 | Py_ssize_t n; |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1182 | int err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1183 | int async_err = 0; |
| 1184 | |
| 1185 | /* _Py_write() must not be called with an exception set, otherwise the |
| 1186 | * caller may think that write() was interrupted by a signal and the signal |
| 1187 | * handler raised an exception. */ |
| 1188 | assert(!PyErr_Occurred()); |
| 1189 | |
| 1190 | if (!_PyVerify_fd(fd)) { |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1191 | /* save/restore errno because PyErr_SetFromErrno() can modify it */ |
| 1192 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1193 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1194 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1195 | return -1; |
| 1196 | } |
| 1197 | |
| 1198 | #ifdef MS_WINDOWS |
| 1199 | if (count > 32767 && isatty(fd)) { |
| 1200 | /* Issue #11395: the Windows console returns an error (12: not |
| 1201 | enough space error) on writing into stdout if stdout mode is |
| 1202 | binary and the length is greater than 66,000 bytes (or less, |
| 1203 | depending on heap usage). */ |
| 1204 | count = 32767; |
| 1205 | } |
| 1206 | else if (count > INT_MAX) |
| 1207 | count = INT_MAX; |
| 1208 | #else |
| 1209 | if (count > PY_SSIZE_T_MAX) { |
| 1210 | /* write() should truncate count to PY_SSIZE_T_MAX, but it's safer |
| 1211 | * to do it ourself to have a portable behaviour. */ |
| 1212 | count = PY_SSIZE_T_MAX; |
| 1213 | } |
| 1214 | #endif |
| 1215 | |
| 1216 | do { |
| 1217 | Py_BEGIN_ALLOW_THREADS |
| 1218 | errno = 0; |
| 1219 | #ifdef MS_WINDOWS |
| 1220 | n = write(fd, buf, (int)count); |
| 1221 | #else |
| 1222 | n = write(fd, buf, count); |
| 1223 | #endif |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1224 | /* save/restore errno because PyErr_CheckSignals() |
| 1225 | * and PyErr_SetFromErrno() can modify it */ |
| 1226 | err = errno; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1227 | Py_END_ALLOW_THREADS |
| 1228 | } while (n < 0 && errno == EINTR && |
| 1229 | !(async_err = PyErr_CheckSignals())); |
| 1230 | |
| 1231 | if (async_err) { |
| 1232 | /* write() was interrupted by a signal (failed with EINTR) |
| 1233 | * and the Python signal handler raised an exception */ |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1234 | errno = err; |
| 1235 | assert(errno == EINTR && PyErr_Occurred()); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1236 | return -1; |
| 1237 | } |
| 1238 | if (n < 0) { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1239 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | a3c0202 | 2015-03-20 11:58:18 +0100 | [diff] [blame] | 1240 | errno = err; |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 1241 | return -1; |
| 1242 | } |
| 1243 | |
| 1244 | return n; |
| 1245 | } |
| 1246 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1247 | #ifdef HAVE_READLINK |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1248 | |
| 1249 | /* 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] | 1250 | the result from the locale encoding. Return -1 on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1251 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1252 | int |
| 1253 | _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz) |
| 1254 | { |
| 1255 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1256 | char cbuf[MAXPATHLEN]; |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1257 | wchar_t *wbuf; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1258 | int res; |
| 1259 | size_t r1; |
| 1260 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1261 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1262 | if (cpath == NULL) { |
| 1263 | errno = EINVAL; |
| 1264 | return -1; |
| 1265 | } |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1266 | res = (int)readlink(cpath, cbuf, Py_ARRAY_LENGTH(cbuf)); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1267 | PyMem_Free(cpath); |
| 1268 | if (res == -1) |
| 1269 | return -1; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1270 | if (res == Py_ARRAY_LENGTH(cbuf)) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1271 | errno = EINVAL; |
| 1272 | return -1; |
| 1273 | } |
| 1274 | cbuf[res] = '\0'; /* buf will be null terminated */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1275 | wbuf = Py_DecodeLocale(cbuf, &r1); |
Victor Stinner | 350147b | 2010-10-16 22:52:09 +0000 | [diff] [blame] | 1276 | if (wbuf == NULL) { |
| 1277 | errno = EINVAL; |
| 1278 | return -1; |
| 1279 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1280 | if (bufsiz <= r1) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1281 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1282 | errno = EINVAL; |
| 1283 | return -1; |
| 1284 | } |
Victor Stinner | 3f711f4 | 2010-10-16 22:47:37 +0000 | [diff] [blame] | 1285 | wcsncpy(buf, wbuf, bufsiz); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1286 | PyMem_RawFree(wbuf); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1287 | return (int)r1; |
| 1288 | } |
| 1289 | #endif |
| 1290 | |
| 1291 | #ifdef HAVE_REALPATH |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* Return the canonicalized absolute pathname. Encode path to the locale |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1294 | encoding, decode the result from the locale encoding. |
| 1295 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1296 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1297 | wchar_t* |
Victor Stinner | 015f4d8 | 2010-10-07 22:29:53 +0000 | [diff] [blame] | 1298 | _Py_wrealpath(const wchar_t *path, |
| 1299 | wchar_t *resolved_path, size_t resolved_path_size) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1300 | { |
| 1301 | char *cpath; |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1302 | char cresolved_path[MAXPATHLEN]; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1303 | wchar_t *wresolved_path; |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1304 | char *res; |
| 1305 | size_t r; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1306 | cpath = Py_EncodeLocale(path, NULL); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1307 | if (cpath == NULL) { |
| 1308 | errno = EINVAL; |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | res = realpath(cpath, cresolved_path); |
| 1312 | PyMem_Free(cpath); |
| 1313 | if (res == NULL) |
| 1314 | return NULL; |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1315 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1316 | wresolved_path = Py_DecodeLocale(cresolved_path, &r); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1317 | if (wresolved_path == NULL) { |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1318 | errno = EINVAL; |
| 1319 | return NULL; |
| 1320 | } |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1321 | if (resolved_path_size <= r) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1322 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 0a1b8cb | 2010-10-16 22:55:47 +0000 | [diff] [blame] | 1323 | errno = EINVAL; |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | wcsncpy(resolved_path, wresolved_path, resolved_path_size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1327 | PyMem_RawFree(wresolved_path); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1328 | return resolved_path; |
| 1329 | } |
| 1330 | #endif |
| 1331 | |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1332 | /* Get the current directory. size is the buffer size in wide characters |
Victor Stinner | af02e1c | 2011-12-16 23:56:01 +0100 | [diff] [blame] | 1333 | including the null character. Decode the path from the locale encoding. |
| 1334 | Return NULL on error. */ |
Victor Stinner | 6672d0c | 2010-10-07 22:53:43 +0000 | [diff] [blame] | 1335 | |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1336 | wchar_t* |
| 1337 | _Py_wgetcwd(wchar_t *buf, size_t size) |
| 1338 | { |
| 1339 | #ifdef MS_WINDOWS |
Victor Stinner | 56785ea | 2013-06-05 00:46:29 +0200 | [diff] [blame] | 1340 | int isize = (int)Py_MIN(size, INT_MAX); |
| 1341 | return _wgetcwd(buf, isize); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1342 | #else |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1343 | char fname[MAXPATHLEN]; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1344 | wchar_t *wname; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1345 | size_t len; |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1346 | |
Victor Stinner | b11d6cb | 2013-11-15 18:14:11 +0100 | [diff] [blame] | 1347 | if (getcwd(fname, Py_ARRAY_LENGTH(fname)) == NULL) |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1348 | return NULL; |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 1349 | wname = Py_DecodeLocale(fname, &len); |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1350 | if (wname == NULL) |
| 1351 | return NULL; |
Victor Stinner | 168e117 | 2010-10-16 23:16:16 +0000 | [diff] [blame] | 1352 | if (size <= len) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1353 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1354 | return NULL; |
| 1355 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 1356 | wcsncpy(buf, wname, size); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 1357 | PyMem_RawFree(wname); |
Victor Stinner | 4e31443 | 2010-10-07 21:45:39 +0000 | [diff] [blame] | 1358 | return buf; |
| 1359 | #endif |
| 1360 | } |
| 1361 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1362 | /* Duplicate a file descriptor. The new file descriptor is created as |
| 1363 | non-inheritable. Return a new file descriptor on success, raise an OSError |
| 1364 | exception and return -1 on error. |
| 1365 | |
| 1366 | The GIL is released to call dup(). The caller must hold the GIL. */ |
| 1367 | int |
| 1368 | _Py_dup(int fd) |
| 1369 | { |
| 1370 | #ifdef MS_WINDOWS |
| 1371 | HANDLE handle; |
| 1372 | DWORD ftype; |
| 1373 | #endif |
| 1374 | |
| 1375 | if (!_PyVerify_fd(fd)) { |
| 1376 | PyErr_SetFromErrno(PyExc_OSError); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
| 1380 | #ifdef MS_WINDOWS |
| 1381 | handle = (HANDLE)_get_osfhandle(fd); |
| 1382 | if (handle == INVALID_HANDLE_VALUE) { |
Steve Dower | 41e7244 | 2015-03-14 11:38:27 -0700 | [diff] [blame] | 1383 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1384 | return -1; |
| 1385 | } |
| 1386 | |
| 1387 | /* get the file type, ignore the error if it failed */ |
| 1388 | ftype = GetFileType(handle); |
| 1389 | |
| 1390 | Py_BEGIN_ALLOW_THREADS |
| 1391 | fd = dup(fd); |
| 1392 | Py_END_ALLOW_THREADS |
| 1393 | if (fd < 0) { |
| 1394 | PyErr_SetFromErrno(PyExc_OSError); |
| 1395 | return -1; |
| 1396 | } |
| 1397 | |
| 1398 | /* Character files like console cannot be make non-inheritable */ |
| 1399 | if (ftype != FILE_TYPE_CHAR) { |
| 1400 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1401 | close(fd); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | } |
| 1405 | #elif defined(HAVE_FCNTL_H) && defined(F_DUPFD_CLOEXEC) |
| 1406 | Py_BEGIN_ALLOW_THREADS |
| 1407 | fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 1408 | Py_END_ALLOW_THREADS |
| 1409 | if (fd < 0) { |
| 1410 | PyErr_SetFromErrno(PyExc_OSError); |
| 1411 | return -1; |
| 1412 | } |
| 1413 | |
| 1414 | #else |
| 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 | if (_Py_set_inheritable(fd, 0, NULL) < 0) { |
| 1424 | close(fd); |
| 1425 | return -1; |
| 1426 | } |
| 1427 | #endif |
| 1428 | return fd; |
| 1429 | } |
| 1430 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 1431 | #ifndef MS_WINDOWS |
| 1432 | /* Get the blocking mode of the file descriptor. |
| 1433 | Return 0 if the O_NONBLOCK flag is set, 1 if the flag is cleared, |
| 1434 | raise an exception and return -1 on error. */ |
| 1435 | int |
| 1436 | _Py_get_blocking(int fd) |
| 1437 | { |
| 1438 | int flags = fcntl(fd, F_GETFL, 0); |
| 1439 | if (flags < 0) { |
| 1440 | PyErr_SetFromErrno(PyExc_OSError); |
| 1441 | return -1; |
| 1442 | } |
| 1443 | |
| 1444 | return !(flags & O_NONBLOCK); |
| 1445 | } |
| 1446 | |
| 1447 | /* Set the blocking mode of the specified file descriptor. |
| 1448 | |
| 1449 | Set the O_NONBLOCK flag if blocking is False, clear the O_NONBLOCK flag |
| 1450 | otherwise. |
| 1451 | |
| 1452 | Return 0 on success, raise an exception and return -1 on error. */ |
| 1453 | int |
| 1454 | _Py_set_blocking(int fd, int blocking) |
| 1455 | { |
| 1456 | #if defined(HAVE_SYS_IOCTL_H) && defined(FIONBIO) |
| 1457 | int arg = !blocking; |
| 1458 | if (ioctl(fd, FIONBIO, &arg) < 0) |
| 1459 | goto error; |
| 1460 | #else |
| 1461 | int flags, res; |
| 1462 | |
| 1463 | flags = fcntl(fd, F_GETFL, 0); |
| 1464 | if (flags < 0) |
| 1465 | goto error; |
| 1466 | |
| 1467 | if (blocking) |
| 1468 | flags = flags & (~O_NONBLOCK); |
| 1469 | else |
| 1470 | flags = flags | O_NONBLOCK; |
| 1471 | |
| 1472 | res = fcntl(fd, F_SETFL, flags); |
| 1473 | if (res < 0) |
| 1474 | goto error; |
| 1475 | #endif |
| 1476 | return 0; |
| 1477 | |
| 1478 | error: |
| 1479 | PyErr_SetFromErrno(PyExc_OSError); |
| 1480 | return -1; |
| 1481 | } |
| 1482 | #endif |
| 1483 | |
Steve Dower | d81431f | 2015-03-06 14:47:02 -0800 | [diff] [blame] | 1484 | #ifdef _MSC_VER |
| 1485 | #if _MSC_VER >= 1900 |
| 1486 | |
| 1487 | /* This function lets the Windows CRT validate the file handle without |
| 1488 | terminating the process if it's invalid. */ |
| 1489 | int |
| 1490 | _PyVerify_fd(int fd) |
| 1491 | { |
| 1492 | intptr_t osh; |
| 1493 | /* Fast check for the only condition we know */ |
| 1494 | if (fd < 0) { |
| 1495 | _set_errno(EBADF); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | osh = _get_osfhandle(fd); |
| 1499 | return osh != (intptr_t)-1; |
| 1500 | } |
| 1501 | |
| 1502 | #elif _MSC_VER >= 1400 |
| 1503 | /* Legacy implementation of _PyVerify_fd while transitioning to |
| 1504 | * MSVC 14.0. This should eventually be removed. (issue23524) |
| 1505 | */ |
| 1506 | |
| 1507 | /* Microsoft CRT in VS2005 and higher will verify that a filehandle is |
| 1508 | * valid and raise an assertion if it isn't. |
| 1509 | * Normally, an invalid fd is likely to be a C program error and therefore |
| 1510 | * an assertion can be useful, but it does contradict the POSIX standard |
| 1511 | * which for write(2) states: |
| 1512 | * "Otherwise, -1 shall be returned and errno set to indicate the error." |
| 1513 | * "[EBADF] The fildes argument is not a valid file descriptor open for |
| 1514 | * writing." |
| 1515 | * Furthermore, python allows the user to enter any old integer |
| 1516 | * as a fd and should merely raise a python exception on error. |
| 1517 | * The Microsoft CRT doesn't provide an official way to check for the |
| 1518 | * validity of a file descriptor, but we can emulate its internal behaviour |
| 1519 | * by using the exported __pinfo data member and knowledge of the |
| 1520 | * internal structures involved. |
| 1521 | * The structures below must be updated for each version of visual studio |
| 1522 | * according to the file internal.h in the CRT source, until MS comes |
| 1523 | * up with a less hacky way to do this. |
| 1524 | * (all of this is to avoid globally modifying the CRT behaviour using |
| 1525 | * _set_invalid_parameter_handler() and _CrtSetReportMode()) |
| 1526 | */ |
| 1527 | /* The actual size of the structure is determined at runtime. |
| 1528 | * Only the first items must be present. |
| 1529 | */ |
| 1530 | typedef struct { |
| 1531 | intptr_t osfhnd; |
| 1532 | char osfile; |
| 1533 | } my_ioinfo; |
| 1534 | |
| 1535 | extern __declspec(dllimport) char * __pioinfo[]; |
| 1536 | #define IOINFO_L2E 5 |
| 1537 | #define IOINFO_ARRAYS 64 |
| 1538 | #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E) |
| 1539 | #define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS) |
| 1540 | #define FOPEN 0x01 |
| 1541 | #define _NO_CONSOLE_FILENO (intptr_t)-2 |
| 1542 | |
| 1543 | /* This function emulates what the windows CRT does to validate file handles */ |
| 1544 | int |
| 1545 | _PyVerify_fd(int fd) |
| 1546 | { |
| 1547 | const int i1 = fd >> IOINFO_L2E; |
| 1548 | const int i2 = fd & ((1 << IOINFO_L2E) - 1); |
| 1549 | |
| 1550 | static size_t sizeof_ioinfo = 0; |
| 1551 | |
| 1552 | /* Determine the actual size of the ioinfo structure, |
| 1553 | * as used by the CRT loaded in memory |
| 1554 | */ |
| 1555 | if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) { |
| 1556 | sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS; |
| 1557 | } |
| 1558 | if (sizeof_ioinfo == 0) { |
| 1559 | /* This should not happen... */ |
| 1560 | goto fail; |
| 1561 | } |
| 1562 | |
| 1563 | /* See that it isn't a special CLEAR fileno */ |
| 1564 | if (fd != _NO_CONSOLE_FILENO) { |
| 1565 | /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead |
| 1566 | * we check pointer validity and other info |
| 1567 | */ |
| 1568 | if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) { |
| 1569 | /* finally, check that the file is open */ |
| 1570 | my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo); |
| 1571 | if (info->osfile & FOPEN) { |
| 1572 | return 1; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | fail: |
| 1577 | errno = EBADF; |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | #endif /* _MSC_VER >= 1900 || _MSC_VER >= 1400 */ |
| 1582 | #endif /* defined _MSC_VER */ |