Resident executable support stage 2/4: userland bits. Augment rtld-elf
[dragonfly.git] / usr.sbin / resident / resident.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
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  *
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
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/usr.sbin/resident/resident.c,v 1.1 2004/01/20 18:46:22 dillon Exp $
27  */
28
29 #include <sys/cdefs.h>
30
31 #include <sys/param.h>
32 #include <sys/wait.h>
33
34 #include <machine/elf.h>
35 #include <a.out.h>
36 #include <dlfcn.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <errno.h>
44
45 static void
46 usage(void)
47 {
48         fprintf(stderr, "usage: resident [-d] program ...\n");
49         exit(1);
50 }
51
52 int
53 main(int argc, char *argv[])
54 {
55         int     rval;
56         int     c;
57         int     doreg = 1;
58         int     force = 0;
59
60         while ((c = getopt(argc, argv, "dfx:")) != -1) {
61                 switch (c) {
62                 case 'f':
63                         force = 1;
64                         break;
65                 case 'd':
66                         doreg = 0;
67                         break;
68                 case 'x':
69                         c = exec_sys_unregister(strtol(optarg, NULL, 0));
70                         if (c < 0)
71                             printf("unregister: %s\n", strerror(errno));
72                         else
73                             printf("unregister: success\n");
74                         exit(0);
75                 default:
76                         usage();
77                         /*NOTREACHED*/
78                 }
79         }
80         argc -= optind;
81         argv += optind;
82
83         if (argc <= 0) {
84                 usage();
85                 /*NOTREACHED*/
86         }
87
88         /* ld-elf.so magic */
89         if (doreg)
90             setenv("LD_RESIDENT_REGISTER_NOW", "yes", 1);
91         else
92             setenv("LD_RESIDENT_UNREGISTER_NOW", "yes", 1);
93
94         rval = 0;
95         for ( ;  argc > 0;  argc--, argv++) {
96                 int     fd;
97                 union {
98                         struct exec aout;
99                         Elf_Ehdr elf;
100                 } hdr;
101                 int     n;
102                 int     status;
103                 int     file_ok;
104                 int     is_shlib;
105
106                 if (force)
107                         goto force_target;
108
109                 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
110                         warn("%s", *argv);
111                         rval |= 1;
112                         continue;
113                 }
114                 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
115                         warn("%s: can't read program header", *argv);
116                         (void)close(fd);
117                         rval |= 1;
118                         continue;
119                 }
120
121                 file_ok = 1;
122                 is_shlib = 0;
123                 if ((size_t)n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
124                         /* a.out file */
125                         if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
126 #if 1 /* Compatibility */
127                             || hdr.aout.a_entry < __LDPGSZ
128 #endif
129                                 ) {
130                                 warnx("%s: not a dynamic executable", *argv);
131                                 file_ok = 0;
132                         }
133                 } else if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
134                         Elf_Ehdr ehdr;
135                         Elf_Phdr phdr;
136                         int dynamic = 0, i;
137
138                         if (lseek(fd, 0, SEEK_SET) == -1 ||
139                             read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
140                             lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
141                            ) {
142                                 warnx("%s: can't read program header", *argv);
143                                 file_ok = 0;
144                         } else {
145                                 for (i = 0; i < ehdr.e_phnum; i++) {
146                                         if (read(fd, &phdr, ehdr.e_phentsize)
147                                            != sizeof phdr) {
148                                                 warnx("%s: can't read program header",
149                                                     *argv);
150                                                 file_ok = 0;
151                                                 break;
152                                         }
153                                         if (phdr.p_type == PT_DYNAMIC)
154                                                 dynamic = 1;
155                                 }
156                         }
157                         if (!dynamic) {
158                                 warnx("%s: not a dynamic executable", *argv);
159                                 file_ok = 0;
160                         } else if (hdr.elf.e_type == ET_DYN) {
161                                 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
162                                         is_shlib = 1;
163                                 } else {
164                                         warnx("%s: not a FreeBSD ELF shared "
165                                               "object", *argv);
166                                         file_ok = 0;
167                                 }
168                         }
169                 } else {
170                         warnx("%s: not a dynamic executable", *argv);
171                         file_ok = 0;
172                 }
173                 (void)close(fd);
174                 if (!file_ok) {
175                         rval |= 1;
176                         continue;
177                 }
178
179                 if (is_shlib) {
180                         rval |= 1;
181                         warnx("%s: prebinding not supported on shared libraries.", *argv);
182                         continue;
183                 }
184
185 force_target:
186                 fflush(stdout);
187
188                 switch (fork()) {
189                 case -1:
190                         err(1, "fork");
191                         break;
192                 default:
193                         if (wait(&status) <= 0) {
194                                 warn("wait");
195                                 rval |= 1;
196                         } else if (WIFSIGNALED(status)) {
197                                 fprintf(stderr, "%s: signal %d\n",
198                                                 *argv, WTERMSIG(status));
199                                 rval |= 1;
200                         } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
201                                 switch(WEXITSTATUS(status)) {
202                                 case ENOENT:
203                                     fprintf(stderr, "%s: entry not found\n",
204                                         *argv);
205                                     break;
206                                 case EEXIST:
207                                     fprintf(stderr, "%s: binary already resident\n",
208                                         *argv);
209                                     break;
210                                 default:
211                                     fprintf(stderr, "%s: exit status %s\n",
212                                                 *argv, strerror(WEXITSTATUS(status)));
213                                 }
214                                 rval |= 1;
215                         } else {
216                         }
217                         break;
218                 case 0:
219                         execl(*argv, *argv, (char *)NULL);
220                         warn("%s", *argv);
221                         _exit(1);
222                 }
223         }
224
225         return rval;
226 }