Merge branch 'upstream' into stable
[gnulib.git] / lib / relocwrapper.c
1 /* Relocating wrapper program.
2    Copyright (C) 2003, 2005-2007, 2009-2011 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 /* Dependencies:
19    relocwrapper
20     -> progname
21     -> progreloc
22         -> areadlink
23            -> careadlinkat
24              -> allocator
25              -> readlink
26         -> canonicalize-lgpl
27            -> malloca
28            -> readlink
29     -> relocatable
30     -> setenv
31        -> malloca
32     -> strerror
33     -> c-ctype
34
35    Macros that need to be set while compiling this file:
36      - ENABLE_RELOCATABLE 1
37      - INSTALLPREFIX the base installation directory
38      - INSTALLDIR the directory into which this program is installed
39      - LIBPATHVAR the platform dependent runtime library path variable
40      - LIBDIRS a comma-terminated list of strings representing the list of
41        directories that contain the libraries at installation time
42
43    We don't want to internationalize this wrapper because then it would
44    depend on libintl and therefore need relocation itself.  So use only
45    libc functions, no gettext(), no error(), no xmalloc(), no xsetenv().
46  */
47
48 #define _GL_USE_STDLIB_ALLOC 1
49 #include <config.h>
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <errno.h>
56
57 #include "progname.h"
58 #include "relocatable.h"
59 #include "c-ctype.h"
60 #include "verify.h"
61
62 /* Use the system functions, not the gnulib overrides in this file.  */
63 #undef fprintf
64
65 /* Return a copy of the filename, with an extra ".bin" at the end.
66    More generally, it replaces "${EXEEXT}" at the end with ".bin${EXEEXT}".  */
67 static char *
68 add_dotbin (const char *filename)
69 {
70   size_t filename_len = strlen (filename);
71   char *result = (char *) malloc (filename_len + 4 + 1);
72
73   if (result != NULL)
74     {
75       if (sizeof (EXEEXT) > sizeof (""))
76         {
77           /* EXEEXT handling.  */
78           const size_t exeext_len = sizeof (EXEEXT) - sizeof ("");
79           static const char exeext[] = EXEEXT;
80           if (filename_len > exeext_len)
81             {
82               /* Compare using an inlined copy of c_strncasecmp(), because
83                  the filenames may have undergone a case conversion since
84                  they were packaged.  In other words, EXEEXT may be ".exe"
85                  on one system and ".EXE" on another.  */
86               const char *s1 = filename + filename_len - exeext_len;
87               const char *s2 = exeext;
88               for (; *s1 != '\0'; s1++, s2++)
89                 {
90                   unsigned char c1 = *s1;
91                   unsigned char c2 = *s2;
92                   if (c_tolower (c1) != c_tolower (c2))
93                     goto simple_append;
94                 }
95               /* Insert ".bin" before EXEEXT or its equivalent.  */
96               memcpy (result, filename, filename_len - exeext_len);
97               memcpy (result + filename_len - exeext_len, ".bin", 4);
98               memcpy (result + filename_len - exeext_len + 4,
99                       filename + filename_len - exeext_len,
100                       exeext_len + 1);
101               return result;
102             }
103         }
104      simple_append:
105       /* Simply append ".bin".  */
106       memcpy (result, filename, filename_len);
107       memcpy (result + filename_len, ".bin", 4 + 1);
108       return result;
109     }
110   else
111     {
112       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
113       exit (1);
114     }
115 }
116
117 /* List of directories that contain the libraries.  */
118 static const char *libdirs[] = { LIBDIRS NULL };
119 /* Verify that at least one directory is given.  */
120 verify (sizeof (libdirs) / sizeof (libdirs[0]) > 1);
121
122 /* Relocate the list of directories that contain the libraries.  */
123 static void
124 relocate_libdirs ()
125 {
126   size_t i;
127
128   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
129     libdirs[i] = relocate (libdirs[i]);
130 }
131
132 /* Activate the list of directories in the LIBPATHVAR.  */
133 static void
134 activate_libdirs ()
135 {
136   const char *old_value;
137   size_t total;
138   size_t i;
139   char *value;
140   char *p;
141
142   old_value = getenv (LIBPATHVAR);
143   if (old_value == NULL)
144     old_value = "";
145
146   total = 0;
147   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
148     total += strlen (libdirs[i]) + 1;
149   total += strlen (old_value) + 1;
150
151   value = (char *) malloc (total);
152   if (value == NULL)
153     {
154       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
155       exit (1);
156     }
157   p = value;
158   for (i = 0; i < sizeof (libdirs) / sizeof (libdirs[0]) - 1; i++)
159     {
160       size_t len = strlen (libdirs[i]);
161       memcpy (p, libdirs[i], len);
162       p += len;
163       *p++ = ':';
164     }
165   if (old_value[0] != '\0')
166     strcpy (p, old_value);
167   else
168     p[-1] = '\0';
169
170   if (setenv (LIBPATHVAR, value, 1) < 0)
171     {
172       fprintf (stderr, "%s: %s\n", program_name, "memory exhausted");
173       exit (1);
174     }
175 }
176
177 int
178 main (int argc, char *argv[])
179 {
180   char *full_program_name;
181
182   /* Set the program name and perform preparations for
183      get_full_program_name() and relocate().  */
184   set_program_name_and_installdir (argv[0], INSTALLPREFIX, INSTALLDIR);
185
186   /* Get the full program path.  (Important if accessed through a symlink.)  */
187   full_program_name = get_full_program_name ();
188   if (full_program_name == NULL)
189     full_program_name = argv[0];
190
191   /* Invoke the real program, with suffix ".bin".  */
192   argv[0] = add_dotbin (full_program_name);
193   relocate_libdirs ();
194   activate_libdirs ();
195   execv (argv[0], argv);
196   fprintf (stderr, "%s: could not execute %s: %s\n",
197            program_name, argv[0], strerror (errno));
198   exit (127);
199 }