ó
[³XMc           @   sõ   d  d l  Z  d  d l Z d  d l Z d  d l m Z m Z m Z d  d l m Z m	 Z	 m
 Z
 d  d l m Z m Z d  d l m Z d „  Z d e j f d „  ƒ  YZ d	 „  Z d
 e j f d „  ƒ  YZ d „  Z d „  Z d e j f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   logt   syslogt   logfile(   t	   switchUIDt   uidFromStringt   gidFromString(   t   appt   service(   t	   copyrightc         C   s   t  |  d ƒ S(   Ni   (   t   int(   t   value(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   _umask   s    t   ServerOptionsc        
   B   sô   e  Z d  Z d d d g d d" d g d d" d g d d	 d
 g g Z d d" d d g d d	 d d g d d" d" d g d d d" d e g d d d" d e g d d" d" d e g g Z i d d 6d d 6Z	 i d d 6d d 6Z
 i d d 6Z d  „  Z d! „  Z RS(#   s   Usage: twistd [options]t   nodaemont   ns0   don't daemonize, don't use default umask of 0077t   originalnames$   Don't try to change the process nameR   s   Log to syslog, not to filet   euidt    s  Set only effective user-id rather than real user-id. (This option has no effect unless the server is running as root, in which case it means not to shed all privileges after binding ports, retaining the option to regain privileges in cases such as spawning processes. Use with caution.)t   prefixt   twisteds$   use the given prefix when sysloggingt   pidfiles
   twistd.pids   Name of the pidfilet   chroots-   Chroot to a supplied directory before runningt   uidt   us   The uid to run as.t   gidt   gs   The gid to run as.t   umasks(   The (octal) file creation mask to apply.s7   Use the given prefix when syslogging (default: twisted)s)   Name of the pidfile (default: twistd.pid)s   _files -g "*.pid"t   _dirss   chroot directoryc         C   s"   d t  j GHt  j  GHt j ƒ  d S(   s,   Print version information and exit.
        s   twistd (the Twisted daemon) %sN(   R   t   versiont   syst   exit(   t   self(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   opt_version3   s    c         C   s;   t  j j |  ƒ |  d r7 t j j |  d ƒ |  d <n  d  S(   NR   (   R   R   t   postOptionst   ost   patht   abspath(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyR!   ;   s    
N(   t   __name__t
   __module__t   synopsist   Nonet   optFlagsR   R   R   t   optParameterst   zsh_altArgDescrt   zsh_actionst   zsh_actionDescrR    R!   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyR      s,   					

	c         C   s÷   |  s
 d  St  j j |  ƒ ró y t t |  ƒ j ƒ  ƒ } Wn" t k
 r\ t j d |  ƒ n Xy t  j	 | d ƒ Wnk t
 k
 rÞ } | d t j k r½ t j d |  d t ƒt  j |  ƒ qð t j d | |  | d f ƒ qó Xt j d | ƒ n  d  S(   Ns%   Pidfile %s contains non-numeric valuei    s   Removing stale pidfile %st   isErrors0   Can't check status of PID %s from pidfile %s: %si   s  Another twistd server is running, PID %s

This could either be a previously started instance of your application or a
different application entirely. To start a new one, either run it in some other
directory, or use the --pidfile and --logfile parameters to avoid clashes.
(   R"   R#   t   existsR	   t   opent   readt
   ValueErrorR   R   t   killt   OSErrort   errnot   ESRCHR    t   msgt   Truet   remove(   R   t   pidt   why(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   checkPIDA   s"    	t   UnixAppLoggerc           B   s    e  Z d  Z d „  Z d „  Z RS(   s­  
    A logger able to log to syslog, to files, and to stdout.

    @ivar _syslog: A flag indicating whether to use syslog instead of file
        logging.
    @type _syslog: C{bool}

    @ivar _syslogPrefix: If C{sysLog} is C{True}, the string prefix to use for
        syslog messages.
    @type _syslogPrefix: C{str}

    @ivar _nodaemon: A flag indicating the process will not be daemonizing.
    @type _nodaemon: C{bool}
    c         C   sV   t  j j |  | ƒ | j d t ƒ |  _ | j d d ƒ |  _ | j d t ƒ |  _ d  S(   NR   R   R   R   (   R   t	   AppLoggert   __init__t   gett   Falset   _syslogt   _syslogPrefixt	   _nodaemon(   R   t   options(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyR?   m   s    c            s  |  j  r t j |  j ƒ j S|  j d k rP |  j sD t j d ƒ n  t j	 ‰  n¤ |  j ro |  j ro t j	 ‰  n… |  j s„ d |  _ n  t
 j j |  j ƒ ‰  y d d l } Wn t k
 r¼ n8 X| j | j ƒ sô ‡  f d †  } | j | j | ƒ n  t j ˆ  ƒ j S(   s™  
        Create and return a suitable log observer for the given configuration.

        The observer will go to syslog using the prefix C{_syslogPrefix} if
        C{_syslog} is true.  Otherwise, it will go to the file named
        C{_logfilename} or, if C{_nodaemon} is true and C{_logfilename} is
        C{"-"}, to stdout.

        @return: An object suitable to be passed to C{log.addObserver}.
        t   -s&   Daemons cannot log to stdout, exiting!s
   twistd.logiÿÿÿÿNc            s$   d d l  m } | j ˆ  j ƒ d  S(   Niÿÿÿÿ(   t   reactor(   t   twisted.internetRG   t   callFromThreadt   rotate(   t   signalt   frameRG   (   t   logFile(    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt	   rotateLog“   s    (   RB   R   t   SyslogObserverRC   t   emitt   _logfilenameRD   R   R   t   stdoutR   t   LogFilet   fromFullPathRK   t   ImportErrort	   getsignalt   SIGUSR1R    t   FileLogObserver(   R   RK   RN   (    (   RM   s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   _getLogObservert   s&    			(   R%   R&   t   __doc__R?   RY   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyR=   ]   s   	c          C   sÁ   t  j ƒ  r t  j d ƒ n  t  j ƒ  t  j ƒ  rB t  j d ƒ n  t  j d t  j ƒ }  xV t d ƒ D]H } y t  j |  | ƒ Wqd t k
 r« } | j	 t	 j
 k r¬ ‚  q¬ qd Xqd Wt  j |  ƒ d  S(   Ni    s	   /dev/nulli   (   R"   t   forkt   _exitt   setsidR0   t   O_RDWRt   ranget   dup2R4   R5   t   EBADFt   close(   t   nullt   it   e(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt	   daemonize›   s    
c         C   sq   |  rm |  t  j d k rm t j j t  j ƒ } t j d |  ƒ t j | |  t  j d d g t  j d ƒ n  d  S(   Ni    s   Changing process name to s   --originalnamei   (	   R   t   argvR"   R#   t   realpatht
   executableR    R7   t   execv(   t   namet   exe(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   launchWithName­   s    t   UnixApplicationRunnerc           B   sJ   e  Z d  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   sx   
    An ApplicationRunner which does Unix-specific things, like fork,
    shed privileges, and maintain a PID file.
    c         C   sN   t  |  j d ƒ |  j d p( |  j d |  j d <t j |  _ t j |  _ d S(   s4   
        Do pre-application-creation setup.
        R   R   t   debugN(   R<   t   configR   RR   t	   oldstdoutt   stderrt	   oldstderr(   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   preApplication¼   s
    c         C   sA   |  j  |  j ƒ |  j d |  j |  j ƒ |  j |  j d ƒ d S(   s±   
        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.
        R   N(   t   startApplicationt   applicationt   startReactorR(   Rq   Rs   t	   removePIDRp   (   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   postApplicationÇ   s    c         C   s   | s
 d Sy t  j | ƒ Wnm t k
 rt } | j t j k sQ | j t j k ra t j d ƒ q‹ t j | d ƒ n t j d d ƒ n Xd S(   s¿   
        Remove the specified PID file, if possible.  Errors are logged, not
        raised.

        @type pidfile: C{str}
        @param pidfile: The path to the PID tracking file.
        Ns)   Warning: No permission to delete pid files   Failed to unlink PID file(
   R"   t   unlinkR4   R5   t   EACCESt   EPERMR    R7   t   errR(   (   R   R   Re   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyRx   Ò   s    $c         C   sË   | } | d k	 r8 t j | ƒ | d k r8 d } q8 n  t j | ƒ | r` | d k r` d } n  | d k	 r| t j | ƒ n  | rŒ t ƒ  n  | rÇ t | d ƒ } | j t t j	 ƒ  ƒ ƒ | j
 ƒ  n  d S(   sæ  
        Set the filesystem root, the working directory, and daemonize.

        @type chroot: C{str} or L{NoneType}
        @param chroot: If not None, a path to use as the filesystem root (using
            L{os.chroot}).

        @type rundir: C{str}
        @param rundir: The path to set as the working directory.

        @type nodaemon: C{bool}
        @param nodaemon: A flag which, if set, indicates that daemonization
            should not be done.

        @type umask: C{int} or L{NoneType}
        @param umask: The value to which to change the process umask.

        @type pidfile: C{str} or L{NoneType}
        @param pidfile: If not C{None}, the path to a file into which to put
            the PID of this process.
        t   .t   /i?   t   wbN(   R(   R"   R   t   chdirR   Rf   R0   t   writet   strt   getpidRb   (   R   R   t   rundirR   R   R   t   daemont   f(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   setupEnvironmentç   s     	
c         C   sž   | d k	 s | d k	 rš | r$ d p' d } d | | | | f } y t | | | ƒ Wn/ t k
 r… t j d | ƒ t j d ƒ qš Xt j d | ƒ n  d S(   s¶  
        Change the UID and GID or the EUID and EGID of this process.

        @type euid: C{bool}
        @param euid: A flag which, if set, indicates that only the I{effective}
            UID and GID should be set.

        @type uid: C{int} or C{NoneType}
        @param uid: If not C{None}, the UID to which to switch.

        @type gid: C{int} or C{NoneType}
        @param gid: If not C{None}, the GID to which to switch.
        Re   R   s   %suid/%sgid %s/%ss,   failed to set %s (are you root?) -- exiting.i   s   set %sN(   R(   R   R4   R    R7   R   R   (   R   R   R   R   t   extrat   desc(    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   shedPrivileges  s    c         C   sü   t  j | ƒ } |  j d s, t | j ƒ n  |  j |  j d |  j d |  j d |  j d |  j d ƒ t  j | ƒ j ƒ  |  j d |  j d } } | d k r® | j	 } n  | d k rÆ | j
 } n  |  j |  j d	 | | ƒ t j | |  j d
 ƒ d S(   sè   
        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}.
        R   R   R…   R   R   R   R   R   R   t   no_saveN(   R   t   IProcessRp   Rm   t   processNameRˆ   t   IServicet   privilegedStartServiceR(   R   R   R‹   R   Ru   (   R   Rv   t   processR   R   (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyRu   *  s    (   R%   R&   RZ   R=   t   loggerFactoryRt   Ry   Rx   Rˆ   R‹   Ru   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyRn   µ   s   				)	(   R"   R5   R   t   twisted.pythonR    R   R   t   twisted.python.utilR   R   R   t   twisted.applicationR   R   R   R   R   R   R<   R>   R=   Rf   Rm   t   ApplicationRunnerRn   (    (    (    s@   /usr/lib/python2.7/dist-packages/twisted/scripts/_twistd_unix.pyt   <module>   s   $	0	>		