示例#1
0
/*
 * Check and create the directory requested
 */
static int orte_create_dir(char *directory)
{
    mode_t my_mode = S_IRWXU;  /* I'm looking for full rights */
    int ret;

    /* Sanity check before creating the directory with the proper mode,
     * Make sure it doesn't exist already */
    if( ORTE_ERR_NOT_FOUND !=
        (ret = opal_os_dirpath_access(directory, my_mode)) ) {
        /* Failure because opal_os_dirpath_access() indicated that either:
         * - The directory exists and we can access it (no need to create it again), 
         *    return OPAL_SUCCESS, or
         * - don't have access rights, return OPAL_ERROR
         */
        if (ORTE_SUCCESS != ret) {
            ORTE_ERROR_LOG(ret);
        }
        return(ret);
    }
    
    /* Get here if the directory doesn't exist, so create it */
    if (ORTE_SUCCESS != (ret = opal_os_dirpath_create(directory, my_mode))) {
        ORTE_ERROR_LOG(ret);
    }
    return ret;
}
示例#2
0
/*
 * Check and create the directory requested
 */
static int orte_create_dir(char *directory)
{
#ifndef __WINDOWS__
    mode_t my_mode = S_IRWXU;  /* at the least, I need to be able to do anything */
#else
    mode_t my_mode = _S_IREAD | _S_IWRITE | _S_IEXEC;
#endif
    int ret;

    /* Sanity check before creating the directory with the proper mode,
     * Make sure it doesn't exist already */
    if( OPAL_ERR_NOT_FOUND != (ret = opal_os_dirpath_access(directory, my_mode)) ) {
        /* Failure because opal_os_dirpath_access() indicated that either:
         * - The directory exists and we can access it (no need to create it again), 
         *    return OPAL_SUCCESS, or
         * - don't have access rights, return OPAL_ERROR
         */
        return(ret);
    }
    /* The directory doesn't exist so create it */
    else {
	    return(opal_os_dirpath_create(directory, my_mode));
    }
}
示例#3
0
/**
 * This function attempts to remove a directory along with all the
 * files in it.  If the recursive variable is non-zero, then it will
 * try to recursively remove all directories.  If provided, the
 * callback function is executed prior to the directory or file being
 * removed.  If the callback returns non-zero, then no removal is
 * done.
 */
int opal_os_dirpath_destroy(const char *path,
                            bool recursive,
                            opal_os_dirpath_destroy_callback_fn_t cbfunc)
{
    int rc, exit_status = OPAL_SUCCESS;
    bool is_dir = false;
    DIR *dp;
    struct dirent *ep;
    char *filenm;
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
    struct stat buf;
#endif

    if (NULL == path) {  /* protect against error */
        return OPAL_ERROR;
    }

    /*
     * Make sure we have access to the the base directory
     */
    if( OPAL_SUCCESS != (rc = opal_os_dirpath_access(path, 0) ) ) {
        exit_status = rc;
        goto cleanup;
    }

    /* Open up the directory */
    dp = opendir(path);
    if (NULL == dp) {
        return OPAL_ERROR;
    }

    while (NULL != (ep = readdir(dp)) ) {
        /* skip:
         *  - . and ..
         */
        if ((0 == strcmp(ep->d_name, ".")) ||
            (0 == strcmp(ep->d_name, "..")) ) {
            continue;
        }

        /* Check to see if it is a directory */
        is_dir = false;

        /* Create a pathname.  This is not always needed, but it makes
         * for cleaner code just to create it here.  Note that we are
         * allocating memory here, so we need to free it later on.
         */
        filenm = opal_os_path(false, path, ep->d_name, NULL);
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
        if (DT_DIR == ep->d_type) {
            is_dir = true;
        }
#else /* have dirent.d_type */
        rc = stat(filenm, &buf);
        if (rc < 0 || S_ISDIR(buf.st_mode)) {
            is_dir = true;
        }
#endif /* have dirent.d_type */

        /*
         * If not recursively decending, then if we find a directory then fail
         * since we were not told to remove it.
         */
        if( is_dir && !recursive) {
            /* Set the error indicating that we found a directory,
             * but continue removing files
             */
            exit_status = OPAL_ERROR;
            free(filenm);
            continue;
        }

        /* Will the caller allow us to remove this file/directory? */
        if(NULL != cbfunc) {
            /*
             * Caller does not wish to remove this file/directory,
             * continue with the rest of the entries
             */
            if( ! (cbfunc(path, ep->d_name)) ) {
                free(filenm);
                continue;
            }
        }
        /* Directories are recursively destroyed */
        if(is_dir) {
            rc = opal_os_dirpath_destroy(filenm, recursive, cbfunc);
            free(filenm);
            if (OPAL_SUCCESS != rc) {
                exit_status = rc;
                closedir(dp);
                goto cleanup;
            }
        }
        /* Files are removed right here */
        else {
            if( 0 != (rc = unlink(filenm) ) ) {
                exit_status = OPAL_ERROR;
            }
            free(filenm);
        }
    }

    /* Done with this directory */
    closedir(dp);

 cleanup:

    /*
     * If the directory is empty, them remove it
     */
    if(opal_os_dirpath_is_empty(path)) {
        rmdir(path);
    }

    return exit_status;
}
示例#4
0
/** 
 * This function attempts to remove a directory along with all the
 * files in it.  If the recursive variable is non-zero, then it will
 * try to recursively remove all directories.  If provided, the
 * callback function is executed prior to the directory or file being
 * removed.  If the callback returns non-zero, then no removal is
 * done.
 */
