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