예제 #1
0
int prepare(const char *lpCmdLine) {
	char tmp[MAX_ARGS] = {0};
	hModule = GetModuleHandle(NULL);
	if (hModule == NULL) {
		return FALSE;
	}

	// Get executable path
	char exePath[_MAX_PATH] = {0};
	int pathLen = getExePath(exePath);
	if (pathLen == -1) {
		return FALSE;
	}

	// Initialize logging
    if (strstr(lpCmdLine, "--l4j-debug") != NULL) {
		hLog = openLogFile(exePath, pathLen);
		if (hLog == NULL) {
			return FALSE;
		}
		debug("\n\nCmdLine:\t%s %s\n", exePath, lpCmdLine);
	}

    setWow64Flag();

	// Set default error message, title and optional support web site url.
	loadString(SUPPORT_URL, errUrl);
	loadString(ERR_TITLE, errTitle);
	if (!loadString(STARTUP_ERR, errMsg)) {
		return FALSE;
	}

	// Single instance
	loadString(MUTEX_NAME, mutexName);
	if (*mutexName) {
		SECURITY_ATTRIBUTES security;
		security.nLength = sizeof(SECURITY_ATTRIBUTES);
		security.bInheritHandle = TRUE;
		security.lpSecurityDescriptor = NULL;
		CreateMutexA(&security, FALSE, mutexName);
		if (GetLastError() == ERROR_ALREADY_EXISTS) {
			debug("Instance already exists.");
			return ERROR_ALREADY_EXISTS;
		}
	}

	// Working dir
	char tmp_path[_MAX_PATH] = {0};
	GetCurrentDirectory(_MAX_PATH, oldPwd);
	if (loadString(CHDIR, tmp_path)) {
		strncpy(workingDir, exePath, pathLen);
		appendPath(workingDir, tmp_path);
		_chdir(workingDir);
		debug("Working dir:\t%s\n", workingDir);
	}

	// Use bundled jre or find java
	if (loadString(JRE_PATH, tmp_path)) {
		char jrePath[MAX_ARGS] = {0};
		expandVars(jrePath, tmp_path, exePath, pathLen);
		debug("Bundled JRE:\t%s\n", jrePath);
		if (jrePath[0] == '\\' || jrePath[1] == ':') {
			// Absolute
			strcpy(cmd, jrePath);
		} else {
			// Relative
			strncpy(cmd, exePath, pathLen);
			appendPath(cmd, jrePath);
		}
    }
	if (!isJrePathOk(cmd)) {
		if (!loadString(JAVA_MIN_VER, javaMinVer)) {
			loadString(BUNDLED_JRE_ERR, errMsg);
			return FALSE;
		}
		loadString(JAVA_MAX_VER, javaMaxVer);
		if (!findJavaHome(cmd, loadInt(JDK_PREFERENCE))) {
			loadString(JRE_VERSION_ERR, errMsg);
			strcat(errMsg, " ");
			strcat(errMsg, javaMinVer);
			if (*javaMaxVer) {
				strcat(errMsg, " - ");
				strcat(errMsg, javaMaxVer);
			}
			loadString(DOWNLOAD_URL, errUrl);
			return FALSE;
		}
		if (!isJrePathOk(cmd)) {
			loadString(LAUNCHER_ERR, errMsg);
			return FALSE;
		}
	}

    // Append a path to the Path environment variable
	char jreBinPath[_MAX_PATH];
	strcpy(jreBinPath, cmd);
	strcat(jreBinPath, "\\bin");
	if (!appendToPathVar(jreBinPath)) {
		return FALSE;
	}

	// Set environment variables
	char envVars[MAX_VAR_SIZE] = {0};
	loadString(ENV_VARIABLES, envVars);
	char *var = strtok(envVars, "\t");
	while (var != NULL) {
		char *varValue = strchr(var, '=');
		*varValue++ = 0;
		*tmp = 0;
		expandVars(tmp, varValue, exePath, pathLen);
		debug("Set var:\t%s = %s\n", var, tmp);
		SetEnvironmentVariable(var, tmp);
		var = strtok(NULL, "\t");
	}
	*tmp = 0;

	// Process priority
	priority = loadInt(PRIORITY_CLASS);

	// Custom process name
	const BOOL setProcName = loadBool(SET_PROC_NAME)
			&& strstr(lpCmdLine, "--l4j-default-proc") == NULL;
	const BOOL wrapper = loadBool(WRAPPER);

    char jdk_path[_MAX_PATH] = {0};  // fry
    strcpy(jdk_path, cmd);
    //msgBox(jdk_path);

	appendLauncher(setProcName, exePath, pathLen, cmd);

	// Heap sizes
	appendHeapSizes(args);

    // JVM options
	if (loadString(JVM_OPTIONS, tmp)) {
		strcat(tmp, " ");
	} else {
        *tmp = 0;
    }
	/*
	 * Load additional JVM options from .l4j.ini file
	 * Options are separated by spaces or CRLF
	 * # starts an inline comment
	 */
	strncpy(tmp_path, exePath, strlen(exePath) - 3);
	strcat(tmp_path, "l4j.ini");
	long hFile;
	if ((hFile = _open(tmp_path, _O_RDONLY)) != -1) {
		const int jvmOptLen = strlen(tmp);
		char* src = tmp + jvmOptLen;
		char* dst = src;
		const int len = _read(hFile, src, MAX_ARGS - jvmOptLen - BIG_STR);
		BOOL copy = TRUE;
		int i;
		for (i = 0; i < len; i++, src++) {
			if (*src == '#') {
				copy = FALSE;
			} else if (*src == 13 || *src == 10) {
				copy = TRUE;
				if (dst > tmp && *(dst - 1) != ' ') {
					*dst++ = ' ';
				}
			} else if (copy) {
				*dst++ = *src;
			}
		}
		*dst = 0;
		if (len > 0 && *(dst - 1) != ' ') {
			strcat(tmp, " ");
		}
		_close(hFile);
	}

    // Expand environment %variables%
	expandVars(args, tmp, exePath, pathLen);

	// MainClass + Classpath or Jar
	char mainClass[STR] = {0};
	char jar[_MAX_PATH] = {0};
	loadString(JAR, jar);
	if (loadString(MAIN_CLASS, mainClass)) {
		if (!loadString(CLASSPATH, tmp)) {
			return FALSE;
		}
		char exp[MAX_ARGS] = {0};
		expandVars(exp, tmp, exePath, pathLen);
		strcat(args, "-classpath \"");
		if (wrapper) {
			appendAppClasspath(args, exePath, exp);
		} else if (*jar) {
			appendAppClasspath(args, jar, exp);
		}

	// add tools.jar for JDK  [fry]
	char tools[_MAX_PATH] = { 0 };
	sprintf(tools, "%s\\lib\\tools.jar", jdk_path);
	appendAppClasspath(args, tools, exp);

		// Deal with wildcards or >> strcat(args, exp); <<
		char* cp = strtok(exp, ";");
		while(cp != NULL) {
			debug("Add classpath:\t%s\n", cp);
			if (strpbrk(cp, "*?") != NULL) {
				int len = strrchr(cp, '\\') - cp + 1;
				strncpy(tmp_path, cp, len);
				char* filename = tmp_path + len;
				*filename = 0;
				struct _finddata_t c_file;
				long hFile;
				if ((hFile = _findfirst(cp, &c_file)) != -1L) {
					do {
						strcpy(filename, c_file.name);
						strcat(args, tmp_path);
						strcat(args, ";");
						debug("      \"      :\t%s\n", tmp_path);
					} while (_findnext(hFile, &c_file) == 0);
				}
				_findclose(hFile);
			} else {
				strcat(args, cp);
				strcat(args, ";");
			}
			cp = strtok(NULL, ";");
		}
		*(args + strlen(args) - 1) = 0;

		strcat(args, "\" ");
		strcat(args, mainClass);
	} else if (wrapper) {
       	strcat(args, "-jar \"");
		strcat(args, exePath);
   		strcat(args, "\"");
    } else {
       	strcat(args, "-jar \"");
        strncat(args, exePath, pathLen);
        appendPath(args, jar);
       	strcat(args, "\"");
    }

	// Constant command line args
	if (loadString(CMD_LINE, tmp)) {
		strcat(args, " ");
		strcat(args, tmp);
	}

	// Command line args
	if (*lpCmdLine) {
		strcpy(tmp, lpCmdLine);
		char* dst;
		while ((dst = strstr(tmp, "--l4j-")) != NULL) {
			char* src = strchr(dst, ' ');
			if (src == NULL || *(src + 1) == 0) {
				*dst = 0;
			} else {
				strcpy(dst, src + 1);
			}
		}
		if (*tmp) {
			strcat(args, " ");
			strcat(args, tmp);
		}
	}

	debug("Launcher:\t%s\n", cmd);
	debug("Launcher args:\t%s\n", args);
	debug("Args length:\t%d/32768 chars\n", strlen(args));
	return TRUE;
}
예제 #2
0
int prepare(const char *lpCmdLine)
{
    char tmp[MAX_ARGS] = {0};
    hModule = GetModuleHandle(NULL);
    if (hModule == NULL)
    {
        return FALSE;
    }

    // Get executable path
    char exePath[_MAX_PATH] = {0};
    int pathLen = getExePath(exePath);
    if (pathLen == -1)
    {
        return FALSE;
    }

    if (!initializeLogging(lpCmdLine, exePath, pathLen))
    {
        return FALSE;
    }

    debug("\n\nVersion:\t%s\n", VERSION);
    debug("CmdLine:\t%s %s\n", exePath, lpCmdLine);
    setWow64Flag();

    // Set default error message, title and optional support web site url.
    loadString(SUPPORT_URL, errUrl);
    loadString(ERR_TITLE, errTitle);
    if (!loadString(STARTUP_ERR, errMsg))
    {
        debug(ERROR_FORMAT, "Startup error message not defined.");
        return FALSE;
    }

    // Single instance
    loadString(MUTEX_NAME, mutexName);
    if (*mutexName)
    {
        SECURITY_ATTRIBUTES security;
        security.nLength = sizeof(SECURITY_ATTRIBUTES);
        security.bInheritHandle = TRUE;
        security.lpSecurityDescriptor = NULL;
        CreateMutexA(&security, FALSE, mutexName);
        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
            debug(ERROR_FORMAT, "Instance already exists.");
            return ERROR_ALREADY_EXISTS;
        }
    }

    // Working dir
    char tmp_path[_MAX_PATH] = {0};
    GetCurrentDirectory(_MAX_PATH, oldPwd);
    if (loadString(CHDIR, tmp_path))
    {
        strncpy(workingDir, exePath, pathLen);
        appendPath(workingDir, tmp_path);
        _chdir(workingDir);
        debug("Working dir:\t%s\n", workingDir);
    }

    // Use bundled jre or find java
    if (loadString(JRE_PATH, tmp_path))
    {
        char jrePath[MAX_ARGS] = {0};
        expandVars(jrePath, tmp_path, exePath, pathLen);
        debug("Bundled JRE:\t%s\n", jrePath);
        if (jrePath[0] == '\\' || jrePath[1] == ':')
        {
            // Absolute
            strcpy(cmd, jrePath);
        }
        else
        {
            // Relative
            strncpy(cmd, exePath, pathLen);
            appendPath(cmd, jrePath);
        }

        if (isLauncherPathValid(cmd))
        {
            foundJava = (wow64 && loadBool(BUNDLED_JRE_64_BIT))
                        ? FOUND_BUNDLED | KEY_WOW64_64KEY
                        : FOUND_BUNDLED;
        }
    }

    if (foundJava == NO_JAVA_FOUND)
    {
        if (!loadString(JAVA_MIN_VER, javaMinVer))
        {
            loadString(BUNDLED_JRE_ERR, errMsg);
            return FALSE;
        }

        loadString(JAVA_MAX_VER, javaMaxVer);
        if (!findJavaHome(cmd, loadInt(JDK_PREFERENCE)))
        {
            loadString(JRE_VERSION_ERR, errMsg);
            strcat(errMsg, " ");
            strcat(errMsg, javaMinVer);

            if (*javaMaxVer)
            {
                strcat(errMsg, " - ");
                strcat(errMsg, javaMaxVer);
            }

            if (runtimeBits == USE_64_BIT_RUNTIME
                    || runtimeBits == USE_32_BIT_RUNTIME)
            {
                strcat(errMsg, " (");
                strcat(errMsg, runtimeBits == USE_64_BIT_RUNTIME ? "64" : "32");
                strcat(errMsg, "-bit)");
            }

            if (corruptedJreFound)
            {
                char launcherErrMsg[BIG_STR] = {0};

                if (loadString(LAUNCHER_ERR, launcherErrMsg))
                {
                    strcat(errMsg, "\n");
                    strcat(errMsg, launcherErrMsg);
                }
            }

            loadString(DOWNLOAD_URL, errUrl);
            return FALSE;
        }
    }

    // Store the JRE Home Dir
    strcpy(jreHomeDir, cmd);

    // Append a path to the Path environment variable
    char jreBinPath[_MAX_PATH] = {0};
    strcpy(jreBinPath, cmd);
    strcat(jreBinPath, "\\bin");
    if (!appendToPathVar(jreBinPath))
    {
        debug(ERROR_FORMAT, "appendToPathVar failed.");
        return FALSE;
    }

    // Set environment variables
    char envVars[MAX_VAR_SIZE] = {0};
    loadString(ENV_VARIABLES, envVars);
    char *var = strtok(envVars, "\t");
    while (var != NULL)
    {
        char *varValue = strchr(var, '=');
        *varValue++ = 0;
        *tmp = 0;
        expandVars(tmp, varValue, exePath, pathLen);
        debug("Set var:\t%s = %s\n", var, tmp);
        SetEnvironmentVariable(var, tmp);
        var = strtok(NULL, "\t");
    }
    *tmp = 0;

    // Process priority
    priority = loadInt(PRIORITY_CLASS);

    // Launcher
    appendJavaw(cmd);

    // Heap sizes
    appendHeapSizes(args);

    // JVM options
    char jvmOptions[MAX_ARGS] = {0};
    setJvmOptions(jvmOptions, exePath);

    // Expand environment %variables%
    expandVars(args, jvmOptions, exePath, pathLen);

    // MainClass + Classpath or Jar
    char mainClass[STR] = {0};
    char jar[_MAX_PATH] = {0};

    const BOOL wrapper = loadBool(WRAPPER);
    loadString(JAR, jar);

    if (loadString(MAIN_CLASS, mainClass))
    {
        if (!loadString(CLASSPATH, tmp))
        {
            debug("Info:\t\tClasspath not defined.\n");
        }
        char exp[MAX_ARGS] = {0};
        expandVars(exp, tmp, exePath, pathLen);
        strcat(args, "-classpath \"");
        if (wrapper)
        {
            appendAppClasspath(args, exePath);
        }
        else if (*jar)
        {
            appendAppClasspath(args, jar);
        }

        // Deal with wildcards or >> strcat(args, exp); <<
        char* cp = strtok(exp, ";");
        while(cp != NULL)
        {
            debug("Add classpath:\t%s\n", cp);
            if (strpbrk(cp, "*?") != NULL)
            {
                int len = strrchr(cp, '\\') - cp + 1;
                strncpy(tmp_path, cp, len);
                char* filename = tmp_path + len;
                *filename = 0;
                struct _finddata_t c_file;
                long hFile;
                if ((hFile = _findfirst(cp, &c_file)) != -1L)
                {
                    do
                    {
                        strcpy(filename, c_file.name);
                        appendAppClasspath(args, tmp_path);
                        debug("      \"      :\t%s\n", tmp_path);
                    } while (_findnext(hFile, &c_file) == 0);
                }
                _findclose(hFile);
            }
            else
            {
                appendAppClasspath(args, cp);
            }
            cp = strtok(NULL, ";");
        }
        *(args + strlen(args) - 1) = 0;

        strcat(args, "\" ");
        strcat(args, mainClass);
    }
    else if (wrapper)
    {
        strcat(args, "-jar \"");
        strcat(args, exePath);
        strcat(args, "\"");
    }
    else
    {
        strcat(args, "-jar \"");
        strncat(args, exePath, pathLen);
        appendPath(args, jar);
        strcat(args, "\"");
    }

    // Constant command line args
    if (loadString(CMD_LINE, tmp))
    {
        strcat(args, " ");
        strcat(args, tmp);
    }

    // Command line args
    if (*lpCmdLine)
    {
        strcpy(tmp, lpCmdLine);
        char* dst;
        while ((dst = strstr(tmp, "--l4j-")) != NULL)
        {
            char* src = strchr(dst, ' ');
            if (src == NULL || *(src + 1) == 0)
            {
                *dst = 0;
            }
            else
            {
                strcpy(dst, src + 1);
            }
        }
        if (*tmp)
        {
            strcat(args, " ");
            strcat(args, tmp);
        }
    }

    debug("Launcher:\t%s\n", cmd);
    debug("Launcher args:\t%s\n", args);
    debug("Args length:\t%d/32768 chars\n", strlen(args));
    return TRUE;
}