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