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
27 #include <sys/param.h>
30 #include <sys/resident.h>
31 #include <sys/sysctl.h>
33 #include <machine/elf.h>
47 fprintf(stderr
, "usage: resident [-l] [-f] [-x id] [-d] [-R] program ...\n");
54 const char *mib
= "vm.resident";
55 struct xresident
*buf
;
56 size_t res_count
, res_total
, i
;
59 /* get the number of resident binaries */
60 error
= sysctlbyname(mib
, NULL
, &res_count
, NULL
, 0);
62 perror("sysctl: vm.resident");
67 printf("no resident binaries to list\n");
71 /* allocate memory for the list of binaries */
72 res_total
= sizeof(*buf
) * res_count
;
73 if ((buf
= malloc(res_total
)) != NULL
) {
74 /* retrieve entries via sysctl */
75 error
= sysctlbyname(mib
, buf
, &res_total
, NULL
, 0);
77 perror("sysctl: vm.resident");
85 /* print the list of retrieved resident binary */
86 printf("%-4s\t%-15s\t%-12s\t%-30s\n","Id", "Size", "Address", "Executable");
87 for (i
= 0; i
< res_count
; ++i
) {
88 printf("%-4d\t%-15jd\t0x%-12x\t%-30s\n",
90 (intmax_t)buf
[i
].res_stat
.st_size
,
91 (int)buf
[i
].res_entry_addr
,
95 /* free back the memory */
103 main(int argc
, char *argv
[])
110 while ((c
= getopt(argc
, argv
, "Rdflx:")) != -1) {
116 rval
= list_residents();
127 c
= exec_sys_unregister(-2);
129 c
= exec_sys_unregister(strtol(optarg
, NULL
, 0));
131 printf("unregister: %s\n", strerror(errno
));
133 printf("unregister: success\n");
148 /* ld-elf.so magic */
150 if (setenv("LD_RESIDENT_REGISTER_NOW", "yes", 1) == -1)
151 err(1, "setenv failed");
153 if (setenv("LD_RESIDENT_UNREGISTER_NOW", "yes", 1) == -1)
154 err(1, "setenv failed");
158 for ( ; argc
> 0; argc
--, argv
++) {
172 if ((fd
= open(*argv
, O_RDONLY
, 0)) < 0) {
177 if ((n
= read(fd
, &hdr
, sizeof hdr
)) == -1) {
178 warn("%s: can't read program header", *argv
);
186 if ((size_t)n
>= sizeof hdr
.aout
&& !N_BADMAG(hdr
.aout
)) {
188 if ((N_GETFLAG(hdr
.aout
) & EX_DPMASK
) != EX_DYNAMIC
189 #if 1 /* Compatibility */
190 || hdr
.aout
.a_entry
< __LDPGSZ
193 warnx("%s: not a dynamic executable", *argv
);
196 } else if ((size_t)n
>= sizeof hdr
.elf
&& IS_ELF(hdr
.elf
)) {
201 if (lseek(fd
, 0, SEEK_SET
) == -1 ||
202 read(fd
, &ehdr
, sizeof ehdr
) != sizeof ehdr
||
203 lseek(fd
, ehdr
.e_phoff
, SEEK_SET
) == -1
205 warnx("%s: can't read program header", *argv
);
208 for (i
= 0; i
< ehdr
.e_phnum
; i
++) {
209 if (read(fd
, &phdr
, ehdr
.e_phentsize
)
211 warnx("%s: can't read program header",
216 if (phdr
.p_type
== PT_DYNAMIC
)
221 warnx("%s: not a dynamic executable", *argv
);
223 } else if (hdr
.elf
.e_type
== ET_DYN
) {
224 if (hdr
.elf
.e_ident
[EI_OSABI
] & ELFOSABI_FREEBSD
) {
227 warnx("%s: not a FreeBSD ELF shared "
233 warnx("%s: not a dynamic executable", *argv
);
244 warnx("%s: resident not supported on shared libraries.", *argv
);
256 if (wait(&status
) <= 0) {
259 } else if (WIFSIGNALED(status
)) {
260 fprintf(stderr
, "%s: signal %d\n",
261 *argv
, WTERMSIG(status
));
263 } else if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
264 switch(WEXITSTATUS(status
)) {
266 fprintf(stderr
, "%s: entry not found\n",
270 fprintf(stderr
, "%s: binary already resident\n",
274 fprintf(stderr
, "%s: exit status %s\n",
275 *argv
, strerror(WEXITSTATUS(status
)));
282 execl(*argv
, *argv
, NULL
);