Back to index

glibc  2.9
Defines | Functions | Variables
loadtest.c File Reference
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <error.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <include/link.h>

Go to the source code of this file.

Defines

#define TEST_ROUNDS   1000
#define NOBJS   (sizeof (testobjs) / sizeof (testobjs[0]))
#define NTESTS   (sizeof (tests) / sizeof (tests[0]))
#define MAPS   ((struct link_map *) _r_debug.r_map)
#define OUT

Functions

int main (int argc, char *argv[])
int foo (int a)

Variables

struct {
const char * name
void * handle
testobjs []
struct {
const char * fname
int index
int options
tests []

Define Documentation

#define MAPS   ((struct link_map *) _r_debug.r_map)

Definition at line 73 of file loadtest.c.

#define NOBJS   (sizeof (testobjs) / sizeof (testobjs[0]))

Definition at line 30 of file loadtest.c.

#define NTESTS   (sizeof (tests) / sizeof (tests[0]))

Definition at line 68 of file loadtest.c.

#define OUT
Value:
for (map = MAPS; map != NULL; map = map->l_next)              \
    if (map->l_type == lt_loaded)                                    \
      printf ("name = \"%s\", direct_opencount = %d\n",                     \
             map->l_name, (int) map->l_direct_opencount);                   \
  fflush (stdout)

Definition at line 75 of file loadtest.c.

#define TEST_ROUNDS   1000

Definition at line 12 of file loadtest.c.


Function Documentation

int foo ( int  a)
int main ( int  argc,
char *  argv[] 
)

Definition at line 84 of file loadtest.c.

{
  int debug = argc > 1 && argv[1][0] != '\0';
  int count = TEST_ROUNDS;
  int result = 0;
  struct link_map *map;

  mtrace ();

  /* Just a seed.  */
  srandom (TEST_ROUNDS);

  if (debug)
    {
      puts ("in the beginning");
      OUT;
    }

  while (count--)
    {
      int nr = random () % NTESTS;
      int index = tests[nr].index;

      printf ("%4d: %4d: ", count + 1, nr);
      fflush (stdout);

      if (testobjs[index].handle == NULL)
       {
         int (*fct) (int);

         /* Load the object.  */
         testobjs[index].handle = dlopen (testobjs[index].name,
                                      tests[nr].options);
         if (testobjs[index].handle == NULL)
           error (EXIT_FAILURE, 0, "cannot load `%s': %s",
                 testobjs[index].name, dlerror ());

         /* Test the function call.  */
         fct = dlsym (testobjs[index].handle, tests[nr].fname);
         if (fct == NULL)
           error (EXIT_FAILURE, 0,
                 "cannot get function `%s' from shared object `%s': %s",
                 tests[nr].fname, testobjs[index].name, dlerror ());

         fct (10);

         printf ("successfully loaded `%s', handle %p\n",
                testobjs[index].name, testobjs[index].handle);
       }
      else
       {
         if (dlclose (testobjs[index].handle) != 0)
           {
             printf ("failed to close %s\n", testobjs[index].name);
             result = 1;
           }
         else
           printf ("successfully unloaded `%s', handle %p\n",
                  testobjs[index].name, testobjs[index].handle);

         testobjs[index].handle = NULL;

         if (testobjs[0].handle == NULL
             && testobjs[1].handle == NULL
             && testobjs[5].handle == NULL)
           {
             /* In this case none of the objects above should be
               present.  */
             for (map = MAPS; map != NULL; map = map->l_next)
              if (map->l_type == lt_loaded
                  && (strstr (map->l_name, testobjs[0].name) != NULL
                     || strstr (map->l_name, testobjs[1].name) != NULL
                     || strstr (map->l_name, testobjs[5].name) != NULL))
                {
                  printf ("`%s' is still loaded\n", map->l_name);
                  result = 1;
                }
           }
       }

      if (debug)
       OUT;
    }

  /* Unload all loaded modules.  */
  for (count = 0; count < (int) NOBJS; ++count)
    if (testobjs[count].handle != NULL)
      {
       printf ("\nclose: %s: l_initfini = %p, l_versions = %p\n",
              testobjs[count].name,
              ((struct link_map *) testobjs[count].handle)->l_initfini,
              ((struct link_map *) testobjs[count].handle)->l_versions);

       if (dlclose (testobjs[count].handle) != 0)
         {
           printf ("failed to close %s\n", testobjs[count].name);
           result = 1;
         }
      }

  /* Check whether all files are unloaded.  */
  for (map = MAPS; map != NULL; map = map->l_next)
    if (map->l_type == lt_loaded)
      {
       printf ("name = \"%s\", direct_opencount = %d\n",
              map->l_name, (int) map->l_direct_opencount);
       result = 1;
      }

  return result;
}

Here is the call graph for this function:


Variable Documentation

struct { ... } testobjs[] [static]
struct { ... } tests[] [static]