a ”ˆ«hÒã@s¼ddlmZmZmZmZmZmZmZddlm Z ddl Zddl Zddl m Z ddl mZddlZddlZddlZddlZddlZddlmZddlmZej ¡ZdgZGd d„deƒZdS) é)ÚstorageÚunitsÚmonitorsÚpluginsÚprofilesÚexportsÚhardware)ÚTunedExceptionNé)Ú controller)Údaemon)Ú GlobalConfigÚ Applicationc@s’eZdZddd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z e j fdd„Z dd„Z dd„Ze j fdd„Zedd„ƒZedd„ƒZdd„ZdS)rNc Cs|t dtjjt ¡df¡d|_d|_t   ¡}t   |¡}|durJt ƒn||_ |j  tj¡rjt d¡n t d¡t ¡}|j  dtj¡}tj|d}t ¡}t ¡} tj  ¡} tj ¡|_t ||||| | |j |j¡} t|j  tj tj!¡ƒ} t" #| || ||j ¡} t  ¡}t $¡}t %|j  &tj'tj(¡¡}t )||||j |j¡}t* +| |||j |¡|_,t- .|j,|j ¡|_/| 0¡d|_1dS)NzTuneD: %s, kernel: %séz8dynamic tuning is enabled (can be overridden in plugins)z#dynamic tuning is globally disabledÚudev_buffer_size)Z buffer_size)2ÚlogÚinfoÚtunedÚversionZTUNED_VERSION_STRÚosÚunameÚ_dbus_exporterÚ_unix_socket_exporterrZPickleProviderZFactoryr ÚconfigÚget_boolÚconstsZCFG_DYNAMIC_TUNINGrZ RepositoryZget_sizeZCFG_DEF_UDEV_BUFFER_SIZErZ InventoryZ DeviceMatcherZDeviceMatcherUdevrÚinstancerZ variablesZ VariablesÚintÚgetZCFG_DEFAULT_INSTANCE_PRIORITYZ!CFG_DEF_DEFAULT_INSTANCE_PRIORITYrZManagerZMergerZLocatorÚget_listZCFG_PROFILE_DIRSZCFG_DEF_PROFILE_DIRSÚLoaderr ZDaemonÚ_daemonr Z ControllerÚ _controllerÚ _init_signalsÚ _pid_file)ÚselfZ profile_namerZstorage_providerZstorage_factoryZmonitors_repositoryrZhardware_inventoryZdevice_matcherZdevice_matcher_udevZplugin_instance_factoryZplugins_repositoryZdef_instance_priorityZ unit_managerZprofile_factoryZprofile_mergerZprofile_locatorZprofile_loader©r&ú.handler_wrapper)Úsignal)r%r,r+r-r&r*r'Ú_handle_signal?szApplication._handle_signalcCs:| tj|jj¡| tj|jj¡| tj|jj¡dSr))r/r.ÚSIGHUPr"ZsighupÚSIGINTZ terminateÚSIGTERM©r%r&r&r'r#EszApplication._init_signalscCs6|jdurtdƒ‚tj ||||¡|_t |j¡dS)Nz&DBus interface is already initialized.)rr rZdbusZ DBusExporterÚregister_exporter)r%Zbus_nameZ object_nameZinterface_nameÚ namespacer&r&r'Úattach_to_dbusJs zApplication.attach_to_dbusc Csj|jdurtdƒ‚tj |j tj¡|j  tj ¡|j tj ¡|j  tj ¡|j  tj¡¡|_t |j¡dS)Nz-Unix socket interface is already initialized.)rr rZ unix_socketZUnixSocketExporterrrrZCFG_UNIX_SOCKET_PATHrZCFG_UNIX_SOCKET_SIGNAL_PATHSZCFG_UNIX_SOCKET_OWNERSHIPZget_intZCFG_UNIX_SOCKET_PERMISIONSZ#CFG_UNIX_SOCKET_CONNECTIONS_BACKLOGr4r3r&r&r'Úattach_to_unix_socketQs     üz!Application.attach_to_unix_socketcCst |j¡dSr))rZregister_objectr"r3r&r&r'Úregister_controller\szApplication.register_controllercCs²t |¡t |gggtj¡\}}}t|ƒdkrBt |¡tdƒ‚t |d¡}t |¡t|ƒdkrltdƒ‚zt  d|¡d}Wntj yœtdƒ‚Yn0|dkr®td ƒ‚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.érz: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ÚselectrZDAEMONIZE_PARENT_TIMEOUTÚlenr ÚreadÚstructÚunpackÚerror)r%Ú parent_in_fdÚ child_out_fdZ read_readyZdropZresponseÚvalr&r&r'Ú_daemonize_parent_s      zApplication._daemonize_parentc CsÔ||_| ¡z€tj |j¡}tj |¡s4t |¡t t |jtj tj Btj Bd¡d¡"}|  dt  ¡¡Wdƒn1s‚0YWn@ttfyÎ}z$t d|jt|ƒf¡WYd}~n d}~00dS)Ni¤Úwz%dzcannot 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_fileZdir_nameÚfrAr&r&r'Úwrite_pid_filexs  (4zApplication.write_pid_filec Cs`tj |j¡r\zt |j¡Wn<tyZ}z$t d|jt|ƒf¡WYd}~n d}~00dS)Nz&cannot remove existing PID file %s, %s) rrHrJr$ÚunlinkrSrÚwarningrV)r%rAr&r&r'rG…s zApplication._delete_pid_filec Cs,t |¡t d¡t ¡t d¡zt ¡}|dkrBt d¡Wn`ty¤}zHt   dt |ƒ¡t   dd¡}t ||¡t |¡tdƒ‚WYd}~n d}~00tdd ƒ}t | ¡tj ¡¡t | ¡tj ¡¡t | ¡tj ¡¡| |¡t  d ¡t   dd ¡}t ||¡t |¡dS) zy Finishes daemonizing process, writes a PID file and signalizes to the parent that the initialization is complete. ú/rz"cannot daemonize, fork() error: %sr:Fz'Cannot daemonize, second fork() failed.Nz /dev/nullzw+zsuccessfully daemonizedT)rr;ÚchdirÚsetsidÚumaskÚforkÚsysÚexitrSrrUrVr?ÚpackrQr rMÚdup2ÚfilenoÚstdinÚstdoutÚstderrrYÚdebug)r%rWrBrCÚpidrArDÚfdr&r&r'Ú_daemonize_childŒs.           zApplication._daemonize_childc Csºt ¡}z t ¡}WnHty\}z0t |d¡t |d¡tdƒ‚WYd}~n d}~00z4|dkr~|j|Žt d¡n|j |g|¢RŽWn"|dkr¦‚n t d¡Yn0dS)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. rr z Cannot daemonize, fork() failed.N) rÚpiper`rSr;r rErarbrl)r%rWZparent_child_fdsZ child_pidrAr&r&r'Ú daemonize®s    zApplication.daemonizecCs|jSr))r!r3r&r&r'r ÉszApplication.daemoncCs|jSr))r"r3r&r&r'r ÍszApplication.controllercCsj|r|j tjd¡|j tjtj¡s0t d¡|j  ¡}|j tjtj¡rTt   ¡|j durf|  ¡|S)NTzrUsing one shot no daemon mode, most of the functionality will be not available, it can be changed in global config)rÚsetrZ CFG_DAEMONrZCFG_DEF_DAEMONrr[r"ÚrunrÚstopr$rG)r%r Úresultr&r&r'rpÑs   zApplication.run)NN)Ú__name__Ú __module__Ú __qualname__r(r/r#r6r7r8rErZPID_FILErYrGrlrnÚpropertyr r rpr&r&r&r'rs  +  "  )rrrrrrrrZtuned.exceptionsr Z tuned.logsZ tuned.versionÚr r r.rrar<r?Z tuned.constsrZtuned.utils.global_configr ZlogsrrÚ__all__Úobjectrr&r&r&r'Ús$