3 f`. @s$ddlmZddlmZddlmZddlmZddlmZmZddlZddl Zddl Z ddl Z ddl Z ddl Z ddlZddlZddd d d d d ddg ZdgZddZddZddZddZddZddZddZdd Zd!d"ZGd#d$d$eZGd%d&d&eZejjGd'd(d(ej j!Z"dS)))absolute_import)division)print_function)unicode_literals)logger_NZkernelz kernel-rtZglibczlinux-firmwareZsystemddbusz dbus-brokerz dbus-daemonZ microcode_ctlzlibc stjj|stSt}xjtj|D]\}tjj|s$|jd rBq$ttjj||&}x|D]}|j |j |fq\WWdQRXq$Wtx4|j j j jdd|DdD]}j |jqWx6fdd|DD] \}}tjtdj||dqWS) z Provide filepath as string if single dir or list of strings Return set of package names contained in files under filepath z.confNcSsh|] }|dqS)r).0xr r &/usr/lib/python3.6/needs_restarting.py Bsz'get_options_from_dir..)namecsh|]}|dkr|qS)rr )r r )packagesr r rDsz`No installed package found for package name "{pkg}" specified in needs-restarting file "{file}".)pkgfile)ospathexistssetlistdirisdirendswithopenjoinaddrstripsackquery installedfilterrrwarningrformat)filepathbaseZoptionsrfplinerrr )rr get_options_from_dir0s"  $&r(ccsxtD]\}}y<|dk r(|t|kr(wt|ddd}|j}WdQRXWn"tk rntjd|wYnXx$|D]}t||}|dk rv|VqvWqWdS)Nrreplace)errorszFailed to read PID %d's smaps.) list_smaps owner_uidr readlinesEnvironmentErrorrr"smap2opened_file)uidpidsmapsZ smaps_filelinesr'ofiler r r list_opened_filesKs   r6c csNxHtjdD]:}y t|}Wntk r2w YnXd|}||fVq WdS)Nz/procz/proc/%d/smaps)rrint ValueError)Zdir_r2r3r r r r,\s r,cstifdd}|S)Ncs,j|}|k r|S|}||<|S)N)get)Zparamval)cachefuncsentinelr r wrapperis  zmemoize..wrapper)object)r<r>r )r;r<r=r memoizefsr@cCstj|tjS)N)rstatST_UID)fnamer r r r-ssr-cCs$|jj|dj}|r |dSdS)N)rr)rr!run)rrCZmatchesr r r owning_packagewsrEc CsPd|}t|}tjj|j}WdQRXdj|jd}td||fdS)Nz/proc/%d/cmdline z%d : %s)rdnfZi18nZucdreadrsplitprint)r2ZcmdlineZ cmdline_fileZcommandr r r print_cmd~s  rLc Cstj}|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 DBusExceptionstrrr"r#ZGetr) r2ZbusZsystemd_manager_objectZsystemd_manager_interfaceZ service_proxyemsgZservice_propertiesrr r r get_service_dbuss0  rPcCsn|jd}|dkrdS|jddkr(dS||dj}|jd}|dkrVt||dSt||d|dSdS)N/rz00:z (deleted)FT)findstriprfind OpenedFile)r2r'ZslashfnZ suffix_indexr r r r0s   r0c@s*eZdZejdZddZeddZdS)rUz^(.+);[0-9A-Fa-f]{8,}$cCs||_||_||_dS)N)deletedrr2)selfr2rrWr r r __init__szOpenedFile.__init__cCs(|jr"|jj|j}|r"|jdS|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. )rWRE_TRANSACTION_FILEmatchrgroup)rXr\r r r presumed_names  zOpenedFile.presumed_nameN) __name__ __module__ __qualname__recompiler[rYpropertyr^r r r r rUs rUc@s4eZdZddZeddZeddZddZd S) ProcessStartcCs|j|_|j|_dS)N) get_boot_time boot_timeget_sc_clk_tck sc_clk_tck)rXr r r rYs zProcessStart.__init__c Cshttjdj}tjjdrdtdd8}|jjj 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/uptimerbrN) r7rrAst_mtimerisfilerreadlinerSrJtimefloatmax)Zproc_1_boot_timefZuptimeZproc_uptime_boot_timer r r rfs  zProcessStart.get_boot_timecCstjtjdS)N SC_CLK_TCK)rsysconf sysconf_namesr r r r rhszProcessStart.get_sc_clk_tckc CsLd|}t|}|jjj}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 res  rec@s4eZdZd ZedZeddZddZddZ d S) NeedsRestartingCommandneeds-restartingz/determine updated binaries that need restartingcCsF|jdddtdd|jdddtdd|jd d dtd ddS) Nz-uz --useronly store_truez#only consider this user's processes)actionhelpz-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)clidemandsZsack_activation)rXrr r r configure sz NeedsRestartingCommand.configurecCsNt}tjt|jj}t|}ttj j |jj j d|j}t j||jjrt}t}|jjjj}x,|jt dD]}|j|jkrx|j|jqxW|jdddgd}t|dkrx,|jtdD]}|j|jkr|j|jqW|s|rfttdxt|D]} td| qWxt|D]} td | q$Wtttd ttd d tjj nttd ttddSt} |jj!rtj"nd} xHt#| D]<} || j$}|dkrĐq|j|| j%kr| j| j%qW|jj&r.tddt| 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 functoolspartialrEr%rr@r(rrrZconfZ installroot NEED_REBOOTextendZoptsZ reboothintrrr r!Z installtimergrrlenNEED_REBOOT_DEPENDS_ON_DBUSrKrsortedrH exceptionsErrorZuseronlygeteuidr6r^r2ZservicesrL)rXZ process_startZ owning_pkg_fnoptZ need_rebootZneed_reboot_depends_on_dbusr rZdbus_installedrZ stale_pidsr1r5namesr2r r r rDsd                zNeedsRestartingCommand.runN)ry) r_r`raaliasesrZsummaryrwrrrDr r r r rxs  rx)#Z __future__rrrrZdnfpluginscorerrrHZdnf.clirrrrbrArnrrr(r6r,r@r-rErLrPr0r?rUreZpluginZregister_commandrZCommandrxr r r r s:      "+