o
    fjY                    @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z% d dlm&Z& d dlm'Z' d dlm(Z( d dlm)Z) d dlm*Z* d dlm+Z+ d dlm,Z, d dlm-Z- d dlm.Z. d dlm/Z/ d dlm0Z0 d dlm1Z1 d dlm2Z2 d dlm3Z3 d d lm4Z4 d d!l5m6Z6 d d"l5m7Z7 d d#l5m8Z8 d d$l5m9Z9 d%d& Z:e: Z;d'd( Z<G d)d* d*Z=G d+d, d,Z>G d-d. d.e>Z?G d/d0 d0e>Z@g ZAG d1d2 d2ejBZCG d3d4 d4eDZEG d5d6 d6eEZFG d7d8 d8eFZGG d9d: d:eFZHG d;d< d<eEZIG d=d> d>eEZJG d?d@ d@eIZKdAdB ZLdCdD ZMdEdF ZNdaOdGdH ZPdIdJ ZQG dKdL dLZRdMdN ZSdOdP ZTdQdR ZUG dSdT dTeVZWG dUdV dVeWZXG dWdX dXeWZYG dYdZ dZeWZZG d[d\ d\eWZ[dS )]    N)PY2)ConfigParser)as_bytes	as_string)	xmlrpclib)StringIO)
basestring)asyncore_25)process_or_group_name)boolean)integer)name_to_uid)gid_for_uid)existing_dirpath)	byte_size)signal_number)list_of_exitcodes)dict_of_key_value_pairs)logfile_name)list_of_strings)
octal_type)existing_directory)logging_level)colon_separated_user_group)inet_address)InetStreamSocketConfig)UnixStreamSocketConfig)url)	Automatic)Syslog)auto_restart)profile_options)loggers)states)xmlrpc)pollerc                  C   s\   t jt jt} t j| d}t|d}|  W  d    S 1 s'w   Y  d S )Nzversion.txtr)	ospathabspathdirname__file__joinopenreadstrip)mydirversion_txtf r3   E/var/www/html/venv/lib/python3.10/site-packages/supervisor/options.py_read_version_txt:   s
   
$r5   c                 C   s   t jt jt j| S N)r'   r(   normpathr)   
expanduser)vr3   r3   r4   normalize_pathA   s   r:   c                   @   s   e Zd ZdS )DummyN)__name__
__module____qualname__r3   r3   r3   r4   r;   D   s    r;   c                   @   s   e Zd ZejZejZejZeZd ZZ	ej
d ZdZdZdZdZdZdddZdd Zdd	 Zd
d Z									d ddZdd Zd!ddZdddZdd Zdd Zd"ddZdd Zdd ZdS )#OptionsNr   Tc                 C   s   g | _ g | _g | _i | _i | _i | _i | _i | _|| _| 	dddd| j
 | 	dddd| j
 | 	dddd tjtjtjd }tj|d	d
tj|d
d
dddg}|| _i | _tj D ]\}}|| jd| < qbdS )znConstructor.

        Params:
        require_configfile -- whether we should fail on no config file.
        Nhhelp?
configfilezc:zconfiguration=r   etczsupervisord.confzetc/supervisord.confz/etc/supervisord.confz /etc/supervisor/supervisord.confENV_%s)
names_listshort_optionslong_optionsoptions_mapdefault_maprequired_mapenviron_mapattr_prioritiesrequire_configfileaddrA   r'   r(   r*   sysargvr,   searchpathsenviron_expansionsenvironitems)selfrN   hererR   kr9   r3   r3   r4   __init__Y   s2   zOptions.__init__c                 C   sL   d}| j D ]}tj|r|} nq|du r$| jr$| dd| j   |S )z=Return the name of the found config file or print usage/exit.NzjNo config file found at default paths (%s); use the -c option to specify a config file at a different path, )rR   r'   r(   existsrN   usager,   )rV   configr(   r3   r3   r4   default_configfilez   s   

zOptions.default_configfilec                 C   s@   | j d }|ddkr|d| j}| j| | d dS )zxPrint a long help message to stdout and exit(0).

        Occurrences of "%s" in are replaced by self.progname.
        
z%sr   N)docfindreplaceprognamestdoutwriteexit)rV   dummyrA   r3   r3   r4   rA      s
   
zOptions.helpc                 C   s4   | j dt|  | j d| j  | d dS )z2Print a brief error message to stderr and exit(2).z
Error: %s
zFor help, use %s -h
   N)stderrre   strrc   rf   rV   msgr3   r3   r4   r\      s   zOptions.usagec
                 C   s  |dur0|durt d|s|st d|r|drt d|r*|dr*t d|fdd}|rF|rF|d|dkrFt d||f |r|d	 d
krRt d|dd |dd }
}|dvrgt dd
|
 }
|
| jv rvt d|
 ||f| j|
< | j| |r|d	 d
krt d|}
|
d dkr|
dd }
d|
 }
|
| jv rt d|
 ||f| j|
< | j| |	r||f| j|	< |rt| |st| |d | j	||f |dur|| j
|< |r|| j|< dS dS dS )a  Add information about a configuration option.

        This can take several forms:

        add(name, confname)
            Configuration option 'confname' maps to attribute 'name'
        add(name, None, short, long)
            Command line option '-short' or '--long' maps to 'name'
        add(None, None, short, long, handler)
            Command line option calls handler
        add(name, None, short, long, handler)
            Assign handler return value to attribute 'name'

        In addition, one of the following keyword arguments may be given:

        default=...  -- if not None, the default value
        required=... -- if nonempty, an error message if no value provided
        flag=...     -- if not None, flag value for command line option
        env=...      -- if not None, name of environment variable that
                        overrides the configuration file or default
        Nz%use at most one of flag= and handler=z"flag= requires a command line flag:=c                 S   s   |S r6   r3   )argflagr3   r3   r4   <lambda>   s    zOptions.add.<locals>.<lambda>z&inconsistent short/long options: %r %rr   -z&short option should not start with '-'   ) rm   z"short option should be 'x' or 'x:'zduplicate short option key '%s'z%long option should not start with '-'z--zduplicate long option key '%s')
ValueErrorendswithrI   rG   appendrH   rL   hasattrsetattrrF   rJ   rK   )rV   nameconfnameshortlonghandlerdefaultrequiredrp   envkeyrestr3   r3   r4   rO      sb    



zOptions.addc                 C   s4   | j |d}||krt| || || j |< d S d S )Nru   )rM   getrz   )rV   attrvaluepriocurrentr3   r3   r4   _set   s
   zOptions._setc                 C   sP  |du rt jdd }|du rt jd }|du r+z	ddl}|j}W n	 ty*   Y nw || _|| _g | _g | _zt		|d
| j| j\| _| _W n t	jyc } z| t| W Y d}~nd}~ww | jrt| jst| dt| j  | jD ]M\}}| j| \}}	|	durz|	|}W n ty }
 z| d|||
f  W Y d}
~
nd}
~
ww |r|durt| |dur| d|  | ||d qw| j D ]L}| j| \}}	|tjv rtj| }|	durz|	|}W n ty }
 z| d	|||
