Back to index

glibc  2.9
neededtest3.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;
00070   void *obj3;
00071   void *obj4;
00072   const char *loaded[] = { NULL, NULL, NULL, NULL, NULL };
00073   int errors = 0;
00074 
00075   printf ("\nThis is what is in memory now:\n");
00076   errors += check_loaded_objects (loaded);
00077 
00078   printf ("Now loading shared object neededobj2.so\n");
00079   obj2 = dlopen ("neededobj2.so", RTLD_LAZY);
00080   if (obj2 == NULL)
00081     {
00082       printf ("%s\n", dlerror ());
00083       exit (1);
00084     }
00085   loaded[0] = "neededobj1.so";
00086   loaded[1] = "neededobj2.so";
00087   errors += check_loaded_objects (loaded);
00088 
00089   printf( "Loading shared object neededobj3.so\n");
00090   obj3 = dlopen( "neededobj3.so", RTLD_LAZY);
00091   if (obj3 == NULL)
00092     {
00093       printf ("%s\n", dlerror ());
00094       exit (1);
00095     }
00096   loaded[2] = "neededobj3.so";
00097   errors += check_loaded_objects (loaded);
00098 
00099 
00100   printf( "Loading shared object neededobj4.so\n");
00101   obj4 = dlopen( "neededobj4.so", RTLD_LAZY);
00102   if (obj4 == NULL)
00103     {
00104       printf ("%s\n", dlerror ());
00105       exit (1);
00106     }
00107   loaded[3] = "neededobj4.so";
00108   errors += check_loaded_objects (loaded);
00109 
00110   printf ("Closing neededobj2.so\n");
00111   dlclose (obj2);
00112   errors += check_loaded_objects (loaded);
00113 
00114   printf ("Closing neededobj3.so\n");
00115   dlclose (obj3);
00116   errors += check_loaded_objects (loaded);
00117 
00118   printf ("Closing neededobj4.so\n");
00119   dlclose (obj4);
00120   loaded[0] = NULL;
00121   loaded[1] = NULL;
00122   loaded[2] = NULL;
00123   loaded[3] = NULL;
00124   errors += check_loaded_objects (loaded);
00125 
00126   if (errors != 0)
00127     printf ("%d errors found\n", errors);
00128   return errors;
00129 }