1 /* $NetBSD: ultrix_misc.c,v 1.126 2021/09/07 11:43:05 riastradh 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.126 2021/09/07 11:43:05 riastradh Exp $");
80
81 #if defined(_KERNEL_OPT)
82 #include "opt_sysv.h"
83 #endif
84
85 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
86
87 /*
88 * Ultrix compatibility module.
89 *
90 * Ultrix system calls that are implemented differently in BSD are
91 * handled here.
92 */
93
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/namei.h>
97 #include <sys/dirent.h>
98 #include <sys/proc.h>
99 #include <sys/file.h>
100 #include <sys/filedesc.h>
101 #include <sys/kernel.h>
102 #include <sys/exec.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/mman.h>
106 #include <sys/mount.h>
107 #include <sys/resource.h>
108 #include <sys/resourcevar.h>
109 #include <sys/signal.h>
110 #include <sys/signalvar.h>
111 #include <sys/socket.h>
112 #include <sys/vnode.h>
113 #include <sys/uio.h>
114 #include <sys/wait.h>
115 #include <sys/utsname.h>
116 #include <sys/unistd.h>
117 #include <sys/ipc.h>
118
119 #include <sys/syscallargs.h>
120
121 #include <uvm/uvm_extern.h>
122
123 #include <compat/ultrix/ultrix_syscall.h>
124 #include <compat/ultrix/ultrix_syscallargs.h>
125 #include <compat/common/compat_util.h>
126 #include <compat/sys/time.h>
127
128 #include <netinet/in.h>
129
130 #include <miscfs/specfs/specdev.h>
131
132 #include <nfs/rpcv2.h>
133 #include <nfs/nfsproto.h>
134 #include <nfs/nfs.h>
135
136 #include <sys/socketvar.h> /* sosetopt() */
137
138 #include <compat/ultrix/ultrix_flock.h>
139
140 #include <compat/sys/signal.h>
141 #include <compat/sys/signalvar.h>
142
143 #ifdef __mips
144 #include <mips/cachectl.h>
145 #include <mips/frame.h>
146 #endif
147
148 static int ultrix_to_bsd_flock(struct ultrix_flock *, struct flock *);
149 static void bsd_to_ultrix_flock(struct flock *, struct ultrix_flock *);
150
151 extern struct sysent ultrix_sysent[];
152 extern const uint32_t ultrix_sysent_nomodbits[];
153 extern const char * const ultrix_syscallnames[];
154 extern char ultrix_sigcode[], ultrix_esigcode[];
155
156 struct uvm_object *emul_ultrix_object;
157
158 #ifndef __HAVE_SYSCALL_INTERN
159 void syscall(void);
160 #endif
161
162 struct emul emul_ultrix = {
163 .e_name = "ultrix",
164 .e_path = "/emul/ultrix",
165 #ifndef __HAVE_MINIMAL_EMUL
166 .e_flags = 0,
167 .e_errno = NULL,
168 .e_nosys = ULTRIX_SYS_syscall,
169 .e_nsysent = ULTRIX_SYS_NSYSENT,
170 #endif
171 .e_sysent = ultrix_sysent,
172 .e_nomodbits = ultrix_sysent_nomodbits,
173 #ifdef SYSCALL_DEBUG
174 .e_syscallnames = ultrix_syscallnames,
175 #else
176 .e_syscallnames = NULL,
177 #endif
178 #ifdef __mips
179 .e_sendsig = sendsig_sigcontext,
180 #else /* vax */
181 .e_sendsig = sendsig,
182 #endif
183 .e_trapsignal = trapsignal,
184 .e_sigcode = ultrix_sigcode,
185 .e_esigcode = ultrix_esigcode,
186 .e_sigobject = &emul_ultrix_object,
187 .e_setregs = setregs,
188 .e_proc_exec = NULL,
189 .e_proc_fork = NULL,
190 .e_proc_exit = NULL,
191 .e_lwp_fork = NULL,
192 .e_lwp_exit = NULL,
193 #ifdef __HAVE_SYSCALL_INTERN
194 .e_syscall_intern = syscall_intern,
195 #else
196 .e_syscall_intern = syscall,
197 #endif
198 .e_sysctlovly = NULL,
199 .e_vm_default_addr = uvm_default_mapaddr,
200 .e_usertrap = NULL,
201 .e_ucsize = 0,
202 .e_startlwp = NULL
203 };
204
205 #define GSI_PROG_ENV 1
206
207 int
ultrix_sys_getsysinfo(struct lwp * l,const struct ultrix_sys_getsysinfo_args * uap,register_t * retval)208 ultrix_sys_getsysinfo(struct lwp *l, const struct ultrix_sys_getsysinfo_args *uap, register_t *retval)
209 {
210 static short progenv = 0;
211
212 switch (SCARG(uap, op)) {
213 /* operations implemented: */
214 case GSI_PROG_ENV:
215 if (SCARG(uap, nbytes) < sizeof(short))
216 return EINVAL;
217 *retval = 1;
218 return copyout(&progenv, SCARG(uap, buffer), sizeof(progenv));
219 default:
220 *retval = 0; /* info unavail */
221 return 0;
222 }
223 }
224
225 int
ultrix_sys_setsysinfo(struct lwp * l,const struct ultrix_sys_setsysinfo_args * uap,register_t * retval)226 ultrix_sys_setsysinfo(struct lwp *l, const struct ultrix_sys_setsysinfo_args *uap, register_t *retval)
227 {
228
229 *retval = 0;
230 return 0;
231 }
232
233 int
ultrix_sys_waitpid(struct lwp * l,const struct ultrix_sys_waitpid_args * uap,register_t * retval)234 ultrix_sys_waitpid(struct lwp *l, const struct ultrix_sys_waitpid_args *uap, register_t *retval)
235 {
236 struct compat_50_sys_wait4_args ap;
237
238 SCARG(&ap, pid) = SCARG(uap, pid);
239 SCARG(&ap, status) = SCARG(uap, status);
240 SCARG(&ap, options) = SCARG(uap, options);
241 SCARG(&ap, rusage) = 0;
242
243 return compat_50_sys_wait4(l, &ap, retval);
244 }
245
246 int
ultrix_sys_wait3(struct lwp * l,const struct ultrix_sys_wait3_args * uap,register_t * retval)247 ultrix_sys_wait3(struct lwp *l, const struct ultrix_sys_wait3_args *uap, register_t *retval)
248 {
249 struct compat_50_sys_wait4_args ap;
250
251 SCARG(&ap, pid) = -1;
252 SCARG(&ap, status) = SCARG(uap, status);
253 SCARG(&ap, options) = SCARG(uap, options);
254 SCARG(&ap, rusage) = SCARG(uap, rusage);
255
256 return compat_50_sys_wait4(l, &ap, retval);
257 }
258
259 /*
260 * Ultrix binaries pass in FD_MAX as the first arg to select().
261 * On Ultrix, FD_MAX is 4096, which is more than the NetBSD sys_select()
262 * can handle.
263 * Since we can't have more than the (native) FD_MAX descriptors open,
264 * limit nfds to at most FD_MAX.
265 */
266 int
ultrix_sys_select(struct lwp * l,const struct ultrix_sys_select_args * uap,register_t * retval)267 ultrix_sys_select(struct lwp *l, const struct ultrix_sys_select_args *uap, register_t *retval)
268 {
269 struct timeval50 atv;
270 int error;
271 struct compat_50_sys_select_args ap;
272
273 /* Limit number of FDs selected on to the native maximum */
274
275 if (SCARG(uap, nd) > FD_SETSIZE)
276 SCARG(&ap, nd) = FD_SETSIZE;
277 else
278 SCARG(&ap, nd) = SCARG(uap, nd);
279
280 SCARG(&ap, in) = SCARG(uap, in);
281 SCARG(&ap, ou) = SCARG(uap, ou);
282 SCARG(&ap, ex) = SCARG(uap, ex);
283 SCARG(&ap, tv) = SCARG(uap, tv);
284 /* Check for negative timeval */
285 if (SCARG(&ap, tv)) {
286 error = copyin(SCARG(uap, tv), &atv, sizeof(atv));
287 if (error)
288 goto done;
289 #ifdef DEBUG
290 /* Ultrix clients sometimes give negative timeouts? */
291 if (atv.tv_sec < 0 || atv.tv_usec < 0)
292 printf("ultrix select( %ld, %ld): negative timeout\n",
293 atv.tv_sec, atv.tv_usec);
294 #endif
295
296 }
297 error = compat_50_sys_select(l, &ap, retval);
298 if (error == EINVAL)
299 printf("ultrix select: bad args?\n");
300
301 done:
302 return error;
303 }
304
305
306 #define SUN__MAP_NEW 0x80000000 /* if not, old mmap & cannot handle */
307
308 int
ultrix_sys_mmap(struct lwp * l,const struct ultrix_sys_mmap_args * uap,register_t * retval)309 ultrix_sys_mmap(struct lwp *l, const struct ultrix_sys_mmap_args *uap, register_t *retval)
310 {
311 struct sys_mmap_args ouap;
312
313 /*
314 * Verify the arguments.
315 */
316 if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
317 return EINVAL; /* XXX still needed? */
318
319 if ((SCARG(uap, flags) & SUN__MAP_NEW) == 0)
320 return EINVAL;
321
322 SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUN__MAP_NEW;
323 SCARG(&ouap, addr) = SCARG(uap, addr);
324 SCARG(&ouap, len) = SCARG(uap, len);
325 SCARG(&ouap, prot) = SCARG(uap, prot);
326 SCARG(&ouap, fd) = SCARG(uap, fd);
327 SCARG(&ouap, pos) = SCARG(uap, pos);
328
329 return sys_mmap(l, &ouap, retval);
330 }
331
332 int
ultrix_sys_setsockopt(struct lwp * l,const struct ultrix_sys_setsockopt_args * uap,register_t * retval)333 ultrix_sys_setsockopt(struct lwp *l, const struct ultrix_sys_setsockopt_args *uap, register_t *retval)
334 {
335 struct sockopt sopt;
336 struct socket *so;
337 int error;
338 struct sys_setsockopt_args ap;
339
340 SCARG(&ap, s) = SCARG(uap, s);
341 SCARG(&ap, level) = SCARG(uap, level);
342 SCARG(&ap, name) = SCARG(uap, name);
343 SCARG(&ap, val) = SCARG(uap, val);
344 SCARG(&ap, valsize) = SCARG(uap, valsize);
345
346 /* fd_getsock() will use the descriptor for us */
347 if ((error = fd_getsock(SCARG(&ap, s), &so)) != 0)
348 return error;
349 #define SO_DONTLINGER (~SO_LINGER)
350 if (SCARG(&ap, name) == SO_DONTLINGER) {
351 struct linger lg;
352
353 lg.l_onoff = 0;
354 error = so_setsockopt(l, so, SCARG(&ap, level), SO_LINGER,
355 &lg, sizeof(lg));
356 goto out;
357 }
358 if (SCARG(&ap, level) == IPPROTO_IP) {
359 #define EMUL_IP_MULTICAST_IF 2
360 #define EMUL_IP_MULTICAST_TTL 3
361 #define EMUL_IP_MULTICAST_LOOP 4
362 #define EMUL_IP_ADD_MEMBERSHIP 5
363 #define EMUL_IP_DROP_MEMBERSHIP 6
364 static const int ipoptxlat[] = {
365 IP_MULTICAST_IF,
366 IP_MULTICAST_TTL,
367 IP_MULTICAST_LOOP,
368 IP_ADD_MEMBERSHIP,
369 IP_DROP_MEMBERSHIP
370 };
371 if (SCARG(&ap, name) >= EMUL_IP_MULTICAST_IF &&
372 SCARG(&ap, name) <= EMUL_IP_DROP_MEMBERSHIP) {
373 SCARG(&ap, name) =
374 ipoptxlat[SCARG(&ap, name) - EMUL_IP_MULTICAST_IF];
375 }
376 }
377 if (SCARG(&ap, valsize) > MLEN) {
378 error = EINVAL;
379 goto out;
380 }
381 sockopt_init(&sopt, SCARG(&ap, level), SCARG(&ap, name),
382 SCARG(&ap, valsize));
383 if (SCARG(&ap, val)) {
384 error = copyin(SCARG(&ap, val), sopt.sopt_data,
385 (u_int)SCARG(&ap, valsize));
386 }
387 if (error == 0)
388 error = sosetopt(so, &sopt);
389 sockopt_destroy(&sopt);
390 out:
391 fd_putfile(SCARG(uap, s));
392 return error;
393 }
394
395 #define ULTRIX__SYS_NMLN 32
396
397 struct ultrix_utsname {
398 char sysname[ULTRIX__SYS_NMLN];
399 char nodename[ULTRIX__SYS_NMLN];
400 char release[ULTRIX__SYS_NMLN];
401 char version[ULTRIX__SYS_NMLN];
402 char machine[ULTRIX__SYS_NMLN];
403 };
404
405 int
ultrix_sys_uname(struct lwp * l,const struct ultrix_sys_uname_args * uap,register_t * retval)406 ultrix_sys_uname(struct lwp *l, const struct ultrix_sys_uname_args *uap, register_t *retval)
407 {
408 struct ultrix_utsname sut;
409 const char *cp;
410 char *dp, *ep;
411
412 memset(&sut, 0, sizeof(sut));
413
414 strncpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
415 strncpy(sut.nodename, hostname, sizeof(sut.nodename) - 1);
416 strncpy(sut.release, osrelease, sizeof(sut.release) - 1);
417 dp = sut.version;
418 ep = &sut.version[sizeof(sut.version) - 1];
419 for (cp = version; *cp && *cp != '('; cp++)
420 ;
421 for (cp++; *cp && *cp != ')' && dp < ep; cp++)
422 *dp++ = *cp;
423 for (; *cp && *cp != '#'; cp++)
424 ;
425 for (; *cp && *cp != ':' && dp < ep; cp++)
426 *dp++ = *cp;
427 *dp = '\0';
428 strncpy(sut.machine, machine, sizeof(sut.machine) - 1);
429
430 return copyout(&sut, SCARG(uap, name), sizeof(sut));
431 }
432
433 int
ultrix_sys_setpgrp(struct lwp * l,const struct ultrix_sys_setpgrp_args * uap,register_t * retval)434 ultrix_sys_setpgrp(struct lwp *l, const struct ultrix_sys_setpgrp_args *uap, register_t *retval)
435 {
436 struct proc *p = l->l_proc;
437 struct sys_setpgid_args ap;
438
439 SCARG(&ap, pid) = SCARG(uap, pid);
440 SCARG(&ap, pgid) = SCARG(uap, pgid);
441 /*
442 * difference to our setpgid call is to include backwards
443 * compatibility to pre-setsid() binaries. Do setsid()
444 * instead of setpgid() in those cases where the process
445 * tries to create a new session the old way.
446 */
447 if (!SCARG(&ap, pgid) &&
448 (!SCARG(&ap, pid) || SCARG(&ap, pid) == p->p_pid))
449 return sys_setsid(l, &ap, retval);
450 else
451 return sys_setpgid(l, &ap, retval);
452 }
453
454 struct ultrix_ustat {
455 daddr_t f_tfree; /* total free */
456 uint32_t f_tinode; /* total inodes free */
457 char f_fname[6]; /* filsys name */
458 char f_fpack[6]; /* filsys pack name */
459 };
460
461 int
ultrix_sys_ustat(struct lwp * l,const struct ultrix_sys_ustat_args * uap,register_t * retval)462 ultrix_sys_ustat(struct lwp *l, const struct ultrix_sys_ustat_args *uap, register_t *retval)
463 {
464 struct ultrix_ustat us;
465 int error;
466
467 memset(&us, 0, sizeof us);
468
469 /*
470 * XXX: should set f_tfree and f_tinode at least
471 * How do we translate dev -> fstat? (and then to ultrix_ustat)
472 */
473
474 if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
475 return error;
476 return 0;
477 }
478
479 int
ultrix_sys_quotactl(struct lwp * l,const struct ultrix_sys_quotactl_args * uap,register_t * retval)480 ultrix_sys_quotactl(struct lwp *l, const struct ultrix_sys_quotactl_args *uap, register_t *retval)
481 {
482
483 return EINVAL;
484 }
485
486 int
ultrix_sys_vhangup(struct lwp * l,const void * uap,register_t * retval)487 ultrix_sys_vhangup(struct lwp *l, const void *uap, register_t *retval)
488 {
489
490 return 0;
491 }
492
493
494 /*
495 * RISC Ultrix cache control syscalls
496 */
497 #ifdef __mips
498 int
ultrix_sys_cacheflush(struct lwp * l,const struct ultrix_sys_cacheflush_args * uap,register_t * retval)499 ultrix_sys_cacheflush(struct lwp *l, const struct ultrix_sys_cacheflush_args *uap, register_t *retval)
500 {
501 /* {
502 syscallarg(void *) addr;
503 syscallarg(unsigned) nbytes;
504 syscallarg(int) flag;
505 } */
506 struct proc *p = l->l_proc;
507 vaddr_t va = (vaddr_t)SCARG(uap, addr);
508 int nbytes = SCARG(uap, nbytes);
509 int whichcache = SCARG(uap, whichcache);
510
511 return mips_user_cacheflush(p, va, nbytes, whichcache);
512 }
513
514
515 int
ultrix_sys_cachectl(struct lwp * l,const struct ultrix_sys_cachectl_args * uap,register_t * retval)516 ultrix_sys_cachectl(struct lwp *l, const struct ultrix_sys_cachectl_args *uap, register_t *retval)
517 {
518 /* {
519 syscallarg(void *) addr;
520 syscallarg(int) nbytes;
521 syscallarg(int) cacheop;
522 } */
523 struct proc *p = l->l_proc;
524 vaddr_t va = (vaddr_t)SCARG(uap, addr);
525 int nbytes = SCARG(uap, nbytes);
526 int cacheop = SCARG(uap, cacheop);
527
528 return mips_user_cachectl(p, va, nbytes, cacheop);
529 }
530
531 #endif /* __mips */
532
533
534 int
ultrix_sys_exportfs(struct lwp * l,const struct ultrix_sys_exportfs_args * uap,register_t * retval)535 ultrix_sys_exportfs(struct lwp *l, const struct ultrix_sys_exportfs_args *uap, register_t *retval)
536 {
537
538 /*
539 * XXX: should perhaps translate into a mount(2)
540 * with MOUNT_EXPORT?
541 */
542 return 0;
543 }
544
545 int
ultrix_sys_sigpending(struct lwp * l,const struct ultrix_sys_sigpending_args * uap,register_t * retval)546 ultrix_sys_sigpending(struct lwp *l, const struct ultrix_sys_sigpending_args *uap, register_t *retval)
547 {
548 sigset_t ss;
549 int mask;
550
551 sigpending1(l, &ss);
552 mask = ss.__bits[0];
553
554 return copyout(&mask, SCARG(uap, mask), sizeof(int));
555 }
556
557 int
ultrix_sys_sigreturn(struct lwp * l,const struct ultrix_sys_sigreturn_args * uap,register_t * retval)558 ultrix_sys_sigreturn(struct lwp *l, const struct ultrix_sys_sigreturn_args *uap, register_t *retval)
559 {
560 /* struct sigcontext13 is close enough to Ultrix */
561 struct compat_13_sys_sigreturn_args ap;
562
563 SCARG(&ap, sigcntxp) = (void *)SCARG(uap, sigcntxp);
564
565 return compat_13_sys_sigreturn(l, &ap, retval);
566 }
567
568 int
ultrix_sys_sigcleanup(struct lwp * l,const struct ultrix_sys_sigcleanup_args * uap,register_t * retval)569 ultrix_sys_sigcleanup(struct lwp *l, const struct ultrix_sys_sigcleanup_args *uap, register_t *retval)
570 {
571
572 /* struct sigcontext13 is close enough to Ultrix */
573 struct compat_13_sys_sigreturn_args ap;
574
575 SCARG(&ap, sigcntxp) = (void *)SCARG(uap, sigcntxp);
576
577 return compat_13_sys_sigreturn(l, &ap, retval);
578 }
579
580 int
ultrix_sys_sigsuspend(struct lwp * l,const struct ultrix_sys_sigsuspend_args * uap,register_t * retval)581 ultrix_sys_sigsuspend(struct lwp *l, const struct ultrix_sys_sigsuspend_args *uap, register_t *retval)
582 {
583 int mask = SCARG(uap, mask);
584 sigset_t ss;
585
586 ss.__bits[0] = mask;
587 ss.__bits[1] = 0;
588 ss.__bits[2] = 0;
589 ss.__bits[3] = 0;
590
591 return sigsuspend1(l, &ss);
592 }
593
594 #define ULTRIX_SV_ONSTACK 0x0001 /* take signal on signal stack */
595 #define ULTRIX_SV_INTERRUPT 0x0002 /* do not restart system on signal return */
596 #define ULTRIX_SV_OLDSIG 0x1000 /* Emulate old signal() for POSIX */
597
598 int
ultrix_sys_sigvec(struct lwp * l,const struct ultrix_sys_sigvec_args * uap,register_t * retval)599 ultrix_sys_sigvec(struct lwp *l, const struct ultrix_sys_sigvec_args *uap, register_t *retval)
600 {
601 struct sigvec nsv, osv;
602 struct sigaction nsa, osa;
603 int error;
604
605 if (SCARG(uap, nsv)) {
606 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
607 if (error)
608 return error;
609 nsa.sa_handler = nsv.sv_handler;
610 #if 0 /* documentation */
611 /* ONSTACK is identical */
612 nsa.sa_flags = nsv.sv_flags & ULTRIX_SV_ONSTACK;
613 if ((nsv.sv_flags & ULTRIX_SV_OLDSIG)
614 /* old signal() - always restart */
615 || (!(nsv.sv_flags & ULTRIX_SV_INTERRUPT))
616 /* inverted meaning (same bit) */
617 )
618 nsa.sa_flags |= SA_RESTART;
619 #else /* optimized - assuming ULTRIX_SV_OLDSIG=>!ULTRIX_SV_INTERRUPT */
620 nsa.sa_flags = nsv.sv_flags & ~ULTRIX_SV_OLDSIG;
621 nsa.sa_flags ^= SA_RESTART;
622 #endif
623 native_sigset13_to_sigset(&nsv.sv_mask, &nsa.sa_mask);
624 }
625 error = sigaction1(l, SCARG(uap, signum),
626 SCARG(uap, nsv) ? &nsa : 0, SCARG(uap, osv) ? &osa : 0,
627 NULL, 0);
628 if (error)
629 return error;
630 if (SCARG(uap, osv)) {
631 memset(&osv, 0, sizeof(osv));
632 osv.sv_handler = osa.sa_handler;
633 osv.sv_flags = osa.sa_flags ^ SA_RESTART;
634 osv.sv_flags &= (ULTRIX_SV_ONSTACK | ULTRIX_SV_INTERRUPT);
635 native_sigset_to_sigset13(&osa.sa_mask, &osv.sv_mask);
636 error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
637 if (error)
638 return error;
639 }
640 return 0;
641 }
642
643 int
ultrix_sys_shmsys(struct lwp * l,const struct ultrix_sys_shmsys_args * uap,register_t * retval)644 ultrix_sys_shmsys(struct lwp *l, const struct ultrix_sys_shmsys_args *uap, register_t *retval)
645 {
646
647 #ifdef SYSVSHM
648 /* Ultrix SVSHM weirndess: */
649 struct sys_shmat_args shmat_args;
650 struct compat_14_sys_shmctl_args shmctl_args;
651 struct sys_shmdt_args shmdt_args;
652 struct sys_shmget_args shmget_args;
653
654
655 switch (SCARG(uap, shmop)) {
656 case 0: /* Ultrix shmat() */
657 SCARG(&shmat_args, shmid) = SCARG(uap, a2);
658 SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a3);
659 SCARG(&shmat_args, shmflg) = SCARG(uap, a4);
660 return sys_shmat(l, &shmat_args, retval);
661
662 case 1: /* Ultrix shmctl() */
663 SCARG(&shmctl_args, shmid) = SCARG(uap, a2);
664 SCARG(&shmctl_args, cmd) = SCARG(uap, a3);
665 SCARG(&shmctl_args, buf) = (struct shmid_ds14 *)SCARG(uap, a4);
666 return compat_14_sys_shmctl(l, &shmctl_args, retval);
667
668 case 2: /* Ultrix shmdt() */
669 SCARG(&shmat_args, shmaddr) = (void *)SCARG(uap, a2);
670 return sys_shmdt(l, &shmdt_args, retval);
671
672 case 3: /* Ultrix shmget() */
673 SCARG(&shmget_args, key) = SCARG(uap, a2);
674 SCARG(&shmget_args, size) = SCARG(uap, a3);
675 SCARG(&shmget_args, shmflg) = SCARG(uap, a4)
676 & (IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
677 return sys_shmget(l, &shmget_args, retval);
678
679 default:
680 return EINVAL;
681 }
682 #else
683 return EOPNOTSUPP;
684 #endif /* SYSVSHM */
685 }
686
687 static int
ultrix_to_bsd_flock(struct ultrix_flock * ufl,struct flock * fl)688 ultrix_to_bsd_flock(struct ultrix_flock *ufl, struct flock *fl)
689 {
690
691 memset(fl, 0, sizeof(*fl));
692
693 fl->l_start = ufl->l_start;
694 fl->l_len = ufl->l_len;
695 fl->l_pid = ufl->l_pid;
696 fl->l_whence = ufl->l_whence;
697
698 switch (ufl->l_type) {
699 case ULTRIX_F_RDLCK:
700 fl->l_type = F_RDLCK;
701 break;
702 case ULTRIX_F_WRLCK:
703 fl->l_type = F_WRLCK;
704 break;
705 case ULTRIX_F_UNLCK:
706 fl->l_type = F_UNLCK;
707 break;
708 default:
709 return EINVAL;
710 }
711
712 return 0;
713 }
714
715 static void
bsd_to_ultrix_flock(struct flock * fl,struct ultrix_flock * ufl)716 bsd_to_ultrix_flock(struct flock *fl, struct ultrix_flock *ufl)
717 {
718
719 memset(ufl, 0, sizeof(*ufl));
720
721 ufl->l_start = fl->l_start;
722 ufl->l_len = fl->l_len;
723 ufl->l_pid = fl->l_pid;
724 ufl->l_whence = fl->l_whence;
725
726 switch (fl->l_type) {
727 case F_RDLCK:
728 ufl->l_type = ULTRIX_F_RDLCK;
729 break;
730 case F_WRLCK:
731 ufl->l_type = ULTRIX_F_WRLCK;
732 break;
733 case F_UNLCK:
734 ufl->l_type = ULTRIX_F_UNLCK;
735 break;
736 }
737 }
738
739 int
ultrix_sys_fcntl(struct lwp * l,const struct ultrix_sys_fcntl_args * uap,register_t * retval)740 ultrix_sys_fcntl(struct lwp *l, const struct ultrix_sys_fcntl_args *uap, register_t *retval)
741 {
742 int error;
743 struct ultrix_flock ufl;
744 struct flock fl;
745
746 switch (SCARG(uap, cmd)) {
747 case F_GETLK:
748 case F_SETLK:
749 case F_SETLKW:
750 error = copyin(SCARG(uap, arg), &ufl, sizeof(ufl));
751 if (error)
752 return error;
753 error = ultrix_to_bsd_flock(&ufl, &fl);
754 if (error)
755 return error;
756 error = do_fcntl_lock(SCARG(uap, fd), SCARG(uap, cmd), &fl);
757 if (SCARG(uap, cmd) != F_GETLK || error != 0)
758 return error;
759 bsd_to_ultrix_flock(&fl, &ufl);
760 return copyout(&ufl, SCARG(uap, arg), sizeof(ufl));
761
762 default:
763 break;
764 }
765
766 return sys_fcntl(l, (const void *)uap, retval);
767 }
768