a9ee9d5aaebd43de9d362380d16895af6ab647ad
[dragonfly.git] / sys / emulation / ibcs2 / i386 / ibcs2_xenix.c
1 /*-
2  * Copyright (c) 1994 Sean Eric Fagan
3  * Copyright (c) 1994 Søren Schmidt
4  * Copyright (c) 1995 Steven Wallace
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software withough 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/i386/ibcs2/ibcs2_xenix.c,v 1.20 1999/12/15 23:01:46 eivind Exp $
31  * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_xenix.c,v 1.10 2004/11/12 00:09:16 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/nlookup.h> 
38 #include <sys/sysproto.h>
39 #include <sys/kernel.h>
40 #include <sys/filio.h>
41 #include <sys/vnode.h>
42 #include <sys/sysctl.h>
43
44 #include <machine/cpu.h>
45
46 #include "ibcs2_types.h"
47 #include "ibcs2_unistd.h"
48 #include "ibcs2_signal.h"
49 #include "ibcs2_util.h"
50 #include "ibcs2_proto.h"
51 #include "ibcs2_xenix.h"
52 #include "ibcs2_xenix_syscall.h"
53
54 #include <vm/vm_zone.h>
55
56 extern struct sysent xenix_sysent[];
57
58 int
59 ibcs2_xenix(struct ibcs2_xenix_args *uap)
60 {
61         struct proc *p = curproc;
62         struct trapframe *tf = p->p_md.md_regs;
63         struct sysent *callp;
64         u_int code;             
65
66         code = (tf->tf_eax & 0xff00) >> 8;
67         callp = &xenix_sysent[code];
68
69         if (code < IBCS2_XENIX_MAXSYSCALL)
70                 return((*callp->sy_call)((void *)uap));
71         else
72                 return ENOSYS;
73 }
74
75 int
76 xenix_rdchk(struct xenix_rdchk_args *uap)
77 {
78         int error;
79         struct ioctl_args sa;
80         caddr_t sg = stackgap_init();
81
82         DPRINTF(("IBCS2: 'xenix rdchk'\n"));
83         SCARG(&sa, fd) = SCARG(uap, fd);
84         SCARG(&sa, com) = FIONREAD;
85         SCARG(&sa, data) = stackgap_alloc(&sg, sizeof(int));
86         if ((error = ioctl(&sa)) != 0)
87                 return error;
88         uap->sysmsg_result = (*((int*)SCARG(&sa, data))) ? 1 : 0;
89         return 0;
90 }
91
92 int
93 xenix_chsize(struct xenix_chsize_args *uap)
94 {
95         struct ftruncate_args sa;
96
97         DPRINTF(("IBCS2: 'xenix chsize'\n"));
98         SCARG(&sa, fd) = SCARG(uap, fd);
99         SCARG(&sa, pad) = 0;
100         SCARG(&sa, length) = SCARG(uap, size);
101         return ftruncate(&sa);
102 }
103
104
105 int
106 xenix_ftime(struct xenix_ftime_args *uap)
107 {
108         struct timeval tv;
109         struct ibcs2_timeb {
110                 unsigned long time __attribute__((packed));
111                 unsigned short millitm;
112                 short timezone;
113                 short dstflag;
114         } itb;
115
116         DPRINTF(("IBCS2: 'xenix ftime'\n"));
117         microtime(&tv);
118         itb.time = tv.tv_sec;
119         itb.millitm = (tv.tv_usec / 1000);
120         itb.timezone = tz.tz_minuteswest;
121         itb.dstflag = tz.tz_dsttime != DST_NONE;
122
123         return copyout((caddr_t)&itb, (caddr_t)SCARG(uap, tp),
124                        sizeof(struct ibcs2_timeb));
125 }
126
127 int
128 xenix_nap(struct xenix_nap_args *uap)
129 {
130         long period;
131
132         DPRINTF(("IBCS2: 'xenix nap %d ms'\n", SCARG(uap, millisec)));
133         period = (long)SCARG(uap, millisec) / (1000/hz);
134         if (period) {
135                 while (tsleep(&period, 0, "nap", period) != EWOULDBLOCK)
136                     ;
137         }
138         return 0;
139 }
140
141 int
142 xenix_utsname(struct xenix_utsname_args *uap)
143 {
144         struct ibcs2_sco_utsname {
145                 char sysname[9];
146                 char nodename[9];
147                 char release[16];
148                 char kernelid[20];
149                 char machine[9];
150                 char bustype[9];
151                 char sysserial[10];
152                 unsigned short sysorigin;
153                 unsigned short sysoem;
154                 char numusers[9];
155                 unsigned short numcpu;
156         } ibcs2_sco_uname;
157
158         DPRINTF(("IBCS2: 'xenix sco_utsname'\n"));
159         bzero(&ibcs2_sco_uname, sizeof(struct ibcs2_sco_utsname));
160         strncpy(ibcs2_sco_uname.sysname, ostype,
161                 sizeof(ibcs2_sco_uname.sysname) - 1);
162         strncpy(ibcs2_sco_uname.nodename, hostname,
163                 sizeof(ibcs2_sco_uname.nodename) - 1);
164         strncpy(ibcs2_sco_uname.release, osrelease,
165                 sizeof(ibcs2_sco_uname.release) - 1);
166         strncpy(ibcs2_sco_uname.kernelid, version,
167                 sizeof(ibcs2_sco_uname.kernelid) - 1);
168         strncpy(ibcs2_sco_uname.machine, machine,
169                 sizeof(ibcs2_sco_uname.machine) - 1);
170         strncpy(ibcs2_sco_uname.bustype, "ISA/EISA",
171                 sizeof(ibcs2_sco_uname.bustype) - 1);
172         strncpy(ibcs2_sco_uname.sysserial, "no charge",
173                 sizeof(ibcs2_sco_uname.sysserial) - 1);
174         strncpy(ibcs2_sco_uname.numusers, "unlim",
175                 sizeof(ibcs2_sco_uname.numusers) - 1);
176         ibcs2_sco_uname.sysorigin = 0xFFFF;
177         ibcs2_sco_uname.sysoem = 0xFFFF;
178         ibcs2_sco_uname.numcpu = 1;
179         return copyout((caddr_t)&ibcs2_sco_uname,
180                        (caddr_t)(void *)(intptr_t)uap->addr,
181                        sizeof(struct ibcs2_sco_utsname));
182 }
183
184 int
185 xenix_scoinfo(struct xenix_scoinfo_args *uap)
186 {
187         /* scoinfo (not documented) */
188         uap->sysmsg_result = 0;
189         return 0;
190 }
191
192 int     
193 xenix_eaccess(struct xenix_eaccess_args *uap)
194 {
195         struct thread *td = curthread;  /* XXX */
196         struct proc *p = curproc;
197         struct ucred *cred = p->p_ucred;
198         struct vnode *vp;
199         struct nlookupdata nd;
200         int error, flags;
201         caddr_t sg = stackgap_init();
202
203         CHECKALTEXIST(&sg, SCARG(uap, path));
204
205         error = nlookup_init(&nd, SCARG(uap, path), UIO_USERSPACE, NLC_FOLLOW);
206         if (error == 0)
207                 error = nlookup(&nd);
208         if (error == 0)
209                 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
210         if (error)
211                 goto done;
212
213         /* Flags == 0 means only check for existence. */
214         if (SCARG(uap, flags)) {
215                 flags = 0;
216                 if (SCARG(uap, flags) & IBCS2_R_OK)
217                         flags |= VREAD;
218                 if (SCARG(uap, flags) & IBCS2_W_OK)
219                         flags |= VWRITE;
220                 if (SCARG(uap, flags) & IBCS2_X_OK)
221                         flags |= VEXEC;
222                 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
223                         error = VOP_ACCESS(vp, flags, cred, td);
224         }
225         vput(vp);
226 done:
227         nlookup_done(&nd);
228         return (error);
229 }