
    (Gjo	                        d Z ddlmZ ddlZddlZddlZddlZddlmZ ddZ	ddZ
dddZddZedk    r ej         e                       dS dS )u  SIGKILL any process left in this systemd unit's cgroup.

Runs as ``ExecStopPost=`` so it only fires after the gateway's main process
has exited. The gateway already reaps its own tool subprocesses on a clean
shutdown; this is the safety net for long-lived helpers it doesn't track
(``adb``, platform bridges, etc.) that would otherwise be orphaned in the
cgroup and block ``Restart=always`` — issue #37454.

We deliberately iterate ``cgroup.procs`` and send per-PID SIGKILLs instead
of writing ``1`` to ``cgroup.kill``: the original failure mode in #37454
was the kernel returning ``EINVAL`` on the cgroup-wide kill, while per-PID
signal delivery uses a separate code path that still works.
    )annotationsN)Pathreturn
str | Nonec                    	 t          d                              d          } n# t          $ r Y dS w xY wt          j        d| t          j                  }|sdS |                    d                                          S )z;Return the cgroup v2 path for the calling process, or None.z/proc/self/cgrouputf-8encodingNz	^0::(.+)$   )r   	read_textOSErrorresearch	MULTILINEgroupstrip)textmatchs     </home/rurouni/.hermes/hermes-agent/gateway/cgroup_cleanup.py_own_cgroup_pathr      s    '((22G2DD   ttIlD",77E t;;q>>!!!s   #& 
44cgroup_pathstr	list[int]c                F   t          d|  d          }	 |                    d          }n# t          $ r g cY S w xY wg }|                                D ]L}|                                }|s	 |                    t          |                     =# t          $ r Y Iw xY w|S )Nz/sys/fs/cgroupz/cgroup.procsr   r	   )r   r   r   
splitlinesr   appendint
ValueError)r   
procs_filerawpidslines        r   _read_cgroup_pidsr#   $   s    A{AAABBJ""G"44   			D    zz|| 		KKD		"""" 	 	 	H	Ks   , ;;."B
BBr   c                
   | t                      } | sdS t          j                    }d}t          |           D ]I}||k    r		 t          j        |t
          j                   |dz  }/# t          $ r Y ;t          $ r Y Fw xY w|S )zPSIGKILL every PID in the cgroup other than the caller. Returns the count killed.Nr   r   )	r   osgetpidr#   killsignalSIGKILLProcessLookupErrorPermissionError)r   ownkilledpids       r   reap_cgroupr/   6   s    &(( q
)++CF -- 	 	#::	GC(((aKFF! 	 	 	H 	 	 	H	Ms   $A((
B 4	B ?B c                 "    t                       dS )Nr   )r/        r   mainr3   K   s    MMM1r2   __main__)r   r   )r   r   r   r   )N)r   r   r   r   )r   r   )__doc__
__future__r   r%   r   r(   syspathlibr   r   r#   r/   r3   __name__exitr1   r2   r   <module>r;      s     # " " " " " 				 				  



      	" 	" 	" 	"   $    *   
 zCHTTVV r2   