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.3 2004/01/20 21:32:43 dillon Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
33 #include <sys/resident.h>
35 #include <machine/elf.h>
49 fprintf(stderr, "usage: resident [-d] program ...\n");
54 main(int argc, char *argv[])
61 while ((c = getopt(argc, argv, "dfx:")) != -1) {
72 c = exec_sys_unregister(-2);
74 c = exec_sys_unregister(strtol(optarg, NULL, 0));
76 printf("unregister: %s\n", strerror(errno));
78 printf("unregister: success\n");
95 setenv("LD_RESIDENT_REGISTER_NOW", "yes", 1);
97 setenv("LD_RESIDENT_UNREGISTER_NOW", "yes", 1);
100 for ( ; argc > 0; argc--, argv++) {
114 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
119 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
120 warn("%s: can't read program header", *argv);
128 if ((size_t)n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
130 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
131 #if 1 /* Compatibility */
132 || hdr.aout.a_entry < __LDPGSZ
135 warnx("%s: not a dynamic executable", *argv);
138 } else if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
143 if (lseek(fd, 0, SEEK_SET) == -1 ||
144 read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
145 lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
147 warnx("%s: can't read program header", *argv);
150 for (i = 0; i < ehdr.e_phnum; i++) {
151 if (read(fd, &phdr, ehdr.e_phentsize)
153 warnx("%s: can't read program header",
158 if (phdr.p_type == PT_DYNAMIC)
163 warnx("%s: not a dynamic executable", *argv);
165 } else if (hdr.elf.e_type == ET_DYN) {
166 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
169 warnx("%s: not a FreeBSD ELF shared "
175 warnx("%s: not a dynamic executable", *argv);
186 warnx("%s: resident not supported on shared libraries.", *argv);
198 if (wait(&status) <= 0) {
201 } else if (WIFSIGNALED(status)) {
202 fprintf(stderr, "%s: signal %d\n",
203 *argv, WTERMSIG(status));
205 } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
206 switch(WEXITSTATUS(status)) {
208 fprintf(stderr, "%s: entry not found\n",
212 fprintf(stderr, "%s: binary already resident\n",
216 fprintf(stderr, "%s: exit status %s\n",
217 *argv, strerror(WEXITSTATUS(status)));
224 execl(*argv, *argv, (char *)NULL);