
    Vh](                        d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ d dlmZmZ ddZd Zd Zd Zdd	Zd
 Zd Zd Zd Zy)    )annotationsN)PY2b)to_bytesto_textc                   |rgt         j                  j                  d      s$t        t	        j                  d|d|             S t        t	        j                  d|d|             S t         j                  j                  d      s!t        t	        j                  d| z              S t        t	        j                  d| z              S )z
    This function will check if the service name supplied
    is enabled in any of the sysv runlevels

    :arg name: name of the service to test for
    :kw runlevel: runlevel to check (default: None)
    z/etc/rc0.d/z/etc/init.d/rcz.d/S??z/etc/rcz/etc/init.d/rc?.d/S??%sz/etc/rc?.d/S??%s)ospathisdirboolglob)namerunlevels     L/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/service.pysysv_is_enabledr   ,   s     ww}}]+		$"OPQQDIIXtDEFFww}}]+		";d"BCDDDII047899    c                :    | j                  d      r| }|S d| z  }|S )z
    This function will return the expected path for an init script
    corresponding to the service name supplied.

    :arg name: name or path of the service to test for
    /z/etc/init.d/%s)
startswith)r   results     r   get_sysv_scriptr   >   s-     s M "D(Mr   c                R    t         j                  j                  t        |             S )z
    This function will return True or False depending on
    the existence of an init script corresponding to the service name supplied.

    :arg name: name of the service to test for
    )r	   r
   existsr   )r   s    r   sysv_existsr   M   s     77>>/$/00r   c                    d}t        j                         dk(  rd}nd}| j                  dd      }| j                  ||g      \  }}}|dk(  r|j	                         D ]  }||v sd} |S  |S )zX
    Last resort to find a service by trying to match pattern to programs in memory
    FSunOS-efauxwwpsTr   )platformsystemget_bin_pathrun_command
splitlines)	modulepatternfoundflagspsbinrcpsoutpserrlines	            r   get_psr.   W   s     EG#d+E++UEN;R	Qw$$& 	D$L	
 Lr   c                :    |s| j                  d|d|       yy)a{  
    This function will return an error or exit gracefully depending on check mode status
    and if the service is missing or not.

    :arg module: is an AnsibleModule object, used for it's utility methods
    :arg found: boolean indicating if services were found or not
    :arg service: name of service
    :kw msg: extra info to append to error/success msg when missing
    z%Could not find the requested service z: msgN)	fail_json)r%   r'   servicer1   s       r   fail_if_missingr4   l   s#     gWZ[\ r   c                 d   t        j                         } | dk(  rt        j                  t         j                  t         j                        }t        d      D ]  }||k7  s	t        j                  ||         |t        d      vrt        j                  |       t        j                         } | dkD  rt        j                  d       t        j                         }|dk(  rt        d      t        j                  d       t        j                         } | dkD  rt        j                  d       | S )zk
    This function performs the double fork process to detach from the
    parent process and execute.
    r      z*Unable to detach session while daemonizingr   )r	   forkopendevnullO_RDWRrangedup2close_exitsetsid	Exceptionchdir)pidfdnumsids       r   fork_processrG   z   s    
 '')C
axWWRZZ+ 8 	!CSyC 	!
 U1XHHRL ggi 7HHQK iik"9HII 	ggi7HHQKJr   c                   d}d}	 t        j                         t               }dk(  rqt        j                  d          t        |t              sGt        r!t        j                  t        ||            }n t        j                  t        ||            }g }|D ]  }|j!                  t        ||               t#        j$                  |dt"        j&                  t"        j&                  fd	
      }|j(                  |j*                  g}	|j(                  t-        d      |j*                  t-        d      i}