f  W Y d}
~
nd}
~
ww |r|dur| ||d q| jdu r"|  | _|   dS )
zRealize a configuration.

        Optional arguments:

        args     -- the command line arguments, less the program name
                    (default is sys.argv[1:])

        doc      -- usage message (default is __main__.__doc__)
        Nrs   r   rt   z*positional arguments are not supported: %szinvalid value for %s %r: %sz"conflicting command line option %rrh   z'invalid environment value for %s %r: %s)rP   rQ   __main____doc__	Exceptionrc   r`   optionsargsgetoptr,   rG   rH   errorr\   rj   positional_args_allowedrI   rv   getattrr   rL   keysr'   rT   rC   r^   process_config)rV   r   r`   rc   r   excoptro   r{   r   rl   envvarr   r3   r3   r4   realize   st   

 


zOptions.realizec           	      C   s   | j r| | | jD ]%\}}|r0|d}| j}|D ]}|du r# nt||}q| ||d q| j D ]\}}t| |du rGt	| || q6| j
 D ]\}}t| |du r]| | qMdS )z}Process configuration data structure.

        This includes reading config file if necessary, setting defaults etc.
        .Nr   )rC   process_config_filerF   split
configrootr   r   rJ   rU   rz   rK   r\   )	rV   do_usager{   r|   partsobjpartr   messager3   r3   r4   r   8  s*   


zOptions.process_configc              
   C   sz   t | jdstjtj| j| _z	| | j W d S  ty< } z|r-| 	t
| nt|W Y d }~d S d }~ww )Nr.   )ry   rC   r'   r(   r)   r*   rW   read_configrv   r\   rj   )rV   r   rl   r3   r3   r4   r   W  s   zOptions.process_config_filec                 C   s   t j|S r6   )r'   r(   r[   rV   r(   r3   r3   r4   r[   e     zOptions.existsr&   c                 C   s
   t ||S r6   )r-   )rV   fnmoder3   r3   r4   r-   h     
zOptions.openc              	   C   s   g }|  D ]X}||sq|ddd }|||d }|d u r)td||f z| |}W n ty?   td||f w i }	||D ]}
|
|krU|||
|	|
< qG||||	f q|S )Nrm   rs   z"section [%s] does not specify a %s!%s cannot be resolved within [%s])	sections
startswithr   sanegetrv   import_specImportErrorr   rx   )rV   parserfactory_keysection_prefix	factoriessectionr{   factory_specfactoryextrasrX   r3   r3   r4   get_pluginsk  s0   
zOptions.get_pluginsc                 C   s,   t jd| }t|dr| S |dS )Nzx=resolveF)pkg_resources
EntryPointparsery   r   load)rV   specepr3   r3   r4   r     s   

zOptions.import_specT)	NNNNNNNNN)NNN)r&   )r<   r=   r>   rP   ri   rd   rf   warningsuidgidrQ   rc   rC   	schemadirr   rW   r   rY   r^   rA   r\   rO   r   r   r   r   r[   r-   r   r   r3   r3   r3   r4   r?   G   sD    

!
T

F
r?   c                   @   s  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZejjZdd Zdd Zdd	 Zd
d Zdd ZdsddZdd Zdd Zdd Z	dtddZ	dtddZdd Zdd Zdd  Z d!d" Z!d#d$ Z"d%d& Z#d'd( Z$d)d* Z%d+d, Z&d-d. Z'd/d0 Z(d1d2 Z)d3d4 Z*d5d6 Z+d7d8 Z,d9d: Z-d;d< Z.d=d> Z/d?d@ Z0dAdB Z1dCdD Z2dEdF Z3dGdH Z4dIdJ Z5dKdL Z6dMdN Z7dOdP Z8dQdR Z9dSdT Z:dUdV Z;dWdX Z<dYdZ Z=d[d\ Z>d]d^ Z?d_d` Z@dadb ZAdcdd ZBdedf ZCdgdh ZDdidj ZEdkdl ZFdsdmdnZGdodp ZHdqdr ZIdS )uServerOptionsNr3   Fc                 C   s  t |  t | _t | j_| d d dd| j | jddddddd | d	d
dd | jddddtdd | ddddt | jddddt	dd | jddddt
d d | jd!d"d#d$td%d | jd&d'd(d)td*d | jd+d,d-d.t	d/d | jd0d1d2d3td4d | jd5d6d7d8tt d | jd9d:d;d<td=d | jd>d?d@dAtdBd | jdCdDdEdCddd | jdFdGdHdFddd | jdIdJd@dKtd d | jdLdMdNdLddd i | _g | _g | _g | _g | _t | _t| | _d S )ONr9   versionnodaemonzsupervisord.nodaemonnrs   r   rp   r   userzsupervisord.useru:zuser=umaskzsupervisord.umaskzm:zumask=022r   	directoryzsupervisord.directoryzd:z
directory=logfilezsupervisord.logfilezl:zlogfile=supervisord.loglogfile_maxbyteszsupervisord.logfile_maxbyteszy:zlogfile_maxbytes=i   logfile_backupszsupervisord.logfile_backupszz:zlogfile_backups=
   loglevelzsupervisord.loglevelze:z	loglevel=infopidfilezsupervisord.pidfilezj:zpidfile=supervisord.pid
identifierzsupervisord.identifierzi:zidentifier=
supervisorchildlogdirzsupervisord.childlogdirzq:zchildlogdir=minfdszsupervisord.minfdsza:zminfds=   minprocszsupervisord.minprocsrt   z	minprocs=   	nocleanupzsupervisord.nocleanuprX   
strip_ansizsupervisord.strip_ansitr!   zsupervisord.profile_optionszprofile_options=silentzsupervisord.silents)r?   rY   r;   r   supervisordrO   r   r   r   r   r   r   r   rj   tempfile
gettempdirintr!   
pidhistoryprocess_group_configsparse_criticalsparse_warningsparse_infosSignalReceiversignal_receiverr%   PollerrV   r3   r3   r4   rY     sz   

zServerOptions.__init__c                 C   s   | j dt  | d dS )z-Print version to stdout and exit(0).
        %s
r   N)rd   re   VERSIONrf   )rV   rg   r3   r3   r4   r     s   zServerOptions.versionc                 O   s   t j|i |S r6   )r"   	getLoggerrV   r   kwargsr3   r3   r4   r        zServerOptions.getLoggerc                 C   s"   t  dkr| jd t| S )Nr   a  Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.)r'   getuidr   warnr?   r^   r   r3   r3   r4   r^     s
   
z ServerOptions.default_configfilec              
   O   sV  t j| g|R i | | jj}| jd ur<zt| j}W n ty3 } z| | W Y d }~nd }~ww || _t	|| _
| jsC|j| _| jrJ| j}n|j}|dkrVt|| _| jr]| j}n|j}t|| _|j| _d | _|j | _}dd |D D ]}	|	d }
d|
 | _ | jd u rdd |D D ]}	|	d }|	d }|sd	}d
||f | _qd S d S )Nsyslogc                 S      g | ]}|d  t ju r|qS family)socketAF_UNIX.0r]   r3   r3   r4   
<listcomp>      z)ServerOptions.realize.<locals>.<listcomp>file	unix://%sc                 S   r   r   )r   AF_INETr   r3   r3   r4   r    r  hostport	localhostzhttp://%s:%s)r?   r   r   r   r   r   rv   r\   r   r   r   r   r   r:   r   rpcinterface_factories	serverurlserver_configs)rV   ro   kwr   r   rl   r   r   sconfigsr]   r(   r  r  r3   r3   r4   r     sL   





