3 Yd@sddlmZmZmZmZmZmZmZddlm Z ddl Zddl Zddl m Z ddl mZddlZddlZddlZddlZddlZddljZddlmZejjZdgZGd ddeZdS) )storageunitsmonitorspluginsprofilesexportshardware)TunedExceptionN) controller)daemon) GlobalConfig Applicationc@seZdZdddZddZddZdd Zd d Zd d ZddZ e j fddZ ddZ ddZe j fddZeddZeddZddZdS)rNc Csptjdtjjtjdfd|_d|_t j }t j |}|dkrJt n||_ |j jtjrjtjdn tjdtj}|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: %sz8dynamic tuning is enabled (can be overridden in plugins)z#dynamic tuning is globally disabledudev_buffer_size) buffer_size)0loginfotunedversionZTUNED_VERSION_STRosuname_dbus_exporter_unix_socket_exporterrZPickleProviderZFactoryr configget_boolconstsZCFG_DYNAMIC_TUNINGrZ RepositoryZget_sizeZCFG_DEF_UDEV_BUFFER_SIZErZ InventoryZ DeviceMatcherZDeviceMatcherUdevrinstancer variables VariablesintgetZCFG_DEFAULT_INSTANCE_PRIORITYZ!CFG_DEF_DEFAULT_INSTANCE_PRIORITYrZManagerZMergerZLocatorZLOAD_DIRECTORIESLoaderr 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_loaderr(!/usr/lib/python3.6/application.py__init__s<         zApplication.__init__csfdd}tj|dS)Ncs|krdS)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|jjdS)N)r/r.SIGHUPr$ZsighupSIGINTZ terminateSIGTERM)r'r(r(r)r%EszApplication._init_signalscCs4|jdk rtdtjj||||_tj|jdS)Nz&DBus interface is already initialized.)rr rZdbusZ DBusExporterregister_exporter)r'Zbus_nameZ object_nameZinterface_namer(r(r)attach_to_dbusJs zApplication.attach_to_dbuscCsj|jdk rtdtjj|jjtj|jjtj |jjtj |jj tj |jj tj |_tj|jdS)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|jdS)N)rZregister_objectr$)r'r(r(r)register_controller\szApplication.register_controllerc Cstj|tj|gggtj\}}}t|dkrBtj|tdtj|d}tj|t|dkrltdytj d|d}Wntj k rtdYnX|dkrtd 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) rcloseselectrZDAEMONIZE_PARENT_TIMEOUTlenr readstructunpackerror)r' parent_in_fd child_out_fdZ read_readyZdropZresponsevalr(r(r)_daemonize_parent_s      zApplication._daemonize_parentcCs||_|jyltjj|j}tjj|s4tj|tjtj|jtj tj Btj Bdd}|j dtj WdQRXWn>ttfk r}ztjd|jt|fWYdd}~XnXdS)Niwz%dzcannot write the PID to %s: %s)r&_delete_pid_filerpathdirnameexistsmakedirsfdopenopenO_CREATO_TRUNCO_WRONLYwritegetpidOSErrorIOErrorrcriticalstr)r'pid_fileZdir_namefr?r(r(r)write_pid_filexs  ( zApplication.write_pid_filecCs^tjj|jrZytj|jWn:tk rX}ztjd|jt|fWYdd}~XnXdS)Nz&cannot remove existing PID file %s, %s) rrFrHr&unlinkrQrZwarningrT)r'r?r(r(r)rEs zApplication._delete_pid_filecCs*tj|tjdtjtjdytj}|dkrBtjdWn^tk r}zBt j dt |t j dd}tj||tj|tdWYdd}~XnXtdd }tj|jtjjtj|jtjjtj|jtjj|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: %sr8Fz'Cannot daemonize, second fork() failed.Nz /dev/nullzw+zsuccessfully daemonizedT)rr9chdirsetsidumaskforksysexitrQrrSrTr=packrOr rKdup2filenostdinstdoutstderrrWdebug)r'rUr@rApidr?rBfdr(r(r)_daemonize_childs.           zApplication._daemonize_childcCstj}y tj}WnFtk rZ}z*tj|dtj|dtdWYdd}~XnXy2|dkr||j|tjdn|j |f|Wn"|dkrn tjdYnXdS)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) rpiper]rQr9r rCr^r_ri)r'rUZparent_child_fdsZ child_pidr?r(r(r) daemonizes    zApplication.daemonizecCs|jS)N)r#)r'r(r(r)r szApplication.daemoncCs|jS)N)r$)r'r(r(r)r szApplication.controllercCsj|r|jjtjd|jjtjtjs0tjd|jj }|jjtjtjrTt 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)rsetrZ CFG_DAEMONrZCFG_DEF_DAEMONrwarnr$runrstopr&rE)r'r resultr(r(r)rns   zApplication.run)NN)__name__ __module__ __qualname__r*r/r%r4r5r6rCrZPID_FILErWrErirkpropertyr r rnr(r(r(r)rs +  "  )rrrrrrrrZtuned.exceptionsr Z tuned.logsZ tuned.versionr r r.rr^r:r=Z tuned.constsrZtuned.utils.global_configr Zlogsr!r__all__objectrr(r(r(r)s$