Back to index

glibc  2.9
neededtest.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <libintl.h>
00003 #include <link.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 
00008 #define MAPS ((struct link_map *) _r_debug.r_map)
00009 
00010 static int
00011 check_loaded_objects (const char **loaded)
00012 {
00013   struct link_map *lm;
00014   int n;
00015   int *found = NULL;
00016   int errors = 0;
00017 
00018   for (n = 0; loaded[n]; n++)
00019     /* NOTHING */;
00020 
00021   if (n)
00022     {
00023       found = (int *) alloca (sizeof (int) * n);
00024       memset (found, 0, sizeof (int) * n);
00025     }
00026 
00027   printf("   Name\n");
00028   printf(" --------------------------------------------------------\n");
00029   for (lm = MAPS; lm; lm = lm->l_next)
00030     {
00031       if (lm->l_name && lm->l_name[0])
00032        printf(" %s, count = %d\n", lm->l_name, (int) lm->l_direct_opencount);
00033       if (lm->l_type == lt_loaded && lm->l_name)
00034        {
00035          int match = 0;
00036          for (n = 0; loaded[n] != NULL; n++)
00037            {
00038              if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
00039                {
00040                 found[n] = 1;
00041                 match = 1;
00042                 break;
00043               }
00044            }
00045 
00046          if (match == 0)
00047            {
00048              ++errors;
00049              printf ("ERRORS: %s is not unloaded\n", lm->l_name);
00050            }
00051        }
00052     }
00053 
00054   for (n = 0; loaded[n] != NULL; n++)
00055     {
00056       if (found[n] == 0)
00057         {
00058          ++errors;
00059          printf ("ERRORS: %s is not loaded\n", loaded[n]);
00060        }
00061     }
00062 
00063   return errors;
00064 }
00065 
00066 int
00067 main (void)
00068 {
00069   void *obj2[2];
00070   void *obj3;
00071   const char *loaded[] = { NULL, NULL, NULL, NULL };
00072   int errors = 0;
00073 
00074   printf ("\nThis is what is in memory now:\n");
00075   errors += check_loaded_objects (loaded);
00076 
00077   printf( "Loading shared object neededobj3.so\n");
00078   obj3 = dlopen( "neededobj3.so", RTLD_LAZY);
00079   if (obj3 == NULL)
00080     {
00081       printf ("%s\n", dlerror ());
00082       exit (1);
00083     }
00084   loaded[0] = "neededobj1.so";
00085   loaded[1] = "neededobj2.so";
00086   loaded[2] = "neededobj3.so";
00087   errors += check_loaded_objects (loaded);
00088 
00089   printf ("Now loading shared object neededobj2.so\n");
00090   obj2[0] = dlopen ("neededobj2.so", RTLD_LAZY);
00091   if (obj2[0] == NULL)
00092     {
00093       printf ("%s\n", dlerror ());
00094       exit (1);
00095     }
00096   errors += check_loaded_objects (loaded);
00097 
00098   printf ("And loading shared object neededobj2.so again\n");
00099   obj2[1] = dlopen ("neededobj2.so", RTLD_LAZY);
00100   if (obj2[1] == NULL)
00101     {
00102       printf ("%s\n", dlerror ());
00103       exit (1);
00104     }
00105   errors += check_loaded_objects (loaded);
00106 
00107   printf ("Closing neededobj2.so for the first time\n");
00108   dlclose (obj2[0]);
00109   errors += check_loaded_objects (loaded);
00110 
00111   printf ("Closing neededobj3.so\n");
00112   dlclose (obj3);
00113   loaded[2] = NULL;
00114   errors += check_loaded_objects (loaded);
00115 
00116   printf ("Closing neededobj2.so for the second time\n");
00117   dlclose (obj2[1]);
00118   loaded[0] = NULL;
00119   loaded[1] = NULL;
00120   errors += check_loaded_objects (loaded);
00121 
00122   if (errors != 0)
00123     printf ("%d errors found\n", errors);
00124   return errors;
00125 }