bpo-42780: Fix set_inheritable() for O_PATH file descriptors on Linux (GH-24172)

This commit is contained in:
cptpcrd 2021-01-20 09:05:51 -05:00 committed by GitHub
parent e0e398ef18
commit 7dc71c425c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View file

@ -3881,6 +3881,33 @@ class FDInheritanceTests(unittest.TestCase):
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
0)
@unittest.skipUnless(hasattr(os, 'O_PATH'), "need os.O_PATH")
def test_get_set_inheritable_o_path(self):
fd = os.open(__file__, os.O_PATH)
self.addCleanup(os.close, fd)
self.assertEqual(os.get_inheritable(fd), False)
os.set_inheritable(fd, True)
self.assertEqual(os.get_inheritable(fd), True)
os.set_inheritable(fd, False)
self.assertEqual(os.get_inheritable(fd), False)
def test_get_set_inheritable_badf(self):
fd = os_helper.make_bad_fd()
with self.assertRaises(OSError) as ctx:
os.get_inheritable(fd)
self.assertEqual(ctx.exception.errno, errno.EBADF)
with self.assertRaises(OSError) as ctx:
os.set_inheritable(fd, True)
self.assertEqual(ctx.exception.errno, errno.EBADF)
with self.assertRaises(OSError) as ctx:
os.set_inheritable(fd, False)
self.assertEqual(ctx.exception.errno, errno.EBADF)
def test_open(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)

View file

@ -0,0 +1 @@
Fix os.set_inheritable() for O_PATH file descriptors on Linux.

View file

@ -1234,6 +1234,13 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
return 0;
}
#ifdef __linux__
if (errno == EBADF) {
// On Linux, ioctl(FIOCLEX) will fail with EBADF for O_PATH file descriptors
// Fall through to the fcntl() path
}
else
#endif
if (errno != ENOTTY && errno != EACCES) {
if (raise)
PyErr_SetFromErrno(PyExc_OSError);