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