diff --git a/src/util/proc_info.c b/src/util/proc_info.c index 67f084cc29..d4754fa826 100644 --- a/src/util/proc_info.c +++ b/src/util/proc_info.c @@ -24,6 +24,7 @@ ompi_proc_info_t ompi_process_info = { /* .seed = */ false, /* .my_universe = */ "default-universe", /* .tmpdir_base = */ NULL, + /* .top_session_dir = */ NULL, /* .universe_session_dir = */ NULL, /* .job_session_dir = */ NULL, /* .proc_session_dir = */ NULL, diff --git a/src/util/proc_info.h b/src/util/proc_info.h index ec07eba9d1..a97320a5d8 100644 --- a/src/util/proc_info.h +++ b/src/util/proc_info.h @@ -32,7 +32,8 @@ struct ompi_proc_info_t { bool seed; /**< Indicate whether or not this is seed daemon */ char *my_universe; /**< The name of the universe to which this process belongs */ char *tmpdir_base; /**< Base directory of the session dir tree */ - char *universe_session_dir; /**< Location of universe temp dir. + char *top_session_dir; /**< Top-most directory of the session tree */ + char *universe_session_dir; /**< Location of universe temp dir. * The session directory has the form * , where the prefix * can either be provided by the user via the diff --git a/src/util/session_dir.c b/src/util/session_dir.c index 7321c53855..ad9d6986b4 100644 --- a/src/util/session_dir.c +++ b/src/util/session_dir.c @@ -46,6 +46,11 @@ int ompi_check_dir(bool create, char *directory); +void ompi_dir_empty(char *pathname); + +bool ompi_no_subdirs(char *pathname); + + #define OMPI_DEFAULT_TMPDIR "tmp" int ompi_check_dir(bool create, char *directory) @@ -64,14 +69,35 @@ int ompi_check_dir(bool create, char *directory) return(OMPI_ERROR); /* couldn't find it, or don't have access rights, and not asked to create it */ } -int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char *batchid, char *universe, char *job, char *proc) +int ompi_session_dir(bool create, char *prfx, char *usr, char *hostid, + char *batchid, char *univ, char *job, char *proc) { char *fulldirpath=NULL, *tmp=NULL, *hostname=NULL, *batchname=NULL; - char *sessions=NULL, *frontend=NULL; + char *sessions=NULL, *frontend=NULL, *user=NULL, *universe=NULL; + char *prefix=NULL; int return_code; - if (NULL == user || NULL == universe) { /* error conditions - have to provide at least that much */ - return OMPI_ERROR; + /* ensure that system info is set */ + ompi_sys_info(); + + if (NULL == usr) { /* check if user set elsewhere */ + if (NULL == ompi_system_info.user) { /* error condition */ + return OMPI_ERROR; + } else { + user = strdup(ompi_system_info.user); + } + } else { + user = strdup(usr); + } + + if (NULL == univ) { /* see if universe set elsewhere */ + if (NULL == ompi_process_info.my_universe) { /* error condition */ + return OMPI_ERROR; + } else { + universe = strdup(ompi_process_info.my_universe); + } + } else { + universe = strdup(univ); } if (NULL == job && NULL != proc) { /* can't give a proc without a job */ @@ -95,11 +121,16 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * batchname = batchid; } - if (0 > asprintf(&frontend, "openmpi-sessions-%s@%s:%s", user, hostname, batchname)) { - return_code = OMPI_ERROR; - goto CLEANUP; + if (NULL == ompi_process_info.top_session_dir) { + if (0 > asprintf(&frontend, "openmpi-sessions-%s@%s:%s", user, hostname, batchname)) { + return_code = OMPI_ERROR; + goto CLEANUP; + } + } else { + frontend = strdup(ompi_process_info.top_session_dir); } + if (NULL != proc) { if (0 > asprintf(&sessions, "%s%s%s%s%s%s%s", frontend, ompi_system_info.path_sep, universe, @@ -122,6 +153,7 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * } } + if (NULL != prefix) { /* if a prefix is specified, start looking here */ tmp = strdup(prefix); fulldirpath = strdup(ompi_os_path(false, tmp, sessions, NULL)); /* make sure it's an absolute pathname */ @@ -132,7 +164,13 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * } /* no prefix was specified, so check other options in order */ - if (NULL != getenv("OMPI_PREFIX_ENV")) { /* we have prefix enviro var - try that next */ + if (NULL != ompi_process_info.tmpdir_base) { /* stored value previously */ + fulldirpath = strdup(ompi_os_path(false, ompi_process_info.tmpdir_base, sessions, NULL)); + if (OMPI_SUCCESS == ompi_check_dir(create, fulldirpath)) { /* check for existence and access, or create it */ + return_code = OMPI_SUCCESS; + goto COMPLETE; + } + } else if (NULL != getenv("OMPI_PREFIX_ENV")) { /* we have prefix enviro var - try that next */ tmp = strdup(getenv("OMPI_PREFIX_ENV")); fulldirpath = strdup(ompi_os_path(false, tmp, sessions, NULL)); if (OMPI_SUCCESS == ompi_check_dir(create, fulldirpath)) { /* check for existence and access, or create it */ @@ -159,7 +197,8 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * if (OMPI_SUCCESS == ompi_check_dir(create, fulldirpath)) { /* check for existence and access, or create it */ return_code = OMPI_SUCCESS; goto COMPLETE; - } } + } + } fulldirpath = strdup(ompi_os_path(false, tmp, sessions, NULL)); if (OMPI_SUCCESS == ompi_check_dir(create, fulldirpath)) { /* check for existence and access, or create it */ @@ -171,20 +210,28 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * } COMPLETE: - if (create) { - if (NULL != proc) { - ompi_process_info.proc_session_dir = strdup(fulldirpath); - fulldirpath = dirname(fulldirpath); - } - - if (NULL != job) { - ompi_process_info.job_session_dir = strdup(fulldirpath); - fulldirpath = dirname(fulldirpath); - } - - ompi_process_info.universe_session_dir = strdup(fulldirpath); + if (NULL == ompi_process_info.tmpdir_base) { ompi_process_info.tmpdir_base = strdup(tmp); } + if (NULL == ompi_process_info.top_session_dir) { + ompi_process_info.top_session_dir = strdup(ompi_os_path(false, tmp, frontend, NULL)); + } + + if (NULL == ompi_process_info.proc_session_dir && NULL != proc) { + ompi_process_info.proc_session_dir = strdup(fulldirpath); + free(fulldirpath); + fulldirpath = strdup(dirname(ompi_process_info.proc_session_dir)); + } + + if (NULL == ompi_process_info.job_session_dir && NULL != job) { + ompi_process_info.job_session_dir = strdup(fulldirpath); + free(fulldirpath); + fulldirpath = strdup(dirname(ompi_process_info.job_session_dir)); + } + + if (NULL == ompi_process_info.universe_session_dir) { + ompi_process_info.universe_session_dir = strdup(fulldirpath); + } CLEANUP: if (tmp) { @@ -213,27 +260,41 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, char * int ompi_session_dir_finalize() { - if (OMPI_SUCCESS != ompi_clean_dir(ompi_process_info.proc_session_dir)) { - return OMPI_ERROR; /* should have been able to at least clear my own */ - } - if (OMPI_SUCCESS != ompi_clean_dir(ompi_process_info.job_session_dir)) { - return OMPI_SUCCESS; /* need new error condition - could be okay not to clear */ - } - - if (OMPI_SUCCESS != ompi_clean_dir(ompi_process_info.universe_session_dir)) { + if (ompi_no_subdirs(ompi_process_info.proc_session_dir)) { + ompi_dir_empty(ompi_process_info.proc_session_dir); + } else { return OMPI_SUCCESS; } + if (ompi_no_subdirs(ompi_process_info.job_session_dir)) { + ompi_dir_empty(ompi_process_info.job_session_dir); + } else { + return OMPI_SUCCESS; + } + + if (ompi_no_subdirs(ompi_process_info.universe_session_dir)) { + ompi_dir_empty(ompi_process_info.universe_session_dir); + } else { + return OMPI_SUCCESS; + } + + if (ompi_no_subdirs(ompi_process_info.top_session_dir)) { + ompi_dir_empty(ompi_process_info.top_session_dir); + } + return OMPI_SUCCESS; } -int -ompi_clean_dir(char *pathname) +void +ompi_dir_empty(char *pathname) { DIR *dp; struct dirent *ep; char *filenm; + bool empty; + + empty = true; if (NULL != pathname) { /* protect against error */ dp = opendir(pathname); @@ -249,7 +310,29 @@ ompi_clean_dir(char *pathname) closedir(dp); } rmdir(pathname); - return OMPI_SUCCESS; } - return OMPI_ERROR; } + +bool ompi_no_subdirs(char *pathname) +{ + DIR *dp; + struct dirent *ep; + + if (NULL != pathname) { /* protect against error */ + dp = opendir(pathname); + if (NULL != dp) { + while ((ep = readdir(dp))) { + if ((0 != strcmp(ep->d_name, ".")) && + (0 != strcmp(ep->d_name, "..")) && + (DT_DIR == ep->d_type)) { + return false; + } + } + closedir(dp); + return true; + } + return false; + } + return false; +} + diff --git a/src/util/session_dir.h b/src/util/session_dir.h index 768ced7bb0..7cf6387c57 100644 --- a/src/util/session_dir.h +++ b/src/util/session_dir.h @@ -129,5 +129,3 @@ int ompi_session_dir(bool create, char *prefix, char *user, char *hostid, * properly cleaned up. */ int ompi_session_dir_finalize(void); - -int ompi_clean_dir(char *pathname); diff --git a/test/util/ompi_session_dir.c b/test/util/ompi_session_dir.c index 0ac2460946..60c5cb12b8 100644 --- a/test/util/ompi_session_dir.c +++ b/test/util/ompi_session_dir.c @@ -27,6 +27,9 @@ static bool test4(void); /* no prefix given, TMP set, one good and one bad */ static bool test5(void); /* no prefix given, HOME set, one good and one bad */ static bool test6(void); /* no prefix given, nothing set, one good and one bad */ static bool test7(void); /* remove session directory tree */ +static bool test8(void); /* attempt to remove tree when subdirs present */ + +void clear_proc_info(void); static FILE *test_out=NULL; @@ -35,12 +38,12 @@ int main(int argc, char* argv[]) ompi_sys_info(); /* initialize system */ test_init("ompi_session_dir_t"); - test_out = fopen( "test_session_dir_out", "w+" ); + test_out = fopen( "test_session_dir_out", "w+" ); if( test_out == NULL ) { test_failure("test_session_dir couldn't open test file failed"); test_finalize(); exit(1); - } + } fprintf(test_out, "running test1\n"); @@ -96,9 +99,19 @@ int main(int argc, char* argv[]) test_success(); } else { - test_failure("ompi_session_dir_t test6 failed"); + test_failure("ompi_session_dir_t test7 failed"); } + fprintf(test_out, "running test8\n"); + if (test8()) { + test_success(); + } + else { + test_failure("ompi_session_dir_t test8 failed"); + } + + fprintf(test_out, "completed all tests\n"); + fclose(test_out); test_finalize(); return 0; @@ -109,20 +122,22 @@ static bool test1(void) { /* Test proper action when given a prefix */ - char *prefix, *tmp, *tmp2; + char *prefix, *tmp; /* see if we can create a specified path */ + clear_proc_info(); + prefix = ompi_os_path(false, "tmp", NULL); if (OMPI_ERROR == ompi_session_dir(true, prefix, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { - printf("test1 - couldn't create specified path\n"); + fprintf(test_out, "test1 - couldn't create specified path\n"); free(prefix); return(false); } /* see if it can access an existing path */ if (OMPI_ERROR == ompi_session_dir(false, prefix, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { - printf("test1 - couldn't access existing path\n"); + fprintf(test_out, "test1 - couldn't access existing path\n"); free(prefix); return(false); } @@ -134,18 +149,6 @@ static bool test1(void) free(prefix); free(tmp); - /* check what happens when given prefix that won't allow access */ - tmp2 = ompi_os_path(false, "test", NULL); /* assume we don't have root privileges */ - if (OMPI_SUCCESS == ompi_session_dir(true, tmp2, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { - printf("created temp directory in %s - shouldn't have been able to do so\n", tmp2); - rmdir(ompi_process_info.universe_session_dir); - tmp = strdup(dirname(ompi_process_info.universe_session_dir)); - rmdir(tmp); - free(tmp); - free(tmp2); - return(false); - } - return true; } @@ -154,6 +157,8 @@ static bool test2(void) { char *tmp; + clear_proc_info(); + /* use the OMPI_PREFIX_ENV variable */ setenv("OMPI_PREFIX_ENV", "/tmp/trythis", 1); @@ -180,6 +185,8 @@ static bool test3(void) /* use the TMPDIR enviro variable */ char *tmp; + clear_proc_info(); + setenv("TMPDIR", "/tmp/trythis", 1); if (OMPI_ERROR == ompi_session_dir(true, NULL, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { @@ -203,6 +210,8 @@ static bool test4(void) /* use the TMP enviro variable */ char *tmp; + clear_proc_info(); + setenv("TMP", "/tmp/trythis", 1); if (OMPI_ERROR == ompi_session_dir(true, NULL, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { @@ -226,6 +235,8 @@ static bool test5(void) /* use the HOME enviro variable */ char *tmp; + clear_proc_info(); + setenv("HOME", "/tmp/trythis", 1); if (OMPI_ERROR == ompi_session_dir(true, NULL, ompi_system_info.user, NULL, NULL, "test-universe", NULL, NULL)) { @@ -248,6 +259,8 @@ static bool test6(void) { char *tmp; + clear_proc_info(); + /* no enviro variables set, no prefix given * Program should turn to default of /tmp (where "/" is whatever * top-level directory is appropriate for given system) @@ -268,6 +281,8 @@ static bool test7(void) char *filenm; FILE *fp; + clear_proc_info(); + /* create test proc session directory tree */ if (OMPI_ERROR == ompi_session_dir(true, NULL, ompi_system_info.user, "localhost", NULL, "test-universe", "test-job", "test-proc")) { return(false); @@ -301,3 +316,58 @@ static bool test7(void) return true; } + +static bool test8(void) +{ + char *filenm; + FILE *fp; + + clear_proc_info(); + + /* create test proc session directory tree */ + if (OMPI_ERROR == ompi_session_dir(true, NULL, ompi_system_info.user, "localhost", NULL, "test-universe2", "test-job", "test-proc")) { + return(false); + } + + fprintf(test_out, "removing directories: %s\n\t%s\n\t%s\n", + ompi_process_info.proc_session_dir, + ompi_process_info.job_session_dir, + ompi_process_info.universe_session_dir); + + /* create some files */ + + filenm = ompi_os_path(false, ompi_process_info.proc_session_dir, "dum1", NULL); + fp = fopen(filenm, "w"); + fprintf(fp, "ss"); + fclose(fp); + + filenm = ompi_os_path(false, ompi_process_info.job_session_dir, "dum2", NULL); + fp = fopen(filenm, "w"); + fprintf(fp, "ss"); + fclose(fp); + + filenm = ompi_os_path(false, ompi_process_info.universe_session_dir, "dum3", NULL); + fp = fopen(filenm, "w"); + fprintf(fp, "ss"); + fclose(fp); + + + filenm = ompi_os_path( false, ompi_process_info.job_session_dir, "dir1", NULL); + ompi_os_create_dirpath(filenm, 0777); + + if (OMPI_ERROR == ompi_session_dir_finalize()) { + return(false); + } + + return true; +} + +void clear_proc_info(void) +{ + ompi_process_info.tmpdir_base = NULL; + ompi_process_info.top_session_dir = NULL; + ompi_process_info.universe_session_dir = NULL; + ompi_process_info.job_session_dir = NULL; + ompi_process_info.proc_session_dir = NULL; + +}