1 /* 2 * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana 3 * University Research and Technology 4 * Corporation. All rights reserved. 5 * Copyright (c) 2004-2008 The University of Tennessee and The University 6 * of Tennessee Research Foundation. All rights 7 * reserved. 8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 9 * University of Stuttgart. All rights reserved. 10 * Copyright (c) 2004-2005 The Regents of the University of California. 11 * All rights reserved. 12 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved. 13 * Copyright (c) 2008-2010 Cisco Systems, Inc. All rights reserved. 14 * Copyright (c) 2014-2016 Intel, Inc. All rights reserved. 15 * Copyright (c) 2014 Research Organization for Information Science 16 * and Technology (RIST). All rights reserved. 17 * $COPYRIGHT$ 18 * 19 * Additional copyrights may follow 20 * 21 * $HEADER$ 22 */ 23 #include "orte_config.h" 24 #include "orte/constants.h" 25 26 #include <string.h> 27 #include <stdlib.h> 28 #ifdef HAVE_UNISTD_H 29 #include <unistd.h> 30 #endif 31 #ifdef HAVE_SYS_TYPES_H 32 #include <sys/types.h> 33 #endif 34 #ifdef HAVE_SYS_STAT_H 35 #include <sys/stat.h> 36 #endif 37 #ifdef HAVE_SYS_PARAM_H 38 #include <sys/param.h> 39 #endif 40 #ifdef HAVE_UNISTD_H 41 #include <unistd.h> 42 #endif 43 #ifdef HAVE_NETDB_H 44 #include <netdb.h> 45 #endif 46 #include <errno.h> 47 48 #include "opal/util/basename.h" 49 #include "opal/util/path.h" 50 #include "opal/util/opal_environ.h" 51 52 #include "orte/runtime/orte_globals.h" 53 54 #include "orte/util/context_fns.h" 55 56 int orte_util_check_context_cwd(orte_app_context_t *context, 57 bool want_chdir) 58 { 59 bool good = true; 60 const char *tmp; 61 62 /* If we want to chdir and the chdir fails (for any reason -- such 63 as if the dir doesn't exist, it isn't a dir, we don't have 64 permissions, etc.), then set good to false. */ 65 if (want_chdir && 0 != chdir(context->cwd)) { 66 good = false; 67 } 68 69 /* If either of the above failed, go into this block */ 70 if (!good) { 71 /* See if the directory was a user-specified directory. If it 72 was, barf because they specifically asked for something we 73 can't provide. */ 74 if (orte_get_attribute(&context->attributes, ORTE_APP_USER_CWD, 75 NULL, OPAL_BOOL)) { 76 return ORTE_ERR_WDIR_NOT_FOUND; 77 } 78 79 /* If the user didn't specifically ask for it, then it 80 was a system-supplied default directory, so it's ok 81 to not go there. Try to go to the $HOME directory 82 instead. */ 83 tmp = opal_home_directory(); 84 if (NULL != tmp) { 85 /* Try $HOME. Same test as above. */ 86 good = true; 87 if (want_chdir && 0 != chdir(tmp)) { 88 good = false; 89 } 90 if (!good) { 91 return ORTE_ERR_WDIR_NOT_FOUND; 92 } 93 94 /* Reset the pwd in this local copy of the 95 context */ 96 if (NULL != context->cwd) free(context->cwd); 97 context->cwd = strdup(tmp); 98 } 99 100 /* If we couldn't find $HOME, then just take whatever 101 the default directory is -- assumedly there *is* 102 one, or we wouldn't be running... */ 103 } 104 105 /* All happy */ 106 return ORTE_SUCCESS; 107 } 108 109 int orte_util_check_context_app(orte_app_context_t *context, char **env) 110 { 111 char *tmp; 112 113 /* If the app is a naked filename, we need to do a path search for 114 it. orterun will send in whatever the user specified (e.g., 115 "orterun -np 2 uptime"), so in some cases, we need to search 116 the path to verify that we can find it. Here's the 117 possibilities: 118 119 1. The user specified an absolute pathname for the executable. 120 We simply need to verify that it exists and we can run it. 121 122 2. The user specified a relative pathname for the executable. 123 Ditto with #1 -- based on the cwd, we need to verify that it 124 exists and we can run it. 125 126 3. The user specified a naked filename. We need to search the 127 path, find a match, and verify that we can run it. 128 129 Note that in some cases, we won't be doing this work here -- 130 bproc, for example, does not use the fork pls for launching, so 131 it does this same work over there. */ 132 133 tmp = opal_basename(context->app); 134 if (strlen(tmp) == strlen(context->app)) { 135 /* If this is a naked executable -- no relative or absolute 136 pathname -- then search the PATH for it */ 137 free(tmp); 138 tmp = opal_path_findv(context->app, X_OK, env, context->cwd); 139 if (NULL == tmp) { 140 return ORTE_ERR_EXE_NOT_FOUND; 141 } 142 free(context->app); 143 context->app = tmp; 144 } else { 145 free(tmp); 146 if (0 != access(context->app, X_OK)) { 147 return ORTE_ERR_EXE_NOT_ACCESSIBLE; 148 } 149 } 150 151 /* All was good */ 152 return ORTE_SUCCESS; 153 }