]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/ibcs2/ibcs2_misc.c
Update Bind to 9.8.5-P2
[FreeBSD/FreeBSD.git] / sys / i386 / ibcs2 / 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
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
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/systm.h>
60 #include <sys/capability.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/filedesc.h>
64 #include <sys/imgact.h>
65 #include <sys/kernel.h>
66 #include <sys/lock.h>
67 #include <sys/malloc.h>
68 #include <sys/file.h>                   /* Must come after sys/malloc.h */
69 #include <sys/mutex.h>
70 #include <sys/namei.h>
71 #include <sys/priv.h>
72 #include <sys/reboot.h>
73 #include <sys/resourcevar.h>
74 #include <sys/stat.h>
75 #include <sys/sysctl.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysproto.h>
78 #include <sys/time.h>
79 #include <sys/times.h>
80 #include <sys/vnode.h>
81 #include <sys/wait.h>
82
83 #include <machine/cpu.h>
84
85 #include <i386/ibcs2/ibcs2_dirent.h>
86 #include <i386/ibcs2/ibcs2_signal.h>
87 #include <i386/ibcs2/ibcs2_proto.h>
88 #include <i386/ibcs2/ibcs2_unistd.h>
89 #include <i386/ibcs2/ibcs2_util.h>
90 #include <i386/ibcs2/ibcs2_utime.h>
91 #include <i386/ibcs2/ibcs2_xenix.h>
92
93 #include <security/mac/mac_framework.h>
94
95 int
96 ibcs2_ulimit(td, uap)
97         struct thread *td;
98         struct ibcs2_ulimit_args *uap;
99 {
100         struct rlimit rl;
101         struct proc *p;
102         int error;
103 #define IBCS2_GETFSIZE          1
104 #define IBCS2_SETFSIZE          2
105 #define IBCS2_GETPSIZE          3
106 #define IBCS2_GETDTABLESIZE     4
107
108         p = td->td_proc;
109         switch (uap->cmd) {
110         case IBCS2_GETFSIZE:
111                 PROC_LOCK(p);
112                 td->td_retval[0] = lim_cur(p, RLIMIT_FSIZE);
113                 PROC_UNLOCK(p);
114                 if (td->td_retval[0] == -1)
115                         td->td_retval[0] = 0x7fffffff;
116                 return 0;
117         case IBCS2_SETFSIZE:
118                 PROC_LOCK(p);
119                 rl.rlim_max = lim_max(p, RLIMIT_FSIZE);
120                 PROC_UNLOCK(p);
121                 rl.rlim_cur = uap->newlimit;
122                 error = kern_setrlimit(td, RLIMIT_FSIZE, &rl);
123                 if (!error) {
124                         PROC_LOCK(p);
125                         td->td_retval[0] = lim_cur(p, RLIMIT_FSIZE);
126                         PROC_UNLOCK(p);
127                 } else {
128                         DPRINTF(("failed "));
129                 }
130                 return error;
131         case IBCS2_GETPSIZE:
132                 PROC_LOCK(p);
133                 td->td_retval[0] = lim_cur(p, RLIMIT_RSS); /* XXX */
134                 PROC_UNLOCK(p);
135                 return 0;
136         case IBCS2_GETDTABLESIZE:
137                 uap->cmd = IBCS2_SC_OPEN_MAX;
138                 return ibcs2_sysconf(td, (struct ibcs2_sysconf_args *)uap);
139         default:
140                 return ENOSYS;
141         }
142 }
143
144 #define IBCS2_WSTOPPED       0177
145 #define IBCS2_STOPCODE(sig)  ((sig) << 8 | IBCS2_WSTOPPED)
146 int
147 ibcs2_wait(td, uap)
148         struct thread *td;
149         struct ibcs2_wait_args *uap;
150 {
151         int error, options, status;
152         int *statusp;
153         pid_t pid;
154         struct trapframe *tf = td->td_frame;
155         
156         if ((tf->tf_eflags & (PSL_Z|PSL_PF|PSL_N|PSL_V))
157             == (PSL_Z|PSL_PF|PSL_N|PSL_V)) {
158                 /* waitpid */
159                 pid = uap->a1;
160                 statusp = (int *)uap->a2;
161                 options = uap->a3;
162         } else {
163                 /* wait */
164                 pid = WAIT_ANY;
165                 statusp = (int *)uap->a1;
166                 options = 0;
167         }
168         error = kern_wait(td, pid, &status, options, NULL);
169         if (error)
170                 return error;
171         if (statusp) {
172                 /*
173                  * Convert status/signal result.
174                  */
175                 if (WIFSTOPPED(status)) {
176                         if (WSTOPSIG(status) <= 0 ||
177                             WSTOPSIG(status) > IBCS2_SIGTBLSZ)
178                                 return (EINVAL);
179                         status =
180                           IBCS2_STOPCODE(bsd_to_ibcs2_sig[_SIG_IDX(WSTOPSIG(status))]);
181                 } else if (WIFSIGNALED(status)) {
182                         if (WTERMSIG(status) <= 0 ||
183                             WTERMSIG(status) > IBCS2_SIGTBLSZ)
184                                 return (EINVAL);
185                         status = bsd_to_ibcs2_sig[_SIG_IDX(WTERMSIG(status))];
186                 }
187                 /* else exit status -- identical */
188
189                 /* record result/status */
190                 td->td_retval[1] = status;
191                 return copyout(&status, statusp, sizeof(status));
192         }
193
194         return 0;
195 }
196
197 int
198 ibcs2_execv(td, uap)
199         struct thread *td;
200         struct ibcs2_execv_args *uap;
201 {
202         struct image_args eargs;
203         char *path;
204         int error;
205
206         CHECKALTEXIST(td, uap->path, &path);
207
208         error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp, NULL);
209         free(path, M_TEMP);
210         if (error == 0)
211                 error = kern_execve(td, &eargs, NULL);
212         return (error);
213 }
214
215 int
216 ibcs2_execve(td, uap) 
217         struct thread *td;
218         struct ibcs2_execve_args *uap;
219 {
220         struct image_args eargs;
221         char *path;
222         int error;
223
224         CHECKALTEXIST(td, uap->path, &path);
225
226         error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, uap->argp,
227             uap->envp);
228         free(path, M_TEMP);
229         if (error == 0)
230                 error = kern_execve(td, &eargs, NULL);
231         return (error);
232 }
233
234 int
235 ibcs2_umount(td, uap)
236         struct thread *td;
237         struct ibcs2_umount_args *uap;
238 {
239         struct unmount_args um;
240
241         um.path = uap->name;
242         um.flags = 0;
243         return sys_unmount(td, &um);
244 }
245
246 int
247 ibcs2_mount(td, uap)
248         struct thread *td;
249         struct ibcs2_mount_args *uap;
250 {
251 #ifdef notyet
252         int oflags = uap->flags, nflags, error;
253         char fsname[MFSNAMELEN];
254
255         if (oflags & (IBCS2_MS_NOSUB | IBCS2_MS_SYS5))
256                 return (EINVAL);
257         if ((oflags & IBCS2_MS_NEWTYPE) == 0)
258                 return (EINVAL);
259         nflags = 0;
260         if (oflags & IBCS2_MS_RDONLY)
261                 nflags |= MNT_RDONLY;
262         if (oflags & IBCS2_MS_NOSUID)
263                 nflags |= MNT_NOSUID;
264         if (oflags & IBCS2_MS_REMOUNT)
265                 nflags |= MNT_UPDATE;
266         uap->flags = nflags;
267
268         if (error = copyinstr((caddr_t)uap->type, fsname, sizeof fsname,
269                               (u_int *)0))
270                 return (error);
271
272         if (strcmp(fsname, "4.2") == 0) {
273                 uap->type = (caddr_t)STACK_ALLOC();
274                 if (error = copyout("ufs", uap->type, sizeof("ufs")))
275                         return (error);
276         } else if (strcmp(fsname, "nfs") == 0) {
277                 struct ibcs2_nfs_args sna;
278                 struct sockaddr_in sain;
279                 struct nfs_args na;
280                 struct sockaddr sa;
281
282                 if (error = copyin(uap->data, &sna, sizeof sna))
283                         return (error);
284                 if (error = copyin(sna.addr, &sain, sizeof sain))
285                         return (error);
286                 bcopy(&sain, &sa, sizeof sa);
287                 sa.sa_len = sizeof(sain);
288                 uap->data = (caddr_t)STACK_ALLOC();
289                 na.addr = (struct sockaddr *)((int)uap->data + sizeof na);
290                 na.sotype = SOCK_DGRAM;
291                 na.proto = IPPROTO_UDP;
292                 na.fh = (nfsv2fh_t *)sna.fh;
293                 na.flags = sna.flags;
294                 na.wsize = sna.wsize;
295                 na.rsize = sna.rsize;
296                 na.timeo = sna.timeo;
297                 na.retrans = sna.retrans;
298                 na.hostname = sna.hostname;
299
300                 if (error = copyout(&sa, na.addr, sizeof sa))
301                         return (error);
302                 if (error = copyout(&na, uap->data, sizeof na))
303                         return (error);
304         }
305         return (mount(td, uap));
306 #else
307         return EINVAL;
308 #endif
309 }
310
311 /*
312  * Read iBCS2-style directory entries.  We suck them into kernel space so
313  * that they can be massaged before being copied out to user code.  Like
314  * SunOS, we squish out `empty' entries.
315  *
316  * This is quite ugly, but what do you expect from compatibility code?
317  */
318
319 int
320 ibcs2_getdents(td, uap)
321         struct thread *td;
322         register struct ibcs2_getdents_args *uap;
323 {
324         register struct vnode *vp;
325         register caddr_t inp, buf;      /* BSD-format */
326         register int len, reclen;       /* BSD-format */
327         register caddr_t outp;          /* iBCS2-format */
328         register int resid;             /* iBCS2-format */
329         struct file *fp;
330         struct uio auio;
331         struct iovec aiov;
332         struct ibcs2_dirent idb;
333         off_t off;                      /* true file offset */
334         int buflen, error, eofflag;
335         u_long *cookies = NULL, *cookiep;
336         int ncookies;
337 #define BSD_DIRENT(cp)          ((struct dirent *)(cp))
338 #define IBCS2_RECLEN(reclen)    (reclen + sizeof(u_short))
339
340         if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ, &fp)) != 0)
341                 return (error);
342         if ((fp->f_flag & FREAD) == 0) {
343                 fdrop(fp, td);
344                 return (EBADF);
345         }
346         vp = fp->f_vnode;
347         if (vp->v_type != VDIR) {       /* XXX  vnode readdir op should do this */
348                 fdrop(fp, td);
349                 return (EINVAL);
350         }
351
352         off = fp->f_offset;
353 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
354         buflen = max(DIRBLKSIZ, uap->nbytes);
355         buflen = min(buflen, MAXBSIZE);
356         buf = malloc(buflen, M_TEMP, M_WAITOK);
357         vn_lock(vp, LK_SHARED | LK_RETRY);
358 again:
359         aiov.iov_base = buf;
360         aiov.iov_len = buflen;
361         auio.uio_iov = &aiov;
362         auio.uio_iovcnt = 1;
363         auio.uio_rw = UIO_READ;
364         auio.uio_segflg = UIO_SYSSPACE;
365         auio.uio_td = td;
366         auio.uio_resid = buflen;
367         auio.uio_offset = off;
368
369         if (cookies) {
370                 free(cookies, M_TEMP);
371                 cookies = NULL;
372         }
373
374 #ifdef MAC
375         error = mac_vnode_check_readdir(td->td_ucred, vp);
376         if (error)
377                 goto out;
378 #endif
379
380         /*
381          * First we read into the malloc'ed buffer, then
382          * we massage it into user space, one record at a time.
383          */
384         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0)
385                 goto out;
386         inp = buf;
387         outp = uap->buf;
388         resid = uap->nbytes;
389         if ((len = buflen - auio.uio_resid) <= 0)
390                 goto eof;
391
392         cookiep = cookies;
393
394         if (cookies) {
395                 /*
396                  * When using cookies, the vfs has the option of reading from
397                  * a different offset than that supplied (UFS truncates the
398                  * offset to a block boundary to make sure that it never reads
399                  * partway through a directory entry, even if the directory
400                  * has been compacted).
401                  */
402                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
403                         len -= BSD_DIRENT(inp)->d_reclen;
404                         inp += BSD_DIRENT(inp)->d_reclen;
405                         cookiep++;
406                         ncookies--;
407                 }
408         }
409
410         for (; len > 0; len -= reclen) {
411                 if (cookiep && ncookies == 0)
412                         break;
413                 reclen = BSD_DIRENT(inp)->d_reclen;
414                 if (reclen & 3) {
415                         printf("ibcs2_getdents: reclen=%d\n", reclen);
416                         error = EFAULT;
417                         goto out;
418                 }
419                 if (BSD_DIRENT(inp)->d_fileno == 0) {
420                         inp += reclen;  /* it is a hole; squish it out */
421                         if (cookiep) {
422                                 off = *cookiep++;
423                                 ncookies--;
424                         } else
425                                 off += reclen;
426                         continue;
427                 }
428                 if (reclen > len || resid < IBCS2_RECLEN(reclen)) {
429                         /* entry too big for buffer, so just stop */
430                         outp++;
431                         break;
432                 }
433                 /*
434                  * Massage in place to make an iBCS2-shaped dirent (otherwise
435                  * we have to worry about touching user memory outside of
436                  * the copyout() call).
437                  */
438                 idb.d_ino = (ibcs2_ino_t)BSD_DIRENT(inp)->d_fileno;
439                 idb.d_off = (ibcs2_off_t)off;
440                 idb.d_reclen = (u_short)IBCS2_RECLEN(reclen);
441                 if ((error = copyout((caddr_t)&idb, outp, 10)) != 0 ||
442                     (error = copyout(BSD_DIRENT(inp)->d_name, outp + 10,
443                                      BSD_DIRENT(inp)->d_namlen + 1)) != 0)
444                         goto out;
445                 /* advance past this real entry */
446                 if (cookiep) {
447                         off = *cookiep++;
448                         ncookies--;
449                 } else
450                         off += reclen;
451                 inp += reclen;
452                 /* advance output past iBCS2-shaped entry */
453                 outp += IBCS2_RECLEN(reclen);
454                 resid -= IBCS2_RECLEN(reclen);
455         }
456         /* if we squished out the whole block, try again */
457         if (outp == uap->buf)
458                 goto again;
459         fp->f_offset = off;             /* update the vnode offset */
460 eof:
461         td->td_retval[0] = uap->nbytes - resid;
462 out:
463         VOP_UNLOCK(vp, 0);
464         fdrop(fp, td);
465         if (cookies)
466                 free(cookies, M_TEMP);
467         free(buf, M_TEMP);
468         return (error);
469 }
470
471 int
472 ibcs2_read(td, uap)
473         struct thread *td;
474         struct ibcs2_read_args *uap;
475 {
476         register struct vnode *vp;
477         register caddr_t inp, buf;      /* BSD-format */
478         register int len, reclen;       /* BSD-format */
479         register caddr_t outp;          /* iBCS2-format */
480         register int resid;             /* iBCS2-format */
481         struct file *fp;
482         struct uio auio;
483         struct iovec aiov;
484         struct ibcs2_direct {
485                 ibcs2_ino_t ino;
486                 char name[14];
487         } idb;
488         off_t off;                      /* true file offset */
489         int buflen, error, eofflag, size;
490         u_long *cookies = NULL, *cookiep;
491         int ncookies;
492
493         if ((error = getvnode(td->td_proc->p_fd, uap->fd, CAP_READ,
494             &fp)) != 0) {
495                 if (error == EINVAL)
496                         return sys_read(td, (struct read_args *)uap);
497                 else
498                         return error;
499         }
500         if ((fp->f_flag & FREAD) == 0) {
501                 fdrop(fp, td);
502                 return (EBADF);
503         }
504         vp = fp->f_vnode;
505         if (vp->v_type != VDIR) {
506                 fdrop(fp, td);
507                 return sys_read(td, (struct read_args *)uap);
508         }
509
510         off = fp->f_offset;
511
512         DPRINTF(("ibcs2_read: read directory\n"));
513
514         buflen = max(DIRBLKSIZ, uap->nbytes);
515         buflen = min(buflen, MAXBSIZE);
516         buf = malloc(buflen, M_TEMP, M_WAITOK);
517         vn_lock(vp, LK_SHARED | LK_RETRY);
518 again:
519         aiov.iov_base = buf;
520         aiov.iov_len = buflen;
521         auio.uio_iov = &aiov;
522         auio.uio_iovcnt = 1;
523         auio.uio_rw = UIO_READ;
524         auio.uio_segflg = UIO_SYSSPACE;
525         auio.uio_td = td;
526         auio.uio_resid = buflen;
527         auio.uio_offset = off;
528
529         if (cookies) {
530                 free(cookies, M_TEMP);
531                 cookies = NULL;
532         }
533
534 #ifdef MAC
535         error = mac_vnode_check_readdir(td->td_ucred, vp);
536         if (error)
537                 goto out;
538 #endif
539
540         /*
541          * First we read into the malloc'ed buffer, then
542          * we massage it into user space, one record at a time.
543          */
544         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0) {
545                 DPRINTF(("VOP_READDIR failed: %d\n", error));
546                 goto out;
547         }
548         inp = buf;
549         outp = uap->buf;
550         resid = uap->nbytes;
551         if ((len = buflen - auio.uio_resid) <= 0)
552                 goto eof;
553
554         cookiep = cookies;
555
556         if (cookies) {
557                 /*
558                  * When using cookies, the vfs has the option of reading from
559                  * a different offset than that supplied (UFS truncates the
560                  * offset to a block boundary to make sure that it never reads
561                  * partway through a directory entry, even if the directory
562                  * has been compacted).
563                  */
564                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
565                         len -= BSD_DIRENT(inp)->d_reclen;
566                         inp += BSD_DIRENT(inp)->d_reclen;
567                         cookiep++;
568                         ncookies--;
569                 }
570         }
571
572         for (; len > 0 && resid > 0; len -= reclen) {
573                 if (cookiep && ncookies == 0)
574                         break;
575                 reclen = BSD_DIRENT(inp)->d_reclen;
576                 if (reclen & 3) {
577                         printf("ibcs2_read: reclen=%d\n", reclen);
578                         error = EFAULT;
579                         goto out;
580                 }
581                 if (BSD_DIRENT(inp)->d_fileno == 0) {
582                         inp += reclen;  /* it is a hole; squish it out */
583                         if (cookiep) {
584                                 off = *cookiep++;
585                                 ncookies--;
586                         } else
587                                 off += reclen;
588                         continue;
589                 }
590                 if (reclen > len || resid < sizeof(struct ibcs2_direct)) {
591                         /* entry too big for buffer, so just stop */
592                         outp++;
593                         break;
594                 }
595                 /*
596                  * Massage in place to make an iBCS2-shaped dirent (otherwise
597                  * we have to worry about touching user memory outside of
598                  * the copyout() call).
599                  *
600                  * TODO: if length(filename) > 14, then break filename into
601                  * multiple entries and set inode = 0xffff except last
602                  */
603                 idb.ino = (BSD_DIRENT(inp)->d_fileno > 0xfffe) ? 0xfffe :
604                         BSD_DIRENT(inp)->d_fileno;
605                 (void)copystr(BSD_DIRENT(inp)->d_name, idb.name, 14, &size);
606                 bzero(idb.name + size, 14 - size);
607                 if ((error = copyout(&idb, outp, sizeof(struct ibcs2_direct))) != 0)
608                         goto out;
609                 /* advance past this real entry */
610                 if (cookiep) {
611                         off = *cookiep++;
612                         ncookies--;
613                 } else
614                         off += reclen;
615                 inp += reclen;
616                 /* advance output past iBCS2-shaped entry */
617                 outp += sizeof(struct ibcs2_direct);
618                 resid -= sizeof(struct ibcs2_direct);
619         }
620         /* if we squished out the whole block, try again */
621         if (outp == uap->buf)
622                 goto again;
623         fp->f_offset = off;             /* update the vnode offset */
624 eof:
625         td->td_retval[0] = uap->nbytes - resid;
626 out:
627         VOP_UNLOCK(vp, 0);
628         fdrop(fp, td);
629         if (cookies)
630                 free(cookies, M_TEMP);
631         free(buf, M_TEMP);
632         return (error);
633 }
634
635 int
636 ibcs2_mknod(td, uap)
637         struct thread *td;
638         struct ibcs2_mknod_args *uap;
639 {
640         char *path;
641         int error;
642
643         CHECKALTCREAT(td, uap->path, &path);
644         if (S_ISFIFO(uap->mode))
645                 error = kern_mkfifo(td, path, UIO_SYSSPACE, uap->mode);
646         else
647                 error = kern_mknod(td, path, UIO_SYSSPACE, uap->mode, uap->dev);
648         free(path, M_TEMP);
649         return (error);
650 }
651
652 int
653 ibcs2_getgroups(td, uap)
654         struct thread *td;
655         struct ibcs2_getgroups_args *uap;
656 {
657         ibcs2_gid_t *iset;
658         gid_t *gp;
659         u_int i, ngrp;
660         int error;
661
662         if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
663                 if (uap->gidsetsize == 0)
664                         ngrp = 0;
665                 else
666                         return (EINVAL);
667         } else
668                 ngrp = td->td_ucred->cr_ngroups;
669         gp = malloc(ngrp * sizeof(*gp), M_TEMP, M_WAITOK);
670         error = kern_getgroups(td, &ngrp, gp);
671         if (error)
672                 goto out;
673         if (uap->gidsetsize > 0) {
674                 iset = malloc(ngrp * sizeof(*iset), M_TEMP, M_WAITOK);
675                 for (i = 0; i < ngrp; i++)
676                         iset[i] = (ibcs2_gid_t)gp[i];
677                 error = copyout(iset, uap->gidset, ngrp * sizeof(ibcs2_gid_t));
678                 free(iset, M_TEMP);
679         }
680         if (error == 0)
681                 td->td_retval[0] = ngrp;
682 out:
683         free(gp, M_TEMP);
684         return (error);
685 }
686
687 int
688 ibcs2_setgroups(td, uap)
689         struct thread *td;
690         struct ibcs2_setgroups_args *uap;
691 {
692         ibcs2_gid_t *iset;
693         gid_t *gp;
694         int error, i;
695
696         if (uap->gidsetsize < 0 || uap->gidsetsize > ngroups_max + 1)
697                 return (EINVAL);
698         if (uap->gidsetsize && uap->gidset == NULL)
699                 return (EINVAL);
700         gp = malloc(uap->gidsetsize * sizeof(*gp), M_TEMP, M_WAITOK);
701         if (uap->gidsetsize) {
702                 iset = malloc(uap->gidsetsize * sizeof(*iset), M_TEMP, M_WAITOK);
703                 error = copyin(uap->gidset, iset, sizeof(ibcs2_gid_t) *
704                     uap->gidsetsize);
705                 if (error) {
706                         free(iset, M_TEMP);
707                         goto out;
708                 }
709                 for (i = 0; i < uap->gidsetsize; i++)
710                         gp[i] = (gid_t)iset[i];
711         }
712
713         error = kern_setgroups(td, uap->gidsetsize, gp);
714 out:
715         free(gp, M_TEMP);
716         return (error);
717 }
718
719 int
720 ibcs2_setuid(td, uap)
721         struct thread *td;
722         struct ibcs2_setuid_args *uap;
723 {
724         struct setuid_args sa;
725
726         sa.uid = (uid_t)uap->uid;
727         return sys_setuid(td, &sa);
728 }
729
730 int
731 ibcs2_setgid(td, uap)
732         struct thread *td;
733         struct ibcs2_setgid_args *uap;
734 {
735         struct setgid_args sa;
736
737         sa.gid = (gid_t)uap->gid;
738         return sys_setgid(td, &sa);
739 }
740
741 int
742 ibcs2_time(td, uap)
743         struct thread *td;
744         struct ibcs2_time_args *uap;
745 {
746         struct timeval tv;
747
748         microtime(&tv);
749         td->td_retval[0] = tv.tv_sec;
750         if (uap->tp)
751                 return copyout((caddr_t)&tv.tv_sec, (caddr_t)uap->tp,
752                                sizeof(ibcs2_time_t));
753         else
754                 return 0;
755 }
756
757 int
758 ibcs2_pathconf(td, uap)
759         struct thread *td;
760         struct ibcs2_pathconf_args *uap;
761 {
762         char *path;
763         int error;
764
765         CHECKALTEXIST(td, uap->path, &path);
766         uap->name++;    /* iBCS2 _PC_* defines are offset by one */
767         error = kern_pathconf(td, path, UIO_SYSSPACE, uap->name, FOLLOW);
768         free(path, M_TEMP);
769         return (error);
770 }
771
772 int
773 ibcs2_fpathconf(td, uap)
774         struct thread *td;
775         struct ibcs2_fpathconf_args *uap;
776 {
777         uap->name++;    /* iBCS2 _PC_* defines are offset by one */
778         return sys_fpathconf(td, (struct fpathconf_args *)uap);
779 }
780
781 int
782 ibcs2_sysconf(td, uap)
783         struct thread *td;
784         struct ibcs2_sysconf_args *uap;
785 {
786         int mib[2], value, len, error;
787         struct proc *p;
788
789         p = td->td_proc;
790         switch(uap->name) {
791         case IBCS2_SC_ARG_MAX:
792                 mib[1] = KERN_ARGMAX;
793                 break;
794
795         case IBCS2_SC_CHILD_MAX:
796                 PROC_LOCK(p);
797                 td->td_retval[0] = lim_cur(td->td_proc, RLIMIT_NPROC);
798                 PROC_UNLOCK(p);
799                 return 0;
800
801         case IBCS2_SC_CLK_TCK:
802                 td->td_retval[0] = hz;
803                 return 0;
804
805         case IBCS2_SC_NGROUPS_MAX:
806                 mib[1] = KERN_NGROUPS;
807                 break;
808
809         case IBCS2_SC_OPEN_MAX:
810                 PROC_LOCK(p);
811                 td->td_retval[0] = lim_cur(td->td_proc, RLIMIT_NOFILE);
812                 PROC_UNLOCK(p);
813                 return 0;
814                 
815         case IBCS2_SC_JOB_CONTROL:
816                 mib[1] = KERN_JOB_CONTROL;
817                 break;
818                 
819         case IBCS2_SC_SAVED_IDS:
820                 mib[1] = KERN_SAVED_IDS;
821                 break;
822                 
823         case IBCS2_SC_VERSION:
824                 mib[1] = KERN_POSIX1;
825                 break;
826                 
827         case IBCS2_SC_PASS_MAX:
828                 td->td_retval[0] = 128;         /* XXX - should we create PASS_MAX ? */
829                 return 0;
830
831         case IBCS2_SC_XOPEN_VERSION:
832                 td->td_retval[0] = 2;           /* XXX: What should that be? */
833                 return 0;
834                 
835         default:
836                 return EINVAL;
837         }
838
839         mib[0] = CTL_KERN;
840         len = sizeof(value);
841         error = kernel_sysctl(td, mib, 2, &value, &len, NULL, 0, NULL, 0);
842         if (error)
843                 return error;
844         td->td_retval[0] = value;
845         return 0;
846 }
847
848 int
849 ibcs2_alarm(td, uap)
850         struct thread *td;
851         struct ibcs2_alarm_args *uap;
852 {
853         struct itimerval itv, oitv;
854         int error;
855
856         timevalclear(&itv.it_interval);
857         itv.it_value.tv_sec = uap->sec;
858         itv.it_value.tv_usec = 0;
859         error = kern_setitimer(td, ITIMER_REAL, &itv, &oitv);
860         if (error)
861                 return (error);
862         if (oitv.it_value.tv_usec != 0)
863                 oitv.it_value.tv_sec++;
864         td->td_retval[0] = oitv.it_value.tv_sec;
865         return (0);
866 }
867
868 int
869 ibcs2_times(td, uap)
870         struct thread *td;
871         struct ibcs2_times_args *uap;
872 {
873         struct rusage ru;
874         struct timeval t;
875         struct tms tms;
876         int error;
877
878 #define CONVTCK(r)      (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
879
880         error = kern_getrusage(td, RUSAGE_SELF, &ru);
881         if (error)
882                 return (error);
883         tms.tms_utime = CONVTCK(ru.ru_utime);
884         tms.tms_stime = CONVTCK(ru.ru_stime);
885
886         error = kern_getrusage(td, RUSAGE_CHILDREN, &ru);
887         if (error)
888                 return (error);
889         tms.tms_cutime = CONVTCK(ru.ru_utime);
890         tms.tms_cstime = CONVTCK(ru.ru_stime);
891
892         microtime(&t);
893         td->td_retval[0] = CONVTCK(t);
894         
895         return (copyout(&tms, uap->tp, sizeof(struct tms)));
896 }
897
898 int
899 ibcs2_stime(td, uap)
900         struct thread *td;
901         struct ibcs2_stime_args *uap;
902 {
903         struct timeval tv;
904         long secs;
905         int error;
906
907         error = copyin(uap->timep, &secs, sizeof(long));
908         if (error)
909                 return (error);
910         tv.tv_sec = secs;
911         tv.tv_usec = 0;
912         error = kern_settimeofday(td, &tv, NULL);
913         if (error)
914                 error = EPERM;
915         return (error);
916 }
917
918 int
919 ibcs2_utime(td, uap)
920         struct thread *td;
921         struct ibcs2_utime_args *uap;
922 {
923         struct ibcs2_utimbuf ubuf;
924         struct timeval tbuf[2], *tp;
925         char *path;
926         int error;
927
928         if (uap->buf) {
929                 error = copyin(uap->buf, &ubuf, sizeof(ubuf));
930                 if (error)
931                         return (error);
932                 tbuf[0].tv_sec = ubuf.actime;
933                 tbuf[0].tv_usec = 0;
934                 tbuf[1].tv_sec = ubuf.modtime;
935                 tbuf[1].tv_usec = 0;
936                 tp = tbuf;
937         } else
938                 tp = NULL;
939
940         CHECKALTEXIST(td, uap->path, &path);
941         error = kern_utimes(td, path, UIO_SYSSPACE, tp, UIO_SYSSPACE);
942         free(path, M_TEMP);
943         return (error);
944 }
945
946 int
947 ibcs2_nice(td, uap)
948         struct thread *td;
949         struct ibcs2_nice_args *uap;
950 {
951         int error;
952         struct setpriority_args sa;
953
954         sa.which = PRIO_PROCESS;
955         sa.who = 0;
956         sa.prio = td->td_proc->p_nice + uap->incr;
957         if ((error = sys_setpriority(td, &sa)) != 0)
958                 return EPERM;
959         td->td_retval[0] = td->td_proc->p_nice;
960         return 0;
961 }
962
963 /*
964  * iBCS2 getpgrp, setpgrp, setsid, and setpgid
965  */
966
967 int
968 ibcs2_pgrpsys(td, uap)
969         struct thread *td;
970         struct ibcs2_pgrpsys_args *uap;
971 {
972         struct proc *p = td->td_proc;
973         switch (uap->type) {
974         case 0:                 /* getpgrp */
975                 PROC_LOCK(p);
976                 td->td_retval[0] = p->p_pgrp->pg_id;
977                 PROC_UNLOCK(p);
978                 return 0;
979
980         case 1:                 /* setpgrp */
981             {
982                 struct setpgid_args sa;
983
984                 sa.pid = 0;
985                 sa.pgid = 0;
986                 sys_setpgid(td, &sa);
987                 PROC_LOCK(p);
988                 td->td_retval[0] = p->p_pgrp->pg_id;
989                 PROC_UNLOCK(p);
990                 return 0;
991             }
992
993         case 2:                 /* setpgid */
994             {
995                 struct setpgid_args sa;
996
997                 sa.pid = uap->pid;
998                 sa.pgid = uap->pgid;
999                 return sys_setpgid(td, &sa);
1000             }
1001
1002         case 3:                 /* setsid */
1003                 return sys_setsid(td, NULL);
1004
1005         default:
1006                 return EINVAL;
1007         }
1008 }
1009
1010 /*
1011  * XXX - need to check for nested calls
1012  */
1013
1014 int
1015 ibcs2_plock(td, uap)
1016         struct thread *td;
1017         struct ibcs2_plock_args *uap;
1018 {
1019         int error;
1020 #define IBCS2_UNLOCK    0
1021 #define IBCS2_PROCLOCK  1
1022 #define IBCS2_TEXTLOCK  2
1023 #define IBCS2_DATALOCK  4
1024
1025         
1026         switch(uap->cmd) {
1027         case IBCS2_UNLOCK:
1028                 error = priv_check(td, PRIV_VM_MUNLOCK);
1029                 if (error)
1030                         return (error);
1031                 /* XXX - TODO */
1032                 return (0);
1033
1034         case IBCS2_PROCLOCK:
1035         case IBCS2_TEXTLOCK:
1036         case IBCS2_DATALOCK:
1037                 error = priv_check(td, PRIV_VM_MLOCK);
1038                 if (error)
1039                         return (error);
1040                 /* XXX - TODO */
1041                 return 0;
1042         }
1043         return EINVAL;
1044 }
1045
1046 int
1047 ibcs2_uadmin(td, uap)
1048         struct thread *td;
1049         struct ibcs2_uadmin_args *uap;
1050 {
1051 #define SCO_A_REBOOT        1
1052 #define SCO_A_SHUTDOWN      2
1053 #define SCO_A_REMOUNT       4
1054 #define SCO_A_CLOCK         8
1055 #define SCO_A_SETCONFIG     128
1056 #define SCO_A_GETDEV        130
1057
1058 #define SCO_AD_HALT         0
1059 #define SCO_AD_BOOT         1
1060 #define SCO_AD_IBOOT        2
1061 #define SCO_AD_PWRDOWN      3
1062 #define SCO_AD_PWRNAP       4
1063
1064 #define SCO_AD_PANICBOOT    1
1065
1066 #define SCO_AD_GETBMAJ      0
1067 #define SCO_AD_GETCMAJ      1
1068
1069         switch(uap->cmd) {
1070         case SCO_A_REBOOT:
1071         case SCO_A_SHUTDOWN:
1072                 switch(uap->func) {
1073                         struct reboot_args r;
1074                 case SCO_AD_HALT:
1075                 case SCO_AD_PWRDOWN:
1076                 case SCO_AD_PWRNAP:
1077                         r.opt = RB_HALT;
1078                         return (sys_reboot(td, &r));
1079                 case SCO_AD_BOOT:
1080                 case SCO_AD_IBOOT:
1081                         r.opt = RB_AUTOBOOT;
1082                         return (sys_reboot(td, &r));
1083                 }
1084                 return EINVAL;
1085         case SCO_A_REMOUNT:
1086         case SCO_A_CLOCK:
1087         case SCO_A_SETCONFIG:
1088                 return 0;
1089         case SCO_A_GETDEV:
1090                 return EINVAL;  /* XXX - TODO */
1091         }
1092         return EINVAL;
1093 }
1094
1095 int
1096 ibcs2_sysfs(td, uap)
1097         struct thread *td;
1098         struct ibcs2_sysfs_args *uap;
1099 {
1100 #define IBCS2_GETFSIND        1
1101 #define IBCS2_GETFSTYP        2
1102 #define IBCS2_GETNFSTYP       3
1103
1104         switch(uap->cmd) {
1105         case IBCS2_GETFSIND:
1106         case IBCS2_GETFSTYP:
1107         case IBCS2_GETNFSTYP:
1108                 break;
1109         }
1110         return EINVAL;          /* XXX - TODO */
1111 }
1112
1113 int
1114 ibcs2_unlink(td, uap)
1115         struct thread *td;
1116         struct ibcs2_unlink_args *uap;
1117 {
1118         char *path;
1119         int error;
1120
1121         CHECKALTEXIST(td, uap->path, &path);
1122         error = kern_unlink(td, path, UIO_SYSSPACE);
1123         free(path, M_TEMP);
1124         return (error);
1125 }
1126
1127 int
1128 ibcs2_chdir(td, uap)
1129         struct thread *td;
1130         struct ibcs2_chdir_args *uap;
1131 {
1132         char *path;
1133         int error;
1134
1135         CHECKALTEXIST(td, uap->path, &path);
1136         error = kern_chdir(td, path, UIO_SYSSPACE);
1137         free(path, M_TEMP);
1138         return (error);
1139 }
1140
1141 int
1142 ibcs2_chmod(td, uap)
1143         struct thread *td;
1144         struct ibcs2_chmod_args *uap;
1145 {
1146         char *path;
1147         int error;
1148
1149         CHECKALTEXIST(td, uap->path, &path);
1150         error = kern_chmod(td, path, UIO_SYSSPACE, uap->mode);
1151         free(path, M_TEMP);
1152         return (error);
1153 }
1154
1155 int
1156 ibcs2_chown(td, uap)
1157         struct thread *td;
1158         struct ibcs2_chown_args *uap;
1159 {
1160         char *path;
1161         int error;
1162
1163         CHECKALTEXIST(td, uap->path, &path);
1164         error = kern_chown(td, path, UIO_SYSSPACE, uap->uid, uap->gid);
1165         free(path, M_TEMP);
1166         return (error);
1167 }
1168
1169 int
1170 ibcs2_rmdir(td, uap)
1171         struct thread *td;
1172         struct ibcs2_rmdir_args *uap;
1173 {
1174         char *path;
1175         int error;
1176
1177         CHECKALTEXIST(td, uap->path, &path);
1178         error = kern_rmdir(td, path, UIO_SYSSPACE);
1179         free(path, M_TEMP);
1180         return (error);
1181 }
1182
1183 int
1184 ibcs2_mkdir(td, uap)
1185         struct thread *td;
1186         struct ibcs2_mkdir_args *uap;
1187 {
1188         char *path;
1189         int error;
1190
1191         CHECKALTEXIST(td, uap->path, &path);
1192         error = kern_mkdir(td, path, UIO_SYSSPACE, uap->mode);
1193         free(path, M_TEMP);
1194         return (error);
1195 }
1196
1197 int
1198 ibcs2_symlink(td, uap)
1199         struct thread *td;
1200         struct ibcs2_symlink_args *uap;
1201 {
1202         char *path, *link;
1203         int error;
1204
1205         CHECKALTEXIST(td, uap->path, &path);
1206
1207         /*
1208          * Have to expand CHECKALTCREAT() so that 'path' can be freed on
1209          * errors.
1210          */
1211         error = ibcs2_emul_find(td, uap->link, UIO_USERSPACE, &link, 1);
1212         if (link == NULL) {
1213                 free(path, M_TEMP);
1214                 return (error);
1215         }
1216         error = kern_symlink(td, path, link, UIO_SYSSPACE);
1217         free(path, M_TEMP);
1218         free(link, M_TEMP);
1219         return (error);
1220 }
1221
1222 int
1223 ibcs2_rename(td, uap)
1224         struct thread *td;
1225         struct ibcs2_rename_args *uap;
1226 {
1227         char *from, *to;
1228         int error;
1229
1230         CHECKALTEXIST(td, uap->from, &from);
1231
1232         /*
1233          * Have to expand CHECKALTCREAT() so that 'from' can be freed on
1234          * errors.
1235          */
1236         error = ibcs2_emul_find(td, uap->to, UIO_USERSPACE, &to, 1);
1237         if (to == NULL) {
1238                 free(from, M_TEMP);
1239                 return (error);
1240         }
1241         error = kern_rename(td, from, to, UIO_SYSSPACE);
1242         free(from, M_TEMP);
1243         free(to, M_TEMP);
1244         return (error);
1245 }
1246
1247 int
1248 ibcs2_readlink(td, uap)
1249         struct thread *td;
1250         struct ibcs2_readlink_args *uap;
1251 {
1252         char *path;
1253         int error;
1254
1255         CHECKALTEXIST(td, uap->path, &path);
1256         error = kern_readlink(td, path, UIO_SYSSPACE, uap->buf, UIO_USERSPACE,
1257                 uap->count);
1258         free(path, M_TEMP);
1259         return (error);
1260 }