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

Simplification of the protocol; more functions implemented

parent 93dab2a9
...@@ -19,12 +19,14 @@ ADDR DEL if# addr_spec 200/500 ip addr del ...@@ -19,12 +19,14 @@ ADDR DEL if# addr_spec 200/500 ip addr del
ROUT LIST 200 serialised data ip route list ROUT LIST 200 serialised data ip route list
ROUT ADD route_spec 200/500 ip route add ROUT ADD route_spec 200/500 ip route add
ROUT DEL route_spec 200/500 ip route del ROUT DEL route_spec 200/500 ip route del
PROC CRTE [args_len] 354+200/500 (2) PROC CRTE proc_params 200/500 (2)
PROC SIN 354+200/500 (3) PROC CWD cwd 200/500 (3)
PROC SOUT 354+200/500 (3) PROC ENV k v k v... 200/500 (3)
PROC SERR 354+200/500 (3) PROC SIN 354+200/500 (4)
PROC RUN 200 <pid>/500 (4) PROC SOUT 354+200/500 (4)
PROC ABRT 200 (4) PROC SERR 354+200/500 (4)
PROC RUN 200 <pid>/500 (5)
PROC ABRT 200 (5)
PROC POLL <pid> 200 <code>/500 check if process alive PROC POLL <pid> 200 <code>/500 check if process alive
PROC WAIT <pid> 200 <code>/500 waitpid(pid) PROC WAIT <pid> 200 <code>/500 waitpid(pid)
PROC KILL <pid> <signal> 200/500 kill(pid, signal) PROC KILL <pid> <signal> 200/500 kill(pid, signal)
...@@ -32,15 +34,19 @@ PROC KILL <pid> <signal> 200/500 kill(pid, signal) ...@@ -32,15 +34,19 @@ PROC KILL <pid> <signal> 200/500 kill(pid, signal)
(1) valid arguments: mtu <n>, state <up|down>, name <name>, lladdr <addr> (1) valid arguments: mtu <n>, state <up|down>, name <name>, lladdr <addr>
(2) After PROC CRTE, only secondary PROC cmds are accepted until finished. (2) After PROC CRTE, only secondary PROC cmds are accepted until finished.
Server waits for serialized data (lenght pre-specified, or empty line to Arguments are: uid gid argv[0] argv[1] ...
finish) specifying complex arguments: cwd, env, argv. The argv parameters are parsed as base64-encoded strings if they start with a
After receiving the arguments, answers with 200 or 500. '=' character.
(3) Secondary PROC commands, only valid after PROC CRTE. Server reply 354 and (3) Secondary PROC commands, only valid after PROC CRTE. All parameters parsed
as base64-encoded strings. Arguments for PROC ENV are pairs of key-value to
set up the process environment.
(4) Secondary PROC commands, only valid after PROC CRTE. Server reply 354 and
waits for a file descriptor to be passed along with a duplicate of the same waits for a file descriptor to be passed along with a duplicate of the same
command. Answers 200/500 after processing the file descriptor. command. Answers 200/500 after processing the file descriptor.
(4) Secondary PROC commands, unconditionally end the PROC transaction. If RUN (5) Secondary PROC commands, unconditionally end the PROC transaction. If RUN
was successful, the process is started and the process ID is returned. was successful, the process is started and the process ID is returned.
......
...@@ -7,7 +7,7 @@ try: ...@@ -7,7 +7,7 @@ try:
except ImportError: except ImportError:
from yaml import Loader, Dumper from yaml import Loader, Dumper
import sys, yaml import passfd, sys, yaml
# Protocol definition # Protocol definition
# #
...@@ -35,7 +35,7 @@ _proto_commands = { ...@@ -35,7 +35,7 @@ _proto_commands = {
"DEL": ("sisi", "") "DEL": ("sisi", "")
}, },
"PROC": { "PROC": {
"CRTE": ("", "i"), "CRTE": ("iib", "b*"),
"POLL": ("i", ""), "POLL": ("i", ""),
"WAIT": ("i", "") "WAIT": ("i", "")
}, },
...@@ -45,6 +45,8 @@ _proc_commands = { ...@@ -45,6 +45,8 @@ _proc_commands = {
"HELP": { None: ("", "") }, "HELP": { None: ("", "") },
"QUIT": { None: ("", "") }, "QUIT": { None: ("", "") },
"PROC": { "PROC": {
"CWD": ("b", ""),
"ENV": ("bb", "b*"),
"SIN": ("", ""), "SIN": ("", ""),
"SOUT": ("", ""), "SOUT": ("", ""),
"SERR": ("", ""), "SERR": ("", ""),
...@@ -58,6 +60,7 @@ class Server(object): ...@@ -58,6 +60,7 @@ class Server(object):
self.commands = _proto_commands self.commands = _proto_commands
self.closed = False self.closed = False
self.debug = True self.debug = True
self._proc = None
if hasattr(fd, "readline"): if hasattr(fd, "readline"):
self.f = fd self.f = fd
else: else:
...@@ -150,22 +153,35 @@ class Server(object): ...@@ -150,22 +153,35 @@ class Server(object):
if len(args) < len(mandatory): if len(args) < len(mandatory):
self.reply(500, "Missing mandatory arguments for %s." % cmdname) self.reply(500, "Missing mandatory arguments for %s." % cmdname)
return None return None
if len(args) > len(argstemplate): if (not argstemplate or argstemplate[-1] != "*") and \
len(args) > len(argstemplate):
self.reply(500, "Too many arguments for %s." % cmdname) self.reply(500, "Too many arguments for %s." % cmdname)
return None return None
j = 0
for i in range(len(args)): for i in range(len(args)):
if argstemplate[i] == 'i': if argstemplate[j] == '*':
j = j - 1
if argstemplate[j] == 'i':
try: try:
args[i] = int(args[i]) args[i] = int(args[i])
except: except:
self.reply(500, "Invalid parameter %s: must be an integer." self.reply(500, "Invalid parameter %s: must be an integer."
% args[i]) % args[i])
return None return None
elif argstemplate[i] == 's': elif argstemplate[j] == 's':
pass pass
elif argstemplate[j] == 'b':
try:
if args[i][0] == '=':
args[i] = base64.b64decode(args[i])
except:
self.reply(500, "Invalid parameter: not base-64 encoded.")
return None
else: else:
raise RuntimeError("Invalid argument template: %s" % _argstmpl) raise RuntimeError("Invalid argument template: %s" % _argstmpl)
j += 1
func = getattr(self, funcname) func = getattr(self, funcname)
return (func, cmdname, args) return (func, cmdname, args)
...@@ -185,6 +201,16 @@ class Server(object): ...@@ -185,6 +201,16 @@ class Server(object):
pass pass
# FIXME: cleanup # FIXME: cleanup
def do_HELP(self, cmdname):
reply = ["Available commands:"]
for c in sorted(self.commands):
for sc in sorted(self.commands[c]):
if sc:
reply.append("%s %s" % (c, sc))
else:
reply.append(c)
self.reply(200, reply)
def do_QUIT(self, cmdname): def do_QUIT(self, cmdname):
self.reply(221, "Sayounara."); self.reply(221, "Sayounara.");
self.closed = True self.closed = True
...@@ -207,63 +233,53 @@ class Server(object): ...@@ -207,63 +233,53 @@ class Server(object):
pass pass
def do_ROUT_DEL(self, cmdname, prefix, prefixlen, nexthop, ifnr): def do_ROUT_DEL(self, cmdname, prefix, prefixlen, nexthop, ifnr):
pass pass
def do_PROC_CRTE(self, cmdname, argslen = None): def do_PROC_CRTE(self, cmdname, uid, gid, file, *argv):
if argslen: self._proc = { 'uid': uid, 'gid': gid, 'file': file, 'argv': argv }
self.reply(354, "Go ahead, reading %d bytes." % argslen) self.commands = _proc_commands
else: self.reply(200, "Entering PROC mode.")
self.reply(354, "Go ahead, enter an empty line to finish.")
argsstr = self.readchunk(argslen)
if argsstr == None: # connection closed
return
if not argsstr:
self.reply(500, "Missing parameters.")
return
try:
args = yaml.load(argsstr, Loader = Loader)
except BaseException, e:
self.reply(500, "Cannot decode parameters: %s" % e)
return
if not hasattr(args, "items"): def do_PROC_CWD(self, cmdname, dir):
self.reply(500, "Invalid parameters.") self._proc['cwd'] = dir
self.reply(200, "CWD set to %s." % dir)
def do_PROC_ENV(self, cmdname, *env):
if len(env) % 2:
self.reply(500,
"Invalid number of arguments for PROC ENV: must be even.")
return return
self._proc['env'] = {}
for i in range(len(env)/2):
self._proc['env'][env[i * 2]] = env[i * 2 + 1]
self.reply(200, "%d environment definition(s) read." % (len(env) / 2))
valid_params = { 'cwd': str, 'exec': str, 'args': list, 'uid': int, def do_PROC_SIN(self, cmdname):
'gid': int } self.reply(354,
for (k, v) in args.items(): "Pass the file descriptor now, with '%s\\n' as payload." %
cmdname)
try: try:
args[k] = valid_params[k](args[k]) fd, payload = passfd.recvfd(len(cmdname) + 1)
assert payload[0:len(cmdname)] == cmdname
except: except:
self.reply(500, "Invalid parameter: %s" % k) self.reply(500, "Invalid FD or payload.")
return raise
required_params = [ 'exec', 'args', 'uid', 'gid' ]
for i in required_params:
if i not in args:
self.reply(500, "Missing required parameter: %s" % i)
return return
m = {'PROC SIN': 'stdin', 'PROC SOUT': 'stdout', 'PROC SERR': 'stderr'}
self._proc[m[cmdname]] = fd
self.reply(200, 'FD saved as %d.' % m[cmdname])
if self.debug: do_PROC_SOUT = do_PROC_SERR = do_PROC_SIN
sys.stderr.write("Arguments for PROC CRTE: %s\n" % str(args))
self.reply(200, "Entering PROC mode.")
self._proc_args = args
self.commands = _proc_commands
def do_PROC_POLL(self, cmdname, pid):
pass
def do_PROC_WAIT(self, cmdname, pid):
pass
def do_PROC_SIN(self, cmdname):
pass
def do_PROC_SOUT(self, cmdname):
pass
def do_PROC_SERR(self, cmdname):
pass
def do_PROC_RUN(self, cmdname): def do_PROC_RUN(self, cmdname):
self.reply(200, "Aborted.") self._proc = None
self.commands = _proto_commands self.commands = _proto_commands
def do_PROC_ABRT(self, cmdname):
self.reply(200, "Aborted.") self.reply(200, "Aborted.")
def do_PROC_ABRT(self, cmdname):
self._proc = None
self.commands = _proto_commands self.commands = _proto_commands
self.reply(200, "Aborted.")
def do_PROC_POLL(self, cmdname, pid):
pass
def do_PROC_WAIT(self, cmdname, pid):
pass
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