³ò
îH°Ic           @   sW   d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k l Z d d d „  ƒ  YZ d S(   iÿÿÿÿN(   t   SIGTERMt   Daemonc           B   sV   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(	   s]   
	A generic daemon class.
	
	Usage: subclass the Daemon class and override the run() method
	s	   /dev/nullc         C   s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   stdint   stdoutt   stderrt   pidfile(   t   selfR   R   R   R   (    (    s	   daemon.pyt   __init__   s    			c      
   C   sý  y. t  i ƒ  } | d j o t i d ƒ n WnB t j
 o6 } t i i d | i | i f ƒ t i d ƒ n Xt  i	 d ƒ t  i
 ƒ  t  i d ƒ y. t  i ƒ  } | d j o t i d ƒ n WnB t j
 o6 } t i i d | i | i f ƒ t i d ƒ n Xt i i ƒ  t i i ƒ  t |  i d ƒ } t |  i d ƒ } t |  i d d ƒ } t  i | i ƒ  t i i ƒ  ƒ t  i | i ƒ  t i i ƒ  ƒ t  i | i ƒ  t i i ƒ  ƒ t i |  i ƒ t t  i ƒ  ƒ } t |  i d ƒ i d	 | ƒ d
 S(   sº   
		do the UNIX double-fork magic, see Stevens' "Advanced 
		Programming in the UNIX Environment" for details (ISBN 0201563177)
		http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
		i    s   fork #1 failed: %d (%s)
i   t   /s   fork #2 failed: %d (%s)
t   rs   a+s   w+s   %s
N(   t   ost   forkt   syst   exitt   OSErrorR   t   writet   errnot   strerrort   chdirt   setsidt   umaskR   t   flusht   fileR   t   dup2t   filenot   atexitt   registert   delpidt   strt   getpidR   (   R   t   pidt   et   sit   sot   se(    (    s	   daemon.pyt	   daemonize   s8     
 c         C   s   t  i |  i ƒ d  S(   N(   R
   t   removeR   (   R   (    (    s	   daemon.pyR   ?   s    c         C   s¡   y8 t  |  i d ƒ } t | i ƒ  i ƒ  ƒ } | i ƒ  Wn t j
 o d } n X| o. d } t i	 i
 | |  i ƒ t i d ƒ n |  i ƒ  |  i ƒ  d S(   s   
		Start the daemon
		R	   s2   pidfile %s already exist. Daemon already running?
i   N(   R   R   t   intt   readt   stript   closet   IOErrort   NoneR   R   R   R   R#   t   run(   R   t   pfR   t   message(    (    s	   daemon.pyt   startB   s    
c      	   C   s+  y8 t  |  i d ƒ } t | i ƒ  i ƒ  ƒ } | i ƒ  Wn t j
 o d } n X| p% d } t i	 i
 | |  i ƒ d Sn y' x  t i | t ƒ t i d ƒ q† Wn} t j
 oq } t | ƒ } | i d ƒ d j o. t i i |  i ƒ o t i |  i ƒ q"q't | ƒ GHt i d ƒ n Xd S(   s   
		Stop the daemon
		R	   s/   pidfile %s does not exist. Daemon not running?
Ngš™™™™™¹?s   No such processi    i   (   R   R   R%   R&   R'   R(   R)   R*   R   R   R   R
   t   killR    t   timet   sleepR   R   t   findt   patht   existsR$   R   (   R   R,   R   R-   t   err(    (    s	   daemon.pyt   stopW   s*    c         C   s   |  i  ƒ  |  i ƒ  d S(   s   
		Restart the daemon
		N(   R6   R.   (   R   (    (    s	   daemon.pyt   restartv   s    
c         C   s   d S(   s’   
		You should override this method when you subclass Daemon. It will be called after the process has been
		daemonized by start() or restart().
		N(    (   R   (    (    s	   daemon.pyR+   }   s    (
   t   __name__t
   __module__t   __doc__R   R#   R   R.   R6   R7   R+   (    (    (    s	   daemon.pyR      s   	-				(    (   R   R
   R0   R   t   signalR    R   (    (    (    s	   daemon.pys   <module>   s   0