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.8 2005/04/22 16:59:46 liamfoy Exp $
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
34 #include <sys/resident.h>
35 #include <sys/sysctl.h>
37 #include <machine/elf.h>
51 fprintf(stderr, "usage: resident [-l] [-f] [-x id] [-d] [-R] program ...\n");
58 const char *mib = "vm.resident";
59 struct xresident *buf;
60 int res_count, res_total;
63 /* get the number of resident binaries */
64 error = sysctlbyname(mib, NULL, &res_count, NULL, 0);
66 perror("sysctl: vm.resident");
71 printf("no resident binaries to list\n");
75 /* allocate memory for the list of binaries */
76 res_total = sizeof(*buf) * res_count;
77 if ((buf = malloc(res_total)) != NULL) {
78 /* retrieve entries via sysctl */
79 error = sysctlbyname(mib, buf, &res_total, NULL, 0);
81 perror("sysctl: vm.resident");
89 /* print the list of retrieved resident binary */
90 printf("%-4s\t%-15s\t%-12s\t%-30s\n","Id", "Size", "Address", "Executable");
91 for (i = 0; i < res_count; ++i) {
92 printf("%-4d\t%-15lld\t0x%-12x\t%-30s\n",
94 buf[i].res_stat.st_size,
95 buf[i].res_entry_addr,
99 /* free back the memory */
107 main(int argc, char *argv[])
114 while ((c = getopt(argc, argv, "Rdflx:")) != -1) {
120 rval = list_residents();
131 c = exec_sys_unregister(-2);
133 c = exec_sys_unregister(strtol(optarg, NULL, 0));
135 printf("unregister: %s\n", strerror(errno));
137 printf("unregister: success\n");
152 /* ld-elf.so magic */
154 if (setenv("LD_RESIDENT_REGISTER_NOW", "yes", 1) == -1)
155 err(1, "setenv failed");
157 if (setenv("LD_RESIDENT_UNREGISTER_NOW", "yes", 1) == -1)
158 err(1, "setenv failed");
162 for ( ; argc > 0; argc--, argv++) {
176 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
181 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
182 warn("%s: can't read program header", *argv);
190 if ((size_t)n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
192 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
193 #if 1 /* Compatibility */
194 || hdr.aout.a_entry < __LDPGSZ
197 warnx("%s: not a dynamic executable", *argv);
200 } else if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
205 if (lseek(fd, 0, SEEK_SET) == -1 ||
206 read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
207 lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
209 warnx("%s: can't read program header", *argv);
212 for (i = 0; i < ehdr.e_phnum; i++) {
213 if (read(fd, &phdr, ehdr.e_phentsize)
215 warnx("%s: can't read program header",
220 if (phdr.p_type == PT_DYNAMIC)
225 warnx("%s: not a dynamic executable", *argv);
227 } else if (hdr.elf.e_type == ET_DYN) {
228 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
231 warnx("%s: not a FreeBSD ELF shared "
237 warnx("%s: not a dynamic executable", *argv);
248 warnx("%s: resident not supported on shared libraries.", *argv);
260 if (wait(&status) <= 0) {
263 } else if (WIFSIGNALED(status)) {
264 fprintf(stderr, "%s: signal %d\n",
265 *argv, WTERMSIG(status));
267 } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
268 switch(WEXITSTATUS(status)) {
270 fprintf(stderr, "%s: entry not found\n",
274 fprintf(stderr, "%s: binary already resident\n",
278 fprintf(stderr, "%s: exit status %s\n",
279 *argv, strerror(WEXITSTATUS(status)));
286 execl(*argv, *argv, (char *)NULL);