xref: /freebsd/sbin/ldconfig/ldconfig.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1993,1995 Paul Kranenburg
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Paul Kranenburg.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/mman.h>
37 #include <a.out.h>
38 #include <ctype.h>
39 #include <dirent.h>
40 #include <elf-hints.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdbool.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #include "ldconfig.h"
51 #include "rtld_paths.h"
52 
53 #define	_PATH_LD32_HINTS	"/var/run/ld32.so.hints"
54 #define	_PATH_ELF32_HINTS	"/var/run/ld-elf32.so.hints"
55 #define	_PATH_ELFSOFT_HINTS	"/var/run/ld-elf-soft.so.hints"
56 
57 static void usage(void);
58 
59 int
60 main(int argc, char **argv)
61 {
62 	const char *hints_file;
63 	int c;
64 	bool is_32, is_soft, justread, merge, rescan, verbose;
65 
66 	is_32 = is_soft = justread = merge = rescan = verbose = false;
67 
68 	while (argc > 1) {
69 		if (strcmp(argv[1], "-aout") == 0) {
70 			errx(1, "aout is not supported");
71 		} else if (strcmp(argv[1], "-elf") == 0) {
72 			argc--;
73 			argv++;
74 		} else if (strcmp(argv[1], "-32") == 0) {
75 			is_32 = true;
76 			argc--;
77 			argv++;
78 		} else if (strcmp(argv[1], "-soft") == 0) {
79 			is_soft = true;
80 			argc--;
81 			argv++;
82 		} else {
83 			break;
84 		}
85 	}
86 
87 	if (is_soft)
88 		hints_file = _PATH_SOFT_ELF_HINTS;
89 	else if (is_32)
90 		hints_file = _PATH_ELF32_HINTS;
91 	else
92 		hints_file = _PATH_ELF_HINTS;
93 	while((c = getopt(argc, argv, "Rf:imrsv")) != -1) {
94 		switch (c) {
95 		case 'R':
96 			rescan = true;
97 			break;
98 		case 'f':
99 			hints_file = optarg;
100 			break;
101 		case 'i':
102 			insecure = true;
103 			break;
104 		case 'm':
105 			merge = true;
106 			break;
107 		case 'r':
108 			justread = true;
109 			break;
110 		case 's':
111 			/* was nostd */
112 			break;
113 		case 'v':
114 			verbose = true;
115 			break;
116 		default:
117 			usage();
118 			break;
119 		}
120 	}
121 
122 	if (justread) {
123 		list_elf_hints(hints_file);
124 	} else {
125 		if (argc == optind)
126 			rescan = true;
127 		update_elf_hints(hints_file, argc - optind,
128 		    argv + optind, merge || rescan);
129 	}
130 	exit(0);
131 }
132 
133 static void
134 usage(void)
135 {
136 	fprintf(stderr,
137 	    "usage: ldconfig [-32] [-elf] [-Rimrv] [-f hints_file] "
138 	    "[directory | file ...]\n");
139 	exit(1);
140 }
141