|	rt/        j.                  |	g |	d      \  }}}||z   |z   s|j1                         ft        |	      D ]W  }||v st        j2                  |j5                         |      }|r|
|xx   t        ||      z  cc<   G|	j7                  |       Y nn|	r|j9                          t;        j<                  |j>                  t        |
|j(                           t        |
|j*                           gt:        j@                        }t        jB                  d   t        ||             t        j                  d          t        jD                  d       y|dk(  r| j	                  d       yt        j                  d          t        jF                  |d       t-        d      }	 t/        j.                  d   gg d   g      \  }}}d   |v r,t        j2                  d   |      }|sn|t        ||      z  }Wt;        jH                  t        ||            S # t        $ r) | j	                  dt        j                                Y qt        $ r9}| j	                  t        |      t        j                                Y d}~d}~ww xY w)a  
    Execute a command while detaching as a daemon, returns rc, stdout, and stderr.

    :arg module: is an AnsibleModule object, used for it's utility methods
    :arg cmd: is a list or string representing the command and options to run

    This is complex because daemonization is hard for people.
    What we do is daemonize a part of this module, the daemon runs the command,
    picks up the return code and output, and returns it to the main process.
    i   surrogate_or_strictz"Error while attempting to fork: %s)r1   	exceptionNr   )errorsFc                 4    t        j                   d         S )N   )r	   r>   )pipes   r   <lambda>zdaemonize.<locals>.<lambda>   s*    vxv~v~  @D  EF  @G  wH r   )shellstdoutstderr
preexec_fn rM   )protocolr7   zSUnable to fork, no exception thrown, probably due to lack of resources, check logs.r0   )%r	   rN   rG   OSErrorr2   	traceback
format_excrA   r   r>   
isinstancelistr   shlexsplitr   append
subprocessPopenPIPErQ   rR   r   selectpollreadfilenoremovewaitpickledumps
returncodeHIGHEST_PROTOCOLwriter?   waitpidloads)r%   cmdchunkrK   rC   excrun_cmdcpfdsoutputrfdwfdefdoutdatareturn_datarN   s                    @r   	daemonizer|      s=    E"FMwwyn ax
a #t$kk(3v">?kk'#f"=>  	7ANN8Af56	7 WE*//R\RaRa  oH  Ixx" ((AbE188QrU3"MM#r3:MCcc	CAFFH$49 ,Ccz!wwszz|U;"3K8D+HHKJJsO,   	
 llALL'&:J2KWU[\]\d\dUeMf#grx  sJ  sJ  K
a(;v>? 	a
	rs 	a


3 e"MM47)R$q'CMCcAw#~wwtAw.xV<<  ||H[@AAQ  eAYMaMaMcd MWS\Y5I5I5KLLMs   M% %.OO.OOc                    t        j                         dk(  rd}nd}| j                  dd      }| j                  |d|      \  }}}|dk(  r|j	                  d      D ]  }||v s y y	)
Nr   r   r   r   T r   
F)r    r!   r"   r#   r\   )r%   r&   psflagsr)   r*   ry   errr-   s           r   check_psr     sy     G# d+E''5'(BCNRc	QwIIdO 	D$	 r   c                    | j                  d      rbdD ]#  }t        j                  j                  |      s# y 	 t	        dd      5 }|j                         j                         }|dk(  cddd       S y# 1 sw Y   yxY w# t        $ r Y yw xY w)	z
    Find out if the machine supports systemd or not
    :arg module: is an AnsibleModule object, used for it's utility methods

    Returns True if the system supports systemd, False if not.
    	systemctl)z/run/systemd/system/z/dev/.run/systemd/z/dev/.systemd/Tz/proc/1/commrsystemdNF)r"   r	   r
   r   r9   readlinestripIOError)r%   canary	init_procinits       r   is_systemd_managedr     s     ;' W 	Fww~~f%	
	nc* )i ))+113y() ) ) 	  		s/   B #A5*	B 5A>:B >B 	BB)N)rT   )
__future__r   r   r	   rg   r    ra   r[   r^   rW   ansible.module_utils.sixr   r   +ansible.module_utils.common.text.convertersr   r   r   r   r   r.   r4   rG   r|   r   r    r   r   <module>r      sZ   : #  	       + I:$1*]'T\B~(r   