Back to index

glibc  2.9
neededtest2.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[2];
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   printf ("\nLoading shared object neededobj2.so\n");
00077   obj2 = dlopen ("neededobj2.so", RTLD_LAZY);
00078   if (obj2 == NULL)
00079     {
00080       printf ("%s\n", dlerror ());
00081       exit (1);
00082     }
00083   loaded[0] = "neededobj1.so";
00084   loaded[1] = "neededobj2.so";
00085   errors += check_loaded_objects (loaded);
00086   printf ("\nLoading shared object neededobj3.so\n");
00087   obj3[0] = dlopen( "neededobj3.so", RTLD_LAZY);
00088   if (obj3[0] == NULL)
00089     {
00090       printf ("%s\n", dlerror ());
00091       exit (1);
00092     }
00093   loaded[2] = "neededobj3.so";
00094   errors += check_loaded_objects (loaded);
00095   printf ("\nNow loading shared object neededobj3.so again\n");
00096   obj3[1] = dlopen ("neededobj3.so", RTLD_LAZY);
00097   if (obj3[1] == NULL)
00098     {
00099       printf ("%s\n", dlerror ());
00100       exit (1);
00101     }
00102   errors += check_loaded_objects (loaded);
00103   printf ("\nClosing neededobj3.so once\n");
00104   dlclose (obj3[0]);
00105   errors += check_loaded_objects (loaded);
00106   printf ("\nClosing neededobj2.so\n");
00107   dlclose (obj2);
00108   errors += check_loaded_objects (loaded);
00109   printf ("\nClosing neededobj3.so for the second time\n");
00110   dlclose (obj3[1]);
00111   loaded[0] = NULL;
00112   loaded[1] = NULL;
00113   loaded[2] = NULL;
00114   errors += check_loaded_objects (loaded);
00115   if (errors != 0)
00116     printf ("%d errors found\n", errors);
00117   return errors;
00118 }