[issue47027] subprocess.run(), subprocess.Popen() should accept file descriptor as cwd parameter

2022-04-05 Thread Gregory P. Smith


Gregory P. Smith  added the comment:

this mostly requires plumbing to accept an int as the cwd and plumb that 
through to the between fork and exec code to call `fchdir(cwd_fd)` on the `int` 
instead of chdir(cwd) on the `char*`.

the Modules/_posixsubprocess.c internals are a bit of a mess today with a 
bazillion parameters passed to internal functions which makes this a pain... I 
want to refactor things to use a struct and not need that, but adding this 
feature is doable regardless.

--
components: +Extension Modules -Library (Lib)

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47027] subprocess.run(), subprocess.Popen() should accept file descriptor as cwd parameter

2022-04-05 Thread Yann Droneaud


Yann Droneaud  added the comment:

I looked at posixmodule: os.chdir() accepts a file descriptor.
Maybe it can be possible to invoke it from _posixsubprocess.c instead of 
calling chdir().

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47027] subprocess.run(), subprocess.Popen() should accept file descriptor as cwd parameter

2022-03-21 Thread Gregory P. Smith


Gregory P. Smith  added the comment:

Basically you want it to call fchdir() instead of chdir() when passed a fd 
(integer) instead of a string/Path-like.  That makes sense and should be a 
reasonably straight forward set of changes to _posixsubprocess.c.

(A way to convert a fd into a Path-like object would _not_ work as that'd 
reintroduce the TOCTOU on the directory - that'd be a pathlib feature request 
anyways, not a subprocess one)

--
stage:  -> needs patch
versions: +Python 3.11 -Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47027] subprocess.run(), subprocess.Popen() should accept file descriptor as cwd parameter

2022-03-21 Thread Ned Deily


Change by Ned Deily :


--
nosy: +gregory.p.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47027] subprocess.run(), subprocess.Popen() should accept file descriptor as cwd parameter

2022-03-15 Thread Yann Droneaud


New submission from Yann Droneaud :

subprocess.run() and subprocess.Popen() accepts a cwd= parameter to change 
directory before running the subprocess.

Unfortunately it's not possible to use a file descriptor to run the subprocess 
in a directory already opened.

For example:

import os
import subprocess
with os.open('/usr/bin', os.O_RDONLY) as f:
subprocess.run(["ls", "-l"], cwd=f, check=True)

fails with

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.9/subprocess.py", line 1754, in _execute_child
self.pid = _posixsubprocess.fork_exec(

Using a file descriptor instead of path is useful to address TOCTOU issues.

Maybe a mean to convert a file descriptor to a Path-like object would do the 
trick.

--
components: Library (Lib)
messages: 415249
nosy: ydroneaud
priority: normal
severity: normal
status: open
title: subprocess.run(), subprocess.Popen() should accept file descriptor as 
cwd parameter
type: enhancement
versions: Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com