35
from ivle import chat, interpret
36
from ivle import (chat, util)
39
trampoline_path = os.path.join(ivle.conf.lib_path, "trampoline")
41
python_path = "/usr/bin/python"
42
console_dir = os.path.join(ivle.conf.share_path, 'services')
43
console_path = os.path.join(console_dir, 'python-console')
37
45
class ConsoleError(Exception):
38
46
""" The console failed in some way. This is bad. """
47
def __init__(self, value):
50
return repr(self.value)
41
52
class ConsoleException(Exception):
42
53
""" The code being exectuted on the console returned an exception.
55
def __init__(self, value):
58
return repr(self.value)
46
60
class TruncateStringIO(StringIO.StringIO):
47
61
""" A class that wraps around StringIO and truncates the buffer when the
108
122
class Console(object):
109
123
""" Provides a nice python interface to the console
111
def __init__(self, config, user, jail_path, working_dir):
125
def __init__(self, uid, jail_path, working_dir):
112
126
"""Starts up a console service for user uid, inside chroot jail
113
127
jail_path with work directory of working_dir
115
129
super(Console, self).__init__()
119
132
self.jail_path = jail_path
120
133
self.working_dir = working_dir
144
self.magic = hashlib.md5(uuid.uuid4().bytes).hexdigest()
157
self.magic = md5.new(uuid.uuid4().bytes).digest().encode('hex')
146
159
# Try to find a free port on the server.
147
160
# Just try some random ports in the range [3000,8000)
152
165
# is (k / N) ** t (e.g. 3.2*10e-9).
157
169
self.port = int(random.uniform(3000, 8000))
159
python_console = os.path.join(self.config['paths']['share'],
160
'services/python-console')
161
args = [python_console, str(self.port), str(self.magic)]
171
# Start the console server (port, magic)
172
# trampoline usage: tramp uid jail_dir working_dir script_path args
173
# console usage: python-console port magic
174
res = os.spawnv(os.P_WAIT, trampoline_path, [
178
ivle.conf.jail_src_base,
179
ivle.conf.jail_system,
164
interpret.execute_raw(self.config, self.user, self.jail_path,
165
self.working_dir, "/usr/bin/python", args)
168
except interpret.ExecutionError, e:
172
# If we can't start the console after 5 attemps (can't find a free
173
# port during random probing, syntax errors, segfaults) throw an
195
# If we can't start the console after 5 attemps (can't find a free port
196
# during random probing, syntax errors, segfaults) throw an exception.
176
raise ConsoleError('Unable to start console service: %s'%error)
198
raise ConsoleError("Unable to start console service!")
178
200
def __chat(self, cmd, args):
179
201
""" A wrapper around chat.chat to comunicate directly with the
194
216
# Couldn't decode the JSON
195
217
raise ConsoleError(
196
218
"Could not understand the python console response")
197
except chat.ProtocolError, e:
198
raise ConsoleError(*e.args)
281
301
# Unpickle any exceptions
282
302
if 'exception' in execute:
283
execute['exception'] = cPickle.loads(execute['exception'])
303
return cPickle.loads(execute['exception'])
287
308
def set_vars(self, variables):