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