1#include <dlfcn.h>
2#include <stdio.h>
3
4
5/* Number of rounds we perform the test. */
6#define TEST_ROUNDS 10
7
8
9static const char unknown[] = "a-file-with-this-name-does-not-exist";
10static const char exists[] = "failtestmod.so";
11
12
13int
14main (void)
15{
16 int i;
17
18 setvbuf (stdout, NULL, _IONBF, n: 0);
19
20 for (i = 0; i < TEST_ROUNDS; ++i)
21 {
22 void *dsc;
23
24 printf (format: "Round %d: Try loading \"%s\"\n", i, unknown);
25
26 dsc = dlopen (file: unknown, RTLD_NOW);
27 if (dsc != NULL)
28 {
29 printf (format: "We found a file of name \"%s\": this should not happen\n",
30 unknown);
31 return 1;
32 }
33
34 printf (format: "Round %d: loading \"%s\" failed\n", i, unknown);
35
36 /* Don't use `dlerror', just load an existing file. */
37 dsc = dlopen (file: exists, RTLD_NOW);
38 if (dsc == NULL)
39 {
40 printf (format: "Could not load \"%s\": %s\n", exists, dlerror ());
41 return 1;
42 }
43
44 printf (format: "Round %d: Loaded \"%s\"\n", i, exists);
45
46 dlclose (handle: dsc);
47
48 printf (format: "Round %d: Unloaded \"%s\"\n", i, exists);
49 }
50
51 return 0;
52}
53
54
55extern void foo (void);
56
57void
58foo (void)
59{
60}
61

source code of glibc/dlfcn/failtest.c