char* java_library_path(euca_opts *args) { #define JAVA_PATH_LEN 65536 char lib_dir[256],etc_dir[256],script_dir[256],*jar_list=(char*)malloc(JAVA_PATH_LEN*sizeof(char)); __die(( strlen(GETARG(args,home))+strlen(EUCA_LIB_DIR)>=254),"Directory path too long: %s/%s", GETARG(args,home), EUCA_LIB_DIR); snprintf(lib_dir,255,"%s%s",GETARG(args,home),EUCA_LIB_DIR); snprintf(etc_dir,255,"%s%s",GETARG(args,home),EUCA_ETC_DIR); snprintf(script_dir,255,"%s%s",GETARG(args,home),EUCA_SCRIPT_DIR); if(!CHECK_ISDIR(lib_dir) ) __die(1,"Can't find library directory %s", lib_dir ); int wb = 0; wb += snprintf(jar_list+wb,JAVA_PATH_LEN-wb,"-Djava.class.path=%s:",etc_dir); wb += snprintf(jar_list+wb,JAVA_PATH_LEN-wb,"%s",script_dir); DIR* lib_dir_p = opendir(lib_dir); struct direct *dir_ent; while ((dir_ent = readdir(lib_dir_p))!=0) { if (strcmp(dir_ent->d_name,".") != 0 && strcmp(dir_ent->d_name,"..") != 0 && strcmp(dir_ent->d_name,"openjdk-crypto.jar") != 0 && strstr(dir_ent->d_name,"disabled") == NULL && strstr(dir_ent->d_name,"eucalyptus-") != NULL ) { char jar[256]; snprintf(jar,255,"%s/%s",lib_dir,dir_ent->d_name); if( (CHECK_ISREG(jar) || CHECK_ISLNK(jar)) ) wb += snprintf(jar_list+wb,JAVA_PATH_LEN-wb,":%s",jar); } } closedir(lib_dir_p); lib_dir_p = opendir(lib_dir); while ((dir_ent = readdir(lib_dir_p))!=0) { if (strcmp(dir_ent->d_name,".") != 0 && strcmp(dir_ent->d_name,"..") != 0 && strcmp(dir_ent->d_name,"openjdk-crypto.jar") != 0 && strstr(dir_ent->d_name,"disabled") == NULL && strstr(dir_ent->d_name,"eucalyptus-") == NULL) { char jar[256]; snprintf(jar,255,"%s/%s",lib_dir,dir_ent->d_name); if( (CHECK_ISREG(jar) || CHECK_ISLNK(jar)) ) wb += snprintf(jar_list+wb,JAVA_PATH_LEN-wb,":%s",jar); } } closedir(lib_dir_p); return jar_list; }
static java_home_t *get_java_home( char *path ) { java_home_t *data = NULL; char buf[ 1024 ]; int x = -1, k = 0, i = 0; if( path == NULL ) return NULL; __debug( "Looking for libjvm in %s", path ); __abort(NULL, (CHECK_ISDIR( path ) == 0 ),"Path %s is not a directory", path ); data = (java_home_t *) malloc( sizeof(java_home_t) ); data->path = strdup( path ); data->jvms = NULL; data->jnum = 0; while( libjvm_paths[ ++x ] != NULL ) { __abort(NULL, ((k = snprintf( buf, 1024, libjvm_paths[ x ], path ) ) <= 0 ),"Error mangling jvm path" ); __debug( "Attempting to locate VM library %s", buf ); if( CHECK_ISREG( buf ) == 1 ) { data->jvms = (jvm_info_t **) malloc( 2 * sizeof(jvm_info_t *) ); data->jvms[ i ] = (jvm_info_t *) malloc( sizeof(jvm_info_t) ); data->jvms[ i ]->libjvm_path = strdup( buf ); char *dir, *base; dir = dirname(buf); base = basename(dir); data->jvms[ i ]->name = base; data->jvms[ ++i ] = NULL; data->jnum = i; return data; } } return data; }
int main(int argc, char *argv[]) { euca_opts euca_args; euca_opts *args = &euca_args; java_home_t *data = NULL; int status = 0; pid_t pid = 0; uid_t uid = 0; gid_t gid = 0; int i; if (arguments(argc, argv, args) != 0) exit(1); debug = args->debug_flag; set_output(GETARG(args, out), GETARG(args, err)); if (args->kill_flag == 1) return stop_child(args); if (checkuser(GETARG(args, user), &uid, &gid) == 0) return 1; for (i = 0; i < args->java_home_given; ++i) { __debug("Trying user supplied java home: %s", args->java_home_arg[i]); data = get_java_home(args->java_home_arg[i]); if (data != NULL) { break; } } char* java_home_env = getenv("JAVA_HOME"); if (data == NULL && java_home_env != NULL) { __debug("Trying environment JAVA_HOME: %s", java_home_env); data = get_java_home(java_home_env); } if (data == NULL && !args->java_home_given && args->java_home_arg[0] != NULL && CHECK_ISDIR(args->java_home_arg[0])) { __debug("Trying built-in java home: %s", args->java_home_arg[0]); data = get_java_home(args->java_home_arg[0]); } if (data == NULL && CHECK_ISREG("/usr/bin/java")) { char * javapath = (char *) calloc(PATH_MAX, sizeof(char)); javapath = realpath("/usr/bin/java", javapath); if (javapath != NULL) { javapath[strlen(javapath)-strlen("jre/bin/java")] = '\0'; __debug("Trying system java home: %s", javapath); data = get_java_home(javapath); } } if (data == NULL) { __error("Cannot locate Java Home"); return 1; } int x; if (debug == 1) { __debug("+-- DUMPING JAVA HOME STRUCTURE ------------------------"); __debug("| Java Home: \"%s\"", PRINT_NULL(data->path)); __debug("| Found JVMs: %d", data->jnum); for (x = 0; x < data->jnum; x++) { jvm_info_t *jvm = data->jvms[x]; __debug("| JVM Name: \"%s\"", PRINT_NULL(jvm->name)); __debug("| \"%s\"", PRINT_NULL(jvm->libjvm_path)); } __debug("+-------------------------------------------------------"); } if (strcmp(argv[0], "eucalyptus-cloud") != 0) { char *oldpath = getenv("LD_LIBRARY_PATH"), *libf = java_library(args, data); char *old = argv[0], buf[32768], *tmp = NULL, *p1 = NULL, *p2 = NULL; p1 = strdup(libf); tmp = strrchr(p1, '/'); if (tmp != NULL) tmp[0] = '\0'; p2 = strdup(p1); tmp = strrchr(p2, '/'); if (tmp != NULL) tmp[0] = '\0'; if (oldpath == NULL) snprintf(buf, 32768, "%s:%s:%s/bin/linux-x64", p1, p2, GETARG(args, profiler_home)); else snprintf(buf, 32768, "%s:%s:%s:%s/bin/linux-x64", oldpath, p1, p2, GETARG(args, profiler_home)); tmp = strdup(buf); setenv("LD_LIBRARY_PATH", tmp, 1); __debug("Invoking w/ LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH")); argv[0] = "eucalyptus-cloud"; execve(old, argv, environ); __error("Cannot execute process"); return 1; } __debug("Running w/ LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH")); if (args->fork_flag) { pid = fork(); __die((pid == -1), "Cannot detach from parent process"); if (pid != 0) return wait_child(args, pid); setsid(); } while ((pid = fork()) != -1) { if (pid == 0) exit(child(args, data, uid, gid)); child_pid = pid; signal(SIGHUP, controller); signal(SIGTERM, controller); signal(SIGINT, controller); while (waitpid(-1, &status, 0) != pid) ; if (WIFEXITED(status)) { status = WEXITSTATUS(status); __debug("Eucalyptus exited with status: %d", status); unlink(GETARG(args, pidfile)); if (status == EUCA_RET_RELOAD) { __debug("Reloading service."); continue; } else if (status == 0) { __debug("Service shut down cleanly."); return 0; } __error("Service exit with a return value of %d.", status); return 1; } else { perror("Service did not exit cleanly."); __error("Service did not exit cleanly: exit value=%d.", status); return 1; } } __error("Cannot decouple controller/child processes"); return 1; }