sunos32_misc.c revision 1.57 1 /* $NetBSD: sunos32_misc.c,v 1.57 2007/12/28 10:00:18 hannken Exp $ */
2 /* from :NetBSD: sunos_misc.c,v 1.107 2000/12/01 19:25:10 jdolecek Exp */
3
4 /*
5 * Copyright (c) 2001 Matthew R. Green
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1992, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This software was developed by the Computer Systems Engineering group
37 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
38 * contributed to Berkeley.
39 *
40 * All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Lawrence Berkeley Laboratory.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 *
69 * @(#)sunos_misc.c 8.1 (Berkeley) 6/18/93
70 *
71 * Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
72 */
73
74 /*
75 * SunOS compatibility module, 64-bit kernel version
76 *
77 * SunOS system calls that are implemented differently in BSD are
78 * handled here.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: sunos32_misc.c,v 1.57 2007/12/28 10:00:18 hannken Exp $");
83
84 #define COMPAT_SUNOS 1
85
86 #if defined(_KERNEL_OPT)
87 #include "opt_nfsserver.h"
88 #include "opt_compat_43.h"
89 #include "opt_compat_netbsd.h"
90 #include "opt_ptrace.h"
91 #include "fs_nfs.h"
92 #endif
93
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/namei.h>
97 #include <sys/proc.h>
98 #include <sys/dirent.h>
99 #include <sys/file.h>
100 #include <sys/stat.h>
101 #include <sys/filedesc.h>
102 #include <sys/ioctl.h>
103 #include <sys/kernel.h>
104 #include <sys/reboot.h>
105 #include <sys/malloc.h>
106 #include <sys/mbuf.h>
107 #include <sys/mman.h>
108 #include <sys/mount.h>
109 #include <sys/ptrace.h>
110 #include <sys/resource.h>
111 #include <sys/resourcevar.h>
112 #include <sys/signal.h>
113 #include <sys/signalvar.h>
114 #include <sys/socket.h>
115 #include <sys/tty.h>
116 #include <sys/vnode.h>
117 #include <sys/uio.h>
118 #include <sys/wait.h>
119 #include <sys/utsname.h>
120 #include <sys/unistd.h>
121 #include <sys/syscall.h>
122 #include <sys/syscallargs.h>
123 #include <sys/conf.h>
124 #include <sys/socketvar.h>
125 #include <sys/exec.h>
126 #include <sys/swap.h>
127 #include <sys/kauth.h>
128 #include <sys/vfs_syscalls.h>
129
130 #include <compat/netbsd32/netbsd32.h>
131 #include <compat/netbsd32/netbsd32_syscallargs.h>
132 #include <compat/sunos32/sunos32.h>
133 #include <compat/sunos32/sunos32_dirent.h>
134 #include <compat/sunos32/sunos32_syscallargs.h>
135 #include <compat/common/compat_util.h>
136 #include <compat/sys/mount.h>
137
138 #include <netinet/in.h>
139
140 #include <miscfs/specfs/specdev.h>
141
142 #include <nfs/rpcv2.h>
143 #include <nfs/nfsproto.h>
144 #include <nfs/nfs.h>
145 #include <nfs/nfsmount.h>
146
147 static void sunos32_sigvec_to_sigaction(const struct netbsd32_sigvec *, struct sigaction *);
148 static void sunos32_sigvec_from_sigaction(struct netbsd32_sigvec *, const struct sigaction *);
149
150 static int sunstatfs(struct statvfs *, void *);
151
152 static void
153 sunos32_sigvec_to_sigaction(const struct netbsd32_sigvec *sv, struct sigaction *sa)
154 {
155 /*XXX*/ extern void compat_43_sigmask_to_sigset(const int *, sigset_t *);
156
157 sa->sa_handler = NETBSD32PTR64(sv->sv_handler);
158 compat_43_sigmask_to_sigset(&sv->sv_mask, &sa->sa_mask);
159 sa->sa_flags = sv->sv_flags ^ SA_RESTART;
160 }
161
162 static
163 void sunos32_sigvec_from_sigaction(sv, sa)
164 struct netbsd32_sigvec *sv;
165 const struct sigaction *sa;
166 {
167 /*XXX*/ extern void compat_43_sigset_to_sigmask(const sigset_t *, int *);
168
169 NETBSD32PTR32(sv->sv_handler, sa->sa_handler);
170 compat_43_sigset_to_sigmask(&sa->sa_mask, &sv->sv_mask);
171 sv->sv_flags = sa->sa_flags ^ SA_RESTART;
172 }
173
174 int
175 sunos32_sys_stime(struct lwp *l, const struct sunos32_sys_stime_args *uap, register_t *retval)
176 {
177 /* {
178 syscallarg(sunos32_time_tp) tp;
179 } */
180 struct netbsd32_timeval ntv;
181 struct timeval tv;
182 int error;
183
184 error = copyin(SCARG_P32(uap, tp), &ntv.tv_sec, sizeof(ntv.tv_sec));
185 if (error)
186 return error;
187 tv.tv_sec = ntv.tv_sec;
188 tv.tv_usec = 0;
189
190 return settimeofday1(&tv, false, NULL, l, true);
191 }
192
193 int
194 sunos32_sys_wait4(struct lwp *l, const struct sunos32_sys_wait4_args *uap, register_t *retval)
195 {
196 /* {
197 syscallarg(int) pid;
198 syscallarg(netbsd32_intp) status;
199 syscallarg(int) options;
200 syscallarg(netbsd32_rusagep_t) rusage;
201 } */
202
203 struct netbsd32_wait4_args bsd_ua;
204
205 SCARG(&bsd_ua, pid) = SCARG(uap, pid) == 0 ? WAIT_ANY : SCARG(uap, pid);
206 SCARG(&bsd_ua, status) = SCARG(uap, status);
207 SCARG(&bsd_ua, options) = SCARG(uap, options);
208 SCARG(&bsd_ua, rusage) = SCARG(uap, rusage);
209
210 return netbsd32_wait4(l, &bsd_ua, retval);
211 }
212
213 int
214 sunos32_sys_creat(struct lwp *l, const struct sunos32_sys_creat_args *uap, register_t *retval)
215 {
216 /* {
217 syscallarg(const netbsd32_charp) path;
218 syscallarg(int) mode;
219 } */
220 struct sys_open_args ua;
221
222 SUNOS32TOP_UAP(path, const char);
223 SCARG(&ua, flags) = O_WRONLY | O_CREAT | O_TRUNC;
224 SUNOS32TO64_UAP(mode);
225
226 return (sys_open(l, &ua, retval));
227 }
228
229 int
230 sunos32_sys_access(struct lwp *l, const struct sunos32_sys_access_args *uap, register_t *retval)
231 {
232 /* {
233 syscallarg(const netbsd32_charp) path;
234 syscallarg(int) flags;
235 } */
236 struct sys_access_args ua;
237
238 SUNOS32TOP_UAP(path, const char);
239 SUNOS32TO64_UAP(flags);
240
241 return (sys_access(l, &ua, retval));
242 }
243
244 static inline void sunos32_from___stat13(struct stat *, struct netbsd32_stat43 *);
245
246 static inline void
247 sunos32_from___stat13(struct stat *sbp, struct netbsd32_stat43 *sb32p)
248 {
249 sb32p->st_dev = sbp->st_dev;
250 sb32p->st_ino = sbp->st_ino;
251 sb32p->st_mode = sbp->st_mode;
252 sb32p->st_nlink = sbp->st_nlink;
253 sb32p->st_uid = sbp->st_uid;
254 sb32p->st_gid = sbp->st_gid;
255 sb32p->st_rdev = sbp->st_rdev;
256 if (sbp->st_size < (quad_t)1 << 32)
257 sb32p->st_size = sbp->st_size;
258 else
259 sb32p->st_size = -2;
260 sb32p->st_atimespec.tv_sec = (netbsd32_time_t)sbp->st_atimespec.tv_sec;
261 sb32p->st_atimespec.tv_nsec = (netbsd32_long)sbp->st_atimespec.tv_nsec;
262 sb32p->st_mtimespec.tv_sec = (netbsd32_time_t)sbp->st_mtimespec.tv_sec;
263 sb32p->st_mtimespec.tv_nsec = (netbsd32_long)sbp->st_mtimespec.tv_nsec;
264 sb32p->st_ctimespec.tv_sec = (netbsd32_time_t)sbp->st_ctimespec.tv_sec;
265 sb32p->st_ctimespec.tv_nsec = (netbsd32_long)sbp->st_ctimespec.tv_nsec;
266 sb32p->st_blksize = sbp->st_blksize;
267 sb32p->st_blocks = sbp->st_blocks;
268 sb32p->st_flags = sbp->st_flags;
269 sb32p->st_gen = sbp->st_gen;
270 }
271
272
273 int
274 sunos32_sys_stat(struct lwp *l, const struct sunos32_sys_stat_args *uap, register_t *retval)
275 {
276 /* {
277 syscallarg(const netbsd32_charp) path;
278 syscallarg(netbsd32_stat43p_t) ub;
279 } */
280 struct netbsd32_stat43 sb32;
281 struct stat sb;
282 const char *path;
283 int error;
284
285 path = SCARG_P32(uap, path);
286
287 error = do_sys_stat(l, path, FOLLOW, &sb);
288 if (error)
289 return (error);
290 sunos32_from___stat13(&sb, &sb32);
291 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof (sb32));
292 return (error);
293 }
294
295 int
296 sunos32_sys_lstat(struct lwp *l, const struct sunos32_sys_lstat_args *uap, register_t *retval)
297 {
298 /* {
299 syscallarg(const netbsd32_charp) path;
300 syscallarg(netbsd32_stat43p_t) ub;
301 } */
302 struct vnode *vp, *dvp;
303 struct stat sb, sb1;
304 struct netbsd32_stat43 sb32;
305 int error;
306 struct nameidata nd;
307 int ndflags;
308 const char *path;
309
310 path = SCARG_P32(uap, path);
311
312 ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
313 again:
314 NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, path);
315 if ((error = namei(&nd))) {
316 if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
317 /*
318 * Should only happen on '/'. Retry without LOCKPARENT;
319 * this is safe since the vnode won't be a VLNK.
320 */
321 ndflags &= ~LOCKPARENT;
322 goto again;
323 }
324 return (error);
325 }
326 /*
327 * For symbolic links, always return the attributes of its
328 * containing directory, except for mode, size, and links.
329 */
330 vp = nd.ni_vp;
331 dvp = nd.ni_dvp;
332 if (vp->v_type != VLNK) {
333 if ((ndflags & LOCKPARENT) != 0) {
334 if (dvp == vp)
335 vrele(dvp);
336 else
337 vput(dvp);
338 }
339 error = vn_stat(vp, &sb, l);
340 vput(vp);
341 if (error)
342 return (error);
343 } else {
344 error = vn_stat(dvp, &sb, l);
345 vput(dvp);
346 if (error) {
347 vput(vp);
348 return (error);
349 }
350 error = vn_stat(vp, &sb1, l);
351 vput(vp);
352 if (error)
353 return (error);
354 sb.st_mode &= ~S_IFDIR;
355 sb.st_mode |= S_IFLNK;
356 sb.st_nlink = sb1.st_nlink;
357 sb.st_size = sb1.st_size;
358 sb.st_blocks = sb1.st_blocks;
359 }
360 sunos32_from___stat13(&sb, &sb32);
361 error = copyout((void *)&sb32, SCARG_P32(uap, ub), sizeof (sb32));
362 return (error);
363 }
364
365 static int
366 sunos32_execve_fetch_element(char * const *array, size_t index, char **value)
367 {
368 int error;
369 netbsd32_charp const *a32 = (void const *)array;
370 netbsd32_charp e;
371
372 error = copyin(a32 + index, &e, sizeof(e));
373 if (error)
374 return error;
375 *value = NETBSD32PTR64(e);
376 return 0;
377 }
378
379 int
380 sunos32_sys_execv(struct lwp *l, const struct sunos32_sys_execv_args *uap, register_t *retval)
381 {
382 /* {
383 syscallarg(const char *) path;
384 syscallarg(char **) argv;
385 } */
386 const char *path = SCARG_P32(uap, path);
387
388 return execve1(l, path, SCARG_P32(uap, argp), NULL,
389 sunos32_execve_fetch_element);
390 }
391
392 int
393 sunos32_sys_execve(struct lwp *l, const struct sunos32_sys_execve_args *uap, register_t *retval)
394 {
395 /* {
396 syscallarg(const char *) path;
397 syscallarg(char **) argv;
398 syscallarg(char **) envp;
399 } */
400 const char *path = SCARG_P32(uap, path);
401
402 return execve1(l, path, SCARG_P32(uap, argp),
403 SCARG_P32(uap, envp),
404 sunos32_execve_fetch_element);
405 }
406
407 int
408 sunos32_sys_omsync(struct lwp *l, const struct sunos32_sys_omsync_args *uap, register_t *retval)
409 {
410 /* {
411 syscallarg(netbsd32_caddr_t) addr;
412 syscallarg(netbsd32_size_t) len;
413 syscallarg(int) flags;
414 } */
415 struct netbsd32___msync13_args ouap;
416
417 SCARG(&ouap, addr) = SCARG(uap, addr);
418 SCARG(&ouap, len) = SCARG(uap, len);
419 SCARG(&ouap, flags) = SCARG(uap, flags);
420
421 return (netbsd32___msync13(l, &ouap, retval));
422 }
423
424 int
425 sunos32_sys_unmount(struct lwp *l, const struct sunos32_sys_unmount_args *uap, register_t *retval)
426 {
427 /* {
428 syscallarg(netbsd32_charp) path;
429 } */
430 struct sys_unmount_args ua;
431
432 SUNOS32TOP_UAP(path, const char);
433 SCARG(&ua, flags) = 0;
434
435 return (sys_unmount(l, &ua, retval));
436 }
437
438 /*
439 * Conversion table for SunOS NFS mount flags.
440 */
441 static struct {
442 int sun_flg;
443 int bsd_flg;
444 } sunnfs_flgtab[] = {
445 { SUNNFS_SOFT, NFSMNT_SOFT },
446 { SUNNFS_WSIZE, NFSMNT_WSIZE },
447 { SUNNFS_RSIZE, NFSMNT_RSIZE },
448 { SUNNFS_TIMEO, NFSMNT_TIMEO },
449 { SUNNFS_RETRANS, NFSMNT_RETRANS },
450 { SUNNFS_HOSTNAME, 0 }, /* Ignored */
451 { SUNNFS_INT, NFSMNT_INT },
452 { SUNNFS_NOAC, 0 }, /* Ignored */
453 { SUNNFS_ACREGMIN, 0 }, /* Ignored */
454 { SUNNFS_ACREGMAX, 0 }, /* Ignored */
455 { SUNNFS_ACDIRMIN, 0 }, /* Ignored */
456 { SUNNFS_ACDIRMAX, 0 }, /* Ignored */
457 { SUNNFS_SECURE, 0 }, /* Ignored */
458 { SUNNFS_NOCTO, 0 }, /* Ignored */
459 { SUNNFS_POSIX, 0 } /* Ignored */
460 };
461
462 int
463 sunos32_sys_mount(struct lwp *l, const struct sunos32_sys_mount_args *uap, register_t *retval)
464 {
465 /* {
466 syscallarg(netbsd32_charp) type;
467 syscallarg(netbsd32_charp) path;
468 syscallarg(int) flags;
469 syscallarg(netbsd32_caddr_t) data;
470 } */
471 int oflags = SCARG(uap, flags), nflags, error;
472 char fsname[MFSNAMELEN];
473 register_t dummy;
474
475 if (oflags & (SUNM_NOSUB | SUNM_SYS5))
476 return (EINVAL);
477 if ((oflags & SUNM_NEWTYPE) == 0)
478 return (EINVAL);
479 nflags = 0;
480 if (oflags & SUNM_RDONLY)
481 nflags |= MNT_RDONLY;
482 if (oflags & SUNM_NOSUID)
483 nflags |= MNT_NOSUID;
484 if (oflags & SUNM_REMOUNT)
485 nflags |= MNT_UPDATE;
486
487 error = copyinstr(SCARG_P32(uap, type), fsname, sizeof fsname, NULL);
488 if (error)
489 return (error);
490
491 if (strncmp(fsname, "nfs", sizeof fsname) == 0) {
492 struct sunos_nfs_args sna;
493 struct nfs_args na; /* XXX */
494 int n;
495
496 error = copyin(SCARG_P32(uap, data), &sna, sizeof sna);
497 if (error)
498 return (error);
499 /* sa.sa_len = sizeof(sain); */
500 na.version = NFS_ARGSVERSION;
501 na.addr = (void *)sna.addr;
502 na.addrlen = sizeof(struct sockaddr);
503 na.sotype = SOCK_DGRAM;
504 na.proto = IPPROTO_UDP;
505 na.fh = sna.fh;
506 na.fhsize = NFSX_V2FH;
507 na.flags = 0;
508 n = sizeof(sunnfs_flgtab) / sizeof(sunnfs_flgtab[0]);
509 while (--n >= 0)
510 if (sna.flags & sunnfs_flgtab[n].sun_flg)
511 na.flags |= sunnfs_flgtab[n].bsd_flg;
512 na.wsize = sna.wsize;
513 na.rsize = sna.rsize;
514 if (na.flags & NFSMNT_RSIZE) {
515 na.flags |= NFSMNT_READDIRSIZE;
516 na.readdirsize = na.rsize;
517 }
518 na.timeo = sna.timeo;
519 na.retrans = sna.retrans;
520 na.hostname = (char *)(u_long)sna.hostname;
521
522 return do_sys_mount(l, vfs_getopsbyname("nfs"), NULL,
523 SCARG_P32(uap, path), nflags, &na, UIO_SYSSPACE, sizeof na,
524 &dummy);
525 }
526
527 if (strcmp(fsname, "4.2") == 0)
528 strcpy(fsname, "ffs");
529
530 return do_sys_mount(l, vfs_getopsbyname(fsname), NULL,
531 SCARG_P32(uap, path), nflags, SCARG_P32(uap, data), UIO_USERSPACE,
532 0, &dummy);
533 }
534
535 #if defined(NFS)
536 int
537 async_daemon(struct lwp *l, const void *v, register_t *retval)
538 {
539 struct netbsd32_nfssvc_args ouap;
540
541 SCARG(&ouap, flag) = NFSSVC_BIOD;
542 NETBSD32PTR32(SCARG(&ouap, argp), 0);
543
544 return (netbsd32_nfssvc(l, &ouap, retval));
545 }
546 #endif /* NFS */
547
548 void native_to_sunos_sigset(const sigset_t *, int *);
549 void sunos_to_native_sigset(const int, sigset_t *);
550
551 inline void
552 native_to_sunos_sigset(const sigset_t *ss, int *mask)
553 {
554 *mask = ss->__bits[0];
555 }
556
557 inline void
558 sunos_to_native_sigset(const int mask, sigset_t *ss)
559 {
560
561 ss->__bits[0] = mask;
562 ss->__bits[1] = 0;
563 ss->__bits[2] = 0;
564 ss->__bits[3] = 0;
565 }
566
567 int
568 sunos32_sys_sigpending(struct lwp *l, const struct sunos32_sys_sigpending_args *uap, register_t *retval)
569 {
570 /* {
571 syscallarg(netbsd32_intp) mask;
572 } */
573 sigset_t ss;
574 int mask;
575
576 sigpending1(l, &ss);
577 native_to_sunos_sigset(&ss, &mask);
578
579 return (copyout((void *)(u_long)&mask, SCARG_P32(uap, mask), sizeof(int)));
580 }
581
582 int
583 sunos32_sys_sigsuspend(struct lwp *l, const struct sunos32_sys_sigsuspend_args *uap, register_t *retval)
584 {
585 /* {
586 syscallarg(int) mask;
587 } */
588 int mask;
589 sigset_t ss;
590
591 mask = SCARG(uap, mask);
592 sunos_to_native_sigset(mask, &ss);
593 return (sigsuspend1(l, &ss));
594 }
595
596 /*
597 * Read Sun-style directory entries. We suck them into kernel space so
598 * that they can be massaged before being copied out to user code. Like
599 * SunOS, we squish out `empty' entries.
600 *
601 * This is quite ugly, but what do you expect from compatibility code?
602 */
603 int
604 sunos32_sys_getdents(struct lwp *l, const struct sunos32_sys_getdents_args *uap, register_t *retval)
605 {
606 /* {
607 syscallarg(int) fd;
608 syscallarg(netbsd32_charp) buf;
609 syscallarg(int) nbytes;
610 } */
611 struct proc *p = l->l_proc;
612 struct dirent *bdp;
613 struct vnode *vp;
614 char *inp, *sbuf; /* BSD-format */
615 int len, reclen; /* BSD-format */
616 char *outp; /* Sun-format */
617 int resid, sunos_reclen;/* Sun-format */
618 struct file *fp;
619 struct uio auio;
620 struct iovec aiov;
621 struct sunos32_dirent idb;
622 off_t off; /* true file offset */
623 int buflen, error, eofflag;
624 off_t *cookiebuf, *cookie;
625 int ncookies;
626
627 /* getvnode() will use the descriptor for us */
628 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
629 return (error);
630
631 if ((fp->f_flag & FREAD) == 0) {
632 error = EBADF;
633 goto out1;
634 }
635
636 vp = (struct vnode *)fp->f_data;
637 if (vp->v_type != VDIR) {
638 error = EINVAL;
639 goto out1;
640 }
641
642 buflen = min(MAXBSIZE, SCARG(uap, nbytes));
643 sbuf = malloc(buflen, M_TEMP, M_WAITOK);
644 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
645 off = fp->f_offset;
646 again:
647 aiov.iov_base = sbuf;
648 aiov.iov_len = buflen;
649 auio.uio_iov = &aiov;
650 auio.uio_iovcnt = 1;
651 auio.uio_rw = UIO_READ;
652 auio.uio_resid = buflen;
653 auio.uio_offset = off;
654 UIO_SETUP_SYSSPACE(&auio);
655 /*
656 * First we read into the malloc'ed buffer, then
657 * we massage it into user space, one record at a time.
658 */
659 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
660 &ncookies);
661 if (error)
662 goto out;
663
664 inp = sbuf;
665 outp = SCARG_P32(uap, buf);
666 resid = SCARG(uap, nbytes);
667 if ((len = buflen - auio.uio_resid) == 0)
668 goto eof;
669
670 for (cookie = cookiebuf; len > 0; len -= reclen) {
671 bdp = (struct dirent *)inp;
672 reclen = bdp->d_reclen;
673 if (reclen & 3)
674 panic("sunos_getdents");
675 if (cookie && (*cookie >> 32) != 0) {
676 compat_offseterr(vp, "sunos_getdents");
677 error = EINVAL;
678 goto out;
679 }
680 if (bdp->d_fileno == 0) {
681 inp += reclen; /* it is a hole; squish it out */
682 if (cookie)
683 off = *cookie++;
684 else
685 off += reclen;
686 continue;
687 }
688 sunos_reclen = SUNOS32_RECLEN(&idb, bdp->d_namlen);
689 if (reclen > len || resid < sunos_reclen) {
690 /* entry too big for buffer, so just stop */
691 outp++;
692 break;
693 }
694 if (cookie)
695 off = *cookie++; /* each entry points to next */
696 else
697 off += reclen;
698 /*
699 * Massage in place to make a Sun-shaped dirent (otherwise
700 * we have to worry about touching user memory outside of
701 * the copyout() call).
702 */
703 idb.d_fileno = bdp->d_fileno;
704 idb.d_off = off;
705 idb.d_reclen = sunos_reclen;
706 idb.d_namlen = bdp->d_namlen;
707 strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
708 if ((error = copyout((void *)&idb, outp, sunos_reclen)) != 0)
709 goto out;
710 /* advance past this real entry */
711 inp += reclen;
712 /* advance output past Sun-shaped entry */
713 outp += sunos_reclen;
714 resid -= sunos_reclen;
715 }
716
717 /* if we squished out the whole block, try again */
718 if (outp == SCARG_P32(uap, buf))
719 goto again;
720 fp->f_offset = off; /* update the vnode offset */
721
722 eof:
723 *retval = SCARG(uap, nbytes) - resid;
724 out:
725 VOP_UNLOCK(vp, 0);
726 free(cookiebuf, M_TEMP);
727 free(sbuf, M_TEMP);
728 out1:
729 FILE_UNUSE(fp, l);
730 return (error);
731 }
732
733 #define SUNOS32__MAP_NEW 0x80000000 /* if not, old mmap & cannot handle */
734
735 int
736 sunos32_sys_mmap(struct lwp *l, const struct sunos32_sys_mmap_args *uap, register_t *retval)
737 {
738 /* {
739 syscallarg(netbsd32_voidp) addr;
740 syscallarg(netbsd32_size_t) len;
741 syscallarg(int) prot;
742 syscallarg(int) flags;
743 syscallarg(int) fd;
744 syscallarg(netbsd32_long) pos;
745 } */
746 struct sys_mmap_args ua;
747 int error;
748
749 /*
750 * Verify the arguments.
751 */
752 if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
753 return (EINVAL); /* XXX still needed? */
754
755 if ((SCARG(uap, flags) & SUNOS32__MAP_NEW) == 0)
756 return (EINVAL);
757
758 SUNOS32TOP_UAP(addr, void);
759 SUNOS32TOX_UAP(len, size_t);
760 SUNOS32TO64_UAP(prot);
761 SCARG(&ua, flags) = SCARG(uap, flags) & ~SUNOS32__MAP_NEW;
762 SUNOS32TO64_UAP(fd);
763 SCARG(&ua, pad) = 0;
764 SUNOS32TOX_UAP(pos, off_t);
765
766 error = sys_mmap(l, &ua, retval);
767 if ((u_long)*retval > (u_long)UINT_MAX) {
768 printf("sunos32_mmap: retval out of range: 0x%lx",
769 (u_long)*retval);
770 /* Should try to recover and return an error here. */
771 }
772 return (error);
773 }
774
775 #define MC_SYNC 1
776 #define MC_LOCK 2
777 #define MC_UNLOCK 3
778 #define MC_ADVISE 4
779 #define MC_LOCKAS 5
780 #define MC_UNLOCKAS 6
781
782 int
783 sunos32_sys_mctl(struct lwp *l, const struct sunos32_sys_mctl_args *uap, register_t *retval)
784 {
785 /* {
786 syscallarg(netbsd32_voidp) addr;
787 syscallarg(int) len;
788 syscallarg(int) func;
789 syscallarg(netbsd32_voidp) arg;
790 } */
791
792 switch (SCARG(uap, func)) {
793 case MC_ADVISE: /* ignore for now */
794 return (0);
795 case MC_SYNC: /* translate to msync */
796 return (netbsd32___msync13(l, (const void *)uap, retval));
797 default:
798 return (EINVAL);
799 }
800 }
801
802 int
803 sunos32_sys_setsockopt(struct lwp *l, const struct sunos32_sys_setsockopt_args *uap, register_t *retval)
804 {
805 /* {
806 syscallarg(int) s;
807 syscallarg(int) level;
808 syscallarg(int) name;
809 syscallarg(netbsd32_caddr_t) val;
810 syscallarg(int) valsize;
811 } */
812 struct proc *p = l->l_proc;
813 struct file *fp;
814 struct mbuf *m = NULL;
815 int name = SCARG(uap, name);
816 int error;
817
818 /* getsock() will use the descriptor for us */
819 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
820 return (error);
821 #define SO_DONTLINGER (~SO_LINGER)
822 if (name == SO_DONTLINGER) {
823 m = m_get(M_WAIT, MT_SOOPTS);
824 mtod(m, struct linger *)->l_onoff = 0;
825 m->m_len = sizeof(struct linger);
826 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
827 SO_LINGER, m);
828 goto out;
829 }
830 if (SCARG(uap, level) == IPPROTO_IP) {
831 #define SUNOS_IP_MULTICAST_IF 2
832 #define SUNOS_IP_MULTICAST_TTL 3
833 #define SUNOS_IP_MULTICAST_LOOP 4
834 #define SUNOS_IP_ADD_MEMBERSHIP 5
835 #define SUNOS_IP_DROP_MEMBERSHIP 6
836 static const int ipoptxlat[] = {
837 IP_MULTICAST_IF,
838 IP_MULTICAST_TTL,
839 IP_MULTICAST_LOOP,
840 IP_ADD_MEMBERSHIP,
841 IP_DROP_MEMBERSHIP
842 };
843 if (name >= SUNOS_IP_MULTICAST_IF &&
844 name <= SUNOS_IP_DROP_MEMBERSHIP) {
845 name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
846 }
847 }
848 if (SCARG(uap, valsize) > MLEN) {
849 error = EINVAL;
850 goto out;
851 }
852 if (SCARG_P32(uap, val)) {
853 m = m_get(M_WAIT, MT_SOOPTS);
854 error = copyin(SCARG_P32(uap, val), mtod(m, void *),
855 (u_int)SCARG(uap, valsize));
856 if (error) {
857 (void) m_free(m);
858 goto out;
859 }
860 m->m_len = SCARG(uap, valsize);
861 }
862 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
863 name, m);
864 out:
865 FILE_UNUSE(fp, l);
866 return (error);
867 }
868
869 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
870 int type);
871 static inline int
872 sunos32_sys_socket_common(struct lwp *l, register_t *retval, int type)
873 {
874 struct socket *so;
875 struct proc *p = l->l_proc;
876 struct file *fp;
877 int error, fd;
878
879 /* getsock() will use the descriptor for us */
880 fd = (int)*retval;
881 if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
882 so = (struct socket *)fp->f_data;
883 if (type == SOCK_DGRAM)
884 so->so_options |= SO_BROADCAST;
885 }
886 FILE_UNUSE(fp, l);
887 return (error);
888 }
889
890 int
891 sunos32_sys_socket(struct lwp *l, const struct sunos32_sys_socket_args *uap, register_t *retval)
892 {
893 /* {
894 syscallarg(int) domain;
895 syscallarg(int) type;
896 syscallarg(int) protocol;
897 } */
898 int error;
899
900 error = netbsd32_sys___socket30(l, (const void *)uap, retval);
901 if (error)
902 return (error);
903 return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
904 }
905
906 int
907 sunos32_sys_socketpair(struct lwp *l, const struct sunos32_sys_socketpair_args *uap, register_t *retval)
908 {
909 /* {
910 syscallarg(int) domain;
911 syscallarg(int) type;
912 syscallarg(int) protocol;
913 syscallarg(int *) rsv;
914 } */
915 int error;
916
917 error = netbsd32_socketpair(l, (const void *)uap, retval);
918 if (error)
919 return (error);
920 return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
921 }
922
923
924 /*
925 * XXX: This needs cleaning up.
926 */
927 int
928 sunos32_sys_auditsys(struct lwp *l, const struct sunos32_sys_auditsys_args *uap, register_t *retval)
929 {
930 return 0;
931 }
932
933 int
934 sunos32_sys_uname(struct lwp *l, const struct sunos32_sys_uname_args *uap, register_t *retval)
935 {
936 /* {
937 syscallarg(sunos32_utsnamep_t) name;
938 } */
939 struct sunos_utsname sut;
940
941 memset(&sut, 0, sizeof(sut));
942
943 memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
944 memcpy(sut.nodename, hostname, sizeof(sut.nodename));
945 sut.nodename[sizeof(sut.nodename)-1] = '\0';
946 memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
947 memcpy(sut.version, "1", sizeof(sut.version) - 1);
948 memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
949
950 return copyout((void *)&sut, SCARG_P32(uap, name),
951 sizeof(struct sunos_utsname));
952 }
953
954 int
955 sunos32_sys_setpgrp(struct lwp *l, const struct sunos32_sys_setpgrp_args *uap, register_t *retval)
956 {
957 /* {
958 syscallarg(int) pid;
959 syscallarg(int) pgid;
960 } */
961 struct proc *p = l->l_proc;
962
963 /*
964 * difference to our setpgid call is to include backwards
965 * compatibility to pre-setsid() binaries. Do setsid()
966 * instead of setpgid() in those cases where the process
967 * tries to create a new session the old way.
968 */
969 if (!SCARG(uap, pgid) &&
970 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
971 return sys_setsid(l, NULL, retval);
972 else
973 return netbsd32_setpgid(l, (const void *)uap, retval);
974 }
975
976 int
977 sunos32_sys_open(struct lwp *l, const struct sunos32_sys_open_args *uap, register_t *retval)
978 {
979 /* {
980 syscallarg(const netbsd32_charp) path;
981 syscallarg(int) flags;
982 syscallarg(int) mode;
983 } */
984 struct proc *p = l->l_proc;
985 struct sys_open_args ua;
986 int lf, r;
987 int noctty;
988 int ret;
989
990 /* convert mode into NetBSD mode */
991 lf = SCARG(uap, flags);
992 noctty = lf & 0x8000;
993 r = (lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
994 r |= ((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
995 r |= ((lf & 0x0080) ? O_SHLOCK : 0);
996 r |= ((lf & 0x0100) ? O_EXLOCK : 0);
997 r |= ((lf & 0x2000) ? O_FSYNC : 0);
998
999 SUNOS32TOP_UAP(path, const char);
1000 SCARG(&ua, flags) = r;
1001 SUNOS32TO64_UAP(mode);
1002
1003 ret = sys_open(l, &ua, retval);
1004
1005 /* XXXSMP unlocked */
1006 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
1007 struct filedesc *fdp = p->p_fd;
1008 struct file *fp;
1009
1010 fp = fd_getfile(fdp, *retval);
1011
1012 /* ignore any error, just give it a try */
1013 if (fp != NULL && fp->f_type == DTYPE_VNODE)
1014 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (void *)0, l);
1015 }
1016 return ret;
1017 }
1018
1019 #if defined (NFSSERVER)
1020 int
1021 sunos32_sys_nfssvc(struct lwp *l, const struct sunos32_sys_nfssvc_args *uap, register_t *retval)
1022 {
1023 #if 0
1024 struct emul *e = p->p_emul;
1025 struct sys_nfssvc_args outuap;
1026 struct sockaddr sa;
1027 int error;
1028 void *sg = stackgap_init(p, 0);
1029
1030 memset(&outuap, 0, sizeof outuap);
1031 SCARG(&outuap, fd) = SCARG(uap, fd);
1032 SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
1033 SCARG(&outuap, msklen) = sizeof(sa);
1034 SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
1035 SCARG(&outuap, mtchlen) = sizeof(sa);
1036
1037 memset(&sa, 0, sizeof sa);
1038 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
1039 return (error);
1040 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
1041 return (error);
1042
1043 return nfssvc(l, &outuap, retval);
1044 #else
1045 return (ENOSYS);
1046 #endif
1047 }
1048 #endif /* NFSSERVER */
1049
1050 int
1051 sunos32_sys_ustat(struct lwp *l, const struct sunos32_sys_ustat_args *uap, register_t *retval)
1052 {
1053 /* {
1054 syscallarg(int) dev;
1055 syscallarg(sunos32_ustatp_t) buf;
1056 } */
1057 struct sunos_ustat us;
1058 int error;
1059
1060 memset(&us, 0, sizeof us);
1061
1062 /*
1063 * XXX: should set f_tfree and f_tinode at least
1064 * How do we translate dev -> fstat? (and then to sunos_ustat)
1065 */
1066
1067 if ((error = copyout(&us, SCARG_P32(uap, buf), sizeof us)) != 0)
1068 return (error);
1069 return 0;
1070 }
1071
1072 int
1073 sunos32_sys_quotactl(struct lwp *l, const struct sunos32_sys_quotactl_args *uap, register_t *retval)
1074 {
1075
1076 return EINVAL;
1077 }
1078
1079 int
1080 sunos32_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
1081 {
1082 struct proc *p = l->l_proc;
1083 struct session *sp = p->p_session;
1084
1085 if (sp->s_ttyvp == 0)
1086 return 0;
1087
1088 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1089 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1090
1091 (void) ttywait(sp->s_ttyp);
1092 if (sp->s_ttyvp)
1093 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1094 if (sp->s_ttyvp)
1095 vrele(sp->s_ttyvp);
1096 sp->s_ttyvp = NULL;
1097
1098 return 0;
1099 }
1100
1101 static int
1102 sunstatfs(struct statvfs *sp, void *sbuf)
1103 {
1104 struct sunos_statfs ssfs;
1105
1106 memset(&ssfs, 0, sizeof ssfs);
1107 ssfs.f_type = 0;
1108 ssfs.f_bsize = sp->f_bsize;
1109 ssfs.f_blocks = sp->f_blocks;
1110 ssfs.f_bfree = sp->f_bfree;
1111 ssfs.f_bavail = sp->f_bavail;
1112 ssfs.f_files = sp->f_files;
1113 ssfs.f_ffree = sp->f_ffree;
1114 ssfs.f_fsid = sp->f_fsidx;
1115 return copyout((void *)&ssfs, sbuf, sizeof ssfs);
1116 }
1117
1118 int
1119 sunos32_sys_statfs(struct lwp *l, const struct sunos32_sys_statfs_args *uap, register_t *retval)
1120 {
1121 /* {
1122 syscallarg(const netbsd32_charp) path;
1123 syscallarg(sunos32_statfsp_t) buf;
1124 } */
1125 struct mount *mp;
1126 struct statvfs *sp;
1127 int error;
1128 struct nameidata nd;
1129 struct sys_statvfs1_args ua;
1130
1131 SUNOS32TOP_UAP(path, const char);
1132
1133 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1134 SCARG(&ua, path));
1135 if ((error = namei(&nd)) != 0)
1136 return (error);
1137 mp = nd.ni_vp->v_mount;
1138 sp = &mp->mnt_stat;
1139 vrele(nd.ni_vp);
1140 if ((error = VFS_STATVFS(mp, sp)) != 0)
1141 return (error);
1142 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1143 return sunstatfs(sp, SCARG_P32(uap, buf));
1144 }
1145
1146 int
1147 sunos32_sys_fstatfs(struct lwp *l, const struct sunos32_sys_fstatfs_args *uap, register_t *retval)
1148 {
1149 /* {
1150 syscallarg(int) fd;
1151 syscallarg(sunos32_statfsp_t) buf;
1152 } */
1153 struct proc *p = l->l_proc;
1154 struct file *fp;
1155 struct mount *mp;
1156 struct statvfs *sp;
1157 int error;
1158
1159 /* getvnode() will use the descriptor for us */
1160 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1161 return (error);
1162 mp = ((struct vnode *)fp->f_data)->v_mount;
1163 sp = &mp->mnt_stat;
1164 if ((error = VFS_STATVFS(mp, sp)) != 0)
1165 goto out;
1166 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1167 error = sunstatfs(sp, SCARG_P32(uap, buf));
1168 out:
1169 FILE_UNUSE(fp, l);
1170 return (error);
1171 }
1172
1173 int
1174 sunos32_sys_exportfs(struct lwp *l, const struct sunos32_sys_exportfs_args *uap, register_t *retval)
1175 {
1176 /*
1177 * XXX: should perhaps translate into a mount(2)
1178 * with MOUNT_EXPORT?
1179 */
1180 return 0;
1181 }
1182
1183 int
1184 sunos32_sys_mknod(struct lwp *l, const struct sunos32_sys_mknod_args *uap, register_t *retval)
1185 {
1186 /* {
1187 syscallarg(const netbsd32_charp) path;
1188 syscallarg(int) mode;
1189 syscallarg(int) dev;
1190 } */
1191
1192 if (S_ISFIFO(SCARG(uap, mode)))
1193 return netbsd32_mkfifo(l, (const struct netbsd32_mkfifo_args *)uap, retval);
1194
1195 return netbsd32_mknod(l, (const struct netbsd32_mknod_args *)uap, retval);
1196 }
1197
1198 #define SUNOS_SC_ARG_MAX 1
1199 #define SUNOS_SC_CHILD_MAX 2
1200 #define SUNOS_SC_CLK_TCK 3
1201 #define SUNOS_SC_NGROUPS_MAX 4
1202 #define SUNOS_SC_OPEN_MAX 5
1203 #define SUNOS_SC_JOB_CONTROL 6
1204 #define SUNOS_SC_SAVED_IDS 7
1205 #define SUNOS_SC_VERSION 8
1206
1207 int
1208 sunos32_sys_sysconf(struct lwp *l, const struct sunos32_sys_sysconf_args *uap, register_t *retval)
1209 {
1210 /* {
1211 syscallarg(int) name;
1212 } */
1213 extern u_int maxfiles;
1214
1215 switch(SCARG(uap, name)) {
1216 case SUNOS_SC_ARG_MAX:
1217 *retval = ARG_MAX;
1218 break;
1219 case SUNOS_SC_CHILD_MAX:
1220 *retval = maxproc;
1221 break;
1222 case SUNOS_SC_CLK_TCK:
1223 *retval = 60; /* should this be `hz', ie. 100? */
1224 break;
1225 case SUNOS_SC_NGROUPS_MAX:
1226 *retval = NGROUPS_MAX;
1227 break;
1228 case SUNOS_SC_OPEN_MAX:
1229 *retval = maxfiles;
1230 break;
1231 case SUNOS_SC_JOB_CONTROL:
1232 *retval = 1;
1233 break;
1234 case SUNOS_SC_SAVED_IDS:
1235 #ifdef _POSIX_SAVED_IDS
1236 *retval = 1;
1237 #else
1238 *retval = 0;
1239 #endif
1240 break;
1241 case SUNOS_SC_VERSION:
1242 *retval = 198808;
1243 break;
1244 default:
1245 return EINVAL;
1246 }
1247 return 0;
1248 }
1249
1250 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */
1251 #define SUNOS_RLIM_NLIMITS 7
1252
1253 int
1254 sunos32_sys_getrlimit(struct lwp *l, const struct sunos32_sys_getrlimit_args *uap, register_t *retval)
1255 {
1256 /* {
1257 syscallarg(u_int) which;
1258 syscallarg(netbsd32_orlimitp_t) rlp;
1259 } */
1260 struct compat_43_netbsd32_ogetrlimit_args ua_43;
1261
1262 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1263 return EINVAL;
1264
1265 SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1266 SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1267
1268 return compat_43_netbsd32_ogetrlimit(l, &ua_43, retval);
1269 }
1270
1271 int
1272 sunos32_sys_setrlimit(struct lwp *l, const struct sunos32_sys_setrlimit_args *uap, register_t *retval)
1273 {
1274 /* {
1275 syscallarg(u_int) which;
1276 syscallarg(netbsd32_orlimitp_t) rlp;
1277 } */
1278 struct compat_43_netbsd32_osetrlimit_args ua_43;
1279
1280 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1281 return EINVAL;
1282
1283 SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1284 SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1285
1286 return compat_43_netbsd32_osetrlimit(l, &ua_43, retval);
1287 }
1288
1289 #if defined(PTRACE) || defined(_LKM)
1290 /* for the m68k machines */
1291 #ifndef PT_GETFPREGS
1292 #define PT_GETFPREGS -1
1293 #endif
1294 #ifndef PT_SETFPREGS
1295 #define PT_SETFPREGS -1
1296 #endif
1297
1298 static const int sreq2breq[] = {
1299 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1,
1300 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE,
1301 PT_KILL, -1, PT_ATTACH, PT_DETACH,
1302 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS
1303 };
1304 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1305 #endif
1306
1307 int
1308 sunos32_sys_ptrace(struct lwp *l, const struct sunos32_sys_ptrace_args *uap, register_t *retval)
1309 {
1310 #if defined(PTRACE) || defined(_LKM)
1311 /* {
1312 syscallarg(int) req;
1313 syscallarg(pid_t) pid;
1314 syscallarg(netbsd32_caddr_t) addr;
1315 syscallarg(int) data;
1316 syscallarg(netbsd32_charp) addr2;
1317 } */
1318 struct netbsd32_ptrace_args pa;
1319 int req;
1320
1321 #ifdef _LKM
1322 #define sys_ptrace sysent[SYS_ptrace].sy_call
1323 if (sys_ptrace == sys_nosys)
1324 return ENOSYS;
1325 #endif
1326
1327 req = SCARG(uap, req);
1328 if ((unsigned int)req >= nreqs)
1329 return (EINVAL);
1330
1331 req = sreq2breq[req];
1332 if (req == -1)
1333 return (EINVAL);
1334
1335 SCARG(&pa, req) = req;
1336 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1337 SCARG(&pa, addr) = SCARG(uap, addr);
1338 SCARG(&pa, data) = SCARG(uap, data);
1339
1340 return netbsd32_ptrace(l, &pa, retval);
1341 #else
1342 return (ENOSYS);
1343 #endif /* PTRACE || _LKM */
1344 }
1345
1346 /*
1347 * SunOS reboot system call (for compatibility).
1348 * Sun lets you pass in a boot string which the PROM
1349 * saves and provides to the next boot program.
1350 */
1351
1352 #define SUNOS_RB_ASKNAME 0x001
1353 #define SUNOS_RB_SINGLE 0x002
1354 #define SUNOS_RB_NOSYNC 0x004
1355 #define SUNOS_RB_HALT 0x008
1356 #define SUNOS_RB_DUMP 0x080
1357 #define SUNOS_RB_STRING 0x200
1358
1359 static struct sunos_howto_conv {
1360 int sun_howto;
1361 int bsd_howto;
1362 } sunos_howto_conv[] = {
1363 { SUNOS_RB_ASKNAME, RB_ASKNAME },
1364 { SUNOS_RB_SINGLE, RB_SINGLE },
1365 { SUNOS_RB_NOSYNC, RB_NOSYNC },
1366 { SUNOS_RB_HALT, RB_HALT },
1367 { SUNOS_RB_DUMP, RB_DUMP },
1368 { SUNOS_RB_STRING, RB_STRING },
1369 { 0x000, 0 },
1370 };
1371
1372 int
1373 sunos32_sys_reboot(struct lwp *l, const struct sunos32_sys_reboot_args *uap, register_t *retval)
1374 {
1375 /* {
1376 syscallarg(int) howto;
1377 syscallarg(netbsd32_charp) bootstr;
1378 } */
1379 struct sys_reboot_args ua;
1380 struct sunos_howto_conv *convp;
1381 int error, bsd_howto, sun_howto;
1382 char *bootstr;
1383
1384 if ((error = kauth_authorize_system(l->l_cred,
1385 KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1386 return (error);
1387
1388 /*
1389 * Convert howto bits to BSD format.
1390 */
1391 sun_howto = SCARG(uap, howto);
1392 bsd_howto = 0;
1393 convp = sunos_howto_conv;
1394 while (convp->sun_howto) {
1395 if (sun_howto & convp->sun_howto)
1396 bsd_howto |= convp->bsd_howto;
1397 convp++;
1398 }
1399
1400 /*
1401 * Sun RB_STRING (Get user supplied bootstring.)
1402 * If the machine supports passing a string to the
1403 * next booted kernel.
1404 */
1405 if (sun_howto & SUNOS_RB_STRING)
1406 bootstr = SCARG_P32(uap, bootstr);
1407 else
1408 bootstr = NULL;
1409
1410 SCARG(&ua, opt) = bsd_howto;
1411 SCARG(&ua, bootstr) = bootstr;
1412 return (sys_reboot(l, &ua, retval));
1413 }
1414
1415 /*
1416 * Generalized interface signal handler, 4.3-compatible.
1417 */
1418 /* ARGSUSED */
1419 int
1420 sunos32_sys_sigvec(struct lwp *l, const struct sunos32_sys_sigvec_args *uap, register_t *retval)
1421 {
1422 /* {
1423 syscallarg(int) signum;
1424 syscallarg(struct sigvec *) nsv;
1425 syscallarg(struct sigvec *) osv;
1426 } */
1427 struct netbsd32_sigvec sv;
1428 struct sigaction nsa, osa;
1429 int error;
1430
1431 if (SCARG_P32(uap, nsv)) {
1432 error = copyin(SCARG_P32(uap, nsv), &sv, sizeof(sv));
1433 if (error != 0)
1434 return (error);
1435
1436 /*
1437 * SunOS uses the mask 0x0004 as SV_RESETHAND
1438 * meaning: `reset to SIG_DFL on delivery'.
1439 * We support only the bits in: 0xF
1440 * (those bits are the same as ours)
1441 */
1442 if (sv.sv_flags & ~0xF)
1443 return (EINVAL);
1444
1445 sunos32_sigvec_to_sigaction(&sv, &nsa);
1446 }
1447 error = sigaction1(l, SCARG(uap, signum),
1448 SCARG_P32(uap, nsv) ? &nsa : 0,
1449 SCARG_P32(uap, osv) ? &osa : 0,
1450 NULL, 0);
1451 if (error != 0)
1452 return (error);
1453
1454 if (SCARG_P32(uap, osv)) {
1455 sunos32_sigvec_from_sigaction(&sv, &osa);
1456 error = copyout(&sv, SCARG_P32(uap, osv), sizeof(sv));
1457 if (error != 0)
1458 return (error);
1459 }
1460
1461 return (0);
1462 }
1463