svr4_sysvec.c revision 71454
1/* 2 * Copyright (c) 1998 Mark Newton 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Christos Zoulas. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $FreeBSD: head/sys/compat/svr4/svr4_sysvec.c 71454 2001-01-23 21:33:55Z jhb $ 31 */ 32 33/* XXX we use functions that might not exist. */ 34#include "opt_compat.h" 35 36#ifndef COMPAT_43 37#error "Unable to compile SVR4-emulator due to missing COMPAT_43 option!" 38#endif 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/proc.h> 43#include <sys/sysent.h> 44#include <sys/imgact.h> 45#include <sys/imgact_elf.h> 46#include <sys/socket.h> 47#include <sys/malloc.h> 48#include <sys/namei.h> 49#include <sys/vnode.h> 50#include <sys/module.h> 51#include <vm/vm.h> 52#include <sys/exec.h> 53#include <sys/kernel.h> 54#include <machine/cpu.h> 55#include <netinet/in.h> 56 57#include <compat/svr4/svr4.h> 58#include <compat/svr4/svr4_types.h> 59#include <compat/svr4/svr4_syscall.h> 60#include <compat/svr4/svr4_signal.h> 61#include <compat/svr4/svr4_sockio.h> 62#include <compat/svr4/svr4_socket.h> 63#include <compat/svr4/svr4_errno.h> 64#include <compat/svr4/svr4_proto.h> 65#include <compat/svr4/svr4_siginfo.h> 66#include <compat/svr4/svr4_util.h> 67 68int bsd_to_svr4_errno[ELAST+1] = { 69 0, 70 SVR4_EPERM, 71 SVR4_ENOENT, 72 SVR4_ESRCH, 73 SVR4_EINTR, 74 SVR4_EIO, 75 SVR4_ENXIO, 76 SVR4_E2BIG, 77 SVR4_ENOEXEC, 78 SVR4_EBADF, 79 SVR4_ECHILD, 80 SVR4_EDEADLK, 81 SVR4_ENOMEM, 82 SVR4_EACCES, 83 SVR4_EFAULT, 84 SVR4_ENOTBLK, 85 SVR4_EBUSY, 86 SVR4_EEXIST, 87 SVR4_EXDEV, 88 SVR4_ENODEV, 89 SVR4_ENOTDIR, 90 SVR4_EISDIR, 91 SVR4_EINVAL, 92 SVR4_ENFILE, 93 SVR4_EMFILE, 94 SVR4_ENOTTY, 95 SVR4_ETXTBSY, 96 SVR4_EFBIG, 97 SVR4_ENOSPC, 98 SVR4_ESPIPE, 99 SVR4_EROFS, 100 SVR4_EMLINK, 101 SVR4_EPIPE, 102 SVR4_EDOM, 103 SVR4_ERANGE, 104 SVR4_EAGAIN, 105 SVR4_EINPROGRESS, 106 SVR4_EALREADY, 107 SVR4_ENOTSOCK, 108 SVR4_EDESTADDRREQ, 109 SVR4_EMSGSIZE, 110 SVR4_EPROTOTYPE, 111 SVR4_ENOPROTOOPT, 112 SVR4_EPROTONOSUPPORT, 113 SVR4_ESOCKTNOSUPPORT, 114 SVR4_EOPNOTSUPP, 115 SVR4_EPFNOSUPPORT, 116 SVR4_EAFNOSUPPORT, 117 SVR4_EADDRINUSE, 118 SVR4_EADDRNOTAVAIL, 119 SVR4_ENETDOWN, 120 SVR4_ENETUNREACH, 121 SVR4_ENETRESET, 122 SVR4_ECONNABORTED, 123 SVR4_ECONNRESET, 124 SVR4_ENOBUFS, 125 SVR4_EISCONN, 126 SVR4_ENOTCONN, 127 SVR4_ESHUTDOWN, 128 SVR4_ETOOMANYREFS, 129 SVR4_ETIMEDOUT, 130 SVR4_ECONNREFUSED, 131 SVR4_ELOOP, 132 SVR4_ENAMETOOLONG, 133 SVR4_EHOSTDOWN, 134 SVR4_EHOSTUNREACH, 135 SVR4_ENOTEMPTY, 136 SVR4_EPROCLIM, 137 SVR4_EUSERS, 138 SVR4_EDQUOT, 139 SVR4_ESTALE, 140 SVR4_EREMOTE, 141 SVR4_EBADRPC, 142 SVR4_ERPCMISMATCH, 143 SVR4_EPROGUNAVAIL, 144 SVR4_EPROGMISMATCH, 145 SVR4_EPROCUNAVAIL, 146 SVR4_ENOLCK, 147 SVR4_ENOSYS, 148 SVR4_EFTYPE, 149 SVR4_EAUTH, 150 SVR4_ENEEDAUTH, 151 SVR4_EIDRM, 152 SVR4_ENOMSG, 153}; 154 155 156static int svr4_fixup(register_t **stack_base, struct image_params *imgp); 157 158extern struct sysent svr4_sysent[]; 159#undef szsigcode 160#undef sigcode 161 162extern int svr4_szsigcode; 163extern char svr4_sigcode[]; 164 165struct sysentvec svr4_sysvec = { 166 SVR4_SYS_MAXSYSCALL, 167 svr4_sysent, 168 0xff, 169 SVR4_SIGTBLSZ, 170 bsd_to_svr4_sig, 171 ELAST, /* ELAST */ 172 bsd_to_svr4_errno, 173 0, 174 svr4_fixup, 175 svr4_sendsig, 176 svr4_sigcode, 177 &svr4_szsigcode, 178 NULL, 179 "SVR4", 180 elf_coredump, 181 NULL, 182 SVR4_MINSIGSTKSZ 183}; 184 185Elf32_Brandinfo svr4_brand = { 186 ELFOSABI_SOLARIS, /* XXX Or should we use ELFOSABI_SYSV here? */ 187 svr4_emul_path, 188 "/lib/libc.so.1", 189 &svr4_sysvec 190}; 191 192const char svr4_emul_path[] = "/compat/svr4"; 193 194static int 195svr4_fixup(register_t **stack_base, struct image_params *imgp) 196{ 197 Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs; 198 register_t *pos; 199 200 pos = *stack_base + (imgp->argc + imgp->envc + 2); 201 202 if (args->trace) { 203 AUXARGS_ENTRY(pos, AT_DEBUG, 1); 204 } 205 if (args->execfd != -1) { 206 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 207 } 208 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 209 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 210 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 211 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 212 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 213 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 214 AUXARGS_ENTRY(pos, AT_BASE, args->base); 215 PROC_LOCK(imgp->proc); 216 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid); 217 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid); 218 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid); 219 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid); 220 PROC_UNLOCK(imgp->proc); 221 AUXARGS_ENTRY(pos, AT_NULL, 0); 222 223 free(imgp->auxargs, M_TEMP); 224 imgp->auxargs = NULL; 225 226 (*stack_base)--; 227 **stack_base = (int)imgp->argc; 228 return 0; 229} 230 231/* 232 * Search an alternate path before passing pathname arguments on 233 * to system calls. Useful for keeping a seperate 'emulation tree'. 234 * 235 * If cflag is set, we check if an attempt can be made to create 236 * the named file, i.e. we check if the directory it should 237 * be in exists. 238 * 239 * Code shamelessly stolen by Mark Newton from IBCS2 emulation code. 240 */ 241int 242svr4_emul_find(p, sgp, prefix, path, pbuf, cflag) 243 struct proc *p; 244 caddr_t *sgp; /* Pointer to stackgap memory */ 245 const char *prefix; 246 char *path; 247 char **pbuf; 248 int cflag; 249{ 250 struct nameidata nd; 251 struct nameidata ndroot; 252 struct vattr vat; 253 struct vattr vatroot; 254 struct ucred *uc; 255 int error; 256 char *ptr, *buf, *cp; 257 size_t sz, len; 258 259 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 260 *pbuf = path; 261 262 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++) 263 continue; 264 265 sz = MAXPATHLEN - (ptr - buf); 266 267 /* 268 * If sgp is not given then the path is already in kernel space 269 */ 270 if (sgp == NULL) 271 error = copystr(path, ptr, sz, &len); 272 else 273 error = copyinstr(path, ptr, sz, &len); 274 275 if (error) { 276 free(buf, M_TEMP); 277 return error; 278 } 279 280 if (*ptr != '/') { 281 free(buf, M_TEMP); 282 return EINVAL; 283 } 284 285 /* 286 * We know that there is a / somewhere in this pathname. 287 * Search backwards for it, to find the file's parent dir 288 * to see if it exists in the alternate tree. If it does, 289 * and we want to create a file (cflag is set). We don't 290 * need to worry about the root comparison in this case. 291 */ 292 293 if (cflag) { 294 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 295 *cp = '\0'; 296 297 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 298 299 if ((error = namei(&nd)) != 0) { 300 free(buf, M_TEMP); 301 return error; 302 } 303 NDFREE(&nd, NDF_ONLY_PNBUF); 304 305 *cp = '/'; 306 } 307 else { 308 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 309 310 if ((error = namei(&nd)) != 0) { 311 free(buf, M_TEMP); 312 return error; 313 } 314 NDFREE(&nd, NDF_ONLY_PNBUF); 315 316 /* 317 * We now compare the vnode of the svr4_root to the one 318 * vnode asked. If they resolve to be the same, then we 319 * ignore the match so that the real root gets used. 320 * This avoids the problem of traversing "../.." to find the 321 * root directory and never finding it, because "/" resolves 322 * to the emulation root directory. This is expensive :-( 323 */ 324 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, svr4_emul_path, 325 p); 326 327 if ((error = namei(&ndroot)) != 0) { 328 /* Cannot happen! */ 329 free(buf, M_TEMP); 330 vrele(nd.ni_vp); 331 return error; 332 } 333 NDFREE(&ndroot, NDF_ONLY_PNBUF); 334 335 PROC_LOCK(p); 336 uc = p->p_ucred; 337 crhold(uc); 338 PROC_UNLOCK(p); 339 if ((error = VOP_GETATTR(nd.ni_vp, &vat, uc, p)) != 0) { 340 crfree(uc); 341 goto done; 342 } 343 344 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, uc, p)) != 0) { 345 crfree(uc); 346 goto done; 347 } 348 crfree(uc); 349 350 if (vat.va_fsid == vatroot.va_fsid && 351 vat.va_fileid == vatroot.va_fileid) { 352 error = ENOENT; 353 goto done; 354 } 355 356 } 357 if (sgp == NULL) 358 *pbuf = buf; 359 else { 360 sz = &ptr[len] - buf; 361 *pbuf = stackgap_alloc(sgp, sz + 1); 362 error = copyout(buf, *pbuf, sz); 363 free(buf, M_TEMP); 364 } 365 366 367done: 368 vrele(nd.ni_vp); 369 if (!cflag) 370 vrele(ndroot.ni_vp); 371 return error; 372} 373 374static int 375svr4_elf_modevent(module_t mod, int type, void *data) 376{ 377 int error; 378 379 error = 0; 380 381 switch(type) { 382 case MOD_LOAD: 383 if (elf_insert_brand_entry(&svr4_brand) < 0) 384 error = EINVAL; 385 if (error) 386 printf("cannot insert svr4 elf brand handler\n"); 387 else if (bootverbose) 388 printf("svr4 ELF exec handler installed\n"); 389 break; 390 case MOD_UNLOAD: 391 /* Only allow the emulator to be removed if it isn't in use. */ 392 if (elf_brand_inuse(&svr4_brand) != 0) { 393 error = EBUSY; 394 } else if (elf_remove_brand_entry(&svr4_brand) < 0) { 395 error = EINVAL; 396 } 397 398 if (error) 399 printf("Could not deinstall ELF interpreter entry (error %d)\n", 400 error); 401 else if (bootverbose) 402 printf("svr4 ELF exec handler removed\n"); 403 break; 404 default: 405 break; 406 } 407 return error; 408} 409 410static moduledata_t svr4_elf_mod = { 411 "svr4elf", 412 svr4_elf_modevent, 413 0 414}; 415DECLARE_MODULE(svr4elf, svr4_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY); 416MODULE_DEPEND(svr4elf, streams, 1, 1, 1); 417