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