3 ÆÀf`.ã @s$ddlmZddlmZddlmZddlmZddlmZmZddlZddl Zddl Z ddl Z ddl Z ddl Z ddlZddlZddd d d d d ddg ZdgZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„ZGd#d$„d$eƒZGd%d&„d&eƒZejjGd'd(„d(ej j!ƒƒZ"dS))é)Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)ÚloggerÚ_NZkernelz kernel-rtZglibczlinux-firmwareZsystemdÚdbusz dbus-brokerz dbus-daemonZ microcode_ctlÚzlibc sütjj|ƒstƒStƒ}xjtj|ƒD]\}tjj|ƒs$|jdƒ rBq$ttjj||ƒƒ�&}x|D]}|j |j ƒ|fƒq\WWdQRXq$Wtƒ‰x4|j j ƒj ƒjdd„|Dƒd�D]}ˆj |jƒq¬Wx6‡fdd„|DƒD] \}}tjtdj||d�ƒƒqÔWˆS) z‡ Provide filepath as string if single dir or list of strings Return set of package names contained in files under filepath z.confNcSsh|] }|d’qS)r©)Ú.0Úxr r ú&/usr/lib/python3.6/needs_restarting.pyú Bsz'get_options_from_dir..)Únamecsh|]}|dˆkr|’qS)rr )r r )Úpackagesr r rDsz`No installed package found for package name "{pkg}" specified in needs-restarting file "{file}".)ÚpkgÚfile)ÚosÚpathÚexistsÚsetÚlistdirÚisdirÚendswithÚopenÚjoinÚaddÚrstripÚsackÚqueryÚ installedÚfilterrrÚwarningrÚformat)ÚfilepathÚbaseZoptionsrÚfpÚlinerrr )rr Úget_options_from_dir0s"  $&r(ccsžx˜tƒD]Ž\}}y<|dk r(|t|ƒkr(wt|ddd��}|jƒ}WdQRXWn"tk rntjd|ƒwYnXx$|D]}t||ƒ}|dk rv|VqvWqWdS)NÚrÚreplace)ÚerrorszFailed to read PID %d's smaps.)Ú list_smapsÚ owner_uidrÚ readlinesÚEnvironmentErrorrr"Úsmap2opened_file)ÚuidÚpidÚsmapsZ smaps_fileÚlinesr'Úofiler r r Úlist_opened_filesKs   r6c csNxHtjdƒD]:}y t|ƒ}Wntk r2w YnXd|}||fVq WdS)Nz/procz/proc/%d/smaps)rrÚintÚ ValueError)Zdir_r2r3r r r r,\s r,cstƒ‰i‰‡‡‡fdd„}|S)Ncs,ˆj|ˆƒ}|ˆk r|Sˆ|ƒ}|ˆ|<|S)N)Úget)ZparamÚval)ÚcacheÚfuncÚsentinelr r Úwrapperis  zmemoize..wrapper)Úobject)r<r>r )r;r<r=r Úmemoizefsr@cCstj|ƒtjS)N)rÚstatÚST_UID)Úfnamer r r r-ssr-cCs$|jƒj|d�jƒ}|r |dSdS)N)rr)rr!Úrun)rrCZmatchesr r r Úowning_packagewsrEc CsPd|}t|ƒ�}tjj|jƒƒ}WdQRXdj|jdƒƒ}td||fƒdS)Nz/proc/%d/cmdlineú úz%d : %s)rÚdnfZi18nZucdÚreadrÚsplitÚprint)r2ZcmdlineZ cmdline_fileZcommandr r r Ú print_cmd~s  rLc Cs¤tjƒ}|jddƒ}tj|dƒ}d}y|jd|j|ƒƒ}Wn<tjk rv}zt|ƒ}tjdj ||ƒƒdSd}~XnXtj|dd�}|j ddƒ}|j d ƒr |SdS) Nzorg.freedesktop.systemd1z/org/freedesktop/systemd1z org.freedesktop.systemd1.Managerz)Failed to get systemd unit for PID {}: {}zorg.freedesktop.DBus.Properties)Zdbus_interfacezorg.freedesktop.systemd1.UnitZIdz.service) rZ SystemBusZ get_objectZ InterfaceZ GetUnitByPIDZ DBusExceptionÚstrrr"r#ZGetr) r2ZbusZsystemd_manager_objectZsystemd_manager_interfaceZ service_proxyÚeÚmsgZservice_propertiesrr r r Úget_service_dbus†s0  rPcCsn|jdƒ}|dkrdS|jdƒdkr(dS||d…jƒ}|jdƒ}|dkrVt||dƒSt||d|…dƒSdS)Nú/rz00:z (deleted)FT)ÚfindÚstripÚrfindÚ OpenedFile)r2r'ZslashÚfnZ suffix_indexr r r r0¨s   r0c@s*eZdZejdƒZdd„Zedd„ƒZdS)rUz^(.+);[0-9A-Fa-f]{8,}$cCs||_||_||_dS)N)Údeletedrr2)Úselfr2rrWr r r Ú__init__ºszOpenedFile.__init__cCs(|jr"|jj|jƒ}|r"|jdƒS|jS)a;Calculate the name of the file pre-transaction. In case of a file that got deleted during the transactionm, possibly just because of an upgrade to a newer version of the same file, RPM renames the old file to the same name with a hexadecimal suffix just before delting it. é)rWÚRE_TRANSACTION_FILEÚmatchrÚgroup)rXr\r r r Ú presumed_name¿s  zOpenedFile.presumed_nameN) Ú__name__Ú __module__Ú __qualname__ÚreÚcompiler[rYÚpropertyr^r r r r rU·s rUc@s4eZdZdd„Zedd„ƒZedd„ƒZdd„Zd S) Ú ProcessStartcCs|jƒ|_|jƒ|_dS)N)Ú get_boot_timeÚ boot_timeÚget_sc_clk_tckÚ sc_clk_tck)rXr r r rYÒs zProcessStart.__init__c Cshttjdƒjƒ}tjjdƒrdtddƒ�8}|jƒjƒj ƒdjƒ}tt j ƒt |ƒƒ}t ||ƒSQRX|S)a  We have two sources from which to derive the boot time. These values vary depending on containerization, existence of a Real Time Clock, etc. For our purposes we want the latest derived value. - st_mtime of /proc/1 Reflects the time the first process was run after booting This works for all known cases except machines without a RTC - they awake at the start of the epoch. - /proc/uptime Seconds field of /proc/uptime subtracted from the current time Works for machines without RTC iff the current time is reasonably correct. Does not work on containers which share their kernel with the host - there the host kernel uptime is returned z/proc/1z /proc/uptimeÚrbrN) r7rrAÚst_mtimerÚisfilerÚreadlinerSrJÚtimeÚfloatÚmax)Zproc_1_boot_timeÚfZuptimeZproc_uptime_boot_timer r r rfÖs  zProcessStart.get_boot_timecCstjtjdƒS)NÚ SC_CLK_TCK)rÚsysconfÚ sysconf_namesr r r r rhïszProcessStart.get_sc_clk_tckc CsLd|}t|ƒ�}|jƒjƒjƒ}WdQRXt|dƒ}||j}|j|S)Nz /proc/%d/staté)rrIrSrJr7rirg)rXr2Zstat_fnZ stat_fileZstatsZticks_after_bootZsecs_after_bootr r r Ú__call__ós    zProcessStart.__call__N)r_r`rarYÚ staticmethodrfrhrvr r r r reÑs  rec@s4eZdZd ZedƒZedd„ƒZdd„Zdd„Z d S) ÚNeedsRestartingCommandúneeds-restartingz/determine updated binaries that need restartingcCsF|jdddtdƒd�|jdddtdƒd�|jd d dtd ƒd�dS) Nz-uz --useronlyÚ store_truez#only consider this user's processes)ÚactionÚhelpz-rz --reboothintzKonly report whether a reboot is required (exit code 1) or not (exit code 0)z-sz --servicesz%only report affected systemd services)Ú add_argumentr)Úparserr r r Ú set_argparsers      z$NeedsRestartingCommand.set_argparsercCs|jj}d|_dS)NT)ÚcliÚdemandsZsack_activation)rXr�r r r Ú configure sz NeedsRestartingCommand.configurecCsNtƒ}tjt|jjƒ}t|ƒ}ttj j |jj j dƒ|jƒ}t j|ƒ|jj�r‚tƒ}tƒ}|jjjƒjƒ}x,|jt d�D]}|j|jkrx|j|jƒqxW|jdddgd�}t|ƒdkräx,|jtd�D]}|j|jkrÄ|j|jƒqÄW|sî|�rfttdƒƒxt|ƒD]} td| ƒ�qWxt|ƒD]} td | ƒ�q$Wtƒttd ƒƒttd ƒd ƒtjj ƒ‚nttd ƒƒttdƒƒdStƒ} |jj!�rštj"ƒnd} xHt#| ƒD]<} || j$ƒ}|dk�rÄ�q¨|j|| j%ƒk�r¨| j| j%ƒ�q¨W|jj&�r.tdd„t| ƒDƒƒ} x | D]} | dk �rt| ƒ�qWdSxt| ƒD]}t'|ƒ�q8WdS)Nz#etc/dnf/plugins/needs-restarting.d/)rrz dbus-daemonz dbus-brokerrz;Core libraries or services have been updated since boot-up:z * %sz8 * %s (dependency of dbus. Recommending reboot of dbus)z2Reboot is required to fully utilize these updates.zMore information:z)https://access.redhat.com/solutions/27943z>No core libraries or services have been updated since boot-up.zReboot should not be necessary.cSsg|] }t|ƒ‘qSr )rP)r r2r r r ú Bsz.NeedsRestartingCommand.run..)(reÚ functoolsÚpartialrEr%rr@r(rrrZconfZ installrootÚ NEED_REBOOTÚextendZoptsZ reboothintrrr r!Z installtimergrrÚlenÚNEED_REBOOT_DEPENDS_ON_DBUSrKrÚsortedrHÚ exceptionsÚErrorZuseronlyÚgeteuidr6r^r2ZservicesrL)rXZ process_startZ owning_pkg_fnÚoptZ need_rebootZneed_reboot_depends_on_dbusr rZdbus_installedrZ stale_pidsr1r5Únamesr2r r r rDsd                zNeedsRestartingCommand.runN)ry) r_r`raÚaliasesrZsummaryrwrr‚rDr r r r rxüs  rx)#Z __future__rrrrZdnfpluginscorerrrHZdnf.clirr„rrbrArnr†r‰r(r6r,r@r-rErLrPr0r?rUreZpluginZregister_commandr€ZCommandrxr r r r Ús:      "+