File: dlopen1.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (78 lines) | stat: -rw-r--r-- 1,846 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
/* To compile, use -DLIB1 to create libdlopen-lib1.so, -DLIB2 for
 *   libdlopen-lib2.so, and define neither to create executable.
 * To run, do:  LD_LIBRARY_PATH=. ./dlopen1
 */

#if !defined(LIB1) && !defined(LIB2)
# include <dlfcn.h>
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>

int (*fnc)(int result[2]);

int main(int argc, char* argv[])
{
  int lib = 1;
  void *handle = NULL;
  int result[2] = {0, 0};
  int i, answer;
  int cnt1 = 0, cnt2 = 0;

  printf("0: "); fflush(stdout);
  while (1) {
    if (handle != NULL)
      dlclose(handle);

    if (lib == 1) {
      handle = dlopen("libdlopen-lib1.so", RTLD_NOW);
      if (handle == NULL) {
        fprintf(stderr, "dlopen failed: %s\n", dlerror());
        exit(1);
      }
      /* See 'man dlopen' for example:  POSIX.1-2002 prefers this workaround */
      *(void **) (&fnc) = dlsym(handle, "fnc");
    }

    if (lib == 2) {
      handle = dlopen("libdlopen-lib2.so", RTLD_LAZY);
      if (handle == NULL) {
        fprintf(stderr, "dlopen failed: %s\n", dlerror());
        exit(1);
      }
      /* See 'man dlopen' for example:  POSIX.1-2002 prefers this workaround */
      *(void **) (&fnc) = dlsym(handle, "fnc");
    }

    assert(lib == 1 || lib == 2);
    for (i = 0; i < 5; i++) {
      answer = fnc(result);
      if (answer != result[lib - 1]) {
        fprintf(stderr, "lib %d returned wrong answer.\n", lib);
        exit(1);
      }
    }
    if (++cnt1 % 1000 == 0) {
      cnt2++;
      cnt1 = 0;
      printf("."); fflush(stdout);
      if (cnt2 % 50 == 0)
        printf("\n%d: ", cnt2 / 50);
      fflush(stdout);
    }
    lib = 3 - lib; /* switch libraries to load */
  }
  return 0;
}

#elif defined(LIB1)
int fnc(int result[2]) {
    return ++(result[0]);
}

#elif defined(LIB2)
int fnc(int result[2]) {
    return ++(result[1]);
}

#endif