def outReceived(self, text): self.__instream.write(text) #self.__instream.seek(0,2) #log.msg("Received {0} bytes of input (total {1}). Unmarshalling...".format(len(text), self.__instream.tell())) self.__instream.seek(0) try: fname = sandlib.marshal.load(self.__instream) args = sandlib.marshal.load(self.__instream) except EOFError as e: #log.msg("EOFError unmarshalling args ({0}). Deferring until we get more data".format(e)) self.__instream.seek(0,2) return except Exception as e: log.msg(traceback.format_exc()) self.abort() return else: self.__instream.truncate(0) #log.msg("unmarshal successful. Sandbox func call: {0}{1!r}".format(fname, sandlib.shortrepr(args))) try: retval = self.handle_message(fname, *args) if isinstance(retval, defer.Deferred): answer, resulttype = (yield retval) else: answer, resulttype = retval except Exception as e: #log.msg("Raise exception: {1}, {0}".format(e, e.__class__.__name__)) tb = sys.exc_info()[2] sandlib.write_exception(self.transport, e, tb) else: if not self.exited: #log.msg("Return: {0}".format(sandlib.shortrepr(answer))) sandlib.write_message(self.transport, 0) # error code - 0 for ok sandlib.write_message(self.transport, answer, resulttype)
def expect(f, g, fnname, args, result, resulttype=None): msg = read_message(f, timeout=10.0) assert msg == fnname msg = read_message(f, timeout=10.0) assert msg == args if isinstance(result, Exception): write_exception(g, result) else: write_message(g, 0) write_message(g, result, resulttype) g.flush()
def outReceived(self, text): self.__instream.write(text) #self.__instream.seek(0,2) #log.msg("Received {0} bytes of input (total {1}). Unmarshalling...".format(len(text), self.__instream.tell())) self.__instream.seek(0) try: fname = sandlib.marshal.load(self.__instream) args = sandlib.marshal.load(self.__instream) except EOFError as e: #log.msg("EOFError unmarshalling args ({0}). Deferring until we get more data".format(e)) self.__instream.seek(0, 2) return except Exception as e: log.msg(traceback.format_exc()) self.abort() return else: self.__instream.truncate(0) #log.msg("unmarshal successful. Sandbox func call: {0}{1!r}".format(fname, sandlib.shortrepr(args))) try: retval = self.handle_message(fname, *args) if isinstance(retval, defer.Deferred): answer, resulttype = (yield retval) else: answer, resulttype = retval except Exception as e: #log.msg("Raise exception: {1}, {0}".format(e, e.__class__.__name__)) tb = sys.exc_info()[2] sandlib.write_exception(self.transport, e, tb) else: if not self.exited: #log.msg("Return: {0}".format(sandlib.shortrepr(answer))) sandlib.write_message(self.transport, 0) # error code - 0 for ok sandlib.write_message(self.transport, answer, resulttype)
self.abort() return else: self.__instream.truncate(0) #log.msg("unmarshal successful. Sandbox func call: {0}{1!r}".format(fname, sandlib.shortrepr(args))) try: retval = self.handle_message(fname, *args) if isinstance(retval, defer.Deferred): answer, resulttype = (yield retval) else: answer, resulttype = retval except Exception, e: #log.msg("Raise exception: {1}, {0}".format(e, e.__class__.__name__)) tb = sys.exc_info()[2] sandlib.write_exception(self.transport, e, tb) else: if not self.exited: #log.msg("Return: {0}".format(sandlib.shortrepr(answer))) sandlib.write_message(self.transport, 0) # error code - 0 for ok sandlib.write_message(self.transport, answer, resulttype) def abort(self): """Kill the process and bail out""" self.transport.loseConnection() if not self.exited: self.transport.signalProcess("KILL") if self.ended_deferred: self.ended_deferred.callback("Process aborted") self.ended_deferred = None