File: hnp_contact.c

package info (click to toggle)
openmpi 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 99,912 kB
  • ctags: 55,589
  • sloc: ansic: 525,999; f90: 18,307; makefile: 12,062; sh: 6,583; java: 6,278; asm: 3,515; cpp: 2,227; perl: 2,136; python: 1,350; lex: 734; fortran: 52; tcl: 12
file content (237 lines) | stat: -rw-r--r-- 6,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 *
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2011 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2015      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 *
 * $Id: orte_universe_setup_file I/O functions $
 *
 */
#include "orte_config.h"
#include "orte/constants.h"

#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif  /* HAVE_DIRENT_H */

#include "opal/util/os_path.h"
#include "opal/util/output.h"
#include "opal/util/os_dirpath.h"

#include "orte/mca/errmgr/errmgr.h"
#include "orte/mca/rml/rml.h"
#include "orte/mca/rml/base/rml_contact.h"
#include "orte/mca/routed/routed.h"

#include "orte/util/proc_info.h"
#include "orte/util/hnp_contact.h"

#define ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH 1024

/* instantiate the hnp_contact object */
static void orte_hnp_contact_construct(orte_hnp_contact_t *ptr)
{
    ptr->name.jobid = ORTE_JOBID_INVALID;
    ptr->name.vpid = ORTE_VPID_INVALID;

    ptr->rml_uri = NULL;
}
static void orte_hnp_contact_destruct(orte_hnp_contact_t *ptr)
{
    if (NULL != ptr->rml_uri) free(ptr->rml_uri);
}
OBJ_CLASS_INSTANCE(orte_hnp_contact_t,
                   opal_list_item_t,
                   orte_hnp_contact_construct,
                   orte_hnp_contact_destruct);


static char *orte_getline(FILE *fp);

int orte_write_hnp_contact_file(char *filename)
{
    FILE *fp;
    char *my_uri;

    my_uri = orte_rml.get_contact_info();
    if (NULL == my_uri) {
        return ORTE_ERROR;
    }

    fp = fopen(filename, "w");
    if (NULL == fp) {
        opal_output( 0, "Impossible to open the file %s in write mode\n",
                     filename );
        ORTE_ERROR_LOG(ORTE_ERR_FILE_OPEN_FAILURE);
        return ORTE_ERR_FILE_OPEN_FAILURE;
    }

    fprintf(fp, "%s\n", my_uri);
    free(my_uri);

    fprintf(fp, "%lu\n", (unsigned long)orte_process_info.pid);
    fclose(fp);

    return ORTE_SUCCESS;
}

int orte_read_hnp_contact_file(char *filename, orte_hnp_contact_t *hnp, bool connect)
{
    char *hnp_uri, *pidstr;
    FILE *fp;
    int rc;

    fp = fopen(filename, "r");
    if (NULL == fp) { /* failed on first read - wait and try again */
        fp = fopen(filename, "r");
        if (NULL == fp) { /* failed twice - give up */
            return ORTE_ERR_FILE_OPEN_FAILURE;
        }
    }

    hnp_uri = orte_getline(fp);
    if (NULL == hnp_uri) {
        ORTE_ERROR_LOG(ORTE_ERR_FILE_READ_FAILURE);
        fclose(fp);
        return ORTE_ERR_FILE_READ_FAILURE;
    }

    /* get the pid */
    pidstr = orte_getline(fp);
    if (NULL == pidstr) {
        ORTE_ERROR_LOG(ORTE_ERR_FILE_READ_FAILURE);
        fclose(fp);
        free(hnp_uri);
        return ORTE_ERR_FILE_READ_FAILURE;
    }
    hnp->pid = (pid_t)atol(pidstr);
    free(pidstr);
    fclose(fp);

    if (connect) {
        /* set the contact info into the comm hash tables*/
        orte_rml.set_contact_info(hnp_uri);

        /* extract the HNP's name and store it */
        if (ORTE_SUCCESS != (rc = orte_rml_base_parse_uris(hnp_uri, &hnp->name, NULL))) {
            ORTE_ERROR_LOG(rc);
            free(hnp_uri);
            return rc;
        }

        /* set the route to be direct */
        if (ORTE_SUCCESS != (rc = orte_routed.update_route(&hnp->name, &hnp->name))) {
            ORTE_ERROR_LOG(rc);
            free(hnp_uri);
            return rc;
        }
    }
    hnp->rml_uri = hnp_uri;

    return ORTE_SUCCESS;
}

static char *orte_getline(FILE *fp)
{
    char *ret, *buff;
    char input[ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH];

    ret = fgets(input, ORTE_HNP_CONTACT_FILE_MAX_LINE_LENGTH, fp);
    if (NULL != ret) {
	   input[strlen(input)-1] = '\0';  /* remove newline */
	   buff = strdup(input);
	   return buff;
    }

    return NULL;
}


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);
        }
        free(contact_filename);
     }

cleanup:
    if( NULL != cur_dirp )
        closedir(cur_dirp);
    free(headdir);

    return (opal_list_is_empty(hnps) ? ORTE_ERR_NOT_FOUND : ORTE_SUCCESS);
}