xref: /netbsd-src/sys/compat/ultrix/ultrix_misc.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: ultrix_misc.c,v 1.92 2003/12/20 19:01:30 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1995, 1997 Jonathan Stone (hereinafter referred to as the author)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Jonathan Stone for
18  *      the NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1992, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  *
39  * This software was developed by the Computer Systems Engineering group
40  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
41  * contributed to Berkeley.
42  *
43  * All advertising materials mentioning features or use of this software
44  * must display the following acknowledgement:
45  *	This product includes software developed by the University of
46  *	California, Lawrence Berkeley Laboratory.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *
73  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
74  *
75  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: ultrix_misc.c,v 1.92 2003/12/20 19:01:30 fvdl Exp $");
80 
81 #if defined(_KERNEL_OPT)
82 #include "opt_nfsserver.h"
83 #include "opt_sysv.h"
84 #endif
85 
86 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
87 
88 /*
89  * Ultrix compatibility module.
90  *
91  * Ultrix system calls that are implemented differently in BSD are
92  * handled here.
93  */
94 
95 #if defined(_KERNEL_OPT)
96 #include "fs_nfs.h"
97 #endif
98 
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/namei.h>
102 #include <sys/dirent.h>
103 #include <sys/proc.h>
104 #include <sys/file.h>
105 #include <sys/filedesc.h>
106 #include <sys/kernel.h>
107 #include <sys/exec.h>
108 #include <sys/malloc.h>
109 #include <sys/mbuf.h>
110 #include <sys/mman.h>
111 #include <sys/mount.h>
112 #include <sys/resource.h>
113 #include <sys/resourcevar.h>
114 #include <sys/signal.h>
115 #include <sys/signalvar.h>
116 #include <sys/socket.h>
117 #include <sys/vnode.h>
118 #include <sys/uio.h>
119 #include <sys/wait.h>
120 #include <sys/utsname.h>
121 #include <sys/unistd.h>
122 #include <sys/ipc.h>
123 
124 #include <sys/sa.h>
125 #include <sys/syscallargs.h>
126 
127 #include <compat/ultrix/ultrix_syscall.h>
128 #include <compat/ultrix/ultrix_syscallargs.h>
129 #include <compat/common/compat_util.h>
130 
131 #include <netinet/in.h>
132 
133 #include <miscfs/specfs/specdev.h>
134 
135 #include <nfs/rpcv2.h>
136 #include <nfs/nfsproto.h>
137 #include <nfs/nfs.h>
138 
139 #include <sys/socketvar.h>				/* sosetopt() */
140 
141 #include <compat/ultrix/ultrix_flock.h>
142 
143 #ifdef __mips
144 #include <mips/cachectl.h>
145 #endif
146 
147 static int ultrix_to_bsd_flock __P((struct ultrix_flock *, struct flock *));
148 static void bsd_to_ultrix_flock __P((struct flock *, struct ultrix_flock *));
149 
150 extern struct sysent ultrix_sysent[];
151 extern const char * const ultrix_syscallnames[];
152 extern char ultrix_sigcode[], ultrix_esigcode[];
153 #ifdef __HAVE_SYSCALL_INTERN
154 void syscall_intern(struct proc *);
155 #else
156 void syscall __P((void));
157 #endif
158 
159 struct uvm_object *emul_ultrix_object;
160 
161 const struct emul emul_ultrix = {
162 	"ultrix",
163 	"/emul/ultrix",
164 #ifndef __HAVE_MINIMAL_EMUL
165 	0,
166 	NULL,
167 	ULTRIX_SYS_syscall,
168 	ULTRIX_SYS_NSYSENT,
169 #endif
170 	ultrix_sysent,
171 	ultrix_syscallnames,
172 	sendsig,
173 	trapsignal,
174 	NULL,
175 	ultrix_sigcode,
176 	ultrix_esigcode,
177 	&emul_ultrix_object,
178 	setregs,
179 	NULL,
180 	NULL,
181 	NULL,
182 	NULL,
183 	NULL,
184 #ifdef __HAVE_SYSCALL_INTERN
185 	syscall_intern,
186 #else
187 	syscall,
188 #endif
189 	NULL,
190 	NULL,
191 };
192 
193 #define GSI_PROG_ENV 1
194 
195 int
196 ultrix_sys_getsysinfo(l, v, retval)
197 	struct lwp *l;
198 	void *v;
199 	register_t *retval;
200 {
201 	struct ultrix_sys_getsysinfo_args *uap = v;
202 	static short progenv = 0;
203 
204 	switch (SCARG(uap, op)) {
205 		/* operations implemented: */
206 	case GSI_PROG_ENV:
207 		if (SCARG(uap, nbytes) < sizeof(short))
208 			return EINVAL;
209 		*retval = 1;
210 		return (copyout(&progenv, SCARG(uap, buffer), sizeof(short)));
211 	default:
212 		*retval = 0; /* info unavail */
213 		return 0;
214 	}
215 }
216 
217 int
218 ultrix_sys_setsysinfo(l, v, retval)
219 	struct lwp *l;
220 	void *v;
221 	register_t *retval;
222 {
223 
224 #ifdef notyet
225 	struct ultrix_sys_setsysinfo_args *uap = v;
226 #endif
227 
228 	*retval = 0;
229 	return 0;
230 }
231 
232 int
233 ultrix_sys_waitpid(l, v, retval)
234 	struct lwp *l;
235 	void *v;
236 	register_t *retval;
237 {
238 	struct ultrix_sys_waitpid_args *uap = v;
239 	struct sys_wait4_args ua;
240 
241 	SCARG(&ua, pid) = SCARG(uap, pid);
242 	SCARG(&ua, status) = SCARG(uap, status);
243 	SCARG(&ua, options) = SCARG(uap, options);
244 	SCARG(&ua, rusage) = 0;
245 
246 	return (sys_wait4(l, &ua, retval));
247 }
248 
249 int
250 ultrix_sys_wait3(l, v, retval)
251 	struct lwp *l;
252 	void *v;
253 	register_t *retval;
254 {
255 	struct ultrix_sys_wait3_args *uap = v;
256 	struct sys_wait4_args ua;
257 
258 	SCARG(&ua, pid) = -1;
259 	SCARG(&ua, status) = SCARG(uap, status);
260 	SCARG(&ua, options) = SCARG(uap, options);
261 	SCARG(&ua, rusage) = SCARG(uap, rusage);
262 
263 	return (sys_wait4(l, &ua, retval));
264 }
265 
266 /*
267  * Ultrix binaries pass in FD_MAX as the first arg to select().
268  * On Ultrix, FD_MAX is 4096, which is more than the NetBSD sys_select()
269  * can handle.
270  * Since we can't have more than the (native) FD_MAX descriptors open,
271  * limit nfds to at most FD_MAX.
272  */
273 int
274 ultrix_sys_select(l, v, retval)
275 	struct lwp *l;
276 	void *v;
277 	register_t *retval;
278 {
279 	struct sys_select_args *uap = v;
280 	struct timeval atv;
281 	int error;
282 
283 	/* Limit number of FDs selected on to the native maximum */
284 
285 	if (SCARG(uap, nd) > FD_SETSIZE)
286 		SCARG(uap, nd) = FD_SETSIZE;
287 
288 	/* Check for negative timeval */
289 	if (SCARG(uap, tv)) {
290 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
291 			       sizeof(atv));
292 		if (error)
293 			goto done;
294 #ifdef DEBUG
295 		/* Ultrix clients sometimes give negative timeouts? */
296 		if (atv.tv_sec < 0 || atv.tv_usec < 0)
297 			printf("ultrix select( %ld, %ld): negative timeout\n",
298 			    atv.tv_sec, atv.tv_usec);
299 #endif
300 
301 	}
302 	error = sys_select(l, (void*) uap, retval);
303 	if (error == EINVAL)
304 		printf("ultrix select: bad args?\n");
305 
306 done:
307 	return error;
308 }
309 
310 #if defined(NFS)
311 int
312 async_daemon(l, v, retval)
313 	struct lwp *l;
314 	void *v;
315 	register_t *retval;
316 {
317 	struct sys_nfssvc_args ouap;
318 
319 	SCARG(&ouap, flag) = NFSSVC_BIOD;
320 	SCARG(&ouap, argp) = NULL;
321 
322 	return (sys_nfssvc(l, &ouap, retval));
323 }
324 #endif /* NFS */
325 
326 
327 #define	SUN__MAP_NEW	0x80000000	/* if not, old mmap & cannot handle */
328 
329 int
330 ultrix_sys_mmap(l, v, retval)
331 	struct lwp *l;
332 	void *v;
333 	register_t *retval;
334 {
335 	struct ultrix_sys_mmap_args *uap = v;
336 	struct sys_mmap_args ouap;
337 
338 	/*
339 	 * Verify the arguments.
340 	 */
341 	if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
342 		return (EINVAL);			/* XXX still needed? */
343 
344 	if ((SCARG(uap, flags) & SUN__MAP_NEW) == 0)
345 		return (EINVAL);
346 
347 	SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUN__MAP_NEW;
348 	SCARG(&ouap, addr) = SCARG(uap, addr);
349 	SCARG(&ouap, len) = SCARG(uap, len);
350 	SCARG(&ouap, prot) = SCARG(uap, prot);
351 	SCARG(&ouap, fd) = SCARG(uap, fd);
352 	SCARG(&ouap, pos) = SCARG(uap, pos);
353 
354 	return (sys_mmap(l, &ouap, retval));
355 }
356 
357 int
358 ultrix_sys_setsockopt(l, v, retval)
359 	struct lwp *l;
360 	void *v;
361 	register_t *retval;
362 {
363 	struct ultrix_sys_setsockopt_args *uap = v;
364 	struct proc *p = l->l_proc;
365 	struct file *fp;
366 	struct mbuf *m = NULL;
367 	int error;
368 
369 	/* getsock() will use the descriptor for us */
370 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp))  != 0)
371 		return (error);
372 #define	SO_DONTLINGER (~SO_LINGER)
373 	if (SCARG(uap, name) == SO_DONTLINGER) {
374 		m = m_get(M_WAIT, MT_SOOPTS);
375 		mtod(m, struct linger *)->l_onoff = 0;
376 		m->m_len = sizeof(struct linger);
377 		error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
378 		    SO_LINGER, m);
379 	}
380 	if (SCARG(uap, level) == IPPROTO_IP) {
381 #define		EMUL_IP_MULTICAST_IF		2
382 #define		EMUL_IP_MULTICAST_TTL		3
383 #define		EMUL_IP_MULTICAST_LOOP		4
384 #define		EMUL_IP_ADD_MEMBERSHIP		5
385 #define		EMUL_IP_DROP_MEMBERSHIP	6
386 		static int ipoptxlat[] = {
387 			IP_MULTICAST_IF,
388 			IP_MULTICAST_TTL,
389 			IP_MULTICAST_LOOP,
390 			IP_ADD_MEMBERSHIP,
391 			IP_DROP_MEMBERSHIP
392 		};
393 		if (SCARG(uap, name) >= EMUL_IP_MULTICAST_IF &&
394 		    SCARG(uap, name) <= EMUL_IP_DROP_MEMBERSHIP) {
395 			SCARG(uap, name) =
396 			    ipoptxlat[SCARG(uap, name) - EMUL_IP_MULTICAST_IF];
397 		}
398 	}
399 	if (SCARG(uap, valsize) > MLEN) {
400 		error = EINVAL;
401 		goto out;
402 	}
403 	if (SCARG(uap, val)) {
404 		m = m_get(M_WAIT, MT_SOOPTS);
405 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
406 		    (u_int)SCARG(uap, valsize));
407 		if (error) {
408 			(void) m_free(m);
409 			goto out;
410 		}
411 		m->m_len = SCARG(uap, valsize);
412 	}
413 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
414 	    SCARG(uap, name), m);
415  out:
416 	FILE_UNUSE(fp, p);
417 	return (error);
418 }
419 
420 #define	ULTRIX__SYS_NMLN	32
421 
422 struct ultrix_utsname {
423 	char    sysname[ULTRIX__SYS_NMLN];
424 	char    nodename[ULTRIX__SYS_NMLN];
425 	char    release[ULTRIX__SYS_NMLN];
426 	char    version[ULTRIX__SYS_NMLN];
427 	char    machine[ULTRIX__SYS_NMLN];
428 };
429 
430 int
431 ultrix_sys_uname(l, v, retval)
432 	struct lwp *l;
433 	void *v;
434 	register_t *retval;
435 {
436 	struct ultrix_sys_uname_args *uap = v;
437 	struct ultrix_utsname sut;
438 	const char *cp;
439 	char *dp, *ep;
440 
441 	memset(&sut, 0, sizeof(sut));
442 
443 	strncpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
444 	strncpy(sut.nodename, hostname, sizeof(sut.nodename) - 1);
445 	strncpy(sut.release, osrelease, sizeof(sut.release) - 1);
446 	dp = sut.version;
447 	ep = &sut.version[sizeof(sut.version) - 1];
448 	for (cp = version; *cp && *cp != '('; cp++)
449 		;
450 	for (cp++; *cp && *cp != ')' && dp < ep; cp++)
451 		*dp++ = *cp;
452 	for (; *cp && *cp != '#'; cp++)
453 		;
454 	for (; *cp && *cp != ':' && dp < ep; cp++)
455 		*dp++ = *cp;
456 	*dp = '\0';
457 	strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
458 
459 	return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
460 	    sizeof(struct ultrix_utsname));
461 }
462 
463 int
464 ultrix_sys_setpgrp(l, v, retval)
465 	struct lwp *l;
466 	void *v;
467 	register_t *retval;
468 {
469 	struct ultrix_sys_setpgrp_args *uap = v;
470 	struct proc *p = l->l_proc;
471 
472 	/*
473 	 * difference to our setpgid call is to include backwards
474 	 * compatibility to pre-setsid() binaries. Do setsid()
475 	 * instead of setpgid() in those cases where the process
476 	 * tries to create a new session the old way.
477 	 */
478 	if (!SCARG(uap, pgid) &&
479 	    (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
480 		return sys_setsid(l, uap, retval);
481 	else
482 		return sys_setpgid(l, uap, retval);
483 }
484 
485 #if defined (NFSSERVER)
486 int
487 ultrix_sys_nfssvc(l, v, retval)
488 	struct lwp *l;
489 	void *v;
490 	register_t *retval;
491 {
492 
493 #if 0	/* XXX */
494 	struct ultrix_sys_nfssvc_args *uap = v;
495 	struct emul *e = p->p_emul;
496 	struct sys_nfssvc_args outuap;
497 	struct sockaddr sa;
498 	int error;
499 	caddr_t sg = stackgap_init(p, 0);
500 
501 	memset(&outuap, 0, sizeof outuap);
502 	SCARG(&outuap, fd) = SCARG(uap, fd);
503 	SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof sa);
504 	SCARG(&outuap, msklen) = sizeof sa;
505 	SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof sa);
506 	SCARG(&outuap, mtchlen) = sizeof sa;
507 
508 	memset(&sa, 0, sizeof sa);
509 	if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
510 		return (error);
511 	if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
512 		return (error);
513 
514 	return nfssvc(l, &outuap, retval);
515 #else
516 	return (ENOSYS);
517 #endif
518 }
519 #endif /* NFSSERVER */
520 
521 struct ultrix_ustat {
522 	daddr_t	f_tfree;	/* total free */
523 	ino_t	f_tinode;	/* total inodes free */
524 	char	f_fname[6];	/* filsys name */
525 	char	f_fpack[6];	/* filsys pack name */
526 };
527 
528 int
529 ultrix_sys_ustat(l, v, retval)
530 	struct lwp *l;
531 	void *v;
532 	register_t *retval;
533 {
534 	struct ultrix_sys_ustat_args *uap = v;
535 	struct ultrix_ustat us;
536 	int error;
537 
538 	memset(&us, 0, sizeof us);
539 
540 	/*
541 	 * XXX: should set f_tfree and f_tinode at least
542 	 * How do we translate dev -> fstat? (and then to ultrix_ustat)
543 	 */
544 
545 	if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
546 		return (error);
547 	return 0;
548 }
549 
550 int
551 ultrix_sys_quotactl(l, v, retval)
552 	struct lwp *l;
553 	void *v;
554 	register_t *retval;
555 {
556 
557 #ifdef notyet
558 	struct ultrix_sys_quotactl_args *uap = v;
559 #endif
560 
561 	return EINVAL;
562 }
563 
564 int
565 ultrix_sys_vhangup(l, v, retval)
566 	struct lwp *l;
567 	void *v;
568 	register_t *retval;
569 {
570 
571 	return 0;
572 }
573 
574 
575 /*
576  * RISC Ultrix cache control syscalls
577  */
578 #ifdef __mips
579 int
580 ultrix_sys_cacheflush(l, v, retval)
581 	struct lwp *l;
582 	void *v;
583 	register_t *retval;
584 {
585 	struct ultrix_sys_cacheflush_args /* {
586 		syscallarg(void *) addr;
587 		syscallarg(int) nbytes;
588 		syscallarg(int) flag;
589 	} */ *uap = v;
590 	struct proc *p = l->l_proc;
591 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
592 	int nbytes     = SCARG(uap, nbytes);
593 	int whichcache = SCARG(uap, whichcache);
594 
595 	return (mips_user_cacheflush(p, va, nbytes, whichcache));
596 }
597 
598 
599 int
600 ultrix_sys_cachectl(l, v, retval)
601 	struct lwp *l;
602 	void *v;
603 	register_t *retval;
604 {
605 	struct ultrix_sys_cachectl_args /* {
606 		syscallarg(void *) addr;
607 		syscallarg(int) nbytes;
608 		syscallarg(int) cacheop;
609 	} */ *uap = v;
610 	struct proc *p = l->l_proc;
611 	vaddr_t va  = (vaddr_t)SCARG(uap, addr);
612 	int nbytes  = SCARG(uap, nbytes);
613 	int cacheop = SCARG(uap, cacheop);
614 
615 	return mips_user_cachectl(p, va, nbytes, cacheop);
616 }
617 
618 #endif	/* __mips */
619 
620 
621 int
622 ultrix_sys_exportfs(l, v, retval)
623 	struct lwp *l;
624 	void *v;
625 	register_t *retval;
626 {
627 #ifdef notyet
628 	struct ultrix_sys_exportfs_args *uap = v;
629 #endif
630 
631 	/*
632 	 * XXX: should perhaps translate into a mount(2)
633 	 * with MOUNT_EXPORT?
634 	 */
635 	return 0;
636 }
637 
638 int
639 ultrix_sys_sigpending(l, v, retval)
640 	struct lwp *l;
641 	void *v;
642 	register_t *retval;
643 {
644 	struct ultrix_sys_sigpending_args *uap = v;
645 	sigset_t ss;
646 	int mask;
647 
648 	sigpending1(l->l_proc, &ss);
649 	mask = ss.__bits[0];
650 
651 	return (copyout((caddr_t)&mask, (caddr_t)SCARG(uap, mask), sizeof(int)));
652 }
653 
654 int
655 ultrix_sys_sigreturn(l, v, retval)
656 	struct lwp *l;
657 	void *v;
658 	register_t *retval;
659 {
660 	struct ultrix_sys_sigreturn_args *uap = v;
661 
662 	/* struct sigcontext13 is close enough to Ultrix */
663 
664 	return (compat_13_sys_sigreturn(l, uap, retval));
665 }
666 
667 int
668 ultrix_sys_sigcleanup(l, v, retval)
669 	struct lwp *l;
670 	void *v;
671 	register_t *retval;
672 {
673 	struct ultrix_sys_sigcleanup_args *uap = v;
674 
675 	/* struct sigcontext13 is close enough to Ultrix */
676 
677 	return (compat_13_sys_sigreturn(l, uap, retval));
678 }
679 
680 int
681 ultrix_sys_sigsuspend(l, v, retval)
682 	struct lwp *l;
683 	void *v;
684 	register_t *retval;
685 {
686 	struct ultrix_sys_sigsuspend_args *uap = v;
687 	int mask = SCARG(uap, mask);
688 	sigset_t ss;
689 
690 	ss.__bits[0] = mask;
691 	ss.__bits[1] = 0;
692 	ss.__bits[2] = 0;
693 	ss.__bits[3] = 0;
694 
695 	return (sigsuspend1(l->l_proc, &ss));
696 }
697 
698 #define ULTRIX_SV_ONSTACK 0x0001  /* take signal on signal stack */
699 #define ULTRIX_SV_INTERRUPT 0x0002  /* do not restart system on signal return */
700 #define ULTRIX_SV_OLDSIG 0x1000  /* Emulate old signal() for POSIX */
701 
702 int
703 ultrix_sys_sigvec(l, v, retval)
704 	struct lwp *l;
705 	void *v;
706 	register_t *retval;
707 {
708 	struct ultrix_sys_sigvec_args *uap = v;
709 	struct sigvec nsv, osv;
710 	struct sigaction nsa, osa;
711 	int error;
712 
713 	if (SCARG(uap, nsv)) {
714 		error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
715 		if (error)
716 			return (error);
717 		nsa.sa_handler = nsv.sv_handler;
718 #if 0 /* documentation */
719 		/* ONSTACK is identical */
720 		nsa.sa_flags = nsv.sv_flags & ULTRIX_SV_ONSTACK;
721 		if ((nsv.sv_flags & ULTRIX_SV_OLDSIG)
722 		    /* old signal() - always restart */
723 		    || (!(nsv.sv_flags & ULTRIX_SV_INTERRUPT))
724 		    /* inverted meaning (same bit) */
725 		    )
726 			nsa.sa_flags |= SA_RESTART;
727 #else /* optimized - assuming ULTRIX_SV_OLDSIG=>!ULTRIX_SV_INTERRUPT */
728 		nsa.sa_flags = nsv.sv_flags & ~ULTRIX_SV_OLDSIG;
729 		nsa.sa_flags ^= SA_RESTART;
730 #endif
731 		native_sigset13_to_sigset(&nsv.sv_mask, &nsa.sa_mask);
732 	}
733 	error = sigaction1(l->l_proc, SCARG(uap, signum),
734 	    SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
735 	    NULL, 0);
736 	if (error)
737 		return (error);
738 	if (SCARG(uap, osv)) {
739 		osv.sv_handler = osa.sa_handler;
740 		osv.sv_flags = osa.sa_flags ^ SA_RESTART;
741 		osv.sv_flags &= (ULTRIX_SV_ONSTACK | ULTRIX_SV_INTERRUPT);
742 		native_sigset_to_sigset13(&osa.sa_mask, &osv.sv_mask);
743 		error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
744 		if (error)
745 			return (error);
746 	}
747 	return (0);
748 }
749 
750 int
751 ultrix_sys_shmsys(l, v, retval)
752 	struct lwp *l;
753 	void *v;
754 	register_t *retval;
755 {
756 
757 #ifdef SYSVSHM
758 
759 	/* Ultrix SVSHM weirndess: */
760 	struct ultrix_sys_shmsys_args *uap = v;
761 	struct sys_shmat_args shmat_args;
762 	struct compat_14_sys_shmctl_args shmctl_args;
763 	struct sys_shmdt_args shmdt_args;
764 	struct sys_shmget_args shmget_args;
765 
766 
767 	switch (SCARG(uap, shmop)) {
768 	case 0:						/* Ultrix shmat() */
769 		SCARG(&shmat_args, shmid) = SCARG(uap, a2);
770 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
771 		SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
772 		return (sys_shmat(l, &shmat_args, retval));
773 
774 	case 1:						/* Ultrix shmctl() */
775 		SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
776 		SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
777 		SCARG(&shmctl_args, buf) = (struct shmid_ds14 *)SCARG(uap, a4);
778 		return (compat_14_sys_shmctl(l, &shmctl_args, retval));
779 
780 	case 2:						/* Ultrix shmdt() */
781 		SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
782 		return (sys_shmdt(l, &shmdt_args, retval));
783 
784 	case 3:						/* Ultrix shmget() */
785 		SCARG(&shmget_args, key) = SCARG(uap, a2);
786 		SCARG(&shmget_args, size) = SCARG(uap, a3);
787 		SCARG(&shmget_args, shmflg) = SCARG(uap, a4)
788 		    & (IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
789 		return (sys_shmget(l, &shmget_args, retval));
790 
791 	default:
792 		return (EINVAL);
793 	}
794 #else
795 	return (EOPNOTSUPP);
796 #endif	/* SYSVSHM */
797 }
798 
799 static int
800 ultrix_to_bsd_flock(ufl, fl)
801 	struct ultrix_flock *ufl;
802 	struct flock *fl;
803 {
804 
805 	fl->l_start = ufl->l_start;
806 	fl->l_len = ufl->l_len;
807 	fl->l_pid = ufl->l_pid;
808 	fl->l_whence = ufl->l_whence;
809 
810 	switch (ufl->l_type) {
811 	case ULTRIX_F_RDLCK:
812 		fl->l_type = F_RDLCK;
813 		break;
814 	case ULTRIX_F_WRLCK:
815 		fl->l_type = F_WRLCK;
816 		break;
817 	case ULTRIX_F_UNLCK:
818 		fl->l_type = F_UNLCK;
819 		break;
820 	default:
821 		return (EINVAL);
822 	}
823 
824 	return (0);
825 }
826 
827 static void
828 bsd_to_ultrix_flock(fl, ufl)
829 	struct flock *fl;
830 	struct ultrix_flock *ufl;
831 {
832 
833 	ufl->l_start = fl->l_start;
834 	ufl->l_len = fl->l_len;
835 	ufl->l_pid = fl->l_pid;
836 	ufl->l_whence = fl->l_whence;
837 
838 	switch (fl->l_type) {
839 	case F_RDLCK:
840 		ufl->l_type = ULTRIX_F_RDLCK;
841 		break;
842 	case F_WRLCK:
843 		ufl->l_type = ULTRIX_F_WRLCK;
844 		break;
845 	case F_UNLCK:
846 		ufl->l_type = ULTRIX_F_UNLCK;
847 		break;
848 	}
849 }
850 
851 int
852 ultrix_sys_fcntl(l, v, retval)
853 	struct lwp *l;
854 	void *v;
855 	register_t *retval;
856 {
857 	struct ultrix_sys_fcntl_args *uap = v;
858 	struct proc *p = l->l_proc;
859 	int error;
860 	struct ultrix_flock ufl;
861 	struct flock fl, *flp = NULL;	/* XXX gcc */
862 	caddr_t sg;
863 	struct sys_fcntl_args *args, fca;
864 
865 	switch (SCARG(uap, cmd)) {
866 	case F_GETLK:
867 	case F_SETLK:
868 	case F_SETLKW:
869 		error = copyin(SCARG(uap, arg), &ufl, sizeof(ufl));
870 		if (error)
871 			return (error);
872 		error = ultrix_to_bsd_flock(&ufl, &fl);
873 		if (error)
874 			return (error);
875 		sg = stackgap_init(p, 0);
876 		flp = (struct flock *)stackgap_alloc(p, &sg, sizeof(*flp));
877 		error = copyout(&fl, flp, sizeof(*flp));
878 		if (error)
879 			return (error);
880 
881 		SCARG(&fca, fd) = SCARG(uap, fd);
882 		SCARG(&fca, cmd) = SCARG(uap, cmd);
883 		SCARG(&fca, arg) = flp;
884 		args = &fca;
885 		break;
886 	default:
887 		args = v;
888 		break;
889 	}
890 
891 	error = sys_fcntl(l, args, retval);
892 	if (error)
893 		return (error);
894 
895 	switch (SCARG(uap, cmd)) {
896 	case F_GETLK:
897 		error = copyin(flp, &fl, sizeof(fl));
898 		if (error)
899 			return (error);
900 		bsd_to_ultrix_flock(&fl, &ufl);
901 		error = copyout(&ufl, SCARG(uap, arg), sizeof(ufl));
902 		break;
903 	}
904 
905 	return (error);
906 }
907