File: components.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (199 lines) | stat: -rw-r--r-- 5,645 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 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) 2009-2010 Cisco Systems, Inc.  All rights reserved.
 * $COPYRIGHT$
 * 
 * Additional copyrights may follow
 * 
 * $HEADER$
 */

#include "opal_config.h"

#include <string.h>
#include <stdio.h>

#include "opal/constants.h"
#include "opal/mca/mca.h"
#include "opal/libltdl/ltdl.h"

#include "components.h"


/*
 * Local functions
 */
static bool try_dlopen(const char *dir, const char *fw, const char *comp,
                       test_component_handle_t *comp_handle,
                       test_component_sym_t *comp_symbol);
static char *dir_concat(const char *a, const char *b);


int test_component_open(const char *framework, const char *component,
                        test_component_handle_t *comp_handle,
                        mca_base_component_t **mca)
{
    test_component_sym_t sym;

    if (NULL == framework || NULL == component || NULL == comp_handle ||
        NULL == mca) {
        return OPAL_ERR_BAD_PARAM;
    }
    comp_handle->tch_handle = NULL;
    sym.tcs_variable = NULL;

    /* It's ok to call lt_dlinit() multiple times; it ref counts for
       lt_dlexit() */

    lt_dlinit();

    /* Try to open */

    if (try_dlopen(".", framework, component, comp_handle, &sym) ||
        try_dlopen(dir_concat(BUILDDIR, "src/mca"), framework, 
                   component, comp_handle, &sym) ||
        try_dlopen(dir_concat(SRCDIR, "src/mca"), framework, 
                   component, comp_handle, &sym)) {

        /* Ok, dlopen'ed it.  Now call the component's open
           function. */

        *mca = (mca_base_component_t*) sym.tcs_variable;
        if (NULL == (*mca)->mca_open_component ||
            (NULL != (*mca)->mca_open_component &&
             OPAL_SUCCESS == (*mca)->mca_open_component())) {
            return OPAL_SUCCESS;
        }

        /* Badness occurred, so dlclose the component */

        test_component_close(comp_handle);
    }

    /* Didn't find it / unable to open it */

    return OPAL_ERROR;
}


int test_component_find_symbol(const char *name, 
                               test_component_handle_t *handle,
                               test_component_sym_t *sym)
{
    /* Use a union to avoid pesky compilers that complain [rightfully,
       unfortunately] about converting (void*) to a function pointer
       -- need to wait until the ltdl library has a lt_dlsymfunc()
       function for a real fix... */

    union {
        void *vvalue;
        void (*fvalue)(void);
    } value;

    if (NULL == handle || NULL == sym) {
        return OPAL_ERR_BAD_PARAM;
    }

    value.vvalue = lt_dlsym(handle->tch_handle, name);
    sym->tcs_function = value.fvalue;
    return OPAL_SUCCESS;
}


int test_component_close(test_component_handle_t *handle)
{
    /* Note that lt_dlinit() refcounts, so ld_dlexit() only actually
       shuts down if we've called it as many times as we've called
       lt_dlinit() */

    if (NULL != handle && NULL != handle->tch_handle) {
        lt_dlclose(handle->tch_handle);
        lt_dlexit();
        handle->tch_handle = NULL;
    }

    return OPAL_SUCCESS;
}


static bool try_dlopen(const char *dir, const char *fw, const char *comp,
                       test_component_handle_t *comp_handle,
                       test_component_sym_t *comp_symbol)
{
    char component_name[BUFSIZ];
    char file_name[BUFSIZ];
#ifdef __WINDOWS__
    char dirsep = '\\';
#else
    char dirsep = '/';
#endif

    component_name[BUFSIZ - 1] = '\0';
    snprintf(component_name, BUFSIZ - 1, "mca_%s_%s_component", fw, comp);
    if ('\0' != component_name[BUFSIZ - 1]) {
        return false;
    }

    /* First, look for the component symbol statically */

    comp_handle->tch_handle = lt_dlopen(NULL);
    if (NULL != comp_handle->tch_handle) {
        comp_symbol->tcs_variable = 
            lt_dlsym(comp_handle->tch_handle, component_name);
        if (NULL != comp_symbol->tcs_variable) {
            return true;
        }
        lt_dlclose(comp_handle->tch_handle);
    }

    /* Not in self -- look for a real component */

    file_name[BUFSIZ - 1] = '\0';
    snprintf(file_name, BUFSIZ - 1, "%s%c%s%c%s%cmca_%s_%s", dir, dirsep,
             fw, dirsep, comp, dirsep, fw, comp);
    if ('\0' != file_name[BUFSIZ - 1]) {
        return false;
    }
    comp_handle->tch_handle = lt_dlopenext(file_name);
    if (NULL != comp_handle->tch_handle) {
        comp_symbol->tcs_variable = 
            lt_dlsym(comp_handle->tch_handle, component_name);
        if (NULL != comp_symbol->tcs_variable) {
            return true;
        }
        lt_dlclose(comp_handle->tch_handle);
    }

    /* Nope, didn't find it */

    return false;
}


static char *dir_concat(const char *a, const char *b)
{
    static char name[BUFSIZ];
    name[0] = '\0';
    if (NULL != a) {
        strcat(name, a);
#ifdef __WINDOWS__
        strcat(name, "\\");
#else
        strcat(name, "/");
#endif        
    }
    if (NULL != b) {
        strcat(name, b);
    }

    return name;
}