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