~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/console.py

  • Committer: William Grant
  • Date: 2010-02-23 08:08:27 UTC
  • Revision ID: grantw@unimelb.edu.au-20100223080827-wklsx122pcw79wi7
Reject off-site non-GET requests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
import cjson
34
34
 
35
 
from ivle import chat, interpret
 
35
from ivle import chat
36
36
 
37
37
class ConsoleError(Exception):
38
38
    """ The console failed in some way. This is bad. """
108
108
class Console(object):
109
109
    """ Provides a nice python interface to the console
110
110
    """
111
 
    def __init__(self, config, user, jail_path, working_dir):
 
111
    def __init__(self, config, uid, jail_path, working_dir):
112
112
        """Starts up a console service for user uid, inside chroot jail 
113
113
        jail_path with work directory of working_dir
114
114
        """
115
115
        super(Console, self).__init__()
116
116
 
117
117
        self.config = config
118
 
        self.user = user
 
118
        self.uid = uid
119
119
        self.jail_path = jail_path
120
120
        self.working_dir = working_dir
121
121
 
152
152
        # is (k / N) ** t (e.g. 3.2*10e-9).
153
153
 
154
154
        tries = 0
155
 
        error = None
156
155
        while tries < 5:
157
156
            self.port = int(random.uniform(3000, 8000))
158
157
 
159
 
            python_console = os.path.join(self.config['paths']['share'],
160
 
                        'services/python-console')
161
 
            args = [python_console, str(self.port), str(self.magic)]
162
 
 
163
 
            try:
164
 
                interpret.execute_raw(self.config, self.user, self.jail_path,
165
 
                        self.working_dir, "/usr/bin/python", args)
 
158
            trampoline_path = os.path.join(self.config['paths']['lib'],
 
159
                                           "trampoline")
 
160
 
 
161
            # Start the console server (port, magic)
 
162
            # trampoline usage: tramp uid jail_dir working_dir script_path args
 
163
            # console usage:    python-console port magic
 
164
            res = os.spawnv(os.P_WAIT, trampoline_path, [
 
165
                trampoline_path,
 
166
                str(self.uid),
 
167
                self.config['paths']['jails']['mounts'],
 
168
                self.config['paths']['jails']['src'],
 
169
                self.config['paths']['jails']['template'],
 
170
                self.jail_path,
 
171
                os.path.join(self.config['paths']['share'], 'services'),
 
172
                "/usr/bin/python",
 
173
                os.path.join(self.config['paths']['share'],
 
174
                             'services/python-console'),
 
175
                str(self.port),
 
176
                str(self.magic),
 
177
                self.working_dir
 
178
                ])
 
179
 
 
180
            if res == 0:
166
181
                # success
167
 
                break
168
 
            except interpret.ExecutionError, e:
169
 
                tries += 1
170
 
                error = e.message
171
 
 
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 
174
 
        # exception.
 
182
                break;
 
183
 
 
184
            tries += 1
 
185
 
 
186
        # If we can't start the console after 5 attemps (can't find a free port 
 
187
        # during random probing, syntax errors, segfaults) throw an exception.
175
188
        if tries == 5:
176
 
            raise ConsoleError('Unable to start console service: %s'%error)
 
189
            raise ConsoleError("Unable to start console service!")
177
190
 
178
191
    def __chat(self, cmd, args):
179
192
        """ A wrapper around chat.chat to comunicate directly with the