]>
Commit | Line | Data |
---|---|---|
45eca4d1 | 1 | /* Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
591e1ffb UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Andreas Jaeger <aj@suse.de>, 1999. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library 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 GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include <argp.h> | |
21 | #include <dirent.h> | |
22 | #include <error.h> | |
23 | #include <errno.h> | |
24 | #include <libintl.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <sys/fcntl.h> | |
30 | #include <sys/mman.h> | |
31 | #include <sys/stat.h> | |
32 | #include <sys/types.h> | |
33 | ||
34 | #include "ldconfig.h" | |
45eca4d1 | 35 | #include "dl-cache.h" |
591e1ffb | 36 | |
45eca4d1 UD |
37 | /* We don't need this here - silence the compiler. */ |
38 | #define _dl_sysdep_message(string, args...) do {} while (0); | |
39 | ||
40 | #include "dl-procinfo.h" | |
591e1ffb UD |
41 | |
42 | #ifndef LD_SO_CONF | |
43 | # define LD_SO_CONF "/etc/ld.so.conf" | |
44 | #endif | |
45 | ||
46 | /* Get libc version number. */ | |
47 | #include <version.h> | |
48 | ||
49 | #define PACKAGE _libc_intl_domainname | |
50 | ||
51 | struct lib_entry | |
52 | { | |
53 | int flags; | |
45eca4d1 | 54 | unsigned long int hwcap; |
591e1ffb UD |
55 | char *lib; |
56 | char *path; | |
57 | }; | |
58 | ||
59 | static const struct | |
60 | { | |
61 | const char *name; | |
62 | int flag; | |
63 | } lib_types [] = | |
64 | { | |
65 | {"libc4", FLAG_LIBC4}, | |
66 | {"libc5", FLAG_ELF_LIBC5}, | |
67 | {"libc6", FLAG_ELF_LIBC6}, | |
68 | {"glibc2", FLAG_ELF_LIBC6} | |
45eca4d1 | 69 | }; |
591e1ffb UD |
70 | |
71 | ||
72 | /* List of directories to handle. */ | |
73 | struct dir_entry | |
74 | { | |
75 | char *path; | |
76 | int flag; | |
77 | struct dir_entry *next; | |
78 | }; | |
79 | ||
80 | /* The list is unsorted, contains no duplicates. Entries are added at | |
81 | the end. */ | |
82 | static struct dir_entry *dir_entries; | |
83 | ||
84 | /* Flags for different options. */ | |
85 | /* Print Cache. */ | |
86 | static int opt_print_cache = 0; | |
87 | ||
88 | /* Be verbose. */ | |
89 | int opt_verbose = 0; | |
90 | ||
45eca4d1 UD |
91 | /* Format to support. */ |
92 | /* 0: only libc5/glibc2; 1: both; 2: only glibc 2.2. */ | |
93 | int opt_format = 1; | |
94 | ||
591e1ffb UD |
95 | /* Build cache. */ |
96 | static int opt_build_cache = 1; | |
97 | ||
98 | /* Generate links. */ | |
99 | static int opt_link = 1; | |
100 | ||
101 | /* Only process directories specified on the command line. */ | |
102 | static int opt_only_cline = 0; | |
103 | ||
104 | /* Path to root for chroot. */ | |
105 | static char *opt_chroot; | |
106 | ||
b85697f6 UD |
107 | /* Manually link given shared libraries. */ |
108 | static int opt_manual_link = 0; | |
109 | ||
591e1ffb UD |
110 | /* Cache file to use. */ |
111 | static const char *cache_file; | |
112 | ||
113 | /* Configuration file. */ | |
114 | static const char *config_file; | |
115 | ||
116 | /* Name and version of program. */ | |
117 | static void print_version (FILE *stream, struct argp_state *state); | |
118 | void (*argp_program_version_hook) (FILE *, struct argp_state *) | |
119 | = print_version; | |
120 | ||
121 | /* Definitions of arguments for argp functions. */ | |
122 | static const struct argp_option options[] = | |
123 | { | |
124 | { "print-cache", 'p', NULL, 0, N_("Print cache"), 0}, | |
125 | { "verbose", 'v', NULL, 0, N_("Generate verbose messages"), 0}, | |
126 | { NULL, 'N', NULL, 0, N_("Don't build cache"), 0}, | |
127 | { NULL, 'X', NULL, 0, N_("Don't generate links"), 0}, | |
128 | { NULL, 'r', "ROOT", 0, N_("Change to and use ROOT as root directory"), 0}, | |
129 | { NULL, 'C', "CACHE", 0, N_("Use CACHE as cache file"), 0}, | |
130 | { NULL, 'f', "CONF", 0, N_("Use CONF as configuration file"), 0}, | |
131 | { NULL, 'n', NULL, 0, N_("Only process directories specified on the command line. Don't build cache."), 0}, | |
b85697f6 | 132 | { NULL, 'l', NULL, 0, N_("Manually link individual libraries."), 0}, |
45eca4d1 | 133 | { "format", 'c', "FORMAT", 0, N_("Format to use: new, old or compat (default)"), 0}, |
591e1ffb UD |
134 | { NULL, 0, NULL, 0, NULL, 0 } |
135 | }; | |
136 | ||
137 | /* Short description of program. */ | |
138 | static const char doc[] = N_("Configure Dynamic Linker Run Time Bindings."); | |
139 | ||
140 | /* Prototype for option handler. */ | |
141 | static error_t parse_opt (int key, char *arg, struct argp_state *state); | |
142 | ||
143 | /* Data structure to communicate with argp functions. */ | |
144 | static struct argp argp = | |
145 | { | |
146 | options, parse_opt, NULL, doc, NULL, NULL, NULL | |
147 | }; | |
148 | ||
45eca4d1 UD |
149 | /* Check if string corresponds to an important hardware capability. */ |
150 | static int | |
151 | is_hwcap (const char *name) | |
152 | { | |
153 | int hwcap_idx = _dl_string_hwcap (name); | |
154 | ||
155 | if (hwcap_idx != -1 && ((1 << hwcap_idx) & HWCAP_IMPORTANT)) | |
156 | return 1; | |
157 | return 0; | |
158 | } | |
159 | ||
160 | /* Get hwcap encoding of path. */ | |
161 | static unsigned long int | |
162 | path_hwcap (const char *path) | |
163 | { | |
164 | char *str = xstrdup (path); | |
165 | char *ptr; | |
166 | unsigned long int hwcap = 0; | |
167 | unsigned long int h; | |
168 | ||
169 | size_t len; | |
170 | ||
171 | len = strlen (str); | |
172 | if (str[len] == '/') | |
173 | str[len] = '\0'; | |
174 | ||
175 | /* Search pathname from the end and check for hwcap strings. */ | |
176 | for (;;) | |
177 | { | |
178 | ptr = strrchr (str, '/'); | |
179 | ||
180 | if (ptr == NULL) | |
181 | break; | |
182 | ||
183 | h = _dl_string_hwcap (ptr+1); | |
184 | ||
185 | if (h == -1) | |
186 | break; | |
187 | hwcap += 1 << h; | |
591e1ffb | 188 | |
45eca4d1 UD |
189 | /* Search the next part of the path. */ |
190 | *ptr = '\0'; | |
191 | } | |
192 | ||
193 | free (str); | |
194 | return hwcap; | |
195 | } | |
591e1ffb UD |
196 | |
197 | /* Handle program arguments. */ | |
198 | static error_t | |
199 | parse_opt (int key, char *arg, struct argp_state *state) | |
200 | { | |
201 | switch (key) | |
202 | { | |
203 | case 'C': | |
204 | cache_file = arg; | |
205 | break; | |
206 | case 'f': | |
207 | config_file = arg; | |
208 | break; | |
b85697f6 UD |
209 | case 'l': |
210 | opt_manual_link = 1; | |
211 | break; | |
591e1ffb UD |
212 | case 'N': |
213 | opt_build_cache = 0; | |
214 | break; | |
215 | case 'n': | |
216 | opt_build_cache = 0; | |
217 | opt_only_cline = 1; | |
218 | break; | |
219 | case 'p': | |
220 | opt_print_cache = 1; | |
221 | break; | |
222 | case 'r': | |
223 | opt_chroot = arg; | |
224 | break; | |
225 | case 'v': | |
226 | opt_verbose = 1; | |
227 | break; | |
228 | case 'X': | |
229 | opt_link = 0; | |
230 | break; | |
45eca4d1 UD |
231 | case 'c': |
232 | if (strcmp (arg, "old") == 0) | |
233 | opt_format = 0; | |
234 | else if (strcmp (arg, "compat") == 0) | |
235 | opt_format = 1; | |
236 | else if (strcmp (arg, "new") == 0) | |
237 | opt_format = 2; | |
238 | break; | |
591e1ffb UD |
239 | default: |
240 | return ARGP_ERR_UNKNOWN; | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | /* Print the version information. */ | |
247 | static void | |
248 | print_version (FILE *stream, struct argp_state *state) | |
249 | { | |
250 | fprintf (stream, "ldconfig (GNU %s) %s\n", PACKAGE, VERSION); | |
251 | fprintf (stream, gettext ("\ | |
252 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
253 | This is free software; see the source for copying conditions. There is NO\n\ | |
254 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
45eca4d1 | 255 | "), "2000"); |
591e1ffb UD |
256 | fprintf (stream, gettext ("Written by %s.\n"), |
257 | "Andreas Jaeger"); | |
258 | } | |
259 | ||
260 | /* Add one directory to the list of directories to process. */ | |
261 | static void | |
262 | add_dir (const char *line) | |
263 | { | |
264 | char *equal_sign; | |
265 | struct dir_entry *entry, *ptr, *prev; | |
266 | unsigned int i; | |
45eca4d1 | 267 | |
591e1ffb UD |
268 | entry = xmalloc (sizeof (struct dir_entry)); |
269 | entry->next = NULL; | |
45eca4d1 | 270 | |
591e1ffb UD |
271 | /* Search for an '=' sign. */ |
272 | entry->path = xstrdup (line); | |
273 | equal_sign = strchr (entry->path, '='); | |
274 | if (equal_sign) | |
275 | { | |
276 | *equal_sign = '\0'; | |
277 | ++equal_sign; | |
278 | entry->flag = FLAG_ANY; | |
279 | for (i = 0; i < sizeof (lib_types) / sizeof (lib_types [0]); ++i) | |
280 | if (strcmp (equal_sign, lib_types[i].name) == 0) | |
281 | { | |
282 | entry->flag = lib_types[i].flag; | |
283 | break; | |
284 | } | |
285 | if (entry->flag == FLAG_ANY) | |
286 | error (0, 0, _("%s is not a known library type"), equal_sign); | |
287 | } | |
288 | else | |
289 | { | |
290 | entry->flag = FLAG_ANY; | |
291 | } | |
292 | ||
293 | /* Canonify path: for now only remove trailing slashes. */ | |
294 | i = strlen (entry->path) - 1; | |
295 | while (entry->path[i] == '/' && i > 0) | |
296 | { | |
297 | entry->path [i] = '\0'; | |
298 | --i; | |
299 | } | |
45eca4d1 | 300 | |
591e1ffb UD |
301 | ptr = dir_entries; |
302 | prev = ptr; | |
303 | while (ptr != NULL) | |
304 | { | |
305 | /* Check for duplicates. */ | |
306 | if (strcmp (ptr->path, entry->path) == 0) | |
307 | { | |
308 | if (opt_verbose) | |
309 | error (0, 0, _("Path `%s' given more than once"), entry->path); | |
310 | /* Use the newer information. */ | |
311 | ptr->flag = entry->flag; | |
312 | free (entry); | |
313 | break; | |
314 | } | |
315 | prev = ptr; | |
316 | ptr = ptr->next; | |
317 | } | |
318 | /* Is this the first entry? */ | |
319 | if (ptr == NULL && dir_entries == NULL) | |
320 | dir_entries = entry; | |
321 | else if (ptr == NULL) | |
322 | prev->next = entry; | |
323 | } | |
324 | ||
325 | ||
326 | /* Create a symbolic link from soname to libname in directory path. */ | |
327 | static void | |
328 | create_links (const char *path, const char *libname, const char *soname) | |
329 | { | |
330 | char full_libname [PATH_MAX], full_soname [PATH_MAX]; | |
331 | struct stat stat_lib, stat_so, lstat_so; | |
332 | int do_link = 1; | |
333 | int do_remove = 1; | |
334 | /* XXX: The logics in this function should be simplified. */ | |
45eca4d1 | 335 | |
591e1ffb UD |
336 | /* Get complete path. */ |
337 | snprintf (full_libname, sizeof full_libname, "%s/%s", path, libname); | |
338 | snprintf (full_soname, sizeof full_soname, "%s/%s", path, soname); | |
339 | ||
340 | /* Does soname already exist and point to the right library? */ | |
341 | if (stat (full_soname, &stat_so) == 0) | |
342 | { | |
343 | if (stat (full_libname, &stat_lib)) | |
344 | { | |
345 | error (0, 0, _("Can't stat %s\n"), full_libname); | |
346 | return; | |
347 | } | |
348 | if (stat_lib.st_dev == stat_so.st_dev | |
349 | && stat_lib.st_ino == stat_so.st_ino) | |
350 | /* Link is already correct. */ | |
351 | do_link = 0; | |
352 | else if (lstat (full_soname, &lstat_so) == 0 | |
353 | && !S_ISLNK (lstat_so.st_mode)) | |
354 | { | |
355 | error (0, 0, _("%s is not a symbolic link\n"), full_soname); | |
356 | do_link = 0; | |
357 | do_remove = 0; | |
358 | } | |
359 | } | |
360 | else if (lstat (full_soname, &lstat_so) != 0 | |
361 | || !S_ISLNK (lstat_so.st_mode)) | |
362 | /* Unless it is a stale symlink, there is no need to remove. */ | |
363 | do_remove = 0; | |
364 | ||
365 | if (opt_verbose) | |
366 | printf ("\t%s -> %s", soname, libname); | |
367 | ||
368 | if (do_link && opt_link) | |
369 | { | |
370 | /* Remove old link. */ | |
371 | if (do_remove) | |
372 | if (unlink (full_soname)) | |
373 | { | |
374 | error (0, 0, _("Can't unlink %s"), full_soname); | |
375 | do_link = 0; | |
376 | } | |
377 | /* Create symbolic link. */ | |
378 | if (do_link && symlink (libname, full_soname)) | |
379 | { | |
380 | error (0, 0, _("Can't link %s to %s"), full_soname, libname); | |
381 | do_link = 0; | |
382 | } | |
383 | if (opt_verbose) | |
384 | { | |
385 | if (do_link) | |
386 | fputs (_(" (changed)\n"), stdout); | |
387 | else | |
388 | fputs (_(" (SKIPPED)\n"), stdout); | |
389 | } | |
390 | } | |
391 | else if (opt_verbose) | |
392 | fputs ("\n", stdout); | |
393 | } | |
394 | ||
b85697f6 UD |
395 | /* Manually link the given library. */ |
396 | static void | |
397 | manual_link (char *library) | |
398 | { | |
399 | char *path; | |
400 | char *libname; | |
401 | char *soname; | |
402 | struct stat stat_buf; | |
403 | int flag; | |
404 | ||
405 | /* Prepare arguments for create_links call. Split library name in | |
406 | directory and filename first. Since path is allocated, we've got | |
407 | to be careful to free at the end. */ | |
408 | path = xstrdup (library); | |
409 | libname = strrchr (path, '/'); | |
410 | ||
411 | if (libname) | |
412 | { | |
413 | /* Successfully split names. Check if path is just "/" to avoid | |
414 | an empty path. */ | |
415 | if (libname == path) | |
416 | { | |
417 | libname = library + 1; | |
418 | path = xrealloc (path, 2); | |
419 | strcpy (path, "/"); | |
420 | } | |
421 | else | |
422 | { | |
423 | *libname = '\0'; | |
424 | ++libname; | |
425 | } | |
426 | } | |
427 | else | |
428 | { | |
429 | /* There's no path, construct one. */ | |
430 | libname = library; | |
431 | path = xrealloc (path, 2); | |
432 | strcpy (path, "."); | |
433 | } | |
434 | ||
435 | /* Do some sanity checks first. */ | |
436 | if (lstat (library, &stat_buf)) | |
437 | { | |
438 | error (0, errno, _("Can't lstat %s"), library); | |
439 | free (path); | |
440 | return; | |
441 | } | |
442 | /* We don't want links here! */ | |
443 | else if (!S_ISREG (stat_buf.st_mode)) | |
444 | { | |
445 | error (0, 0, _("Ignored file %s since it is not a regular file."), | |
446 | library); | |
447 | free (path); | |
448 | return; | |
449 | } | |
450 | libname = basename (library); | |
451 | if (process_file (library, libname, &flag, &soname, 0)) | |
452 | { | |
453 | error (0, 0, _("No link created since soname could not be found for %s"), | |
454 | library); | |
455 | free (path); | |
456 | return; | |
457 | } | |
458 | create_links (path, libname, soname); | |
459 | free (soname); | |
460 | free (path); | |
461 | } | |
462 | ||
463 | ||
591e1ffb UD |
464 | /* Read a whole directory and search for libraries. |
465 | The purpose is two-fold: | |
466 | - search for libraries which will be added to the cache | |
467 | - create symbolic links to the soname for each library | |
468 | ||
469 | This has to be done separatly for each directory. | |
45eca4d1 | 470 | |
591e1ffb UD |
471 | To keep track of which libraries to add to the cache and which |
472 | links to create, we save a list of all libraries. | |
473 | ||
474 | The algorithm is basically: | |
475 | for all libraries in the directory do | |
476 | get soname of library | |
477 | if soname is already in list | |
478 | if new library is newer, replace entry | |
479 | otherwise ignore this library | |
480 | otherwise add library to list | |
45eca4d1 | 481 | |
591e1ffb UD |
482 | For example, if the two libraries libxy.so.1.1 and libxy.so.1.2 |
483 | exist and both have the same soname, e.g. libxy.so, a symbolic link | |
484 | is created from libxy.so.1.2 (the newer one) to libxy.so. | |
485 | libxy.so.1.2 and libxy.so are added to the cache - but not | |
486 | libxy.so.1.1. */ | |
487 | ||
488 | /* Information for one library. */ | |
45eca4d1 | 489 | struct dlib_entry |
591e1ffb UD |
490 | { |
491 | char *name; | |
492 | char *soname; | |
493 | int flag; | |
494 | int is_link; | |
495 | struct dlib_entry *next; | |
496 | }; | |
497 | ||
498 | ||
499 | static void | |
500 | search_dir (const struct dir_entry *entry) | |
501 | { | |
502 | DIR *dir; | |
503 | struct dirent *direntry; | |
504 | char buf [PATH_MAX]; | |
505 | char *soname; | |
506 | struct dlib_entry *dlibs; | |
507 | struct dlib_entry *dlib_ptr; | |
508 | int nchars; | |
509 | struct stat stat_buf; | |
510 | int is_link; | |
45eca4d1 UD |
511 | unsigned long int hwcap = path_hwcap (entry->path); |
512 | ||
591e1ffb UD |
513 | dlibs = NULL; |
514 | ||
515 | if (opt_verbose) | |
45eca4d1 UD |
516 | { |
517 | if (hwcap != 0) | |
518 | printf ("%s: (hwcap: 0x%lx)\n", entry->path, hwcap); | |
519 | else | |
520 | printf ("%s:\n", entry->path); | |
521 | } | |
522 | ||
591e1ffb UD |
523 | dir = opendir (entry->path); |
524 | if (dir == NULL) | |
525 | { | |
526 | if (opt_verbose) | |
527 | error (0, errno, _("Can't open directory %s"), entry->path); | |
528 | return; | |
529 | } | |
45eca4d1 | 530 | |
591e1ffb UD |
531 | |
532 | while ((direntry = readdir (dir)) != NULL) | |
533 | { | |
534 | int flag; | |
535 | #ifdef _DIRENT_HAVE_D_TYPE | |
536 | /* We only look at links and regular files. */ | |
537 | if (direntry->d_type != DT_UNKNOWN | |
538 | && direntry->d_type != DT_LNK | |
45eca4d1 UD |
539 | && direntry->d_type != DT_REG |
540 | && direntry->d_type != DT_DIR) | |
591e1ffb UD |
541 | continue; |
542 | #endif /* _DIRENT_HAVE_D_TYPE */ | |
45eca4d1 UD |
543 | /* Does this file look like a shared library or is it a hwcap |
544 | subdirectory? The dynamic linker is also considered as | |
545 | shared library. */ | |
546 | if (((strncmp (direntry->d_name, "lib", 3) != 0 | |
547 | && strncmp (direntry->d_name, "ld-", 3) != 0) | |
548 | || strstr (direntry->d_name, ".so") == NULL) | |
549 | && !is_hwcap (direntry->d_name)) | |
591e1ffb UD |
550 | continue; |
551 | nchars = snprintf (buf, sizeof (buf), "%s/%s", entry->path, | |
552 | direntry->d_name); | |
553 | /* Check for overflow. */ | |
554 | if (nchars >= (int) sizeof (buf)) | |
555 | { | |
556 | error (0, 0, _("buffer for snprintf too small for %s/%s--file is ignored\n"), | |
557 | entry->path, direntry->d_name); | |
558 | continue; | |
559 | } | |
560 | if (lstat (buf, &stat_buf)) | |
561 | { | |
562 | error (0, errno, _("Can't lstat %s"), buf); | |
563 | continue; | |
564 | } | |
45eca4d1 UD |
565 | else if (S_ISDIR (stat_buf.st_mode) && is_hwcap (direntry->d_name)) |
566 | { | |
567 | /* Handle subdirectory also, make a recursive call. */ | |
568 | struct dir_entry new_entry; | |
569 | new_entry.path = buf; | |
570 | new_entry.flag = entry->flag; | |
571 | new_entry.next = NULL; | |
572 | search_dir (&new_entry); | |
573 | continue; | |
574 | } | |
591e1ffb UD |
575 | else if (!S_ISREG (stat_buf.st_mode) && !S_ISLNK (stat_buf.st_mode)) |
576 | continue; | |
577 | ||
578 | is_link = S_ISLNK (stat_buf.st_mode); | |
579 | ||
580 | if (process_file (buf, direntry->d_name, &flag, &soname, is_link)) | |
581 | continue; | |
582 | ||
583 | /* Links will just point to itself. */ | |
584 | if (is_link) | |
585 | { | |
586 | free (soname); | |
587 | soname = xstrdup (direntry->d_name); | |
588 | } | |
45eca4d1 | 589 | |
591e1ffb UD |
590 | if (flag == FLAG_ELF |
591 | && (entry->flag == FLAG_ELF_LIBC5 | |
592 | || entry->flag == FLAG_ELF_LIBC6)) | |
593 | flag = entry->flag; | |
594 | /* Some sanity checks to print warnings. */ | |
595 | if (opt_verbose) | |
596 | { | |
597 | if (flag == FLAG_ELF_LIBC5 && entry->flag != FLAG_ELF_LIBC5 | |
598 | && entry->flag != FLAG_ANY) | |
599 | error (0, 0, _("libc5 library %s in wrong directory"), buf); | |
600 | if (flag == FLAG_ELF_LIBC6 && entry->flag != FLAG_ELF_LIBC6 | |
601 | && entry->flag != FLAG_ANY) | |
602 | error (0, 0, _("libc6 library %s in wrong directory"), buf); | |
603 | if (flag == FLAG_LIBC4 && entry->flag != FLAG_LIBC4 | |
604 | && entry->flag != FLAG_ANY) | |
605 | error (0, 0, _("libc4 library %s in wrong directory"), buf); | |
606 | } | |
45eca4d1 | 607 | |
591e1ffb UD |
608 | /* Add library to list. */ |
609 | for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next) | |
610 | { | |
611 | /* Is soname already in list? */ | |
612 | if (strcmp (dlib_ptr->soname, soname) == 0) | |
613 | { | |
614 | /* Prefer a file to a link, otherwise check which one | |
615 | is newer. */ | |
616 | if ((!is_link && dlib_ptr->is_link) | |
617 | || (is_link == dlib_ptr->is_link | |
45eca4d1 | 618 | && _dl_cache_libcmp (dlib_ptr->name, direntry->d_name) < 0)) |
591e1ffb UD |
619 | { |
620 | /* It's newer - add it. */ | |
621 | /* Flag should be the same - sanity check. */ | |
622 | if (dlib_ptr->flag != flag) | |
623 | { | |
624 | if (dlib_ptr->flag == FLAG_ELF | |
625 | && (flag == FLAG_ELF_LIBC5 || flag == FLAG_ELF_LIBC6)) | |
626 | dlib_ptr->flag = flag; | |
627 | else if ((dlib_ptr->flag == FLAG_ELF_LIBC5 | |
628 | || dlib_ptr->flag == FLAG_ELF_LIBC6) | |
629 | && flag == FLAG_ELF) | |
630 | dlib_ptr->flag = flag; | |
631 | else | |
632 | error (0, 0, _("libraries %s and %s in directory %s have same soname but different type."), | |
633 | dlib_ptr->name, direntry->d_name, entry->path); | |
634 | } | |
635 | free (dlib_ptr->name); | |
636 | dlib_ptr->name = xstrdup (direntry->d_name); | |
637 | dlib_ptr->is_link = is_link; | |
638 | } | |
639 | /* Don't add this library, abort loop. */ | |
640 | /* Also free soname, since it's dynamically allocated. */ | |
641 | free (soname); | |
642 | break; | |
643 | } | |
644 | } | |
645 | /* Add the library if it's not already in. */ | |
646 | if (dlib_ptr == NULL) | |
647 | { | |
648 | dlib_ptr = (struct dlib_entry *)xmalloc (sizeof (struct dlib_entry)); | |
649 | dlib_ptr->name = xstrdup (direntry->d_name); | |
650 | dlib_ptr->flag = flag; | |
651 | dlib_ptr->soname = soname; | |
652 | dlib_ptr->is_link = is_link; | |
653 | /* Add at head of list. */ | |
654 | dlib_ptr->next = dlibs; | |
655 | dlibs = dlib_ptr; | |
656 | } | |
657 | } | |
658 | ||
659 | closedir (dir); | |
660 | ||
661 | /* Now dlibs contains a list of all libs - add those to the cache | |
662 | and created all symbolic links. */ | |
663 | for (dlib_ptr = dlibs; dlib_ptr != NULL; dlib_ptr = dlib_ptr->next) | |
664 | { | |
665 | /* Don't create links to links. */ | |
666 | if (dlib_ptr->is_link == 0) | |
667 | create_links (entry->path, dlib_ptr->name, dlib_ptr->soname); | |
668 | if (opt_build_cache) | |
45eca4d1 | 669 | add_to_cache (entry->path, dlib_ptr->soname, dlib_ptr->flag, hwcap); |
591e1ffb UD |
670 | } |
671 | ||
672 | /* Free all resources. */ | |
45eca4d1 | 673 | while (dlibs) |
591e1ffb UD |
674 | { |
675 | dlib_ptr = dlibs; | |
676 | free (dlib_ptr->soname); | |
677 | free (dlib_ptr->name); | |
678 | dlibs = dlibs->next; | |
679 | free (dlib_ptr); | |
680 | } | |
681 | } | |
682 | ||
683 | /* Search through all libraries. */ | |
684 | static void | |
685 | search_dirs (void) | |
686 | { | |
687 | struct dir_entry *entry; | |
688 | ||
689 | for (entry = dir_entries; entry != NULL; entry = entry->next) | |
690 | search_dir (entry); | |
691 | ||
692 | /* Free all allocated memory. */ | |
693 | while (dir_entries) | |
694 | { | |
695 | entry = dir_entries; | |
696 | dir_entries = dir_entries->next; | |
697 | free (entry->path); | |
698 | free (entry); | |
699 | } | |
700 | } | |
701 | ||
702 | ||
703 | /* Parse configuration file. */ | |
704 | static void | |
705 | parse_conf (const char *filename) | |
706 | { | |
707 | FILE *file; | |
708 | char *line = NULL; | |
709 | size_t len = 0; | |
45eca4d1 | 710 | |
591e1ffb UD |
711 | file = fopen (filename, "r"); |
712 | ||
713 | if (file == NULL) | |
714 | { | |
715 | error (0, errno, _("Can't open configuration file %s"), filename); | |
716 | return; | |
717 | } | |
718 | ||
719 | do | |
720 | { | |
721 | ssize_t n = getline (&line, &len, file); | |
722 | if (n < 0) | |
723 | break; | |
724 | ||
725 | if (line[n - 1] == '\n') | |
726 | line[n - 1] = '\0'; | |
727 | ||
728 | /* Because the file format does not know any form of quoting we | |
729 | can search forward for the next '#' character and if found | |
730 | make it terminating the line. */ | |
731 | *strchrnul (line, '#') = '\0'; | |
732 | ||
733 | /* If the line is blank it is ignored. */ | |
734 | if (line[0] == '\0') | |
735 | continue; | |
736 | ||
737 | add_dir (line); | |
738 | } while (!feof (file)); | |
739 | ||
740 | /* Free buffer and close file. */ | |
741 | free (line); | |
742 | fclose (file); | |
743 | } | |
744 | ||
745 | ||
746 | int | |
747 | main (int argc, char **argv) | |
748 | { | |
749 | int remaining; | |
45eca4d1 | 750 | |
591e1ffb UD |
751 | /* Parse and process arguments. */ |
752 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); | |
753 | ||
b85697f6 UD |
754 | /* Remaining arguments are additional libraries if opt_manual_link |
755 | is not set. */ | |
756 | if (remaining != argc && !opt_manual_link) | |
591e1ffb UD |
757 | { |
758 | int i; | |
759 | for (i = remaining; i < argc; ++i) | |
760 | add_dir (argv [i]); | |
761 | } | |
762 | ||
763 | if (cache_file == NULL) | |
764 | cache_file = LD_SO_CACHE; | |
765 | ||
766 | if (config_file == NULL) | |
767 | config_file = LD_SO_CONF; | |
45eca4d1 | 768 | |
591e1ffb UD |
769 | /* Chroot first. */ |
770 | if (opt_chroot) | |
771 | { | |
772 | if (chroot (opt_chroot)) | |
773 | /* Report failure and exit program. */ | |
774 | error (EXIT_FAILURE, errno, _("Can't chroot to %s"), opt_chroot); | |
775 | /* chroot doesn't change the working directory, let's play safe. */ | |
776 | if (chdir ("/")) | |
777 | error (EXIT_FAILURE, errno, _("Can't chdir to /")); | |
778 | } | |
779 | ||
780 | if (opt_print_cache) | |
781 | { | |
782 | print_cache (cache_file); | |
783 | exit (0); | |
784 | } | |
785 | ||
b85697f6 UD |
786 | if (opt_manual_link) |
787 | { | |
788 | /* Link all given libraries manually. */ | |
789 | int i; | |
790 | ||
791 | for (i = remaining; i < argc; ++i) | |
792 | manual_link (argv [i]); | |
793 | ||
794 | exit (0); | |
795 | } | |
45eca4d1 UD |
796 | |
797 | ||
591e1ffb UD |
798 | if (opt_build_cache) |
799 | init_cache (); | |
800 | ||
801 | if (!opt_only_cline) | |
802 | { | |
803 | /* Always add the standard search paths. */ | |
804 | add_dir ("/lib"); | |
805 | add_dir ("/usr/lib"); | |
806 | ||
807 | parse_conf (config_file); | |
808 | } | |
45eca4d1 | 809 | |
591e1ffb UD |
810 | search_dirs (); |
811 | ||
812 | if (opt_build_cache) | |
813 | save_cache (cache_file); | |
814 | ||
815 | return 0; | |
816 | } |