# HG changeset patch
# User Florian Haas <[email protected]>
# Date 1292402996 -3600
# Node ID 8459a4918ad86c93962b8b59dd14d380c1e48eed
# Parent  2b64174a3b9c8404391d5de27b800edb25c1833a
Medium: .ocf-shellfuncs: add ocf_test_pid convenience function

Add an OCF-style function, ocf_test_pid(), to test for a running
process by PID. This function employs the following logic:

* Send the process a 0 signal.
  - If sending the signal succeeds:
    * Check whether /proc/$pid/status exists.
      - If it does:
        * Test whether the process status is Z (zombie, defunct).
          - If it is, return $OCF_ERR_GENERIC.
          - If it is not, return $OCF_SUCCESS.
      - If it does not (as on any non-Linux platform if I'm not
        mistaken), then just assume that the process is running, and
        return $OCF_SUCCESS.
  - If sending the signal succeeds:
    * The process can be safely assumed to not exist. Return
      $OCF_NOT_RUNNING.

The name is deliberately ocf_test_pid not ocf_check_pid, to not create
the false impression that this has anything to do with
OCF_CHECK_LEVEL.

diff -r 2b64174a3b9c -r 8459a4918ad8 heartbeat/.ocf-shellfuncs.in
--- a/heartbeat/.ocf-shellfuncs.in      Tue Dec 14 15:22:39 2010 +0100
+++ b/heartbeat/.ocf-shellfuncs.in      Wed Dec 15 09:49:56 2010 +0100
@@ -117,6 +117,42 @@
        esac
 }
 
+# ocf_test_pid: test for the status of a process identified by ID, and
+# return an OCF compliant status code.
+#
+# Given a process ID, try to send it a 0 signal. If that returns an
+# error, we know that that PID is not in the process table, and the
+# process is certainly not running. If kill does return successfully,
+# then the process may still be a zombie, so test for that too. That
+# test, however, is Linux specific -- so on platforms where
+# /proc/$pid/status does not exist, just be happy with what kill says.
+ocf_test_pid() {
+       local rc
+       local pid
+
+       rc=$OCF_SUCCESS
+       pid=$1
+
+       if kill -s 0 $pid 2>/dev/null; then
+               # Process exists in process table, check its status
+               # (Linux only)
+               if [ -r /proc/$pid/status ]; then
+                       if grep -E "State:[[:space:]]+Z \(zombie\)" 
/proc/$pid/status; then
+                               ocf_log err "Process $pid is defunct"
+                               rc=$OCF_ERR_GENERIC
+                       fi
+               fi
+       else
+               ocf_log debug "Process $pid is dead"
+               rc=$OCF_NOT_RUNNING
+       fi
+
+       if [ $rc -eq $OCF_SUCCESS ]; then
+               ocf_log debug "Process $pid is currently running"
+       fi
+       return $rc
+}
+
 __ocf_set_defaults() {
        __OCF_ACTION="$1"
 
_______________________________________________________
Linux-HA-Dev: [email protected]
http://lists.linux-ha.org/mailman/listinfo/linux-ha-dev
Home Page: http://linux-ha.org/

Reply via email to