Resident executable support stage 3/4: Cleanup rtld-elf and augment and
[dragonfly.git] / usr.sbin / prebind / prebind.c
1 /*
2  * Copyright (c) 1993 Paul Kranenburg
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Paul Kranenburg.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $DragonFly: src/usr.sbin/prebind/Attic/prebind.c,v 1.1 2003/09/18 21:23:06 dillon Exp $
31  */
32
33 #include <sys/cdefs.h>
34
35 #include <sys/param.h>
36 #include <sys/wait.h>
37
38 #include <machine/elf.h>
39
40 #include <a.out.h>
41 #include <dlfcn.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 static void
49 usage(void)
50 {
51         fprintf(stderr, "usage: prebind program ...\n");
52         exit(1);
53 }
54
55 int
56 main(int argc, char *argv[])
57 {
58         int             rval;
59         int             c;
60
61         while ((c = getopt(argc, argv, "")) != -1) {
62                 switch (c) {
63                 default:
64                         usage();
65                         /*NOTREACHED*/
66                 }
67         }
68         argc -= optind;
69         argv += optind;
70
71         if (argc <= 0) {
72                 usage();
73                 /*NOTREACHED*/
74         }
75
76         /* ld-elf.so magic */
77         setenv("LD_PREBIND", "yes", 1);
78
79         rval = 0;
80         for ( ;  argc > 0;  argc--, argv++) {
81                 int     fd;
82                 union {
83                         struct exec aout;
84                         Elf_Ehdr elf;
85                 } hdr;
86                 int     n;
87                 int     status;
88                 int     file_ok;
89                 int     is_shlib;
90
91                 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
92                         warn("%s", *argv);
93                         rval |= 1;
94                         continue;
95                 }
96                 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
97                         warn("%s: can't read program header", *argv);
98                         (void)close(fd);
99                         rval |= 1;
100                         continue;
101                 }
102
103                 file_ok = 1;
104                 is_shlib = 0;
105                 if ((size_t)n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
106                         /* a.out file */
107                         if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
108 #if 1 /* Compatibility */
109                             || hdr.aout.a_entry < __LDPGSZ
110 #endif
111                                 ) {
112                                 warnx("%s: not a dynamic executable", *argv);
113                                 file_ok = 0;
114                         }
115                 } else if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
116                         Elf_Ehdr ehdr;
117                         Elf_Phdr phdr;
118                         int dynamic = 0, i;
119
120                         if (lseek(fd, 0, SEEK_SET) == -1 ||
121                             read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
122                             lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
123                            ) {
124                                 warnx("%s: can't read program header", *argv);
125                                 file_ok = 0;
126                         } else {
127                                 for (i = 0; i < ehdr.e_phnum; i++) {
128                                         if (read(fd, &phdr, ehdr.e_phentsize)
129                                            != sizeof phdr) {
130                                                 warnx("%s: can't read program header",
131                                                     *argv);
132                                                 file_ok = 0;
133                                                 break;
134                                         }
135                                         if (phdr.p_type == PT_DYNAMIC)
136                                                 dynamic = 1;
137                                 }
138                         }
139                         if (!dynamic) {
140                                 warnx("%s: not a dynamic executable", *argv);
141                                 file_ok = 0;
142                         } else if (hdr.elf.e_type == ET_DYN) {
143                                 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
144                                         is_shlib = 1;
145                                 } else {
146                                         warnx("%s: not a FreeBSD ELF shared "
147                                               "object", *argv);
148                                         file_ok = 0;
149                                 }
150                         }
151                 } else {
152                         warnx("%s: not a dynamic executable", *argv);
153                         file_ok = 0;
154                 }
155                 (void)close(fd);
156                 if (!file_ok) {
157                         rval |= 1;
158                         continue;
159                 }
160
161                 if (is_shlib) {
162                         rval |= 1;
163                         warnx("%s: prebinding not supported on shared libraries.", *argv);
164                         continue;
165                 }
166
167                 fflush(stdout);
168
169                 switch (fork()) {
170                 case -1:
171                         err(1, "fork");
172                         break;
173                 default:
174                         if (wait(&status) <= 0) {
175                                 warn("wait");
176                                 rval |= 1;
177                         } else if (WIFSIGNALED(status)) {
178                                 fprintf(stderr, "%s: signal %d\n",
179                                                 *argv, WTERMSIG(status));
180                                 rval |= 1;
181                         } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
182                                 fprintf(stderr, "%s: exit status %d\n",
183                                                 *argv, WEXITSTATUS(status));
184                                 rval |= 1;
185                         } else {
186                         }
187                         break;
188                 case 0:
189                         execl(*argv, *argv, (char *)NULL);
190                         warn("%s", *argv);
191                         _exit(1);
192                 }
193         }
194
195         return rval;
196 }