<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
<e                 @   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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jZd dlmZ e j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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   sp  t jdtjjtj d f  d | _d | _t	j
 }t	j|}|d krJt n|| _| jjtjrjt jd n
t jd tj }| jjdtj}tj|d}tj }tj }	tjj }
tjj | _tj|||||	|
| j| j}t| jjtj tj!}t"j#||||| j}tj }tj$ }tj%tj&}tj'|||| j| j}t(j)|||| j| | _*t+j,| j*| j| _-| j.  d | _/d S )NzTuneD: %s, kernel: %s   z8dynamic tuning is enabled (can be overridden in plugins)z#dynamic tuning is globally disabledudev_buffer_size)buffer_size)0loginfotunedversionZTUNED_VERSION_STRosuname_dbus_exporter_unix_socket_exporterr   ZPickleProviderZFactoryr   configget_boolconstsZCFG_DYNAMIC_TUNINGr   Z
RepositoryZget_sizeZCFG_DEF_UDEV_BUFFER_SIZEr   Z	InventoryZDeviceMatcherZDeviceMatcherUdevr   instancer   	variables	VariablesintgetZCFG_DEFAULT_INSTANCE_PRIORITYZ!CFG_DEF_DEFAULT_INSTANCE_PRIORITYr   ZManagerZMergerZLocatorZLOAD_DIRECTORIESLoaderr   ZDaemon_daemonr   Z
Controller_controller_init_signals	_pid_file)selfZprofile_namer   Zstorage_providerZstorage_factoryZmonitors_repositoryr   Zhardware_inventoryZdevice_matcherZdevice_matcher_udevZplugin_instance_factoryZplugins_repositoryZdef_instance_priorityZunit_managerZprofile_factoryZprofile_mergerZprofile_locatorZprofile_loader r(   !/usr/lib/python3.6/application.py__init__   s<    



zApplication.__init__c                s    fdd}t j | d S )Nc                s   | kr   d S )Nr(   )Z_signal_numberZ_frame)handlersignal_numberr(   r)   handler_wrapper@   s    z3Application._handle_signal.<locals>.handler_wrapper)signal)r'   r,   r+   r-   r(   )r+   r,   r)   _handle_signal?   s    zApplication._handle_signalc             C   s:   | j tj| jj | j tj| jj | j tj| jj d S )N)r/   r.   SIGHUPr$   ZsighupSIGINTZ	terminateSIGTERM)r'   r(   r(   r)   r%   E   s    zApplication._init_signalsc             C   s6   | j d k	rtdtjj||||| _ tj| j  d S )Nz&DBus interface is already initialized.)r   r	   r   ZdbusZDBusExporterregister_exporter)r'   Zbus_nameZobject_nameZinterface_name	namespacer(   r(   r)   attach_to_dbusJ   s    
zApplication.attach_to_dbusc             C   sj   | j d k	rtdtjj| jjtj| jjtj	| jjtj
| jjtj| jjtj| _ tj| j  d S )Nz-Unix socket interface is already initialized.)r   r	   r   Zunix_socketZUnixSocketExporterr   r!   r   ZCFG_UNIX_SOCKET_PATHZCFG_UNIX_SOCKET_SIGNAL_PATHSZCFG_UNIX_SOCKET_OWNERSHIPZget_intZCFG_UNIX_SOCKET_PERMISIONSZ#CFG_UNIX_SOCKET_CONNECTIONS_BACKLOGr3   )r'   r(   r(   r)   attach_to_unix_socketQ   s    
z!Application.attach_to_unix_socketc             C   s   t j| j d S )N)r   Zregister_objectr$   )r'   r(   r(   r)   register_controller\   s    zApplication.register_controllerc             C   s   t j| tj|gg g tj\}}}t|dkrBt j| tdt j|d}t j| t|dkrltdytj	d|d }W n tj
k
r   tdY nX |dkrt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)r   closeselectr   ZDAEMONIZE_PARENT_TIMEOUTlenr	   readstructunpackerror)r'   parent_in_fdchild_out_fdZ
read_readyZdropZresponsevalr(   r(   r)   _daemonize_parent_   s    


zApplication._daemonize_parentc             C   s   || _ | j  yltjj| j }tjj|s4tj| tjtj| j tj	tj
B tjB dd}|jdtj   W d Q R X W n> ttfk
r } ztjd| j t|f  W Y d d }~X nX d S )Ni  wz%dzcannot write the PID to %s: %s)r&   _delete_pid_filer   pathdirnameexistsmakedirsfdopenopenO_CREATO_TRUNCO_WRONLYwritegetpidOSErrorIOErrorr   criticalstr)r'   pid_fileZdir_namefr@   r(   r(   r)   write_pid_filex   s    
( zApplication.write_pid_filec             C   s^   t jj| jrZyt j| j W n: tk
rX } ztjd| jt|f  W Y d d }~X nX d S )Nz&cannot remove existing PID file %s, %s)	r   rG   rI   r&   unlinkrR   r   ZwarningrU   )r'   r@   r(   r(   r)   rF      s
    zApplication._delete_pid_filec             C   s*  t j| t jd t j  t jd yt j }|dkrBtjd W n^ tk
r } zBt	j
dt|  tjdd}t j|| t j| tdW Y dd}~X nX tdd	}t j|j tjj  t j|j tjj  t j|j tjj  | j| t	jd
 tjdd}t j|| t j| 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: %sr9   Fz'Cannot daemonize, second fork() failed.Nz	/dev/nullzw+zsuccessfully daemonizedT)r   r:   chdirsetsidumaskforksysexitrR   r   rT   rU   r>   packrP   r	   rL   dup2filenostdinstdoutstderrrX   debug)r'   rV   rA   rB   pidr@   rC   fdr(   r(   r)   _daemonize_child   s.    






zApplication._daemonize_childc             C   s   t j }yt j }W nF tk
rZ } z*t j|d  t j|d  tdW Y dd}~X nX y2|dkr|| j|  tjd n| j	|f|  W n"   |dkr n
tjd Y nX 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^   rR   r:   r	   rD   r_   r`   rj   )r'   rV   Zparent_child_fdsZ	child_pidr@   r(   r(   r)   	daemonize   s     
zApplication.daemonizec             C   s   | j S )N)r#   )r'   r(   r(   r)   r      s    zApplication.daemonc             C   s   | j S )N)r$   )r'   r(   r(   r)   r      s    zApplication.controllerc             C   sj   |r| j jtjd | j jtjtjs0tjd | jj	 }| j jtjtjrTt
j  | jd k	rf| j  |S )NTzrUsing one shot no daemon mode, most of the functionality will be not available, it can be changed in global config)r   setr   Z
CFG_DAEMONr   ZCFG_DEF_DAEMONr   warnr$   runr   stopr&   rF   )r'   r   resultr(   r(   r)   ro      s    


zApplication.run)NN)__name__
__module____qualname__r*   r/   r%   r5   r6   r7   rD   r   ZPID_FILErX   rF   rj   rl   propertyr   r   ro   r(   r(   r(   r)   r      s   
+")r   r   r   r   r   r   r   r   Ztuned.exceptionsr	   Z
tuned.logsZtuned.version r   r   r.   r   r_   r;   r>   Ztuned.constsr   Ztuned.utils.global_configr   Zlogsr!   r   __all__objectr   r(   r(   r(   r)   <module>   s   $