int opal_os_dirpath_destroy(const char *path,
                            bool recursive,
                            opal_os_dirpath_destroy_callback_fn_t cbfunc)
{
    int rc, exit_status = OPAL_SUCCESS;
    bool is_dir = false;

    if (NULL == path) {  /* protect against error */
        return OPAL_ERROR;
    }

    /*
     * Make sure we have access to the the base directory
     */
    if( OPAL_SUCCESS != (rc = opal_os_dirpath_access(path, 0) ) ) {
        exit_status = rc;
        goto cleanup;
    }

#ifndef __WINDOWS__
    {
        DIR *dp;
        struct dirent *ep;
        char *filenm;
#ifndef HAVE_STRUCT_DIRENT_D_TYPE 
        struct stat buf;
#endif

        /* Open up the directory */
        dp = opendir(path);
        if (NULL == dp) {
            return OPAL_ERROR;
        }

        while (NULL != (ep = readdir(dp)) ) {
            /* skip:
            *  - . and ..
            */
            if ((0 == strcmp(ep->d_name, ".")) ||
                (0 == strcmp(ep->d_name, "..")) ) {
                    continue;
            }
        
            /* Check to see if it is a directory */
            is_dir = false;

            /* Create a pathname.  This is not always needed, but it makes
             * for cleaner code just to create it here.  Note that we are
             * allocating memory here, so we need to free it later on. 
             */
            filenm = opal_os_path(false, path, ep->d_name, NULL);
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
            if (DT_DIR == ep->d_type) {
                is_dir = true;
            }
#else /* have dirent.d_type */
            rc = stat(filenm, &buf);
            if (rc < 0 || S_ISDIR(buf.st_mode)) {
                is_dir = true;
            }
#endif /* have dirent.d_type */        

            /*
              * If not recursively decending, then if we find a directory then fail
              * since we were not told to remove it.
              */
            if( is_dir && !recursive) {
                /* Set the error indicating that we found a directory,
                 * but continue removing files
                 */
                exit_status = OPAL_ERROR;
                free(filenm);
                continue;
            }

            /* Will the caller allow us to remove this file/directory? */
            if(NULL != cbfunc) {
                /*
                 * Caller does not wish to remove this file/directory,
                 * continue with the rest of the entries
                 */
                if( ! (cbfunc(path, ep->d_name)) ) {
                    free(filenm);
                    continue;
                }
            }
            /* Directories are recursively destroyed */
            if(is_dir) {
                rc = opal_os_dirpath_destroy(filenm, recursive, cbfunc);
                free(filenm);
                if (OPAL_SUCCESS != rc) {
                    exit_status = rc;
                    closedir(dp);
                    goto cleanup;
                }
            }
            /* Files are removed right here */
            else {
                if( 0 != (rc = unlink(filenm) ) ) {
                    exit_status = OPAL_ERROR;
                }
                free(filenm);
            }
        }
    
        /* Done with this directory */
        closedir(dp);
    }
#else
    {
        char search_path[MAX_PATH];
        HANDLE file;
        WIN32_FIND_DATA file_data;
        TCHAR *file_name;
    
        strncpy(search_path, path, strlen(path)+1);
        strncat (search_path, OPAL_PATH_SEP"*", 3);
        file = FindFirstFile(search_path, &file_data);
    
        if (INVALID_HANDLE_VALUE == file) {
            FindClose(file);
            return OPAL_ERROR;
        } 
    
        do {
            /* Skip . and .. */
            if ((0 == strcmp(file_data.cFileName, ".")) ||
                (0 == strcmp(file_data.cFileName, "..")) ) {
                    continue;
            }
        
            is_dir = false;
            if(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
                is_dir = true;
            } 
            /*
             * If not recursively decending, then if we find a directory then fail
             * since we were not told to remove it.
             */
            if( is_dir && !recursive) {
                /* Set the error indicating that we found a directory,
                 * but continue removing files
                 */
                exit_status = OPAL_ERROR;
                continue;
            }
        
            /* Will the caller allow us to remove this file/directory */
            if( NULL != cbfunc) {
                /*
                 * Caller does not wish to remove this file/directory,
                 * continue with the rest of the entries
                 */
                if( !cbfunc(path, file_data.cFileName) ) {
                    continue;
                }
            }

            file_name = opal_os_path(false, path, file_data.cFileName, NULL);
            if( is_dir ) {
                if( OPAL_SUCCESS != (rc = opal_os_dirpath_destroy(file_name,
                                                                  recursive,
                                                                  cbfunc) ) ) {
                    exit_status = rc;
                    free(file_name);
                    FindClose(file);
                    goto cleanup;
                }
            } else {
                DeleteFile(file_name);
            }
            free(file_name);
        } while( 0 != FindNextFile(file, &file_data) );
    
        FindClose(file);
    }
#endif
    
 cleanup:
    
    /*
     * If the directory is empty, them remove it
     */
    if(opal_os_dirpath_is_empty(path)) {
        rmdir(path);
    }

    return exit_status;
}
示例#5
0
/*
 * Construct the session directory and create it if necessary
 */
