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