/** * Locates a file with certain permissions */ char *opal_path_find(char *fname, char **pathv, int mode, char **envv) { char *fullpath; char *delimit; char *env; char *pfix; int i; /* If absolute path is given, return it without searching. */ if( opal_path_is_absolute(fname) ) { return opal_path_access(fname, NULL, mode); } /* Initialize. */ fullpath = NULL; i = 0; /* Consider each directory until the file is found. Thus, the order of directories is important. */ while (pathv[i] && NULL == fullpath) { /* Replace environment variable at the head of the string. */ if ('$' == *pathv[i]) { delimit = strchr(pathv[i], OPAL_PATH_SEP[0]); if (delimit) { *delimit = '\0'; } env = list_env_get(pathv[i]+1, envv); if (delimit) { *delimit = OPAL_PATH_SEP[0]; } if (NULL != env) { if (!delimit) { fullpath = opal_path_access(fname, env, mode); } else { pfix = (char*) malloc(strlen(env) + strlen(delimit) + 1); if (NULL == pfix) { return NULL; } strcpy(pfix, env); strcat(pfix, delimit); fullpath = opal_path_access(fname, pfix, mode); free(pfix); } } } else { fullpath = opal_path_access(fname, pathv[i], mode); } i++; } return opal_make_filename_os_friendly(fullpath); }
static int fork_hnp(void) { int p[2], death_pipe[2]; char *cmd; char **argv = NULL; int argc; char *param, *cptr; sigset_t sigs; int buffer_length, num_chars_read, chunk; char *orted_uri; int rc, i; /* A pipe is used to communicate between the parent and child to indicate whether the exec ultimately succeeded or failed. The child sets the pipe to be close-on-exec; the child only ever writes anything to the pipe if there is an error (e.g., executable not found, exec() fails, etc.). The parent does a blocking read on the pipe; if the pipe closed with no data, then the exec() succeeded. If the parent reads something from the pipe, then the child was letting us know that it failed. */ if (pipe(p) < 0) { ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_PIPES); return ORTE_ERR_SYS_LIMITS_PIPES; } /* we also have to give the HNP a pipe it can watch to know when * we terminated. Since the HNP is going to be a child of us, it * can't just use waitpid to see when we leave - so it will watch * the pipe instead */ if (pipe(death_pipe) < 0) { ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_PIPES); return ORTE_ERR_SYS_LIMITS_PIPES; } /* find the orted binary using the install_dirs support - this also * checks to ensure that we can see this executable and it *is* executable by us */ cmd = opal_path_access("orted", opal_install_dirs.bindir, X_OK); if (NULL == cmd) { /* guess we couldn't do it - best to abort */ ORTE_ERROR_LOG(ORTE_ERR_FILE_NOT_EXECUTABLE); close(p[0]); close(p[1]); return ORTE_ERR_FILE_NOT_EXECUTABLE; } /* okay, setup an appropriate argv */ opal_argv_append(&argc, &argv, "orted"); /* tell the daemon it is to be the HNP */ opal_argv_append(&argc, &argv, "--hnp"); /* tell the daemon to get out of our process group */ opal_argv_append(&argc, &argv, "--set-sid"); /* tell the daemon to report back its uri so we can connect to it */ opal_argv_append(&argc, &argv, "--report-uri"); asprintf(¶m, "%d", p[1]); opal_argv_append(&argc, &argv, param); free(param); /* give the daemon a pipe it can watch to tell when we have died */ opal_argv_append(&argc, &argv, "--singleton-died-pipe"); asprintf(¶m, "%d", death_pipe[0]); opal_argv_append(&argc, &argv, param); free(param); /* add any debug flags */ if (orte_debug_flag) { opal_argv_append(&argc, &argv, "--debug"); } if (orte_debug_daemons_flag) { opal_argv_append(&argc, &argv, "--debug-daemons"); } if (orte_debug_daemons_file_flag) { if (!orte_debug_daemons_flag) { opal_argv_append(&argc, &argv, "--debug-daemons"); } opal_argv_append(&argc, &argv, "--debug-daemons-file"); } /* indicate that it must use the novm state machine */ opal_argv_append(&argc, &argv, "-"OPAL_MCA_CMD_LINE_ID); opal_argv_append(&argc, &argv, "state_novm_select"); opal_argv_append(&argc, &argv, "1"); /* direct the selection of the ess component */ opal_argv_append(&argc, &argv, "-"OPAL_MCA_CMD_LINE_ID); opal_argv_append(&argc, &argv, "ess"); opal_argv_append(&argc, &argv, "hnp"); /* direct the selection of the pmix component */ opal_argv_append(&argc, &argv, "-"OPAL_MCA_CMD_LINE_ID); opal_argv_append(&argc, &argv, "pmix"); opal_argv_append(&argc, &argv, "^s1,s2,cray,isolated"); /* Fork off the child */ orte_process_info.hnp_pid = fork(); if(orte_process_info.hnp_pid < 0) { ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_CHILDREN); close(p[0]); close(p[1]); close(death_pipe[0]); close(death_pipe[1]); free(cmd); opal_argv_free(argv); return ORTE_ERR_SYS_LIMITS_CHILDREN; } if (orte_process_info.hnp_pid == 0) { close(p[0]); close(death_pipe[1]); /* I am the child - exec me */ /* Set signal handlers back to the default. Do this close to the execve() because the event library may (and likely will) reset them. If we don't do this, the event library may have left some set that, at least on some OS's, don't get reset via fork() or exec(). Hence, the orted could be unkillable (for example). */ set_handler_default(SIGTERM); set_handler_default(SIGINT); set_handler_default(SIGHUP); set_handler_default(SIGPIPE); set_handler_default(SIGCHLD); /* Unblock all signals, for many of the same reasons that we set the default handlers, above. This is noticable on Linux where the event library blocks SIGTERM, but we don't want that blocked by the orted (or, more specifically, we don't want it to be blocked by the orted and then inherited by the ORTE processes that it forks, making them unkillable by SIGTERM). */ sigprocmask(0, 0, &sigs); sigprocmask(SIG_UNBLOCK, &sigs, 0); execv(cmd, argv); /* if I get here, the execv failed! */ orte_show_help("help-ess-base.txt", "ess-base:execv-error", true, cmd, strerror(errno)); exit(1); } else { free(cmd); /* I am the parent - wait to hear something back and * report results */ close(p[1]); /* parent closes the write - orted will write its contact info to it*/ close(death_pipe[0]); /* parent closes the death_pipe's read */ opal_argv_free(argv); /* setup the buffer to read the HNP's uri */ buffer_length = ORTE_URI_MSG_LGTH; chunk = ORTE_URI_MSG_LGTH-1; num_chars_read = 0; orted_uri = (char*)malloc(buffer_length); memset(orted_uri, 0, buffer_length); while (chunk == (rc = read(p[0], &orted_uri[num_chars_read], chunk))) { /* we read an entire buffer - better get more */ num_chars_read += chunk; orted_uri = realloc((void*)orted_uri, buffer_length+ORTE_URI_MSG_LGTH); memset(&orted_uri[buffer_length], 0, ORTE_URI_MSG_LGTH); buffer_length += ORTE_URI_MSG_LGTH; } num_chars_read += rc; if (num_chars_read <= 0) { /* we didn't get anything back - this is bad */ ORTE_ERROR_LOG(ORTE_ERR_HNP_COULD_NOT_START); free(orted_uri); return ORTE_ERR_HNP_COULD_NOT_START; } /* parse the sysinfo from the returned info - must * start from the end of the string as the uri itself * can contain brackets */ if (NULL == (param = strrchr(orted_uri, '['))) { ORTE_ERROR_LOG(ORTE_ERR_COMM_FAILURE); free(orted_uri); return ORTE_ERR_COMM_FAILURE; } *param = '\0'; /* terminate the uri string */ ++param; /* point to the start of the sysinfo */ /* find the end of the sysinfo */ if (NULL == (cptr = strchr(param, ']'))) { ORTE_ERROR_LOG(ORTE_ERR_COMM_FAILURE); free(orted_uri); return ORTE_ERR_COMM_FAILURE; } *cptr = '\0'; /* terminate the sysinfo string */ ++cptr; /* point to the start of the pmix uri */ /* convert the sysinfo string */ if (ORTE_SUCCESS != (rc = orte_util_convert_string_to_sysinfo(&orte_local_cpu_type, &orte_local_cpu_model, param))) { ORTE_ERROR_LOG(rc); free(orted_uri); return rc; } /* save the daemon uri - we will process it later */ orte_process_info.my_daemon_uri = strdup(orted_uri); /* likewise, since this is also the HNP, set that uri too */ orte_process_info.my_hnp_uri = orted_uri; /* split the pmix_uri into its parts */ argv = opal_argv_split(cptr, ','); if (4 != opal_argv_count(argv)) { opal_argv_free(argv); return ORTE_ERR_BAD_PARAM; } /* push each piece into the environment */ for (i=0; i < 4; i++) { pmixenvars[i] = strdup(argv[i]); putenv(pmixenvars[i]); } opal_argv_free(argv); added_pmix_envs = true; /* all done - report success */ return ORTE_SUCCESS; } }