int orte_session_dir(bool create, 
                     char *prefix, char *hostid,
                     char *batchid, orte_process_name_t *proc)
{
    char *fulldirpath = NULL,
    *frontend     = NULL,
    *sav          = NULL;
    int rc = ORTE_SUCCESS;
    char *local_prefix = NULL;
    
    /* use the specified prefix, if one was given */
    if (NULL != prefix) {
        local_prefix = strdup(prefix);
    }
    
    /*
     * Get the session directory full name
     */
    if( ORTE_SUCCESS != ( rc = orte_session_dir_get_name(&fulldirpath, 
                                                         &local_prefix,
                                                         &frontend,
                                                         hostid, 
                                                         batchid, proc) ) ) {
        if (ORTE_ERR_FATAL == rc) {
            /* this indicates we should abort quietly */
            rc = ORTE_ERR_SILENT;
            goto cleanup;
        }
        /* otherwise, bark a little first */
        ORTE_ERROR_LOG(rc);
        goto cleanup;
    }
    
    /*
     * Now that we have the full path, go ahead and create it if necessary
     */
    if( create ) {
        if( ORTE_SUCCESS != (rc = orte_create_dir(fulldirpath) ) ) {
            ORTE_ERROR_LOG(rc);
            goto cleanup;
        }
    }
    /*
     * if we are not creating, then just verify that the path is OK
     */
    else {
        if( ORTE_SUCCESS != (rc = opal_os_dirpath_access(fulldirpath, 0) )) {
            /* it is okay for the path not to be found - don't error
             * log that case, but do error log others
             */
            if (ORTE_ERR_NOT_FOUND != rc) {
                ORTE_ERROR_LOG(rc);
            }
            goto cleanup;
        }
    }
    
    /*
     * If we are creating the directory tree, then force overwrite of the
     * global structure fields
     */
    if (create) {
    	if (NULL != orte_process_info.tmpdir_base) {
    	    free(orte_process_info.tmpdir_base);
    	    orte_process_info.tmpdir_base = NULL;
    	}
    	if (NULL != orte_process_info.top_session_dir) {
    	    free(orte_process_info.top_session_dir);
    	    orte_process_info.top_session_dir = NULL;
    	}
    }
    
    /* 
     * Update some of the global structures if they are empty
     */
    if (NULL == orte_process_info.tmpdir_base) {
        orte_process_info.tmpdir_base = strdup(local_prefix);
    }
    
    if (NULL == orte_process_info.top_session_dir &&
        NULL != frontend) {
        orte_process_info.top_session_dir = strdup(frontend);
    }

    /*
     * Set the process session directory
     */
    if (ORTE_VPID_INVALID != proc->vpid) {
    	if (create) { /* overwrite if creating */
    	    if (NULL != orte_process_info.proc_session_dir) {
                free(orte_process_info.proc_session_dir);
                orte_process_info.proc_session_dir = NULL;
    	    }
    	}
    	if (NULL == orte_process_info.proc_session_dir) {
    	    orte_process_info.proc_session_dir = strdup(fulldirpath);
    	}
        
        /* Strip off last part of directory structure */
        sav = opal_dirname(fulldirpath);
        free(fulldirpath);
        fulldirpath = sav;
        sav = NULL;
    }
    
    /*
     * Set the job session directory
     */
    if (ORTE_JOBID_INVALID != proc->jobid) {
    	if (create) { /* overwrite if creating */
    	    if (NULL != orte_process_info.job_session_dir) {
                free(orte_process_info.job_session_dir);
                orte_process_info.job_session_dir = NULL;
    	    }
    	}
    	if (NULL == orte_process_info.job_session_dir) {
    	    orte_process_info.job_session_dir = strdup(fulldirpath);
    	}
    }
    
    if (orte_debug_flag) {
    	opal_output(0, "procdir: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.proc_session_dir));
    	opal_output(0, "jobdir: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.job_session_dir));
    	opal_output(0, "top: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.top_session_dir));
    	opal_output(0, "tmp: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.tmpdir_base));
    }
    
cleanup:
    if (NULL != local_prefix) {
        free(local_prefix);
    }
    if(NULL != fulldirpath) {
        free(fulldirpath);
    }
    if(NULL != frontend) {
        free(frontend);
    }
    
    return rc;
}
示例#6
0
int
orte_session_dir_finalize(orte_process_name_t *proc)
{
    int rc=ORTE_SUCCESS;

    if (!orte_create_session_dirs || orte_process_info.rm_session_dirs ) {
        /* we haven't created them or RM will clean them up for us*/
        return ORTE_SUCCESS;
    }

    if (NULL == orte_process_info.job_session_dir ||
        NULL == orte_process_info.proc_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        rc = ORTE_ERR_NOT_INITIALIZED;
        goto CLEANUP;
    }

    opal_os_dirpath_destroy(orte_process_info.proc_session_dir,
                            false, orte_dir_check_file);
    opal_os_dirpath_destroy(orte_process_info.job_session_dir,
                            false, orte_dir_check_file);
    if( NULL != orte_process_info.top_session_dir ){
        opal_os_dirpath_destroy(orte_process_info.top_session_dir,
                                false, orte_dir_check_file);
    }

    if (opal_os_dirpath_is_empty(orte_process_info.proc_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found proc session dir empty - deleting");
        }
        rmdir(orte_process_info.proc_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.proc_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: proc session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: proc session dir not empty - leaving");
            }
        }
        goto CLEANUP;
    }

    if (opal_os_dirpath_is_empty(orte_process_info.job_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found job session dir empty - deleting");
        }
        rmdir(orte_process_info.job_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.job_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: job session dir not empty - leaving");
            }
        }
        goto CLEANUP;
    }

    if(NULL != orte_process_info.top_session_dir) {
        if (opal_os_dirpath_is_empty(orte_process_info.top_session_dir)) {
            if (orte_debug_flag) {
                opal_output(0, "sess_dir_finalize: found top session dir empty - deleting");
            }
            rmdir(orte_process_info.top_session_dir);
        } else {
            if (orte_debug_flag) {
                if (OPAL_ERR_NOT_FOUND ==
                        opal_os_dirpath_access(orte_process_info.top_session_dir, 0)) {
                    opal_output(0, "sess_dir_finalize: top session dir does not exist");
                } else {
                    opal_output(0, "sess_dir_finalize: top session dir not empty - leaving");
                }
            }
        }
    }

CLEANUP:
    return rc;
}
示例#7
0
/*
 * A job has aborted - so force cleanup of the session directory
 */
int
orte_session_dir_cleanup(orte_jobid_t jobid)
{
    int rc = ORTE_SUCCESS;

    if (!orte_create_session_dirs || orte_process_info.rm_session_dirs ) {
        /* we haven't created them or RM will clean them up for us*/
        return ORTE_SUCCESS;
    }

    if (NULL == orte_process_info.job_session_dir ||
        NULL == orte_process_info.proc_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        rc = ORTE_ERR_NOT_INITIALIZED;
        goto CLEANUP;
    }

    /* recursively blow the whole session away for our job family,
     * saving only output files
     */
    opal_os_dirpath_destroy(orte_process_info.job_session_dir,
                            true, orte_dir_check_file);

    /* now attempt to eliminate the top level directory itself - this
     * will fail if anything is present, but ensures we cleanup if
     * we are the last one out
     */
    if( NULL != orte_process_info.top_session_dir ){
        opal_os_dirpath_destroy(orte_process_info.top_session_dir,
                                false, orte_dir_check_file);
    }

    if (opal_os_dirpath_is_empty(orte_process_info.job_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_cleanup: found job session dir empty - deleting");
        }
        rmdir(orte_process_info.job_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.job_session_dir, 0)) {
                opal_output(0, "sess_dir_cleanup: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_cleanup: job session dir not empty - leaving");
            }
        }
        goto CLEANUP;
    }

    if ( NULL != orte_process_info.top_session_dir ){

        if( opal_os_dirpath_is_empty(orte_process_info.top_session_dir) ) {
            if (orte_debug_flag) {
                opal_output(0, "sess_dir_cleanup: found top session dir empty - deleting");
            }
            rmdir(orte_process_info.top_session_dir);
        } else {
            if (orte_debug_flag) {
                if (OPAL_ERR_NOT_FOUND ==
                        opal_os_dirpath_access(orte_process_info.top_session_dir, 0)) {
                    opal_output(0, "sess_dir_cleanup: top session dir does not exist");
                } else {
                    opal_output(0, "sess_dir_cleanup: top session dir not empty - leaving");
                }
            }
        }
    }

CLEANUP:

    return rc;
}
示例#8
0
int
orte_session_dir_finalize(orte_process_name_t *proc)
{
    if (!orte_create_session_dirs || orte_process_info.rm_session_dirs ) {
        /* we haven't created them or RM will clean them up for us*/
        return ORTE_SUCCESS;
    }

    if (NULL == orte_process_info.job_session_dir ||
        NULL == orte_process_info.proc_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        return ORTE_ERR_NOT_INITIALIZED;
    }

    opal_os_dirpath_destroy(orte_process_info.proc_session_dir,
                            false, orte_dir_check_file);
    opal_os_dirpath_destroy(orte_process_info.job_session_dir,
                            false, orte_dir_check_file);
    /* only remove the jobfam session dir if we are the
     * local daemon and we are finalizing our own session dir */
    if ((ORTE_PROC_IS_HNP || ORTE_PROC_IS_DAEMON) &&
        (ORTE_PROC_MY_NAME == proc)) {
        opal_os_dirpath_destroy(orte_process_info.jobfam_session_dir,
                                false, orte_dir_check_file);
    }

    if( NULL != orte_process_info.top_session_dir ){
        opal_os_dirpath_destroy(orte_process_info.top_session_dir,
                                false, orte_dir_check_file);
    }

    if (opal_os_dirpath_is_empty(orte_process_info.proc_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found proc session dir empty - deleting");
        }
        rmdir(orte_process_info.proc_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.proc_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: proc session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: proc session dir not empty - leaving");
            }
        }
    }

    if (opal_os_dirpath_is_empty(orte_process_info.job_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found job session dir empty - deleting");
        }
        rmdir(orte_process_info.job_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.job_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: job session dir not empty - leaving");
            }
        }
    }

    if (opal_os_dirpath_is_empty(orte_process_info.jobfam_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found jobfam session dir empty - deleting");
        }
        rmdir(orte_process_info.jobfam_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.jobfam_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: jobfam session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: jobfam session dir not empty - leaving");
            }
        }
    }

    if(NULL != orte_process_info.top_session_dir) {
        if (opal_os_dirpath_is_empty(orte_process_info.top_session_dir)) {
            if (orte_debug_flag) {
                opal_output(0, "sess_dir_finalize: found top session dir empty - deleting");
            }
            rmdir(orte_process_info.top_session_dir);
        } else {
            if (orte_debug_flag) {
                if (OPAL_ERR_NOT_FOUND ==
                        opal_os_dirpath_access(orte_process_info.top_session_dir, 0)) {
                    opal_output(0, "sess_dir_finalize: top session dir does not exist");
                } else {
                    opal_output(0, "sess_dir_finalize: top session dir not empty - leaving");
                }
            }
        }
    }

    return ORTE_SUCCESS;
}
示例#9
0
int orte_list_local_hnps(opal_list_t *hnps, bool connect)
{
    int ret;
#ifndef __WINDOWS__
    DIR *cur_dirp = NULL;
    struct dirent * dir_entry;
#else
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA file_data;
#endif  /* __WINDOWS__ */
    char *contact_filename = NULL;
    orte_hnp_contact_t *hnp;
    char *headdir;
    
#if !defined(__WINDOWS__)

    /*
     * Check to make sure we have access to the top-level directory
     */
    headdir = opal_os_path(false, orte_process_info.tmpdir_base, orte_process_info.top_session_dir, NULL);
    
    if( ORTE_SUCCESS != (ret = opal_os_dirpath_access(headdir, 0) )) {
        /* it is okay not to find this as there may not be any
         * HNP's present, and we don't write our own session dir
         */
        if (ORTE_ERR_NOT_FOUND != ret) {
            ORTE_ERROR_LOG(ret);
        }
        goto cleanup;
    }
    
    /*
     * Open up the base directory so we can get a listing
     */
    if( NULL == (cur_dirp = opendir(headdir)) ) {
        goto cleanup;
    }
    /*
     * For each directory
     */
    while( NULL != (dir_entry = readdir(cur_dirp)) ) {
        
        /*
         * Skip the obvious
         */
        if( 0 == strncmp(dir_entry->d_name, ".", strlen(".")) ||
            0 == strncmp(dir_entry->d_name, "..", strlen("..")) ) {
            continue;
        }
        
        /*
         * See if a contact file exists in this directory and read it
         */
        contact_filename = opal_os_path( false, headdir,
                                         dir_entry->d_name, "contact.txt", NULL );
        
        hnp = OBJ_NEW(orte_hnp_contact_t);
        if (ORTE_SUCCESS == (ret = orte_read_hnp_contact_file(contact_filename, hnp, connect))) {
            opal_list_append(hnps, &(hnp->super));
        } else {
            OBJ_RELEASE(hnp);
        }
     }
#else
    /*
     * Open up the base directory so we can get a listing.
     *
     * On Windows if we want to parse the content of a directory the filename
     * should end with the "*". Otherwise we will only open the directory
     * structure (and not the content).
     */
    char *subdirs = opal_os_path(false, orte_process_info.tmpdir_base, orte_process_info.top_session_dir, "*", NULL);
    headdir = opal_os_path(false, orte_process_info.tmpdir_base, orte_process_info.top_session_dir, NULL);

    hFind = FindFirstFile( subdirs, &file_data );
    if( INVALID_HANDLE_VALUE == hFind ) {
        goto cleanup;
    }
    
    /*
     * For each directory
     */
    do {
        /*
         * Skip the obvious
         */
        if( 0 == strncmp(file_data.cFileName, ".", strlen(".")) ||
            0 == strncmp(file_data.cFileName, "..", strlen("..")) ) {
            continue;
        }
        
        /*
         * See if a contact file exists in this directory and read it
         */
        contact_filename = opal_os_path( false, headdir,
                                         file_data.cFileName, "contact.txt", NULL );
        
        hnp = OBJ_NEW(orte_hnp_contact_t);
        if (ORTE_SUCCESS == (ret = orte_read_hnp_contact_file(contact_filename, hnp, connect))) {
            opal_list_append(hnps, &(hnp->super));
        } else {
            OBJ_RELEASE(hnp);
        }
    } while( 0 != FindNextFile( hFind, &file_data ) );
    
#endif  /* !defined(__WINDOWS__) */
    
cleanup:
#ifndef __WINDOWS__
    if( NULL != cur_dirp )
        closedir(cur_dirp);
#else
    FindClose(hFind);
#endif  /* __WINDOWS__ */
    free(headdir);
    if( NULL != contact_filename)
        free(contact_filename);
    
    return (opal_list_is_empty(hnps) ? ORTE_ERR_NOT_FOUND : ORTE_SUCCESS);
}
示例#10
0
int orte_list_local_hnps(opal_list_t *hnps, bool connect)
{
    int ret;
    DIR *cur_dirp = NULL;
    struct dirent * dir_entry;
    char *contact_filename = NULL;
    orte_hnp_contact_t *hnp;
    char *headdir;
    
    /*
     * Check to make sure we have access to the top-level directory
     */
    headdir = opal_os_path(false, orte_process_info.tmpdir_base, orte_process_info.top_session_dir, NULL);
    
    if( ORTE_SUCCESS != (ret = opal_os_dirpath_access(headdir, 0) )) {
        /* it is okay not to find this as there may not be any
         * HNP's present, and we don't write our own session dir
         */
        if (ORTE_ERR_NOT_FOUND != ret) {
            ORTE_ERROR_LOG(ret);
        }
        goto cleanup;
    }
    
    /*
     * Open up the base directory so we can get a listing
     */
    if( NULL == (cur_dirp = opendir(headdir)) ) {
        goto cleanup;
    }
    /*
     * For each directory
     */
    while( NULL != (dir_entry = readdir(cur_dirp)) ) {
        
        /*
         * Skip the obvious
         */
        if( 0 == strncmp(dir_entry->d_name, ".", strlen(".")) ||
            0 == strncmp(dir_entry->d_name, "..", strlen("..")) ) {
            continue;
        }
        
        /*
         * See if a contact file exists in this directory and read it
         */
        contact_filename = opal_os_path( false, headdir,
                                         dir_entry->d_name, "contact.txt", NULL );
        
        hnp = OBJ_NEW(orte_hnp_contact_t);
        if (ORTE_SUCCESS == (ret = orte_read_hnp_contact_file(contact_filename, hnp, connect))) {
            opal_list_append(hnps, &(hnp->super));
        } else {
            OBJ_RELEASE(hnp);
        }
     }
    
cleanup:
    if( NULL != cur_dirp )
        closedir(cur_dirp);
    free(headdir);
    if( NULL != contact_filename)
        free(contact_filename);
    
    return (opal_list_is_empty(hnps) ? ORTE_ERR_NOT_FOUND : ORTE_SUCCESS);
}
示例#11
0
/*
 * Construct the session directory and create it if necessary
 */
