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