root/orte/util/hnp_contact.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. orte_hnp_contact_construct
  2. orte_hnp_contact_destruct
  3. orte_write_hnp_contact_file
  4. orte_read_hnp_contact_file
  5. orte_getline
  6. orte_list_local_hnps

   1 /*
   2  *
   3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2011 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2005 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2015      Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2016-2019 Intel, Inc.  All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  *
  22  * $Id: orte_universe_setup_file I/O functions $
  23  *
  24  */
  25 #include "orte_config.h"
  26 #include "orte/constants.h"
  27 
  28 #include <stdio.h>
  29 #ifdef HAVE_SYS_TYPES_H
  30 #include <sys/types.h>
  31 #endif
  32 #include <stdarg.h>
  33 #include <string.h>
  34 #ifdef HAVE_UNISTD_H
  35 #include <unistd.h>
  36 #endif
  37 #ifdef HAVE_DIRENT_H
  38 #include <dirent.h>
  39 #endif  /* HAVE_DIRENT_H */
  40 
  41 #include "opal/util/os_path.h"
  42 #include "opal/util/output.h"
  43 #include "opal/util/os_dirpath.h"
  44 #include "opal/mca/pmix/pmix.h"
  45 
  46 #include "orte/mca/errmgr/errmgr.h"
  47 #include "orte/mca/oob/base/base.h"
  48 #include "orte/mca/rml/rml.h"
  49 #include "orte/mca/rml/base/rml_contact.h"
  50 #include "orte/mca/routed/routed.h"
  51 
  52 #include "orte/util/proc_info.h"
  53 #include "orte/util/hnp_contact.h"
  54 
  55 #define ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH 1024
  56 
  57 /* instantiate the hnp_contact object */
  58 static void orte_hnp_contact_construct(orte_hnp_contact_t *ptr)
  59 {
  60     ptr->name.jobid = ORTE_JOBID_INVALID;
  61     ptr->name.vpid = ORTE_VPID_INVALID;
  62 
  63     ptr->rml_uri = NULL;
  64 }
  65 static void orte_hnp_contact_destruct(orte_hnp_contact_t *ptr)
  66 {
  67     if (NULL != ptr->rml_uri) free(ptr->rml_uri);
  68 }
  69 OBJ_CLASS_INSTANCE(orte_hnp_contact_t,
  70                    opal_list_item_t,
  71                    orte_hnp_contact_construct,
  72                    orte_hnp_contact_destruct);
  73 
  74 
  75 static char *orte_getline(FILE *fp);
  76 
  77 int orte_write_hnp_contact_file(char *filename)
  78 {
  79     FILE *fp;
  80     char *my_uri;
  81 
  82     orte_oob_base_get_addr(&my_uri);
  83     if (NULL == my_uri) {
  84         return ORTE_ERROR;
  85     }
  86 
  87     fp = fopen(filename, "w");
  88     if (NULL == fp) {
  89         opal_output( 0, "Impossible to open the file %s in write mode\n",
  90                      filename );
  91         ORTE_ERROR_LOG(ORTE_ERR_FILE_OPEN_FAILURE);
  92         return ORTE_ERR_FILE_OPEN_FAILURE;
  93     }
  94 
  95     fprintf(fp, "%s\n", my_uri);
  96     free(my_uri);
  97 
  98     fprintf(fp, "%lu\n", (unsigned long)orte_process_info.pid);
  99     fclose(fp);
 100 
 101     return ORTE_SUCCESS;
 102 }
 103 
 104 int orte_read_hnp_contact_file(char *filename, orte_hnp_contact_t *hnp, bool connect)
 105 {
 106     char *hnp_uri, *pidstr;
 107     FILE *fp;
 108     int rc;
 109     opal_value_t val;
 110 
 111     fp = fopen(filename, "r");
 112     if (NULL == fp) { /* failed on first read - wait and try again */
 113         fp = fopen(filename, "r");
 114         if (NULL == fp) { /* failed twice - give up */
 115             return ORTE_ERR_FILE_OPEN_FAILURE;
 116         }
 117     }
 118 
 119     hnp_uri = orte_getline(fp);
 120     if (NULL == hnp_uri) {
 121         ORTE_ERROR_LOG(ORTE_ERR_FILE_READ_FAILURE);
 122         fclose(fp);
 123         return ORTE_ERR_FILE_READ_FAILURE;
 124     }
 125 
 126     /* get the pid */
 127     pidstr = orte_getline(fp);
 128     if (NULL == pidstr) {
 129         ORTE_ERROR_LOG(ORTE_ERR_FILE_READ_FAILURE);
 130         fclose(fp);
 131         free(hnp_uri);
 132         return ORTE_ERR_FILE_READ_FAILURE;
 133     }
 134     hnp->pid = (pid_t)atol(pidstr);
 135     free(pidstr);
 136     fclose(fp);
 137 
 138     if (connect) {
 139         /* extract the HNP's name and store it */
 140         if (ORTE_SUCCESS != (rc = orte_rml_base_parse_uris(hnp_uri, &hnp->name, NULL))) {
 141             ORTE_ERROR_LOG(rc);
 142             free(hnp_uri);
 143             return rc;
 144         }
 145 
 146         /* set the contact info into the comm hash tables*/
 147         OBJ_CONSTRUCT(&val, opal_value_t);
 148         val.key = OPAL_PMIX_PROC_URI;
 149         val.type = OPAL_STRING;
 150         val.data.string = hnp_uri;
 151         if (OPAL_SUCCESS != (rc = opal_pmix.store_local(&hnp->name, &val))) {
 152             ORTE_ERROR_LOG(rc);
 153             val.key = NULL;
 154             val.data.string = NULL;
 155             OBJ_DESTRUCT(&val);
 156             free(hnp_uri);
 157             return rc;
 158         }
 159         val.key = NULL;
 160         val.data.string = NULL;
 161         OBJ_DESTRUCT(&val);
 162 
 163         /* set the route to be direct */
 164         if (ORTE_SUCCESS != (rc = orte_routed.update_route(&hnp->name, &hnp->name))) {
 165             ORTE_ERROR_LOG(rc);
 166             free(hnp_uri);
 167             return rc;
 168         }
 169     }
 170     hnp->rml_uri = hnp_uri;
 171 
 172     return ORTE_SUCCESS;
 173 }
 174 
 175 static char *orte_getline(FILE *fp)
 176 {
 177     char *ret, *buff;
 178     char input[ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH];
 179 
 180     ret = fgets(input, ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH, fp);
 181     if (NULL != ret) {
 182        input[strlen(input)-1] = '\0';  /* remove newline */
 183        buff = strdup(input);
 184        return buff;
 185     }
 186 
 187     return NULL;
 188 }
 189 
 190 
 191 int orte_list_local_hnps(opal_list_t *hnps, bool connect)
 192 {
 193     int ret;
 194     DIR *cur_dirp = NULL;
 195     struct dirent * dir_entry;
 196     char *contact_filename = NULL;
 197     orte_hnp_contact_t *hnp;
 198     char *headdir;
 199 
 200     /*
 201      * Check to make sure we have access to the top-level directory
 202      */
 203     headdir = orte_process_info.top_session_dir;
 204 
 205     if( ORTE_SUCCESS != (ret = opal_os_dirpath_access(headdir, 0) )) {
 206         /* it is okay not to find this as there may not be any
 207          * HNP's present, and we don't write our own session dir
 208          */
 209         if (ORTE_ERR_NOT_FOUND != ret) {
 210             ORTE_ERROR_LOG(ret);
 211         }
 212         goto cleanup;
 213     }
 214 
 215     /*
 216      * Open up the base directory so we can get a listing
 217      */
 218     if( NULL == (cur_dirp = opendir(headdir)) ) {
 219         goto cleanup;
 220     }
 221     /*
 222      * For each directory
 223      */
 224     while( NULL != (dir_entry = readdir(cur_dirp)) ) {
 225 
 226         /*
 227          * Skip the obvious
 228          */
 229         if( 0 == strncmp(dir_entry->d_name, ".", strlen(".")) ||
 230             0 == strncmp(dir_entry->d_name, "..", strlen("..")) ) {
 231             continue;
 232         }
 233 
 234         /*
 235          * See if a contact file exists in this directory and read it
 236          */
 237         contact_filename = opal_os_path( false, headdir,
 238                                          dir_entry->d_name, "contact.txt", NULL );
 239 
 240         hnp = OBJ_NEW(orte_hnp_contact_t);
 241         if (ORTE_SUCCESS == (ret = orte_read_hnp_contact_file(contact_filename, hnp, connect))) {
 242             opal_list_append(hnps, &(hnp->super));
 243         } else {
 244             OBJ_RELEASE(hnp);
 245         }
 246         free(contact_filename);
 247      }
 248 
 249 cleanup:
 250     if( NULL != cur_dirp )
 251         closedir(cur_dirp);
 252 
 253     return (opal_list_is_empty(hnps) ? ORTE_ERR_NOT_FOUND : ORTE_SUCCESS);
 254 }

/* [<][>][^][v][top][bottom][index][help] */