root/ompi/debuggers/dlopen_test.c

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

DEFINITIONS

This source file includes following definitions.
  1. main
  2. try_open
  3. do_test
  4. main

   1 /*
   2  * Copyright (c) 2009-2018 Cisco Systems, Inc.  All rights reserved.
   3  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
   4  * $COPYRIGHT$
   5  *
   6  * Additional copyrights may follow
   7  *
   8  * $HEADER$
   9  */
  10 
  11 #include "opal_config.h"
  12 
  13 #include <stdio.h>
  14 #include <string.h>
  15 #include <stdlib.h>
  16 #include <unistd.h>
  17 
  18 #include "opal/runtime/opal.h"
  19 #include "opal/util/printf.h"
  20 #include "opal/mca/dl/base/base.h"
  21 
  22 #if !OPAL_HAVE_DL_SUPPORT
  23 int main(int argc, char *argv[])
  24 {
  25     /* If OPAL wasn't built with libltdl support, then skip this test */
  26     fprintf(stderr, "OPAL was not built with libltdl support; skipping\n");
  27     return 77;
  28 }
  29 
  30 #else /* OPAL_HAVE_DL_SUPPORT */
  31 
  32 static int try_open(const char *filename)
  33 {
  34     char *err_msg;
  35     opal_dl_handle_t *handle;
  36     int ret;
  37 
  38     ret = opal_dl_open(filename, true, true, &handle, &err_msg);
  39     if (OPAL_SUCCESS == ret) {
  40         opal_dl_close(handle);
  41         printf("File opened with private namespace, all passed\n");
  42         return 0;
  43     }
  44 
  45     printf("Failed to open with private namespace: %s\n", err_msg);
  46     printf("Retrying with global namespace\n");
  47 
  48     ret = opal_dl_open(filename, true, false, &handle, &err_msg);
  49     if (OPAL_SUCCESS == ret) {
  50         opal_dl_close(handle);
  51         printf("File opened with global namespace\n");
  52         return 0;
  53     }
  54 
  55     fprintf(stderr, "File failed to open with global namespace: %s\n",
  56             err_msg);
  57 
  58     return 2;
  59 }
  60 
  61 static int do_test(void)
  62 {
  63     FILE *fp;
  64     char filename[] = "./libompi_dbg_msgq";
  65     char full_filename[] = "./libompi_dbg_msgq.la";
  66     char line[1024];
  67     int happy;
  68 
  69     /* Double check that the .la file is there that we expect; if it's
  70        not, skip this test. */
  71     fp = fopen(full_filename, "r");
  72     if (NULL == fp) {
  73         fprintf(stderr,
  74                 "File %s.la doesn't seem to exist; skipping this test\n",
  75                 full_filename);
  76         exit(77);
  77     }
  78     /* We know the .la file is there, so read it, looking for the
  79        dlopen value.  If the dlopen value is '' (i.e., empty), then
  80        there's nothing to dlopen (i.e., OMPI was built with
  81        --enable-static --disable-shared, so return 77 to skip this
  82        test.  This is horrible, but I can't think of a better way to
  83        check it (since there is no good way to #define whether we have
  84        built statically or not...). */
  85     happy = 0;
  86     while (1) {
  87         if (0 == fgets(line, sizeof(line) - 1, fp)) {
  88             break;
  89         }
  90         if (0 == strncmp(line, "dlname=", 7)) {
  91             if (0 == strncmp(line + 7, "''", 2)) {
  92                 happy = 0;
  93             } else {
  94                 happy = 1;
  95             }
  96             break;
  97         }
  98     }
  99     fclose(fp);
 100     if (!happy) {
 101         fprintf(stderr, "No test file to dlopen (perhaps --enable-static?); skipping\n");
 102         exit(77);
 103     }
 104 
 105     char cwd[4096];
 106     getcwd(cwd, sizeof(cwd) - 1);
 107     cwd[sizeof(cwd) - 1] = '\0';
 108     printf("Running in CWD: %s\n", cwd);
 109 
 110     printf("Trying to open file with private namespace: %s\n", filename);
 111 
 112     /* If that works, great */
 113     if (0 == try_open(filename)) {
 114         return 0;
 115     }
 116 
 117     /* If we're using libltdl, it will find the .la file and may
 118        discover that it needs to open the actual file in the .libs
 119        directory.  If we're not using libltdl, then we won't know
 120        about the magic .la file / .libs directory.  Hueristic: if we
 121        get here, manually prefix the filename with .libs/ and try
 122        again. */
 123     char *rel_filename;
 124     opal_asprintf(&rel_filename, ".libs/%s", filename);
 125     if (NULL == rel_filename) {
 126         return 1;
 127     }
 128     int rc = try_open(rel_filename);
 129     free(rel_filename);
 130 
 131     return rc;
 132 }
 133 
 134 int main(int argc, char *argv[])
 135 {
 136     opal_init(&argc, &argv);
 137     int ret = do_test();
 138     opal_finalize();
 139 
 140     return ret;
 141 }
 142 #endif /* OPAL_HAVE_DL_SUPPORT */

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