Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions unix-ffi/os/os/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
write_ = libc.func("i", "write", "iPi")
close_ = libc.func("i", "close", "i")
dup_ = libc.func("i", "dup", "i")
dup2_ = libc.func("i", "dup2", "ii")
access_ = libc.func("i", "access", "si")
fork_ = libc.func("i", "fork", "")
pipe_ = libc.func("i", "pipe", "p")
Expand Down Expand Up @@ -207,6 +208,10 @@ def dup(fd):
check_error(r)
return r

def dup2(oldfd, newfd):
r = dup2_(oldfd, newfd)
check_error(r)
return r

def access(path, mode):
return access_(path, mode) == 0
Expand Down Expand Up @@ -293,24 +298,53 @@ def urandom(n):
with builtins.open("/dev/urandom", "rb") as f:
return f.read(n)

class _PopenStream:
def __init__(self, fd, pid, mode):
self._fd = open(f"/dev/fd/{fd}", mode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use builtins.open so that the returned object is a stream.

self._pid = pid
self._closed = False
self._exitcode = None

def popen(cmd, mode="r"):
import builtins
def __enter__(self):
return self._fd

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

i, o = pipe()
if mode[0] == "w":
i, o = o, i
def close(self):
if not self._closed:
try:
self._fd.close()
finally:
pid, status = waitpid(self._pid, 0)
self._exitcode = status
self._closed = True
return self._exitcode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This _PopenStream object will need to implement read/write/etc operations, so that you can communicate with the process.

Eg prior to this patch it's possible to do:

import os
ls = os.popen("/bin/ls")
print(ls.read())
ls.close()

and that still needs to work.



def popen(cmd, mode='r'):
rfd, wfd = pipe()
pid = fork()
if not pid:
if mode[0] == "r":
close(1)

if pid == 0:
# --- CHILD ---
if mode[0] == 'r':
close(rfd)
dup2(wfd, 1) # connect pipe to stdout
close(wfd)
else:
close(0)
close(i)
dup(o)
close(o)
s = system(cmd)
_exit(s)
close(wfd)
dup2(rfd, 0) # connect pipe to stdin
close(rfd)

execvp("sh", ["sh", "-c", cmd])
# If execvp SUCCEEDS, the code below is NEVER executed.
_exit(127)

# --- PARENT ---
if mode[0] == 'r':
close(wfd)
return _PopenStream(rfd, pid, mode)
else:
close(o)
return builtins.open(i, mode)
close(rfd)
return _PopenStream(wfd, pid, mode)
Loading