2 * Copyright (c) 1998 John D. Polstra
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
35 #include <elf-hints.h>
46 #define MAXDIRS 1024 /* Maximum directories in path */
47 #define MAXFILESIZE (16*1024) /* Maximum hints file size */
49 static void add_dir(const char *, const char *, int);
50 static void read_dirs_from_file(const char *, const char *);
51 static void read_elf_hints(const char *, int);
52 static void write_elf_hints(const char *);
54 static const char *dirs[MAXDIRS];
59 add_dir(const char *hintsfile, const char *name, int trusted)
64 /* Do some security checks */
65 if (!trusted && !insecure) {
66 if (stat(name, &stbuf) == -1) {
70 if (stbuf.st_uid != 0) {
71 warnx("%s: ignoring directory not owned by root", name);
74 if ((stbuf.st_mode & S_IWOTH) != 0) {
75 warnx("%s: ignoring world-writable directory", name);
78 if ((stbuf.st_mode & S_IWGRP) != 0) {
79 warnx("%s: ignoring group-writable directory", name);
84 for (i = 0; i < ndirs; i++)
85 if (strcmp(dirs[i], name) == 0)
88 errx(1, "\"%s\": Too many directories in path", hintsfile);
93 list_elf_hints(const char *hintsfile)
98 read_elf_hints(hintsfile, 1);
99 printf("%s:\n", hintsfile);
100 printf("\tsearch directories:");
101 for (i = 0; i < ndirs; i++)
102 printf("%c%s", i == 0 ? ' ' : ':', dirs[i]);
106 for (i = 0; i < ndirs; i++) {
110 if ((dirp = opendir(dirs[i])) == NULL)
112 while ((dp = readdir(dirp)) != NULL) {
118 /* Name can't be shorter than "libx.so.0" */
119 if ((len = strlen(dp->d_name)) < 9 ||
120 strncmp(dp->d_name, "lib", 3) != 0)
122 name = dp->d_name + 3;
123 vers = dp->d_name + len;
124 while (vers > dp->d_name && isdigit(*(vers-1)))
126 if (vers == dp->d_name + len)
128 if (vers < dp->d_name + 4 ||
129 strncmp(vers - 4, ".so.", 4) != 0)
132 /* We have a valid shared library name. */
133 namelen = (vers - 4) - name;
134 printf("\t%d:-l%.*s.%s => %s/%s\n", nlibs,
135 namelen, name, vers, dirs[i], dp->d_name);
143 read_dirs_from_file(const char *hintsfile, const char *listfile)
146 char buf[MAXPATHLEN];
149 if ((fp = fopen(listfile, "r")) == NULL)
150 err(1, "%s", listfile);
153 while (fgets(buf, sizeof buf, fp) != NULL) {
158 /* Skip leading white space. */
161 if (*cp == '#' || *cp == '\0')
164 /* Advance over the directory name. */
165 while (!isspace(*cp) && *cp != '\0')
167 /* Terminate the string and skip trailing white space. */
173 /* Now we had better be at the end of the line. */
175 warnx("%s:%d: trailing characters ignored",
178 if ((sp = strdup(sp)) == NULL)
179 errx(1, "Out of memory");
180 add_dir(hintsfile, sp, 0);
187 read_elf_hints(const char *hintsfile, int must_exist)
192 struct elfhints_hdr *hdr;
197 if ((fd = open(hintsfile, O_RDONLY)) == -1) {
198 if (errno == ENOENT && !must_exist)
200 err(1, "Cannot open \"%s\"", hintsfile);
202 if (fstat(fd, &s) == -1)
203 err(1, "Cannot stat \"%s\"", hintsfile);
204 if (s.st_size > MAXFILESIZE)
205 errx(1, "\"%s\" is unreasonably large", hintsfile);
207 * We use a read-write, private mapping so that we can null-terminate
208 * some strings in it without affecting the underlying file.
210 mapbase = mmap(NULL, s.st_size, PROT_READ|PROT_WRITE,
212 if (mapbase == MAP_FAILED)
213 err(1, "Cannot mmap \"%s\"", hintsfile);
216 hdr = (struct elfhints_hdr *)mapbase;
217 if (hdr->magic != ELFHINTS_MAGIC)
218 errx(1, "\"%s\": invalid file format", hintsfile);
219 if (hdr->version != 1)
220 errx(1, "\"%s\": unrecognized file version (%d)", hintsfile,
223 strtab = (char *)mapbase + hdr->strtab;
224 dirlist = strtab + hdr->dirlist;
226 if (*dirlist != '\0')
227 while ((p = strsep(&dirlist, ":")) != NULL)
228 add_dir(hintsfile, p, 1);
232 update_elf_hints(const char *hintsfile, int argc, char **argv, int merge)
237 read_elf_hints(hintsfile, 0);
238 for (i = 0; i < argc; i++) {
241 if (stat(argv[i], &s) == -1)
242 warn("warning: %s", argv[i]);
243 else if (S_ISREG(s.st_mode))
244 read_dirs_from_file(hintsfile, argv[i]);
246 add_dir(hintsfile, argv[i], 0);
248 write_elf_hints(hintsfile);
252 write_elf_hints(const char *hintsfile)
254 struct elfhints_hdr hdr;
260 if (asprintf(&tempname, "%s.XXXXXX", hintsfile) == -1)
261 errx(1, "Out of memory");
262 if ((fd = mkstemp(tempname)) == -1)
263 err(1, "mkstemp(%s)", tempname);
264 if (fchmod(fd, 0444) == -1)
265 err(1, "fchmod(%s)", tempname);
266 if ((fp = fdopen(fd, "wb")) == NULL)
267 err(1, "fdopen(%s)", tempname);
269 hdr.magic = ELFHINTS_MAGIC;
271 hdr.strtab = sizeof hdr;
274 memset(hdr.spare, 0, sizeof hdr.spare);
276 /* Count up the size of the string table. */
278 hdr.strsize += strlen(dirs[0]);
279 for (i = 1; i < ndirs; i++)
280 hdr.strsize += 1 + strlen(dirs[i]);
282 hdr.dirlistlen = hdr.strsize;
283 hdr.strsize++; /* For the null terminator */
285 /* Write the header. */
286 if (fwrite(&hdr, 1, sizeof hdr, fp) != sizeof hdr)
287 err(1, "%s: write error", tempname);
288 /* Write the strings. */
290 if (fputs(dirs[0], fp) == EOF)
291 err(1, "%s: write error", tempname);
292 for (i = 1; i < ndirs; i++)
293 if (fprintf(fp, ":%s", dirs[i]) < 0)
294 err(1, "%s: write error", tempname);
296 if (putc('\0', fp) == EOF || fclose(fp) == EOF)
297 err(1, "%s: write error", tempname);
299 if (rename(tempname, hintsfile) == -1)
300 err(1, "rename %s to %s", tempname, hintsfile);