o
    •Û_å  ã                   @   s´   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
Z ddlmZ ddlm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 e j ¡ ZdgZG d	d„ deƒZdS )
é    )ÚstorageÚunitsÚmonitorsÚpluginsÚprofilesÚexportsÚhardware)ÚTunedExceptionNé   )Ú
controller)Údaemon)ÚGlobalConfigÚApplicationc                   @   s‚   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zej	fdd„Z
dd„ Zdd„ Zej	fdd„Zedd„ ƒZedd„ ƒZdd„ ZdS )r   Nc              
   C   sL  d | _ t ¡ }t |¡}|d u rtƒ n|| _| j tj¡r#t	 
d¡ nt	 
d¡ t ¡ }| j dtj¡}tj|d}t ¡ }t ¡ }	tj ¡ }
tj ¡ | _t |||||	|
| j| j¡}t| j tjtj¡ƒ}t ||||| j¡}t ¡ }t ¡ }t tj ¡}t !|||| j| j¡}t" #|||| j| ¡| _$t% &| j$| j¡| _'|  (¡  d | _)d S )Nz8dynamic tuning is enabled (can be overridden in plugins)z#dynamic tuning is globally disabledÚudev_buffer_size)Úbuffer_size)*Ú_dbus_exporterr   ÚPickleProviderÚFactoryr   ÚconfigÚget_boolÚconstsÚCFG_DYNAMIC_TUNINGÚlogÚinfor   Ú
RepositoryÚget_sizeÚCFG_DEF_UDEV_BUFFER_SIZEr   Ú	InventoryÚDeviceMatcherÚDeviceMatcherUdevr   Úinstancer   Ú	variablesÚ	VariablesÚintÚgetÚCFG_DEFAULT_INSTANCE_PRIORITYÚ!CFG_DEF_DEFAULT_INSTANCE_PRIORITYr   ÚManagerÚMergerÚLocatorÚLOAD_DIRECTORIESÚLoaderr   ÚDaemonÚ_daemonr   Ú
ControllerÚ_controllerÚ_init_signalsÚ	_pid_file)ÚselfÚprofile_namer   Ústorage_providerÚstorage_factoryÚmonitors_repositoryr   Úhardware_inventoryÚdevice_matcherÚdevice_matcher_udevÚplugin_instance_factoryÚplugins_repositoryÚdef_instance_priorityÚunit_managerÚprofile_factoryÚprofile_mergerÚprofile_locatorÚprofile_loader© rB   ú:/usr/lib/python3/dist-packages/tuned/daemon/application.pyÚ__init__   s<   



ÿþ
zApplication.__init__c                    s   ‡ ‡fdd„}t   ˆ|¡ d S )Nc                    s   ˆ| kr	ˆ ƒ  d S d S ©NrB   )Ú_signal_numberÚ_frame©ÚhandlerÚsignal_numberrB   rC   Úhandler_wrapper;   s   
ÿz3Application._handle_signal.<locals>.handler_wrapper)Úsignal)r2   rJ   rI   rK   rB   rH   rC   Ú_handle_signal:   s   zApplication._handle_signalc                 C   s:   |   tj| jj¡ |   tj| jj¡ |   tj| jj¡ d S rE   )rM   rL   ÚSIGHUPr/   ÚreloadÚSIGINTÚ	terminateÚSIGTERM©r2   rB   rB   rC   r0   @   s   zApplication._init_signalsc                 C   s@   | j d ur	tdƒ‚tj |||¡| _ t | j ¡ t | j¡ d S )Nz&DBus interface is already initialized.)r   r	   r   ÚdbusÚDBusExporterÚregister_exporterÚregister_objectr/   )r2   Úbus_nameÚobject_nameÚinterface_namerB   rB   rC   Úattach_to_dbusE   s
   
zApplication.attach_to_dbusc                 C   s®   t  |¡ t |gg g tj¡\}}}t|ƒdkr!t  |¡ tdƒ‚t  |d¡}t  |¡ t|ƒdkr6tdƒ‚z
t 	d|¡d }W n tj
yL   tdƒ‚w |dkrUtd	ƒ‚d
S )z|
		Wait till the child signalizes that the initialization is complete by writing
		some uninteresting data into the pipe.
		r
   z=Cannot daemonize, timeout when waiting for the child process.é   r   z:Cannot daemonize, no response from child process received.ú?z?Cannot daemonize, invalid response from child process received.Tz0Cannot daemonize, child process reports failure.N)ÚosÚcloseÚselectr   ÚDAEMONIZE_PARENT_TIMEOUTÚlenr	   ÚreadÚstructÚunpackÚerror)r2   Úparent_in_fdÚchild_out_fdÚ