zServerOptions.realizeTc                 C   s"   t j| |d | jjj}|| _d S )N)r   )r?   r   r   r   r   )rV   r   newr3   r3   r4   r   "  s   

zServerOptions.process_configc                    s*  g | _ g | _g | _| jj}d}t|ds9| |std| z
| |d}d}W n t	t
fy8   td| w t | j_z4zz| W n tyW   | Y nw W n tjyl } ztt|d }~ww W |rt|  n|r||  w w t }| j|d}|| j dr| j dd	std
dd	}t||d}| }t|drtj !tj "|j#}nd}|D ]Q}	tj $||	}	t%%|	}
|
s| j&d|	  qt'|
D ]3}| j&d|  z(| W n tjy
 } ztt|d }~ww tj "tj !| qqȈ) }d|vr'tdd| ji  fdd}t*|dd|_+t*|dd|_,|dd }|d u rQd |_-nt.||_-|dd |_/t0|dd|_1t2|dd|_3t4|dd |_5t*|d!d"|_6t7|d#d$|_8t2|d%d&|_9|d'd(|_:t;|d)d*|_<t;|d+d*|_=t>? }t.|d,||_@t;|d-d*|_At;|d.d*|_B|d/d0}t||d/}tC||_D|jDE D ]\}}|| jd1| < q| Fd2d3|_G| H|_I|jID ]}|jJD ]}|jDK }||jD ||_Dqq| L|_Md |_N|S )4NFr.   could not find config file %sr&   Tcould not read config file %s)rW   host_node_nameincludefilesz5.ini file has [include] section, but no files settingzinclude.filesr{   r   z No file matches via include "%s"z'Included extra file "%s" during parsingr   z..ini file does not include supervisord sectionrW   c                    s2   | di }|  ||d< j| |fi |S N
expansions)r   update
getdefault)r   r   r   r  common_expansionsr   r3   r4   r   o  s   
z&ServerOptions.read_config.<locals>.getr   r   r   r   r   r   r   r   r   r   r   50MBr   r   r   r   r   r   r   r   r   falser   r   r   r   environmentrt   rE   zsupervisor.rpcinterface_factoryzrpcinterface:)Or   r   r   r   r   ry   r[   rv   r-   IOErrorOSErrorUnhosedConfigParserrS   r  	read_fileAttributeErrorreadfpr   ParsingErrorrj   closeplatformnoderW   r  has_sectionexpand_here
has_optionr   expandr   r'   r(   r*   r)   r{   r,   globrx   sortedr.   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rU   r   r	  process_groups_from_parserr   process_configscopyserver_configs_from_parserr  r!   )rV   fpr   
need_closewhyr  r  r  basepattern	filenamesfilenamer   r   r   tempdirenviron_strrX   r9   groupprocr   r3   r  r4   r   (  s   















zServerOptions.read_configc           "         s  g }  }g }d| ji  fdd}|D ]u}|dsqt|ddd }t||dd }t||dd	}	g }
|D ]D}d
| }d| }||vrV||vrVtd||f ||v rf||v rftd||f ||v rl|n|}|| | 	||t
}|
| q<|t| ||	|
 q|D ]0}|dr||v rqt|ddd }t||dd	}	| 	||t
}|t| ||	| q|D ]}|dsq|ddd }t||dd}	t||dd}|dk rtd||f ||dd}z| |}W n ty
   td||f w dd t||ddD }t|}|s%td| ddlm} g }|D ]}t||d }|d u rDtd||f || q/t||dd }|r\td!| | 	||t}|t| ||	|||| q|D ]}|d"r||v rqtt|ddd }t||dd	}	d#|i}||d$d }|d u rd }nt|}||d%d }|d urt|}|dk s|d&krtd'| ||d(d }|d urzt|}W n ty   td)| w ||d*d }|d urzt|}W n ttfy   td+| w ||d,d |d-}|std.| z| |||||} W n ty? }! z
td/t|!|f d }!~!ww | 	||t}|t| ||	||  qt|  |S )0NrW   c                    s4   | di }|  ||d< j| ||fi |S r  r   r  r   )r   r   r   r   r  r  r3   r4   r     s   
z5ServerOptions.process_groups_from_parser.<locals>.getzgroup:rm   rs   programspriority  z
program:%szfcgi-program:%sz-[%s] names unknown program or fcgi-program %sz>[%s] name %s is ambiguous (exists as program and fcgi-program)zprogram:zeventlistener:ru   buffer_sizer   z*[%s] section sets invalid buffer_size (%d)result_handlerz&supervisor.dispatchers:default_handlerr   c                 S   s   g | ]}|  qS r3   )upper)r   xr3   r3   r4   r    s    z<ServerOptions.process_groups_from_parser.<locals>.<listcomp>eventsrt   z&[%s] section requires an "events" liner   )
EventTypesz$Unknown event type %s in [%s] eventsredirect_stderrr  zx[%s] section sets redirect_stderr=true but this is not allowed because it will interfere with the eventlistener protocolzfcgi-program:program_namer   socket_backlog  zInvalid socket_backlog value %ssocket_ownerzInvalid socket_owner value %ssocket_modezInvalid socket_mode value %sr   r  z%[%s] section requires a "socket" linez%s in [%s] socket)r   rW   r   r
   r   r   r   rv   rx   processes_from_sectionProcessConfigextendProcessGroupConfigr   r   setsupervisor.eventsrE  r   r   EventListenerConfigEventListenerPoolConfigr   r   r   	TypeErrorparse_fcgi_socketrj   FastCGIProcessConfigFastCGIGroupConfigsort)"rV   r   groupsall_sectionshomogeneous_excluder   r   
group_namer=  r>  group_processesprogramprogram_sectionfcgi_section	processesrG  	pool_namer@  rA  pool_event_namesrE  pool_eventspool_event_name
pool_eventrF  fcgi_expansions	proc_userproc_uidrH  rJ  rK  r   socket_configer3   r  r4   r-    s:  
















z(ServerOptions.process_groups_from_parserc                 C   s   | dr>|dd  }tj|std|t|}|d u r0t }|d ur0||kr0|t|f}|d u r6d}t||||dS |d usF|d urJtdt	
d|}|re|d}	t|d	}
t|	|
|d
S td|)Nunix://   z+Unix socket path %s is not an absolute path  )ownerr   backlogzQsocket_owner and socket_mode params should only be used with a Unix domain socketztcp://([^\s:]+):(\d+)$rs   rh   )rq  zBad socket format %s)r   r'   r(   isabsrv   r:   r   r   r   rematchr:  r   r   )rV   sockrj  rJ  rK  rH  r(   r   mr  r  r3   r3   r4   rV  P  s6   


zServerOptions.parse_fcgi_socketc              
   C   sN   z	|  ||||W S  ty& } z|j|| j}td|||f d }~ww )Nz%s in section %r (file: %r))_processes_from_sectionrv   section_to_filer   rC   )rV   r   r   r]  klassrl  r7  r3   r3   r4   rM  s  s   z$ServerOptions.processes_from_sectionc           1   	      s  |d u rt }g }t|ddd }t }| j|||d  fdd}t||dd}	t||dd	}
