mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
bpo-42780: Fix set_inheritable() for O_PATH file descriptors on Linux (GH-24172)
This commit is contained in:
parent
e0e398ef18
commit
7dc71c425c
3 changed files with 35 additions and 0 deletions
|
@ -3881,6 +3881,33 @@ class FDInheritanceTests(unittest.TestCase):
|
||||||
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
|
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
|
||||||
0)
|
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):
|
def test_open(self):
|
||||||
fd = os.open(__file__, os.O_RDONLY)
|
fd = os.open(__file__, os.O_RDONLY)
|
||||||
self.addCleanup(os.close, fd)
|
self.addCleanup(os.close, fd)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix os.set_inheritable() for O_PATH file descriptors on Linux.
|
|
@ -1234,6 +1234,13 @@ set_inheritable(int fd, int inheritable, int raise, int *atomic_flag_works)
|
||||||
return 0;
|
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 (errno != ENOTTY && errno != EACCES) {
|
||||||
if (raise)
|
if (raise)
|
||||||
PyErr_SetFromErrno(PyExc_OSError);
|
PyErr_SetFromErrno(PyExc_OSError);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue