Update.
[glibc.git] / sysdeps / generic / dl-cache.c
blob8eb18b79cc05e9c9531a706f7c87b21cf755e11d
1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <link.h>
21 #include <stddef.h>
22 #include <sys/mman.h>
24 /* System-dependent function to read a file's whole contents
25 in the most convenient manner available. */
26 extern void *_dl_sysdep_read_whole_file (const char *filename,
27 size_t *filesize_ptr,
28 int mmap_prot);
30 #ifndef LD_SO_CACHE
31 # define LD_SO_CACHE "/etc/ld.so.cache"
32 #endif
34 #define CACHEMAGIC "ld.so-1.7.0"
36 struct cache_file
38 char magic[sizeof CACHEMAGIC - 1];
39 unsigned int nlibs;
40 struct
42 int flags; /* This is 1 for an ELF library. */
43 unsigned int key, value; /* String table indices. */
44 } libs[0];
47 /* Look up NAME in ld.so.cache and return the file name stored there,
48 or null if none is found. */
50 const char *
51 _dl_load_cache_lookup (const char *name)
53 static struct cache_file *cache;
54 static size_t cachesize;
55 unsigned int i;
56 const char *best;
58 if (cache == NULL)
60 /* Read the contents of the file. */
61 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
62 PROT_READ);
63 if (file && cachesize > sizeof *cache &&
64 !memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1))
65 /* Looks ok. */
66 cache = file;
67 else
69 if (file)
70 __munmap (file, cachesize);
71 cache = (void *) -1;
72 return NULL;
76 if (cache == (void *) -1)
77 /* Previously looked for the cache file and didn't find it. */
78 return NULL;
80 best = NULL;
81 for (i = 0; i < cache->nlibs; ++i)
82 if ((cache->libs[i].flags == 1 ||
83 cache->libs[i].flags == 3) && /* ELF library entry. */
84 /* Make sure string table indices are not bogus before using them. */
85 cache->libs[i].key < cachesize - sizeof *cache &&
86 cache->libs[i].value < cachesize - sizeof *cache &&
87 /* Does the name match? */
88 ! strcmp (name, ((const char *) &cache->libs[cache->nlibs] +
89 cache->libs[i].key)))
91 if ((best == NULL) || (cache->libs[i].flags == 3))
93 best = ((const char *) &cache->libs[cache->nlibs]
94 + cache->libs[i].value);
96 if (cache->libs[i].flags == 3)
97 /* We've found an exact match for the shared object and no
98 general `ELF' release. Stop searching. */
99 break;
102 return best;