t||d
d}t||dd}t||dd}t	||dd}t||dd}t||dd}t||d|}t
||dd}t||dd}t||dd}t||dd}||dddd}t||d d}t||d!d}t||d"d}t||d#d}||d$d }|r|  d%krd }||d&d }|d u rd }nt|}||d'd }|d urt|}t||d(d)dd}|dkrd*|vrtd+|r|std,t||| D ]W}  }!|!| |d- |!| j tt||!d}"|" D ]\}#}$|$|!d.|# < q,||d/d }%i }&d0D ]}}#d1|# }'|||'t}(t|(trZt|(|!|'}(t|(}(|(|&|'< d2|# })t|||)d}*|*|&|)< d3|# }+t|||+d4},|,|&|+< d5|# }-t|||-d}.|.|&|-< |(tu r| jd6||'|-f  d  |&|'< }(d7|&|-< |(tu r|,s| jd8||'f  qB|r|&d9 td fvr| jd:|  d |&d9< ||d;d |!d<}/|/d u rtd=| || fi d>t||!d(d;|/d/|%d'|d|	d|
d
|d|d|d?|d@|&d@ d |d!|dA|&dA dB|&dB dC|&dC d9|&d9 d"|d#|dD|&dD dE|&dE dF|&dF d|d|d|d|d|d|d|"d$|}0||0 q|  |S )GNrm   rs   )rW   rG  r  r]  c                    s8   | di }|  ||d< j| |g|R i |S r  r<  )r   r   r   r   r  r  r3   r4   r     s   
z2ServerOptions._processes_from_section.<locals>.getr>  r?  	autostarttrueautorestart
unexpected	startsecsstartretries   
stopsignalTERMstopwaitsecsr   stopasgroupr  killasgroup	exitcodes0rF  numprocsnumprocs_startr   r  rt   F)	do_expandstdout_capture_maxbytesstdout_events_enabledstderr_capture_maxbytesstderr_events_enabledr
  AUTOr   r   process_namez%(program_name)sz%(process_num)zD%(process_num) must be present within process_name when numprocs > 1z1Cannot set stopasgroup=true and killasgroup=false)process_numr  rE   r   )rd   ri   z
%s_logfilez%s_logfile_backupsz%s_logfile_maxbytesr  z	%s_syslogzfFor [%s], %s=syslog but this is deprecated and will be removed.  Use %s=true to enable syslog instead.TztFor [%s], AUTO logging used for %s without rollover, set maxbytes > 0 to avoid filling up filesystem unintentionallystderr_logfilezpFor [%s], redirect_stderr=true but stderr_logfile has also been set to a filename, the filename has been ignoredcommandrL  z-program section %s does not specify a commandr{   r   stdout_logfilestdout_logfile_backupsstdout_logfile_maxbytesstdout_syslogstderr_logfile_backupsstderr_logfile_maxbytesstderr_syslog)rN  r
   r   r%  r&  rW   r   r   r    r   r   r   r/   rB  r   r   rv   ranger  rS   r   r*  rU   r   
isinstancer   r   r   r   rx   rY  )1rV   r   r   r]  ry  r=  rG  r  r   r>  rz  r|  r~  r  r  r  r  r  r  rF  r  r  environment_strstdout_cmaxbytesstdout_eventsstderr_cmaxbytesstderr_eventsr
  r   r   r   r  r  r  r  rX   r9   r   logfileslf_keylf_valbu_keybackupsmb_keymaxbytessy_keyr   r  pconfigr3   r  r4   rw  }  sZ  


	
!z%ServerOptions._processes_from_sectionc                 C   sT   g }|  D ]!}||r'|dd}t|dkr|d }nd }|||f q|S )Nrm   rs   )r   r   r   lenrx   )rV   r   styper   r   r   r{   r3   r3   r4   _parse_servernames)  s   

z ServerOptions._parse_servernamesc                 C   sT   |j }||dd }||dd }|d us|d ur%|d u s|d u r%td| ||dS )NusernamepasswordzSection [%s] contains incomplete authentication: If a username or a password is specified, both the username and password must be specified)r  r  )r   rv   )rV   r   r   r   r  r  r3   r3   r4   _parse_username_and_password5  s   
z*ServerOptions._parse_username_and_passwordc              
   C   s  g }|  |d}|D ]B\}}i }|j}|| || ||d< tj|d< ||dd }|d u r5td| t|\}	}|	|d< ||d< ||d< || q
|  |d}
|
D ]\}}i }|j}||d	d d
| j	id}|d u rstd| |
 }||d< tj|d< t||d	< || || ||dd }|d urzt|}W n ty   td| w d}||d< ||dd }|d urzt|}W n ttfy   td| w d}||d< ||d< || qU|S )Ninet_http_serverr{   r   r  zsection [%s] has no port valuer  r   unix_http_serverr  rW   rL  zsection [%s] has no file valuechownzInvalid sockchown value %s)ru   ru   chmodzInvalid chmod value %sro  )r  r   r  r  r   r  rv   r   rx   rW   r/   r   r:   r   r   rU  )rV   r   configsinet_serverdefsr{   r   r]   r   r  r  unix_serverdefssfiler  r  r3   r3   r4   r0  A  sb   

z(ServerOptions.server_configs_from_parserc                 C   s    | j   |   | j   d S r6   )r%   before_daemonize
_daemonizeafter_daemonizer   r3   r3   r4   	daemonizev  s   
zServerOptions.daemonizec              
   C   s  t  }|dkr| jd t d | jd | jrKzt | j W n tyA } z| j	d| j|f  W Y d }~nd }~ww | jd| j  t 
d td | _ t_t_t 
d tdd | _ t_t_t 
d	 tdd | _ t_t_t   t | j d S )
Nr   z"supervisord forked; parent exitingz#daemonizing the supervisord processzcan't chdir into %r: %szset current directory: %rz	/dev/nullrs   wrh   )r'   forkloggerblather_exitr   r   chdirr  criticalr$  r-   stdinrP   	__stdin__rd   
__stdout__ri   
__stderr__setsidr   )rV   piderrr3   r3   r4   r  {  s2   



zServerOptions._daemonizec              	   C   s   t  }zt| jd}|d|  W d    n1 sw   Y  W n ttfy8   | jd| j  Y d S w d| _	| j
d|  d S )Nr  r   zcould not write pidfile %sTzsupervisord started with pid %s)r'   getpidr-   r   re   r  r  r  r  unlink_pidfiler   )rV   r  r2   r3   r3   r4   write_pidfile  s   zServerOptions.write_pidfilec                 C   sV   | j D ]\}}|d tjkr| jr|d }| | q| jr$| | j | j  d S )Nr   r  )	httpserversr   r   unlink_socketfiles_try_unlinkr  r   r%   r$  )rV   r]   server
socketnamer3   r3   r4   cleanup  s   
zServerOptions.cleanupc                 C   &   zt | W d S  ty   Y d S w r6   )r'   unlinkr  r   r3   r3   r4   r    
   zServerOptions._try_unlinkc                 C   sb   g }| j D ] \}}|  |   D ]}t|dd }||u r$|| qq|D ]}|  q(d S )Nr  )r  r$  get_socket_mapvaluesr   rx   )rV   dispatcher_serversr]   r  
dispatcherdispatcher_serverr3   r3   r4   close_httpservers  s   

zServerOptions.close_httpserversc                 C   s   | j   d S r6   )r  r$  r   r3   r3   r4   close_logger     zServerOptions.close_loggerc                 C   s`   | j j}ttj| ttj| ttj| ttj| ttj| ttj| d S r6   )	r   receivesignalSIGTERMSIGINTSIGQUITSIGHUPSIGCHLDSIGUSR2)rV   r  r3   r3   r4   
setsignals  s   zServerOptions.setsignalsc                 C   s
   | j  S r6   )r   
get_signalr   r3   r3   r4   r    r   zServerOptions.get_signalc              
   C   s   z|  || _d| _W d S  tjye } zL|jd tjkr#| d n0d}tj	
|jd }|d u r?| d||jd f  n| d|||jd f  W Y d }~d S W Y d }~d S W Y d }~d S d }~w ty } z| |jd  W Y d }~d S d }~ww )NTr   zAnother program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.z1Cannot open an HTTP server: socket.error reportedz%s %sz%s errno.%s (%d))make_http_serversr  r  r   r   r   errno
EADDRINUSEr\   	errorcoder   rv   )rV   r   r3  rA   	errornamer3   r3   r4   openhttpservers  s(   zServerOptions.openhttpserversc                 C   s$   d|||f }| j d|| jd}|S )Nz%s-%s---%s-z.log)suffixprefixdir)
mktempfiler   )rV   r{   r   channelr  r   r3   r3   r4   get_autochildlog_name  s   z#ServerOptions.get_autochildlog_namec              
   C   s   | j }td| j }zt|}W n ttfy$   | j	d Y d S w |D ])}|
|rPtj||}z| | W q' ttfyO   | j	d|  Y q'w q'd S )Nz.+?---%s-\S+\.log\.{0,1}\d{0,4}zCould not clear childlog dirzFailed to clean up %r)r   rs  compiler   r'   listdirr  r  r  r   rt  r(   r,   remove)rV   r   fnrer6  r7  pathnamer3   r3   r4   clear_autochildlogdir  s$   
z#ServerOptions.clear_autochildlogdirc                 C   s   t jS r6   )asyncore
socket_mapr   r3   r3   r4   r    s   zServerOptions.get_socket_mapc                 C   s   d}t || j d S )N   )r'   
closeranger   )rV   startr3   r3   r4   cleanup_fds"  s   zServerOptions.cleanup_fdsc                 C   s   t || d S r6   )r'   kill)rV   r  r  r3   r3   r4   r  '  r   zServerOptions.killc              
   C   s   zt dt j\}}W ||fS  tyG } z-|jd }|tjtjfvr+| j	d|  |tjkr6| j
d d\}}W Y d }~||fS d }~ww )Nru   r   z:waitpid error %r; a process may not be cleaned up properlyzEINTR during reap)NN)r'   waitpidWNOHANGr  r   r  ECHILDEINTRr  r  r  )rV   r  stsr   coder3   r3   r4   r  *  s"   


zServerOptions.waitpidc                    s>   du rdS zt  }W n$ ty0   zt }W n ty)   d   Y  Y S w |d }Y nw zt|}W n tyE   d|  Y S w t }||krPdS |dkrVdS |d }ttd	r|d   fd
dt	
 D }|d| zt| W n
 ty   Y dS w zt| W n
 ty   Y dS w t| dS )zDrop privileges to become the specified user, which may be a
        username or uid.  Called for supervisord startup and when spawning
        subprocesses.  Returns None on success or a string error message if
        privileges could not be dropped.NzNo user specified to setuid to!zCan't find username %rrh   zCan't find uid %rr   z$Can't drop privilege as nonroot userr  	setgroupsc                    s    g | ]} |d  v r|d qS )r  rh   r3   )r   grprecr   r3   r4   r  h  s   
 z1ServerOptions.drop_privileges.<locals>.<listcomp>z&Could not set groups of effective userz(Could not set group id of effective user)r   rv   pwdgetpwnamKeyErrorgetpwuidr'   r   ry   grpgetgrallinsertr  r  setgidsetuid)rV   r   r   pwreccurrent_uidr   rZ  r3   r
  r4   drop_privilegesB  sL   
zServerOptions.drop_privilegesc                 C   sb   | j du rt dkr| jd dS dS | | j }|du r*| jd| j   dS | | dS )zSet the uid of the supervisord process.  Called during supervisord
        startup only.  No return value.  Exits the process via usage() if
        privileges could not be dropped.Nr   zSupervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.zSet uid to user %s succeeded)r   r'   r   r   rx   r  r   r\   rk   r3   r3   r4   set_uid_or_exit{  s   

zServerOptions.set_uid_or_exitc           	   
   C   s   g }t tdr|d| jtjdd t tdr$|d| jtjdd |D ]U}|d }|d }|d }|d	 }|}t|\}}||k r{|d
kr{||k rS|d
krS|}zt|||f | j	dt
   W q& tjtfyz   | |t
   Y q&w q&dS )zSet the rlimits of the supervisord process.  Called during
        supervisord startup only.  No return value.  Exits the process via
        usage() if any rlimits could not be set.RLIMIT_NOFILEa  The minimum number of file descriptors required to run this process is %(min_limit)s as per the "minfds" command-line argument or config file setting. The current environment will only allow you to open %(hard)s file descriptors.  Either raise the number of usable file descriptors in your environment (see README.rst) or lower the minfds setting in the config file to allow the process to start.)rl   minresourcer{   RLIMIT_NPROCa  The minimum number of available processes required to run this program is %(min_limit)s as per the "minprocs" command-line argument or config file setting. The current environment will only allow you to open %(hard)s processes.  Either raise the number of usable processes in your environment (see README.rst) or lower the minprocs setting in the config file to allow the program to start.r  r  rl   r{   ru   z)Increased %(name)s limit to %(min_limit)sN)ry   r  rx   r   r  r   r  	getrlimit	setrlimitr   localsr   rv   r\   )	rV   limitslimit	min_limitresrl   r{   softhardr3   r3   r4   set_rlimits_or_exit  sJ   
	
	
z!ServerOptions.set_rlimits_or_exitc                 C   s   d}t | j| _| jr| jst | j| t j| j| j|| j	  | j	| j
d | jD ]}| j| q+| jD ]}| j| q7| jD ]}| j| qCd S )Nz&%(asctime)s %(levelname)s %(message)s
)rotatingr  r  )r"   r   r   r  r   r   handle_stdouthandle_filer   r   r   r   r  r   r   r   r   )rV   formatrl   r3   r3   r4   make_logger  s&   


zServerOptions.make_loggerc                 C   s   ddl m} || |S )Nr   )r  )supervisor.httpr  )rV   r   r  r3   r3   r4   r    s   
zServerOptions.make_http_serversc                 C   r  r6   )r'   r$  r  )rV   fdr3   r3   r4   close_fd  r  zServerOptions.close_fdc                 C      t  S r6   )r'   r  r   r3   r3   r4   r       zServerOptions.forkc                 C   s   t ||S r6   )r'   dup2)rV   frmtor3   r3   r4   r0    r   zServerOptions.dup2c                 C   r.  r6   )r'   setpgrpr   r3   r3   r4   r3    r/  zServerOptions.setpgrpc                 C   s
   t |S r6   )r'   stat)rV   r7  r3   r3   r4   r4    r   zServerOptions.statc                 C   s   t |t|S r6   )r'   re   r   )rV   r,  datar3   r3   r4   re     r   zServerOptions.writec                 C   s   t |||S r6   )r'   execve)rV   r7  rQ   r   r3   r3   r4   r6    r  zServerOptions.execvec                 C   s&   d t _t|||\}}t | |S r6   )r'   
_urandomfdr   mkstempr$  )rV   r  r  r  r,  r7  r3   r3   r4   r    s   
zServerOptions.mktempfilec                 C      t | d S r6   )r'   r  r   r3   r3   r4   r    r  zServerOptions.removec                 C   r9  r6   )r'   r  )rV   r  r3   r3   r4   r    r  zServerOptions._exitc                 C   r9  r6   )r'   r   )rV   maskr3   r3   r4   setumask
  r  zServerOptions.setumaskc                 C   s0   g d}dt jv rt jd }|r|t j}|S )z3Return a list corresponding to $PATH, or a default.)z/binz/usr/binz/usr/local/binPATH)r'   rT   r   pathsep)rV   r(   pr3   r3   r4   get_path  s   

zServerOptions.get_pathc                 C   r.  r6   )r'   r  r   r3   r3   r4   get_pid  r/  zServerOptions.get_pidc                 C   sn   |d u r
t d| t|tj rtd| t|tj d@ s(td| t|tjs5t	d| d S )Nzcan't find command %rzcommand at %r is a directoryI   zcommand at %r is not executablezno permission to run command %r)
NotFoundr4  S_ISDIRST_MODENotExecutableS_IMODEr'   accessX_OKNoPermission)rV   r7  rQ   str3   r3   r4   check_execv_args  s   zServerOptions.check_execv_argsc                 C   s0   | j d | j jD ]}t|dr|  q
d S )Nzsupervisord logreopenreopen)r  r   handlersry   rL  )rV   r   r3   r3   r4   
reopenlogs&  s   
zServerOptions.reopenlogsc              
   C   sZ   z	t |d}W |S  ty, } z|jd tjtjtjfvr d}W Y d }~|S d }~ww )Ni   r       )r'   r.   r  r   r  EWOULDBLOCKEBADFr  )rV   r,  r5  r3  r3   r3   r4   readfd,  s   zServerOptions.readfdc                 C   r9  r6   )r'   r  )rV   r  r3   r3   r4   r  5  r  zServerOptions.chdirc           
      C   s   ddddddd}zVt  \}}|||d< |d< t  \}}|||d< |d< |r9t  \}}|||d< |d< |d |d |d fD ]}|dur\t|tjt jB }	t|tj|	 qD|W S  tyw   | D ]}|duru| | qj w )	z Create pipes for parent to child stdin/stdout/stderr
        communications.  Open fd in non-blocking mode so we can read them
        in the mainloop without blocking.  If stderr is False, don't
        create a pipe for stderr. N)child_stdinr  rd   child_stdoutri   child_stderrrS  r  rd   rT  ri   rU  )	r'   pipefcntlF_GETFLO_NDELAYF_SETFLr  r  r-  )
rV   ri   pipesr  rS  rd   rT  rU  r,  flagsr3   r3   r4   
make_pipes8  s8   
zServerOptions.make_pipesc                 C   *   dD ]}| |}|d ur| | qd S )N)r  rd   ri   r   r-  rV   r[  fdnamer,  r3   r3   r4   close_parent_pipesW     

z ServerOptions.close_parent_pipesc                 C   r^  )N)rS  rT  rU  r_  r`  r3   r3   r4   close_child_pipes]  rc  zServerOptions.close_child_pipesr   r6   )Jr<   r=   r>   r   	sockchown	sockchmodr   r   r   
passwdfiler   r   r  r  r  r#   SupervisorStatesRUNNINGmoodrY   r   r   r^   r   r   r   r-  rV  rM  rw  r  r  r0  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r%  r*  r  r-  r  r0  r3  r4  re   r6  r  r  r  r;  r?  r@  rK  rN  rR  r  r]  rb  rd  r3   r3   r3   r4   r     s    0
> *$

 -54
	9<			
r   c                   @   sH   e Zd ZdZdZdZdZdZdZdZ	dd Z
dd Zdd Zd	d
 ZdS )ClientOptionsrs   Nc                 C   s   t j| dd t | _t | j_d | jj_d| jj_d | jj_d | jj_d | jj_	d | jj_
ddlm} d|i f}|g| _| jddd	dd
dd | jdddd | jddddtdd | dddd | dddd | dddd d S ) NF)rN   r   r   )DefaultControllerPluginr   interactivezsupervisorctl.interactiveirs   r   promptzsupervisorctl.promptr   r
  zsupervisorctl.serverurlzs:z
serverurl=http://localhost:9001r  zsupervisorctl.usernamer   z	username=r  zsupervisorctl.passwordzp:z	password=historyzsupervisorctl.history_filezr:zhistory_file=)r?   rY   r;   r   supervisorctlrm  ro  r
  r  r  history_filesupervisor.supervisorctlrl  plugin_factoriesrO   r   )rV   rl  default_factoryr3   r3   r4   rY   m  s,   








zClientOptions.__init__c                 O   s,   t j| g|R i | | jsd| _d S d S )Nrs   )r?   r   r   rm  )rV   ro   r  r3   r3   r4   r     s   
zClientOptions.realizec           	   	   C   s  | j j}d}t|ds9tjt|| _| |st	d| z
| 
|d}d}W n ttfy8   t	d| w t }| j|_d|_z|| W n tyX   || Y nw |r_|  | }d|vrkt	d|jd	d
d| jid}|drt|dd  }d| }||_|d|j|_|d|j|_|d|j|_|jd|jd| jid}|rt|}||_|| _nd |_d | _|  j| |dd7  _|S )NFr.   r  r&   Tr  rr  z0.ini file does not include supervisorctl sectionr
  rp  rW   rL  rm  rn  r  ro  r  r  rs  zsupervisor.ctl_factoryz
ctlplugin:)r   rr  ry   r'   r(   r*   r:   rW   r[   rv   r-   r  r  r  rS   r  	mysectionr   r!  r"  r$  r   r  r   r
  ro  r  r  rs  ru  r   )	rV   r1  r   r2  r   r   r
  r(   rs  r3   r3   r4   r     sd   




zClientOptions.read_configc                 C   s   t jdt| j| j| jdS )Nzhttp://127.0.0.1)	transport)r   ServerProxyr$   SupervisorTransportr  r  r
  r   r3   r3   r4   getServerProxy  s   zClientOptions.getServerProxy)r<   r=   r>   r   rm  ro  r
  r  r  rs  rY   r   r   r{  r3   r3   r3   r4   rk  c  s    9rk  c                   @   sP   e Zd ZdZdd ZdddZdd Zed	i fd
dZei fddZ	dd Z
dS )r  r   c                 O   sN   t sd|vr
d|d< d|vrd|d< tjj| g|R i | i | _i | _d S )Ninline_comment_prefixes);#strictF)r   r   RawConfigParserrY   rx  r  r   r3   r3   r4   rY     s   
zUnhosedConfigParser.__init__<string>c                 C   s4   z	t j| ||W S  ty   | t| Y S w )zParse configuration data from a string.  This is intended
        to be used in tests only.  We add this method for Py 2/3 compat.)r   r  read_stringr!  r"  r   )rV   stringsourcer3   r3   r4   r    s   zUnhosedConfigParser.read_stringc                 K   sp   t |tr|g}g }|D ])}| j }|tjj| |gfi | t| jt| }|D ]}|| j	|< q-q|S )a%  Attempt to read and parse a list of filenames, returning a list
        of filenames which were successfully parsed.  This is a method of
        RawConfigParser that is overridden to build self.section_to_file,
        which is a mapping of section names to the files they came from.
        )
r  r   	_sectionsr/  rO  r   r  r.   	frozensetrx  )rV   r6  r   ok_filenamesr7  sections_origdiffr   r3   r3   r4   r.     s   

zUnhosedConfigParser.readTc                 C   sx   z|  ||}W n tjy   |tu r |}Y nw |r:t|tr:tt| j	 t|	  }t
||d||f }|S )Nz%s.%s)r   r   NoOptionError_markerr  r   dictlistr  rU   r*  )rV   r   optionr   r  r  optvalcombined_expansionsr3   r3   r4   r     s   
zUnhosedConfigParser.sanegetc                 K   s   | j | j|f||d|S )N)r   r  )r   rw  )rV   r  r   r  r   r3   r3   r4   r    s
   zUnhosedConfigParser.getdefaultc                 C   s\   d}|   D ]%}| |D ]\}}||v r*|d usJ d|||}| ||| qqd S )Nz%(here)szhere has not been set to a path)r   rU   rb   rQ  )rV   rW   HERE_FORMATr   r   r   r3   r3   r4   r(    s   zUnhosedConfigParser.expand_hereN)r  )r<   r=   r>   rw  rY   r  r.   r  r   r  r(  r3   r3   r3   r4   r    s    
	
r  c                   @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )Configc                 C   s   |  | S r6   )__eq__rV   otherr3   r3   r4   __ne__%  r   zConfig.__ne__c                 C   s$   | j |j kr| j|jk S | j |j k S r6   r>  r{   r  r3   r3   r4   __lt__(     zConfig.__lt__c                 C   s$   | j |j kr| j|jkS | j |j kS r6   r  r  r3   r3   r4   __le__.  r  zConfig.__le__c                 C   s$   | j |j kr| j|jkS | j |j kS r6   r  r  r3   r3   r4   __gt__4  r  zConfig.__gt__c                 C   s$   | j |j kr| j|jkS | j |j kS r6   r  r  r3   r3   r4   __ge__:  r  zConfig.__ge__c                 C   s   d| j t| | jf S )Nz<%s instance at %s named %s>)	__class__idr{   r   r3   r3   r4   __repr__@  s   zConfig.__repr__N)	r<   r=   r>   r  r  r  r  r  r  r3   r3   r3   r4   r  $  s    r  c                   @   sN   e Zd Zg dZddgZdd Zdd Zdd	 Zd
d ZdddZ	dd Z
dS )rN  )r{   r   r  r   r   r>  rz  r|  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rF  r  r
  c                 K   sF   || _ | jD ]
}t| |||  q| jD ]}t| |||d  qd S r6   )r   req_param_namesrz   optional_param_namesr   )rV   r   paramsr{   r3   r3   r4   rY   R  s   

zProcessConfig.__init__c                 C   sX   t |tsdS | j| j D ]}tt| |t||fv rqt| |t||kr) dS qdS NFT)r  rN  r  r  r   r   )rV   r  r{   r3   r3   r4   r  Y  s   
zProcessConfig.__eq__c                 C   s4   | j dur| j d}|dur|tjS | j S )z|Return a list corresponding to $PATH that is configured to be set
        in the process environment, or the system default.Nr<  )r  r   r   r'   r=  r   r?  r   r3   r3   r4   r?  e  s
   

zProcessConfig.get_pathc                 C   sN   | j j}| j j}| j}| jtu r|||d| _| jtu r%|||d| _d S d S )Nrd   ri   )r   r  r   r{   r  r   r  )rV   get_autonamesidr{   r3   r3   r4   create_autochildlogsn  s   

z"ProcessConfig.create_autochildlogsNc                 C   s   ddl m} || }||_|S )Nr   )
Subprocess)supervisor.processr  r:  )rV   r:  r  processr3   r3   r4   make_processx  s   zProcessConfig.make_processc                 C   s   | j  }| j|}|d |d |d }}}i }ddlm} ddlm}	 ddlm}
 |d ur;|
j}||||||< |d urJ|
j	}||||||< |d urV|	|d|||< ||fS )Nrd   ri   r  r   POutputDispatcherPInputDispatcherrD  )
rF  r   r]  supervisor.dispatchersr  r  r   rD  ProcessCommunicationStdoutEventProcessCommunicationStderrEvent)rV   r;  
use_stderrr>  	stdout_fd	stderr_fdstdin_fddispatchersr  r  rD  etyper3   r3   r4   make_dispatchers~  s    zProcessConfig.make_dispatchersr6   )r<   r=   r>   r  r  rY   r  r?  r  r  r  r3   r3   r3   r4   rN  D  s    	

rN  c                   @   s   e Zd Zdd ZdS )rS  c                 C   s   d}| j |}|d |d |d }}}i }ddlm} ddlm}	 ddlm}
 dd	lm} |d ur<||d|||< |d urK|j}|
|||||< |d urW|	|d|||< ||fS )
NTrd   ri   r  r   )PEventListenerDispatcherr  r  r  )	r   r]  r  r  r  r  r   rD  r  )rV   r;  r  r>  r  r  r  r  r  r  r  rD  r  r3   r3   r4   r    s$   z$EventListenerConfig.make_dispatchersN)r<   r=   r>   r  r3   r3   r3   r4   rS    s    rS  c                   @   s   e Zd ZdddZdd ZdS )rW  Nc                 C   s.   |d u rt dddlm} || }||_|S )Nz FastCGI programs require a groupr   )FastCGISubprocess)NotImplementedErrorr  r  r:  )rV   r:  r  r  r3   r3   r4   r    s   z!FastCGIProcessConfig.make_processc                 C   s4   t | |\}}|d }|d ur||   ||fS )Nr  )rN  r  r$  )rV   r;  r  r>  r  r3   r3   r4   r    s
   z%FastCGIProcessConfig.make_dispatchersr6   )r<   r=   r>   r  r  r3   r3   r3   r4   rW    s    
rW  c                   @   ,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rP  c                 C   s   || _ || _|| _|| _d S r6   )r   r{   r>  r.  )rV   r   r{   r>  r.  r3   r3   r4   rY     s   
zProcessGroupConfig.__init__c                 C   sB   t |tsdS | j|jkrdS | j|jkrdS | j|jkrdS dS r  )r  rP  r{   r>  r.  r  r3   r3   r4   r    s   
zProcessGroupConfig.__eq__c                 C      | j D ]}|  qd S r6   r.  r  rV   r]   r3   r3   r4   after_setuid     

