Revision e3e66f02 lib/utils.py

b/lib/utils.py
569 569
  return [tup[1] for tup in to_sort]
570 570

  
571 571

  
572
def CheckDaemonAlive(pid_file, process_string):
573
  """Check wether the specified daemon is alive.
574

  
575
  Args:
576
   - pid_file: file to read the daemon pid from, the file is
577
               expected to contain only a single line containing
578
               only the PID
579
   - process_string: a substring that we expect to find in
580
                     the command line of the daemon process
581

  
582
  Returns:
583
   - True if the daemon is judged to be alive (that is:
584
      - the PID file exists, is readable and contains a number
585
      - a process of the specified PID is running
586
      - that process contains the specified string in its
587
        command line
588
      - the process is not in state Z (zombie))
589
   - False otherwise
590

  
591
  """
592
  try:
593
    pid_file = file(pid_file, 'r')
594
    try:
595
      pid = int(pid_file.readline())
596
    finally:
597
      pid_file.close()
598

  
599
    cmdline_file_path = "/proc/%s/cmdline" % (pid)
600
    cmdline_file = open(cmdline_file_path, 'r')
601
    try:
602
      cmdline = cmdline_file.readline()
603
    finally:
604
      cmdline_file.close()
605

  
606
    if not process_string in cmdline:
607
      return False
608

  
609
    stat_file_path =  "/proc/%s/stat" % (pid)
610
    stat_file = open(stat_file_path, 'r')
611
    try:
612
      process_state = stat_file.readline().split()[2]
613
    finally:
614
      stat_file.close()
615

  
616
    if process_state == 'Z':
617
      return False
618

  
619
  except (IndexError, IOError, ValueError):
620
    return False
621

  
622
  return True
623

  
624

  
625 572
def TryConvert(fn, val):
626 573
  """Try to convert a value ignoring errors.
627 574

  

Also available in: Unified diff