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