]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/i386/ibcs2/ibcs2_misc.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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, vfslocked;
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,
341             CAP_READ | CAP_SEEK, &fp)) != 0)
342                 return (error);
343         if ((fp->f_flag & FREAD) == 0) {
344                 fdrop(fp, td);
345                 return (EBADF);
346         }
347         vp = fp->f_vnode;
348         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
349         if (vp->v_type != VDIR) {       /* XXX  vnode readdir op should do this */
350                 VFS_UNLOCK_GIANT(vfslocked);
351                 fdrop(fp, td);
352                 return (EINVAL);
353         }
354
355         off = fp->f_offset;
356 #define DIRBLKSIZ       512             /* XXX we used to use ufs's DIRBLKSIZ */
357         buflen = max(DIRBLKSIZ, uap->nbytes);
358         buflen = min(buflen, MAXBSIZE);
359         buf = malloc(buflen, M_TEMP, M_WAITOK);
360         vn_lock(vp, LK_SHARED | LK_RETRY);
361 again:
362         aiov.iov_base = buf;
363         aiov.iov_len = buflen;
364         auio.uio_iov = &aiov;
365         auio.uio_iovcnt = 1;
366         auio.uio_rw = UIO_READ;
367         auio.uio_segflg = UIO_SYSSPACE;
368         auio.uio_td = td;
369         auio.uio_resid = buflen;
370         auio.uio_offset = off;
371
372         if (cookies) {
373                 free(cookies, M_TEMP);
374                 cookies = NULL;
375         }
376
377 #ifdef MAC
378         error = mac_vnode_check_readdir(td->td_ucred, vp);
379         if (error)
380                 goto out;
381 #endif
382
383         /*
384          * First we read into the malloc'ed buffer, then
385          * we massage it into user space, one record at a time.
386          */
387         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0)
388                 goto out;
389         inp = buf;
390         outp = uap->buf;
391         resid = uap->nbytes;
392         if ((len = buflen - auio.uio_resid) <= 0)
393                 goto eof;
394
395         cookiep = cookies;
396
397         if (cookies) {
398                 /*
399                  * When using cookies, the vfs has the option of reading from
400                  * a different offset than that supplied (UFS truncates the
401                  * offset to a block boundary to make sure that it never reads
402                  * partway through a directory entry, even if the directory
403                  * has been compacted).
404                  */
405                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
406                         len -= BSD_DIRENT(inp)->d_reclen;
407                         inp += BSD_DIRENT(inp)->d_reclen;
408                         cookiep++;
409                         ncookies--;
410                 }
411         }
412
413         for (; len > 0; len -= reclen) {
414                 if (cookiep && ncookies == 0)
415                         break;
416                 reclen = BSD_DIRENT(inp)->d_reclen;
417                 if (reclen & 3) {
418                         printf("ibcs2_getdents: reclen=%d\n", reclen);
419                         error = EFAULT;
420                         goto out;
421                 }
422                 if (BSD_DIRENT(inp)->d_fileno == 0) {
423                         inp += reclen;  /* it is a hole; squish it out */
424                         if (cookiep) {
425                                 off = *cookiep++;
426                                 ncookies--;
427                         } else
428                                 off += reclen;
429                         continue;
430                 }
431                 if (reclen > len || resid < IBCS2_RECLEN(reclen)) {
432                         /* entry too big for buffer, so just stop */
433                         outp++;
434                         break;
435                 }
436                 /*
437                  * Massage in place to make an iBCS2-shaped dirent (otherwise
438                  * we have to worry about touching user memory outside of
439                  * the copyout() call).
440                  */
441                 idb.d_ino = (ibcs2_ino_t)BSD_DIRENT(inp)->d_fileno;
442                 idb.d_off = (ibcs2_off_t)off;
443                 idb.d_reclen = (u_short)IBCS2_RECLEN(reclen);
444                 if ((error = copyout((caddr_t)&idb, outp, 10)) != 0 ||
445                     (error = copyout(BSD_DIRENT(inp)->d_name, outp + 10,
446                                      BSD_DIRENT(inp)->d_namlen + 1)) != 0)
447                         goto out;
448                 /* advance past this real entry */
449                 if (cookiep) {
450                         off = *cookiep++;
451                         ncookies--;
452                 } else
453                         off += reclen;
454                 inp += reclen;
455                 /* advance output past iBCS2-shaped entry */
456                 outp += IBCS2_RECLEN(reclen);
457                 resid -= IBCS2_RECLEN(reclen);
458         }
459         /* if we squished out the whole block, try again */
460         if (outp == uap->buf)
461                 goto again;
462         fp->f_offset = off;             /* update the vnode offset */
463 eof:
464         td->td_retval[0] = uap->nbytes - resid;
465 out:
466         VOP_UNLOCK(vp, 0);
467         VFS_UNLOCK_GIANT(vfslocked);
468         fdrop(fp, td);
469         if (cookies)
470                 free(cookies, M_TEMP);
471         free(buf, M_TEMP);
472         return (error);
473 }
474
475 int
476 ibcs2_read(td, uap)
477         struct thread *td;
478         struct ibcs2_read_args *uap;
479 {
480         register struct vnode *vp;
481         register caddr_t inp, buf;      /* BSD-format */
482         register int len, reclen;       /* BSD-format */
483         register caddr_t outp;          /* iBCS2-format */
484         register int resid;             /* iBCS2-format */
485         struct file *fp;
486         struct uio auio;
487         struct iovec aiov;
488         struct ibcs2_direct {
489                 ibcs2_ino_t ino;
490                 char name[14];
491         } idb;
492         off_t off;                      /* true file offset */
493         int buflen, error, eofflag, size, vfslocked;
494         u_long *cookies = NULL, *cookiep;
495         int ncookies;
496
497         if ((error = getvnode(td->td_proc->p_fd, uap->fd,
498             CAP_READ | CAP_SEEK, &fp)) != 0) {
499                 if (error == EINVAL)
500                         return sys_read(td, (struct read_args *)uap);
501                 else
502                         return error;
503         }
504         if ((fp->f_flag & FREAD) == 0) {
505                 fdrop(fp, td);
506                 return (EBADF);
507         }
508         vp = fp->f_vnode;
509         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
510         if (vp->v_type != VDIR) {
511                 VFS_UNLOCK_GIANT(vfslocked);
512                 fdrop(fp, td);
513                 return sys_read(td, (struct read_args *)uap);
514         }
515
516         off = fp->f_offset;
517
518         DPRINTF(("ibcs2_read: read directory\n"));
519
520         buflen = max(DIRBLKSIZ, uap->nbytes);
521         buflen = min(buflen, MAXBSIZE);
522         buf = malloc(buflen, M_TEMP, M_WAITOK);
523         vn_lock(vp, LK_SHARED | LK_RETRY);
524 again:
525         aiov.iov_base = buf;
526         aiov.iov_len = buflen;
527         auio.uio_iov = &aiov;
528         auio.uio_iovcnt = 1;
529         auio.uio_rw = UIO_READ;
530         auio.uio_segflg = UIO_SYSSPACE;
531         auio.uio_td = td;
532         auio.uio_resid = buflen;
533         auio.uio_offset = off;
534
535         if (cookies) {
536                 free(cookies, M_TEMP);
537                 cookies = NULL;
538         }
539
540 #ifdef MAC
541         error = mac_vnode_check_readdir(td->td_ucred, vp);
542         if (error)
543                 goto out;
544 #endif
545
546         /*
547          * First we read into the malloc'ed buffer, then
548          * we massage it into user space, one record at a time.
549          */
550         if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies)) != 0) {
551                 DPRINTF(("VOP_READDIR failed: %d\n", error));
552                 goto out;
553         }
554         inp = buf;
555         outp = uap->buf;
556         resid = uap->nbytes;
557         if ((len = buflen - auio.uio_resid) <= 0)
558                 goto eof;
559
560         cookiep = cookies;
561
562         if (cookies) {
563                 /*
564                  * When using cookies, the vfs has the option of reading from
565                  * a different offset than that supplied (UFS truncates the
566                  * offset to a block boundary to make sure that it never reads
567                  * partway through a directory entry, even if the directory
568                  * has been compacted).
569                  */
570                 while (len > 0 && ncookies > 0 && *cookiep <= off) {
571                         len -= BSD_DIRENT(inp)->d_reclen;
572                         inp += BSD_DIRENT(inp)->d_reclen;
573                         cookiep++;
574                         ncookies--;
575                 }
576         }
577
578         for (; len > 0 && resid > 0; len -= reclen) {
579                 if (cookiep && ncookies == 0)
580                         break;
581                 reclen = BSD_DIRENT(inp)->d_reclen;
582                 if (reclen & 3) {
583                         printf("ibcs2_read: reclen=%d\n", reclen);
584                         error = EFAULT;
585                         goto out;
586                 }
587                 if (BSD_DIRENT(inp)->d_fileno == 0) {
588                         inp += reclen;  /* it is a hole; squish it out */
589                         if (cookiep) {
590                                 off = *cookiep++;
591                                 ncookies--;
592                         } else
593                                 off += reclen;
594                         continue;
595                 }
596                 if (reclen > len || resid < sizeof(struct ibcs2_direct)) {
597                         /* entry too big for buffer, so just stop */
598                         outp++;
599                         break;
600                 }
601                 /*
602                  * Massage in place to make an iBCS2-shaped dirent (otherwise
603                  * we have to worry about touching user memory outside of
604                  * the copyout() call).
605                  *
606                  * TODO: if length(filename) > 14, then break filename into
607                  * multiple entries and set inode = 0xffff except last
608                  */
609                 idb.ino = (BSD_DIRENT(inp)->d_fileno > 0xfffe) ? 0xfffe :
610                         BSD_DIRENT(inp)->d_fileno;
611                 (void)copystr(BSD_DIRENT(inp)->d_name, idb.name, 14, &size);
612                 bzero(idb.name + size, 14 - size);
613                 if ((error = copyout(&idb, outp, sizeof(struct ibcs2_direct))) != 0)
614                         goto out;
615                 /* advance past this real entry */
616                 if (cookiep) {
617                         off = *cookiep++;
618                         ncookies--;
619                 } else
620                         off += reclen;
621                 inp += reclen;
622                 /* advance output past iBCS2-shaped entry */
623                 outp += sizeof(struct ibcs2_direct);
624                 resid -= sizeof(struct ibcs2_direct);
625         }
626         /* if we squished out the whole block, try again */
627         if (outp == uap->buf)
628                 goto again;
629         fp->f_offset = off;             /* update the vnode offset */
630 eof:
631         td->td_retval[0] = uap->nbytes - resid;
632 out:
633         VOP_UNLOCK(vp, 0);
634         VFS_UNLOCK_GIANT(vfslocked);
635         fdrop(fp, td);
636         if (cookies)
637                 free(cookies, M_TEMP);
638         free(buf, M_TEMP);
639         return (error);
640 }
641
642 int
643 ibcs2_mknod(td, uap)
644         struct thread *td;
645         struct ibcs2_mknod_args *uap;
646 {
647         char *path;
648         int error;
649
650         CHECKALTCREAT(td, uap->path, &path);
651         if (S_ISFIFO(uap->mode))
652                 error = kern_mkfifo(td, path, UIO_SYSSPACE, uap->mode);
653         else
654                 error = kern_mknod(td, path, UIO_SYSSPACE, uap->mode, uap->dev);
655         free(path, M_TEMP);
656         return (error);
657 }
658
659 int
660 ibcs2_getgroups(td, uap)
661         struct thread *td;
662         struct ibcs2_getgroups_args *uap;
663 {
664         ibcs2_gid_t *iset;
665         gid_t *gp;
666         u_int i, ngrp;
667         int error;
668
669         if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
670                 if (uap->gidsetsize == 0)
671                         ngrp = 0;
672                 else
673                         return (EINVAL);
674         } else
675                 ngrp = td->td_ucred->cr_ngroups;
676         gp = malloc(ngrp * sizeof(*gp), M_TEMP, M_WAITOK);
677         error = kern_getgroups(td, &ngrp, gp);
678         if (error)
679                 goto out;
680         if (uap->gidsetsize > 0) {
681                 iset = malloc(ngrp * sizeof(*iset), M_TEMP, M_WAITOK);
682                 for (i = 0; i < ngrp; i++)
683                         iset[i] = (ibcs2_gid_t)gp[i];
684                 error = copyout(iset, uap->gidset, ngrp * sizeof(ibcs2_gid_t));
685                 free(iset, M_TEMP);
686         }
687         if (error == 0)
688                 td->td_retval[0] = ngrp;
689 out:
690         free(gp, M_TEMP);
691         return (error);
692 }
693
694 int
695 ibcs2_setgroups(td, uap)
696         struct thread *td;
697         struct ibcs2_setgroups_args *uap;
698 {
699         ibcs2_gid_t *iset;
700         gid_t *gp;
701         int error, i;
702
703         if (uap->gidsetsize < 0 || uap->gidsetsize > ngroups_max + 1)
704                 return (EINVAL);
705         if (uap->gidsetsize && uap->gidset == NULL)
706                 return (EINVAL);
707         gp = malloc(uap->gidsetsize * sizeof(*gp), M_TEMP, M_WAITOK);
708         if (uap->gidsetsize) {
709                 iset = malloc(uap->gidsetsize * sizeof(*iset), M_TEMP, M_WAITOK);
710                 error = copyin(uap->gidset, iset, sizeof(ibcs2_gid_t) *
711                     uap->gidsetsize);
712                 if (error) {
713                         free(iset, M_TEMP);
714                         goto out;
715                 }
716                 for (i = 0; i < uap->gidsetsize; i++)
717                         gp[i] = (gid_t)iset[i];
718         }
719
720         error = kern_setgroups(td, uap->gidsetsize, gp);
721 out:
722         free(gp, M_TEMP);
723         return (error);
724 }
725
726 int
727 ibcs2_setuid(td, uap)
728         struct thread *td;
729         struct ibcs2_setuid_args *uap;
730 {
731         struct setuid_args sa;
732
733         sa.uid = (uid_t)uap->uid;
734         return sys_setuid(td, &sa);
735 }
736
737 int
738 ibcs2_setgid(td, uap)
739         struct thread *td;
740         struct ibcs2_setgid_args *uap;
741 {
742         struct setgid_args sa;
743
744         sa.gid = (gid_t)uap->gid;
745         return sys_setgid(td, &sa);
746 }
747
748 int
749 ibcs2_time(td, uap)
750         struct thread *td;
751         struct ibcs2_time_args *uap;
752 {
753         struct timeval tv;
754
755         microtime(&tv);
756         td->td_retval[0] = tv.tv_sec;
757         if (uap->tp)
758                 return copyout((caddr_t)&tv.tv_sec, (caddr_t)uap->tp,
759                                sizeof(ibcs2_time_t));
760         else
761                 return 0;
762 }
763
764 int
765 ibcs2_pathconf(td, uap)
766         struct thread *td;
767         struct ibcs2_pathconf_args *uap;
768 {
769         char *path;
770         int error;
771
772         CHECKALTEXIST(td, uap->path, &path);
773         uap->name++;    /* iBCS2 _PC_* defines are offset by one */
774         error = kern_pathconf(td, path, UIO_SYSSPACE, uap->name, FOLLOW);
775         free(path, M_TEMP);
776         return (error);
777 }
778
779 int
780 ibcs2_fpathconf(td, uap)
781         struct thread *td;
782         struct ibcs2_fpathconf_args *uap;
783 {
784         uap->name++;    /* iBCS2 _PC_* defines are offset by one */
785         return sys_fpathconf(td, (struct fpathconf_args *)uap);
786 }
787
788 int
789 ibcs2_sysconf(td, uap)
790         struct thread *td;
791         struct ibcs2_sysconf_args *uap;
792 {
793         int mib[2], value, len, error;
794         struct proc *p;
795
796         p = td->td_proc;
797         switch(uap->name) {
798         case IBCS2_SC_ARG_MAX:
799                 mib[1] = KERN_ARGMAX;
800                 break;
801
802         case IBCS2_SC_CHILD_MAX:
803                 PROC_LOCK(p);
804                 td->td_retval[0] = lim_cur(td->td_proc, RLIMIT_NPROC);
805                 PROC_UNLOCK(p);
806                 return 0;
807
808         case IBCS2_SC_CLK_TCK:
809                 td->td_retval[0] = hz;
810                 return 0;
811
812         case IBCS2_SC_NGROUPS_MAX:
813                 mib[1] = KERN_NGROUPS;
814                 break;
815
816         case IBCS2_SC_OPEN_MAX:
817                 PROC_LOCK(p);
818                 td->td_retval[0] = lim_cur(td->td_proc, RLIMIT_NOFILE);
819                 PROC_UNLOCK(p);
820                 return 0;
821                 
822         case IBCS2_SC_JOB_CONTROL:
823                 mib[1] = KERN_JOB_CONTROL;
824                 break;
825                 
826         case IBCS2_SC_SAVED_IDS:
827                 mib[1] = KERN_SAVED_IDS;
828                 break;
829                 
830         case IBCS2_SC_VERSION:
831                 mib[1] = KERN_POSIX1;
832                 break;
833                 
834         case IBCS2_SC_PASS_MAX:
835                 td->td_retval[0] = 128;         /* XXX - should we create PASS_MAX ? */
836                 return 0;
837
838         case IBCS2_SC_XOPEN_VERSION:
839                 td->td_retval[0] = 2;           /* XXX: What should that be? */
840                 return 0;
841                 
842         default:
843                 return EINVAL;
844         }
845
846         mib[0] = CTL_KERN;
847         len = sizeof(value);
848         error = kernel_sysctl(td, mib, 2, &value, &len, NULL, 0, NULL, 0);
849         if (error)
850                 return error;
851         td->td_retval[0] = value;
852         return 0;
853 }
854
855 int
856 ibcs2_alarm(td, uap)
857         struct thread *td;
858         struct ibcs2_alarm_args *uap;
859 {
860         struct itimerval itv, oitv;
861         int error;
862
863         timevalclear(&itv.it_interval);
864         itv.it_value.tv_sec = uap->sec;
865         itv.it_value.tv_usec = 0;
866         error = kern_setitimer(td, ITIMER_REAL, &itv, &oitv);
867         if (error)
868                 return (error);
869         if (oitv.it_value.tv_usec != 0)
870                 oitv.it_value.tv_sec++;
871         td->td_retval[0] = oitv.it_value.tv_sec;
872         return (0);
873 }
874
875 int
876 ibcs2_times(td, uap)
877         struct thread *td;
878         struct ibcs2_times_args *uap;
879 {
880         struct rusage ru;
881         struct timeval t;
882         struct tms tms;
883         int error;
884
885 #define CONVTCK(r)      (r.tv_sec * hz + r.tv_usec / (1000000 / hz))
886
887         error = kern_getrusage(td, RUSAGE_SELF, &ru);
888         if (error)
889                 return (error);
890         tms.tms_utime = CONVTCK(ru.ru_utime);
891         tms.tms_stime = CONVTCK(ru.ru_stime);
892
893         error = kern_getrusage(td, RUSAGE_CHILDREN, &ru);
894         if (error)
895                 return (error);
896         tms.tms_cutime = CONVTCK(ru.ru_utime);
897         tms.tms_cstime = CONVTCK(ru.ru_stime);
898
899         microtime(&t);
900         td->td_retval[0] = CONVTCK(t);
901         
902         return (copyout(&tms, uap->tp, sizeof(struct tms)));
903 }
904
905 int
906 ibcs2_stime(td, uap)
907         struct thread *td;
908         struct ibcs2_stime_args *uap;
909 {
910         struct timeval tv;
911         long secs;
912         int error;
913
914         error = copyin(uap->timep, &secs, sizeof(long));
915         if (error)
916                 return (error);
917         tv.tv_sec = secs;
918         tv.tv_usec = 0;
919         error = kern_settimeofday(td, &tv, NULL);
920         if (error)
921                 error = EPERM;
922         return (error);
923 }
924
925 int
926 ibcs2_utime(td, uap)
927         struct thread *td;
928         struct ibcs2_utime_args *uap;
929 {
930         struct ibcs2_utimbuf ubuf;
931         struct timeval tbuf[2], *tp;
932         char *path;
933         int error;
934
935         if (uap->buf) {
936                 error = copyin(uap->buf, &ubuf, sizeof(ubuf));
937                 if (error)
938                         return (error);
939                 tbuf[0].tv_sec = ubuf.actime;
940                 tbuf[0].tv_usec = 0;
941                 tbuf[1].tv_sec = ubuf.modtime;
942                 tbuf[1].tv_usec = 0;
943                 tp = tbuf;
944         } else
945                 tp = NULL;
946
947         CHECKALTEXIST(td, uap->path, &path);
948         error = kern_utimes(td, path, UIO_SYSSPACE, tp, UIO_SYSSPACE);
949         free(path, M_TEMP);
950         return (error);
951 }
952
953 int
954 ibcs2_nice(td, uap)
955         struct thread *td;
956         struct ibcs2_nice_args *uap;
957 {
958         int error;
959         struct setpriority_args sa;
960
961         sa.which = PRIO_PROCESS;
962         sa.who = 0;
963         sa.prio = td->td_proc->p_nice + uap->incr;
964         if ((error = sys_setpriority(td, &sa)) != 0)
965                 return EPERM;
966         td->td_retval[0] = td->td_proc->p_nice;
967         return 0;
968 }
969
970 /*
971  * iBCS2 getpgrp, setpgrp, setsid, and setpgid
972  */
973
974 int
975 ibcs2_pgrpsys(td, uap)
976         struct thread *td;
977         struct ibcs2_pgrpsys_args *uap;
978 {
979         struct proc *p = td->td_proc;
980         switch (uap->type) {
981         case 0:                 /* getpgrp */
982                 PROC_LOCK(p);
983                 td->td_retval[0] = p->p_pgrp->pg_id;
984                 PROC_UNLOCK(p);
985                 return 0;
986
987         case 1:                 /* setpgrp */
988             {
989                 struct setpgid_args sa;
990
991                 sa.pid = 0;
992                 sa.pgid = 0;
993                 sys_setpgid(td, &sa);
994                 PROC_LOCK(p);
995                 td->td_retval[0] = p->p_pgrp->pg_id;
996                 PROC_UNLOCK(p);
997                 return 0;
998             }
999
1000         case 2:                 /* setpgid */
1001             {
1002                 struct setpgid_args sa;
1003
1004                 sa.pid = uap->pid;
1005                 sa.pgid = uap->pgid;
1006                 return sys_setpgid(td, &sa);
1007             }
1008
1009         case 3:                 /* setsid */
1010                 return sys_setsid(td, NULL);
1011
1012         default:
1013                 return EINVAL;
1014         }
1015 }
1016
1017 /*
1018  * XXX - need to check for nested calls
1019  */
1020
1021 int
1022 ibcs2_plock(td, uap)
1023         struct thread *td;
1024         struct ibcs2_plock_args *uap;
1025 {
1026         int error;
1027 #define IBCS2_UNLOCK    0
1028 #define IBCS2_PROCLOCK  1
1029 #define IBCS2_TEXTLOCK  2
1030 #define IBCS2_DATALOCK  4
1031
1032         
1033         switch(uap->cmd) {
1034         case IBCS2_UNLOCK:
1035                 error = priv_check(td, PRIV_VM_MUNLOCK);
1036                 if (error)
1037                         return (error);
1038                 /* XXX - TODO */
1039                 return (0);
1040
1041         case IBCS2_PROCLOCK:
1042         case IBCS2_TEXTLOCK:
1043         case IBCS2_DATALOCK:
1044                 error = priv_check(td, PRIV_VM_MLOCK);
1045                 if (error)
1046                         return (error);
1047                 /* XXX - TODO */
1048                 return 0;
1049         }
1050         return EINVAL;
1051 }
1052
1053 int
1054 ibcs2_uadmin(td, uap)
1055         struct thread *td;
1056         struct ibcs2_uadmin_args *uap;
1057 {
1058 #define SCO_A_REBOOT        1
1059 #define SCO_A_SHUTDOWN      2
1060 #define SCO_A_REMOUNT       4
1061 #define SCO_A_CLOCK         8
1062 #define SCO_A_SETCONFIG     128
1063 #define SCO_A_GETDEV        130
1064
1065 #define SCO_AD_HALT         0
1066 #define SCO_AD_BOOT         1
1067 #define SCO_AD_IBOOT        2
1068 #define SCO_AD_PWRDOWN      3
1069 #define SCO_AD_PWRNAP       4
1070
1071 #define SCO_AD_PANICBOOT    1
1072
1073 #define SCO_AD_GETBMAJ      0
1074 #define SCO_AD_GETCMAJ      1
1075
1076         switch(uap->cmd) {
1077         case SCO_A_REBOOT:
1078         case SCO_A_SHUTDOWN:
1079                 switch(uap->func) {
1080                         struct reboot_args r;
1081                 case SCO_AD_HALT:
1082                 case SCO_AD_PWRDOWN:
1083                 case SCO_AD_PWRNAP:
1084                         r.opt = RB_HALT;
1085                         return (sys_reboot(td, &r));
1086                 case SCO_AD_BOOT:
1087                 case SCO_AD_IBOOT:
1088                         r.opt = RB_AUTOBOOT;
1089                         return (sys_reboot(td, &r));
1090                 }
1091                 return EINVAL;
1092         case SCO_A_REMOUNT:
1093         case SCO_A_CLOCK:
1094         case SCO_A_SETCONFIG:
1095                 return 0;
1096         case SCO_A_GETDEV:
1097                 return EINVAL;  /* XXX - TODO */
1098         }
1099         return EINVAL;
1100 }
1101
1102 int
1103 ibcs2_sysfs(td, uap)
1104         struct thread *td;
1105         struct ibcs2_sysfs_args *uap;
1106 {
1107 #define IBCS2_GETFSIND        1
1108 #define IBCS2_GETFSTYP        2
1109 #define IBCS2_GETNFSTYP       3
1110
1111         switch(uap->cmd) {
1112         case IBCS2_GETFSIND:
1113         case IBCS2_GETFSTYP:
1114         case IBCS2_GETNFSTYP:
1115                 break;
1116         }
1117         return EINVAL;          /* XXX - TODO */
1118 }
1119
1120 int
1121 ibcs2_unlink(td, uap)
1122         struct thread *td;
1123         struct ibcs2_unlink_args *uap;
1124 {
1125         char *path;
1126         int error;
1127
1128         CHECKALTEXIST(td, uap->path, &path);
1129         error = kern_unlink(td, path, UIO_SYSSPACE);
1130         free(path, M_TEMP);
1131         return (error);
1132 }
1133
1134 int
1135 ibcs2_chdir(td, uap)
1136         struct thread *td;
1137         struct ibcs2_chdir_args *uap;
1138 {
1139         char *path;
1140         int error;
1141
1142         CHECKALTEXIST(td, uap->path, &path);
1143         error = kern_chdir(td, path, UIO_SYSSPACE);
1144         free(path, M_TEMP);
1145         return (error);
1146 }
1147
1148 int
1149 ibcs2_chmod(td, uap)
1150         struct thread *td;
1151         struct ibcs2_chmod_args *uap;
1152 {
1153         char *path;
1154         int error;
1155
1156         CHECKALTEXIST(td, uap->path, &path);
1157         error = kern_chmod(td, path, UIO_SYSSPACE, uap->mode);
1158         free(path, M_TEMP);
1159         return (error);
1160 }
1161
1162 int
1163 ibcs2_chown(td, uap)
1164         struct thread *td;
1165         struct ibcs2_chown_args *uap;
1166 {
1167         char *path;
1168         int error;
1169
1170         CHECKALTEXIST(td, uap->path, &path);
1171         error = kern_chown(td, path, UIO_SYSSPACE, uap->uid, uap->gid);
1172         free(path, M_TEMP);
1173         return (error);
1174 }
1175
1176 int
1177 ibcs2_rmdir(td, uap)
1178         struct thread *td;
1179         struct ibcs2_rmdir_args *uap;
1180 {
1181         char *path;
1182         int error;
1183
1184         CHECKALTEXIST(td, uap->path, &path);
1185         error = kern_rmdir(td, path, UIO_SYSSPACE);
1186         free(path, M_TEMP);
1187         return (error);
1188 }
1189
1190 int
1191 ibcs2_mkdir(td, uap)
1192         struct thread *td;
1193         struct ibcs2_mkdir_args *uap;
1194 {
1195         char *path;
1196         int error;
1197
1198         CHECKALTEXIST(td, uap->path, &path);
1199         error = kern_mkdir(td, path, UIO_SYSSPACE, uap->mode);
1200         free(path, M_TEMP);
1201         return (error);
1202 }
1203
1204 int
1205 ibcs2_symlink(td, uap)
1206         struct thread *td;
1207         struct ibcs2_symlink_args *uap;
1208 {
1209         char *path, *link;
1210         int error;
1211
1212         CHECKALTEXIST(td, uap->path, &path);
1213
1214         /*
1215          * Have to expand CHECKALTCREAT() so that 'path' can be freed on
1216          * errors.
1217          */
1218         error = ibcs2_emul_find(td, uap->link, UIO_USERSPACE, &link, 1);
1219         if (link == NULL) {
1220                 free(path, M_TEMP);
1221                 return (error);
1222         }
1223         error = kern_symlink(td, path, link, UIO_SYSSPACE);
1224         free(path, M_TEMP);
1225         free(link, M_TEMP);
1226         return (error);
1227 }
1228
1229 int
1230 ibcs2_rename(td, uap)
1231         struct thread *td;
1232         struct ibcs2_rename_args *uap;
1233 {
1234         char *from, *to;
1235         int error;
1236
1237         CHECKALTEXIST(td, uap->from, &from);
1238
1239         /*
1240          * Have to expand CHECKALTCREAT() so that 'from' can be freed on
1241          * errors.
1242          */
1243         error = ibcs2_emul_find(td, uap->to, UIO_USERSPACE, &to, 1);
1244         if (to == NULL) {
1245                 free(from, M_TEMP);
1246                 return (error);
1247         }
1248         error = kern_rename(td, from, to, UIO_SYSSPACE);
1249         free(from, M_TEMP);
1250         free(to, M_TEMP);
1251         return (error);
1252 }
1253
1254 int
1255 ibcs2_readlink(td, uap)
1256         struct thread *td;
1257         struct ibcs2_readlink_args *uap;
1258 {
1259         char *path;
1260         int error;
1261
1262         CHECKALTEXIST(td, uap->path, &path);
1263         error = kern_readlink(td, path, UIO_SYSSPACE, uap->buf, UIO_USERSPACE,
1264                 uap->count);
1265         free(path, M_TEMP);
1266         return (error);
1267 }