int orte_session_dir(bool create, 
                     char *prefix, char *usr, char *hostid,
                     char *batchid, char *univ, char *job, char *proc)
{
    char *fulldirpath = NULL,
        *frontend     = NULL,
        *sav          = NULL;
    int return_code = ORTE_SUCCESS, rtn;
    /* This indicates if the prefix was set, and so if it fails then we
     * should try with the default prefixes.*/
    bool dbl_check_prefix = false;

    if( NULL != prefix)
        dbl_check_prefix = true;

 try_again:
    /*
     * If the first attempt at the path creation failed, try with a null
     * prefix. unless the original prefix was null, then we fail.
     */
    if(!dbl_check_prefix && /* an indicator that we are trying a second time */
       NULL != prefix) {
        free(prefix);
        prefix = NULL;
    }
    
    /*
     * Get the session directory full name
     * First try it with the specified prefix.
     */
    if( ORTE_SUCCESS != ( rtn = orte_session_dir_get_name(&fulldirpath, 
                                                          &prefix,
                                                          &frontend,
                                                          usr, hostid, 
                                                          batchid, univ, job,
                                                          proc) ) ) {
        return_code = rtn;
        /*
         * If the first attempt at the path creation failed, try with a null
         * prefix. unless the original prefix was null, then we fail :(
         */
        if(dbl_check_prefix) {
            dbl_check_prefix = false;
            goto try_again;
        }
        else {
            goto cleanup;
        }
    }

    /*
     * Now that we have the full path, go ahead and create it if necessary
     */
    if( create ) {
        if( ORTE_SUCCESS != (rtn = orte_create_dir(fulldirpath) ) ) {
            return_code = rtn;
            
            if(dbl_check_prefix) {
                dbl_check_prefix = false;
                goto try_again;
            }
            else {
                goto cleanup;
            }
        }
    }
    /*
     * if we are not creating, then just verify that the path is OK
     */
    else {
        if( ORTE_SUCCESS != (rtn = opal_os_dirpath_access(fulldirpath, 0) )) {
            /* It is not valid so we give up and return an error */
            return_code = rtn;
            
            if(dbl_check_prefix) {
                dbl_check_prefix = false;
                goto try_again;
            }
            else {
                goto cleanup;
            }
        }
    }

    return_code = ORTE_SUCCESS;

    /*
     * If we are creating the directory tree, the overwrite the
     * global structure fields
     */
    if (create) {
    	if (NULL != orte_process_info.tmpdir_base) {
    	    free(orte_process_info.tmpdir_base);
    	    orte_process_info.tmpdir_base = NULL;
    	}
    	if (NULL != orte_process_info.top_session_dir) {
    	    free(orte_process_info.top_session_dir);
    	    orte_process_info.top_session_dir = NULL;
    	}
    }

    /* 
     * Update some of the global structures if they are empty
     */
    if (NULL == orte_process_info.tmpdir_base)
        orte_process_info.tmpdir_base = strdup(prefix);

    if (NULL == orte_process_info.top_session_dir)
        orte_process_info.top_session_dir = strdup(frontend);
    

    /*
     * Set the process session directory
     */
    if (NULL != proc) {
    	if (create) { /* overwrite if creating */
    	    if (NULL != orte_process_info.proc_session_dir) {
                free(orte_process_info.proc_session_dir);
                orte_process_info.proc_session_dir = NULL;
    	    }
    	}
    	if (NULL == orte_process_info.proc_session_dir) {
    	    orte_process_info.proc_session_dir = strdup(fulldirpath);
    	}

        /* Strip off last part of directory structure */
        sav = opal_dirname(fulldirpath);
        free(fulldirpath);
        fulldirpath = sav;
        sav = NULL;
    }

    /*
     * Set the job session directory
     */
    if (NULL != job) {
    	if (create) { /* overwrite if creating */
    	    if (NULL != orte_process_info.job_session_dir) {
                free(orte_process_info.job_session_dir);
                orte_process_info.job_session_dir = NULL;
    	    }
    	}
    	if (NULL == orte_process_info.job_session_dir) {
    	    orte_process_info.job_session_dir = strdup(fulldirpath);
    	}

        /* Strip off last part of directory structure */
        sav = opal_dirname(fulldirpath);
        free(fulldirpath);
        fulldirpath = sav;
        sav = NULL;
    }

    /*
     * Set the universe session directory
     */
    if (create) { /* overwrite if creating */
    	if (NULL != orte_process_info.universe_session_dir) {
    	    free(orte_process_info.universe_session_dir);
    	    orte_process_info.universe_session_dir = NULL;
    	}
    }
    if (NULL == orte_process_info.universe_session_dir) {
        orte_process_info.universe_session_dir = strdup(fulldirpath);
    }

    if (orte_debug_flag) {
    	opal_output(0, "procdir: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.proc_session_dir));
    	opal_output(0, "jobdir: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.job_session_dir));
    	opal_output(0, "unidir: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.universe_session_dir));
    	opal_output(0, "top: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.top_session_dir));
    	opal_output(0, "tmp: %s", 
                    OMPI_PRINTF_FIX_STRING(orte_process_info.tmpdir_base));
    }

 cleanup:
    if(NULL != fulldirpath)
        free(fulldirpath);
    if(NULL != frontend)
        free(frontend);
    if(NULL != sav)
        free(sav);

    return return_code;
}
示例#12
0
文件: session_dir.c 项目: IanYXXL/A1
int
orte_session_dir_finalize(orte_process_name_t *proc)
{
    int rc;
    char *tmp;
    char *job_session_dir, *vpid, *proc_session_dir;

    if (!orte_create_session_dirs) {
        /* didn't create them */
        return ORTE_SUCCESS;
    }
    
    if (NULL == orte_process_info.tmpdir_base &&
        NULL == orte_process_info.top_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        ORTE_ERROR_LOG(ORTE_ERR_NOT_INITIALIZED);
        return ORTE_ERR_NOT_INITIALIZED;
    }

    /* need to setup the top_session_dir with the prefix */
    tmp = opal_os_path(false,
                       orte_process_info.tmpdir_base,
                       orte_process_info.top_session_dir, NULL);
    
    /* define the proc and job session directories for this process */
    if (ORTE_SUCCESS != (rc = orte_util_convert_vpid_to_string(&vpid, proc->vpid))) {
        ORTE_ERROR_LOG(rc);
        free(tmp);
        return rc;
    }
    job_session_dir = orte_build_job_session_dir(tmp, proc, proc->jobid);
    if( NULL == job_session_dir) {
        free(tmp);
        free(vpid);
        return ORTE_ERR_OUT_OF_RESOURCE;
    }
    proc_session_dir = opal_os_path( false, job_session_dir, vpid, NULL );
    if( NULL == proc_session_dir ) {
        ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
        free(tmp);
        free(vpid);
        free(job_session_dir);
        return ORTE_ERR_OUT_OF_RESOURCE;
    }
    
    opal_os_dirpath_destroy(proc_session_dir,
                            false, orte_dir_check_file);
    opal_os_dirpath_destroy(job_session_dir,
                            false, orte_dir_check_file);
    opal_os_dirpath_destroy(tmp,
                            false, orte_dir_check_file);

    if (opal_os_dirpath_is_empty(proc_session_dir)) {
    	if (orte_debug_flag) {
    	    opal_output(0, "sess_dir_finalize: found proc session dir empty - deleting");
    	}
    	rmdir(proc_session_dir);
    } else {
    	if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND == opal_os_dirpath_access(proc_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: proc session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: proc session dir not empty - leaving");
            }
    	}
        goto CLEANUP;
    }

    if (opal_os_dirpath_is_empty(job_session_dir)) {
    	if (orte_debug_flag) {
    	    opal_output(0, "sess_dir_finalize: found job session dir empty - deleting");
    	}
    	rmdir(job_session_dir);
    } else {
    	if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND == opal_os_dirpath_access(job_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: job session dir not empty - leaving");
            }
    	}
        goto CLEANUP;
    }

    if (opal_os_dirpath_is_empty(tmp)) {
    	if (orte_debug_flag) {
    	    opal_output(0, "sess_dir_finalize: found top session dir empty - deleting");
    	}
    	rmdir(tmp);
    } else {
    	if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND == opal_os_dirpath_access(tmp, 0)) {
                opal_output(0, "sess_dir_finalize: top session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: top session dir not empty - leaving");
            }
    	}
    }

CLEANUP:
    free(tmp);
    free(vpid);
    free(job_session_dir);
    free(proc_session_dir);
    return ORTE_SUCCESS;
}
示例#13
0
文件: session_dir.c 项目: IanYXXL/A1
/*
 * A job has aborted - so force cleanup of the session directory
 */
