From 18b8a71dcd67f0c01541968023c6a53f3e5e0d11 Mon Sep 17 00:00:00 2001 From: cptpcrd <31829097+cptpcrd@users.noreply.github.com> Date: Fri, 8 Jan 2021 15:46:11 -0500 Subject: [PATCH 1/3] bpo-42780: fix set_inheritable() for O_PATH file descriptors on Linux --- Lib/test/test_os.py | 21 +++++++++++++++++++ .../2021-01-08-15-49-20.bpo-42780.rtqi6B.rst | 1 + Python/fileutils.c | 7 +++++++ 3 files changed, 29 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 08d7ab8a30ba7e..e41ea2ea012951 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3881,6 +3881,27 @@ def test_set_inheritable_cloexec(self): 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): + with self.assertRaises(OSError) as ctx: + os.set_inheritable(-1, True) + self.assertEqual(ctx.exception.errno, errno.EBADF) + + with self.assertRaises(OSError) as ctx: + os.set_inheritable(-1, False) + self.assertEqual(ctx.exception.errno, errno.EBADF) + def test_open(self): fd = os.open(__file__, os.O_RDONLY) self.addCleanup(os.close, fd) diff --git a/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst b/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst new file mode 100644 index 00000000000000..a491690507129e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-01-08-15-49-20.bpo-42780.rtqi6B.rst @@ -0,0 +1 @@ +Fix os.set_inheritable() for O_PATH file descriptors on Linux. diff --git a/Python/fileutils.c b/Python/fileutils.c index 8dc90fbe2b2e71..f2b4681ea849c5 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -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); From 24034fbbf49b24ff3b46d681eb7991144886c43d Mon Sep 17 00:00:00 2001 From: cptpcrd <31829097+cptpcrd@users.noreply.github.com> Date: Wed, 20 Jan 2021 08:13:39 -0500 Subject: [PATCH 2/3] bpo-42780: use os_helper.make_bad_fd() in "inheritable" test --- Lib/test/test_os.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index e41ea2ea012951..4276987ffb5796 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3894,12 +3894,14 @@ def test_get_set_inheritable_o_path(self): 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.set_inheritable(-1, True) + os.set_inheritable(fd, True) self.assertEqual(ctx.exception.errno, errno.EBADF) with self.assertRaises(OSError) as ctx: - os.set_inheritable(-1, False) + os.set_inheritable(fd, False) self.assertEqual(ctx.exception.errno, errno.EBADF) def test_open(self): From 731ee777fc80612a9084ba8291baba9ee8432bd0 Mon Sep 17 00:00:00 2001 From: cptpcrd <31829097+cptpcrd@users.noreply.github.com> Date: Wed, 20 Jan 2021 08:15:07 -0500 Subject: [PATCH 3/3] bpo-42780: test for EBADF from os.get_inheritable() --- Lib/test/test_os.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 4276987ffb5796..96fddc7d057663 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3896,6 +3896,10 @@ def test_get_set_inheritable_o_path(self): 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)