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