diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-09-02 09:49:48 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-09-02 09:49:48 (GMT) |
commit | 8257b6283e3ea41f5746835871dedcb00139bdfe (patch) | |
tree | ac4faa143dee7dbe08579bb12ec25fb17f8017c7 /Python | |
parent | 7809569a2ea1a9d330ad7a5949bc43f835c1a276 (diff) | |
parent | 282124b8c4a277d634435c3a2fb2353abd567c31 (diff) | |
download | cpython-8257b6283e3ea41f5746835871dedcb00139bdfe.zip cpython-8257b6283e3ea41f5746835871dedcb00139bdfe.tar.gz cpython-8257b6283e3ea41f5746835871dedcb00139bdfe.tar.bz2 |
(Merge 3.4) Closes #22258: Fix the the internal function set_inheritable() on
Illumos. This platform exposes the function ioctl(FIOCLEX), but calling it
fails with errno is ENOTTY: "Inappropriate ioctl for device". set_inheritable()
now falls back to the slower fcntl() (F_GETFD and then F_SETFD).
Diffstat (limited to 'Python')
-rw-r--r-- | Python/fileutils.c | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c index 227e92a..0c76e72 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -625,10 +625,12 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) #ifdef MS_WINDOWS HANDLE handle; DWORD flags; -#elif defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) +#else +#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) + static int ioctl_works = -1; int request; int err; -#elif defined(HAVE_FCNTL_H) +#endif int flags; int res; #endif @@ -674,20 +676,38 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works) } return 0; -#elif defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) - if (inheritable) - request = FIONCLEX; - else - request = FIOCLEX; - err = ioctl(fd, request, NULL); - if (err) { - if (raise) - PyErr_SetFromErrno(PyExc_OSError); - return -1; +#else + +#if defined(HAVE_SYS_IOCTL_H) && defined(FIOCLEX) && defined(FIONCLEX) + if (ioctl_works != 0) { + /* fast-path: ioctl() only requires one syscall */ + if (inheritable) + request = FIONCLEX; + else + request = FIOCLEX; + err = ioctl(fd, request, NULL); + if (!err) { + ioctl_works = 1; + return 0; + } + + if (errno != ENOTTY) { + if (raise) + PyErr_SetFromErrno(PyExc_OSError); + return -1; + } + else { + /* Issue #22258: Here, ENOTTY means "Inappropriate ioctl for + device". The ioctl is declared but not supported by the kernel. + Remember that ioctl() doesn't work. It is the case on + Illumos-based OS for example. */ + ioctl_works = 0; + } + /* fallback to fcntl() if ioctl() does not work */ } - return 0; +#endif -#else + /* slow-path: fcntl() requires two syscalls */ flags = fcntl(fd, F_GETFD); if (flags < 0) { if (raise) |