Home | History | Annotate | Line # | Download | only in sunos
sunos_misc.c revision 1.15
      1 /*
      2  * Copyright (c) 1992, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This software was developed by the Computer Systems Engineering group
      6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      7  * contributed to Berkeley.
      8  *
      9  * All advertising materials mentioning features or use of this software
     10  * must display the following acknowledgement:
     11  *	This product includes software developed by the University of
     12  *	California, Lawrence Berkeley Laboratory.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the University of
     25  *	California, Berkeley and its contributors.
     26  * 4. Neither the name of the University nor the names of its contributors
     27  *    may be used to endorse or promote products derived from this software
     28  *    without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  * SUCH DAMAGE.
     41  *
     42  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
     43  *
     44  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     45  * $Id: sunos_misc.c,v 1.15 1994/04/02 08:32:56 cgd Exp $
     46  */
     47 
     48 /*
     49  * SunOS compatibility module.
     50  *
     51  * SunOS system calls that are implemented differently in BSD are
     52  * handled here.
     53  */
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/namei.h>
     58 #include <ufs/dir.h>
     59 #include <sys/proc.h>
     60 #include <sys/file.h>
     61 #include <sys/stat.h>
     62 #include <sys/filedesc.h>
     63 #include <sys/ioctl.h>
     64 #include <sys/kernel.h>
     65 #include <sys/exec.h>
     66 #include <sys/malloc.h>
     67 #include <sys/mbuf.h>
     68 #include <sys/mman.h>
     69 #include <sys/mount.h>
     70 #include <sys/resource.h>
     71 #include <sys/resourcevar.h>
     72 #include <sys/signal.h>
     73 #include <sys/signalvar.h>
     74 #include <sys/socket.h>
     75 #include <sys/vnode.h>
     76 #include <sys/uio.h>
     77 #include <sys/wait.h>
     78 #include <sys/utsname.h>
     79 #include <sys/unistd.h>
     80 
     81 #include <netinet/in.h>
     82 
     83 #include <miscfs/specfs/specdev.h>
     84 
     85 #include <vm/vm.h>
     86 
     87 struct sun_wait4_args {
     88 	int	pid;
     89 	int	*status;
     90 	int	options;
     91 	struct	rusage *rusage;
     92 };
     93 sun_wait4(p, uap, retval)
     94 	struct proc *p;
     95 	struct sun_wait4_args *uap;
     96 	int *retval;
     97 {
     98 
     99 	if (uap->pid == 0)
    100 		uap->pid = WAIT_ANY;
    101 	return (wait4(p, uap, retval));
    102 }
    103 
    104 struct sun_creat_args {
    105 	char	*fname;
    106 	int	fmode;
    107 };
    108 sun_creat(p, uap, retval)
    109 	struct proc *p;
    110 	struct sun_creat_args *uap;
    111 	int *retval;
    112 {
    113 	struct args {
    114 		char	*fname;
    115 		int	mode;
    116 		int	crtmode;
    117 	} openuap;
    118 
    119 	openuap.fname = uap->fname;
    120 	openuap.crtmode = uap->fmode;
    121 	openuap.mode = O_WRONLY | O_CREAT | O_TRUNC;
    122 	return (open(p, &openuap, retval));
    123 }
    124 
    125 struct sun_execv_args {
    126 	char	*fname;
    127 	char	**argp;
    128 	char	**envp;		/* pseudo */
    129 };
    130 sun_execv(p, uap, retval)
    131 	struct proc *p;
    132 	struct sun_execv_args *uap;
    133 	int *retval;
    134 {
    135 
    136 	uap->envp = NULL;
    137 	return (execve(p, uap, retval));
    138 }
    139 
    140 struct sun_omsync_args {
    141 	caddr_t	addr;
    142 	int	len;
    143 	int	flags;
    144 };
    145 sun_omsync(p, uap, retval)
    146 	struct proc *p;
    147 	struct sun_omsync_args *uap;
    148 	int *retval;
    149 {
    150 
    151 	if (uap->flags)
    152 		return (EINVAL);
    153 	return (msync(p, uap, retval));
    154 }
    155 
    156 struct sun_unmount_args {
    157 	char	*name;
    158 	int	flags;	/* pseudo */
    159 };
    160 sun_unmount(p, uap, retval)
    161 	struct proc *p;
    162 	struct sun_unmount_args *uap;
    163 	int *retval;
    164 {
    165 
    166 	uap->flags = 0;
    167 	return (unmount(p, uap, retval));
    168 }
    169 
    170 static int
    171 gettype(tptr)
    172 	int *tptr;
    173 {
    174 	int type, error;
    175 	char in[20];
    176 
    177 	if (error = copyinstr((caddr_t)*tptr, in, sizeof in, (u_int *)0))
    178 		return (error);
    179 	if (strcmp(in, "4.2") == 0 || strcmp(in, "ufs") == 0)
    180 		type = MOUNT_UFS;
    181 	else if (strcmp(in, "nfs") == 0)
    182 		type = MOUNT_NFS;
    183 	else
    184 		return (EINVAL);
    185 	*tptr = type;
    186 	return (0);
    187 }
    188 
    189 #define	SUNM_RDONLY	0x01	/* mount fs read-only */
    190 #define	SUNM_NOSUID	0x02	/* mount fs with setuid disallowed */
    191 #define	SUNM_NEWTYPE	0x04	/* type is string (char *), not int */
    192 #define	SUNM_GRPID	0x08	/* (bsd semantics; ignored) */
    193 #define	SUNM_REMOUNT	0x10	/* update existing mount */
    194 #define	SUNM_NOSUB	0x20	/* prevent submounts (rejected) */
    195 #define	SUNM_MULTI	0x40	/* (ignored) */
    196 #define	SUNM_SYS5	0x80	/* Sys 5-specific semantics (rejected) */
    197 
    198 struct	sun_nfs_args {
    199 	struct	sockaddr_in *addr;	/* file server address */
    200 	caddr_t	fh;			/* file handle to be mounted */
    201 	int	flags;			/* flags */
    202 	int	wsize;			/* write size in bytes */
    203 	int	rsize;			/* read size in bytes */
    204 	int	timeo;			/* initial timeout in .1 secs */
    205 	int	retrans;		/* times to retry send */
    206 	char	*hostname;		/* server's hostname */
    207 	int	acregmin;		/* attr cache file min secs */
    208 	int	acregmax;		/* attr cache file max secs */
    209 	int	acdirmin;		/* attr cache dir min secs */
    210 	int	acdirmax;		/* attr cache dir max secs */
    211 	char	*netname;		/* server's netname */
    212 	struct	pathcnf *pathconf;	/* static pathconf kludge */
    213 };
    214 
    215 struct sun_mount_args {
    216 	int	type;
    217 	char	*dir;
    218 	int	flags;
    219 	caddr_t	data;
    220 };
    221 sun_mount(p, uap, retval)
    222 	struct proc *p;
    223 	struct sun_mount_args *uap;
    224 	int *retval;
    225 {
    226 	int oflags = uap->flags, nflags, error;
    227 	extern char sigcode[], esigcode[];
    228 #define	szsigcode	(esigcode - sigcode)
    229 
    230 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
    231 		return (EINVAL);
    232 	if (oflags & SUNM_NEWTYPE && (error = gettype(&uap->type)))
    233 		return (error);
    234 	nflags = 0;
    235 	if (oflags & SUNM_RDONLY)
    236 		nflags |= MNT_RDONLY;
    237 	if (oflags & SUNM_NOSUID)
    238 		nflags |= MNT_NOSUID;
    239 	if (oflags & SUNM_REMOUNT)
    240 		nflags |= MNT_UPDATE;
    241 	uap->flags = nflags;
    242 
    243 	if (uap->type == MOUNT_NFS) {
    244 		struct sun_nfs_args sna;
    245 		struct sockaddr_in sain;
    246 		struct nfs_args na;
    247 		struct sockaddr sa;
    248 
    249 		if (error = copyin(uap->data, &sna, sizeof sna))
    250 			return (error);
    251 		if (error = copyin(sna.addr, &sain, sizeof sain))
    252 			return (error);
    253 		bcopy(&sain, &sa, sizeof sa);
    254 		sa.sa_len = sizeof(sain);
    255 		uap->data = (caddr_t)ALIGN(PS_STRINGS - szsigcode - STACKGAPLEN);
    256 		na.addr = (struct sockaddr *)((int)uap->data + sizeof na);
    257 		na.sotype = SOCK_DGRAM;
    258 		na.proto = IPPROTO_UDP;
    259 		na.fh = (nfsv2fh_t *)sna.fh;
    260 		na.flags = sna.flags;
    261 		na.wsize = sna.wsize;
    262 		na.rsize = sna.rsize;
    263 		na.timeo = sna.timeo;
    264 		na.retrans = sna.retrans;
    265 		na.hostname = sna.hostname;
    266 
    267 		if (error = copyout(&sa, na.addr, sizeof sa))
    268 			return (error);
    269 		if (error = copyout(&na, uap->data, sizeof na))
    270 			return (error);
    271 	}
    272 	return (mount(p, uap, retval));
    273 }
    274 
    275 struct sun_sigpending_args {
    276 	int	*mask;
    277 };
    278 sun_sigpending(p, uap, retval)
    279 	struct proc *p;
    280 	struct sun_sigpending_args *uap;
    281 	int *retval;
    282 {
    283 	int mask = p->p_sig & p->p_sigmask;
    284 
    285 	return (copyout((caddr_t)&mask, (caddr_t)uap->mask, sizeof(int)));
    286 }
    287 
    288 /* XXX: Temporary until sys/dir.h, include/dirent.h and sys/dirent.h are fixed */
    289 struct dirent {
    290 	u_long	d_fileno;		/* file number of entry */
    291 	u_short	d_reclen;		/* length of this record */
    292 	u_short	d_namlen;		/* length of string in d_name */
    293 	char	d_name[255 + 1];	/* name must be no longer than this */
    294 };
    295 
    296 /*
    297  * Here is the sun layout.  (Compare the BSD layout in <sys/dirent.h>.)
    298  * We can assume big-endian, so the BSD d_type field is just the high
    299  * byte of the SunOS d_namlen field, after adjusting for the extra "long".
    300  */
    301 struct sun_dirent {
    302 	long	d_off;
    303 	u_long	d_fileno;
    304 	u_short	d_reclen;
    305 	u_short	d_namlen;
    306 	char	d_name[256];
    307 };
    308 
    309 /*
    310  * Read Sun-style directory entries.  We suck them into kernel space so
    311  * that they can be massaged before being copied out to user code.  Like
    312  * SunOS, we squish out `empty' entries.
    313  *
    314  * This is quite ugly, but what do you expect from compatibility code?
    315  */
    316 struct sun_getdents_args {
    317 	int	fd;
    318 	char	*buf;
    319 	int	nbytes;
    320 };
    321 sun_getdents(p, uap, retval)
    322 	struct proc *p;
    323 	register struct sun_getdents_args *uap;
    324 	int *retval;
    325 {
    326 	register struct vnode *vp;
    327 	register caddr_t inp, buf;	/* BSD-format */
    328 	register int len, reclen;	/* BSD-format */
    329 	register caddr_t outp;		/* Sun-format */
    330 	register int resid;		/* Sun-format */
    331 	struct file *fp;
    332 	struct uio auio;
    333 	struct iovec aiov;
    334 	off_t off;			/* true file offset */
    335 	long soff;			/* Sun file offset */
    336 	int buflen, error, eofflag;
    337 #define	BSD_DIRENT(cp) ((struct dirent *)(cp))
    338 #define	SUN_RECLEN(reclen) (reclen + sizeof(long))
    339 
    340 	if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
    341 		return (error);
    342 	if ((fp->f_flag & FREAD) == 0)
    343 		return (EBADF);
    344 	vp = (struct vnode *)fp->f_data;
    345 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
    346 		return (EINVAL);
    347 	buflen = min(MAXBSIZE, uap->nbytes);
    348 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    349 	VOP_LOCK(vp);
    350 	off = fp->f_offset;
    351 again:
    352 	aiov.iov_base = buf;
    353 	aiov.iov_len = buflen;
    354 	auio.uio_iov = &aiov;
    355 	auio.uio_iovcnt = 1;
    356 	auio.uio_rw = UIO_READ;
    357 	auio.uio_segflg = UIO_SYSSPACE;
    358 	auio.uio_procp = p;
    359 	auio.uio_resid = buflen;
    360 	auio.uio_offset = off;
    361 	/*
    362 	 * First we read into the malloc'ed buffer, then
    363 	 * we massage it into user space, one record at a time.
    364 	 */
    365 	if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, 0))
    366 		goto out;
    367 	inp = buf;
    368 	outp = uap->buf;
    369 	resid = uap->nbytes;
    370 	if ((len = buflen - auio.uio_resid) == 0)
    371 		goto eof;
    372 	for (; len > 0; len -= reclen) {
    373 		reclen = ((struct dirent *)inp)->d_reclen;
    374 		if (reclen & 3)
    375 			panic("sun_getdents");
    376 		off += reclen;		/* each entry points to next */
    377 		if (BSD_DIRENT(inp)->d_fileno == 0) {
    378 			inp += reclen;	/* it is a hole; squish it out */
    379 			continue;
    380 		}
    381 		if (reclen > len || resid < SUN_RECLEN(reclen)) {
    382 			/* entry too big for buffer, so just stop */
    383 			outp++;
    384 			break;
    385 		}
    386 		/*
    387 		 * Massage in place to make a Sun-shaped dirent (otherwise
    388 		 * we have to worry about touching user memory outside of
    389 		 * the copyout() call).
    390 		 */
    391 		BSD_DIRENT(inp)->d_reclen = SUN_RECLEN(reclen);
    392 #if notdef
    393 		BSD_DIRENT(inp)->d_type = 0; 	/* 4.4 specific */
    394 #endif
    395 		soff = off;
    396 		if ((error = copyout((caddr_t)&soff, outp, sizeof soff)) != 0 ||
    397 		    (error = copyout(inp, outp + sizeof soff, reclen)) != 0)
    398 			goto out;
    399 		/* advance past this real entry */
    400 		inp += reclen;
    401 		/* advance output past Sun-shaped entry */
    402 		outp += SUN_RECLEN(reclen);
    403 		resid -= SUN_RECLEN(reclen);
    404 	}
    405 	/* if we squished out the whole block, try again */
    406 	if (outp == uap->buf)
    407 		goto again;
    408 	fp->f_offset = off;		/* update the vnode offset */
    409 eof:
    410 	*retval = uap->nbytes - resid;
    411 out:
    412 	VOP_UNLOCK(vp);
    413 	free(buf, M_TEMP);
    414 	return (error);
    415 }
    416 
    417 #if defined(sparc)
    418 #define	DEVZERO	makedev(3, 12)		/* major,minor of /dev/zero */
    419 #else /* all m68k architectures */
    420 #define	DEVZERO	makedev(2, 12)		/* major,minor of /dev/zero */
    421 #endif
    422 
    423 #define	SUN__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
    424 
    425 struct sun_mmap_args {
    426 	caddr_t	addr;
    427 	size_t	len;
    428 	int	prot;
    429 	int	flags;
    430 	int	fd;
    431 	long	off;		/* not off_t! */
    432 	off_t	qoff;		/* created here and fed to mmap() */
    433 };
    434 sun_mmap(p, uap, retval)
    435 	register struct proc *p;
    436 	register struct sun_mmap_args *uap;
    437 	int *retval;
    438 {
    439 	register struct filedesc *fdp;
    440 	register struct file *fp;
    441 	register struct vnode *vp;
    442 
    443 	/*
    444 	 * Verify the arguments.
    445 	 */
    446 	if (uap->prot & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
    447 		return (EINVAL);			/* XXX still needed? */
    448 
    449 	if ((uap->flags & SUN__MAP_NEW) == 0)
    450 		return (EINVAL);
    451 	uap->flags &= ~SUN__MAP_NEW;
    452 
    453 	if ((uap->flags & MAP_FIXED) == 0 &&
    454 		uap->addr != 0 &&
    455 		uap->addr < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
    456 		uap->addr = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
    457 
    458 	/*
    459 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
    460 	 */
    461 	fdp = p->p_fd;
    462 	if ((unsigned)uap->fd < fdp->fd_nfiles &&			/*XXX*/
    463 	    (fp = fdp->fd_ofiles[uap->fd]) != NULL &&			/*XXX*/
    464 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
    465 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
    466 	    vp->v_rdev == DEVZERO) {					/*XXX*/
    467 		uap->flags |= MAP_ANON;
    468 		uap->fd = -1;
    469 	}
    470 
    471 	uap->qoff = uap->off;
    472 	return (smmap(p, uap, retval));
    473 }
    474 
    475 #define	MC_SYNC		1
    476 #define	MC_LOCK		2
    477 #define	MC_UNLOCK	3
    478 #define	MC_ADVISE	4
    479 #define	MC_LOCKAS	5
    480 #define	MC_UNLOCKAS	6
    481 
    482 struct sun_mctl_args {
    483 	caddr_t	addr;
    484 	size_t	len;
    485 	int	func;
    486 	void	*arg;
    487 };
    488 sun_mctl(p, uap, retval)
    489 	register struct proc *p;
    490 	register struct sun_mctl_args *uap;
    491 	int *retval;
    492 {
    493 
    494 	switch (uap->func) {
    495 
    496 	case MC_ADVISE:		/* ignore for now */
    497 		return (0);
    498 
    499 	case MC_SYNC:		/* translate to msync */
    500 		return (msync(p, uap, retval));
    501 
    502 	default:
    503 		return (EINVAL);
    504 	}
    505 }
    506 
    507 struct sun_setsockopt_args {
    508 	int	s;
    509 	int	level;
    510 	int	name;
    511 	caddr_t	val;
    512 	int	valsize;
    513 };
    514 sun_setsockopt(p, uap, retval)
    515 	struct proc *p;
    516 	register struct sun_setsockopt_args *uap;
    517 	int *retval;
    518 {
    519 	struct file *fp;
    520 	struct mbuf *m = NULL;
    521 	int error;
    522 
    523 	if (error = getsock(p->p_fd, uap->s, &fp))
    524 		return (error);
    525 #define	SO_DONTLINGER (~SO_LINGER)
    526 	if (uap->name == SO_DONTLINGER) {
    527 		m = m_get(M_WAIT, MT_SOOPTS);
    528 		if (m == NULL)
    529 			return (ENOBUFS);
    530 		mtod(m, struct linger *)->l_onoff = 0;
    531 		m->m_len = sizeof(struct linger);
    532 		return (sosetopt((struct socket *)fp->f_data, uap->level,
    533 		    SO_LINGER, m));
    534 	}
    535 	if (uap->valsize > MLEN)
    536 		return (EINVAL);
    537 	if (uap->val) {
    538 		m = m_get(M_WAIT, MT_SOOPTS);
    539 		if (m == NULL)
    540 			return (ENOBUFS);
    541 		if (error = copyin(uap->val, mtod(m, caddr_t),
    542 		    (u_int)uap->valsize)) {
    543 			(void) m_free(m);
    544 			return (error);
    545 		}
    546 		m->m_len = uap->valsize;
    547 	}
    548 	return (sosetopt((struct socket *)fp->f_data, uap->level,
    549 	    uap->name, m));
    550 }
    551 
    552 struct sun_fchroot_args {
    553 	int	fdes;
    554 };
    555 sun_fchroot(p, uap, retval)
    556 	register struct proc *p;
    557 	register struct sun_fchroot_args *uap;
    558 	int *retval;
    559 {
    560 	register struct filedesc *fdp = p->p_fd;
    561 	register struct vnode *vp;
    562 	struct file *fp;
    563 	int error;
    564 
    565 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    566 		return (error);
    567 	if ((error = getvnode(fdp, uap->fdes, &fp)) != 0)
    568 		return (error);
    569 	vp = (struct vnode *)fp->f_data;
    570 	VOP_LOCK(vp);
    571 	if (vp->v_type != VDIR)
    572 		error = ENOTDIR;
    573 	else
    574 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    575 	VOP_UNLOCK(vp);
    576 	if (error)
    577 		return (error);
    578 	VREF(vp);
    579 	if (fdp->fd_rdir != NULL)
    580 		vrele(fdp->fd_rdir);
    581 	fdp->fd_rdir = vp;
    582 	return (0);
    583 }
    584 
    585 /*
    586  * XXX: This needs cleaning up.
    587  */
    588 sun_auditsys(...)
    589 {
    590 	return 0;
    591 }
    592 
    593 struct sun_utsname {
    594 	char    sysname[9];
    595 	char    nodename[9];
    596 	char    nodeext[65-9];
    597 	char    release[9];
    598 	char    version[9];
    599 	char    machine[9];
    600 };
    601 
    602 struct sun_uname_args {
    603 	struct sun_utsname *name;
    604 };
    605 sun_uname(p, uap, retval)
    606 	struct proc *p;
    607 	struct sun_uname_args *uap;
    608 	int *retval;
    609 {
    610 	struct sun_utsname sut;
    611 
    612 	/* first update utsname just as with NetBSD uname() */
    613 	bcopy(hostname, utsname.nodename, sizeof(utsname.nodename));
    614 	utsname.nodename[sizeof(utsname.nodename)-1] = '\0';
    615 
    616 	/* then copy it over into SunOS struct utsname */
    617 	bzero(&sut, sizeof(sut));
    618 	bcopy(utsname.sysname, sut.sysname, sizeof(sut.sysname) - 1);
    619 	bcopy(utsname.nodename, sut.nodename, sizeof(sut.nodename) - 1);
    620 	bcopy(utsname.release, sut.release, sizeof(sut.release) - 1);
    621 	bcopy(utsname.version, sut.version, sizeof(sut.version) - 1);
    622 	bcopy(utsname.machine, sut.machine, sizeof(sut.machine) - 1);
    623 
    624 	return copyout((caddr_t)&sut, (caddr_t)uap->name, sizeof(struct sun_utsname));
    625 }
    626 
    627 struct sun_setpgid_args {
    628 	int	pid;	/* target process id */
    629 	int	pgid;	/* target pgrp id */
    630 };
    631 int
    632 sun_setpgid(p, uap, retval)
    633 	struct proc *p;
    634 	struct sun_setpgid_args *uap;
    635 	int *retval;
    636 {
    637 	/*
    638 	 * difference to our setpgid call is to include backwards
    639 	 * compatibility to pre-setsid() binaries. Do setsid()
    640 	 * instead of setpgid() in those cases where the process
    641 	 * tries to create a new session the old way.
    642 	 */
    643 	if (!uap->pgid && (!uap->pid || uap->pid == p->p_pid))
    644 		return setsid(p, uap, retval);
    645 	else
    646 		return setpgid(p, uap, retval);
    647 }
    648 
    649 struct sun_open_args {
    650 	char	*fname;
    651 	int	fmode;
    652 	int	crtmode;
    653 };
    654 sun_open(p, uap, retval)
    655 	struct proc *p;
    656 	struct sun_open_args *uap;
    657 	int *retval;
    658 {
    659 	int l, r;
    660 	int noctty = uap->fmode & 0x8000;
    661 	int ret;
    662 
    663 	/* convert mode into NetBSD mode */
    664 	l = uap->fmode;
    665 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    666 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    667 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    668 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    669 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    670 
    671 	uap->fmode = r;
    672 	ret = open(p, uap, retval);
    673 
    674 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & SCTTY)) {
    675 		struct filedesc *fdp = p->p_fd;
    676 		struct file *fp = fdp->fd_ofiles[*retval];
    677 
    678 		/* ignore any error, just give it a try */
    679 		if (fp->f_type == DTYPE_VNODE)
    680 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
    681 	}
    682 	return ret;
    683 }
    684 
    685 #if defined (NFSSERVER)
    686 struct nfssvc_args {
    687 	int	fd;
    688 	caddr_t mskval;
    689 	int msklen;
    690 	caddr_t mtchval;
    691 	int mtchlen;
    692 };
    693 struct sun_nfssvc_args {
    694 	int	fd;
    695 };
    696 sun_nfssvc(p, uap, retval)
    697 	struct proc *p;
    698 	struct sun_nfssvc_args *uap;
    699 	int *retval;
    700 {
    701 	struct nfssvc_args outuap;
    702 	struct sockaddr sa;
    703 	int error;
    704 	extern char sigcode[], esigcode[];
    705 
    706 	bzero(&outuap, sizeof outuap);
    707 	outuap.fd = uap->fd;
    708 	outuap.mskval = (caddr_t)ALIGN(PS_STRINGS - szsigcode - STACKGAPLEN);
    709 	outuap.msklen = sizeof sa;
    710 	outuap.mtchval = outuap.mskval + sizeof sa;
    711 	outuap.mtchlen = sizeof sa;
    712 
    713 	bzero(&sa, sizeof sa);
    714 	if (error = copyout(&sa, outuap.mskval, outuap.msklen))
    715 		return (error);
    716 	if (error = copyout(&sa, outuap.mtchval, outuap.mtchlen))
    717 		return (error);
    718 
    719 	return nfssvc(p, &outuap, retval);
    720 }
    721 #endif /* NFSSERVER */
    722 
    723 struct sun_ustat {
    724 	daddr_t	f_tfree;	/* total free */
    725 	ino_t	f_tinode;	/* total inodes free */
    726 	char	f_fname[6];	/* filsys name */
    727 	char	f_fpack[6];	/* filsys pack name */
    728 };
    729 struct sun_ustat_args {
    730 	int	dev;
    731 	struct	sun_ustat *buf;
    732 };
    733 sun_ustat(p, uap, retval)
    734 	struct proc *p;
    735 	struct sun_ustat_args *uap;
    736 	int *retval;
    737 {
    738 	struct sun_ustat us;
    739 	int error;
    740 
    741 	bzero(&us, sizeof us);
    742 
    743 	/*
    744 	 * XXX: should set f_tfree and f_tinode at least
    745 	 * How do we translate dev -> fstat? (and then to sun_ustat)
    746 	 */
    747 
    748 	if (error = copyout(&us, uap->buf, sizeof us))
    749 		return (error);
    750 	return 0;
    751 }
    752 
    753 struct sun_quotactl_args {
    754 	int	cmd;
    755 	char	*special;
    756 	int	uid;
    757 	caddr_t	addr;
    758 };
    759 sun_quotactl(p, uap, retval)
    760 	struct proc *p;
    761 	struct sun_quotactl_args *uap;
    762 	int *retval;
    763 {
    764 	return EINVAL;
    765 }
    766 
    767 sun_vhangup(p, uap, retval)
    768 	struct proc *p;
    769 	void *uap;
    770 	int *retval;
    771 {
    772 	return 0;
    773 }
    774 
    775 struct sun_statfs {
    776 	long	f_type;		/* type of info, zero for now */
    777 	long	f_bsize;	/* fundamental file system block size */
    778 	long	f_blocks;	/* total blocks in file system */
    779 	long	f_bfree;	/* free blocks */
    780 	long	f_bavail;	/* free blocks available to non-super-user */
    781 	long	f_files;	/* total file nodes in file system */
    782 	long	f_ffree;	/* free file nodes in fs */
    783 	fsid_t	f_fsid;		/* file system id */
    784 	long	f_spare[7];	/* spare for later */
    785 };
    786 static
    787 sunstatfs(sp, buf)
    788 	struct statfs *sp;
    789 	caddr_t buf;
    790 {
    791 	struct sun_statfs ssfs;
    792 
    793 	bzero(&ssfs, sizeof ssfs);
    794 	ssfs.f_type = 0;
    795 	ssfs.f_bsize = sp->f_fsize;
    796 	ssfs.f_blocks = sp->f_blocks;
    797 	ssfs.f_bfree = sp->f_bfree;
    798 	ssfs.f_bavail = sp->f_bavail;
    799 	ssfs.f_files = sp->f_files;
    800 	ssfs.f_ffree = sp->f_ffree;
    801 	ssfs.f_fsid = sp->f_fsid;
    802 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
    803 }
    804 
    805 struct sun_statfs_args {
    806 	char	*path;
    807 	struct	sun_statfs *buf;
    808 };
    809 sun_statfs(p, uap, retval)
    810 	struct proc *p;
    811 	struct sun_statfs_args *uap;
    812 	int *retval;
    813 {
    814 	register struct mount *mp;
    815 	register struct nameidata *ndp;
    816 	register struct statfs *sp;
    817 	int error;
    818 	struct nameidata nd;
    819 
    820 	ndp = &nd;
    821 	ndp->ni_nameiop = LOOKUP | FOLLOW;
    822 	ndp->ni_segflg = UIO_USERSPACE;
    823 	ndp->ni_dirp = uap->path;
    824 	if (error = namei(ndp, p))
    825 		return (error);
    826 	mp = ndp->ni_vp->v_mount;
    827 	sp = &mp->mnt_stat;
    828 	vrele(ndp->ni_vp);
    829 	if (error = VFS_STATFS(mp, sp, p))
    830 		return (error);
    831 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    832 	return sunstatfs(sp, (caddr_t)uap->buf);
    833 }
    834 
    835 struct sun_fstatfs_args {
    836 	int	fd;
    837 	struct	sun_statfs *buf;
    838 };
    839 sun_fstatfs(p, uap, retval)
    840 	struct proc *p;
    841 	struct sun_fstatfs_args *uap;
    842 	int *retval;
    843 {
    844 	struct file *fp;
    845 	struct mount *mp;
    846 	register struct statfs *sp;
    847 	int error;
    848 
    849 	if (error = getvnode(p->p_fd, uap->fd, &fp))
    850 		return (error);
    851 	mp = ((struct vnode *)fp->f_data)->v_mount;
    852 	sp = &mp->mnt_stat;
    853 	if (error = VFS_STATFS(mp, sp, p))
    854 		return (error);
    855 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    856 	return sunstatfs(sp, (caddr_t)uap->buf);
    857 }
    858 
    859 struct sun_exportfs_args {
    860 	char	*path;
    861 	char	*ex;			/* struct sun_export * */
    862 };
    863 sun_exportfs(p, uap, retval)
    864 	struct proc *p;
    865 	struct sun_exportfs_args *uap;
    866 	int *retval;
    867 {
    868 	/*
    869 	 * XXX: should perhaps translate into a mount(2)
    870 	 * with MOUNT_EXPORT?
    871 	 */
    872 	return 0;
    873 }
    874 
    875 struct sun_mknod_args {
    876 	char	*fname;
    877 	int	fmode;
    878 	int	dev;
    879 };
    880 
    881 sun_mknod(p, uap, retval)
    882 	struct proc *p;
    883 	struct sun_mknod_args *uap;
    884 	int *retval;
    885 {
    886 	if (S_ISFIFO(uap->fmode))
    887 		return mkfifo(p, uap, retval);
    888 
    889 	return mknod(p, uap, retval);
    890 }
    891 
    892 #define SUN_SC_ARG_MAX		1
    893 #define SUN_SC_CHILD_MAX	2
    894 #define SUN_SC_CLK_TCK		3
    895 #define SUN_SC_NGROUPS_MAX	4
    896 #define SUN_SC_OPEN_MAX		5
    897 #define SUN_SC_JOB_CONTROL	6
    898 #define SUN_SC_SAVED_IDS	7
    899 #define SUN_SC_VERSION		8
    900 
    901 struct sun_sysconf_args {
    902 	int	name;
    903 };
    904 
    905 sun_sysconf(p, uap, retval)
    906 	struct proc *p;
    907 	struct sun_sysconf_args *uap;
    908 	int *retval;
    909 {
    910 	extern int maxfdescs;
    911 
    912 	switch(uap->name) {
    913 	case SUN_SC_ARG_MAX:
    914 		*retval = ARG_MAX;
    915 		break;
    916 	case SUN_SC_CHILD_MAX:
    917 		*retval = maxproc;
    918 		break;
    919 	case SUN_SC_CLK_TCK:
    920 		*retval = 60;		/* should this be `hz', ie. 100? */
    921 		break;
    922 	case SUN_SC_NGROUPS_MAX:
    923 		*retval = NGROUPS_MAX;
    924 		break;
    925 	case SUN_SC_OPEN_MAX:
    926 		*retval = maxfdescs;
    927 		break;
    928 	case SUN_SC_JOB_CONTROL:
    929 		*retval = 1;
    930 		break;
    931 	case SUN_SC_SAVED_IDS:
    932 #ifdef _POSIX_SAVED_IDS
    933 		*retval = 1;
    934 #else
    935 		*retval = 0;
    936 #endif
    937 		break;
    938 	case SUN_SC_VERSION:
    939 		*retval = 198808;
    940 		break;
    941 	default:
    942 		return EINVAL;
    943 	}
    944 	return 0;
    945 }
    946