Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Feb 2003 19:29:46 -0500
From:      Brent Verner <brent@rcfile.org>
To:        Greg Lewis <glewis@eyesbeyond.com>
Cc:        freebsd-java@FreeBSD.ORG
Subject:   Re: [patch] daemonctl.c modified to use JAVA_HOME environment variable
Message-ID:  <20030208002946.GA4681@rcfile.org>
In-Reply-To: <20030208055654.A20219@misty.eyesbeyond.com>
References:  <20030207163217.GA99329@rcfile.org> <20030208055654.A20219@misty.eyesbeyond.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--sdtB3X0nJg68CQEu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

[2003-02-08 05:56] Greg Lewis said:
| On Fri, Feb 07, 2003 at 11:32:17AM -0500, Brent Verner wrote:
| > The attached patch modified the daemonctl.c program to use JAVA_HOME
| > if set in the environment.
| > 
| > @@ -71,7 +75,16 @@
| >  
| >  	/* Declare variables, like all other good ANSI C programs do :) */
| >  	char *argument;
| > -
| > +    
| > +    /* use JAVA_HOME environemt variable if set */
| > +    char* _java_home = getenv("JAVA_HOME");
| > +    if( _java_home != NULL && strcmp("",_java_home) != 0 ){
| > +      /* TODO: verify that java_prog would exist before overriding the default */
| > +      strncpy(java_home,_java_home,PATH_MAX-1);
| > +      snprintf(java_prog,PATH_MAX-1,"%s/%%JAVA_CMD%%",_java_home);
| > +      using_java_home_env = 1;
| > +    }
| > +    
| 
| Just some minor feedback.
| 
| I don't think this change is safe.  If _java_home is too long to fit
| then java_home won't get null terminated.  I suggest trying strlcpy
| instead.  You might want to think about using strlcat instead of
| snprintf too, and adding some checking for overflow (and appropriate
| error handling if it occurs).

  Here's a better version (thanks for the pointer to strlcat).  If
JAVA_HOME is set (and not empty) in the environment, try to use it
to find a usable JAVA_HOME/bin/java executable.  Use this executable
if found, otherwise the default compile-time executable will be used.
In any case, if JAVA_HOME was checked, an 'info' message is printed,
alerting the user of the attempted command override.

cheers.
  b

-- 
"Develop your talent, man, and leave the world something. Records are 
really gifts from people. To think that an artist would love you enough
to share his music with anyone is a beautiful thing."  -- Duane Allman

--sdtB3X0nJg68CQEu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="daemonctl.c.diff"

--- ports/www/jakarta-tomcat4/files/daemonctl.c.orig	Fri Feb  7 18:13:10 2003
+++ ports/www/jakarta-tomcat4/files/daemonctl.c	Fri Feb  7 18:17:50 2003
@@ -20,6 +20,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/uio.h>
+#include <limits.h>
 
 /* The maximum size of the PID file, in bytes */
 #define MAX_FILE_SIZE			32
@@ -55,7 +56,13 @@
 private void start(void);
 private void stop(void);
 private void restart(void);
+private void setup_java_prog(void);
 
+#define C_JAVA_HOME "%%JAVA_HOME%%"
+#define C_JAVA_PROG "%%JAVA_HOME%%/%%JAVA_CMD%%"
+private int  using_java_home_env = 0;
+private char java_home[PATH_MAX] = C_JAVA_HOME;
+private char java_prog[PATH_MAX] = C_JAVA_PROG;
 
 /**
  * Main function. This function is called when this program is executed.
@@ -314,6 +321,8 @@
 	int stderrLogFile;
 	struct stat sb;
 
+	setup_java_prog(); /* check for and use JAVA_HOME env */
+
 	/* Open and read the PID file */
 	printf(">> Reading PID file (%%PID_FILE%%)...");
 	file = openPIDFile();
@@ -332,36 +341,36 @@
 	}
 
 	/* Check if the JDK home directory is actually a directory */
-	result = stat("%%JAVA_HOME%%", &sb);
+	result = stat(java_home, &sb);
 	if (result != 0) {
 		printf(" [ FAILED ]\n");
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to stat %%JAVA_HOME%%: ");
+		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to stat %s: ",java_home);
 		perror(NULL);
 		exit(ERR_STAT_JAVA_HOME);
 	}
 	if (!S_ISDIR(sb.st_mode)) {
 		printf(" [ FAILED ]\n");
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java home directory %%JAVA_HOME%% is not a directory.\n");
+		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java home directory %s is not a directory.\n",java_home);
 		exit(ERR_JAVA_HOME_NOT_DIR);
 	}
 
 	/* Check if the Java command is actually an executable regular file */
