Commit bfb3a065 authored by Martín Ferrari's avatar Martín Ferrari

Improve base64 encoding, close file descriptors after spawn()

parent 85d80ba4
...@@ -308,8 +308,14 @@ class Server(object): ...@@ -308,8 +308,14 @@ class Server(object):
return return
self._children[chld.pid] = chld self._children[chld.pid] = chld
self._proc = None
self.commands = _proto_commands self.commands = _proto_commands
# I can close the fds now
for d in ('stdin', 'stdout', 'stderr'):
if d in self._proc:
os.close(self._proc[d])
self._proc = None
self.reply(200, "%d running." % chld.pid) self.reply(200, "%d running." % chld.pid)
def do_PROC_ABRT(self, cmdname): def do_PROC_ABRT(self, cmdname):
...@@ -406,6 +412,7 @@ class Client(object): ...@@ -406,6 +412,7 @@ class Client(object):
defaults to 2.""" defaults to 2."""
code, text = self._read_reply() code, text = self._read_reply()
if code / 100 != expected: if code / 100 != expected:
# FIXME: shuld try to save and re-create exceptions
raise RuntimeError("Error from slave: %d %s" % (code, text)) raise RuntimeError("Error from slave: %d %s" % (code, text))
return text return text
...@@ -428,26 +435,26 @@ class Client(object): ...@@ -428,26 +435,26 @@ class Client(object):
stdin/stdout/stderr can only be None or a open file descriptor. stdin/stdout/stderr can only be None or a open file descriptor.
See netns.subprocess.spawn for details.""" See netns.subprocess.spawn for details."""
params = ["PROC", "CRTE", base64.b64encode(executable)] params = ["PROC", "CRTE", _b64(executable)]
if argv != None: if argv != None:
for i in argv: for i in argv:
params.append(base64.b64encode(i)) params.append(_b64(i))
self._send_cmd(*params) self._send_cmd(*params)
self._read_and_check_reply() self._read_and_check_reply()
if user != None: if user != None:
self._send_cmd("PROC", "USER", base64.b64encode(user)) self._send_cmd("PROC", "USER", _b64(user))
self._read_and_check_reply() self._read_and_check_reply()
if cwd != None: if cwd != None:
self._send_cmd("PROC", "CWD", base64.b64encode(cwd)) self._send_cmd("PROC", "CWD", _b64(cwd))
self._read_and_check_reply() self._read_and_check_reply()
if env != None: if env != None:
params = [] params = []
for i in env: for i in env:
params.append(base64.b64encode(i)) params.append(_b64(i))
self._send_cmd("PROC", "ENV", params) self._send_cmd("PROC", "ENV", params)
self._read_and_check_reply() self._read_and_check_reply()
...@@ -484,7 +491,7 @@ class Client(object): ...@@ -484,7 +491,7 @@ class Client(object):
exitcode = text.split()[0] exitcode = text.split()[0]
return exitcode return exitcode
def kill(self, pid, sig = signal.SIGTERM): def signal(self, pid, sig = signal.SIGTERM):
"""Equivalent to Popen.send_signal(). Sends a signal to the child """Equivalent to Popen.send_signal(). Sends a signal to the child
process; signal defaults to SIGTERM.""" process; signal defaults to SIGTERM."""
if sig: if sig:
...@@ -494,3 +501,11 @@ class Client(object): ...@@ -494,3 +501,11 @@ class Client(object):
text = self._read_and_check_reply() text = self._read_and_check_reply()
def _b64(text):
text = str(text)
if filter(lambda x: ord(x) > ord(" ") and ord(x) <= ord("z")
and x != "=", text):
return text
else:
return "=" + base64.b64encode(text)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment