/** * Try to figure out the absolute path based on the application name * (usually argv[0]). If the path is already absolute return a copy, if * it start with . look into the current directory, if not dig into * the $PATH. * In case of error or if executable was not found (as an example if * the application did a cwd between the start and this call), the * function will return NULL. Otherwise, an newly allocated string * will be returned. */ char* opal_find_absolute_path( char* app_name ) { char* abs_app_name; char cwd[OPAL_PATH_MAX], *pcwd; if( opal_path_is_absolute(app_name) ) { /* already absolute path */ abs_app_name = app_name; } else if ( '.' == app_name[0] || NULL != strchr(app_name, OPAL_PATH_SEP[0])) { /* the app is in the current directory or below it */ pcwd = getcwd( cwd, OPAL_PATH_MAX ); if( NULL == pcwd ) { /* too bad there is no way we can get the app absolute name */ return NULL; } abs_app_name = opal_os_path( false, pcwd, app_name, NULL ); } else { /* Otherwise try to search for the application in the PATH ... */ abs_app_name = opal_path_findv( app_name, X_OK, NULL, NULL ); } if( NULL != abs_app_name ) { char* resolved_path = (char*)malloc(OPAL_PATH_MAX); #if !defined(__WINDOWS__) realpath( abs_app_name, resolved_path ); #else #ifdef HAVE_SHLWAPI_H PathCanonicalize(resolved_path, abs_app_name); #endif #endif /* !defined(__WINDOWS__) */ if( abs_app_name != app_name ) free(abs_app_name); return resolved_path; } return NULL; }
/** * 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); }
/** * Forms a complete pathname and checks it for existance and * permissions * * Accepts: * -fname File name * -path Path prefix * -mode Target permissions which must be satisfied * * Returns: * -Full pathname of located file Success * -NULL Failure */ char *opal_path_access(char *fname, char *path, int mode) { char *fullpath = NULL; struct stat buf; bool relative; /* Allocate space for the full pathname. */ if (NULL == path) { fullpath = opal_os_path(false, fname, NULL); } else { relative = !opal_path_is_absolute(path); fullpath = opal_os_path(relative, path, fname, NULL); } if (NULL == fullpath) { return NULL; } /* first check to see - is this a file or a directory? We * only want files */ if (0 != stat(fullpath, &buf)) { /* couldn't stat the path - obviously, this also meets the * existence check, if that was requested */ free(fullpath); return NULL; } if (!(S_IFREG & buf.st_mode) && !(S_IFLNK & buf.st_mode)) { /* this isn't a regular file or a symbolic link, so * ignore it */ free(fullpath); return NULL; } /* check the permissions */ if ((X_OK & mode) && !(S_IXUSR & buf.st_mode)) { /* if they asked us to check executable permission, * and that isn't set, then return NULL */ free(fullpath); return NULL; } if ((R_OK & mode) && !(S_IRUSR & buf.st_mode)) { /* if they asked us to check read permission, * and that isn't set, then return NULL */ free(fullpath); return NULL; } if ((W_OK & mode) && !(S_IWUSR & buf.st_mode)) { /* if they asked us to check write permission, * and that isn't set, then return NULL */ free(fullpath); return NULL; } /* must have met all criteria! */ return fullpath; }
int main(int argc, char *argv[]) { int ret, exit_status = ORTE_SUCCESS; pid_t child_pid = 0; orte_sstore_base_global_snapshot_info_t *snapshot = NULL; char *basedir = NULL; char *tmp_str = NULL; /*************** * Initialize ***************/ if (ORTE_SUCCESS != (ret = initialize(argc, argv))) { exit_status = ret; goto cleanup; } snapshot = OBJ_NEW(orte_sstore_base_global_snapshot_info_t); if( opal_path_is_absolute(orte_restart_globals.snapshot_ref) ) { basedir = opal_dirname(orte_restart_globals.snapshot_ref); tmp_str = opal_basename(orte_restart_globals.snapshot_ref); free(orte_restart_globals.snapshot_ref); orte_restart_globals.snapshot_ref = strdup(tmp_str); free(tmp_str); tmp_str = NULL; } else if( NULL != strchr(orte_restart_globals.snapshot_ref, '/') ) { basedir = opal_dirname(orte_restart_globals.snapshot_ref); tmp_str = opal_basename(orte_restart_globals.snapshot_ref); free(orte_restart_globals.snapshot_ref); orte_restart_globals.snapshot_ref = strdup(tmp_str); free(tmp_str); tmp_str = NULL; } else { basedir = NULL; /* Use MCA parameter */ } /* * Note: If the seq # passed is -1, then the largest seq # is selected, * ow the seq # requested is selected if available * 'basedir': Snapshot Base location to look in. If NULL then MCA parameter is used */ if( ORTE_SUCCESS != (ret = orte_sstore.request_restart_handle(&(snapshot->ss_handle), basedir, orte_restart_globals.snapshot_ref, orte_restart_globals.seq_number, snapshot))) { opal_show_help("help-orte-restart.txt", "invalid_filename", true, orte_restart_globals.snapshot_ref); exit_status = ret; goto cleanup; } orte_restart_globals.seq_number = snapshot->seq_num; if(orte_restart_globals.info_only ) { if (ORTE_SUCCESS != (ret = snapshot_info(snapshot))) { exit_status = ret; goto cleanup; } exit_status = ORTE_SUCCESS; goto cleanup; } /****************************** * Create the app file to use with mpirun/orterun ******************************/ if( ORTE_SUCCESS != (ret = create_appfile(snapshot) ) ) { exit_status = ret; goto cleanup; } if( orte_restart_globals.app_only ) { printf("Created Appfile:\n\t%s\n", orte_restart_globals.appfile); exit_status = ORTE_SUCCESS; goto cleanup; } /****************************** * Restart in this process [mpirun/orterun] ******************************/ if( orte_restart_globals.verbose ) { opal_output_verbose(10, orte_restart_globals.output, "Restarting from file (%s)", orte_restart_globals.snapshot_ref); if( orte_restart_globals.forked ) { opal_output_verbose(10, orte_restart_globals.output, "\t Forking off a child"); } else { opal_output_verbose(10, orte_restart_globals.output, "\t Exec in self"); } } if( ORTE_SUCCESS != (ret = spawn_children(snapshot, &child_pid)) ) { opal_show_help("help-orte-restart.txt", "restart_cmd_failure", true, orte_restart_globals.snapshot_ref, ret); exit_status = ret; goto cleanup; } /*************** * Cleanup ***************/ cleanup: if( NULL != basedir ) { free(basedir); basedir = NULL; } if( NULL != tmp_str ) { free(tmp_str); tmp_str = NULL; } if( NULL != snapshot ) { OBJ_RELEASE(snapshot); snapshot = NULL; } if (OPAL_SUCCESS != (ret = finalize())) { return ret; } return exit_status; }