Back to index

glibc  2.9
multiload.c
Go to the documentation of this file.
00001 #include <dlfcn.h>
00002 #include <errno.h>
00003 #include <mcheck.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <unistd.h>
00008 
00009 int
00010 main (void)
00011 {
00012   void *a;
00013   void *b;
00014   void *c;
00015   void *d;
00016   char *wd;
00017   char *base;
00018   char *buf;
00019 
00020   mtrace ();
00021 
00022   /* Change to the binary directory.  */
00023   if (chdir (OBJDIR) != 0)
00024     {
00025       printf ("cannot change to `%s': %m", OBJDIR);
00026       exit (EXIT_FAILURE);
00027     }
00028 
00029   wd = getcwd (NULL, 0);
00030   base = basename (wd);
00031   buf = alloca (strlen (wd) + strlen (base) + 5 + sizeof "testobj1.so");
00032 
00033   printf ("loading `%s'\n", "./testobj1.so");
00034   a = dlopen ("./testobj1.so", RTLD_NOW);
00035   if (a == NULL)
00036     {
00037       printf ("cannot load `./testobj1.so': %s\n", dlerror ());
00038       exit (EXIT_FAILURE);
00039     }
00040 
00041   stpcpy (stpcpy (stpcpy (buf, "../"), base), "/testobj1.so");
00042   printf ("loading `%s'\n", buf);
00043   b = dlopen (buf, RTLD_NOW);
00044   if (b == NULL)
00045     {
00046       printf ("cannot load `%s': %s\n", buf, dlerror ());
00047       exit (EXIT_FAILURE);
00048     }
00049 
00050   stpcpy (stpcpy (buf, wd), "/testobj1.so");
00051   printf ("loading `%s'\n", buf);
00052   c = dlopen (buf, RTLD_NOW);
00053   if (c == NULL)
00054     {
00055       printf ("cannot load `%s': %s\n", buf, dlerror ());
00056       exit (EXIT_FAILURE);
00057     }
00058 
00059   stpcpy (stpcpy (stpcpy (stpcpy (buf, wd), "/../"), base), "/testobj1.so");
00060   printf ("loading `%s'\n", buf);
00061   d = dlopen (buf, RTLD_NOW);
00062   if (d == NULL)
00063     {
00064       printf ("cannot load `%s': %s\n", buf, dlerror ());
00065       exit (EXIT_FAILURE);
00066     }
00067 
00068   if (a != b || b != c || c != d)
00069     {
00070       puts ("shared object loaded more than once");
00071       exit (EXIT_FAILURE);
00072     }
00073 
00074   if (dlclose (a) != 0)
00075     {
00076       puts ("closing `a' failed");
00077       exit (EXIT_FAILURE);
00078     }
00079   if (dlclose (b) != 0)
00080     {
00081       puts ("closing `a' failed");
00082       exit (EXIT_FAILURE);
00083     }
00084   if (dlclose (c) != 0)
00085     {
00086       puts ("closing `a' failed");
00087       exit (EXIT_FAILURE);
00088     }
00089   if (dlclose (d) != 0)
00090     {
00091       puts ("closing `a' failed");
00092       exit (EXIT_FAILURE);
00093     }
00094 
00095   free (wd);
00096 
00097   return 0;
00098 }
00099 
00100 extern int foo (int a);
00101 int
00102 foo (int a)
00103 {
00104   return a;
00105 }