2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 John D. Polstra
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
37 #include <elf-hints.h>
48 #define MAXDIRS 1024 /* Maximum directories in path */
49 #define MAXFILESIZE (16*1024) /* Maximum hints file size */
51 static void add_dir(const char *, const char *, int);
52 static void read_dirs_from_file(const char *, const char *);
53 static void read_elf_hints(const char *, int);
54 static void write_elf_hints(const char *);
56 static const char *dirs[MAXDIRS];
61 add_dir(const char *hintsfile, const char *name, int trusted)
66 /* Do some security checks */
67 if (!trusted && !insecure) {
68 if (stat(name, &stbuf) == -1) {
72 if (stbuf.st_uid != 0) {
73 warnx("%s: ignoring directory not owned by root", name);
76 if ((stbuf.st_mode & S_IWOTH) != 0) {
77 warnx("%s: ignoring world-writable directory", name);
80 if ((stbuf.st_mode & S_IWGRP) != 0) {
81 warnx("%s: ignoring group-writable directory", name);
86 for (i = 0; i < ndirs; i++)
87 if (strcmp(dirs[i], name) == 0)
90 errx(1, "\"%s\": Too many directories in path", hintsfile);
95 list_elf_hints(const char *hintsfile)
100 read_elf_hints(hintsfile, 1);
101 printf("%s:\n", hintsfile);
102 printf("\tsearch directories:");
103 for (i = 0; i < ndirs; i++)
104 printf("%c%s", i == 0 ? ' ' : ':', dirs[i]);
108 for (i = 0; i < ndirs; i++) {
112 if ((dirp = opendir(dirs[i])) == NULL)
114 while ((dp = readdir(dirp)) != NULL) {
120 /* Name can't be shorter than "libx.so.0" */
121 if ((len = strlen(dp->d_name)) < 9 ||
122 strncmp(dp->d_name, "lib", 3) != 0)
124 name = dp->d_name + 3;
125 vers = dp->d_name + len;
126 while (vers > dp->d_name && isdigit(*(vers-1)))
128 if (vers == dp->d_name + len)
130 if (vers < dp->d_name + 4 ||
131 strncmp(vers - 4, ".so.", 4) != 0)
134 /* We have a valid shared library name. */
135 namelen = (vers - 4) - name;
136 printf("\t%d:-l%.*s.%s => %s/%s\n", nlibs,
137 namelen, name, vers, dirs[i], dp->d_name);
145 read_dirs_from_file(const char *hintsfile, const char *listfile)
148 char buf[MAXPATHLEN];
151 if ((fp = fopen(listfile, "r")) == NULL)
152 err(1, "%s", listfile);
155 while (fgets(buf, sizeof buf, fp) != NULL) {
160 /* Skip leading white space. */
163 if (*cp == '#' || *cp == '\0')
166 /* Advance over the directory name. */
167 while (!isspace(*cp) && *cp != '\0')
169 /* Terminate the string and skip trailing white space. */
175 /* Now we had better be at the end of the line. */
177 warnx("%s:%d: trailing characters ignored",
180 if ((sp = strdup(sp)) == NULL)
181 errx(1, "Out of memory");
182 add_dir(hintsfile, sp, 0);
189 read_elf_hints(const char *hintsfile, int must_exist)
194 struct elfhints_hdr *hdr;
199 if ((fd = open(hintsfile, O_RDONLY)) == -1) {
200 if (errno == ENOENT && !must_exist)
202 err(1, "Cannot open \"%s\"", hintsfile);
204 if (fstat(fd, &s) == -1)
205 err(1, "Cannot stat \"%s\"", hintsfile);
206 if (s.st_size > MAXFILESIZE)
207 errx(1, "\"%s\" is unreasonably large", hintsfile);
209 * We use a read-write, private mapping so that we can null-terminate
210 * some strings in it without affecting the underlying file.
212 mapbase = mmap(NULL, s.st_size, PROT_READ|PROT_WRITE,
214 if (mapbase == MAP_FAILED)
215 err(1, "Cannot mmap \"%s\"", hintsfile);
218 hdr = (struct elfhints_hdr *)mapbase;
219 if (hdr->magic != ELFHINTS_MAGIC)
220 errx(1, "\"%s\": invalid file format", hintsfile);
221 if (hdr->version != 1)
222 errx(1, "\"%s\": unrecognized file version (%d)", hintsfile,
225 strtab = (char *)mapbase + hdr->strtab;
226 dirlist = strtab + hdr->dirlist;
228 if (*dirlist != '\0')
229 while ((p = strsep(&dirlist, ":")) != NULL)
230 add_dir(hintsfile, p, 1);
234 update_elf_hints(const char *hintsfile, int argc, char **argv, int merge)
239 read_elf_hints(hintsfile, 0);
240 for (i = 0; i < argc; i++) {
243 if (stat(argv[i], &s) == -1)
244 warn("warning: %s", argv[i]);
245 else if (S_ISREG(s.st_mode))
246 read_dirs_from_file(hintsfile, argv[i]);
248 add_dir(hintsfile, argv[i], 0);
250 write_elf_hints(hintsfile);
254 write_elf_hints(const char *hintsfile)
256 struct elfhints_hdr hdr;
262 if (asprintf(&tempname, "%s.XXXXXX", hintsfile) == -1)
263 errx(1, "Out of memory");
264 if ((fd = mkstemp(tempname)) == -1)
265 err(1, "mkstemp(%s)", tempname);
266 if (fchmod(fd, 0444) == -1)
267 err(1, "fchmod(%s)", tempname);
268 if ((fp = fdopen(fd, "wb")) == NULL)
269 err(1, "fdopen(%s)", tempname);
271 hdr.magic = ELFHINTS_MAGIC;
273 hdr.strtab = sizeof hdr;
276 memset(hdr.spare, 0, sizeof hdr.spare);
278 /* Count up the size of the string table. */
280 hdr.strsize += strlen(dirs[0]);
281 for (i = 1; i < ndirs; i++)
282 hdr.strsize += 1 + strlen(dirs[i]);
284 hdr.dirlistlen = hdr.strsize;
285 hdr.strsize++; /* For the null terminator */
287 /* Write the header. */
288 if (fwrite(&hdr, 1, sizeof hdr, fp) != sizeof hdr)
289 err(1, "%s: write error", tempname);
290 /* Write the strings. */
292 if (fputs(dirs[0], fp) == EOF)
293 err(1, "%s: write error", tempname);
294 for (i = 1; i < ndirs; i++)
295 if (fprintf(fp, ":%s", dirs[i]) < 0)
296 err(1, "%s: write error", tempname);
298 if (putc('\0', fp) == EOF || fclose(fp) == EOF)
299 err(1, "%s: write error", tempname);
301 if (rename(tempname, hintsfile) == -1)
302 err(1, "rename %s to %s", tempname, hintsfile);