def KillHungManagedSoftwareUpdate():
  """Kill hung managedsoftwareupdate instances, if any can be found.

  Returns:
    True if a managedsoftwareupdate instance was killed, False otherwise.
  """
  rc, stdout, stderr = Exec(['/bin/ps', '-eo', 'pid,ppid,lstart,command'])
  if rc != 0 or not stdout or stderr:
    return False

  pids = {}
  msu_pids = []

  for l in stdout.splitlines():
    a = l.split()
    if len(a) < 8:
      continue

    try:
      pids[int(a[0])] = {
          'ppid': int(a[1]),
          'lstart': datetime.datetime(*time.strptime(' '.join(a[2:7]))[0:7]),
          'command': '\t'.join(a[7:]),
      }
    except ValueError:
      continue

    if re.search(r'(MacOS\/Python|python)', a[7], re.IGNORECASE):
      if len(a) > 8 and a[8].find('managedsoftwareupdate') > -1:
        msu_pids.append(int(a[0]))

  now = datetime.datetime.now()
  kill = []

  for pid in msu_pids:
    if (now - pids[pid]['lstart']) >= HUNG_MSU_TIMEOUT:
      for opid in pids:
        if pids[opid]['ppid'] == pid:
          kill.append(opid)  # child
      kill.append(pid)  # parent last

  for pid in kill:
    if pid == 1:  # sanity check
      continue
    try:
      logging.warning('Sending SIGKILL to pid %d', pid)
      os.kill(pid, signal.SIGKILL)
    except __HOLE__ as e:
      # if the process died between ps and now we're OK, otherwise log error.
      if e.args[0] != errno.ESRCH:
        logging.warning('OSError on kill(%d, SIGKILL): %s', pid, str(e))

  return bool(len(kill))
dataset/ETHPy150Open google/simian/src/simian/mac/client/flight_common.py/KillHungManagedSoftwareUpdate