relocwrapper.c 5.6 KB

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