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