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