def _doReadOrWrite(self, selectable, fd, event): why = None inRead = False if event & POLL_DISCONNECTED and not (event & POLLIN): if fd in self._reads: why = main.CONNECTION_DONE inRead = True else: why = main.CONNECTION_LOST else: try: if event & POLLIN: why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway('Filedescriptor went away') inRead = False except: log.deferr() why = sys.exc_info()[1] if why: self._disconnectSelectable(selectable, why, inRead)
def connectionFailed(self): for notifier in self.failures: try: notifier() except: log.deferr() self.failures = None
def _doReadOrWrite(self, selectable, fd, event, POLLIN, POLLOUT, log, faildict=None): if not faildict: faildict = { error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) } why = None inRead = False if event & POLL_DISCONNECTED and not (event & POLLIN): why = main.CONNECTION_LOST else: try: if event & POLLIN: why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway('Filedescriptor went away') inRead = False except AttributeError, ae: if "'NoneType' object has no attribute 'writeHeaders'" not in ae.message: log.deferr() why = sys.exc_info()[1] else: why = None except:
def _doReadOrWrite(self, source, condition, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()), }): why = None didRead = None if condition & POLL_DISCONNECTED and \ not (condition & gobject.IO_IN): why = main.CONNECTION_LOST else: try: if condition & gobject.IO_IN: why = source.doRead() didRead = source.doRead if not why and condition & gobject.IO_OUT: # if doRead caused connectionLost, don't call doWrite # if doRead is doWrite, don't call it again. if not source.disconnected and source.doWrite != didRead: why = source.doWrite() didRead = source.doWrite # if failed it was in write except: why = sys.exc_info()[1] log.msg('Error In %s' % source) log.deferr() if why: self._disconnectSelectable(source, why, didRead == source.doRead)
def connectionReady(self): """Initialize. """ # Some terms: # PUID: process unique ID; return value of id() function. type "int". # LUID: locally unique ID; an ID unique to an object mapped over this # connection. type "int" # GUID: (not used yet) globally unique ID; an ID for an object which # may be on a redirected or meta server. Type as yet undecided. self.sendCall("version", self.version) self.currentRequestID = 0 self.currentLocalID = 0 # Dictionary mapping LUIDs to local objects. # set above to allow root object to be assigned before connection is made # self.localObjects = {} # Dictionary mapping PUIDs to LUIDs. self.luids = {} # Dictionary mapping LUIDs to local (remotely cached) objects. Remotely # cached means that they're objects which originate here, and were # copied remotely. self.remotelyCachedObjects = {} # Dictionary mapping PUIDs to (cached) LUIDs self.remotelyCachedLUIDs = {} # Dictionary mapping (remote) LUIDs to (locally cached) objects. self.locallyCachedObjects = {} self.waitingForAnswers = {} for notifier in self.connects: try: notifier() except: log.deferr() self.connects = None
def _doReadOrWrite(self, selectable, fd, event, POLLIN, POLLOUT, log, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): why = None inRead = False if event & POLL_DISCONNECTED and not (event & POLLIN): why = main.CONNECTION_LOST else: try: if event & POLLIN: why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway('Filedescriptor went away') inRead = False except: log.deferr() why = sys.exc_info()[1] if why: self._disconnectSelectable(selectable, why, inRead)
def _readAndWrite(self, source, condition): # note: gtk-1.2's gtk_input_add presents an API in terms of gdk # constants like INPUT_READ and INPUT_WRITE. Internally, it will add # POLL_HUP and POLL_ERR to the poll() events, but if they happen it # will turn them back into INPUT_READ and INPUT_WRITE. gdkevents.c # maps IN/HUP/ERR to INPUT_READ, and OUT/ERR to INPUT_WRITE. This # means there is no immediate way to detect a disconnected socket. # The g_io_add_watch() API is more suited to this task. I don't think # pygtk exposes it, though. why = None didRead = None try: if condition & gtk.GDK.INPUT_READ: why = source.doRead() didRead = source.doRead if not why and condition & gtk.GDK.INPUT_WRITE: # if doRead caused connectionLost, don't call doWrite # if doRead is doWrite, don't call it again. if not source.disconnected and source.doWrite != didRead: why = source.doWrite() didRead = source.doWrite # if failed it was in write except: why = sys.exc_info()[1] log.msg('Error In %s' % source) log.deferr() if why: self._disconnectSelectable(source, why, didRead == source.doRead)
def _runOperationInTransaction(self, trans, *args, **kwargs): try: return trans.execute(*args, **kwargs) except: log.msg('Exception in SQL operation %s %s'%(trans, args)) log.deferr() raise
def _doReadOrWrite(self, source, condition, faildict=None): faildict = faildict or { error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()), } why = None inRead = False if condition & POLL_DISCONNECTED and not (condition & glib.IO_IN): if source in self._reads: why = main.CONNECTION_DONE inRead = True else: why = main.CONNECTION_LOST else: try: if condition & glib.IO_IN: why = source.doRead() inRead = True if not why and condition & glib.IO_OUT: # if doRead caused connectionLost, don't call doWrite # if doRead is doWrite, don't call it again. if not source.disconnected: why = source.doWrite() except: why = sys.exc_info()[1] log.msg('Error In %s' % source) log.deferr() if why: self._disconnectSelectable(source, why, inRead)
def display(self, request): tm = [] flip = 0 namespace = {} self.prePresent(request) self.addVariables(namespace, request) # This variable may not be obscured... namespace['request'] = request namespace['self'] = self for elem in self.tmpl: flip = not flip if flip: if elem: tm.append(elem) else: try: x = eval(elem, namespace, namespace) except: log.deferr() tm.append(webutil.formatFailure(failure.Failure())) else: if isinstance(x, types.ListType): tm.extend(x) elif isinstance(x, Widget): val = x.display(request) if not isinstance(val, types.ListType): raise Exception("%s.display did not return a list, it returned %s!" % (x.__class__, repr(val))) tm.extend(val) else: # Only two allowed types here should be deferred and # string. tm.append(x) return tm
def runUntilCurrent(self): """Run all pending timed calls. """ if self.threadCallQueue: # Keep track of how many calls we actually make, as we're # making them, in case another call is added to the queue # while we're in this loop. count = 0 total = len(self.threadCallQueue) for (f, a, kw) in self.threadCallQueue: try: f(*a, **kw) except: log.err() count += 1 if count == total: break del self.threadCallQueue[:count] if len(self.threadCallQueue) > 0: if self.waker: self.waker.wakeUp() # insert new delayed calls now self._insertNewDelayedCalls() now = seconds() while self._pendingTimedCalls and (self._pendingTimedCalls[0].time <= now): call = heappop(self._pendingTimedCalls) if call.cancelled: self._cancellations-=1 continue if call.delayed_time > 0: call.activate_delay() heappush(self._pendingTimedCalls, call) continue try: call.called = 1 call.func(*call.args, **call.kw) except: log.deferr() if hasattr(call, "creator"): e = "\n" e += " C: previous exception occurred in " + \ "a DelayedCall created here:\n" e += " C:" e += "".join(call.creator).rstrip().replace("\n","\n C:") e += "\n" log.msg(e) if (self._cancellations > 50 and self._cancellations > len(self._pendingTimedCalls) >> 1): self._cancellations = 0 self._pendingTimedCalls = [x for x in self._pendingTimedCalls if not x.cancelled] heapify(self._pendingTimedCalls)
def __del__(self): """Do distributed reference counting on finalize. """ try: if self.broker: self.broker.decCacheRef(self.luid) except: log.deferr()
def _continueSystemEvent(self, eventType): sysEvtTriggers = self._eventTriggers.get(eventType) for callList in sysEvtTriggers[1], sysEvtTriggers[2]: for callable, args, kw in callList: try: callable(*args, **kw) except: log.deferr()
def errConnectionLost(self): self.lostErrorConnection = 1 del self.err try: self.proto.errConnectionLost() except: log.deferr() self.maybeCallProcessEnded()
def _runAction(self, action, fd): try: closed = getattr(fd, action)() except: closed = sys.exc_info()[1] log.deferr() if closed: self._disconnectSelectable(fd, closed, action == 'doRead')
def inConnectionLost(self): try: self.proto.inConnectionLost() except: log.deferr() del self.writer self.lostInConnection = 1 self.maybeCallProcessEnded()
def connectionFailed(self): # XXX should never get called anymore? check! for notifier in self.failures: try: notifier() except: log.deferr() self.failures = None
def lineReceived(self, line): try: irc.IRCClient.lineReceived(self, line) except: # If you *don't* catch exceptions here, any unhandled exception # raised by anything lineReceived calls (which is most of the # client code) ends up making Connection Lost happen, which # is almost certainly not necessary for us. log.deferr()
def addStdout(self, data): if not self.decoder: return try: self.decoder.dataReceived(data) except BananaError: self.decoder = None log.msg("trial --jelly output unparseable, traceback follows") log.deferr()
def botCommand(self, command, prefix, params, recipient): method = getattr(self, "bot_%s" % command, None) try: if method is not None: method(prefix, params, recipient) else: self.bot_unknown(prefix, command, params, recipient) except: log.deferr()
def __del__(self): """Do distributed reference counting on finalize. """ try: # log.msg( ' --- decache: %s %s' % (self, self.luid) ) if self.broker: self.broker.decCacheRef(self.luid) except: log.deferr()
def do_RSET(self): """Unset all deleted message flags""" try: self.mbox.undeleteMessages() except: log.deferr() self.failResponse() else: self.highest = 1 self.successResponse()
def sendUpdate(self, remote, last=0): self.watchers[remote].needUpdate = 0 #text = self.asText() # TODO: not text, duh try: remote.callRemote("progress", self.remaining()) if last: remote.callRemote("finished", self) except: log.deferr() self.removeWatcher(remote)
def _continueSystemEvent(self, eventType): sysEvtTriggers = self._eventTriggers.get(eventType) for callList in sysEvtTriggers[1], sysEvtTriggers[2]: for callable, args, kw in callList: try: callable(*args, **kw) except: log.deferr() # now that we've called all callbacks, no need to store # references to them anymore, in fact this can cause problems. del self._eventTriggers[eventType]
def removePID(pidfile): if not pidfile: return try: os.unlink(pidfile) except OSError, e: if e.errno == errno.EACCES or e.errno == errno.EPERM: log.msg("Warning: No permission to delete pid file") else: log.msg("Failed to unlink PID file:") log.deferr()
def connectionLost(self, reason): """stdout closed. """ self.lostOutConnection = 1 abstract.FileDescriptor.connectionLost(self, reason) os.close(self.stdout) try: self.proto.outConnectionLost() except: log.deferr() self.maybeCallProcessEnded()
def _runQueryInTransaction(self, trans, *args, **kwargs): try: trans.execute(*args, **kwargs) if(trans.rowcount != 0): result = trans.fetchall() else: result = [] return result except: log.msg('Exception in SQL query %s'%args) log.deferr() raise
def read(self, sock): why = None w = self.watcher try: why = w.doRead() except: why = sys.exc_info()[1] log.msg('Error in %s.doRead()' % w) log.deferr() if why: self.reactor._disconnectSelectable(w, why, True) self.reactor.simulate()
def _beforeShutDown(self): l = [] services = self.services.values() for service in services: try: d = service.stopService() if isinstance(d, defer.Deferred): l.append(d) except: log.deferr() if l: return defer.DeferredList(l)
def connectionReady(self): """Initialize. Called after Banana negotiation is done. """ self.sendCall("version", self.version) for notifier in self.connects: try: notifier() except: log.deferr() self.connects = None if self.factory: # in tests we won't have factory self.factory.clientConnectionMade(self)
def connectionLost(self, reason): """The connection was lost. """ self.disconnected = 1 # nuke potential circular references. self.luids = None if self.waitingForAnswers: for d in self.waitingForAnswers.values(): try: d.errback(failure.Failure(PBConnectionLost(reason))) except: log.deferr() # Assure all Cacheable.stoppedObserving are called for lobj in self.remotelyCachedObjects.values(): cacheable = lobj.object perspective = lobj.perspective try: cacheable.stoppedObserving( perspective, RemoteCacheObserver(self, cacheable, perspective)) except: log.deferr() # Loop on a copy to prevent notifiers to mixup # the list by calling dontNotifyOnDisconnect for notifier in self.disconnects[:]: try: notifier() except: log.deferr() self.disconnects = None self.waitingForAnswers = None self.localSecurity = None self.remoteSecurity = None self.remotelyCachedObjects = None self.remotelyCachedLUIDs = None self.locallyCachedObjects = None self.localObjects = None
why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway('Filedescriptor went away') inRead = False except AttributeError, ae: if "'NoneType' object has no attribute 'writeHeaders'" not in ae.message: log.deferr() why = sys.exc_info()[1] else: why = None except: log.deferr() why = sys.exc_info()[1] if why: try: self._disconnectSelectable(selectable, why, inRead) except RuntimeError: pass def callLater(self, *args, **kwargs): poller.eApp.interruptPoll() return posixbase.PosixReactorBase.callLater(self, *args, **kwargs) def install(): """Install the poll() reactor.""" p = PollReactor()
def runUntilCurrent(self): """Run all pending timed calls. """ if self.threadCallQueue: # Keep track of how many calls we actually make, as we're # making them, in case another call is added to the queue # while we're in this loop. count = 0 total = len(self.threadCallQueue) for (f, a, kw) in self.threadCallQueue: try: f(*a, **kw) except: log.err() count += 1 if count == total: break del self.threadCallQueue[:count] if self.threadCallQueue: if self.waker: self.waker.wakeUp() # insert new delayed calls now self._insertNewDelayedCalls() now = self.seconds() while self._pendingTimedCalls and (self._pendingTimedCalls[0].time <= now): call = heappop(self._pendingTimedCalls) if call.cancelled: self._cancellations-=1 continue if call.delayed_time > 0: call.activate_delay() heappush(self._pendingTimedCalls, call) continue try: call.called = 1 call.func(*call.args, **call.kw) except: log.deferr() if hasattr(call, "creator"): e = "\n" e += " C: previous exception occurred in " + \ "a DelayedCall created here:\n" e += " C:" e += "".join(call.creator).rstrip().replace("\n","\n C:") e += "\n" log.msg(e) if (self._cancellations > 50 and self._cancellations > len(self._pendingTimedCalls) >> 1): self._cancellations = 0 self._pendingTimedCalls = [x for x in self._pendingTimedCalls if not x.cancelled] heapify(self._pendingTimedCalls) if self._justStopped: self._justStopped = False self.fireSystemEvent("shutdown")
def makeService(config): s = appservice.MultiService() conf = inetdconf.InetdConf() conf.parseFile(open(config['file'])) rpcConf = inetdconf.RPCServicesConf() try: rpcConf.parseFile(open(config['rpc'])) except: # We'll survive even if we can't read /etc/rpc log.deferr() for service in conf.services: rpc = service.protocol.startswith('rpc/') protocol = service.protocol if rpc and not rpcOk: log.msg('Skipping rpc service due to lack of rpc support') continue if rpc: # RPC has extra options, so extract that protocol = protocol[4:] # trim 'rpc/' if not protocolDict.has_key(protocol): log.msg('Bad protocol: ' + protocol) continue try: name, rpcVersions = service.name.split('/') except ValueError: log.msg('Bad RPC service/version: ' + service.name) continue if not rpcConf.services.has_key(name): log.msg('Unknown RPC service: ' + repr(service.name)) continue try: if '-' in rpcVersions: start, end = map(int, rpcVersions.split('-')) rpcVersions = range(start, end+1) else: rpcVersions = [int(rpcVersions)] except ValueError: log.msg('Bad RPC versions: ' + str(rpcVersions)) continue if (protocol, service.socketType) not in [('tcp', 'stream'), ('udp', 'dgram')]: log.msg('Skipping unsupported type/protocol: %s/%s' % (service.socketType, service.protocol)) continue # Convert the username into a uid (if necessary) try: service.user = int(service.user) except ValueError: try: service.user = pwd.getpwnam(service.user)[2] except KeyError: log.msg('Unknown user: '******'s primary group service.group = pwd.getpwuid(service.user)[3] else: try: service.group = int(service.group) except ValueError: try: service.group = grp.getgrnam(service.group)[2] except KeyError: log.msg('Unknown group: ' + service.group) continue if service.program == 'internal': if config['nointernal']: continue # Internal services can use a standard ServerFactory if not inetd.internalProtocols.has_key(service.name): log.msg('Unknown internal service: ' + service.name) continue factory = ServerFactory() factory.protocol = inetd.internalProtocols[service.name] elif rpc: i = RPCServer(rpcVersions, rpcConf, proto, service) i.setServiceParent(s) continue else: # Non-internal non-rpc services use InetdFactory factory = inetd.InetdFactory(service) if protocol == 'tcp': internet.TCPServer(service.port, factory).setServiceParent(s) elif protocol == 'udp': raise RuntimeError("not supporting UDP") return s
def doRead(self): """Called when my socket is ready for reading. This accepts a connection and calls self.protocol() to handle the wire-level protocol. """ try: if platformType == "posix": numAccepts = self.numberAccepts else: # win32 event loop breaks if we do more than one accept() # in an iteration of the event loop. numAccepts = 1 for i in range(numAccepts): # we need this so we can deal with a factory's buildProtocol # calling our loseConnection if self.disconnecting: return try: skt, addr = self.socket.accept() except socket.error as e: if e.args[0] in (EWOULDBLOCK, EAGAIN): self.numberAccepts = i break elif e.args[0] == EPERM: # Netfilter on Linux may have rejected the # connection, but we get told to try to accept() # anyway. continue elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED): # Linux gives EMFILE when a process is not allowed to # allocate any more file descriptors. *BSD and Win32 # give (WSA)ENOBUFS. Linux can also give ENFILE if the # system is out of inodes, or ENOMEM if there is # insufficient memory to allocate a new dentry. # ECONNABORTED is documented as possible on all # relevant platforms (Linux, Windows, macOS, and the # BSDs) but occurs only on the BSDs. It occurs when a # client sends a FIN or RST after the server sends a # SYN|ACK but before application code calls accept(2). # On Linux, calling accept(2) on such a listener # returns a connection that fails as though the it were # terminated after being fully established. This # appears to be an implementation choice (see # inet_accept in inet/ipv4/af_inet.c). On macOS X, # such a listener is not considered readable, so # accept(2) will never be called. Calling accept(2) on # such a listener, however, does not return at all. log.msg("Could not accept new connection (%s)" % ( errorcode[e.args[0]],)) break raise fdesc._setCloseOnExec(skt.fileno()) protocol = self.factory.buildProtocol(self._buildAddr(addr)) if protocol is None: skt.close() continue s = self.sessionno self.sessionno = s+1 transport = self.transport(skt, protocol, addr, self, s, self.reactor) protocol.makeConnection(transport) else: self.numberAccepts = self.numberAccepts+20 except: # Note that in TLS mode, this will possibly catch SSL.Errors # raised by self.socket.accept() # # There is no "except SSL.Error:" above because SSL may be # None if there is no SSL support. In any case, all the # "except SSL.Error:" suite would probably do is log.deferr() # and return, so handling it here works just as well. log.deferr()
def doRead(self): """Called when my socket is ready for reading. This accepts a connection and calls self.protocol() to handle the wire-level protocol. """ try: if platformType == "posix": numAccepts = self.numberAccepts else: # win32 event loop breaks if we do more than one accept() # in an iteration of the event loop. numAccepts = 1 for i in range(numAccepts): # we need this so we can deal with a factory's buildProtocol # calling our loseConnection if self.disconnecting: return try: skt, addr = self.socket.accept() except socket.error as e: if e.args[0] in (EWOULDBLOCK, EAGAIN): self.numberAccepts = i break elif e.args[0] == EPERM: # Netfilter on Linux may have rejected the # connection, but we get told to try to accept() # anyway. continue elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED): # Linux gives EMFILE when a process is not allowed # to allocate any more file descriptors. *BSD and # Win32 give (WSA)ENOBUFS. Linux can also give # ENFILE if the system is out of inodes, or ENOMEM # if there is insufficient memory to allocate a new # dentry. ECONNABORTED is documented as possible on # both Linux and Windows, but it is not clear # whether there are actually any circumstances under # which it can happen (one might expect it to be # possible if a client sends a FIN or RST after the # server sends a SYN|ACK but before application code # calls accept(2), however at least on Linux this # _seems_ to be short-circuited by syncookies. log.msg("Could not accept new connection (%s)" % (errorcode[e.args[0]], )) break raise fdesc._setCloseOnExec(skt.fileno()) protocol = self.factory.buildProtocol(self._buildAddr(addr)) if protocol is None: skt.close() continue s = self.sessionno self.sessionno = s + 1 transport = self.transport(skt, protocol, addr, self, s, self.reactor) protocol.makeConnection(transport) else: self.numberAccepts = self.numberAccepts + 20 except: # Note that in TLS mode, this will possibly catch SSL.Errors # raised by self.socket.accept() # # There is no "except SSL.Error:" above because SSL may be # None if there is no SSL support. In any case, all the # "except SSL.Error:" suite would probably do is log.deferr() # and return, so handling it here works just as well. log.deferr()