3 .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_signalscCs6|jdk rtdtjj|||||_tj|jdS)Nz&DBus interface is already initialized.)rr rZdbusZ DBusExporterregister_exporter)r'Zbus_nameZ object_nameZinterface_name namespacer(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) rrGrIr&unlinkrRrZwarningrU)r'r@r(r(r)rFs 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: %sr9Fz'Cannot daemonize, second fork() failed.Nz /dev/nullzw+zsuccessfully daemonizedT)rr:chdirsetsidumaskforksysexitrRrrTrUr>packrPr rLdup2filenostdinstdoutstderrrXdebug)r'rVrArBpidr@rCfdr(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^rRr:r rDr_r`rj)r'rVZparent_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&rF)r'r resultr(r(r)ros   zApplication.run)NN)__name__ __module__ __qualname__r*r/r%r5r6r7rDrZPID_FILErXrFrjrlpropertyr r ror(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$