Fix os.set_inheritable() on Android


Issue #27057: Fix os.set_inheritable() on Android, ioctl() is blocked by
SELinux and fails with EACCESS. The function now falls back to fcntl().

Patch written by Michał Bednarski.
diff --git a/Misc/ACKS b/Misc/ACKS
index dda42ca..810036e 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -110,6 +110,7 @@
 Robin Becker
 Torsten Becker
 Bill Bedford
+Michał Bednarski
 Ian Beer
 Stefan Behnel
 Reimer Behrends
diff --git a/Misc/NEWS b/Misc/NEWS
index 8d91d78..fcfcd4f 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,10 @@
 Library
 -------
 
+- Issue #27057: Fix os.set_inheritable() on Android, ioctl() is blocked by
+  SELinux and fails with EACCESS. The function now falls back to fcntl().
+  Patch written by Michał Bednarski.
+
 - Issue #27014: Fix infinite recursion using typing.py.  Thanks to Kalle Tuure!
 
 - Issue #14132: Fix urllib.request redirect handling when the target only has
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 06d632a..8987ce5 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -856,7 +856,7 @@
             return 0;
         }
 
-        if (errno != ENOTTY) {
+        if (errno != ENOTTY && errno != EACCES) {
             if (raise)
                 PyErr_SetFromErrno(PyExc_OSError);
             return -1;
@@ -865,7 +865,12 @@
             /* 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. */
+               Illumos-based OS for example.
+
+               Issue #27057: When SELinux policy disallows ioctl it will fail
+               with EACCES. While FIOCLEX is safe operation it may be
+               unavailable because ioctl was denied altogether.
+               This can be the case on Android. */
             ioctl_works = 0;
         }
         /* fallback to fcntl() if ioctl() does not work */