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
ROUT LIST 200 serialised data ip route list
ROUT ADD route_spec 200/500 ip route add
ROUT DEL route_spec 200/500 ip route del
PROC CRTE [args_len] 354+200/500 (2)
PROC SIN 354+200/500 (3)
PROC SOUT 354+200/500 (3)
PROC SERR 354+200/500 (3)
PROC RUN 200 <pid>/500 (4)
PROC ABRT 200 (4)
PROC CRTE proc_params 200/500 (2)
PROC CWD cwd 200/500 (3)
PROC ENV k v k v... 200/500 (3)
PROC SIN 354+200/500 (4)
PROC SOUT 354+200/500 (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 WAIT <pid> 200 <code>/500 waitpid(pid)
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>
(2) After PROC CRTE, only secondary PROC cmds are accepted until finished.
Server waits for serialized data (lenght pre-specified, or empty line to
finish) specifying complex arguments: cwd, env, argv.
After receiving the arguments, answers with 200 or 500.
Arguments are: uid gid argv[0] argv[1] ...
The argv parameters are parsed as base64-encoded strings if they start with a
'=' 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
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.
......
......@@ -7,7 +7,7 @@ try:
except ImportError:
from yaml import Loader, Dumper
import sys, yaml
import passfd, sys, yaml
# Protocol definition
#
......@@ -35,7 +35,7 @@ _proto_commands = {
"DEL": ("sisi", "")
},
"PROC": {
"CRTE": ("", "i"),
"CRTE": ("iib", "b*"),
"POLL": ("i", ""),
"WAIT": ("i", "")
},
......@@ -45,6 +45,8 @@ _proc_commands = {
"HELP": { None: ("", "") },
"QUIT": { None: ("", "") },
"PROC": {
"CWD": ("b", ""),
"ENV": ("bb", "b*"),
"SIN": ("", ""),
"SOUT": ("", ""),
"SERR": ("", ""),
......@@ -58,6 +60,7 @@ class Server(object):
self.commands = _proto_commands
self.closed = False
self.debug = True
self._proc = None
if hasattr(fd, "readline"):
self.f = fd
else:
......@@ -150,22 +153,35 @@ class Server(object):
if len(args) < len(mandatory):
self.reply(500, "Missing mandatory arguments for %s." % cmdname)
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)
return None
j = 0
for i in range(len(args)):
if argstemplate[i] == 'i':
if argstemplate[j] == '*':
j = j - 1
if argstemplate[j] == 'i':
try:
args[i] = int(args[i])
except:
self.reply(500, "Invalid parameter %s: must be an integer."
% args[i])
return None
elif argstemplate[i] == 's':
elif argstemplate[j] == 's':
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:
raise RuntimeError("Invalid argument template: %s" % _argstmpl)
j += 1
func = getattr(self, funcname)
return (func, cmdname, args)
......@@ -185,6 +201,16 @@ class Server(object):
pass
# 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):
self.reply(221, "Sayounara.");
self.closed = True
......@@ -207,63 +233,53 @@ class Server(object):
pass
def do_ROUT_DEL(self, cmdname, prefix, prefixlen, nexthop, ifnr):
pass
def do_PROC_CRTE(self, cmdname, argslen = None):
if argslen:
self.reply(354, "Go ahead, reading %d bytes." % argslen)
else:
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
def do_PROC_CRTE(self, cmdname, uid, gid, file, *argv):
self._proc = { 'uid': uid, 'gid': gid, 'file': file, 'argv': argv }
self.commands = _proc_commands
self.reply(200, "Entering PROC mode.")
if not hasattr(args, "items"):
self.reply(500, "Invalid parameters.")
return
def do_PROC_CWD(self, cmdname, dir):
self._proc['cwd'] = dir
self.reply(200, "CWD set to %s." % dir)
valid_params = { 'cwd': str, 'exec': str, 'args': list, 'uid': int,
'gid': int }
for (k, v) in args.items():
try:
args[k] = valid_params[k](args[k])
except:
self.reply(500, "Invalid parameter: %s" % k)
return
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
self._proc['env'] = {}
for i in range(len(env)/2):
self._proc['env'][env[i * 2]] = env[i * 2 + 1]
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
self.reply(200, "%d environment definition(s) read." % (len(env) / 2))
if self.debug:
sys.stderr.write("Arguments for PROC CRTE: %s\n" % str(args))
self.reply(200, "Entering PROC mode.")
def do_PROC_SIN(self, cmdname):
self.reply(354,
"Pass the file descriptor now, with '%s\\n' as payload." %
cmdname)
try:
fd, payload = passfd.recvfd(len(cmdname) + 1)
assert payload[0:len(cmdname)] == cmdname
except:
self.reply(500, "Invalid FD or payload.")
raise
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])
self._proc_args = args
self.commands = _proc_commands
do_PROC_SOUT = do_PROC_SERR = do_PROC_SIN
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):
self.reply(200, "Aborted.")
self._proc = None
self.commands = _proto_commands
def do_PROC_ABRT(self, cmdname):
self.reply(200, "Aborted.")
def do_PROC_ABRT(self, cmdname):
self._proc = None
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