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