Bug 49728 - catalina.sh PID file handling not working if started by initscript
Summary: catalina.sh PID file handling not working if started by initscript
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 6.0.29
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-09 10:05 UTC by Peter Bieringer
Modified: 2010-09-30 12:26 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Bieringer 2010-08-09 10:05:07 UTC
the PID file handling was changed in 6.0.24 in catalina.sh, which avoid the use of an initscript to start tomcat proper (e.g. on CentOS/RHEL5). catalina.sh is currently too strict regarding existing PID file and has imho a too lightweight check.

Below is a patch which improves the PID file handling.

It fixes 2 issues.

Issue 1: tomcat won't start, if initscript has already created as root a PID file and changed permissions, that user tomcat would able to write it's PID into this file.

Fix: check existing PID file whether it's non-empty and if yes, check, whether PID is stale

Issue 2: catalina.sh unconditionally tries to remove the given PID file, not testing the case that it has no write access to the directory (e.g. /var/run).

Fix: check before removing a PID file (because this needs write access to pid file directory, which is e.g. /var/run, were user tomcat has no write access)

Pls. include this fix into upstream, thank you.

  Peter

--- catalina.sh 2010-07-19 12:59:45.000000000 +0000
+++ catalina.sh 2010-08-09 13:00:56.000000000 +0000
@@ -311,9 +311,15 @@
elif [ "$1" = "start" ] ; then

  if [ ! -z "$CATALINA_PID" ]; then
-    if [ -f "$CATALINA_PID" ]; then
-      echo "PID file ($CATALINA_PID) found. Is Tomcat still running? Start aborted."
-      exit 1
+    if [ -f "$CATALINA_PID" -a -s "$CATALINA_PID" ]; then
+      echo "Non-empty PID file ($CATALINA_PID) found. Is Tomcat still running?"
+      pid="`cat "$CATALINA_PID"`"
+      if ps -p $pid >/dev/null; then
+        echo "Tomcat is probably still running with PID $pid! Start aborted."
+        exit 1
+      else
+        echo "Tomcat is no longer running (stale PID file)."
+      fi
    fi
  fi

@@ -393,7 +399,11 @@
      while [ $SLEEP -ge 0 ]; do
        kill -0 `cat $CATALINA_PID` >/dev/null 2>&1
        if [ $? -gt 0 ]; then
-          rm $CATALINA_PID
+          if [ -w `dirname "$CATALINA_PID"` ]; then
+            rm $CATALINA_PID
+          else
+            echo "Non-removable PID file found ($CATALINA_PID)."
+          fi
          break
        fi
        if [ $SLEEP -gt 0 ]; then
@@ -416,7 +426,11 @@
      if [ -f "$CATALINA_PID" ]; then
        echo "Killing: `cat $CATALINA_PID`"
        kill -9 `cat $CATALINA_PID`
-        rm $CATALINA_PID
+        if [ -w `dirname "$CATALINA_PID"` ]; then
+          rm $CATALINA_PID
+        else
+          echo "Non-removable PID file found ($CATALINA_PID)."
+        fi
      fi
    fi
  fi
Comment 1 Mark Thomas 2010-09-30 12:26:51 UTC
This has been fixed in 6.0.x and will be included in 6.0.30 onwards.