%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/utils/process.pyc

�
���^c@�s�dZddlmZmZmZmZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZddlm Z m!Z!ddl"m#Z#ej$e%�Z&e'Z(yddl)Z)e*Z(Wne+k
r�nXyddl,Z,e*Z-Wne+k
r�e'Z-nXd�Z.e*d	�Z/d
�Z0d�Z1d�Z2d
�Z3d�Z4d�Z5d�Z6dd�Z7d�Z8de9fd��YZ:de9fd��YZ;dej<efd��YZ=de=fd��YZ>ej?d��Z@de9fd��YZAdS(uE
Functions for daemonizing and otherwise modifying running processes
i(tabsolute_importtwith_statementtprint_functiontunicode_literalsN(tNewStyleClassMixIn(tsix(tqueuetrange(tgencC�s(tr$tjtj�d|�ndS(u4
    Append "name" to the current process title
    u N(tHAS_SETPROCTITLEtsetproctitletgetproctitle(tname((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytappendproctitle8scC�s�ddl}yEtj�}|dkrP|jjj�tj|jjj	�nWn?t
k
r�}tjd|j
|�tj|jjj�nXtjd�tj�tjd�yEtj�}|dkr�|jjj�tj|jjj	�nWn?t
k
r=}tjd|j
|�tj|jjj�nX|jjj�|r�|jjjdd��^}t|tj�t|tj�t|tj�t|d�t|d	�t|d
�WdQXndS(u
    Daemonize a process
    iNufork #1 failed: %s (%s)u/iufork #2 failed: %s (%s)u	/dev/nullur+ii(tsalt.utils.crypttostforktutilstcryptt
reinit_cryptot_exittdefaultst	exitcodestEX_OKtOSErrortlogterrorterrnotsystexitt
EX_GENERICtchdirtsetsidtumasktfilestfopentdup2tstdintstdouttstderr(tredirect_outtsalttpidtexctdev_null((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt	daemonize@s:




cC�s�t|t�r|}n8y|j�}Wn%tjk
rOtjd|�dSXt|t�rh|}n8y|j�}Wn%tjk
r�tjd|�dSXtj||�dS(u�
    Duplicate file descriptor fd to fd2, closing the latter first if necessary.
    This method is similar to os.dup2 but ignores streams that do not have a
    supported fileno method.
    u!Unsupported operation on file: %rN(	t
isinstancetinttfilenotiotUnsupportedOperationRtwarnRR$(tfile1tfile2tfno1tfno2((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR$rs		cC�sQdtjdkrdS|jdt�s-dStjjd�rCdStt�dS(u}
    Daemonize a module function process if multiprocessing is True and the
    process is not being called by salt-call
    u	salt-calliNumultiprocessinguwin(RtargvtgettTruetplatformt
startswithR-tFalse(topts((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytdaemonize_if�scC�sGtjd|gdtjdtj�}|j�|j�}|dkS(Nusystemd-notifyR&R'i(t
subprocesstPopentPIPEtcommunicatetpoll(tactiontprocesststatus((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytsystemd_notify_call�s'
cC�s)yddl}Wn�tk
r�tjjjd�r�td�r�tjd�}|r�|j	d�r{dj
|d�}nyFtjtjtj
�}|j|�|jd	j��|j�Wntjk
r�td
�SXtSntSX|jj�r%y|jjd	�SWq%tk
r!q%XndS(u6
    Notify systemd that this process has started
    iNusystemd-notifyu--bootedu
NOTIFY_SOCKETu@u{0}iuREADY=1u--ready(tsystemd.daemontImportErrorR)RtpathtwhichRHRtgetenvR<tformattsockettAF_UNIXt
SOCK_DGRAMtconnecttsendalltencodetcloseRR:R=tdaemontbootedtnotifytSystemError(tsystemdt
notify_sockettsock((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytnotify_systemd�s.


c
C�s�tjj|�}tjj|�r;|r;tj|�ny>tjjj|d�� }|j	t
tj���WdQXWntk
r�nXt
jd|�tjjj�r�tSddl}y'|j|�}|d}|d}WnFttfk
r.tjj	dj|��tjtjjj�nXtj�|krEdSytj|||�Wndtk
r�}dj||�}	t
jd	|	d
t�tjj	dj|	��tj|j �nXt
jd||�dS(
u
    Save the pidfile
    uw+NuCreated pidfile: %siiiu?Failed to set the pid to user: {0}. The user is not available.
u8Failed to set the ownership of PID file {0} to user {1}.u%s Traceback follows:texc_infou{0}
uChowned pidfile: %s to user: %s(!RRKtdirnametisdirtmakedirsR)RR"R#twritetstrtgetpidtIOErrorRtdebugR;t
is_windowsR:tpwdtgetpwnamtKeyErrort
IndexErrorRR'RNRRRt	EX_NOUSERtgetuidtchownRR(
tpidfiletusertpdirtofileRhtpwnamtuidtgidterrtmsg((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytset_pidfile�s@#

	
cC�stjj|�S(u5
    Determine if a pidfile has been written out
    (RRKtisfile(Ro((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt
check_pidfile�scC�scy>tjjj|��}|j�j�}WdQXt|�SWnttt	t
fk
r^dSXdS(u5
    Return the pid from a pidfile as an integer
    Ni����(R)RR"R#treadtstripR/RRet	TypeErrort
ValueError(RotpdfR*((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytget_pidfile�si
cC�s�|s
dSy�d}xu|j�r�|j�|d7}tjd�|j�r||krtjd|j�tj|jt	j
�qqWWnttfk
r�nXdS(u>
    Generic method for cleaning up multiprocessing procs
    Niig�������?u(Process did not die with terminate(): %s(
tis_alivet	terminatettimetsleepRRR*RtkilltsignaltSIGKILLtAssertionErrortAttributeError(tproct
wait_for_killtwaited((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt
clean_procs


!cC�set|tj�r!t|�}ntr4tj|�Sytj|d�t	SWnt
k
r`tSXdS(u@
    Use OS facilities to determine if a process is running
    iN(R.Rtstring_typesR/t
HAS_PSUTILtpsutilt
pid_existsRR�R:RR=(R*((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt
os_is_runnings

t
ThreadPoolcB�s5eZdZddd�Zddd�Zd�ZRS(u%
    This is a very VERY basic threadpool implementation
    This was made instead of using multiprocessing ThreadPool because
    we want to set max queue size and we want to daemonize threads (neither
    is exposed in the stdlib version).

    Since there isn't much use for this class as of right now this implementation
    Only supports daemonized threads and will *not* return results

    TODO: if this is found to be more generally useful it would be nice to pull
    in the majority of code from upstream or from http://bit.ly/1wTeJtM
    icC�s�|dkrtj�}n||_tj|�|_g|_xLt|�D]>}t	j
d|j�}t|_
|j�|jj|�qLWdS(Nttarget(tNonetmultiprocessingt	cpu_counttnum_threadsRtQueuet
_job_queuet_workersRt	threadingtThreadt_thread_targetR:RVtstarttappend(tselfR�t
queue_sizet_tthread((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt__init__;s			
cC�sg|dkrg}n|dkr*i}ny!|jj|||f�tSWntjk
rbtSXdS(N(R�R�t
put_nowaitR:RtFullR=(R�tfunctargstkwargs((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt
fire_asyncRs		cC�s�x�tr�yMy/|jjdd�\}}}|jj�Wntjk
rTwnXWntk
rlqnXy'tjd|||�|||�Wqt	k
r�}tj|dt�qXqWdS(Nttimeoutiu4ThreadPool executing func: %s with args=%s kwargs=%sR^(
R:R�R9t	task_doneRtEmptyR�RRft	Exception(R�R�R�R�Rv((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�]s 	

N(t__name__t
__module__t__doc__R�R�R�R�(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�.s
tProcessManagercB�sqeZdZd
dd�Zd
d
d
d�Zd�Zd�Zd�Ze	j
ed��Zd�Z
d	�ZRS(uD
    A class which will manage processes that should be running
    icC�smi|_||_|jdkr3|jj|_n||_tj�|_t	j
t	j�|_t
|_dS(N(t_process_mapRR�t	__class__R�R�RRdt_pidR�t	getsignaltSIGTERMt_sigterm_handlerR:t_restart_processes(R�RR�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�ys			cC�sr|dkrg}n|dkr*i}ntjjj�rtt�t|�krlt|t�rlt}nt	}|rd|kr�t
|d�r�|j|d<q�tjj
j�|d<nd|krt
|d�r�|j|d<qtjj
j�|d<qqn|dkr�t|tj�r?dj|j|j�}q�dj|jtj|j�dkrudj|j�nd|j�}nttj�t|�kr�t|tj�r�|||�}n$tjd|d	|d
|d|�}t|t�r"ttjtj ��|j!�WdQXn
|j!�tj"d
||j#�i|d6|d6|d6|d6|j$|j#<|S(u�
        Create a processes and args + kwargs
        This will deterimine if it is a Process class, otherwise it assumes
        it is a function
        u	log_queueulog_queue_levelu{0}.{1}u
{0}{1}.{2}u
<type 'type'>u.{0}uR�R�R�RNuStarted '%s' with pid %sutgtuargsukwargsuProcess(%R�R)RR;RgttypetMultiprocessingProcesst
issubclassR:R=thasattrt	log_queueRtsetupt!get_multiprocessing_logging_queuetlog_queue_levelt!get_multiprocessing_logging_levelR.ttypestFunctionTypeRNR�R�Rt	text_typeR�R�tProcesst$SignalHandlingMultiprocessingProcesstdefault_signalsR�tSIGINTR�R�RfR*R�(R�ttgtR�R�Rtneed_log_queueRF((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytadd_process�sP			--$

cC�s�|jtkrdStjd|j|d||j|dj�|j|djd�|j|j|d|j|d|j|d�|j|=dS(uY
        Create new process (assuming this one is dead), then remove the old one
        Nu7Process %s (%s) died with exit status %s, restarting...utgtuProcessiuargsukwargs(R�R=RtinfoR�texitcodetjoinR�(R�R*((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytrestart_process�scC�s
t|_dS(N(R=R�(R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytstop_restarting�scC�s�tjjj�r.|tjtjfkr.dSxutj|j	j
��D][}ytj||�WqGt
k
r�}|jtjtjfkr��n|j	|=qGXqGWdS(N(R)RR;RgR�R�R�RtiterkeysR�tcopyRR�RRtESRCHtEACCES(R�tsignal_R*R+((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytsend_signal_to_processes�scc�s+tjd�t|j�tjtj�tjkrNtjtj|j�ntjtj	�tjkr�tjtj	|j�nx�t
r&tjd�yK|j�|r�t
jd�Vn
tjd�t|j�dkr�PnWq�tk
r�Pq�tk
r"}|jtjkr�nPq�Xq�WdS(u:
        Load and start all available api modules
        uProcess Manager starting!uProcess manager iterationi
iN(RRfR
RR�R�R�tSIG_DFLt
kill_childrenR�R:ttracetcheck_childrenRR�R�tlenR�RReRtEINTR(R�tasynchronousR+((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytrun�s*

	



cC�si|jtkrexStj|j�D]<\}}|dj�s"tjd|�|j|�q"q"WndS(u)
        Check the children once
        uProcessuProcess restart of %sN(	R�R:Rt	iteritemsR�R�RR�R�(R�R*tmapping((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�&s
c
O�s�tjtjtj�tjtjtj�tj�|jkr�t|j�r]|j|�S|jdk	r�tj
tj�|�SdSntjj
j�r=tj�jdkr�dStjjjtjd��i}x_tj|j�D]K\}}tjddddtj|�gd|d	|�|d
j�q�WWdQXn�x�tj|jj��D]�\}}tjd||d
�|r�ytj||d�Wq�t k
r�q�Xny|d
j�Wn4t k
r�}|j!t!j"t!j#fkr��q�nX|d
j$�sVy|j|=Wq,t%k
r(q,XqVqVWt&j&�|j'}tjd
�x�|jr�t&j&�|kr�x�tj|jj��D]i\}}tjd||d
�|d
j(d�|d
j$�s�y|j|=Wq�t%k
r�q�Xq�q�WqSWd}x|dkr|d8}x�tj|jj��D]�\}}|d
j$�sty|j|=Wq0t%k
rmq0Xq0ntjd||d
�ytj|tj)�Wq0t k
r�}tj*|�|d
j$�s�y|j|=Wq�t%k
r�q�Xq�q0Xq0WqW|jr�|j+dd�}	|	dkr�tj,ddj(d�|jj-�D���tj,d|	�|	d|d<|j.||�Stj/ddj(d�|jj-�D���tj/d�ndS(u*
        Kill all of the children
        NuMainProcessuwbutaskkillu/Fu/Tu/PIDR&R'uProcessuTerminating pid %s: %siu(Waiting to kill process manager childrenuJoining pid %s: %siiuKilling pid %s: %suretryiu4Some processes failed to respect the KILL signal: %su; cs�s+|]!\}}dj|d|�VqdS(uProcess: {0} (Pid: {1})uProcessN(RN(t.0tktv((s6/usr/lib/python2.7/site-packages/salt/utils/process.pys	<genexpr>�sukill_children retries left: %su*Failed to kill the following processes: %scs�s+|]!\}}dj|d|�VqdS(uProcess: {0} (Pid: {1})uProcessN(RN(R�R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pys	<genexpr>�suLSalt will either fail to terminate now or leave some zombie processes behind(0R�R�tSIG_IGNR�RRdR�tcallableR�R�tdefault_int_handlerR)RR;RgR�tcurrent_processRR"R#tdevnullRR�R�R@tcallR�R�R�RR�R�RRR�R�R�RjR�R�R�R�t	exceptionR9R�titemsR�twarning(
R�R�R�R�R*tp_mapR+tend_timetkill_iterationstavailable_retries((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�0s�

%



%

%


	N(R�R�R�R�R�R�R�R�R�Rt	coroutineR=R�R�R�(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�usE			'	
R�cB�s5eZd�Zd�Zd�Zd�Zd�ZRS(cO�s?|jdd�}|jdd�}tt|�j||�tjjj�rvt	j	|�|_
t	j	|�|_n||_|jdkr�tj
jj�|_ntj
jj|j�||_|jdkr�tj
jj�|_ntj
jj|j�tj|gifg|_tj
jjgifg|_dS(Nu	log_queueulog_queue_level(tpopR�tsuperR�R�R)RR;RgR�t_args_for_getstatet_kwargs_for_getstateR�RR�R�t!set_multiprocessing_logging_queueR�R�t!set_multiprocessing_logging_levelt_setup_process_loggingt_after_fork_methodst shutdown_multiprocessing_loggingt_finalize_methods(R�R�R�R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��s 		cC�s@|d}|d}|j||�|j|_|j|_dS(Nuargsukwargs(R�R�R�(R�tstateR�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt__setstate__�s


cC�sp|j}|j}d|kr.|j|d<nd|krJ|j|d<ni|d6|d6|jd6|jd6S(Nu	log_queueulog_queue_leveluargsukwargsu_after_fork_methodsu_finalize_methods(R�R�R�R�R�R�(R�R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt__getstate__�s		

cC�stjjj|j�dS(N(R)RR�tsetup_multiprocessing_loggingR�(R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��scC�s�x'|jD]\}}}|||�q
Wz`ytt|�j�SWnBtk
rZ�n/tk
r�}tjd|jdt	��nXWdx'|j
D]\}}}|||�q�WXdS(NuJAn un-handled exception from the multiprocessing process '%s' was caught:
R^(R�R�R�R�t
SystemExitR�RRRR:R�(R�tmethodR�R�R+((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��s
(R�R�R�R�RR�R�(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��s
			
	R�cB�s5eZd�Zd�Zd�Zd�Zd�ZRS(cO�sKtt|�j||�tj�|_|jjtj|gif�dS(N(	R�R�R�R�tEventt_signal_handledR�R�t_setup_signals(R�R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��s	cC�s
|jj�S(N(Rtis_set(R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytsignal_handledscC�s0tjtj|j�tjtj|j�dS(N(R�R�t_handle_signalsR�(R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyRscC�s�|jj�tjtjtj�tjtjtj�dj|jj�}|tjkrj|d7}n|tjkr�|d7}n|d7}t	j
|�trqy�tj
tj��}t|d�r:xm|jdt�D]V}y|j�r�|j�nWq�tjk
r2t	jd|jtj��q�Xq�WnWqqtjk
rmt	jd|jtj��qqXntjtjjj�dS(	Nu{0} received a uSIGINTuSIGTERMu	. Exitinguchildrent	recursiveuCUnable to kill child of process %d, it does not exist. My pid is %duEUnable to kill children of process %d, it does not exist.My pid is %d(RtsetR�R�R�R�RNR�R�RRfR�R�R�RRdR�tchildrenR:t
is_runningR�t
NoSuchProcessR3R*RRR)RRR(R�tsignumtsigframeRwRFtchild((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR	s6




"cC�s3ttjtj��tt|�j�WdQXdS(N(R�R�R�R�R�R�R�(R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�,s(R�R�R�RRR	R�(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR��s
				!cg�s�i}xg|D]_}y&tj|�}tj|tj�Wn&tk
ra}tjd||�q
X|||<q
WdVx"|D]}tj|||�q|W~dS(Nu+Failed to register signal for signum %d: %s(R�R�R�R~RR�(tsignalstold_signalsRtsaved_signalR+((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�1s

tSubprocessListcB�s)eZddd�Zd�Zd�ZRS(cC�sL|dkrg|_n	||_|dkr?tj�|_n	||_dS(N(R�t	processesR�tLocktlock(R�RR((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyR�Os	cC�s7|j�(|jj|�tjd|j�WdQXdS(NuSubprocess %s added(RRR�RRfR(R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytaddYs
cC�sg|j�XxP|jD]E}|j�r,qn|j�|jj|�tjd|j�qWWdQXdS(NuSubprocess %s cleaned up(RRR�R�tremoveRRfR(R�R�((s6/usr/lib/python2.7/site-packages/salt/utils/process.pytcleanup^s

N(R�R�R�R�RR(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyRMs
	(BR�t
__future__RRRRR�R1RRR�RR�R�tloggingR�t
contextlibR@R�tmultiprocessing.utilROtsalt.defaults.exitcodesR)tsalt.utils.filestsalt.utils.pathtsalt.utils.platformtsalt.log.setuptsalt.log.mixinsRtsalt.extRtsalt.ext.six.movesRRttornadoRt	getLoggerR�RR=R�R�R:RJR
R	R
R-R$R?RHR]RxRzR�R�R�tobjectR�R�R�R�R�tcontextmanagerR�R(((s6/usr/lib/python2.7/site-packages/salt/utils/process.pyt<module>sp"




	2				!	3			G�:N5

Zerion Mini Shell 1.0