etwist/server.py
changeset 3199 fc63b80ec979
parent 3185 bd0126d17e83
parent 3198 d2f48d30e73e
child 3503 06bced8edddf
equal deleted inserted replaced
3195:a6f1daddfe8a 3199:fc63b80ec979
    27                           ExplicitLogin)
    27                           ExplicitLogin)
    28 from cubicweb.web.application import CubicWebPublisher
    28 from cubicweb.web.application import CubicWebPublisher
    29 
    29 
    30 from cubicweb.etwist.request import CubicWebTwistedRequestAdapter
    30 from cubicweb.etwist.request import CubicWebTwistedRequestAdapter
    31 
    31 
    32 def daemonize(uid):
    32 def daemonize():
    33     # XXX unix specific
    33     # XXX unix specific
    34     # XXX factorize w/ code in cw.server.server and cw.server.serverctl
    34     # XXX factorize w/ code in cw.server.server and cw.server.serverctl
    35     # (start-repository command)
    35     # (start-repository command)
    36     if uid is not None:
       
    37         try:
       
    38             uid = int(uid)
       
    39         except ValueError:
       
    40             from pwd import getpwnam
       
    41             uid = getpwnam(uid).pw_uid
       
    42         os.setuid(uid)
       
    43     # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
    36     # See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
    44     if os.fork():   # launch child and...
    37     if os.fork():   # launch child and...
    45         return -1
    38         return -1
    46     os.setsid()
    39     os.setsid()
    47     if os.fork():   # launch child and...
    40     if os.fork():   # launch child and...
   395     reactor.listenTCP(port, channel.HTTPFactory(website))
   388     reactor.listenTCP(port, channel.HTTPFactory(website))
   396     baseurl = config['base-url'] or config.default_base_url()
   389     baseurl = config['base-url'] or config.default_base_url()
   397     logger = getLogger('cubicweb.twisted')
   390     logger = getLogger('cubicweb.twisted')
   398     logger.info('instance started on %s', baseurl)
   391     logger.info('instance started on %s', baseurl)
   399     if not debug:
   392     if not debug:
   400         if daemonize(config['uid']):
   393         if daemonize():
   401             # child process
   394             # child process
   402             return
   395             return
   403         if config['pid-file']:
   396         if config['pid-file']:
   404             # ensure the directory where the pid-file should be set exists (for
   397             # ensure the directory where the pid-file should be set exists (for
   405             # instance /var/run/cubicweb may be deleted on computer restart)
   398             # instance /var/run/cubicweb may be deleted on computer restart)
   406             piddir = os.path.dirname(config['pid-file'])
   399             piddir = os.path.dirname(config['pid-file'])
   407             if not os.path.exists(piddir):
   400             if not os.path.exists(piddir):
   408                 os.makedirs(piddir)
   401                 os.makedirs(piddir)
   409             file(config['pid-file'], 'w').write(str(os.getpid()))
   402             file(config['pid-file'], 'w').write(str(os.getpid()))
       
   403     if config['uid'] is not None:
       
   404         try:
       
   405             uid = int(config['uid'])
       
   406         except ValueError:
       
   407             from pwd import getpwnam
       
   408             uid = getpwnam(config['uid']).pw_uid
       
   409         os.setuid(uid)
   410     if config['profile']:
   410     if config['profile']:
   411         prof = hotshot.Profile(config['profile'])
   411         prof = hotshot.Profile(config['profile'])
   412         prof.runcall(reactor.run)
   412         prof.runcall(reactor.run)
   413     else:
   413     else:
   414         reactor.run()
   414         reactor.run()