Home | History | Annotate | Line # | Download | only in gnulib-lib
relocwrapper.c revision 1.1
      1 /* Relocating wrapper program.
      2    Copyright (C) 2003, 2005-2006 Free Software Foundation, Inc.
      3    Written by Bruno Haible <bruno (at) clisp.org>, 2003.
      4 
      5    This program is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 2, or (at your option)
      8    any later version.
      9 
     10    This program is distributed in the hope that it will be useful,
     11    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     18 
     19 /* Dependencies:
     20    relocwrapper
     21     -> progname
     22     -> progreloc
     23         -> xreadlink
     24            -> readlink
     25         -> canonicalize
     26            -> allocsa
     27     -> relocatable
     28     -> setenv
     29        -> allocsa
     30     -> strerror
     31 
     32    Macros that need to be set while compiling this file:
     33      - ENABLE_RELOCATABLE 1
     34      - INSTALLPREFIX the base installation directory
     35      - INSTALLDIR the directory into which this program is installed
     36      - LIBPATHVAR the platform dependent runtime library path variable
     37      - LIBDIRS a comma-terminated list of strings representing the list of
     38        directories that contain the libraries at installation time
     39 
     40    We don't want to internationalize this wrapper because then it would
     41    depend on libintl and therefore need relocation itself.  So use only
     42    libc functions, no gettext(), no error(), no xmalloc(), no xsetenv().
     43  */
     44 
     45 #include <config.h>
     46 
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #if HAVE_UNISTD_H
     51 # include <unistd.h>
     52 #endif
     53 #include <errno.h>
     54 
     55 #include "progname.h"
     56 #include "relocatable.h"
     57 #include "setenv.h"
     58 
     59 /* Return a copy of the filename, with an extra ".bin" at the end.
     60    More generally, it replaces "${EXEEXT}" at the end with ".bin${EXEEXT}".  */
     61 static char *
     62 add_dotbin (const char *filename)
     63 {
     64   size_t filename_len = strlen (filename);
     65   char *result = (char *) malloc (filename_len + 4 + 1);
     66 
     67   if (result != NULL)
     68     {
     69       if (sizeof (EXEEXT) > sizeof (""))
     70 	{
     71 	  /* EXEEXT handling.  */
     72 	  const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
     73 	  static const char exeext[] = EXEEXT;
     74 	  if (filename_len > exeext_len)
     75 	    {
     76 	      /* Compare using an inlined copy of c_strncasecmp(), because
     77 		 the filenames may have undergone a case conversion since
     78 		 they were packaged.  In other words, EXEEXT may be ".exe"
     79 		 on one system and ".EXE" on another.  */
     80 	      const char *s1 = filename + filename_len - exeext_len;
     81 	      const char *s2 = exeext;
     82 	      for (; *s1 != '\0'; s1++, s2++)
     83 		{
     84 		  unsigned char c1 = *s1;
     85 		  unsigned char c2 = *s2;
     86 		  if ((c1 >= 'A' && c1 <= 'Z' ? c1 - 'A' + 'a' : c1)
     87 		      != (c2 >= 'A' && c2 <= 'Z' ? c2 - 'A' + 'a' : c2))
     88 		    goto simple_append;
     89 		}
     90 	      /* Insert ".bin" before EXEEXT or its equivalent.  */
     91 	      memcpy (result, filename, filename_len - exeext_len);
     92 	      memcpy (result + filename_len - exeext_len, ".bin", 4);
     93 	      memcpy (result + filename_len - exeext_len + 4,
     94 		      filename + filename_len - exeext_len,
     95 		      exeext_len + 1);
     96 	      return result;
     97 	    }
     98 	}
     99      simple_append:
    100       /* Simply append ".bin".  */
    101       memcpy (result, filename, filename_len);
    102       memcpy (result + filename_len, ".bin", 4 + 1);
    103       return result;
    104     }
    105   else
    106     {
    107       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
    108       exit (1);
    109     }
    110 }
    111 
    112 /* List of directories that contain the libraries.  */
    113 static const char *libdirs[] = { LIBDIRS NULL };
    114 /* Verify that at least one directory is given.  */
    115 typedef int verify1[2 * (sizeof (libdirs) / sizeof (libdirs[0]) > 1) - 1];
    116 
    117 /* Relocate the list of directories that contain the libraries.  */
    118 static void
    119 relocate_libdirs ()
    120 {
    121   size_t i;
    122 
    123   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
    124     libdirs[i] = relocate (libdirs[i]);
    125 }
    126 
    127 /* Activate the list of directories in the LIBPATHVAR.  */
    128 static void
    129 activate_libdirs ()
    130 {
    131   const char *old_value;
    132   size_t total;
    133   size_t i;
    134   char *value;
    135   char *p;
    136 
    137   old_value = getenv (LIBPATHVAR);
    138   if (old_value == NULL)
    139     old_value = "";
    140 
    141   total = 0;
    142   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
    143     total += strlen (libdirs[i]) + 1;
    144   total += strlen (old_value) + 1;
    145 
    146   value = (char *) malloc (total);
    147   if (value == NULL)
    148     {
    149       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
    150       exit (1);
    151     }
    152   p = value;
    153   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
    154     {
    155       size_t len = strlen (libdirs[i]);
    156       memcpy (p, libdirs[i], len);
    157       p += len;
    158       *p++ = ':';
    159     }
    160   if (old_value[0] != '\0')
    161     strcpy (p, old_value);
    162   else
    163     p[-1] = '\0';
    164 
    165   if (setenv (LIBPATHVAR, value, 1) < 0)
    166     {
    167       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
    168       exit (1);
    169     }
    170 }
    171 
    172 int
    173 main (int argc, char *argv[])
    174 {
    175   char *full_program_name;
    176 
    177   /* Set the program name and perform preparations for
    178      get_full_program_name() and relocate().  */
    179   set_program_name_and_installdir (argv[0], INSTALLPREFIX, INSTALLDIR);
    180 
    181   /* Get the full program path.  (Important if accessed through a symlink.)  */
    182   full_program_name = get_full_program_name ();
    183   if (full_program_name == NULL)
    184     full_program_name = argv[0];
    185 
    186   /* Invoke the real program, with suffix ".bin".  */
    187   argv[0] = add_dotbin (full_program_name);
    188   relocate_libdirs ();
    189   activate_libdirs ();
    190   execv (argv[0], argv);
    191   fprintf (stderr, "%s: could not execute %s: %s\n",
    192 	   program_name, argv[0], strerror (errno));
    193   exit (127);
    194 }
    195