Back to index

glibc  2.9
neededtest4.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 extern void c_function (void);
00067 extern char *dirname (__const char *__filename);
00068 
00069 int
00070 main (int argc, char **argv)
00071 {
00072   void *obj;
00073   const char *loaded[] = { NULL, NULL, NULL};
00074   int errors = 0;
00075   void (*f) (void);
00076   const char *dir = dirname (argv [0]);
00077   char *oldfilename;
00078   char *newfilename;
00079 
00080   c_function ();
00081 
00082   printf ("\nThis is what is in memory now:\n");
00083   errors += check_loaded_objects (loaded);
00084 
00085   printf( "Loading shared object neededobj6.so\n");
00086   obj = dlopen( "neededobj6.so", RTLD_LAZY);
00087   if (obj == NULL)
00088     {
00089       printf ("%s\n", dlerror ());
00090       exit (1);
00091     }
00092   f = dlsym (obj, "a2_function");
00093   if (f == NULL)
00094     {
00095       printf ("%s\n", dlerror ());
00096       exit (1);
00097     }
00098   f ();
00099   loaded[0] = "neededobj5.so";
00100   loaded[1] = "neededobj6.so";
00101   errors += check_loaded_objects (loaded);
00102 
00103   printf ("Closing neededobj6.so\n");
00104   dlclose (obj);
00105   loaded[0] = NULL;
00106   errors += check_loaded_objects (loaded);
00107 
00108   printf ("Rename neededobj5.so\n");
00109   oldfilename = alloca (strlen (dir) + 1 + sizeof ("neededobj5.so"));
00110   strcpy (oldfilename, dir);
00111   strcat (oldfilename, "/");
00112   strcat (oldfilename, "neededobj5.so");
00113   newfilename = alloca (strlen (oldfilename) + sizeof (".renamed"));
00114   strcpy (newfilename, oldfilename);
00115   strcat (newfilename, ".renamed");
00116   if (rename (oldfilename, newfilename))
00117     {
00118       perror ("rename");
00119       exit (1);
00120     }
00121 
00122   printf( "Loading shared object neededobj6.so\n");
00123   obj = dlopen( "neededobj6.so", RTLD_LAZY);
00124   if (obj == NULL)
00125     printf ("%s\n", dlerror ());
00126   else
00127     {
00128       printf ("neededobj6.so should fail to load\n");
00129       exit (1);
00130     }
00131 
00132   printf( "Loading shared object neededobj1.so\n");
00133   obj = dlopen( "neededobj1.so", RTLD_LAZY);
00134   if (obj == NULL)
00135     {
00136       printf ("%s\n", dlerror ());
00137       exit (1);
00138     }
00139   errors += check_loaded_objects (loaded);
00140   f = dlsym (obj, "c_function");
00141   if (f == NULL)
00142     {
00143       printf ("%s\n", dlerror ());
00144       exit (1);
00145     }
00146   f ();
00147 
00148   printf ("Restore neededobj5.so\n");
00149   if (rename (newfilename, oldfilename))
00150     {
00151       perror ("rename");
00152       exit (1);
00153     }
00154 
00155   if (errors != 0)
00156     printf ("%d errors found\n", errors);
00157   return errors;
00158 }