2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
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
26 * $DragonFly: src/usr.sbin/resident/resident.c,v 1.1 2004/01/20 18:46:22 dillon Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
34 #include <machine/elf.h>
48 fprintf(stderr, "usage: resident [-d] program ...\n");
53 main(int argc, char *argv[])
60 while ((c = getopt(argc, argv, "dfx:")) != -1) {
69 c = exec_sys_unregister(strtol(optarg, NULL, 0));
71 printf("unregister: %s\n", strerror(errno));
73 printf("unregister: success\n");
90 setenv("LD_RESIDENT_REGISTER_NOW", "yes", 1);
92 setenv("LD_RESIDENT_UNREGISTER_NOW", "yes", 1);
95 for ( ; argc > 0; argc--, argv++) {
109 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
114 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
115 warn("%s: can't read program header", *argv);
123 if ((size_t)n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
125 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
126 #if 1 /* Compatibility */
127 || hdr.aout.a_entry < __LDPGSZ
130 warnx("%s: not a dynamic executable", *argv);
133 } else if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
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
142 warnx("%s: can't read program header", *argv);
145 for (i = 0; i < ehdr.e_phnum; i++) {
146 if (read(fd, &phdr, ehdr.e_phentsize)
148 warnx("%s: can't read program header",
153 if (phdr.p_type == PT_DYNAMIC)
158 warnx("%s: not a dynamic executable", *argv);
160 } else if (hdr.elf.e_type == ET_DYN) {
161 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
164 warnx("%s: not a FreeBSD ELF shared "
170 warnx("%s: not a dynamic executable", *argv);
181 warnx("%s: prebinding not supported on shared libraries.", *argv);
193 if (wait(&status) <= 0) {
196 } else if (WIFSIGNALED(status)) {
197 fprintf(stderr, "%s: signal %d\n",
198 *argv, WTERMSIG(status));
200 } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
201 switch(WEXITSTATUS(status)) {
203 fprintf(stderr, "%s: entry not found\n",
207 fprintf(stderr, "%s: binary already resident\n",
211 fprintf(stderr, "%s: exit status %s\n",
212 *argv, strerror(WEXITSTATUS(status)));
219 execl(*argv, *argv, (char *)NULL);