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