Skip to content

Add support for piping into wsl-sudo.py #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
33 changes: 23 additions & 10 deletions wsl-sudo.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def child_process(self, argv, cwd, winsize, envdict):
def main_process(self):
self.transfer_loop()
for fd in set(self.child_fds):
os.close(fd)
if fd is not None:
os.close(fd)

print('pty closed, getting return value')
(success, exit_status) = os.waitpid(self.child_pid, 0)
Expand All @@ -152,7 +153,12 @@ def transfer_loop(self):
if fd == sock_fd:
cmd, data = self.channel.recv_command()
if cmd == CMD_STDIN:
os.write(self.child_fds[0], data)
if not data:
# Close the child's stdin to send EOF
os.close(self.child_fds[0])
self.child_fds[0] = None
else:
os.write(self.child_fds[0], data)
elif cmd == CMD_WINSZ:
fcntl.ioctl(self.child_fds[1], termios.TIOCSWINSZ, data)
os.kill(self.child_pid, signal.SIGWINCH)
Expand Down Expand Up @@ -220,14 +226,20 @@ def main(self, command, visibility, **kwargs):
window_style = ['Hidden', 'Minimized', 'Normal'][visibility]

try:
subprocess.check_call(
["powershell.exe", "Start-Process", "-Verb", "runas",
"-WindowStyle", window_style,
"-FilePath", "wsl", "-ArgumentList",
'"{}"'.format(subprocess.list2cmdline([
sys.executable, os.path.abspath(__file__),
'--elevated', 'visible' if visibility else 'hidden',
str(port), pwf.name]))])
subprocess.check_call([
"powershell.exe", "Start-Process",
"-Verb", "runas",
"-WindowStyle", window_style,
"-FilePath", "wsl",
"-ArgumentList", '"{}"'.format(subprocess.list2cmdline([
sys.executable,
os.path.abspath(__file__),
'--elevated',
'visible' if visibility else 'hidden',
str(port),
pwf.name,
]))
], stdin=subprocess.DEVNULL)
except subprocess.CalledProcessError as e:
print("wudo: failed to start elevated process")
return
Expand Down Expand Up @@ -264,6 +276,7 @@ def handle_sigwinch(n, f):
self.channel.send_command(CMD_STDIN, chunk)
else:
# stdin is a pipe and is closed
self.channel.send_command(CMD_STDIN, b'')
fdset.remove(0)
else:
self.recv_command()
Expand Down