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