File: dlopen_test.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 (130 lines) | stat: -rw-r--r-- 3,507 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
/*
 * Copyright (c) 2009-2010 Cisco Systems, Inc.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

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

#include "opal/libltdl/ltdl.h"

static int do_test(void);

int main(int argc, char *argv[])
{
#if OPAL_WANT_LIBLTDL
    return do_test();
#else
    /* If OPAL wasn't built with libltdl support, then skip this test */
    fprintf(stderr, "OPAL was not built with libltdl support; skipping\n");
    return 77;
#endif
}

static int do_test(void)
{
    FILE *fp;
    char filename[] = "./libompi_dbg_msgq";
    char full_filename[] = "./libompi_dbg_msgq.la";
    char line[1024];
    int happy;
    lt_dlhandle dlhandle;

#if OPAL_HAVE_LTDL_ADVISE
    lt_dladvise dladvise;
#endif

    /* Double check that the .la file is there that we expect; if it's
       not, skip this test. */
    fp = fopen(full_filename, "r");
    if (NULL == fp) {
        fprintf(stderr, 
                "File %s.la doesn't seem to exist; skipping this test\n",
                full_filename);
        exit(77);
    }
    /* We know the .la file is there, so read it, looking for the
       dlopen value.  If the dlopen value is '' (i.e., empty), then
       there's nothing to dlopen (i.e., OMPI was built with
       --enable-static --disable-shared, so return 77 to skip this
       test.  This is horrible, but I can't think of a better way to
       check it (since there is no good way to #define whether we have
       built statically or not...). */
    happy = 0;
    while (1) {
        if (0 == fgets(line, sizeof(line) - 1, fp)) {
            break;
        }
        if (0 == strncmp(line, "dlname=", 7)) {
            if (0 == strncmp(line + 7, "''", 2)) {
                happy = 0;
            } else {
                happy = 1;
            }
            break;
        }
    }
    fclose(fp);
    if (!happy) {
        fprintf(stderr, "No test file to dlopen (perhaps --enable-static?); skipping\n");
        exit(77);
    }

    /* Startup LT */
    if (lt_dlinit() != 0) {
        fprintf(stderr, "Failed to lt_dlinit\n");
        return 1;
    }

    printf("Trying to lt_dlopen file with dladvise_local: %s\n", filename);

#if OPAL_HAVE_LTDL_ADVISE
    if (lt_dladvise_init(&dladvise) ||
        lt_dladvise_ext(&dladvise) ||
        lt_dladvise_local(&dladvise)) {
        fprintf(stderr, "lt_dladvise failed to initialize properly\n");
        return 1;
    }
    dlhandle = lt_dlopenadvise(filename, dladvise);
    lt_dladvise_destroy(&dladvise);
#else
    dlhandle = lt_dlopenext(filename);
#endif
    if (NULL != dlhandle) {
        lt_dlclose(dlhandle);
	printf("File opened with dladvise_local, all passed\n");
        return 0;
    }

    printf("Failed to open with dladvise_local: %s\n", lt_dlerror());
    printf("Retrying with dladvise_global\n");

#if OPAL_HAVE_LTDL_ADVISE
    if (lt_dladvise_init(&dladvise) ||
        lt_dladvise_ext(&dladvise) ||
        lt_dladvise_global(&dladvise)) {
        fprintf(stderr, "lt_dladvise failed to initialize properly\n");
        return 1;
    }
    dlhandle = lt_dlopenadvise(filename, dladvise);
    lt_dladvise_destroy(&dladvise);
#else
    dlhandle = lt_dlopenext(filename);
#endif
    if (NULL != dlhandle) {
        lt_dlclose(dlhandle);
	printf("File opened with dladvise_global\n");
	return 0;
    }
    fprintf(stderr, "File failed to open with dladvise_global: %s\n", 
            lt_dlerror());

    return 2;
}