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