def startApplication(self, application): """ Configure global process state based on the given application and run the application. @param application: An object which can be adapted to L{service.IProcess} and L{service.IService}. """ process = service.IProcess(application) if not self.config['originalname']: launchWithName(process.processName) self.setupEnvironment(self.config['chroot'], self.config['rundir'], self.config['nodaemon'], self.config['umask'], self.config['pidfile']) service.IService(application).privilegedStartService() uid, gid = self.config['uid'], self.config['gid'] if uid is None: uid = process.uid if gid is None: gid = process.gid if uid is not None and gid is None: gid = pwd.getpwuid(uid).pw_gid self.shedPrivileges(self.config['euid'], uid, gid) app.startApplication(application, not self.config['no_save'])
def startTwisted(application, startDir='./', nodaemon=0, logfile=None, rundir='.', appname='tums', pidfile='/var/run/tums.pid'): """ A freezable twistd bootstrap layer """ from twisted.application import service, internet, strports, app from twisted.python import log, syslog try: from twisted.scripts import _twistd_unix as twistd except: from twisted.scripts import twistd config = { 'profile': None, 'reactor': None, 'encrypted': 0, 'syslog': 0, 'prefix': appname, 'report-profile': None, 'euid': 0, 'file': 'twistd.tap', 'originalname': appname, 'rundir': rundir, 'logfile': logfile, 'nodaemon': nodaemon, 'uid': None, 'xml': None, 'chroot': None, 'no_save': True, 'quiet': 0, 'source': None, 'nothotshot': 0, 'gid': None, 'savestats': 0, 'debug': False, 'pidfile': pidfile } twistd.checkPID(config['pidfile']) #app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr twistd.startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) app.initialLog() twistd.startApplication(config, application) app.runReactorWithLogging(config, oldstdout, oldstderr) twistd.removePID(config['pidfile']) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def postOptions(self): # This does not invoke the super implementation. At the time this # method was implemented, all the super method did was *conditionally* # set self['no_save'] to True and take the abspath of self['pidfile']. # See below for the irrelevance of those operations. self._fixConfig() self._checkPID() S = self.parent.getStore() # make sure we open it here oldstdout = sys.stdout oldstderr = sys.stderr self._startLogging() app.initialLog() self.application = application = self._constructApplication() self._startApplication() app.runReactorWithLogging(self, oldstdout, oldstderr) self._removePID() app.reportProfile(self['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def convert(oldApp): '''Convert an C{i.app.Application} to a C{application.service.Application} @type oldApp: C{twisted.internet.app.Application} @rtype C{twisted.application.service.Application} This function might damage oldApp beyond repair: services that other parts might be depending on might be missing. It is not safe to use oldApp after it has been converted. In case this behaviour is not desirable, pass a deep copy of the old application ''' ret = service.Application(oldApp.name, getattr(oldApp, "uid", None), getattr(oldApp, "gid", None)) c = service.IServiceCollection(ret) service.IProcess(ret).processName = oldApp.processName for (pList, klass) in [(oldApp.extraPorts, internet.GenericServer), (oldApp.extraConnectors, internet.GenericClient),]: for (portType, args, kw) in pList: klass(portType, *args, **kw).setServiceParent(c) for (name, klass) in _mapping: for args in getattr(oldApp, name): klass(*args).setServiceParent(c) for s in c: if hasattr(s, 'privileged'): s.privileged = 1 for s in oldApp.services.values(): if not service.IService.providedBy(s): s.serviceParent = None s = _NewService(s) s.setServiceParent(IOldApplication(c)) else: s.serviceParent = None s.setServiceParent(c) return ret
def postApplication(self): """ Start the application and run the reactor. """ service.IService(self.application).privilegedStartService() app.startApplication(self.application, not self.config['no_save']) app.startApplication(internet.TimerService(0.1, lambda: None), 0) app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr) app.reportProfile(self.config['report-profile'], service.IProcess(self.application).processName) log.msg("Server Shut Down.")
def postApplication(self): """ To be called after the application is created: start the application and run the reactor. After the reactor stops, clean up PID files and such. """ startApplication(self.config, self.application) app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr) removePID(self.config['pidfile']) app.reportProfile(self.config['report-profile'], service.IProcess(self.application).processName) log.msg("Server Shut Down.")
def addToApplication(ser, name, append, procname, type, encrypted, uid, gid): if append and os.path.exists(append): a = service.loadApplication(append, 'pickle', None) else: a = service.Application(name, uid, gid) if procname: service.IProcess(a).processName = procname ser.setServiceParent(service.IServiceCollection(a)) sob.IPersistable(a).setStyle(type) passphrase = app.getSavePassphrase(encrypted) if passphrase: append = None sob.IPersistable(a).save(filename=append, passphrase=passphrase)
def testSimpleInternet(self): # XXX - replace this test with one that does the same thing, but # with no web dependencies. if not gotMicrodom: raise unittest.SkipTest("Need twisted.web to run this test.") s = "(dp0\nS'udpConnectors'\np1\n(lp2\nsS'unixConnectors'\np3\n(lp4\nsS'twisted.internet.app.Application.persistenceVersion'\np5\nI12\nsS'name'\np6\nS'web'\np7\nsS'sslConnectors'\np8\n(lp9\nsS'sslPorts'\np10\n(lp11\nsS'tcpPorts'\np12\n(lp13\n(I8080\n(itwisted.web.server\nSite\np14\n(dp16\nS'resource'\np17\n(itwisted.web.demo\nTest\np18\n(dp19\nS'files'\np20\n(lp21\nsS'paths'\np22\n(dp23\nsS'tmpl'\np24\n(lp25\nS'\\n Congratulations, twisted.web appears to work!\\n <ul>\\n <li>Funky Form:\\n '\np26\naS'self.funkyForm()'\np27\naS'\\n <li>Exception Handling:\\n '\np28\naS'self.raiseHell()'\np29\naS'\\n </ul>\\n '\np30\nasS'widgets'\np31\n(dp32\nsS'variables'\np33\n(dp34\nsS'modules'\np35\n(lp36\nsS'children'\np37\n(dp38\nsbsS'logPath'\np39\nNsS'timeOut'\np40\nI43200\nsS'sessions'\np41\n(dp42\nsbI5\nS''\np43\ntp44\nasS'unixPorts'\np45\n(lp46\nsS'services'\np47\n(dp48\nsS'gid'\np49\nI1000\nsS'tcpConnectors'\np50\n(lp51\nsS'extraConnectors'\np52\n(lp53\nsS'udpPorts'\np54\n(lp55\nsS'extraPorts'\np56\n(lp57\nsS'persistStyle'\np58\nS'pickle'\np59\nsS'uid'\np60\nI1000\ns." d = pickle.loads(s) a = Dummy() a.__dict__ = d appl = compat.convert(a) self.assertEqual(service.IProcess(appl).uid, 1000) self.assertEqual(service.IProcess(appl).gid, 1000) self.assertEqual(service.IService(appl).name, "web") services = list(service.IServiceCollection(appl)) self.assertEqual(len(services), 1) s = services[0] self.assertEqual(s.parent, service.IServiceCollection(appl)) self.assert_(s.privileged) self.assert_(isinstance(s, internet.TCPServer)) args = s.args self.assertEqual(args[0], 8080) self.assertEqual(args[3], '')
def testSimpleUNIX(self): # XXX - replace this test with one that does the same thing, but # with no web dependencies. if not interfaces.IReactorUNIX(reactor, None): raise unittest.SkipTest, "This reactor does not support UNIX domain sockets" if not gotMicrodom: raise unittest.SkipTest("Need twisted.web to run this test.") s = "(dp0\nS'udpConnectors'\np1\n(lp2\nsS'unixConnectors'\np3\n(lp4\nsS'twisted.internet.app.Application.persistenceVersion'\np5\nI12\nsS'name'\np6\nS'web'\np7\nsS'sslConnectors'\np8\n(lp9\nsS'sslPorts'\np10\n(lp11\nsS'tcpPorts'\np12\n(lp13\nsS'unixPorts'\np14\n(lp15\n(S'/home/moshez/.twistd-web-pb'\np16\n(itwisted.spread.pb\nBrokerFactory\np17\n(dp19\nS'objectToBroker'\np20\n(itwisted.web.distrib\nResourcePublisher\np21\n(dp22\nS'twisted.web.distrib.ResourcePublisher.persistenceVersion'\np23\nI2\nsS'site'\np24\n(itwisted.web.server\nSite\np25\n(dp26\nS'resource'\np27\n(itwisted.web.static\nFile\np28\n(dp29\nS'ignoredExts'\np30\n(lp31\nsS'defaultType'\np32\nS'text/html'\np33\nsS'registry'\np34\n(itwisted.web.static\nRegistry\np35\n(dp36\nS'twisted.web.static.Registry.persistenceVersion'\np37\nI1\nsS'twisted.python.components.Componentized.persistenceVersion'\np38\nI1\nsS'_pathCache'\np39\n(dp40\nsS'_adapterCache'\np41\n(dp42\nS'twisted.internet.interfaces.IServiceCollection'\np43\n(itwisted.internet.app\nApplication\np44\n(dp45\ng1\ng2\nsg3\ng4\nsg5\nI12\nsg6\ng7\nsg8\ng9\nsg10\ng11\nsg12\ng13\nsg14\ng15\nsS'extraPorts'\np46\n(lp47\nsS'gid'\np48\nI1053\nsS'tcpConnectors'\np49\n(lp50\nsS'extraConnectors'\np51\n(lp52\nsS'udpPorts'\np53\n(lp54\nsS'services'\np55\n(dp56\nsS'persistStyle'\np57\nS'pickle'\np58\nsS'delayeds'\np59\n(lp60\nsS'uid'\np61\nI1053\nsbssbsS'encoding'\np62\nNsS'twisted.web.static.File.persistenceVersion'\np63\nI6\nsS'path'\np64\nS'/home/moshez/public_html.twistd'\np65\nsS'type'\np66\ng33\nsS'children'\np67\n(dp68\nsS'processors'\np69\n(dp70\nS'.php3'\np71\nctwisted.web.twcgi\nPHP3Script\np72\nsS'.rpy'\np73\nctwisted.web.script\nResourceScript\np74\nsS'.php'\np75\nctwisted.web.twcgi\nPHPScript\np76\nsS'.cgi'\np77\nctwisted.web.twcgi\nCGIScript\np78\nsS'.epy'\np79\nctwisted.web.script\nPythonScript\np80\nsS'.trp'\np81\nctwisted.web.trp\nResourceUnpickler\np82\nssbsS'logPath'\np83\nNsS'sessions'\np84\n(dp85\nsbsbsS'twisted.spread.pb.BrokerFactory.persistenceVersion'\np86\nI3\nsbI5\nI438\ntp87\nasg55\ng56\nsg48\nI1053\nsg49\ng50\nsg51\ng52\nsg53\ng54\nsg46\ng47\nsg57\ng58\nsg61\nI1053\nsg59\ng60\ns." d = pickle.loads(s) a = Dummy() a.__dict__ = d appl = compat.convert(a) self.assertEqual(service.IProcess(appl).uid, 1053) self.assertEqual(service.IProcess(appl).gid, 1053) self.assertEqual(service.IService(appl).name, "web") services = list(service.IServiceCollection(appl)) self.assertEqual(len(services), 1) s = services[0] self.assertEqual(s.parent, service.IServiceCollection(appl)) self.assert_(s.privileged) self.assert_(isinstance(s, internet.UNIXServer)) args = s.args self.assertEqual(args[0], '/home/moshez/.twistd-web-pb')
def startApplication(config, application): process = service.IProcess(application, None) if not config['originalname']: launchWithName(process.processName) setupEnvironment(config) service.IService(application).privilegedStartService() uid, gid = mktap.getid(config['uid'], config['gid']) if uid is None: uid = process.uid if gid is None: gid = process.gid shedPrivileges(config['euid'], uid, gid) app.startApplication(application, not config['no_save'])
def testProcessComponent(self): a = service.Application("hello") self.assertEqual(service.IProcess(a).uid, None) self.assertEqual(service.IProcess(a).gid, None) a = service.Application("hello", 5) self.assertEqual(service.IProcess(a).uid, 5) self.assertEqual(service.IProcess(a).gid, None) a = service.Application("hello", 5, 6) self.assertEqual(service.IProcess(a).uid, 5) self.assertEqual(service.IProcess(a).gid, 6)
def runApp(config): passphrase = app.getPassphrase(config['encrypted']) app.installReactor(config['reactor']) application = app.getApplication(config, passphrase) oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config['logfile']) app.initialLog() os.chdir(config['rundir']) service.IService(application).privilegedStartService() app.startApplication(application, not config['no_save']) app.startApplication(internet.TimerService(0.1, lambda: None), 0) app.runReactorWithLogging(config, oldstdout, oldstderr) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def runApp(config): checkPID(config['pidfile']) passphrase = app.getPassphrase(config['encrypted']) app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) app.initialLog() application = app.getApplication(config, passphrase) startApplication(config, application) app.runReactorWithLogging(config, oldstdout, oldstderr) removePID(config['pidfile']) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
sys.excepthook = MailException t2w = Tor2web(config) rexp = { 'body': re.compile(r'(<body.*?\s*>)', re.I), 'w2t': re.compile(r'(https:)?//([a-z0-9]{16}).' + config.basehost + '(:443)?', re.I), 't2w': re.compile(r'(http:)?//([a-z0-9]{16}).onion(:80)?', re.I) } application = service.Application("Tor2web") service.IProcess(application).processName = "tor2web" class T2WLogObserver(log.FileLogObserver): """Custom Logging observer""" def emit(self, eventDict): """Custom emit for FileLogObserver""" log.FileLogObserver.emit(self, eventDict) if 'failure' in eventDict: vf = eventDict['failure'] e_t, e_v, e_tb = vf.type, vf.value, vf.getTracebackObject() sys.excepthook(e_t, e_v, e_tb) if config.debugmode:
def startTwisted(application, startDir='./', nodaemon=0, logfile=None, rundir='.', appname='enamel', pidfile='/var/run/enamel.pid'): """ A freezable twistd bootstrap layer """ config = { 'profile': None, 'reactor': None, 'encrypted': 0, 'syslog': None, 'prefix': appname, 'report-profile': None, 'euid': 0, 'file': 'twistd.tap', 'originalname': appname, 'rundir': rundir, 'logfile': logfile, 'nodaemon': nodaemon, 'uid': None, 'xml': None, 'chroot': None, 'no_save': True, 'quiet': 0, 'source': None, 'nothotshot': 0, 'gid': None, 'savestats': 0, 'debug': False, 'pidfile': pidfile, 'umask': None, } #application = compat.convert(application) twistd.checkPID(config['pidfile']) #app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr try: twistd.startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) passDeprecate = False except AttributeError: passDeprecate = True if config['nodaemon']: config['logfile'] = "-" #if not passDeprecate: #app.initialLog() try: twistd.startApplication(config, application) print "twisted 2.5" except AttributeError: print "wt?" # Use hotshot in Twisted 8.x config['profiler'] = 'hotshot' AR = twistd.UnixApplicationRunner(config) AR.startApplication(application) AR.logger.start(application) app.runReactorWithLogging(config, oldstdout, oldstderr) try: twistd.removePID(config['pidfile']) except AttributeError: AR.removePID(config['pidfile']) if not passDeprecate: app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")