read_readyÚdropÚresponseÚvalrB   rB   rC   Ú_daemonize_parentM   s"   


ÿÿzApplication._daemonize_parentc              
   C   sØ   || _ |  ¡  zCtj | j ¡}tj |¡st |¡ t t | j tj	tj
B tjB d¡d¡}| dt ¡  ¡ W d   ƒ W d S 1 sCw   Y  W d S  ttfyk } zt d| j t|ƒf ¡ W Y d }~d S d }~ww )Ni¤  Úwz%dzcannot write the PID to %s: %s)r1   Ú_delete_pid_filer^   ÚpathÚdirnameÚexistsÚmakedirsÚfdopenÚopenÚO_CREATÚO_TRUNCÚO_WRONLYÚwriteÚgetpidÚOSErrorÚIOErrorr   ÚcriticalÚstr)r2   Úpid_fileÚdir_nameÚfrf   rB   rB   rC   Úwrite_pid_filef   s   
(&ÿ&€ÿzApplication.write_pid_filec              
   C   sd   t j | j¡r0z	t  | j¡ W d S  ty/ } zt d| jt|ƒf ¡ W Y d }~d S d }~ww d S )Nz&cannot remove existing PID file %s, %s)	r^   rp   rr   r1   Úunlinkr{   r   Úwarningr~   )r2   rf   rB   rB   rC   ro   s   s   &€ÿýzApplication._delete_pid_filec              
   C   s   t  |¡ t  d¡ t  ¡  t  d¡ zt  ¡ }|dkr!t d¡ W n* tyL } zt	 
dt|ƒ ¡ t dd¡}t  ||¡ t  |¡ tdƒ‚d}~ww tdd	ƒ}t  | ¡ tj ¡ ¡ t  | ¡ tj ¡ ¡ t  | ¡ tj ¡ ¡ |  |¡ t	 d
¡ t dd¡}t  ||¡ t  |¡ dS )zy
		Finishes daemonizing process, writes a PID file and signalizes to the parent
		that the initialization is complete.
		ú/r   z"cannot daemonize, fork() error: %sr]   Fz'Cannot daemonize, second fork() failed.Nz	/dev/nullzw+zsuccessfully daemonizedT)r^   r_   ÚchdirÚsetsidÚumaskÚforkÚsysÚexitr{   r   r}   r~   rd   Úpackry   r	   ru   Údup2ÚfilenoÚstdinÚstdoutÚstderrr‚   Údebug)r2   r   rg   rh   Úpidrf   rl   ÚfdrB   rB   rC   Ú_daemonize_childz   s4   



€
€û


zApplication._daemonize_childc              
   C   s®   t  ¡ }zt  ¡ }W n ty( } zt  |d ¡ t  |d ¡ tdƒ‚d}~ww z|dkr;| j|Ž  t d¡ W dS | j	|g|¢R Ž  W dS    |dkrO‚ t d¡ Y dS )zÚ
		Daemonizes the application. In case of failure, TunedException is raised
		in the parent process. If the operation is successfull, the main process
		is terminated and only child process returns from this method.
		r   r
   z Cannot daemonize, fork() failed.N)
r^   Úpiper‰   r{   r_   r	   rm   rŠ   r‹   r•   )r2   r   Úparent_child_fdsÚ	child_pidrf   rB   rB   rC   Ú	daemonizeœ   s$   €ý
zApplication.daemonizec                 C   ó   | j S rE   )r-   rS   rB   rB   rC   r   ·   ó   zApplication.daemonc                 C   rš   rE   )r/   rS   rB   rB   rC   r   »   r›   zApplication.controllerc                 C   sj   |r
| j  tjd¡ | j  tjtj¡st d¡ | j 	¡ }| j  tjtj¡r*t
 ¡  | jd ur3|  ¡  |S )NTzrUsing one shot no deamon mode, most of the functionality will be not available, it can be changed in global config)r   Úsetr   Ú
CFG_DAEMONr   ÚCFG_DEF_DAEMONr   Úwarnr/   Úrunr   Ústopr1   ro   )r2   r   ÚresultrB   rB   rC   r    ¿   s   


zApplication.run)NN)Ú__name__Ú
__module__Ú__qualname__rD   rM   r0   r[   rm   r   ÚPID_FILEr‚   ro   r•   r™   Úpropertyr   r   r    rB   rB   rB   rC   r      s    
'"

)Útunedr   r   r   r   r   r   r   Útuned.exceptionsr	   Ú
tuned.logsÚ r   r   rL   r^   rŠ   r`   rd   Útuned.constsr   Útuned.utils.global_configr   Úlogsr$   r   Ú__all__Úobjectr   rB   rB   rB   rC   Ú<module>   s   $ 
