Back to index

glibc  2.9
unload.c
Go to the documentation of this file.
00001 /* Test for unloading (really unmapping) of objects.  By Franz Sirl.
00002    This test does not have to passed in all dlopen() et.al. implementation
00003    since it is not required the unloading actually happens.  But we
00004    require it for glibc.  */
00005 
00006 #include <dlfcn.h>
00007 #include <link.h>
00008 #include <mcheck.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 
00012 #define MAPS ((struct link_map *) _r_debug.r_map)
00013 
00014 #define OUT \
00015   for (map = MAPS; map != NULL; map = map->l_next)                          \
00016     if (map->l_type == lt_loaded)                                    \
00017       printf ("name = \"%s\", direct_opencount = %d\n",                     \
00018              map->l_name, (int) map->l_direct_opencount);                   \
00019   fflush (stdout)
00020 
00021 typedef struct
00022 {
00023   void *next;
00024 } strct;
00025 
00026 int
00027 main (void)
00028 {
00029    void *sohandle;
00030    strct *testdat;
00031    int ret;
00032    int result = 0;
00033    struct link_map *map;
00034 
00035    mtrace ();
00036 
00037    puts ("\nBefore");
00038    OUT;
00039 
00040    sohandle = dlopen ("unloadmod.so", RTLD_NOW | RTLD_GLOBAL);
00041    if (sohandle == NULL)
00042      {
00043        printf ("*** first dlopen failed: %s\n", dlerror ());
00044        exit (1);
00045      }
00046 
00047    puts ("\nAfter loading unloadmod.so");
00048    OUT;
00049 
00050    testdat = dlsym (sohandle, "testdat");
00051    testdat->next = (void *) -1;
00052 
00053    ret = dlclose (sohandle);
00054    if (ret != 0)
00055      {
00056        puts ("*** first dlclose failed");
00057        result = 1;
00058      }
00059 
00060    puts ("\nAfter closing unloadmod.so");
00061    OUT;
00062 
00063    sohandle = dlopen ("unloadmod.so", RTLD_NOW | RTLD_GLOBAL);
00064    if (sohandle == NULL)
00065      {
00066        printf ("*** second dlopen failed: %s\n", dlerror ());
00067        exit (1);
00068      }
00069 
00070    puts ("\nAfter loading unloadmod.so the second time");
00071    OUT;
00072 
00073    testdat = dlsym (sohandle, "testdat");
00074    if (testdat->next == (void *) -1)
00075      {
00076        puts ("*** testdat->next == (void *) -1");
00077        result = 1;
00078      }
00079 
00080    ret = dlclose (sohandle);
00081    if (ret != 0)
00082      {
00083        puts ("*** second dlclose failed");
00084        result = 1;
00085      }
00086 
00087    puts ("\nAfter closing unloadmod.so again");
00088    OUT;
00089 
00090    return result;
00091 }