-	result = stat("%%JAVA_HOME%%/%%JAVA_CMD%%", &sb);
+	result = stat(java_prog, &sb);
 	if (result != 0) {
 		printf(" [ FAILED ]\n");
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to stat %%JAVA_HOME%%/%%JAVA_CMD%%: ");
+		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to stat %s: ",java_prog);
 		perror(NULL);
 		exit(ERR_STAT_JAVA_CMD);
 	}
 	if (!S_ISREG(sb.st_mode)) {
 		printf(" [ FAILED ]\n");
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java command %%JAVA_HOME%%/%%JAVA_CMD%% is not a regular file.\n");
+		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java command %s is not a regular file.\n",java_prog);
 		exit(ERR_JAVA_CMD_NOT_FILE);
 	}
-	result = access("%%JAVA_HOME%%/%%JAVA_CMD%%", X_OK);
+	result = access(java_prog, X_OK);
 	if (result != 0) {
 		printf(" [ FAILED ]\n");
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java command %%JAVA_HOME%%/%%JAVA_CMD%% is not executable: ");
+		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Java command %s is not executable: ",java_prog);
 		perror(NULL);
 		exit(ERR_JAVA_CMD_NOT_EXECUTABLE);
 	}
@@ -425,9 +434,9 @@
 		         file using pipe(2) */
 
 		/* Execute the command */
-		execl("%%JAVA_HOME%%/%%JAVA_CMD%%", "%%JAVA_HOME%%/%%JAVA_CMD%%", "-jar", %%JAVA_ARGS%% "%%JAR_FILE%%", %%JAR_ARGS%% NULL);
-
-		fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to start %%APP_TITLE%% %%PORTVERSION%% since '%%JAVA_HOME%%/%%JAVA_CMD%% -jar %%JAR_FILE%%' in %%APP_HOME%%: ");
+		execl(java_prog, java_prog, "-jar", %%JAVA_ARGS%% "%%JAR_FILE%%", %%JAR_ARGS%% NULL);
+		
+        fprintf(stderr, "%%CONTROL_SCRIPT_NAME%%: Unable to start %%APP_TITLE%% %%PORTVERSION%% since '%s -jar %%JAR_FILE%%' in %%APP_HOME%%: ",java_prog);
 		perror(NULL);
 	} else {
 		printf(" [ DONE ]\n");
@@ -482,3 +491,43 @@
 	stop();
 	start();
 }
+
+/*
+ * setup java_home and java_prog.  If JAVA_HOME is defined in the 
+ * environemt, try to use it.  If JAVA_HOME does contain bin/java,
+ * do not use it.
+ */
+private void setup_java_prog(void){
+  char* _java_home;
+
+  /* use JAVA_HOME environemt variable if set */
+  _java_home = getenv("JAVA_HOME");
+  if( _java_home != NULL && strcmp("",_java_home) != 0 ){
+    char _java_prog[PATH_MAX];
+    struct stat st;
+    int rv;
+    strlcpy(_java_prog,_java_home,PATH_MAX);
+    strlcat(_java_prog,"/",PATH_MAX);
+    strlcat(_java_prog,"%%JAVA_CMD%%",PATH_MAX);
+    rv = stat(_java_prog,&st);
+
+    /* make sure _java_prog can be used */
+    if( rv == 0 
+        && S_ISREG(st.st_mode) 
+        && ! access(_java_prog, X_OK) )
+	{
+      strlcpy(java_home,_java_home,PATH_MAX);
+      strlcpy(java_prog,_java_prog,PATH_MAX);
+      using_java_home_env = 1;
+      printf("[info] using JAVA_HOME environment variable (%s)\n",java_home);
+      printf("[info] starting tomcat with %s\n",java_prog);
+    
+    }
+    else
+    {
+      fprintf(stdout,"[info] invalid JAVA_HOME; %s not found\n",_java_prog);
+      fprintf(stdout,"[info] Using default JAVA_HOME (%%JAVA_HOME%%)\n");
+    }
+  }
+}
+

--sdtB3X0nJg68CQEu--

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-java" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030208002946.GA4681>