Bug 449990 - [1.9] --launcher.XXMaxPermSize should not pass
-XX:MaxPermSize= for Oracle VMs >= 8

Change-Id: Ia48f28d9115b04e4772a6a454c6565c0ad57abbb
diff --git a/features/org.eclipse.equinox.executable.feature/library/carbon/eclipseCarbon.c b/features/org.eclipse.equinox.executable.feature/library/carbon/eclipseCarbon.c
index 0170578..500311a 100644
--- a/features/org.eclipse.equinox.executable.feature/library/carbon/eclipseCarbon.c
+++ b/features/org.eclipse.equinox.executable.feature/library/carbon/eclipseCarbon.c
@@ -45,7 +45,7 @@
 char*  defaultVM     = "java";
 char*  vmLibrary	 = "JavaVM";
 char*  shippedVMDir  = "../../../jre/Contents/Home/jre/bin/";
-int isSUN = 0;
+int isSunMaxPermSizeVM = 0;
 
 static void adjustLibraryPath(char * vmLibrary);
 static char * findLib(char * command);
@@ -532,11 +532,15 @@
 			}
 		}
 		if (strstr(buffer, "Java HotSpot(TM)") || strstr(buffer, "OpenJDK")) {
-			isSUN = 1;
+			if (version != NULL) {
+				if (version[0] == '1' && ((int)(version[2] - '0') < 8)) {
+					isSunMaxPermSizeVM = 1;
+				}
+			}
 			break;
 		}
 		if (strstr(buffer, "IBM") != NULL) {
-			isSUN = 0;
+			isSunMaxPermSizeVM = 0;
 			break;
 		}
 	}
@@ -598,7 +602,7 @@
 	if (strstr(cmd, "/JavaVM.framework/") != NULL && (strstr(cmd, "/Current/") != NULL || strstr(cmd, "/A/") != NULL)) {
 		cmd = getJavaHome();
 	}
-	// This is necessary to initialize isSUN
+	// This is necessary to initialize isSunMaxPermSizeVM
 	getJavaVersion(cmd);
 	result = JAVA_FRAMEWORK;
 	if (strstr(cmd, "/JavaVM.framework/") == NULL) {
@@ -858,6 +862,6 @@
 	}
 }
 
-int isSunVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
-	return isSUN;
+int isMaxPermSizeVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
+	return isSunMaxPermSizeVM;
 }
diff --git a/features/org.eclipse.equinox.executable.feature/library/eclipse.c b/features/org.eclipse.equinox.executable.feature/library/eclipse.c
index 2f448da..1d62c7e 100644
--- a/features/org.eclipse.equinox.executable.feature/library/eclipse.c
+++ b/features/org.eclipse.equinox.executable.feature/library/eclipse.c
@@ -968,7 +968,7 @@
 }
 
 static void adjustVMArgs(_TCHAR *javaVM, _TCHAR *jniLib, _TCHAR **vmArgv[]) {
-	/* Sun VMs need some extra perm gen space */
+	/* Sun/Oracle VMs below version 8 need some extra perm gen space */
 	/* Detecting Sun VM is expensive - only do so if necessary */
 	if (permGen != NULL) {
 		int specified = 0, i = -1;
@@ -981,7 +981,7 @@
 			}
 		}
 
-		if (!specified && isSunVM(javaVM, jniLib)) {
+		if (!specified && isMaxPermSizeVM(javaVM, jniLib)) {
 			_TCHAR ** oldArgs = *vmArgv;
 			_TCHAR *newArg = malloc((_tcslen(XXPERMGEN) + _tcslen(permGen) + 1) * sizeof(_TCHAR));
 			_stprintf(newArg, _T_ECLIPSE("%s%s"), XXPERMGEN, permGen);
diff --git a/features/org.eclipse.equinox.executable.feature/library/eclipseNix.c b/features/org.eclipse.equinox.executable.feature/library/eclipseNix.c
index bc57e22..a4439c7 100644
--- a/features/org.eclipse.equinox.executable.feature/library/eclipseNix.c
+++ b/features/org.eclipse.equinox.executable.feature/library/eclipseNix.c
@@ -174,61 +174,42 @@
 	return startJavaJNI(libPath, vmArgs, progArgs, jarFile);
 }
 
-int isSunVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
-	int descriptors[2];
-	int result = 0;
-	int pid = -1;
-	
-	if (javaVM == NULL)
+int isMaxPermSizeVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
+	FILE *fp = NULL;
+	_TCHAR buffer[4096];
+	_TCHAR *version = NULL, *firstChar;
+	int numChars = 0, result = 0;
+	_stprintf(buffer,"%s -version 2>&1", javaVM);
+	fp = popen(buffer, "r");
+	if (fp == NULL) {
 		return 0;
-	
-	/* create pipe, [0] is read end, [1] is write end */
-	if (pipe(descriptors) != 0)
-		return 0; /* error */
-	
-	pid = fork();
-	if (pid == 0 ) {
-		/* child, connect stdout & stderr to write end of the pipe*/
-		dup2(descriptors[1], STDERR_FILENO);
-		dup2(descriptors[1], STDOUT_FILENO);
-		
-		/* close descriptors */
-		close(descriptors[0]);
-		close(descriptors[1]);
-		
-		{
-			/* exec java -version */
-			_TCHAR *args [] = { javaVM, _T_ECLIPSE("-version"), NULL };
-			execv(args[0], args);
-			/* if we make it here, there was a problem with exec, just exit */
-			exit(0);
+	}
+	while (fgets(buffer, sizeof(buffer)-1, fp) != NULL) {
+		if (!version) {
+			firstChar = (_TCHAR *) (_tcschr(buffer, '"') + 1);
+			if (firstChar != NULL)
+				numChars = (int)  (_tcsrchr(buffer, '"') - firstChar);
+
+			/* Allocate a buffer and copy the version string into it. */
+			if (numChars > 0) {
+				version = malloc( numChars + 1 );
+				_tcsncpy(version, firstChar, numChars);
+				version[numChars] = '\0';
+			}
 		}
-	} else if (pid > 0){
-		/* parent */
-		FILE * stream = NULL;
-		int status = 0;
-		close(descriptors[1]);
-		stream = fdopen( descriptors[0], "r");
-		if (stream != NULL) {
-			_TCHAR buffer[256];
-			while ( fgets(buffer, 256, stream) != NULL) {
-				if (_tcsstr(buffer, _T_ECLIPSE("Java HotSpot(TM)")) || _tcsstr(buffer, _T_ECLIPSE("OpenJDK"))) {
+		if (_tcsstr(buffer, "Java HotSpot(TM)") || _tcsstr(buffer, "OpenJDK")) {
+			if (version != NULL) {
+				if (version[0] == '1' && ((int)(version[2] - '0') < 8)) {
 					result = 1;
-					break;
-				}
-				if (_tcsstr(buffer, _T_ECLIPSE("IBM")) != NULL) {
-					result = 0;
-					break;
 				}
 			}
-			fclose(stream);
-			close(descriptors[0]);
+			break;
 		}
-		waitpid(pid, &status, 0);
-	} else {
-		/* failed to fork */
-		close(descriptors[0]);
-		close(descriptors[1]);
+		if (_tcsstr(buffer, "IBM") != NULL) {
+			result = 0;
+			break;
+		}
 	}
+	pclose(fp);
 	return result;
 }
diff --git a/features/org.eclipse.equinox.executable.feature/library/eclipseOS.h b/features/org.eclipse.equinox.executable.feature/library/eclipseOS.h
index a0e0902..3beca9c 100644
--- a/features/org.eclipse.equinox.executable.feature/library/eclipseOS.h
+++ b/features/org.eclipse.equinox.executable.feature/library/eclipseOS.h
@@ -109,8 +109,8 @@
 /* do any platform specific processing of the user vmargs */
 extern void processVMArgs(_TCHAR **vmargs[] );
 
-/* is this a Sun VM, returns 0 if we don't know */
-extern int isSunVM( _TCHAR * javaVM, _TCHAR * jniLib );
+/* is this a Sun/Oracle VM whose version is < 8 (then it needs extra perm gen space), returns 0 if we don't know */
+extern int isMaxPermSizeVM( _TCHAR * javaVM, _TCHAR * jniLib );
 
 /* an array of paths that will need to be on the search path to load the vm shared library */
 extern _TCHAR ** getVMLibrarySearchPath(_TCHAR * vmLibrary);
diff --git a/features/org.eclipse.equinox.executable.feature/library/win32/eclipseWin.c b/features/org.eclipse.equinox.executable.feature/library/win32/eclipseWin.c
index 1e9c1c8..e933882 100644
--- a/features/org.eclipse.equinox.executable.feature/library/win32/eclipseWin.c
+++ b/features/org.eclipse.equinox.executable.feature/library/win32/eclipseWin.c
@@ -577,7 +577,7 @@
 	return startJavaJNI(libPath, vmArgs, progArgs, jarFile);
 }
 
-int isSunVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
+int isMaxPermSizeVM( _TCHAR * javaVM, _TCHAR * jniLib ) {
 	_TCHAR *vm = (jniLib != NULL) ? jniLib : javaVM;
 	int result = 0;
 	DWORD infoSize;