zProcessGroupConfig.after_setuidc                 C      ddl m} || S )Nr   )ProcessGroup)r  r  )rV   r  r3   r3   r4   
make_group     zProcessGroupConfig.make_groupNr<   r=   r>   rY   r  r  r  r3   r3   r3   r4   rP    s
    rP  c                   @   r  )
rT  c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S r6   )r   r{   r>  r.  r@  re  rA  )rV   r   r{   r>  r.  r@  re  rA  r3   r3   r4   rY     s   
z EventListenerPoolConfig.__init__c                 C   s^   t |tsdS | j|jkr-| j|jkr-| j|jkr-| j|jkr-| j|jkr-| j|jkr-dS dS r  )r  rT  r{   r>  r.  r@  re  rA  r  r3   r3   r4   r    s   
zEventListenerPoolConfig.__eq__c                 C   r  r6   r  r  r3   r3   r4   r    r  z$EventListenerPoolConfig.after_setuidc                 C   r  )Nr   )EventListenerPool)r  r  )rV   r  r3   r3   r4   r    r  z"EventListenerPoolConfig.make_groupNr  r3   r3   r3   r4   rT    s
    
rT  c                   @   $   e Zd Zdd Zdd Zdd ZdS )rX  c                 C   s   t | |||| || _d S r6   )rP  rY   rk  )rV   r   r{   r>  r.  rk  r3   r3   r4   rY     s   
zFastCGIGroupConfig.__init__c                 C   s*   t |tsdS | j|jkrdS t| |S )NF)r  rX  rk  rP  r  r  r3   r3   r4   r    s
   
zFastCGIGroupConfig.__eq__c                 C   r  )Nr   )FastCGIProcessGroup)r  r  )rV   r  r3   r3   r4   r    r  zFastCGIGroupConfig.make_groupN)r<   r=   r>   rY   r  r  r3   r3   r3   r4   rX    s    
	rX  c           	   	   C   s  t |}t |}zwt| dg}||kr:|rtd|dd | }t|| }|dk r/d}|| ||}n)||krBtd|dkrP|| | }n|| ||}W d   W |S W d   W |S W d   W |S 1 sxw   Y  W |S  ttfy   tdw )zJ Read length bytes from the file named by filename starting at
    offset rbBAD_ARGUMENTSr   rh   NFAILED)	absr-   rv   seektellr   r.   r  r  )	r7  offsetlength	absoffset	abslengthr2   szposr5  r3   r3   r4   readFile  sD   



r  c              	   C   s   zit | dY}d}|dd | }||| krd}|d }|| |kr1||d kr-d}|| }|dk r7d}|dk r=d}|dkrDd}n
|| ||}|}t|||gW  d   W S 1 sbw   Y  W dS  ttfyy   d	|dg Y S w )
a  
    Read length bytes from the file named by filename starting at
    offset, automatically increasing offset and setting overflow
    flag if log size has grown beyond (offset + length).  If length
    bytes are not available, as many bytes as are available are returned.
    r  Fr   rh   Trs   rO  Nrt   )r-   r  r  r.   r   r  r  )r7  r  r  r2   overflowr  r5  r3   r3   r4   tailFile1  s4   

(r  c                 C   s   t | rt | d@ }d| }||fS t | r=t | }dt| }tt dr/t | }n| d@ }|r9|d7 }d|fS d|  }d|fS )	a*  Decode the status returned by wait() or waitpid().

    Return a tuple (exitstatus, message) where exitstatus is the exit
    status, or -1 if the process was killed by a signal; and message
    is a message telling what happened.  It is the caller's
    responsibility to display the message.
    rI  zexit status %szterminated by %s	WCOREDUMP   z (core dumped)ru   z unknown termination cause 0x%04x)r'   	WIFEXITEDWEXITSTATUSWIFSIGNALEDWTERMSIGsignamery   r  )r  esrl   sigiscorer3   r3   r4   decode_wait_statusZ  s   



r  c                 C   s    t du rt  t | pd|  S )zReturn a symbolic name for a signal.

    Return "signal NNN" if there is no corresponding SIG name in the
    signal module.
    Nz	signal %d)	_signames_init_signamesr   )r  r3   r3   r4   r  v  s   r  c                  C   sN   i } t j D ]\}}t|dd }|d u rq|dr"|ds"|| |< q| ad S )Nr   SIGSIG_)r  __dict__rU   r   r  )drX   r9   k_startswithr3   r3   r4   r    s   r  c                   @   r  )r   c                 C   s
   g | _ d S r6   )_signals_recvdr   r3   r3   r4   rY     r   zSignalReceiver.__init__c                 C   s   || j vr| j | d S d S r6   )r  rx   )rV   r  framer3   r3   r4   r    s   
zSignalReceiver.receivec                 C   s   | j r| j d}|S d }|S )Nr   )r  pop)rV   r  r3   r3   r4   r    s
   zSignalReceiver.get_signalN)r<   r=   r>   rY   r  r  r3   r3   r3   r4   r     s    r   c                 C   s   z| | W S  t y* } zt| }|  td| |t|d|f d }~w ty@ } ztd| |t|f d }~ww )NzYFormat string %r for %r contains names (%s) which cannot be expanded. Available names: %srZ   z.Format string %r for %r is badly formatted: %s)r  r  r   rY  rv   rj   r,   r   )r   r  r{   ex	availabler3   r3   r4   r*    s&   
r*  c                 C   s    | |kr|}|S d| |f }|S )Nz%s:%sr3   )r]  r  r{   r3   r3   r4   make_namespec  s
   r  c                 C   sJ   |  dd}t|dkr|\}}|r|dkrd }||fS | | }}||fS )Nrm   rs   rh   *)r   r  )namespecnamesr]  r  r3   r3   r4   split_namespec  s   
r  c                   @      e Zd ZdZdS )ProcessExceptionz@ Specialized exceptions used when attempting to start a process Nr<   r=   r>   r   r3   r3   r3   r4   r        r  c                   @   r  )
BadCommandz5 Indicates the command could not be parsed properly. Nr  r3   r3   r3   r4   r    r  r  c                   @   r  )rE  z Indicates that the filespec cannot be executed because its path
    resolves to a file which is not executable, or which is a directory. Nr  r3   r3   r3   r4   rE    r  rE  c                   @   r  )rB  zR Indicates that the filespec cannot be executed because it could not
    be found Nr  r3   r3   r3   r4   rB    r  rB  c                   @   r  )rI  z Indicates that the file cannot be executed because the supervisor
    process does not possess the appropriate UNIX filesystem permission
    to execute the file. Nr  r3   r3   r3   r4   rI    r  rI  )\r   r   r'   rP   r   r  r  rs  r  r  r  r4  r   r+  r%  r   rW  supervisor.compatr   r   r   r   r   r   r   supervisor.medusar	   r  supervisor.datatypesr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r"   r#   r$   r%   r5   r   r:   r;   r?   r   rk  r  r  r  objectr  rN  rS  rW  rP  rT  rX  r  r  r  r  r  r  r   r*  r  r  r   r  r  rE  rB  rI  r3   r3   r3   r4   <module>   s      J         ]mR L!")
