]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1999-2024 Free Software Foundation, Inc. |
591e1ffb | 2 | This file is part of the GNU C Library. |
591e1ffb | 3 | |
43bc8ac6 | 4 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
5 | it under the terms of the GNU General Public License as published |
6 | by the Free Software Foundation; version 2 of the License, or | |
7 | (at your option) any later version. | |
591e1ffb | 8 | |
43bc8ac6 | 9 | This program is distributed in the hope that it will be useful, |
591e1ffb | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. | |
591e1ffb | 13 | |
43bc8ac6 | 14 | You should have received a copy of the GNU General Public License |
5a82c748 | 15 | along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
591e1ffb | 16 | |
ee600e3f | 17 | #define PROCINFO_CLASS static |
b44ac4f4 | 18 | #include <assert.h> |
7b0d235c | 19 | #include <alloca.h> |
591e1ffb UD |
20 | #include <argp.h> |
21 | #include <dirent.h> | |
9c95d361 | 22 | #include <elf.h> |
591e1ffb UD |
23 | #include <error.h> |
24 | #include <errno.h> | |
5a35dfca | 25 | #include <inttypes.h> |
591e1ffb | 26 | #include <libintl.h> |
11bf311e | 27 | #include <locale.h> |
f57ebe29 | 28 | #include <stdbool.h> |
591e1ffb | 29 | #include <stdio.h> |
c96873d7 | 30 | #include <stdio_ext.h> |
591e1ffb UD |
31 | #include <stdlib.h> |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
e054f494 | 34 | #include <stdint.h> |
591e1ffb UD |
35 | #include <sys/fcntl.h> |
36 | #include <sys/mman.h> | |
37 | #include <sys/stat.h> | |
38 | #include <sys/types.h> | |
8e115d80 RM |
39 | #include <glob.h> |
40 | #include <libgen.h> | |
591e1ffb | 41 | |
8f480b4b | 42 | #include <ldconfig.h> |
ed2b8d3a | 43 | #include <endswith.h> |
8f480b4b | 44 | #include <dl-cache.h> |
b44ac4f4 | 45 | #include <dl-hwcaps.h> |
6bf789d6 | 46 | #include <dl-is_dso.h> |
591e1ffb | 47 | |
8f480b4b | 48 | #include <dl-procinfo.h> |
591e1ffb UD |
49 | |
50 | #ifndef LD_SO_CONF | |
8ca91b36 | 51 | # define LD_SO_CONF SYSCONFDIR "/ld.so.conf" |
591e1ffb UD |
52 | #endif |
53 | ||
54 | /* Get libc version number. */ | |
55 | #include <version.h> | |
56 | ||
57 | #define PACKAGE _libc_intl_domainname | |
58 | ||
591e1ffb UD |
59 | /* List of directories to handle. */ |
60 | struct dir_entry | |
61 | { | |
62 | char *path; | |
63 | int flag; | |
4ceae915 AJ |
64 | ino64_t ino; |
65 | dev_t dev; | |
b7176cc2 DD |
66 | const char *from_file; |
67 | int from_line; | |
b44ac4f4 FW |
68 | |
69 | /* Non-NULL for subdirectories under a glibc-hwcaps subdirectory. */ | |
70 | struct glibc_hwcaps_subdirectory *hwcaps; | |
71 | ||
591e1ffb UD |
72 | struct dir_entry *next; |
73 | }; | |
74 | ||
75 | /* The list is unsorted, contains no duplicates. Entries are added at | |
76 | the end. */ | |
77 | static struct dir_entry *dir_entries; | |
78 | ||
79 | /* Flags for different options. */ | |
80 | /* Print Cache. */ | |
c96873d7 | 81 | static int opt_print_cache; |
591e1ffb UD |
82 | |
83 | /* Be verbose. */ | |
c96873d7 | 84 | int opt_verbose; |
591e1ffb | 85 | |
45eca4d1 | 86 | /* Format to support. */ |
db07fae8 | 87 | enum opt_format opt_format = opt_format_new; |
45eca4d1 | 88 | |
591e1ffb UD |
89 | /* Build cache. */ |
90 | static int opt_build_cache = 1; | |
91 | ||
920b35c9 FW |
92 | /* Enable symbolic link processing. If set, create or update symbolic |
93 | links, and remove stale symbolic links. */ | |
591e1ffb UD |
94 | static int opt_link = 1; |
95 | ||
96 | /* Only process directories specified on the command line. */ | |
c96873d7 | 97 | static int opt_only_cline; |
591e1ffb UD |
98 | |
99 | /* Path to root for chroot. */ | |
100 | static char *opt_chroot; | |
101 | ||
b85697f6 | 102 | /* Manually link given shared libraries. */ |
c96873d7 | 103 | static int opt_manual_link; |
b85697f6 | 104 | |
27d9ffda UD |
105 | /* Should we ignore an old auxiliary cache file? */ |
106 | static int opt_ignore_aux_cache; | |
107 | ||
591e1ffb | 108 | /* Cache file to use. */ |
b4a555d6 | 109 | static char *cache_file; |
591e1ffb UD |
110 | |
111 | /* Configuration file. */ | |
112 | static const char *config_file; | |
113 | ||
114 | /* Name and version of program. */ | |
115 | static void print_version (FILE *stream, struct argp_state *state); | |
116 | void (*argp_program_version_hook) (FILE *, struct argp_state *) | |
117 | = print_version; | |
118 | ||
cbbcaf23 UD |
119 | /* Function to print some extra text in the help message. */ |
120 | static char *more_help (int key, const char *text, void *input); | |
121 | ||
591e1ffb UD |
122 | /* Definitions of arguments for argp functions. */ |
123 | static const struct argp_option options[] = | |
124 | { | |
125 | { "print-cache", 'p', NULL, 0, N_("Print cache"), 0}, | |
126 | { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0}, | |
127 | { NULL, 'N', NULL, 0, N_("Don't build cache"), 0}, | |
920b35c9 | 128 | { NULL, 'X', NULL, 0, N_("Don't update symbolic links"), 0}, |
7864c09b UD |
129 | { NULL, 'r', N_("ROOT"), 0, N_("Change to and use ROOT as root directory"), 0}, |
130 | { NULL, 'C', N_("CACHE"), 0, N_("Use CACHE as cache file"), 0}, | |
131 | { NULL, 'f', N_("CONF"), 0, N_("Use CONF as configuration file"), 0}, | |
591e1ffb | 132 | { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0}, |
b85697f6 | 133 | { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0}, |
cad64f77 | 134 | { "format", 'c', N_("FORMAT"), 0, N_("Format to use: new (default), old, or compat"), 0}, |
27d9ffda | 135 | { "ignore-aux-cache", 'i', NULL, 0, N_("Ignore auxiliary cache file"), 0}, |
591e1ffb UD |
136 | { NULL, 0, NULL, 0, NULL, 0 } |
137 | }; | |
138 | ||
11bf8ce1 UD |
139 | #define PROCINFO_CLASS static |
140 | #include <dl-procinfo.c> | |
141 | ||
591e1ffb UD |
142 | /* Short description of program. */ |
143 | static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings."); | |
144 | ||
145 | /* Prototype for option handler. */ | |
146 | static error_t parse_opt (int key, char *arg, struct argp_state *state); | |
147 | ||
148 | /* Data structure to communicate with argp functions. */ | |
149 | static struct argp argp = | |
150 | { | |
cbbcaf23 | 151 | options, parse_opt, NULL, doc, NULL, more_help, NULL |
591e1ffb UD |
152 | }; |
153 | ||
591e1ffb UD |
154 | /* Handle program arguments. */ |
155 | static error_t | |
156 | parse_opt (int key, char *arg, struct argp_state *state) | |
157 | { | |
158 | switch (key) | |
159 | { | |
160 | case 'C': | |
161 | cache_file = arg; | |
27d9ffda UD |
162 | /* Ignore auxiliary cache since we use non-standard cache. */ |
163 | opt_ignore_aux_cache = 1; | |
591e1ffb UD |
164 | break; |
165 | case 'f': | |
166 | config_file = arg; | |
167 | break; | |
27d9ffda UD |
168 | case 'i': |
169 | opt_ignore_aux_cache = 1; | |
170 | break; | |
b85697f6 UD |
171 | case 'l': |
172 | opt_manual_link = 1; | |
173 | break; | |
591e1ffb UD |
174 | case 'N': |
175 | opt_build_cache = 0; | |
176 | break; | |
177 | case 'n': | |
178 | opt_build_cache = 0; | |
179 | opt_only_cline = 1; | |
180 | break; | |
181 | case 'p': | |
182 | opt_print_cache = 1; | |
183 | break; | |
184 | case 'r': | |
185 | opt_chroot = arg; | |
186 | break; | |
187 | case 'v': | |
188 | opt_verbose = 1; | |
189 | break; | |
190 | case 'X': | |
191 | opt_link = 0; | |
192 | break; | |
45eca4d1 UD |
193 | case 'c': |
194 | if (strcmp (arg, "old") == 0) | |
db07fae8 | 195 | opt_format = opt_format_old; |
45eca4d1 | 196 | else if (strcmp (arg, "compat") == 0) |
db07fae8 | 197 | opt_format = opt_format_compat; |
45eca4d1 | 198 | else if (strcmp (arg, "new") == 0) |
db07fae8 | 199 | opt_format = opt_format_new; |
45eca4d1 | 200 | break; |
591e1ffb UD |
201 | default: |
202 | return ARGP_ERR_UNKNOWN; | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
cbbcaf23 UD |
208 | /* Print bug-reporting information in the help message. */ |
209 | static char * | |
210 | more_help (int key, const char *text, void *input) | |
211 | { | |
8b748aed | 212 | char *tp = NULL; |
cbbcaf23 UD |
213 | switch (key) |
214 | { | |
215 | case ARGP_KEY_HELP_EXTRA: | |
216 | /* We print some extra information. */ | |
8b748aed | 217 | if (asprintf (&tp, gettext ("\ |
cbbcaf23 | 218 | For bug reporting instructions, please see:\n\ |
8b748aed JM |
219 | %s.\n"), REPORT_BUGS_TO) < 0) |
220 | return NULL; | |
221 | return tp; | |
cbbcaf23 UD |
222 | default: |
223 | break; | |
224 | } | |
225 | return (char *) text; | |
226 | } | |
227 | ||
591e1ffb UD |
228 | /* Print the version information. */ |
229 | static void | |
230 | print_version (FILE *stream, struct argp_state *state) | |
231 | { | |
8b748aed | 232 | fprintf (stream, "ldconfig %s%s\n", PKGVERSION, VERSION); |
591e1ffb UD |
233 | fprintf (stream, gettext ("\ |
234 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
235 | This is free software; see the source for copying conditions. There is NO\n\ | |
236 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
1059defe | 237 | "), "2024"); |
591e1ffb UD |
238 | fprintf (stream, gettext ("Written by %s.\n"), |
239 | "Andreas Jaeger"); | |
240 | } | |
241 | ||
5e598c2b FW |
242 | /* Allocate a new subdirectory with full path PATH under ENTRY, using |
243 | inode data from *ST. */ | |
244 | static struct dir_entry * | |
245 | new_sub_entry (const struct dir_entry *entry, const char *path, | |
9fe6f636 | 246 | const struct stat *st) |
5e598c2b FW |
247 | { |
248 | struct dir_entry *new_entry = xmalloc (sizeof (struct dir_entry)); | |
249 | new_entry->from_file = entry->from_file; | |
250 | new_entry->from_line = entry->from_line; | |
251 | new_entry->path = xstrdup (path); | |
252 | new_entry->flag = entry->flag; | |
b44ac4f4 | 253 | new_entry->hwcaps = NULL; |
5e598c2b FW |
254 | new_entry->next = NULL; |
255 | new_entry->ino = st->st_ino; | |
256 | new_entry->dev = st->st_dev; | |
257 | return new_entry; | |
258 | } | |
259 | ||
b44ac4f4 FW |
260 | /* Add a single directory entry. Return true if the directory is |
261 | actually added (because it is not a duplicate). */ | |
262 | static bool | |
a8fd59b0 AJ |
263 | add_single_dir (struct dir_entry *entry, int verbose) |
264 | { | |
265 | struct dir_entry *ptr, *prev; | |
b44ac4f4 | 266 | bool added = true; |
a8fd59b0 AJ |
267 | |
268 | ptr = dir_entries; | |
269 | prev = ptr; | |
270 | while (ptr != NULL) | |
271 | { | |
272 | /* Check for duplicates. */ | |
4ceae915 | 273 | if (ptr->ino == entry->ino && ptr->dev == entry->dev) |
a8fd59b0 AJ |
274 | { |
275 | if (opt_verbose && verbose) | |
b7176cc2 DD |
276 | { |
277 | error (0, 0, _("Path `%s' given more than once"), entry->path); | |
278 | fprintf (stderr, _("(from %s:%d and %s:%d)\n"), | |
279 | entry->from_file, entry->from_line, | |
280 | ptr->from_file, ptr->from_line); | |
281 | } | |
a8fd59b0 AJ |
282 | /* Use the newer information. */ |
283 | ptr->flag = entry->flag; | |
4ceae915 | 284 | free (entry->path); |
a8fd59b0 | 285 | free (entry); |
b44ac4f4 | 286 | added = false; |
a8fd59b0 AJ |
287 | break; |
288 | } | |
289 | prev = ptr; | |
290 | ptr = ptr->next; | |
291 | } | |
292 | /* Is this the first entry? */ | |
293 | if (ptr == NULL && dir_entries == NULL) | |
294 | dir_entries = entry; | |
295 | else if (ptr == NULL) | |
296 | prev->next = entry; | |
b44ac4f4 FW |
297 | return added; |
298 | } | |
299 | ||
300 | /* Check if PATH contains a "glibc-hwcaps" subdirectory. If so, queue | |
301 | its subdirectories for glibc-hwcaps processing. */ | |
302 | static void | |
303 | add_glibc_hwcaps_subdirectories (struct dir_entry *entry, const char *path) | |
304 | { | |
305 | /* glibc-hwcaps subdirectories do not nest. */ | |
306 | assert (entry->hwcaps == NULL); | |
307 | ||
308 | char *glibc_hwcaps; | |
309 | if (asprintf (&glibc_hwcaps, "%s/" GLIBC_HWCAPS_SUBDIRECTORY, path) < 0) | |
310 | error (EXIT_FAILURE, errno, _("Could not form glibc-hwcaps path")); | |
311 | ||
312 | DIR *dir = opendir (glibc_hwcaps); | |
313 | if (dir != NULL) | |
314 | { | |
315 | while (true) | |
316 | { | |
317 | errno = 0; | |
318 | struct dirent64 *e = readdir64 (dir); | |
319 | if (e == NULL) | |
320 | { | |
321 | if (errno == 0) | |
322 | break; | |
323 | else | |
324 | error (EXIT_FAILURE, errno, _("Listing directory %s"), path); | |
325 | } | |
326 | ||
327 | /* Ignore hidden subdirectories, including "." and "..", and | |
328 | regular files. File names containing a ':' cannot be | |
329 | looked up by the dynamic loader, so skip those as | |
330 | well. */ | |
331 | if (e->d_name[0] == '.' || e->d_type == DT_REG | |
332 | || strchr (e->d_name, ':') != NULL) | |
333 | continue; | |
334 | ||
335 | /* See if this entry eventually resolves to a directory. */ | |
9fe6f636 AZ |
336 | struct stat st; |
337 | if (fstatat (dirfd (dir), e->d_name, &st, 0) < 0) | |
b44ac4f4 FW |
338 | /* Ignore unreadable entries. */ |
339 | continue; | |
340 | ||
341 | if (S_ISDIR (st.st_mode)) | |
342 | { | |
343 | /* This is a directory, so it needs to be scanned for | |
344 | libraries, associated with the hwcaps implied by the | |
345 | subdirectory name. */ | |
346 | char *new_path; | |
347 | if (asprintf (&new_path, "%s/" GLIBC_HWCAPS_SUBDIRECTORY "/%s", | |
348 | /* Use non-canonicalized path here. */ | |
349 | entry->path, e->d_name) < 0) | |
350 | error (EXIT_FAILURE, errno, | |
351 | _("Could not form glibc-hwcaps path")); | |
352 | struct dir_entry *new_entry = new_sub_entry (entry, new_path, | |
353 | &st); | |
354 | free (new_path); | |
355 | new_entry->hwcaps = new_glibc_hwcaps_subdirectory (e->d_name); | |
356 | add_single_dir (new_entry, 0); | |
357 | } | |
358 | } | |
359 | ||
360 | closedir (dir); | |
361 | } | |
362 | ||
363 | free (glibc_hwcaps); | |
a8fd59b0 AJ |
364 | } |
365 | ||
591e1ffb UD |
366 | /* Add one directory to the list of directories to process. */ |
367 | static void | |
b7176cc2 | 368 | add_dir_1 (const char *line, const char *from_file, int from_line) |
591e1ffb | 369 | { |
591e1ffb | 370 | unsigned int i; |
539842a4 | 371 | struct dir_entry *entry = xmalloc (sizeof (struct dir_entry)); |
b44ac4f4 | 372 | entry->hwcaps = NULL; |
591e1ffb | 373 | entry->next = NULL; |
45eca4d1 | 374 | |
b7176cc2 DD |
375 | entry->from_file = strdup (from_file); |
376 | entry->from_line = from_line; | |
377 | ||
591e1ffb | 378 | entry->path = xstrdup (line); |
8ee87859 | 379 | entry->flag = FLAG_ELF_LIBC6; |
591e1ffb | 380 | |
69acbe9f | 381 | /* Canonify path: for now only remove leading and trailing |
00ee369c AS |
382 | whitespace and the trailing slashes. */ |
383 | i = strlen (entry->path); | |
69acbe9f | 384 | |
00ee369c AS |
385 | while (i > 0 && isspace (entry->path[i - 1])) |
386 | entry->path[--i] = '\0'; | |
69acbe9f | 387 | |
00ee369c AS |
388 | while (i > 0 && entry->path[i - 1] == '/') |
389 | entry->path[--i] = '\0'; | |
390 | ||
391 | if (i == 0) | |
b0234d79 SP |
392 | { |
393 | free (entry->path); | |
394 | free (entry); | |
395 | return; | |
396 | } | |
45eca4d1 | 397 | |
539842a4 | 398 | char *path = entry->path; |
22866ed8 | 399 | if (opt_chroot != NULL) |
539842a4 UD |
400 | path = chroot_canon (opt_chroot, path); |
401 | ||
9fe6f636 AZ |
402 | struct stat stat_buf; |
403 | if (path == NULL || stat (path, &stat_buf)) | |
4ceae915 | 404 | { |
8645ad47 AJ |
405 | if (opt_verbose) |
406 | error (0, errno, _("Can't stat %s"), entry->path); | |
4ceae915 AJ |
407 | free (entry->path); |
408 | free (entry); | |
4ceae915 | 409 | } |
539842a4 UD |
410 | else |
411 | { | |
412 | entry->ino = stat_buf.st_ino; | |
413 | entry->dev = stat_buf.st_dev; | |
4ceae915 | 414 | |
b44ac4f4 FW |
415 | if (add_single_dir (entry, 1)) |
416 | /* Add glibc-hwcaps subdirectories if present. */ | |
417 | add_glibc_hwcaps_subdirectories (entry, path); | |
539842a4 | 418 | } |
4ceae915 | 419 | |
22866ed8 | 420 | if (opt_chroot != NULL) |
539842a4 | 421 | free (path); |
591e1ffb UD |
422 | } |
423 | ||
b7176cc2 DD |
424 | static void |
425 | add_dir (const char *line) | |
426 | { | |
427 | add_dir_1 (line, "<builtin>", 0); | |
428 | } | |
591e1ffb | 429 | |
b4a555d6 | 430 | static int |
9fe6f636 | 431 | chroot_stat (const char *real_path, const char *path, struct stat *st) |
b4a555d6 UD |
432 | { |
433 | int ret; | |
434 | char *canon_path; | |
435 | ||
436 | if (!opt_chroot) | |
9fe6f636 | 437 | return stat (real_path, st); |
b4a555d6 | 438 | |
9fe6f636 | 439 | ret = lstat (real_path, st); |
b4a555d6 UD |
440 | if (ret || !S_ISLNK (st->st_mode)) |
441 | return ret; | |
442 | ||
443 | canon_path = chroot_canon (opt_chroot, path); | |
444 | if (canon_path == NULL) | |
445 | return -1; | |
446 | ||
9fe6f636 | 447 | ret = stat (canon_path, st); |
b4a555d6 UD |
448 | free (canon_path); |
449 | return ret; | |
450 | } | |
451 | ||
591e1ffb UD |
452 | /* Create a symbolic link from soname to libname in directory path. */ |
453 | static void | |
b4a555d6 UD |
454 | create_links (const char *real_path, const char *path, const char *libname, |
455 | const char *soname) | |
591e1ffb | 456 | { |
7b0d235c | 457 | char *full_libname, *full_soname; |
b4a555d6 | 458 | char *real_full_libname, *real_full_soname; |
9fe6f636 | 459 | struct stat stat_lib, stat_so, lstat_so; |
591e1ffb UD |
460 | int do_link = 1; |
461 | int do_remove = 1; | |
462 | /* XXX: The logics in this function should be simplified. */ | |
45eca4d1 | 463 | |
591e1ffb | 464 | /* Get complete path. */ |
7b0d235c | 465 | full_libname = alloca (strlen (path) + strlen (libname) + 2); |
7c545dbe | 466 | full_soname = alloca (strlen (path) + strlen (soname) + 2); |
7b0d235c AJ |
467 | sprintf (full_libname, "%s/%s", path, libname); |
468 | sprintf (full_soname, "%s/%s", path, soname); | |
22866ed8 | 469 | if (opt_chroot != NULL) |
b4a555d6 UD |
470 | { |
471 | real_full_libname = alloca (strlen (real_path) + strlen (libname) + 2); | |
7c545dbe | 472 | real_full_soname = alloca (strlen (real_path) + strlen (soname) + 2); |
b4a555d6 UD |
473 | sprintf (real_full_libname, "%s/%s", real_path, libname); |
474 | sprintf (real_full_soname, "%s/%s", real_path, soname); | |
475 | } | |
476 | else | |
477 | { | |
478 | real_full_libname = full_libname; | |
479 | real_full_soname = full_soname; | |
480 | } | |
591e1ffb UD |
481 | |
482 | /* Does soname already exist and point to the right library? */ | |
b4a555d6 | 483 | if (chroot_stat (real_full_soname, full_soname, &stat_so) == 0) |
591e1ffb | 484 | { |
b4a555d6 | 485 | if (chroot_stat (real_full_libname, full_libname, &stat_lib)) |
591e1ffb UD |
486 | { |
487 | error (0, 0, _("Can't stat %s\n"), full_libname); | |
488 | return; | |
489 | } | |
490 | if (stat_lib.st_dev == stat_so.st_dev | |
491 | && stat_lib.st_ino == stat_so.st_ino) | |
492 | /* Link is already correct. */ | |
493 | do_link = 0; | |
9fe6f636 | 494 | else if (lstat (full_soname, &lstat_so) == 0 |
591e1ffb UD |
495 | && !S_ISLNK (lstat_so.st_mode)) |
496 | { | |
497 | error (0, 0, _("%s is not a symbolic link\n"), full_soname); | |
498 | do_link = 0; | |
499 | do_remove = 0; | |
500 | } | |
501 | } | |
9fe6f636 | 502 | else if (lstat (real_full_soname, &lstat_so) != 0 |
591e1ffb UD |
503 | || !S_ISLNK (lstat_so.st_mode)) |
504 | /* Unless it is a stale symlink, there is no need to remove. */ | |
505 | do_remove = 0; | |
506 | ||
507 | if (opt_verbose) | |
508 | printf ("\t%s -> %s", soname, libname); | |
509 | ||
510 | if (do_link && opt_link) | |
511 | { | |
512 | /* Remove old link. */ | |
513 | if (do_remove) | |
b4a555d6 | 514 | if (unlink (real_full_soname)) |
591e1ffb UD |
515 | { |
516 | error (0, 0, _("Can't unlink %s"), full_soname); | |
517 | do_link = 0; | |
518 | } | |
519 | /* Create symbolic link. */ | |
b4a555d6 | 520 | if (do_link && symlink (libname, real_full_soname)) |
591e1ffb UD |
521 | { |
522 | error (0, 0, _("Can't link %s to %s"), full_soname, libname); | |
523 | do_link = 0; | |
524 | } | |
525 | if (opt_verbose) | |
526 | { | |
527 | if (do_link) | |
528 | fputs (_(" (changed)\n"), stdout); | |
529 | else | |
530 | fputs (_(" (SKIPPED)\n"), stdout); | |
531 | } | |
532 | } | |
533 | else if (opt_verbose) | |
534 | fputs ("\n", stdout); | |
535 | } | |
536 | ||
b85697f6 UD |
537 | /* Manually link the given library. */ |
538 | static void | |
539 | manual_link (char *library) | |
540 | { | |
541 | char *path; | |
b4a555d6 UD |
542 | char *real_path; |
543 | char *real_library; | |
b85697f6 UD |
544 | char *libname; |
545 | char *soname; | |
9fe6f636 | 546 | struct stat stat_buf; |
b85697f6 | 547 | int flag; |
efbbd9c3 | 548 | unsigned int isa_level; |
b85697f6 UD |
549 | |
550 | /* Prepare arguments for create_links call. Split library name in | |
551 | directory and filename first. Since path is allocated, we've got | |
552 | to be careful to free at the end. */ | |
553 | path = xstrdup (library); | |
554 | libname = strrchr (path, '/'); | |
555 | ||
556 | if (libname) | |
557 | { | |
558 | /* Successfully split names. Check if path is just "/" to avoid | |
27d9ffda | 559 | an empty path. */ |
b85697f6 UD |
560 | if (libname == path) |
561 | { | |
562 | libname = library + 1; | |
563 | path = xrealloc (path, 2); | |
564 | strcpy (path, "/"); | |
565 | } | |
566 | else | |
567 | { | |
568 | *libname = '\0'; | |
569 | ++libname; | |
570 | } | |
571 | } | |
572 | else | |
573 | { | |
574 | /* There's no path, construct one. */ | |
575 | libname = library; | |
576 | path = xrealloc (path, 2); | |
577 | strcpy (path, "."); | |
578 | } | |
579 | ||
22866ed8 | 580 | if (opt_chroot != NULL) |
b4a555d6 UD |
581 | { |
582 | real_path = chroot_canon (opt_chroot, path); | |
583 | if (real_path == NULL) | |
584 | { | |
585 | error (0, errno, _("Can't find %s"), path); | |
586 | free (path); | |
587 | return; | |
588 | } | |
589 | real_library = alloca (strlen (real_path) + strlen (libname) + 2); | |
590 | sprintf (real_library, "%s/%s", real_path, libname); | |
591 | } | |
592 | else | |
593 | { | |
594 | real_path = path; | |
595 | real_library = library; | |
596 | } | |
597 | ||
b85697f6 | 598 | /* Do some sanity checks first. */ |
9fe6f636 | 599 | if (lstat (real_library, &stat_buf)) |
b85697f6 | 600 | { |
11bf311e | 601 | error (0, errno, _("Cannot lstat %s"), library); |
468d772e | 602 | goto out; |
b85697f6 UD |
603 | } |
604 | /* We don't want links here! */ | |
605 | else if (!S_ISREG (stat_buf.st_mode)) | |
606 | { | |
607 | error (0, 0, _("Ignored file %s since it is not a regular file."), | |
608 | library); | |
468d772e | 609 | goto out; |
b85697f6 | 610 | } |
27d9ffda | 611 | |
c628c229 AZ |
612 | if (process_file (real_library, library, libname, &flag, &isa_level, &soname, |
613 | 0, &stat_buf)) | |
b85697f6 UD |
614 | { |
615 | error (0, 0, _("No link created since soname could not be found for %s"), | |
616 | library); | |
468d772e | 617 | goto out; |
b85697f6 | 618 | } |
27d9ffda | 619 | if (soname == NULL) |
1b0ea8c5 | 620 | soname = xstrdup (libname); |
b4a555d6 | 621 | create_links (real_path, path, libname, soname); |
b85697f6 | 622 | free (soname); |
468d772e | 623 | out: |
468d772e SP |
624 | if (path != real_path) |
625 | free (real_path); | |
03ad8688 | 626 | free (path); |
b85697f6 UD |
627 | } |
628 | ||
629 | ||
591e1ffb UD |
630 | /* Read a whole directory and search for libraries. |
631 | The purpose is two-fold: | |
632 | - search for libraries which will be added to the cache | |
633 | - create symbolic links to the soname for each library | |
634 | ||
630da022 | 635 | This has to be done separately for each directory. |
45eca4d1 | 636 | |
591e1ffb UD |
637 | To keep track of which libraries to add to the cache and which |
638 | links to create, we save a list of all libraries. | |
639 | ||
640 | The algorithm is basically: | |
641 | for all libraries in the directory do | |
642 | get soname of library | |
643 | if soname is already in list | |
644 | if new library is newer, replace entry | |
645 | otherwise ignore this library | |
646 | otherwise add library to list | |
45eca4d1 | 647 | |
591e1ffb UD |
648 | For example, if the two libraries libxy.so.1.1 and libxy.so.1.2 |
649 | exist and both have the same soname, e.g. libxy.so, a symbolic link | |
650 | is created from libxy.so.1.2 (the newer one) to libxy.so. | |
651 | libxy.so.1.2 and libxy.so are added to the cache - but not | |
652 | libxy.so.1.1. */ | |
653 | ||
654 | /* Information for one library. */ | |
45eca4d1 | 655 | struct dlib_entry |
591e1ffb UD |
656 | { |
657 | char *name; | |
658 | char *soname; | |
659 | int flag; | |
660 | int is_link; | |
efbbd9c3 | 661 | unsigned int isa_level; |
591e1ffb UD |
662 | struct dlib_entry *next; |
663 | }; | |
664 | ||
2aa0974d FW |
665 | /* Skip some temporary DSO files. These files may be partially written |
666 | and lead to ldconfig crashes when examined. */ | |
667 | static bool | |
668 | skip_dso_based_on_name (const char *name, size_t len) | |
669 | { | |
670 | /* Skip temporary files created by the prelink program. Files with | |
671 | names like these are never really DSOs we want to look at. */ | |
672 | if (len >= sizeof (".#prelink#") - 1) | |
673 | { | |
cfb5a97a | 674 | if (endswithn (name, len, ".#prelink#")) |
2aa0974d FW |
675 | return true; |
676 | if (len >= sizeof (".#prelink#.XXXXXX") - 1 | |
677 | && memcmp (name + len - sizeof (".#prelink#.XXXXXX") | |
678 | + 1, ".#prelink#.", sizeof (".#prelink#.") - 1) == 0) | |
679 | return true; | |
680 | } | |
681 | /* Skip temporary files created by RPM. */ | |
cfb5a97a | 682 | if (memchr (name, ';', len) != NULL) |
2aa0974d FW |
683 | return true; |
684 | /* Skip temporary files created by dpkg. */ | |
cfb5a97a FW |
685 | if (endswithn (name, len, ".dpkg-new") |
686 | || endswithn (name, len, ".dpkg-tmp")) | |
2aa0974d FW |
687 | return true; |
688 | return false; | |
689 | } | |
591e1ffb UD |
690 | |
691 | static void | |
692 | search_dir (const struct dir_entry *entry) | |
693 | { | |
b78ff5a2 | 694 | if (opt_verbose) |
b44ac4f4 | 695 | { |
b78ff5a2 JP |
696 | if (entry->hwcaps == NULL) |
697 | printf ("%s:", entry->path); | |
698 | else | |
b44ac4f4 FW |
699 | printf ("%s: (hwcap: \"%s\")", entry->path, |
700 | glibc_hwcaps_subdirectory_name (entry->hwcaps)); | |
b78ff5a2 | 701 | printf (_(" (from %s:%d)\n"), entry->from_file, entry->from_line); |
b44ac4f4 | 702 | } |
45eca4d1 | 703 | |
27d9ffda UD |
704 | char *dir_name; |
705 | char *real_file_name; | |
daf7b53a | 706 | char *file_name; |
22866ed8 | 707 | if (opt_chroot != NULL) |
daf7b53a | 708 | dir_name = chroot_canon (opt_chroot, entry->path); |
b4a555d6 | 709 | else |
daf7b53a | 710 | dir_name = entry->path; |
b4a555d6 | 711 | |
27d9ffda | 712 | DIR *dir; |
b4a555d6 | 713 | if (dir_name == NULL || (dir = opendir (dir_name)) == NULL) |
591e1ffb UD |
714 | { |
715 | if (opt_verbose) | |
716 | error (0, errno, _("Can't open directory %s"), entry->path); | |
daf7b53a | 717 | if (opt_chroot != NULL) |
b4a555d6 | 718 | free (dir_name); |
591e1ffb UD |
719 | return; |
720 | } | |
45eca4d1 | 721 | |
27d9ffda UD |
722 | struct dirent64 *direntry; |
723 | struct dlib_entry *dlibs = NULL; | |
2b510e91 | 724 | while ((direntry = readdir64 (dir)) != NULL) |
591e1ffb UD |
725 | { |
726 | int flag; | |
591e1ffb UD |
727 | /* We only look at links and regular files. */ |
728 | if (direntry->d_type != DT_UNKNOWN | |
729 | && direntry->d_type != DT_LNK | |
45eca4d1 UD |
730 | && direntry->d_type != DT_REG |
731 | && direntry->d_type != DT_DIR) | |
591e1ffb | 732 | continue; |
b78ff5a2 JP |
733 | /* Does this file look like a shared library? The dynamic linker |
734 | is also considered as shared library. */ | |
6bf789d6 | 735 | if (!_dl_is_dso (direntry->d_name) |
b78ff5a2 | 736 | && (direntry->d_type == DT_REG || entry->hwcaps == NULL)) |
591e1ffb | 737 | continue; |
27d9ffda UD |
738 | |
739 | size_t len = strlen (direntry->d_name); | |
2aa0974d FW |
740 | if (skip_dso_based_on_name (direntry->d_name, len)) |
741 | continue; | |
daf7b53a SB |
742 | if (asprintf (&file_name, "%s/%s", entry->path, direntry->d_name) < 0) |
743 | error (EXIT_FAILURE, errno, _("Could not form library path")); | |
22866ed8 | 744 | if (opt_chroot != NULL) |
b4a555d6 | 745 | { |
daf7b53a SB |
746 | if (asprintf (&real_file_name, "%s/%s", |
747 | dir_name, direntry->d_name) < 0) | |
748 | error (EXIT_FAILURE, errno, _("Could not form library path")); | |
591e1ffb | 749 | } |
daf7b53a SB |
750 | else |
751 | real_file_name = xstrdup (file_name); | |
27d9ffda | 752 | |
9fe6f636 | 753 | struct stat lstat_buf; |
27d9ffda | 754 | /* We optimize and try to do the lstat call only if needed. */ |
7c3002f0 | 755 | if (direntry->d_type != DT_UNKNOWN) |
4ceae915 | 756 | lstat_buf.st_mode = DTTOIF (direntry->d_type); |
7c3002f0 | 757 | else |
9fe6f636 | 758 | if (__glibc_unlikely (lstat (real_file_name, &lstat_buf))) |
7c3002f0 | 759 | { |
95ce33a5 | 760 | error (0, errno, _("Cannot lstat %s"), file_name); |
daf7b53a | 761 | goto next; |
7c3002f0 UD |
762 | } |
763 | ||
9fe6f636 | 764 | struct stat stat_buf; |
27d9ffda | 765 | int is_link = S_ISLNK (lstat_buf.st_mode); |
4ceae915 | 766 | if (is_link) |
27d9ffda | 767 | { |
4ceae915 AJ |
768 | /* In case of symlink, we check if the symlink refers to |
769 | a directory. */ | |
7dee2660 | 770 | char *target_name = real_file_name; |
22866ed8 | 771 | if (opt_chroot != NULL) |
7dee2660 UD |
772 | { |
773 | target_name = chroot_canon (opt_chroot, file_name); | |
774 | if (target_name == NULL) | |
775 | { | |
776 | if (strstr (file_name, ".so") == NULL) | |
777 | error (0, 0, _("Input file %s not found.\n"), file_name); | |
daf7b53a | 778 | goto next; |
7dee2660 UD |
779 | } |
780 | } | |
9fe6f636 | 781 | if (__glibc_unlikely (stat (target_name, &stat_buf))) |
4ceae915 | 782 | { |
8645ad47 | 783 | if (opt_verbose) |
95ce33a5 | 784 | error (0, errno, _("Cannot stat %s"), file_name); |
647eb037 UD |
785 | |
786 | /* Remove stale symlinks. */ | |
920b35c9 | 787 | if (opt_link && strstr (direntry->d_name, ".so.")) |
647eb037 | 788 | unlink (real_file_name); |
468d772e SP |
789 | |
790 | if (opt_chroot != NULL) | |
791 | free (target_name); | |
792 | ||
daf7b53a | 793 | goto next; |
4ceae915 | 794 | } |
468d772e SP |
795 | |
796 | if (opt_chroot != NULL) | |
797 | free (target_name); | |
798 | ||
27d9ffda UD |
799 | /* lstat_buf is later stored, update contents. */ |
800 | lstat_buf.st_dev = stat_buf.st_dev; | |
801 | lstat_buf.st_ino = stat_buf.st_ino; | |
802 | lstat_buf.st_size = stat_buf.st_size; | |
803 | lstat_buf.st_ctime = stat_buf.st_ctime; | |
4ceae915 | 804 | } |
b78ff5a2 | 805 | else if (!S_ISREG (lstat_buf.st_mode)) |
daf7b53a | 806 | goto next; |
591e1ffb | 807 | |
27d9ffda | 808 | char *real_name; |
22866ed8 | 809 | if (opt_chroot != NULL && is_link) |
b4a555d6 UD |
810 | { |
811 | real_name = chroot_canon (opt_chroot, file_name); | |
812 | if (real_name == NULL) | |
813 | { | |
814 | if (strstr (file_name, ".so") == NULL) | |
815 | error (0, 0, _("Input file %s not found.\n"), file_name); | |
daf7b53a | 816 | goto next; |
b4a555d6 UD |
817 | } |
818 | } | |
819 | else | |
820 | real_name = real_file_name; | |
591e1ffb | 821 | |
9fe6f636 | 822 | /* Call lstat if not done yet. */ |
27d9ffda UD |
823 | if (!is_link |
824 | && direntry->d_type != DT_UNKNOWN | |
9fe6f636 | 825 | && __builtin_expect (lstat (real_file_name, &lstat_buf), 0)) |
b4a555d6 | 826 | { |
27d9ffda | 827 | error (0, errno, _("Cannot lstat %s"), file_name); |
daf7b53a | 828 | goto next; |
b4a555d6 | 829 | } |
27d9ffda UD |
830 | |
831 | /* First search whether the auxiliary cache contains this | |
832 | library already and it's not changed. */ | |
833 | char *soname; | |
efbbd9c3 | 834 | unsigned int isa_level; |
c628c229 | 835 | if (!search_aux_cache (&lstat_buf, &flag, &isa_level, &soname)) |
27d9ffda UD |
836 | { |
837 | if (process_file (real_name, file_name, direntry->d_name, &flag, | |
c628c229 | 838 | &isa_level, &soname, is_link, &lstat_buf)) |
27d9ffda UD |
839 | { |
840 | if (real_name != real_file_name) | |
841 | free (real_name); | |
daf7b53a | 842 | goto next; |
27d9ffda UD |
843 | } |
844 | else if (opt_build_cache) | |
c628c229 | 845 | add_to_aux_cache (&lstat_buf, flag, isa_level, soname); |
27d9ffda | 846 | } |
b4a555d6 | 847 | |
27d9ffda | 848 | if (soname == NULL) |
1b0ea8c5 | 849 | soname = xstrdup (direntry->d_name); |
82d8607d UD |
850 | |
851 | /* A link may just point to itself. */ | |
591e1ffb UD |
852 | if (is_link) |
853 | { | |
97114a38 CD |
854 | /* If the path the link points to isn't its soname or it is not |
855 | the .so symlink for ld(1), we treat it as a normal file. | |
856 | ||
857 | You should always do this: | |
858 | ||
859 | libfoo.so -> SONAME -> Arbitrary package-chosen name. | |
860 | ||
861 | e.g. libfoo.so -> libfoo.so.1 -> libfooimp.so.9.99. | |
862 | Given a SONAME of libfoo.so.1. | |
863 | ||
864 | You should *never* do this: | |
865 | ||
866 | libfoo.so -> libfooimp.so.9.99 | |
867 | ||
868 | If you do, and your SONAME is libfoo.so.1, then libfoo.so | |
869 | fails to point at the SONAME. In that case ldconfig may consider | |
870 | libfoo.so as another implementation of SONAME and will create | |
871 | symlinks against it causing problems when you try to upgrade | |
872 | or downgrade. The problems will arise because ldconfig will, | |
873 | depending on directory ordering, creat symlinks against libfoo.so | |
874 | e.g. libfoo.so.1.2 -> libfoo.so, but when libfoo.so is removed | |
630da022 | 875 | (typically by the removal of a development package not required |
97114a38 CD |
876 | for the runtime) it will break the libfoo.so.1.2 symlink and the |
877 | application will fail to start. */ | |
2fe98a4a | 878 | const char *real_base_name = basename (real_file_name); |
82d8607d UD |
879 | |
880 | if (strcmp (real_base_name, soname) != 0) | |
881 | { | |
882 | len = strlen (real_base_name); | |
883 | if (len < strlen (".so") | |
884 | || strcmp (real_base_name + len - strlen (".so"), ".so") != 0 | |
885 | || strncmp (real_base_name, soname, len) != 0) | |
886 | is_link = 0; | |
887 | } | |
27d9ffda | 888 | } |
45eca4d1 | 889 | |
395412a0 UD |
890 | if (real_name != real_file_name) |
891 | free (real_name); | |
892 | ||
82d8607d UD |
893 | if (is_link) |
894 | { | |
895 | free (soname); | |
896 | soname = xstrdup (direntry->d_name); | |
897 | } | |
898 | ||
591e1ffb UD |
899 | /* Some sanity checks to print warnings. */ |
900 | if (opt_verbose) | |
901 | { | |
8ee87859 | 902 | if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6) |
7b0d235c | 903 | error (0, 0, _("libc6 library %s in wrong directory"), file_name); |
591e1ffb | 904 | } |
45eca4d1 | 905 | |
591e1ffb | 906 | /* Add library to list. */ |
27d9ffda | 907 | struct dlib_entry *dlib_ptr; |
591e1ffb UD |
908 | for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next) |
909 | { | |
910 | /* Is soname already in list? */ | |
911 | if (strcmp (dlib_ptr->soname, soname) == 0) | |
912 | { | |
913 | /* Prefer a file to a link, otherwise check which one | |
914 | is newer. */ | |
915 | if ((!is_link && dlib_ptr->is_link) | |
916 | || (is_link == dlib_ptr->is_link | |
45eca4d1 | 917 | && _dl_cache_libcmp (dlib_ptr->name, direntry->d_name) < 0)) |
591e1ffb UD |
918 | { |
919 | /* It's newer - add it. */ | |
920 | /* Flag should be the same - sanity check. */ | |
921 | if (dlib_ptr->flag != flag) | |
8ee87859 LM |
922 | error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."), |
923 | dlib_ptr->name, direntry->d_name, entry->path); | |
591e1ffb UD |
924 | free (dlib_ptr->name); |
925 | dlib_ptr->name = xstrdup (direntry->d_name); | |
926 | dlib_ptr->is_link = is_link; | |
efbbd9c3 | 927 | dlib_ptr->isa_level = isa_level; |
591e1ffb UD |
928 | } |
929 | /* Don't add this library, abort loop. */ | |
930 | /* Also free soname, since it's dynamically allocated. */ | |
931 | free (soname); | |
932 | break; | |
933 | } | |
934 | } | |
935 | /* Add the library if it's not already in. */ | |
936 | if (dlib_ptr == NULL) | |
937 | { | |
938 | dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry)); | |
939 | dlib_ptr->name = xstrdup (direntry->d_name); | |
591e1ffb | 940 | dlib_ptr->soname = soname; |
27d9ffda | 941 | dlib_ptr->flag = flag; |
591e1ffb | 942 | dlib_ptr->is_link = is_link; |
efbbd9c3 | 943 | dlib_ptr->isa_level = isa_level; |
591e1ffb UD |
944 | /* Add at head of list. */ |
945 | dlib_ptr->next = dlibs; | |
946 | dlibs = dlib_ptr; | |
947 | } | |
daf7b53a SB |
948 | |
949 | next: | |
950 | free (file_name); | |
951 | free (real_file_name); | |
591e1ffb UD |
952 | } |
953 | ||
954 | closedir (dir); | |
955 | ||
956 | /* Now dlibs contains a list of all libs - add those to the cache | |
957 | and created all symbolic links. */ | |
27d9ffda | 958 | struct dlib_entry *dlib_ptr; |
591e1ffb UD |
959 | for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next) |
960 | { | |
b44ac4f4 FW |
961 | /* The cached file name is the soname for non-glibc-hwcaps |
962 | subdirectories (relying on symbolic links; this helps with | |
963 | library updates that change the file name), and the actual | |
964 | file for glibc-hwcaps subdirectories. */ | |
965 | const char *filename; | |
966 | if (entry->hwcaps == NULL) | |
967 | { | |
968 | /* Don't create links to links. */ | |
969 | if (dlib_ptr->is_link == 0) | |
970 | create_links (dir_name, entry->path, dlib_ptr->name, | |
971 | dlib_ptr->soname); | |
972 | filename = dlib_ptr->soname; | |
973 | } | |
974 | else | |
975 | { | |
976 | /* Do not create links in glibc-hwcaps subdirectories, but | |
977 | still log the cache addition. */ | |
978 | if (opt_verbose) | |
979 | printf ("\t%s -> %s\n", dlib_ptr->soname, dlib_ptr->name); | |
980 | filename = dlib_ptr->name; | |
981 | } | |
591e1ffb | 982 | if (opt_build_cache) |
b44ac4f4 | 983 | add_to_cache (entry->path, filename, dlib_ptr->soname, |
4a709411 | 984 | dlib_ptr->flag, dlib_ptr->isa_level, entry->hwcaps); |
591e1ffb UD |
985 | } |
986 | ||
987 | /* Free all resources. */ | |
45eca4d1 | 988 | while (dlibs) |
591e1ffb UD |
989 | { |
990 | dlib_ptr = dlibs; | |
991 | free (dlib_ptr->soname); | |
992 | free (dlib_ptr->name); | |
993 | dlibs = dlibs->next; | |
994 | free (dlib_ptr); | |
995 | } | |
b4a555d6 | 996 | |
22866ed8 | 997 | if (opt_chroot != NULL && dir_name != NULL) |
b4a555d6 | 998 | free (dir_name); |
591e1ffb UD |
999 | } |
1000 | ||
1001 | /* Search through all libraries. */ | |
1002 | static void | |
1003 | search_dirs (void) | |
1004 | { | |
1005 | struct dir_entry *entry; | |
1006 | ||
1007 | for (entry = dir_entries; entry != NULL; entry = entry->next) | |
1008 | search_dir (entry); | |
1009 | ||
1010 | /* Free all allocated memory. */ | |
1011 | while (dir_entries) | |
1012 | { | |
1013 | entry = dir_entries; | |
1014 | dir_entries = dir_entries->next; | |
1015 | free (entry->path); | |
1016 | free (entry); | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | ||
8e115d80 | 1021 | static void parse_conf_include (const char *config_file, unsigned int lineno, |
a334319f | 1022 | bool do_chroot, const char *pattern); |
8e115d80 | 1023 | |
591e1ffb UD |
1024 | /* Parse configuration file. */ |
1025 | static void | |
a334319f | 1026 | parse_conf (const char *filename, bool do_chroot) |
591e1ffb | 1027 | { |
b4a555d6 | 1028 | FILE *file = NULL; |
591e1ffb | 1029 | char *line = NULL; |
b4a555d6 | 1030 | const char *canon; |
591e1ffb | 1031 | size_t len = 0; |
8e115d80 | 1032 | unsigned int lineno; |
45eca4d1 | 1033 | |
8e115d80 | 1034 | if (do_chroot && opt_chroot) |
b4a555d6 UD |
1035 | { |
1036 | canon = chroot_canon (opt_chroot, filename); | |
1037 | if (canon) | |
1038 | file = fopen (canon, "r"); | |
1039 | else | |
1040 | canon = filename; | |
1041 | } | |
1042 | else | |
1043 | { | |
1044 | canon = filename; | |
1045 | file = fopen (filename, "r"); | |
1046 | } | |
591e1ffb UD |
1047 | |
1048 | if (file == NULL) | |
1049 | { | |
b5a1271c FW |
1050 | if (errno != ENOENT) |
1051 | error (0, errno, _("\ | |
1db86e88 | 1052 | Warning: ignoring configuration file that cannot be opened: %s"), |
b5a1271c | 1053 | canon); |
b4a555d6 UD |
1054 | if (canon != filename) |
1055 | free ((char *) canon); | |
591e1ffb UD |
1056 | return; |
1057 | } | |
1058 | ||
c96873d7 UD |
1059 | /* No threads use this stream. */ |
1060 | __fsetlocking (file, FSETLOCKING_BYCALLER); | |
1061 | ||
b4a555d6 UD |
1062 | if (canon != filename) |
1063 | free ((char *) canon); | |
1064 | ||
8e115d80 | 1065 | lineno = 0; |
591e1ffb UD |
1066 | do |
1067 | { | |
1068 | ssize_t n = getline (&line, &len, file); | |
1069 | if (n < 0) | |
1070 | break; | |
1071 | ||
8e115d80 | 1072 | ++lineno; |
591e1ffb UD |
1073 | if (line[n - 1] == '\n') |
1074 | line[n - 1] = '\0'; | |
1075 | ||
1076 | /* Because the file format does not know any form of quoting we | |
1077 | can search forward for the next '#' character and if found | |
1078 | make it terminating the line. */ | |
1079 | *strchrnul (line, '#') = '\0'; | |
1080 | ||
69acbe9f UD |
1081 | /* Remove leading whitespace. NUL is no whitespace character. */ |
1082 | char *cp = line; | |
1083 | while (isspace (*cp)) | |
1084 | ++cp; | |
1085 | ||
591e1ffb | 1086 | /* If the line is blank it is ignored. */ |
69acbe9f | 1087 | if (cp[0] == '\0') |
591e1ffb UD |
1088 | continue; |
1089 | ||
8e115d80 RM |
1090 | if (!strncmp (cp, "include", 7) && isblank (cp[7])) |
1091 | { | |
1092 | char *dir; | |
1093 | cp += 8; | |
1094 | while ((dir = strsep (&cp, " \t")) != NULL) | |
1095 | if (dir[0] != '\0') | |
a334319f | 1096 | parse_conf_include (filename, lineno, do_chroot, dir); |
8e115d80 | 1097 | } |
ab1d521d | 1098 | else if (!strncasecmp (cp, "hwcap", 5) && isblank (cp[5])) |
31563b68 | 1099 | error (0, 0, _("%s:%u: hwcap directive ignored"), filename, lineno); |
8e115d80 | 1100 | else |
b7176cc2 | 1101 | add_dir_1 (cp, filename, lineno); |
69acbe9f UD |
1102 | } |
1103 | while (!feof_unlocked (file)); | |
591e1ffb UD |
1104 | |
1105 | /* Free buffer and close file. */ | |
1106 | free (line); | |
1107 | fclose (file); | |
1108 | } | |
1109 | ||
8e115d80 RM |
1110 | /* Handle one word in an `include' line, a glob pattern of additional |
1111 | config files to read. */ | |
1112 | static void | |
1113 | parse_conf_include (const char *config_file, unsigned int lineno, | |
a334319f | 1114 | bool do_chroot, const char *pattern) |
8e115d80 | 1115 | { |
22866ed8 | 1116 | if (opt_chroot != NULL && pattern[0] != '/') |
8e115d80 RM |
1117 | error (EXIT_FAILURE, 0, |
1118 | _("need absolute file name for configuration file when using -r")); | |
1119 | ||
1120 | char *copy = NULL; | |
1121 | if (pattern[0] != '/' && strchr (config_file, '/') != NULL) | |
1122 | { | |
322861e8 UD |
1123 | if (asprintf (©, "%s/%s", dirname (strdupa (config_file)), |
1124 | pattern) < 0) | |
1125 | error (EXIT_FAILURE, 0, _("memory exhausted")); | |
8e115d80 RM |
1126 | pattern = copy; |
1127 | } | |
1128 | ||
1129 | glob64_t gl; | |
1130 | int result; | |
1131 | if (do_chroot && opt_chroot) | |
1132 | { | |
1133 | char *canon = chroot_canon (opt_chroot, pattern); | |
49bd556d UD |
1134 | if (canon == NULL) |
1135 | return; | |
1136 | result = glob64 (canon, 0, NULL, &gl); | |
8e115d80 RM |
1137 | free (canon); |
1138 | } | |
1139 | else | |
1140 | result = glob64 (pattern, 0, NULL, &gl); | |
1141 | ||
1142 | switch (result) | |
1143 | { | |
1144 | case 0: | |
1145 | for (size_t i = 0; i < gl.gl_pathc; ++i) | |
a334319f | 1146 | parse_conf (gl.gl_pathv[i], false); |
8e115d80 RM |
1147 | globfree64 (&gl); |
1148 | break; | |
1149 | ||
1150 | case GLOB_NOMATCH: | |
1151 | break; | |
1152 | ||
1153 | case GLOB_NOSPACE: | |
1154 | errno = ENOMEM; | |
32db86d5 | 1155 | /* Fall through. */ |
8e115d80 RM |
1156 | case GLOB_ABORTED: |
1157 | if (opt_verbose) | |
1158 | error (0, errno, _("%s:%u: cannot read directory %s"), | |
1159 | config_file, lineno, pattern); | |
1160 | break; | |
1161 | ||
1162 | default: | |
1163 | abort (); | |
1164 | break; | |
1165 | } | |
1166 | ||
72e6cdfa | 1167 | free (copy); |
8e115d80 RM |
1168 | } |
1169 | ||
591e1ffb UD |
1170 | |
1171 | int | |
1172 | main (int argc, char **argv) | |
1173 | { | |
11bf311e UD |
1174 | /* Set locale via LC_ALL. */ |
1175 | setlocale (LC_ALL, ""); | |
1176 | ||
7d38eb38 AJ |
1177 | /* But keep the C collation. That way `include' directives using |
1178 | globbing patterns are processed in a locale-independent order. */ | |
1179 | setlocale (LC_COLLATE, "C"); | |
1180 | ||
11bf311e UD |
1181 | /* Set the text message domain. */ |
1182 | textdomain (_libc_intl_domainname); | |
45eca4d1 | 1183 | |
591e1ffb | 1184 | /* Parse and process arguments. */ |
11bf311e | 1185 | int remaining; |
591e1ffb UD |
1186 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
1187 | ||
11bf8ce1 | 1188 | /* Remaining arguments are additional directories if opt_manual_link |
b85697f6 UD |
1189 | is not set. */ |
1190 | if (remaining != argc && !opt_manual_link) | |
591e1ffb UD |
1191 | { |
1192 | int i; | |
1193 | for (i = remaining; i < argc; ++i) | |
11bf8ce1 UD |
1194 | if (opt_build_cache && argv[i][0] != '/') |
1195 | error (EXIT_FAILURE, 0, | |
1196 | _("relative path `%s' used to build cache"), | |
1197 | argv[i]); | |
1198 | else | |
b7176cc2 | 1199 | add_dir_1 (argv[i], "<cmdline>", 0); |
591e1ffb UD |
1200 | } |
1201 | ||
22866ed8 | 1202 | if (opt_chroot != NULL) |
591e1ffb | 1203 | { |
f0189a54 | 1204 | /* Normalize the path a bit, we might need it for printing later. */ |
32c7acd4 | 1205 | char *endp = strchr (opt_chroot, '\0'); |
b4a555d6 | 1206 | while (endp > opt_chroot && endp[-1] == '/') |
f0189a54 UD |
1207 | --endp; |
1208 | *endp = '\0'; | |
b4a555d6 UD |
1209 | if (endp == opt_chroot) |
1210 | opt_chroot = NULL; | |
f0189a54 | 1211 | |
22866ed8 | 1212 | if (opt_chroot != NULL) |
b4a555d6 UD |
1213 | { |
1214 | /* It is faster to use chroot if we can. */ | |
1215 | if (!chroot (opt_chroot)) | |
1216 | { | |
1217 | if (chdir ("/")) | |
1218 | error (EXIT_FAILURE, errno, _("Can't chdir to /")); | |
1219 | opt_chroot = NULL; | |
1220 | } | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | if (cache_file == NULL) | |
1225 | { | |
1226 | cache_file = alloca (strlen (LD_SO_CACHE) + 1); | |
1227 | strcpy (cache_file, LD_SO_CACHE); | |
591e1ffb UD |
1228 | } |
1229 | ||
b4a555d6 UD |
1230 | if (config_file == NULL) |
1231 | config_file = LD_SO_CONF; | |
1232 | ||
591e1ffb UD |
1233 | if (opt_print_cache) |
1234 | { | |
22866ed8 | 1235 | if (opt_chroot != NULL) |
b4a555d6 UD |
1236 | { |
1237 | char *p = chroot_canon (opt_chroot, cache_file); | |
1238 | if (p == NULL) | |
1239 | error (EXIT_FAILURE, errno, _("Can't open cache file %s\n"), | |
1240 | cache_file); | |
1241 | cache_file = p; | |
1242 | } | |
591e1ffb | 1243 | print_cache (cache_file); |
22866ed8 | 1244 | if (opt_chroot != NULL) |
b4a555d6 | 1245 | free (cache_file); |
591e1ffb UD |
1246 | exit (0); |
1247 | } | |
1248 | ||
22866ed8 | 1249 | if (opt_chroot != NULL) |
b4a555d6 UD |
1250 | { |
1251 | /* Canonicalize the directory name of cache_file, not cache_file, | |
27d9ffda | 1252 | because we'll rename a temporary cache file to it. */ |
b4a555d6 UD |
1253 | char *p = strrchr (cache_file, '/'); |
1254 | char *canon = chroot_canon (opt_chroot, | |
1255 | p ? (*p = '\0', cache_file) : "/"); | |
1256 | ||
1257 | if (canon == NULL) | |
4bc2bcba UD |
1258 | error (EXIT_FAILURE, errno, |
1259 | _("Can't open cache file directory %s\n"), | |
1260 | p ? cache_file : "/"); | |
b4a555d6 UD |
1261 | |
1262 | if (p) | |
1263 | ++p; | |
1264 | else | |
1265 | p = cache_file; | |
1266 | ||
1267 | cache_file = alloca (strlen (canon) + strlen (p) + 2); | |
1268 | sprintf (cache_file, "%s/%s", canon, p); | |
1269 | free (canon); | |
1270 | } | |
1271 | ||
b85697f6 UD |
1272 | if (opt_manual_link) |
1273 | { | |
1274 | /* Link all given libraries manually. */ | |
1275 | int i; | |
1276 | ||
1277 | for (i = remaining; i < argc; ++i) | |
a986484f | 1278 | manual_link (argv[i]); |
b85697f6 UD |
1279 | |
1280 | exit (0); | |
1281 | } | |
45eca4d1 UD |
1282 | |
1283 | ||
591e1ffb UD |
1284 | if (opt_build_cache) |
1285 | init_cache (); | |
1286 | ||
1287 | if (!opt_only_cline) | |
1288 | { | |
a334319f | 1289 | parse_conf (config_file, true); |
334fcf2a | 1290 | |
591e1ffb | 1291 | /* Always add the standard search paths. */ |
bd89c0b5 | 1292 | add_system_dir (SLIBDIR); |
8ca91b36 | 1293 | if (strcmp (SLIBDIR, LIBDIR)) |
bd89c0b5 | 1294 | add_system_dir (LIBDIR); |
591e1ffb | 1295 | } |
45eca4d1 | 1296 | |
3f7dcb2b | 1297 | const char *aux_cache_file = _PATH_LDCONFIG_AUX_CACHE; |
22866ed8 | 1298 | if (opt_chroot != NULL) |
6db52fbb | 1299 | aux_cache_file = chroot_canon (opt_chroot, aux_cache_file); |
4bc2bcba | 1300 | |
6db52fbb | 1301 | if (! opt_ignore_aux_cache && aux_cache_file) |
4bc2bcba | 1302 | load_aux_cache (aux_cache_file); |
27d9ffda UD |
1303 | else |
1304 | init_aux_cache (); | |
1305 | ||
591e1ffb UD |
1306 | search_dirs (); |
1307 | ||
1308 | if (opt_build_cache) | |
27d9ffda UD |
1309 | { |
1310 | save_cache (cache_file); | |
6db52fbb | 1311 | if (aux_cache_file) |
c0dafcf1 | 1312 | save_aux_cache (aux_cache_file); |
27d9ffda | 1313 | } |
591e1ffb UD |
1314 | |
1315 | return 0; | |
1316 | } |