syscall messaging 2: Change the standard return value storage for system
[dragonfly.git] / sys / emulation / ibcs2 / i386 / ibcs2_misc.c
1 /*
2  * Copyright (c) 1995 Steven Wallace
3  * Copyright (c) 1994, 1995 Scott Bartram
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *      This product includes software developed by the University of
14  *      California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *      This product includes software developed by the University of
27  *      California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp 
45  *
46  *      @(#)sun_misc.c  8.1 (Berkeley) 6/18/93
47  *
48  * $FreeBSD: src/sys/i386/ibcs2/ibcs2_misc.c,v 1.34 1999/09/29 15:12:09 marcel Exp $
49  * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_misc.c,v 1.5 2003/07/26 18:12:43 dillon Exp $
50  */
51
52 /*
53  * IBCS2 compatibility module.
54  *
55  * IBCS2 system calls that are implemented differently in BSD are
56  * handled here.
57  */
58 #include <sys/param.h>
59 #include <sys/dirent.h>
60 #include <sys/fcntl.h>
61 #include <sys/file.h>
62 #include <sys/filedesc.h>
63 #include <sys/kernel.h>
64 #include <sys/lock.h>
65 #include <sys/malloc.h>
66 #include <sys/reboot.h>
67 #include <sys/resourcevar.h>
68 #include <sys/stat.h>
69 #include <sys/sysctl.h>
70 #include <sys/sysproto.h>
71 #include <sys/systm.h>
72 #include <sys/time.h>
73 #include <sys/times.h>
74 #include <sys/vnode.h>
75 #include <sys/wait.h>
76
77 #include <machine/cpu.h>
78
79 #include <i386/ibcs2/ibcs2_dirent.h>
80 #include <i386/ibcs2/ibcs2_signal.h>
81 #include <i386/ibcs2/ibcs2_proto.h>
82 #include <i386/ibcs2/ibcs2_unistd.h>
83 #include <i386/ibcs2/ibcs2_util.h>
84 #include <i386/ibcs2/ibcs2_utime.h>
85 #include <i386/ibcs2/ibcs2_xenix.h>
86
87 int
88 ibcs2_ulimit(struct ibcs2_ulimit_args *uap)
89 {
90 #ifdef notyet
91         int error;
92         struct rlimit rl;
93         struct setrlimit_args {
94                 int resource;
95                 struct rlimit *rlp;
96         } sra;
97 #endif
98         struct proc *p = curproc;
99
100 #define IBCS2_GETFSIZE          1
101 #define IBCS2_SETFSIZE          2
102 #define IBCS2_GETPSIZE          3
103 #define IBCS2_GETDTABLESIZE     4
104         
105         switch (SCARG(uap, cmd)) {
106         case IBCS2_GETFSIZE:
107                 uap->lmsg.u.ms_result = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
108                 if (uap->lmsg.u.ms_result == -1) 
109                     uap->lmsg.u.ms_result = 0x7fffffff;
110                 return 0;
111         case IBCS2_SETFSIZE:    /* XXX - fix this */
112 #ifdef notyet
113                 rl.rlim_cur = SCARG(uap, newlimit);
114                 sra.resource = RLIMIT_FSIZE;
115                 sra.rlp = &rl;
116                 error = setrlimit(&sra);
117                 if (!error)
118                         uap->lmsg.u.ms_result = p->p_rlimit[RLIMIT_FSIZE].rlim_cur;
119                 else
120                         DPRINTF(("failed "));
121                 return error;
122 #else
123                 uap->lmsg.u.ms_result = SCARG(uap, newlimit);
124                 return 0;
125 #endif
126         case IBCS2_GETPSIZE:
127                 uap->lmsg.u.ms_result = p->p_rlimit[RLIMIT_RSS].rlim_cur; /* XXX */
128                 return 0;
129         case IBCS2_GETDTABLESIZE:
130                 uap->cmd = IBCS2_SC_OPEN_MAX;
131                 return ibcs2_sysconf((struct ibcs2_sysconf_args *)uap);
132         default:
133                 return ENOSYS;
134         }
135 }
136
137 #define IBCS2_WSTOPPED       0177
138 #define IBCS2_STOPCODE(sig)  ((sig) << 8 | IBCS2_WSTOPPED)
139 int
140 ibcs2_wait(struct ibcs2_wait_args *uap)
141 {
142         struct proc *p = curproc;
143         int error, status;
144         struct wait_args w4;
145         struct trapframe *tf = p->p_md.md_regs;
146         
147         SCARG(&w4, rusage) = NULL;
148         if ((tf->tf_eflags & (PSL_Z|PSL_PF|PSL_N|PSL_V))
149             == (PSL_Z|PSL_PF|PSL_N|PSL_V)) {
150                 /* waitpid */
151                 SCARG(&w4, pid) = SCARG(uap, a1);
152                 SCARG(&w4, status) = (int *)SCARG(uap, a2);
153                 SCARG(&w4, options) = SCARG(uap, a3);
154         } else {
155                 /* wait */
156                 SCARG(&w4, pid) = WAIT_ANY;
157                 SCARG(&w4, status) = (int *)SCARG(uap, a1);
158                 SCARG(&w4, options) = 0;
159         }
160         if ((error = wait4(&w4)) != 0)
161                 return error;
162         uap->lmsg.u.ms_fds[0] = w4.lmsg.u.ms_fds[0];
163         if (SCARG(&w4, status)) {       /* this is real iBCS brain-damage */
164                 error = copyin((caddr_t)SCARG(&w4, status), (caddr_t)&status,
165                                sizeof(SCARG(&w4, status)));
166                 if(error)
167                   return error;
168
169                 /* convert status/signal result */
170                 if(WIFSTOPPED(status))
171                         status =
172                           IBCS2_STOPCODE(bsd_to_ibcs2_sig[_SIG_IDX(WSTOPSIG(status))]);
173                 else if(WIFSIGNALED(status))
174                         status = bsd_to_ibcs2_sig[_SIG_IDX(WTERMSIG(status))];
175                 /* else exit status -- identical */
176
177                 /* record result/status */
178                 uap->lmsg.u.ms_fds[1] = status;
179                 return copyout((caddr_t)&status, (caddr_t)SCARG(&w4, status),
180                                sizeof(SCARG(&w4, status)));
181         }
182
183         return 0;
184 }
185
186 int
187 ibcs2_execv(struct ibcs2_execv_args *uap)
188 {
189         struct execve_args ea;
190         caddr_t sg = stackgap_init();
191         int error;
192
193         CHECKALTEXIST(&sg, SCARG(uap, path));
194         SCARG(&ea, fname) = SCARG(uap, path);
195         SCARG(&ea, argv) = SCARG(uap, argp);
196         SCARG(&ea, envv) = NULL;
197         error = execve(&ea);
198         uap->lmsg.u.ms_result = ea.lmsg.u.ms_result;
199         return(error);
200 }
201
202 int
203 ibcs2_execve(struct ibcs2_execve_args *uap)
204 {
205         caddr_t sg = stackgap_init();
206         CHECKALTEXIST(&sg, SCARG(uap, path));
207         return execve((struct execve_args *)uap);
208 }
209
210 int
211 ibcs2_umount(struct ibcs2_umount_args *uap)
212 {
213         struct unmount_args um;
214         int error;
215
216         SCARG(&um, path) = SCARG(uap, name);
217         SCARG(&um, flags) = 0;
218         error = unmount(&um);
219         uap->lmsg.u.ms_result = um.lmsg.u.ms_result;
220         return(error);
221 }
222
223 int
224 ibcs2_mount(struct ibcs2_mount_args *uap)
225 {
226 #ifdef notyet
227         int oflags = SCARG(uap, flags), nflags, error;
228         char fsname[MFSNAMELEN];
229
230         if (oflags & (IBCS2_MS_NOSUB | IBCS2_MS_SYS5))
231                 return (EINVAL);
232         if ((oflags & IBCS2_MS_NEWTYPE) == 0)
233                 return (EINVAL);
234         nflags = 0;
235         if (oflags & IBCS2_MS_RDONLY)
236                 nflags |= MNT_RDONLY;
237         if (oflags & IBCS2_MS_NOSUID)
238                 nflags |= MNT_NOSUID;
239         if (oflags & IBCS2_MS_REMOUNT)
240                 nflags |= MNT_UPDATE;
241         SCARG(uap, flags) = nflags;
242
243         if (error = copyinstr((caddr_t)SCARG(uap, type), fsname, sizeof fsname,
244                               (u_int *)0))
245                 return (error);
246
247         if (strcmp(fsname, "4.2") == 0) {
248                 SCARG(uap, type) = (caddr_t)STACK_ALLOC();
249                 if (error = copyout("ufs", SCARG(uap, type), sizeof("ufs")))
250                         return (error);
251         } else if (strcmp(fsname, "nfs") == 0) {
252                 struct ibcs2_nfs_args sna;
253                 struct sockaddr_in sain;
254                 struct nfs_args na;
255                 struct sockaddr sa;
256
257                 if (error = copyin(SCARG(uap, data), &sna, sizeof sna))
258                         return (error);
259                 if (error = copyin(sna.addr, &sain, sizeof sain))
260                         return (error);
261                 bcopy(&sain, &sa, sizeof sa);
262                 sa.sa_len = sizeof(sain);
263                 SCARG(uap, data) = (caddr_t)STACK_ALLOC();
264                 na.addr = (struct sockaddr *)((int)SCARG(uap, data) + sizeof na);
265                 na.sotype = SOCK_DGRAM;
266                 na.proto = IPPROTO_UDP;
267                 na.fh = (nfsv2fh_t *)sna.fh;
268                 na.flags = sna.flags;
269                 na.wsize = sna.wsize;
270                 na.rsize = sna.rsize;
271                 na.timeo = sna.timeo;
272                 na.retrans = sna.retrans;
273                 na.hostname = sna.hostname;
274
275                 if (error = copyout(&sa, na.addr, sizeof sa))
276                         return (error);
277                 if (error = copyout(&na, SCARG(uap, data), sizeof na))
278                         return (error);
279         }
280         return (mount(uap));
281 #else
282         return EINVAL;
283 #endif
284 }
285
286 /*
287  * Read iBCS2-style directory entries.  We suck them into kernel space so
288  * that they can be massaged before being copied out to user code.  Like
289  * SunOS, we squish out `empty' entries.
290  *
291  * This is quite ugly, but what do you expect from compatibility code?
292  */
293
294 int
295 ibcs2_getdents(struct ibcs2_getdents_args *uap)
296 {
297         struct thread *td = curthread;
298         struct proc *p = td->td_proc;
299         struct vnode *vp;
300         caddr_t inp, buf;       /* BSD-format */
301         int len, reclen;        /* BSD-format */
302         caddr_t outp;           /* iBCS2-format */
303         int resid;              /* iBCS2-format */
304         struct file *fp;
305         struct uio auio;
306         struct iovec aiov;
307         struct ibcs2_dirent idb;
308         off_t off;                      /* true file offset */
309         int buflen, error, eofflag;
310         u_long *cookies = NULL, *cookiep;
311         int ncookies;
312 #define BSD_DIRENT(cp)          ((struct dirent *)(cp))
313 #define IBCS2_RECLEN(reclen)    (reclen + sizeof(u_short))
314
315         KKASSERT(p);
316
317         if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
318                 return (error);
319         if ((fp->f_flag & FREAD) == 0)
320                 return (EBADF);
321         vp = (struct vnode *)fp->f_data;
322         if (vp->v_type != VDIR) /* XXX  vnode readdir op should do this */
323                 return (EINVAL);
324
325         off = fp->f_offset;
326 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
327         buflen = max(DIRBLKSIZ, SCARG(uap, nbytes));
328         buflen = min(buflen, MAXBSIZE);
329         buf = malloc(buflen, M_TEMP, M_WAITOK);
330         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
331 again:
332         aiov.iov_base = buf;
333         aiov.iov_len = buflen;
334         auio.uio_iov = &aiov;
335         auio.uio_iovcnt = 1;
336         auio.uio_rw = UIO_READ;
337         auio.uio_segflg = UIO_SYSSPACE;
338         auio.uio_td = td;
339         auio.uio_resid = buflen;
340         auio.uio_offset = off;
341
342         if (cookies) {
343                 free(cookies, M_TEMP);
344                 cookies = NULL;
345         }
346
347         /*
348          * First we read into the malloc'ed buffer, then
349          * we massage it into user space, one record at a time.
350          */
351         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0)
352                 goto out;
353         inp = buf;
354         outp = SCARG(uap, buf);
355         resid = SCARG(uap, nbytes);
356         if ((len = buflen - auio.uio_resid) <= 0)
357                 goto eof;
358
359         cookiep = cookies;
360
361         if (cookies) {
362                 /*
363                  * When using cookies, the vfs has the option of reading from
364                  * a different offset than that supplied (UFS truncates the
365                  * offset to a block boundary to make sure that it never reads
366                  * partway through a directory entry, even if the directory
367                  * has been compacted).
368                  */
369                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
370                         len -= BSD_DIRENT(inp)->d_reclen;
371                         inp += BSD_DIRENT(inp)->d_reclen;
372                         cookiep++;
373                         ncookies--;
374                 }
375         }
376
377         for (; len > 0; len -= reclen) {
378                 if (cookiep && ncookies == 0)
379                         break;
380                 reclen = BSD_DIRENT(inp)->d_reclen;
381                 if (reclen & 3) {
382                         printf("ibcs2_getdents: reclen=%d\n", reclen);
383                         error = EFAULT;
384                         goto out;
385                 }
386                 if (BSD_DIRENT(inp)->d_fileno == 0) {
387                         inp += reclen;  /* it is a hole; squish it out */
388                         if (cookiep) {
389                                 off = *cookiep++;
390                                 ncookies--;
391                         } else
392                                 off += reclen;
393                         continue;
394                 }
395                 if (reclen > len || resid < IBCS2_RECLEN(reclen)) {
396                         /* entry too big for buffer, so just stop */
397                         outp++;
398                         break;
399                 }
400                 /*
401                  * Massage in place to make a iBCS2-shaped dirent (otherwise
402                  * we have to worry about touching user memory outside of
403                  * the copyout() call).
404                  */
405                 idb.d_ino = (ibcs2_ino_t)BSD_DIRENT(inp)->d_fileno;
406                 idb.d_off = (ibcs2_off_t)off;
407                 idb.d_reclen = (u_short)IBCS2_RECLEN(reclen);
408                 if ((error = copyout((caddr_t)&idb, outp, 10)) != 0 ||
409                     (error = copyout(BSD_DIRENT(inp)->d_name, outp + 10,
410                                      BSD_DIRENT(inp)->d_namlen + 1)) != 0)
411                         goto out;
412                 /* advance past this real entry */
413                 if (cookiep) {
414                         off = *cookiep++;
415                         ncookies--;
416                 } else
417                         off += reclen;
418                 inp += reclen;
419                 /* advance output past iBCS2-shaped entry */
420                 outp += IBCS2_RECLEN(reclen);
421                 resid -= IBCS2_RECLEN(reclen);
422         }
423         /* if we squished out the whole block, try again */
424         if (outp == SCARG(uap, buf))
425                 goto again;
426         fp->f_offset = off;             /* update the vnode offset */
427 eof:
428         uap->lmsg.u.ms_result = SCARG(uap, nbytes) - resid;
429 out:
430         if (cookies)
431                 free(cookies, M_TEMP);
432         VOP_UNLOCK(vp, 0, td);
433         free(buf, M_TEMP);
434         return (error);
435 }
436
437 int
438 ibcs2_read(struct ibcs2_read_args *uap)
439 {
440         struct thread *td = curthread;
441         struct proc *p = td->td_proc;
442         struct vnode *vp;
443         caddr_t inp, buf;       /* BSD-format */
444         int len, reclen;        /* BSD-format */
445         caddr_t outp;           /* iBCS2-format */
446         int resid;              /* iBCS2-format */
447         struct file *fp;
448         struct uio auio;
449         struct iovec aiov;
450         struct ibcs2_direct {
451                 ibcs2_ino_t ino;
452                 char name[14];
453         } idb;
454         off_t off;                      /* true file offset */
455         int buflen, error, eofflag, size;
456         u_long *cookies = NULL, *cookiep;
457         int ncookies;
458
459         KKASSERT(p);
460
461         if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0) {
462                 if (error == EINVAL)
463                         return read((struct read_args *)uap);
464                 else
465                         return error;
466         }
467         if ((fp->f_flag & FREAD) == 0)
468                 return (EBADF);
469         vp = (struct vnode *)fp->f_data;
470         if (vp->v_type != VDIR)
471                 return read((struct read_args *)uap);
472
473         DPRINTF(("ibcs2_read: read directory\n"));
474
475         off = fp->f_offset;
476         buflen = max(DIRBLKSIZ, SCARG(uap, nbytes));
477         buflen = min(buflen, MAXBSIZE);
478         buf = malloc(buflen, M_TEMP, M_WAITOK);
479         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
480 again:
481         aiov.iov_base = buf;
482         aiov.iov_len = buflen;
483         auio.uio_iov = &aiov;
484         auio.uio_iovcnt = 1;
485         auio.uio_rw = UIO_READ;
486         auio.uio_segflg = UIO_SYSSPACE;
487         auio.uio_td = td;
488         auio.uio_resid = buflen;
489         auio.uio_offset = off;
490
491         if (cookies) {
492                 free(cookies, M_TEMP);
493                 cookies = NULL;
494         }
495
496         /*
497          * First we read into the malloc'ed buffer, then
498          * we massage it into user space, one record at a time.
499          */
500         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0) {
501                 DPRINTF(("VOP_READDIR failed: %d\n", error));
502                 goto out;
503         }
504         inp = buf;
505         outp = SCARG(uap, buf);
506         resid = SCARG(uap, nbytes);
507         if ((len = buflen - auio.uio_resid) <= 0)
508                 goto eof;
509
510         cookiep = cookies;
511
512         if (cookies) {
513                 /*
514                  * When using cookies, the vfs has the option of reading from
515                  * a different offset than that supplied (UFS truncates the
516                  * offset to a block boundary to make sure that it never reads
517                  * partway through a directory entry, even if the directory
518                  * has been compacted).
519                  */
520                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
521                         len -= BSD_DIRENT(inp)->d_reclen;
522                         inp += BSD_DIRENT(inp)->d_reclen;
523                         cookiep++;
524                         ncookies--;
525                 }
526         }
527
528         for (; len > 0 && resid > 0; len -= reclen) {
529                 if (cookiep && ncookies == 0)
530                         break;
531                 reclen = BSD_DIRENT(inp)->d_reclen;
532                 if (reclen & 3) {
533                         printf("ibcs2_read: reclen=%d\n", reclen);
534                         error = EFAULT;
535                         goto out;
536                 }
537                 if (BSD_DIRENT(inp)->d_fileno == 0) {
538                         inp += reclen;  /* it is a hole; squish it out */
539                         if (cookiep) {
540                                 off = *cookiep++;
541                                 ncookies--;
542                         } else
543                                 off += reclen;
544                         continue;
545                 }
546                 if (reclen > len || resid < sizeof(struct ibcs2_direct)) {
547                         /* entry too big for buffer, so just stop */
548                         outp++;
549                         break;
550                 }
551                 /*
552                  * Massage in place to make a iBCS2-shaped dirent (otherwise
553                  * we have to worry about touching user memory outside of
554                  * the copyout() call).
555                  *
556                  * TODO: if length(filename) > 14, then break filename into
557                  * multiple entries and set inode = 0xffff except last
558                  */
559                 idb.ino = (BSD_DIRENT(inp)->d_fileno > 0xfffe) ? 0xfffe :
560                         BSD_DIRENT(inp)->d_fileno;
561                 (void)copystr(BSD_DIRENT(inp)->d_name, idb.name, 14, &size);
562                 bzero(idb.name + size, 14 - size);
563                 if ((error = copyout(&idb, outp, sizeof(struct ibcs2_direct))) != 0)
564                         goto out;
565                 /* advance past this real entry */
566                 if (cookiep) {
567                         off = *cookiep++;
568                         ncookies--;
569                 } else
570                         off += reclen;
571                 inp += reclen;
572                 /* advance output past iBCS2-shaped entry */
573                 outp += sizeof(struct ibcs2_direct);
574                 resid -= sizeof(struct ibcs2_direct);
575         }
576         /* if we squished out the whole block, try again */
577         if (outp == SCARG(uap, buf))
578                 goto again;
579         fp->f_offset = off;             /* update the vnode offset */
580 eof:
581         uap->lmsg.u.ms_result = SCARG(uap, nbytes) - resid;
582 out:
583         if (cookies)
584                 free(cookies, M_TEMP);
585         VOP_UNLOCK(vp, 0, td);
586         free(buf, M_TEMP);
587         return (error);
588 }
589
590 int
591 ibcs2_mknod(struct ibcs2_mknod_args *uap)
592 {
593         caddr_t sg = stackgap_init();
594         int error;
595
596         CHECKALTCREAT(&sg, SCARG(uap, path));
597         if (S_ISFIFO(SCARG(uap, mode))) {
598                 struct mkfifo_args ap;
599                 SCARG(&ap, path) = SCARG(uap, path);
600                 SCARG(&ap, mode) = SCARG(uap, mode);
601                 error = mkfifo(&ap);
602                 uap->lmsg.u.ms_result = ap.lmsg.u.ms_result;
603         } else {
604                 struct mknod_args ap;
605                 SCARG(&ap, path) = SCARG(uap, path);
606                 SCARG(&ap, mode) = SCARG(uap, mode);
607                 SCARG(&ap, dev) = SCARG(uap, dev);
608                 error = mknod(&ap);
609                 uap->lmsg.u.ms_result = ap.lmsg.u.ms_result;
610         }
611         return(error);
612 }
613
614 int
615 ibcs2_getgroups(struct ibcs2_getgroups_args *uap)
616 {
617         int error, i;
618         ibcs2_gid_t *iset = NULL;
619         struct getgroups_args sa;
620         gid_t *gp;
621         caddr_t sg = stackgap_init();
622
623         SCARG(&sa, gidsetsize) = SCARG(uap, gidsetsize);
624         if (SCARG(uap, gidsetsize)) {
625                 SCARG(&sa, gidset) = stackgap_alloc(&sg, NGROUPS_MAX *
626                                                     sizeof(gid_t *));
627                 iset = stackgap_alloc(&sg, SCARG(uap, gidsetsize) *
628                                       sizeof(ibcs2_gid_t));
629         }
630         if ((error = getgroups(&sa)) != 0)
631                 return error;
632         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
633         if (SCARG(uap, gidsetsize) == 0)
634                 return 0;
635
636         for (i = 0, gp = SCARG(&sa, gidset); i < uap->lmsg.u.ms_result; i++)
637                 iset[i] = (ibcs2_gid_t)*gp++;
638         if (uap->lmsg.u.ms_result && (error = copyout((caddr_t)iset,
639                                           (caddr_t)SCARG(uap, gidset),
640                                           sizeof(ibcs2_gid_t) * uap->lmsg.u.ms_result)))
641                 return error;
642         return 0;
643 }
644
645 int
646 ibcs2_setgroups(struct ibcs2_setgroups_args *uap)
647 {
648         int error, i;
649         ibcs2_gid_t *iset;
650         struct setgroups_args sa;
651         gid_t *gp;
652         caddr_t sg = stackgap_init();
653
654         SCARG(&sa, gidsetsize) = SCARG(uap, gidsetsize);
655         SCARG(&sa, gidset) = stackgap_alloc(&sg, SCARG(&sa, gidsetsize) *
656                                             sizeof(gid_t *));
657         iset = stackgap_alloc(&sg, SCARG(&sa, gidsetsize) *
658                               sizeof(ibcs2_gid_t *));
659         if (SCARG(&sa, gidsetsize)) {
660                 if ((error = copyin((caddr_t)SCARG(uap, gidset), (caddr_t)iset, 
661                                    sizeof(ibcs2_gid_t *) *
662                                    SCARG(uap, gidsetsize))) != 0)
663                         return error;
664         }
665         for (i = 0, gp = SCARG(&sa, gidset); i < SCARG(&sa, gidsetsize); i++)
666                 *gp++ = (gid_t)iset[i];
667         error = setgroups(&sa);
668         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
669         return(error);
670 }
671
672 int
673 ibcs2_setuid(struct ibcs2_setuid_args *uap)
674 {
675         struct setuid_args sa;
676         int error;
677
678         SCARG(&sa, uid) = (uid_t)SCARG(uap, uid);
679         error = setuid(&sa);
680         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
681         return(error);
682 }
683
684 int
685 ibcs2_setgid(struct ibcs2_setgid_args *uap)
686 {
687         struct setgid_args sa;
688         int error;
689
690         SCARG(&sa, gid) = (gid_t)SCARG(uap, gid);
691         error = setgid(&sa);
692         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
693         return(error);
694 }
695
696 int
697 ibcs2_time(struct ibcs2_time_args *uap)
698 {
699         struct timeval tv;
700
701         microtime(&tv);
702         uap->lmsg.u.ms_result = tv.tv_sec;
703         if (SCARG(uap, tp))
704                 return copyout((caddr_t)&tv.tv_sec, (caddr_t)SCARG(uap, tp),
705                                sizeof(ibcs2_time_t));
706         else
707                 return 0;
708 }
709
710 int
711 ibcs2_pathconf(struct ibcs2_pathconf_args *uap)
712 {
713         SCARG(uap, name)++;     /* iBCS2 _PC_* defines are offset by one */
714         return pathconf((struct pathconf_args *)uap);
715 }
716
717 int
718 ibcs2_fpathconf(struct ibcs2_fpathconf_args *uap)
719 {
720         SCARG(uap, name)++;     /* iBCS2 _PC_* defines are offset by one */
721         return fpathconf((struct fpathconf_args *)uap);
722 }
723
724 int
725 ibcs2_sysconf(struct ibcs2_sysconf_args *uap)
726 {
727         int mib[2], value, len, error;
728         struct sysctl_args sa;
729         struct __getrlimit_args ga;
730
731         switch(SCARG(uap, name)) {
732         case IBCS2_SC_ARG_MAX:
733                 mib[1] = KERN_ARGMAX;
734                 break;
735
736         case IBCS2_SC_CHILD_MAX:
737             {
738                 caddr_t sg = stackgap_init();
739
740                 SCARG(&ga, which) = RLIMIT_NPROC;
741                 SCARG(&ga, rlp) = stackgap_alloc(&sg, sizeof(struct rlimit *));
742                 if ((error = getrlimit(&ga)) != 0)
743                         return error;
744                 uap->lmsg.u.ms_result = SCARG(&ga, rlp)->rlim_cur;
745                 return 0;
746             }
747
748         case IBCS2_SC_CLK_TCK:
749                 uap->lmsg.u.ms_result = hz;
750                 return 0;
751
752         case IBCS2_SC_NGROUPS_MAX:
753                 mib[1] = KERN_NGROUPS;
754                 break;
755
756         case IBCS2_SC_OPEN_MAX:
757             {
758                 caddr_t sg = stackgap_init();
759
760                 SCARG(&ga, which) = RLIMIT_NOFILE;
761                 SCARG(&ga, rlp) = stackgap_alloc(&sg, sizeof(struct rlimit *));
762                 if ((error = getrlimit(&ga)) != 0)
763                         return error;
764                 uap->lmsg.u.ms_result = SCARG(&ga, rlp)->rlim_cur;
765                 return 0;
766             }
767                 
768         case IBCS2_SC_JOB_CONTROL:
769                 mib[1] = KERN_JOB_CONTROL;
770                 break;
771                 
772         case IBCS2_SC_SAVED_IDS:
773                 mib[1] = KERN_SAVED_IDS;
774                 break;
775                 
776         case IBCS2_SC_VERSION:
777                 mib[1] = KERN_POSIX1;
778                 break;
779                 
780         case IBCS2_SC_PASS_MAX:
781                 uap->lmsg.u.ms_result = 128;            /* XXX - should we create PASS_MAX ? */
782                 return 0;
783
784         case IBCS2_SC_XOPEN_VERSION:
785                 uap->lmsg.u.ms_result = 2;              /* XXX: What should that be? */
786                 return 0;
787                 
788         default:
789                 return EINVAL;
790         }
791
792         mib[0] = CTL_KERN;
793         len = sizeof(value);
794         SCARG(&sa, name) = mib;
795         SCARG(&sa, namelen) = 2;
796         SCARG(&sa, old) = &value;
797         SCARG(&sa, oldlenp) = &len;
798         SCARG(&sa, new) = NULL;
799         SCARG(&sa, newlen) = 0;
800         if ((error = __sysctl(&sa)) != 0)
801                 return error;
802         uap->lmsg.u.ms_result = value;
803         return 0;
804 }
805
806 int
807 ibcs2_alarm(struct ibcs2_alarm_args *uap)
808 {
809         int error;
810         struct itimerval *itp, *oitp;
811         struct setitimer_args sa;
812         caddr_t sg = stackgap_init();
813
814         itp = stackgap_alloc(&sg, sizeof(*itp));
815         oitp = stackgap_alloc(&sg, sizeof(*oitp));
816         timevalclear(&itp->it_interval);
817         itp->it_value.tv_sec = SCARG(uap, sec);
818         itp->it_value.tv_usec = 0;
819
820         SCARG(&sa, which) = ITIMER_REAL;
821         SCARG(&sa, itv) = itp;
822         SCARG(&sa, oitv) = oitp;
823         error = setitimer(&sa);
824         if (error)
825                 return error;
826         if (oitp->it_value.tv_usec)
827                 oitp->it_value.tv_sec++;
828         uap->lmsg.u.ms_result = oitp->it_value.tv_sec;
829         return 0;
830 }
831
832 int
833 ibcs2_times(struct ibcs2_times_args *uap)
834 {
835         int error;
836         struct getrusage_args ga;
837         struct tms tms;
838         struct timeval t;
839         caddr_t sg = stackgap_init();
840         struct rusage *ru = stackgap_alloc(&sg, sizeof(*ru));
841 #define CONVTCK(r)      (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
842
843         SCARG(&ga, who) = RUSAGE_SELF;
844         SCARG(&ga, rusage) = ru;
845         error = getrusage(&ga);
846         if (error)
847                 return error;
848         tms.tms_utime = CONVTCK(ru->ru_utime);
849         tms.tms_stime = CONVTCK(ru->ru_stime);
850
851         SCARG(&ga, who) = RUSAGE_CHILDREN;
852         error = getrusage(&ga);
853         if (error)
854                 return error;
855         tms.tms_cutime = CONVTCK(ru->ru_utime);
856         tms.tms_cstime = CONVTCK(ru->ru_stime);
857
858         microtime(&t);
859         uap->lmsg.u.ms_result = CONVTCK(t);
860         
861         return copyout((caddr_t)&tms, (caddr_t)SCARG(uap, tp),
862                        sizeof(struct tms));
863 }
864
865 int
866 ibcs2_stime(struct ibcs2_stime_args *uap)
867 {
868         int error;
869         struct settimeofday_args sa;
870         caddr_t sg = stackgap_init();
871
872         SCARG(&sa, tv) = stackgap_alloc(&sg, sizeof(*SCARG(&sa, tv)));
873         SCARG(&sa, tzp) = NULL;
874         if ((error = copyin((caddr_t)SCARG(uap, timep),
875                            &(SCARG(&sa, tv)->tv_sec), sizeof(long))) != 0)
876                 return error;
877         SCARG(&sa, tv)->tv_usec = 0;
878         if ((error = settimeofday(&sa)) != 0)
879                 return EPERM;
880         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
881         return 0;
882 }
883
884 int
885 ibcs2_utime(struct ibcs2_utime_args *uap)
886 {
887         int error;
888         struct utimes_args sa;
889         struct timeval *tp;
890         caddr_t sg = stackgap_init();
891
892         CHECKALTEXIST(&sg, SCARG(uap, path));
893         SCARG(&sa, path) = SCARG(uap, path);
894         if (SCARG(uap, buf)) {
895                 struct ibcs2_utimbuf ubuf;
896
897                 if ((error = copyin((caddr_t)SCARG(uap, buf), (caddr_t)&ubuf,
898                                    sizeof(ubuf))) != 0)
899                         return error;
900                 SCARG(&sa, tptr) = stackgap_alloc(&sg,
901                                                   2 * sizeof(struct timeval *));
902                 tp = (struct timeval *)SCARG(&sa, tptr);
903                 tp->tv_sec = ubuf.actime;
904                 tp->tv_usec = 0;
905                 tp++;
906                 tp->tv_sec = ubuf.modtime;
907                 tp->tv_usec = 0;
908         } else
909                 SCARG(&sa, tptr) = NULL;
910         error = utimes(&sa);
911         uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
912         return(error);
913 }
914
915 int
916 ibcs2_nice(struct ibcs2_nice_args *uap)
917 {
918         struct proc *p = curproc;
919         int error;
920         struct setpriority_args sa;
921
922         SCARG(&sa, which) = PRIO_PROCESS;
923         SCARG(&sa, who) = 0;
924         SCARG(&sa, prio) = p->p_nice + SCARG(uap, incr);
925         if ((error = setpriority(&sa)) != 0)
926                 return EPERM;
927         uap->lmsg.u.ms_result = p->p_nice;
928         return 0;
929 }
930
931 /*
932  * iBCS2 getpgrp, setpgrp, setsid, and setpgid
933  */
934
935 int
936 ibcs2_pgrpsys(struct ibcs2_pgrpsys_args *uap)
937 {
938         struct proc *p = curproc;
939
940         switch (SCARG(uap, type)) {
941         case 0:                 /* getpgrp */
942                 uap->lmsg.u.ms_result = p->p_pgrp->pg_id;
943                 return 0;
944
945         case 1:                 /* setpgrp */
946             {
947                 struct setpgid_args sa;
948
949                 SCARG(&sa, pid) = 0;
950                 SCARG(&sa, pgid) = 0;
951                 setpgid(&sa);
952                 uap->lmsg.u.ms_result = p->p_pgrp->pg_id;
953                 return 0;
954             }
955
956         case 2:                 /* setpgid */
957             {
958                 struct setpgid_args sa;
959                 int error;
960
961                 SCARG(&sa, pid) = SCARG(uap, pid);
962                 SCARG(&sa, pgid) = SCARG(uap, pgid);
963                 error = setpgid(&sa);
964                 uap->lmsg.u.ms_result = sa.lmsg.u.ms_result;
965                 return(error);
966             }
967
968         case 3:                 /* setsid */
969             {
970                 struct setsid_args sida;
971                 int error;
972
973                 error = setsid(&sida);
974                 uap->lmsg.u.ms_result = sida.lmsg.u.ms_result;
975                 return(error);
976             }
977
978         default:
979                 return EINVAL;
980         }
981 }
982
983 /*
984  * XXX - need to check for nested calls
985  */
986
987 int
988 ibcs2_plock(struct ibcs2_plock_args *uap)
989 {
990         int error;
991 #define IBCS2_UNLOCK    0
992 #define IBCS2_PROCLOCK  1
993 #define IBCS2_TEXTLOCK  2
994 #define IBCS2_DATALOCK  4
995
996         
997         if ((error = suser(curthread)) != 0)
998                 return EPERM;
999         switch(SCARG(uap, cmd)) {
1000         case IBCS2_UNLOCK:
1001         case IBCS2_PROCLOCK:
1002         case IBCS2_TEXTLOCK:
1003         case IBCS2_DATALOCK:
1004                 return 0;       /* XXX - TODO */
1005         }
1006         return EINVAL;
1007 }
1008
1009 int
1010 ibcs2_uadmin(struct ibcs2_uadmin_args *uap)
1011 {
1012 #define SCO_A_REBOOT        1
1013 #define SCO_A_SHUTDOWN      2
1014 #define SCO_A_REMOUNT       4
1015 #define SCO_A_CLOCK         8
1016 #define SCO_A_SETCONFIG     128
1017 #define SCO_A_GETDEV        130
1018
1019 #define SCO_AD_HALT         0
1020 #define SCO_AD_BOOT         1
1021 #define SCO_AD_IBOOT        2
1022 #define SCO_AD_PWRDOWN      3
1023 #define SCO_AD_PWRNAP       4
1024
1025 #define SCO_AD_PANICBOOT    1
1026
1027 #define SCO_AD_GETBMAJ      0
1028 #define SCO_AD_GETCMAJ      1
1029
1030         if (suser(curthread))
1031                 return EPERM;
1032
1033         switch(SCARG(uap, cmd)) {
1034         case SCO_A_REBOOT:
1035         case SCO_A_SHUTDOWN:
1036                 switch(SCARG(uap, func)) {
1037                         struct reboot_args r;
1038                 case SCO_AD_HALT:
1039                 case SCO_AD_PWRDOWN:
1040                 case SCO_AD_PWRNAP:
1041                         r.opt = RB_HALT;
1042                         reboot(&r);
1043                 case SCO_AD_BOOT:
1044                 case SCO_AD_IBOOT:
1045                         r.opt = RB_AUTOBOOT;
1046                         reboot(&r);
1047                 }
1048                 return EINVAL;
1049         case SCO_A_REMOUNT:
1050         case SCO_A_CLOCK:
1051         case SCO_A_SETCONFIG:
1052                 return 0;
1053         case SCO_A_GETDEV:
1054                 return EINVAL;  /* XXX - TODO */
1055         }
1056         return EINVAL;
1057 }
1058
1059 int
1060 ibcs2_sysfs(struct ibcs2_sysfs_args *uap)
1061 {
1062 #define IBCS2_GETFSIND        1
1063 #define IBCS2_GETFSTYP        2
1064 #define IBCS2_GETNFSTYP       3
1065
1066         switch(SCARG(uap, cmd)) {
1067         case IBCS2_GETFSIND:
1068         case IBCS2_GETFSTYP:
1069         case IBCS2_GETNFSTYP:
1070                 break;
1071         }
1072         return EINVAL;          /* XXX - TODO */
1073 }
1074
1075 int
1076 ibcs2_unlink(struct ibcs2_unlink_args *uap)
1077 {
1078         caddr_t sg = stackgap_init();
1079
1080         CHECKALTEXIST(&sg, SCARG(uap, path));
1081         return unlink((struct unlink_args *)uap);
1082 }
1083
1084 int
1085 ibcs2_chdir(struct ibcs2_chdir_args *uap)
1086 {
1087         caddr_t sg = stackgap_init();
1088
1089         CHECKALTEXIST(&sg, SCARG(uap, path));
1090         return chdir((struct chdir_args *)uap);
1091 }
1092
1093 int
1094 ibcs2_chmod(struct ibcs2_chmod_args *uap)
1095 {
1096         caddr_t sg = stackgap_init();
1097
1098         CHECKALTEXIST(&sg, SCARG(uap, path));
1099         return chmod((struct chmod_args *)uap);
1100 }
1101
1102 int
1103 ibcs2_chown(struct ibcs2_chown_args *uap)
1104 {
1105         caddr_t sg = stackgap_init();
1106
1107         CHECKALTEXIST(&sg, SCARG(uap, path));
1108         return chown((struct chown_args *)uap);
1109 }
1110
1111 int
1112 ibcs2_rmdir(struct ibcs2_rmdir_args *uap)
1113 {
1114         caddr_t sg = stackgap_init();
1115
1116         CHECKALTEXIST(&sg, SCARG(uap, path));
1117         return rmdir((struct rmdir_args *)uap);
1118 }
1119
1120 int
1121 ibcs2_mkdir(struct ibcs2_mkdir_args *uap)
1122 {
1123         caddr_t sg = stackgap_init();
1124
1125         CHECKALTCREAT(&sg, SCARG(uap, path));
1126         return mkdir((struct mkdir_args *)uap);
1127 }
1128
1129 int
1130 ibcs2_symlink(struct ibcs2_symlink_args *uap)
1131 {
1132         caddr_t sg = stackgap_init();
1133
1134         CHECKALTEXIST(&sg, SCARG(uap, path));
1135         CHECKALTCREAT(&sg, SCARG(uap, link));
1136         return symlink((struct symlink_args *)uap);
1137 }
1138
1139 int
1140 ibcs2_rename(struct ibcs2_rename_args *uap)
1141 {
1142         caddr_t sg = stackgap_init();
1143
1144         CHECKALTEXIST(&sg, SCARG(uap, from));
1145         CHECKALTCREAT(&sg, SCARG(uap, to));
1146         return rename((struct rename_args *)uap);
1147 }
1148
1149 int
1150 ibcs2_readlink(struct ibcs2_readlink_args *uap)
1151 {
1152         caddr_t sg = stackgap_init();
1153
1154         CHECKALTEXIST(&sg, SCARG(uap, path));
1155         return readlink((struct readlink_args *) uap);
1156 }