int
orte_session_dir_cleanup(orte_jobid_t jobid)
{
    int rc = ORTE_SUCCESS;
    char *tmp = NULL;
    char *job_session_dir=NULL;

    if (!orte_create_session_dirs) {
        /* didn't create them */
        return ORTE_SUCCESS;
    }
    
    if (NULL == orte_process_info.tmpdir_base &&
        NULL == orte_process_info.top_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        rc = ORTE_ERR_NOT_INITIALIZED;
        goto CLEANUP;
    }

    /* need to setup the top_session_dir with the prefix */
    tmp = opal_os_path(false,
                       orte_process_info.tmpdir_base,
                       orte_process_info.top_session_dir, NULL);

    /* we can only blow away session directories for our job family */
    job_session_dir = orte_build_job_session_dir(tmp, ORTE_PROC_MY_NAME, jobid);
    if (NULL == job_session_dir) {
        rc = ORTE_ERR_OUT_OF_RESOURCE;
        goto CLEANUP;
    }
    
    if (ORTE_JOBID_WILDCARD != jobid) {
        opal_os_dirpath_destroy(job_session_dir, true, orte_dir_check_file);
    } else {
        /* if we want the session_dir removed for ALL jobids, then
         * just recursively blow the whole session away for our job family,
         * saving only output files
         */
        opal_os_dirpath_destroy(job_session_dir, true, orte_dir_check_file_output);
    }
    
    /* now attempt to eliminate the top level directory itself - this
     * will fail if anything is present, but ensures we cleanup if
     * we are the last one out
     */
    opal_os_dirpath_destroy(tmp, false, orte_dir_check_file);

    if (NULL != job_session_dir && opal_os_dirpath_is_empty(job_session_dir)) {
    	if (orte_debug_flag) {
    	    opal_output(0, "sess_dir_cleanup: found job session dir empty - deleting");
    	}
    	rmdir(job_session_dir);
    } else {
    	if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND == opal_os_dirpath_access(job_session_dir, 0)) {
                opal_output(0, "sess_dir_cleanup: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_cleanup: job session dir not empty - leaving");
            }
    	}
        goto CLEANUP;
    }

    if (opal_os_dirpath_is_empty(tmp)) {
    	if (orte_debug_flag) {
    	    opal_output(0, "sess_dir_cleanup: found top session dir empty - deleting");
    	}
    	rmdir(tmp);
    } else {
    	if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND == opal_os_dirpath_access(tmp, 0)) {
                opal_output(0, "sess_dir_cleanup: top session dir does not exist");
            } else {
                opal_output(0, "sess_dir_cleanup: top session dir not empty - leaving");
            }
    	}
    }

CLEANUP:
    if (NULL != tmp) free(tmp);
    if (NULL != job_session_dir) free(job_session_dir);
    return rc;
}