3 À<×e—ã@sºddlmZmZmZmZmZmZmZddlm Z ddl Zddl Zddl m Z ddl mZddlZddlZddlZddlZddlZddljZddlmZejjƒ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 Csptjdtjjtjƒdfƒd|_d|_t j ƒ}t j |ƒ}|dkrJt ƒn||_ |j jtjƒrjtjdƒn tjdƒtjƒ}|j jdtjƒ}tj|d�}tjƒ}tjƒ} tjj ƒ} tjjƒ|_tj||||| | |j |jƒ} t|j jtj tj!ƒƒ} t"j#| || ||j ƒ} tj ƒ}tj$ƒ}tj%tj&ƒ}tj'||||j |jƒ}t(j)| |||j |ƒ|_*t+j,|j*|j ƒ|_-|j.ƒd|_/dS)NzTuneD: %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ÚversionZTUNED_VERSION_STRÚosÚunameÚ_dbus_exporterÚ_unix_socket_exporterrZPickleProviderZFactoryr ÚconfigÚget_boolÚconstsZCFG_DYNAMIC_TUNINGrZ RepositoryZget_sizeZCFG_DEF_UDEV_BUFFER_SIZErZ InventoryZ DeviceMatcherZDeviceMatcherUdevrÚinstancerÚ variablesÚ VariablesÚintÚgetZCFG_DEFAULT_INSTANCE_PRIORITYZ!CFG_DEF_DEFAULT_INSTANCE_PRIORITYrZManagerZMergerZLocatorZLOAD_DIRECTORIESÚ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(ú!/usr/lib/python3.6/application.pyÚ__init__s<         zApplication.__init__cs‡‡fdd„}tjˆ|ƒdS)Ncsˆ|krˆƒdS)Nr()Z_signal_numberZ_frame)ÚhandlerÚ signal_numberr(r)Úhandler_wrapper@sz3Application._handle_signal..handler_wrapper)Úsignal)r'r,r+r-r()r+r,r)Ú_handle_signal?szApplication._handle_signalcCs:|jtj|jjƒ|jtj|jjƒ|jtj|jjƒdS)N)r/r.ÚSIGHUPr$ZsighupÚSIGINTZ terminateÚSIGTERM)r'r(r(r)r%EszApplication._init_signalscCs6|jdk rtdƒ‚tjj||||ƒ|_tj|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_dbuscCsj|jdk rtdƒ‚tjj|jjtjƒ|jjtj ƒ|jjtj ƒ|jj tj ƒ|jj tj ƒƒ|_tj|jƒdS)Nz-Unix socket interface is already initialized.)rr rZ unix_socketZUnixSocketExporterrr!rZCFG_UNIX_SOCKET_PATHZCFG_UNIX_SOCKET_SIGNAL_PATHSZCFG_UNIX_SOCKET_OWNERSHIPZget_intZCFG_UNIX_SOCKET_PERMISIONSZ#CFG_UNIX_SOCKET_CONNECTIONS_BACKLOGr3)r'r(r(r)Úattach_to_unix_socketQs    z!Application.attach_to_unix_socketcCstj|jƒdS)N)rZregister_objectr$)r'r(r(r)Úregister_controller\szApplication.register_controllerc Cs´tj|ƒtj|gggtjƒ\}}}t|ƒdkrBtj|ƒtdƒ‚tj|dƒ}tj|ƒt|ƒdkrltdƒ‚ytj d|ƒd}Wntj k ržtdƒ‚YnX|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_parentcCs¾||_|jƒyltjj|jƒ}tjj|ƒs4tj|ƒtjtj|jtj tj Btj Bdƒdƒ�}|j dtj ƒƒWdQRXWn>ttfk r¸}ztjd|jt|ƒfƒWYdd}~XnXdS)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Úfr@r(r(r)Úwrite_pid_filexs  ( zApplication.write_pid_filecCs^tjj|jƒrZytj|jƒWn:tk rX}ztjd|jt|ƒfƒWYdd}~XnXdS)Nz&cannot remove existing PID file %s, %s) rrGrIr&ÚunlinkrRrZwarningrU)r'r@r(r(r)rF…s zApplication._delete_pid_filecCs*tj|ƒtjdƒtjƒtjdƒytjƒ}|dkrBtjdƒWn^tk r¢}zBt j dt |ƒƒt j ddƒ}tj||ƒtj|ƒtdƒ‚WYdd}~XnXtdd ƒ}tj|jƒtjjƒƒtj|jƒtjjƒƒtj|jƒtjjƒƒ|j|ƒt jd ƒt j dd ƒ}tj||ƒtj|ƒdS) zy Finishes daemonizing process, writes a PID file and signalizes to the parent that the initialization is complete. ú/rz"cannot daemonize, fork() error: %sr9Fz'Cannot daemonize, second fork() failed.Nz /dev/nullzw+zsuccessfully daemonizedT)rr:ÚchdirÚsetsidÚumaskÚforkÚsysÚexitrRrrTrUr>ÚpackrPr rLÚdup2ÚfilenoÚstdinÚstdoutÚstderrrXÚdebug)r'rVrArBÚpidr@rCÚfdr(r(r)Ú_daemonize_childŒs.           zApplication._daemonize_childcCs¶tjƒ}y tjƒ}WnFtk rZ}z*tj|dƒtj|dƒtdƒ‚WYdd}~XnXy2|dkr||j|Žtjdƒn|j |f|žŽWn"|dkr¢‚n tjdƒYnXdS)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^rRr:r rDr_r`rj)r'rVZparent_child_fdsZ child_pidr@r(r(r)Ú daemonize®s    zApplication.daemonizecCs|jS)N)r#)r'r(r(r)r ÉszApplication.daemoncCs|jS)N)r$)r'r(r(r)r ÍszApplication.controllercCsj|r|jjtjdƒ|jjtjtjƒs0tjdƒ|jj ƒ}|jjtjtjƒrTt j ƒ|j dk 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ÚsetrZ CFG_DAEMONrZCFG_DEF_DAEMONrÚwarnr$ÚrunrÚstopr&rF)r'r Úresultr(r(r)roÑs   zApplication.run)NN)Ú__name__Ú __module__Ú __qualname__r*r/r%r5r6r7rDrZPID_FILErXrFrjrlÚpropertyr r ror(r(r(r)rs +  "  )rrrrrrrrZtuned.exceptionsr Z tuned.logsZ tuned.versionÚr r r.rr_r;r>Z tuned.constsrZtuned.utils.global_configr Zlogsr!rÚ__all__Úobjectrr(r(r(r)Ús$