xref: /netbsd-src/sys/compat/sunos/sunos_misc.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: sunos_misc.c,v 1.72 1996/09/07 12:40:48 mycroft Exp $	*/
2 
3 /*
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  *	@(#)sunos_misc.c	8.1 (Berkeley) 6/18/93
45  *
46  *	Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
47  */
48 
49 /*
50  * SunOS compatibility module.
51  *
52  * SunOS system calls that are implemented differently in BSD are
53  * handled here.
54  */
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/dirent.h>
61 #include <sys/file.h>
62 #include <sys/stat.h>
63 #include <sys/filedesc.h>
64 #include <sys/ioctl.h>
65 #include <sys/kernel.h>
66 #include <sys/reboot.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/mman.h>
70 #include <sys/mount.h>
71 #include <sys/ptrace.h>
72 #include <sys/resource.h>
73 #include <sys/resourcevar.h>
74 #include <sys/signal.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/tty.h>
78 #include <sys/vnode.h>
79 #include <sys/uio.h>
80 #include <sys/wait.h>
81 #include <sys/utsname.h>
82 #include <sys/unistd.h>
83 #include <sys/syscallargs.h>
84 #include <sys/conf.h>
85 #include <sys/socketvar.h>
86 
87 #include <compat/sunos/sunos.h>
88 #include <compat/sunos/sunos_syscallargs.h>
89 #include <compat/sunos/sunos_util.h>
90 #include <compat/sunos/sunos_dirent.h>
91 
92 #include <netinet/in.h>
93 
94 #include <miscfs/specfs/specdev.h>
95 
96 #include <nfs/rpcv2.h>
97 #include <nfs/nfsproto.h>
98 #include <nfs/nfs.h>
99 
100 #include <vm/vm.h>
101 
102 static int sunstatfs __P((struct statfs *, caddr_t));
103 
104 int
105 sunos_sys_stime(p, v, retval)
106 	struct proc *p;
107 	void *v;
108 	register_t *retval;
109 {
110 	struct sunos_sys_stime_args *uap = v;
111 	struct sys_settimeofday_args ap;
112 	caddr_t sg = stackgap_init(p->p_emul);
113 	struct timeval tv;
114 	int error;
115 
116 	error = copyin(SCARG(uap, tp), &tv.tv_sec, sizeof(tv.tv_sec));
117 	if (error)
118 		return error;
119 	tv.tv_usec = 0;
120 
121 	SCARG(&ap, tv) = stackgap_alloc(&sg, sizeof(struct timeval));
122 	SCARG(&ap, tzp) = NULL;
123 
124 	error = copyout(&tv, SCARG(&ap, tv), sizeof(struct timeval));
125 	if (error)
126 		return error;
127 
128 	return sys_settimeofday(p, &ap, retval);
129 }
130 
131 int
132 sunos_sys_wait4(p, v, retval)
133 	struct proc *p;
134 	void *v;
135 	register_t *retval;
136 {
137 	struct sunos_sys_wait4_args *uap = v;
138 	if (SCARG(uap, pid) == 0)
139 		SCARG(uap, pid) = WAIT_ANY;
140 	return (sys_wait4(p, uap, retval));
141 }
142 
143 int
144 sunos_sys_creat(p, v, retval)
145 	struct proc *p;
146 	void *v;
147 	register_t *retval;
148 {
149 	struct sunos_sys_creat_args *uap = v;
150 	struct sys_open_args ouap;
151 
152 	caddr_t sg = stackgap_init(p->p_emul);
153 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
154 
155 	SCARG(&ouap, path) = SCARG(uap, path);
156 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
157 	SCARG(&ouap, mode) = SCARG(uap, mode);
158 
159 	return (sys_open(p, &ouap, retval));
160 }
161 
162 int
163 sunos_sys_access(p, v, retval)
164 	struct proc *p;
165 	void *v;
166 	register_t *retval;
167 {
168 	struct sunos_sys_access_args *uap = v;
169 	caddr_t sg = stackgap_init(p->p_emul);
170 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
171 
172 	return (sys_access(p, uap, retval));
173 }
174 
175 int
176 sunos_sys_stat(p, v, retval)
177 	struct proc *p;
178 	void *v;
179 	register_t *retval;
180 {
181 	struct sunos_sys_stat_args *uap = v;
182 	caddr_t sg = stackgap_init(p->p_emul);
183 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
184 
185 	return (compat_43_sys_stat(p, uap, retval));
186 }
187 
188 int
189 sunos_sys_lstat(p, v, retval)
190 	struct proc *p;
191 	void *v;
192 	register_t *retval;
193 {
194 	struct sunos_sys_lstat_args *uap = v;
195 	caddr_t sg = stackgap_init(p->p_emul);
196 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
197 
198 	return (compat_43_sys_lstat(p, uap, retval));
199 }
200 
201 int
202 sunos_sys_execv(p, v, retval)
203 	struct proc *p;
204 	void *v;
205 	register_t *retval;
206 {
207 	struct sunos_sys_execv_args /* {
208 		syscallarg(char *) path;
209 		syscallarg(char **) argv;
210 	} */ *uap = v;
211 	struct sys_execve_args ap;
212 	caddr_t sg;
213 
214 	sg = stackgap_init(p->p_emul);
215 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
216 
217 	SCARG(&ap, path) = SCARG(uap, path);
218 	SCARG(&ap, argp) = SCARG(uap, argp);
219 	SCARG(&ap, envp) = NULL;
220 
221 	return (sys_execve(p, &ap, retval));
222 }
223 
224 int
225 sunos_sys_execve(p, v, retval)
226 	struct proc *p;
227 	void *v;
228 	register_t *retval;
229 {
230 	struct sunos_sys_execve_args /* {
231 		syscallarg(char *) path;
232 		syscallarg(char **) argv;
233 		syscallarg(char **) envp;
234 	} */ *uap = v;
235 	struct sys_execve_args ap;
236 	caddr_t sg;
237 
238 	sg = stackgap_init(p->p_emul);
239 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
240 
241 	SCARG(&ap, path) = SCARG(uap, path);
242 	SCARG(&ap, argp) = SCARG(uap, argp);
243 	SCARG(&ap, envp) = SCARG(uap, envp);
244 
245 	return (sys_execve(p, &ap, retval));
246 }
247 
248 int
249 sunos_sys_omsync(p, v, retval)
250 	struct proc *p;
251 	void *v;
252 	register_t *retval;
253 {
254 	struct sunos_sys_omsync_args *uap = v;
255 	struct sys_msync_args ouap;
256 
257 	if (SCARG(uap, flags))
258 		return (EINVAL);
259 	SCARG(&ouap, addr) = SCARG(uap, addr);
260 	SCARG(&ouap, len) = SCARG(uap, len);
261 
262 	return (sys_msync(p, &ouap, retval));
263 }
264 
265 int
266 sunos_sys_unmount(p, v, retval)
267 	struct proc *p;
268 	void *v;
269 	register_t *retval;
270 {
271 	struct sunos_sys_unmount_args *uap = v;
272 	struct sys_unmount_args ouap;
273 
274 	SCARG(&ouap, path) = SCARG(uap, path);
275 	SCARG(&ouap, flags) = 0;
276 
277 	return (sys_unmount(p, &ouap, retval));
278 }
279 
280 /*
281  * Conversion table for SunOS NFS mount flags.
282  */
283 static struct {
284 	int	sun_flg;
285 	int	bsd_flg;
286 } sunnfs_flgtab[] = {
287 	{ SUNNFS_SOFT,		NFSMNT_SOFT },
288 	{ SUNNFS_WSIZE,		NFSMNT_WSIZE },
289 	{ SUNNFS_RSIZE,		NFSMNT_RSIZE },
290 	{ SUNNFS_TIMEO,		NFSMNT_TIMEO },
291 	{ SUNNFS_RETRANS,	NFSMNT_RETRANS },
292 	{ SUNNFS_HOSTNAME,	0 },			/* Ignored */
293 	{ SUNNFS_INT,		NFSMNT_INT },
294 	{ SUNNFS_NOAC,		0 },			/* Ignored */
295 	{ SUNNFS_ACREGMIN,	0 },			/* Ignored */
296 	{ SUNNFS_ACREGMAX,	0 },			/* Ignored */
297 	{ SUNNFS_ACDIRMIN,	0 },			/* Ignored */
298 	{ SUNNFS_ACDIRMAX,	0 },			/* Ignored */
299 	{ SUNNFS_SECURE,	0 },			/* Ignored */
300 	{ SUNNFS_NOCTO,		0 },			/* Ignored */
301 	{ SUNNFS_POSIX,		0 }			/* Ignored */
302 };
303 
304 int
305 sunos_sys_mount(p, v, retval)
306 	struct proc *p;
307 	void *v;
308 	register_t *retval;
309 {
310 	struct sunos_sys_mount_args *uap = v;
311 	struct emul *e = p->p_emul;
312 	int oflags = SCARG(uap, flags), nflags, error;
313 	char fsname[MFSNAMELEN];
314 
315 	if (oflags & (SUNM_NOSUB | SUNM_SYS5))
316 		return (EINVAL);
317 	if ((oflags & SUNM_NEWTYPE) == 0)
318 		return (EINVAL);
319 	nflags = 0;
320 	if (oflags & SUNM_RDONLY)
321 		nflags |= MNT_RDONLY;
322 	if (oflags & SUNM_NOSUID)
323 		nflags |= MNT_NOSUID;
324 	if (oflags & SUNM_REMOUNT)
325 		nflags |= MNT_UPDATE;
326 	SCARG(uap, flags) = nflags;
327 
328 	error = copyinstr((caddr_t)SCARG(uap, type), fsname,
329 	    sizeof fsname, (size_t *)0);
330 	if (error)
331 		return (error);
332 
333 	if (strncmp(fsname, "4.2", sizeof fsname) == 0) {
334 		SCARG(uap, type) = STACKGAPBASE;
335 		error = copyout("ffs", SCARG(uap, type), sizeof("ffs"));
336 		if (error)
337 			return (error);
338 	} else if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
339 		struct sunos_nfs_args sna;
340 		struct sockaddr_in sain;
341 		struct nfs_args na;
342 		struct sockaddr sa;
343 		int n;
344 
345 		error = copyin(SCARG(uap, data), &sna, sizeof sna);
346 		if (error)
347 			return (error);
348 		error = copyin(sna.addr, &sain, sizeof sain);
349 		if (error)
350 			return (error);
351 		bcopy(&sain, &sa, sizeof sa);
352 		sa.sa_len = sizeof(sain);
353 		SCARG(uap, data) = STACKGAPBASE;
354 		na.version = NFS_ARGSVERSION;
355 		na.addr = (struct sockaddr *)
356 			  ((int)SCARG(uap, data) + sizeof na);
357 		na.addrlen = sizeof(struct sockaddr);
358 		na.sotype = SOCK_DGRAM;
359 		na.proto = IPPROTO_UDP;
360 		na.fh = (void *)sna.fh;
361 		na.fhsize = NFSX_V2FH;
362 		na.flags = 0;
363 		n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
364 		while (--n >= 0)
365 			if (sna.flags & sunnfs_flgtab[n].sun_flg)
366 				na.flags |= sunnfs_flgtab[n].bsd_flg;
367 		na.wsize = sna.wsize;
368 		na.rsize = sna.rsize;
369 		if (na.flags & NFSMNT_RSIZE) {
370 			na.flags |= NFSMNT_READDIRSIZE;
371 			na.readdirsize = na.rsize;
372 		}
373 		na.timeo = sna.timeo;
374 		na.retrans = sna.retrans;
375 		na.hostname = sna.hostname;
376 
377 		error = copyout(&sa, na.addr, sizeof sa);
378 		if (error)
379 			return (error);
380 		error = copyout(&na, SCARG(uap, data), sizeof na);
381 		if (error)
382 			return (error);
383 	}
384 	return (sys_mount(p, (struct sys_mount_args *)uap, retval));
385 }
386 
387 #if defined(NFSCLIENT)
388 int
389 async_daemon(p, v, retval)
390 	struct proc *p;
391 	void *v;
392 	register_t *retval;
393 {
394 	struct sys_nfssvc_args ouap;
395 
396 	SCARG(&ouap, flag) = NFSSVC_BIOD;
397 	SCARG(&ouap, argp) = NULL;
398 
399 	return (sys_nfssvc(p, &ouap, retval));
400 }
401 #endif /* NFSCLIENT */
402 
403 int
404 sunos_sys_sigpending(p, v, retval)
405 	struct proc *p;
406 	void *v;
407 	register_t *retval;
408 {
409 	struct sunos_sys_sigpending_args *uap = v;
410 	int mask = p->p_siglist & p->p_sigmask;
411 
412 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
413 }
414 
415 /*
416  * Read Sun-style directory entries.  We suck them into kernel space so
417  * that they can be massaged before being copied out to user code.  Like
418  * SunOS, we squish out `empty' entries.
419  *
420  * This is quite ugly, but what do you expect from compatibility code?
421  */
422 int
423 sunos_sys_getdents(p, v, retval)
424 	struct proc *p;
425 	void *v;
426 	register_t *retval;
427 {
428 	struct sunos_sys_getdents_args *uap = v;
429 	struct dirent *bdp;
430 	struct vnode *vp;
431 	caddr_t inp, buf;	/* BSD-format */
432 	int len, reclen;	/* BSD-format */
433 	caddr_t outp;		/* Sun-format */
434 	int resid, sunos_reclen;/* Sun-format */
435 	struct file *fp;
436 	struct uio auio;
437 	struct iovec aiov;
438 	struct sunos_dirent idb;
439 	off_t off;			/* true file offset */
440 	int buflen, error, eofflag;
441 	u_long *cookiebuf, *cookie;
442 	int ncookies;
443 
444 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
445 		return (error);
446 
447 	if ((fp->f_flag & FREAD) == 0)
448 		return (EBADF);
449 
450 	vp = (struct vnode *)fp->f_data;
451 
452 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
453 		return (EINVAL);
454 
455 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
456 	buf = malloc(buflen, M_TEMP, M_WAITOK);
457 	ncookies = buflen / 16;
458 	cookiebuf = malloc(ncookies * sizeof(*cookiebuf), M_TEMP, M_WAITOK);
459 	VOP_LOCK(vp);
460 	off = fp->f_offset;
461 again:
462 	aiov.iov_base = buf;
463 	aiov.iov_len = buflen;
464 	auio.uio_iov = &aiov;
465 	auio.uio_iovcnt = 1;
466 	auio.uio_rw = UIO_READ;
467 	auio.uio_segflg = UIO_SYSSPACE;
468 	auio.uio_procp = p;
469 	auio.uio_resid = buflen;
470 	auio.uio_offset = off;
471 	/*
472 	 * First we read into the malloc'ed buffer, then
473 	 * we massage it into user space, one record at a time.
474 	 */
475 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookiebuf,
476 	    ncookies);
477 	if (error)
478 		goto out;
479 
480 	inp = buf;
481 	outp = SCARG(uap, buf);
482 	resid = SCARG(uap, nbytes);
483 	if ((len = buflen - auio.uio_resid) == 0)
484 		goto eof;
485 
486 	for (cookie = cookiebuf; len > 0; len -= reclen) {
487 		bdp = (struct dirent *)inp;
488 		reclen = bdp->d_reclen;
489 		if (reclen & 3)
490 			panic("sunos_getdents");
491 		off = *cookie++;	/* each entry points to next */
492 		if (bdp->d_fileno == 0) {
493 			inp += reclen;	/* it is a hole; squish it out */
494 			continue;
495 		}
496 		sunos_reclen = SUNOS_RECLEN(&idb, bdp->d_namlen);
497 		if (reclen > len || resid < sunos_reclen) {
498 			/* entry too big for buffer, so just stop */
499 			outp++;
500 			break;
501 		}
502 		/*
503 		 * Massage in place to make a Sun-shaped dirent (otherwise
504 		 * we have to worry about touching user memory outside of
505 		 * the copyout() call).
506 		 */
507 		idb.d_fileno = bdp->d_fileno;
508 		idb.d_off = off;
509 		idb.d_reclen = sunos_reclen;
510 		idb.d_namlen = bdp->d_namlen;
511 		strcpy(idb.d_name, bdp->d_name);
512 		if ((error = copyout((caddr_t)&idb, outp, sunos_reclen)) != 0)
513 			goto out;
514 		/* advance past this real entry */
515 		inp += reclen;
516 		/* advance output past Sun-shaped entry */
517 		outp += sunos_reclen;
518 		resid -= sunos_reclen;
519 	}
520 
521 	/* if we squished out the whole block, try again */
522 	if (outp == SCARG(uap, buf))
523 		goto again;
524 	fp->f_offset = off;		/* update the vnode offset */
525 
526 eof:
527 	*retval = SCARG(uap, nbytes) - resid;
528 out:
529 	VOP_UNLOCK(vp);
530 	free(cookiebuf, M_TEMP);
531 	free(buf, M_TEMP);
532 	return (error);
533 }
534 
535 #define	SUNOS__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
536 
537 int
538 sunos_sys_mmap(p, v, retval)
539 	register struct proc *p;
540 	void *v;
541 	register_t *retval;
542 {
543 	register struct sunos_sys_mmap_args *uap = v;
544 	struct sys_mmap_args ouap;
545 	register struct filedesc *fdp;
546 	register struct file *fp;
547 	register struct vnode *vp;
548 
549 	/*
550 	 * Verify the arguments.
551 	 */
552 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
553 		return (EINVAL);			/* XXX still needed? */
554 
555 	if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
556 		return (EINVAL);
557 
558 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
559 	SCARG(&ouap, addr) = SCARG(uap, addr);
560 
561 	if ((SCARG(&ouap, flags) & MAP_FIXED) == 0 &&
562 	    SCARG(&ouap, addr) != 0 &&
563 	    SCARG(&ouap, addr) < (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ))
564 		SCARG(&ouap, addr) = (caddr_t)round_page(p->p_vmspace->vm_daddr+MAXDSIZ);
565 
566 	SCARG(&ouap, len) = SCARG(uap, len);
567 	SCARG(&ouap, prot) = SCARG(uap, prot);
568 	SCARG(&ouap, fd) = SCARG(uap, fd);
569 	SCARG(&ouap, pos) = SCARG(uap, pos);
570 
571 	/*
572 	 * Special case: if fd refers to /dev/zero, map as MAP_ANON.  (XXX)
573 	 */
574 	fdp = p->p_fd;
575 	if ((unsigned)SCARG(&ouap, fd) < fdp->fd_nfiles &&		/*XXX*/
576 	    (fp = fdp->fd_ofiles[SCARG(&ouap, fd)]) != NULL &&		/*XXX*/
577 	    fp->f_type == DTYPE_VNODE &&				/*XXX*/
578 	    (vp = (struct vnode *)fp->f_data)->v_type == VCHR &&	/*XXX*/
579 	    iszerodev(vp->v_rdev)) {					/*XXX*/
580 		SCARG(&ouap, flags) |= MAP_ANON;
581 		SCARG(&ouap, fd) = -1;
582 	}
583 
584 	return (sys_mmap(p, &ouap, retval));
585 }
586 
587 #define	MC_SYNC		1
588 #define	MC_LOCK		2
589 #define	MC_UNLOCK	3
590 #define	MC_ADVISE	4
591 #define	MC_LOCKAS	5
592 #define	MC_UNLOCKAS	6
593 
594 int
595 sunos_sys_mctl(p, v, retval)
596 	register struct proc *p;
597 	void *v;
598 	register_t *retval;
599 {
600 	register struct sunos_sys_mctl_args *uap = v;
601 
602 	switch (SCARG(uap, func)) {
603 	case MC_ADVISE:		/* ignore for now */
604 		return (0);
605 	case MC_SYNC:		/* translate to msync */
606 		return (sys_msync(p, uap, retval));
607 	default:
608 		return (EINVAL);
609 	}
610 }
611 
612 int
613 sunos_sys_setsockopt(p, v, retval)
614 	struct proc *p;
615 	void *v;
616 	register_t *retval;
617 {
618 	register struct sunos_sys_setsockopt_args *uap = v;
619 	struct file *fp;
620 	struct mbuf *m = NULL;
621 	int error;
622 
623 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
624 		return (error);
625 #define	SO_DONTLINGER (~SO_LINGER)
626 	if (SCARG(uap, name) == SO_DONTLINGER) {
627 		m = m_get(M_WAIT, MT_SOOPTS);
628 		mtod(m, struct linger *)->l_onoff = 0;
629 		m->m_len = sizeof(struct linger);
630 		return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
631 		    SO_LINGER, m));
632 	}
633 	if (SCARG(uap, level) == IPPROTO_IP) {
634 #define		SUNOS_IP_MULTICAST_IF		2
635 #define		SUNOS_IP_MULTICAST_TTL		3
636 #define		SUNOS_IP_MULTICAST_LOOP		4
637 #define		SUNOS_IP_ADD_MEMBERSHIP		5
638 #define		SUNOS_IP_DROP_MEMBERSHIP	6
639 		static int ipoptxlat[] = {
640 			IP_MULTICAST_IF,
641 			IP_MULTICAST_TTL,
642 			IP_MULTICAST_LOOP,
643 			IP_ADD_MEMBERSHIP,
644 			IP_DROP_MEMBERSHIP
645 		};
646 		if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
647 		    SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
648 			SCARG(uap, name) =
649 			    ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
650 		}
651 	}
652 	if (SCARG(uap, valsize) > MLEN)
653 		return (EINVAL);
654 	if (SCARG(uap, val)) {
655 		m = m_get(M_WAIT, MT_SOOPTS);
656 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
657 		    (u_int)SCARG(uap, valsize));
658 		if (error) {
659 			(void) m_free(m);
660 			return (error);
661 		}
662 		m->m_len = SCARG(uap, valsize);
663 	}
664 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
665 	    SCARG(uap, name), m));
666 }
667 
668 int
669 sunos_sys_fchroot(p, v, retval)
670 	register struct proc *p;
671 	void *v;
672 	register_t *retval;
673 {
674 	register struct sunos_sys_fchroot_args *uap = v;
675 	register struct filedesc *fdp = p->p_fd;
676 	register struct vnode *vp;
677 	struct file *fp;
678 	int error;
679 
680 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
681 		return (error);
682 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
683 		return (error);
684 	vp = (struct vnode *)fp->f_data;
685 	VOP_LOCK(vp);
686 	if (vp->v_type != VDIR)
687 		error = ENOTDIR;
688 	else
689 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
690 	VOP_UNLOCK(vp);
691 	if (error)
692 		return (error);
693 	VREF(vp);
694 	if (fdp->fd_rdir != NULL)
695 		vrele(fdp->fd_rdir);
696 	fdp->fd_rdir = vp;
697 	return (0);
698 }
699 
700 /*
701  * XXX: This needs cleaning up.
702  */
703 int
704 sunos_sys_auditsys(p, v, retval)
705 	struct proc *p;
706 	void *v;
707 	register_t *retval;
708 {
709 	return 0;
710 }
711 
712 int
713 sunos_sys_uname(p, v, retval)
714 	struct proc *p;
715 	void *v;
716 	register_t *retval;
717 {
718 	struct sunos_sys_uname_args *uap = v;
719 	struct sunos_utsname sut;
720 	extern char ostype[], machine[], osrelease[];
721 
722 	bzero(&sut, sizeof(sut));
723 
724 	bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
725 	bcopy(hostname, sut.nodename, sizeof(sut.nodename));
726 	sut.nodename[sizeof(sut.nodename)-1] = '\0';
727 	bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
728 	bcopy("1", sut.version, sizeof(sut.version) - 1);
729 	bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
730 
731 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
732 	    sizeof(struct sunos_utsname));
733 }
734 
735 int
736 sunos_sys_setpgrp(p, v, retval)
737 	struct proc *p;
738 	void *v;
739 	register_t *retval;
740 {
741 	struct sunos_sys_setpgrp_args *uap = v;
742 
743 	/*
744 	 * difference to our setpgid call is to include backwards
745 	 * compatibility to pre-setsid() binaries. Do setsid()
746 	 * instead of setpgid() in those cases where the process
747 	 * tries to create a new session the old way.
748 	 */
749 	if (!SCARG(uap, pgid) &&
750 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
751 		return sys_setsid(p, uap, retval);
752 	else
753 		return sys_setpgid(p, uap, retval);
754 }
755 
756 int
757 sunos_sys_open(p, v, retval)
758 	struct proc *p;
759 	void *v;
760 	register_t *retval;
761 {
762 	struct sunos_sys_open_args *uap = v;
763 	int l, r;
764 	int noctty;
765 	int ret;
766 
767 	caddr_t sg = stackgap_init(p->p_emul);
768 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
769 
770 	/* convert mode into NetBSD mode */
771 	l = SCARG(uap, flags);
772 	noctty = l & 0x8000;
773 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
774 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
775 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
776 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
777 	r |=	((l & 0x2000) ? O_FSYNC : 0);
778 
779 	SCARG(uap, flags) = r;
780 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
781 
782 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
783 		struct filedesc *fdp = p->p_fd;
784 		struct file *fp = fdp->fd_ofiles[*retval];
785 
786 		/* ignore any error, just give it a try */
787 		if (fp->f_type == DTYPE_VNODE)
788 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
789 	}
790 	return ret;
791 }
792 
793 #if defined (NFSSERVER)
794 int
795 sunos_sys_nfssvc(p, v, retval)
796 	struct proc *p;
797 	void *v;
798 	register_t *retval;
799 {
800 #if 0
801 	struct sunos_sys_nfssvc_args *uap = v;
802 	struct emul *e = p->p_emul;
803 	struct sys_nfssvc_args outuap;
804 	struct sockaddr sa;
805 	int error;
806 
807 	bzero(&outuap, sizeof outuap);
808 	SCARG(&outuap, fd) = SCARG(uap, fd);
809 	SCARG(&outuap, mskval) = STACKGAPBASE;
810 	SCARG(&outuap, msklen) = sizeof sa;
811 	SCARG(&outuap, mtchval) = SCARG(&outuap, mskval) + sizeof sa;
812 	SCARG(&outuap, mtchlen) = sizeof sa;
813 
814 	bzero(&sa, sizeof sa);
815 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
816 		return (error);
817 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
818 		return (error);
819 
820 	return nfssvc(p, &outuap, retval);
821 #else
822 	return (ENOSYS);
823 #endif
824 }
825 #endif /* NFSSERVER */
826 
827 int
828 sunos_sys_ustat(p, v, retval)
829 	struct proc *p;
830 	void *v;
831 	register_t *retval;
832 {
833 	struct sunos_sys_ustat_args *uap = v;
834 	struct sunos_ustat us;
835 	int error;
836 
837 	bzero(&us, sizeof us);
838 
839 	/*
840 	 * XXX: should set f_tfree and f_tinode at least
841 	 * How do we translate dev -> fstat? (and then to sunos_ustat)
842 	 */
843 
844 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
845 		return (error);
846 	return 0;
847 }
848 
849 int
850 sunos_sys_quotactl(p, v, retval)
851 	struct proc *p;
852 	void *v;
853 	register_t *retval;
854 {
855 
856 	return EINVAL;
857 }
858 
859 int
860 sunos_sys_vhangup(p, v, retval)
861 	struct proc *p;
862 	void *v;
863 	register_t *retval;
864 {
865 	struct session *sp = p->p_session;
866 
867 	if (sp->s_ttyvp == 0)
868 		return 0;
869 
870 	if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
871 		pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
872 
873 	(void) ttywait(sp->s_ttyp);
874 	if (sp->s_ttyvp)
875 		vgoneall(sp->s_ttyvp);
876 	if (sp->s_ttyvp)
877 		vrele(sp->s_ttyvp);
878 	sp->s_ttyvp = NULL;
879 
880 	return 0;
881 }
882 
883 static int
884 sunstatfs(sp, buf)
885 	struct statfs *sp;
886 	caddr_t buf;
887 {
888 	struct sunos_statfs ssfs;
889 
890 	bzero(&ssfs, sizeof ssfs);
891 	ssfs.f_type = 0;
892 	ssfs.f_bsize = sp->f_bsize;
893 	ssfs.f_blocks = sp->f_blocks;
894 	ssfs.f_bfree = sp->f_bfree;
895 	ssfs.f_bavail = sp->f_bavail;
896 	ssfs.f_files = sp->f_files;
897 	ssfs.f_ffree = sp->f_ffree;
898 	ssfs.f_fsid = sp->f_fsid;
899 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
900 }
901 
902 int
903 sunos_sys_statfs(p, v, retval)
904 	struct proc *p;
905 	void *v;
906 	register_t *retval;
907 {
908 	struct sunos_sys_statfs_args *uap = v;
909 	register struct mount *mp;
910 	register struct statfs *sp;
911 	int error;
912 	struct nameidata nd;
913 
914 	caddr_t sg = stackgap_init(p->p_emul);
915 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
916 
917 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
918 	if ((error = namei(&nd)) != 0)
919 		return (error);
920 	mp = nd.ni_vp->v_mount;
921 	sp = &mp->mnt_stat;
922 	vrele(nd.ni_vp);
923 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
924 		return (error);
925 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
926 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
927 }
928 
929 int
930 sunos_sys_fstatfs(p, v, retval)
931 	struct proc *p;
932 	void *v;
933 	register_t *retval;
934 {
935 	struct sunos_sys_fstatfs_args *uap = v;
936 	struct file *fp;
937 	struct mount *mp;
938 	register struct statfs *sp;
939 	int error;
940 
941 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
942 		return (error);
943 	mp = ((struct vnode *)fp->f_data)->v_mount;
944 	sp = &mp->mnt_stat;
945 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
946 		return (error);
947 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
948 	return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
949 }
950 
951 int
952 sunos_sys_exportfs(p, v, retval)
953 	struct proc *p;
954 	void *v;
955 	register_t *retval;
956 {
957 	/*
958 	 * XXX: should perhaps translate into a mount(2)
959 	 * with MOUNT_EXPORT?
960 	 */
961 	return 0;
962 }
963 
964 int
965 sunos_sys_mknod(p, v, retval)
966 	struct proc *p;
967 	void *v;
968 	register_t *retval;
969 {
970 	struct sunos_sys_mknod_args *uap = v;
971 
972 	caddr_t sg = stackgap_init(p->p_emul);
973 	SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
974 
975 	if (S_ISFIFO(SCARG(uap, mode)))
976 		return sys_mkfifo(p, uap, retval);
977 
978 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
979 }
980 
981 #define SUNOS_SC_ARG_MAX	1
982 #define SUNOS_SC_CHILD_MAX	2
983 #define SUNOS_SC_CLK_TCK	3
984 #define SUNOS_SC_NGROUPS_MAX	4
985 #define SUNOS_SC_OPEN_MAX	5
986 #define SUNOS_SC_JOB_CONTROL	6
987 #define SUNOS_SC_SAVED_IDS	7
988 #define SUNOS_SC_VERSION	8
989 
990 int
991 sunos_sys_sysconf(p, v, retval)
992 	struct proc *p;
993 	void *v;
994 	register_t *retval;
995 {
996 	struct sunos_sys_sysconf_args *uap = v;
997 	extern int maxfiles;
998 
999 	switch(SCARG(uap, name)) {
1000 	case SUNOS_SC_ARG_MAX:
1001 		*retval = ARG_MAX;
1002 		break;
1003 	case SUNOS_SC_CHILD_MAX:
1004 		*retval = maxproc;
1005 		break;
1006 	case SUNOS_SC_CLK_TCK:
1007 		*retval = 60;		/* should this be `hz', ie. 100? */
1008 		break;
1009 	case SUNOS_SC_NGROUPS_MAX:
1010 		*retval = NGROUPS_MAX;
1011 		break;
1012 	case SUNOS_SC_OPEN_MAX:
1013 		*retval = maxfiles;
1014 		break;
1015 	case SUNOS_SC_JOB_CONTROL:
1016 		*retval = 1;
1017 		break;
1018 	case SUNOS_SC_SAVED_IDS:
1019 #ifdef _POSIX_SAVED_IDS
1020 		*retval = 1;
1021 #else
1022 		*retval = 0;
1023 #endif
1024 		break;
1025 	case SUNOS_SC_VERSION:
1026 		*retval = 198808;
1027 		break;
1028 	default:
1029 		return EINVAL;
1030 	}
1031 	return 0;
1032 }
1033 
1034 #define SUNOS_RLIMIT_NOFILE	6	/* Other RLIMIT_* are the same */
1035 #define SUNOS_RLIM_NLIMITS	7
1036 
1037 int
1038 sunos_sys_getrlimit(p, v, retval)
1039 	struct proc *p;
1040 	void *v;
1041 	register_t *retval;
1042 {
1043 	struct sunos_sys_getrlimit_args *uap = v;
1044 
1045 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1046 		return EINVAL;
1047 
1048 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1049 		SCARG(uap, which) = RLIMIT_NOFILE;
1050 
1051 	return compat_43_sys_getrlimit(p, uap, retval);
1052 }
1053 
1054 int
1055 sunos_sys_setrlimit(p, v, retval)
1056 	struct proc *p;
1057 	void *v;
1058 	register_t *retval;
1059 {
1060 	struct sunos_sys_getrlimit_args *uap = v;
1061 
1062 	if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1063 		return EINVAL;
1064 
1065 	if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1066 		SCARG(uap, which) = RLIMIT_NOFILE;
1067 
1068 	return compat_43_sys_setrlimit(p, uap, retval);
1069 }
1070 
1071 /* for the m68k machines */
1072 #ifndef PT_GETFPREGS
1073 #define PT_GETFPREGS -1
1074 #endif
1075 #ifndef PT_SETFPREGS
1076 #define PT_SETFPREGS -1
1077 #endif
1078 
1079 static int sreq2breq[] = {
1080 	PT_TRACE_ME,    PT_READ_I,      PT_READ_D,      -1,
1081 	PT_WRITE_I,     PT_WRITE_D,     -1,             PT_CONTINUE,
1082 	PT_KILL,        -1,             PT_ATTACH,      PT_DETACH,
1083 	PT_GETREGS,     PT_SETREGS,     PT_GETFPREGS,   PT_SETFPREGS
1084 };
1085 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1086 
1087 int
1088 sunos_sys_ptrace(p, v, retval)
1089 	struct proc *p;
1090 	void *v;
1091 	register_t *retval;
1092 {
1093 	struct sunos_sys_ptrace_args *uap = v;
1094 	struct sys_ptrace_args pa;
1095 	int req;
1096 
1097 	req = SCARG(uap, req);
1098 
1099 	if (req < 0 || req >= nreqs)
1100 		return (EINVAL);
1101 
1102 	req = sreq2breq[req];
1103 	if (req == -1)
1104 		return (EINVAL);
1105 
1106 	SCARG(&pa, req) = req;
1107 	SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1108 	SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1109 	SCARG(&pa, data) = SCARG(uap, data);
1110 
1111 	return sys_ptrace(p, &pa, retval);
1112 }
1113 
1114 /*
1115  * SunOS reboot system call (for compatibility).
1116  * Sun lets you pass in a boot string which the PROM
1117  * saves and provides to the next boot program.
1118  */
1119 
1120 #define SUNOS_RB_ASKNAME	0x001
1121 #define SUNOS_RB_SINGLE 	0x002
1122 #define SUNOS_RB_NOSYNC		0x004
1123 #define SUNOS_RB_HALT		0x008
1124 #define SUNOS_RB_DUMP		0x080
1125 #define	SUNOS_RB_STRING		0x200
1126 
1127 static struct sunos_howto_conv {
1128 	int sun_howto;
1129 	int bsd_howto;
1130 } sunos_howto_conv[] = {
1131 	{ SUNOS_RB_ASKNAME,	RB_ASKNAME },
1132 	{ SUNOS_RB_SINGLE,	RB_SINGLE },
1133 	{ SUNOS_RB_NOSYNC,	RB_NOSYNC },
1134 	{ SUNOS_RB_HALT,	RB_HALT },
1135 	{ SUNOS_RB_DUMP,	RB_DUMP },
1136 	{ SUNOS_RB_STRING,	RB_STRING },
1137 	{ 0x000,		0 },
1138 };
1139 
1140 int
1141 sunos_sys_reboot(p, v, retval)
1142 	struct proc *p;
1143 	void *v;
1144 	register_t *retval;
1145 {
1146 	struct sunos_sys_reboot_args *uap = v;
1147 	struct sys_reboot_args ua;
1148 	struct sunos_howto_conv *convp;
1149 	int error, bsd_howto, sun_howto;
1150 	char *bootstr;
1151 
1152 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1153 		return (error);
1154 
1155 	/*
1156 	 * Convert howto bits to BSD format.
1157 	 */
1158 	sun_howto = SCARG(uap, howto);
1159 	bsd_howto = 0;
1160 	convp = sunos_howto_conv;
1161 	while (convp->sun_howto) {
1162 		if (sun_howto & convp->sun_howto)
1163 			bsd_howto |= convp->bsd_howto;
1164 		convp++;
1165 	}
1166 
1167 	/*
1168 	 * Sun RB_STRING (Get user supplied bootstring.)
1169 	 * If the machine supports passing a string to the
1170 	 * next booted kernel.
1171 	 */
1172 	if (sun_howto & SUNOS_RB_STRING) {
1173 		char bs[128];
1174 
1175 		error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1176 
1177 		if (error)
1178 			bootstr = NULL;
1179 		else
1180 			bootstr = bs;
1181 	} else
1182 		bootstr = NULL;
1183 
1184 	SCARG(&ua, opt) = bsd_howto;
1185 	SCARG(&ua, bootstr) = bootstr;
1186 	sys_reboot(p, &ua, retval);
1187 	return(0);
1188 }
1189 
1190 /*
1191  * Generalized interface signal handler, 4.3-compatible.
1192  */
1193 /* ARGSUSED */
1194 int
1195 sunos_sys_sigvec(p, v, retval)
1196 	struct proc *p;
1197 	void *v;
1198 	register_t *retval;
1199 {
1200 	register struct sunos_sys_sigvec_args /* {
1201 		syscallarg(int) signum;
1202 		syscallarg(struct sigvec *) nsv;
1203 		syscallarg(struct sigvec *) osv;
1204 	} */ *uap = v;
1205 	struct sigvec vec;
1206 	register struct sigacts *ps = p->p_sigacts;
1207 	register struct sigvec *sv;
1208 	register int signum;
1209 	int bit, error;
1210 
1211 	signum = SCARG(uap, signum);
1212 	if (signum <= 0 || signum >= NSIG ||
1213 	    signum == SIGKILL || signum == SIGSTOP)
1214 		return (EINVAL);
1215 	sv = &vec;
1216 	if (SCARG(uap, osv)) {
1217 		*(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
1218 		sv->sv_mask = ps->ps_catchmask[signum];
1219 		bit = sigmask(signum);
1220 		sv->sv_flags = 0;
1221 		if ((ps->ps_sigonstack & bit) != 0)
1222 			sv->sv_flags |= SV_ONSTACK;
1223 		if ((ps->ps_sigintr & bit) != 0)
1224 			sv->sv_flags |= SV_INTERRUPT;
1225 		if ((ps->ps_sigreset & bit) != 0)
1226 			sv->sv_flags |= SA_RESETHAND;
1227 		sv->sv_mask &= ~bit;
1228 		error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, osv),
1229 		    sizeof (vec));
1230 		if (error)
1231 			return (error);
1232 	}
1233 	if (SCARG(uap, nsv)) {
1234 		error = copyin((caddr_t)SCARG(uap, nsv), (caddr_t)sv,
1235 		    sizeof (vec));
1236 		if (error)
1237 			return (error);
1238 		/*
1239 		 * SunOS uses the mask 0x0004 as SV_RESETHAND
1240 		 * meaning: `reset to SIG_DFL on delivery'.
1241 		 * We support only the bits in: 0xF
1242 		 * (those bits are the same as ours)
1243 		 */
1244 		if (sv->sv_flags & ~0xF)
1245 			return (EINVAL);
1246 		/* SunOS binaries have a user-mode trampoline. */
1247 		sv->sv_flags |= SA_USERTRAMP;
1248 		/* Convert sigvec:SV_INTERRUPT to sigaction:SA_RESTART */
1249 		sv->sv_flags ^= SA_RESTART;	/* same bit, inverted */
1250 		setsigvec(p, signum, (struct sigaction *)sv);
1251 	}
1252 	return (0);
1253 }
1254