sunos32_misc.c revision 1.62 1 /* $NetBSD: sunos32_misc.c,v 1.62 2008/08/07 20:15:32 plunky 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.62 2008/08/07 20:15:32 plunky 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 sockopt sopt;
810 struct socket *so;
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 struct linger lg;
820
821 lg.l_onoff = 0;
822 error = so_setsockopt(l, so, SCARG(uap, level), SO_LINGER,
823 &lg, sizeof(lg));
824 goto out;
825 }
826 if (SCARG(uap, level) == IPPROTO_IP) {
827 #define SUNOS_IP_MULTICAST_IF 2
828 #define SUNOS_IP_MULTICAST_TTL 3
829 #define SUNOS_IP_MULTICAST_LOOP 4
830 #define SUNOS_IP_ADD_MEMBERSHIP 5
831 #define SUNOS_IP_DROP_MEMBERSHIP 6
832 static const int ipoptxlat[] = {
833 IP_MULTICAST_IF,
834 IP_MULTICAST_TTL,
835 IP_MULTICAST_LOOP,
836 IP_ADD_MEMBERSHIP,
837 IP_DROP_MEMBERSHIP
838 };
839 if (name >= SUNOS_IP_MULTICAST_IF &&
840 name <= SUNOS_IP_DROP_MEMBERSHIP) {
841 name = ipoptxlat[name - SUNOS_IP_MULTICAST_IF];
842 }
843 }
844 if (SCARG(uap, valsize) > MLEN) {
845 error = EINVAL;
846 goto out;
847 }
848 sockopt_init(&sopt, SCARG(uap, level), name, SCARG(uap, valsize));
849 if (SCARG_P32(uap, val)) {
850 error = copyin(SCARG_P32(uap, val), sopt.sopt_data,
851 (u_int)SCARG(uap, valsize));
852 }
853 if (error == 0)
854 error = sosetopt(so, &sopt);
855 sockopt_destroy(&sopt);
856 out:
857 fd_putfile(SCARG(uap, s));
858 return (error);
859 }
860
861 static inline int sunos32_sys_socket_common(struct lwp *, register_t *,
862 int type);
863 static inline int
864 sunos32_sys_socket_common(struct lwp *l, register_t *retval, int type)
865 {
866 struct socket *so;
867 int error, fd;
868
869 /* fd_getsock() will use the descriptor for us */
870 fd = (int)*retval;
871 if ((error = fd_getsock(fd, &so)) == 0) {
872 if (type == SOCK_DGRAM)
873 so->so_options |= SO_BROADCAST;
874 fd_putfile(fd);
875 }
876 return (error);
877 }
878
879 int
880 sunos32_sys_socket(struct lwp *l, const struct sunos32_sys_socket_args *uap, register_t *retval)
881 {
882 /* {
883 syscallarg(int) domain;
884 syscallarg(int) type;
885 syscallarg(int) protocol;
886 } */
887 int error;
888
889 error = netbsd32_sys___socket30(l, (const void *)uap, retval);
890 if (error)
891 return (error);
892 return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
893 }
894
895 int
896 sunos32_sys_socketpair(struct lwp *l, const struct sunos32_sys_socketpair_args *uap, register_t *retval)
897 {
898 /* {
899 syscallarg(int) domain;
900 syscallarg(int) type;
901 syscallarg(int) protocol;
902 syscallarg(int *) rsv;
903 } */
904 int error;
905
906 error = netbsd32_socketpair(l, (const void *)uap, retval);
907 if (error)
908 return (error);
909 return sunos32_sys_socket_common(l, retval, SCARG(uap, type));
910 }
911
912
913 /*
914 * XXX: This needs cleaning up.
915 */
916 int
917 sunos32_sys_auditsys(struct lwp *l, const struct sunos32_sys_auditsys_args *uap, register_t *retval)
918 {
919 return 0;
920 }
921
922 int
923 sunos32_sys_uname(struct lwp *l, const struct sunos32_sys_uname_args *uap, register_t *retval)
924 {
925 /* {
926 syscallarg(sunos32_utsnamep_t) name;
927 } */
928 struct sunos_utsname sut;
929
930 memset(&sut, 0, sizeof(sut));
931
932 memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
933 memcpy(sut.nodename, hostname, sizeof(sut.nodename));
934 sut.nodename[sizeof(sut.nodename)-1] = '\0';
935 memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
936 memcpy(sut.version, "1", sizeof(sut.version) - 1);
937 memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
938
939 return copyout((void *)&sut, SCARG_P32(uap, name),
940 sizeof(struct sunos_utsname));
941 }
942
943 int
944 sunos32_sys_setpgrp(struct lwp *l, const struct sunos32_sys_setpgrp_args *uap, register_t *retval)
945 {
946 /* {
947 syscallarg(int) pid;
948 syscallarg(int) pgid;
949 } */
950 struct proc *p = l->l_proc;
951
952 /*
953 * difference to our setpgid call is to include backwards
954 * compatibility to pre-setsid() binaries. Do setsid()
955 * instead of setpgid() in those cases where the process
956 * tries to create a new session the old way.
957 */
958 if (!SCARG(uap, pgid) &&
959 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
960 return sys_setsid(l, NULL, retval);
961 else
962 return netbsd32_setpgid(l, (const void *)uap, retval);
963 }
964
965 int
966 sunos32_sys_open(struct lwp *l, const struct sunos32_sys_open_args *uap, register_t *retval)
967 {
968 /* {
969 syscallarg(const netbsd32_charp) path;
970 syscallarg(int) flags;
971 syscallarg(int) mode;
972 } */
973 struct proc *p = l->l_proc;
974 struct sys_open_args ua;
975 int lf, r;
976 int noctty;
977 int ret;
978
979 /* convert mode into NetBSD mode */
980 lf = SCARG(uap, flags);
981 noctty = lf & 0x8000;
982 r = (lf & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
983 r |= ((lf & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
984 r |= ((lf & 0x0080) ? O_SHLOCK : 0);
985 r |= ((lf & 0x0100) ? O_EXLOCK : 0);
986 r |= ((lf & 0x2000) ? O_FSYNC : 0);
987
988 SUNOS32TOP_UAP(path, const char);
989 SCARG(&ua, flags) = r;
990 SUNOS32TO64_UAP(mode);
991
992 ret = sys_open(l, &ua, retval);
993
994 /* XXXSMP unlocked */
995 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
996 file_t *fp;
997 int fd;
998
999 fd = (int)*retval;
1000 fp = fd_getfile(fd);
1001
1002 /* ignore any error, just give it a try */
1003 if (fp != NULL) {
1004 if (fp->f_type == DTYPE_VNODE)
1005 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, NULL);
1006 fd_putfile(fd);
1007 }
1008 }
1009 return ret;
1010 }
1011
1012 #if defined (NFSSERVER)
1013 int
1014 sunos32_sys_nfssvc(struct lwp *l, const struct sunos32_sys_nfssvc_args *uap, register_t *retval)
1015 {
1016 #if 0
1017 struct emul *e = p->p_emul;
1018 struct sys_nfssvc_args outuap;
1019 struct sockaddr sa;
1020 int error;
1021 void *sg = stackgap_init(p, 0);
1022
1023 memset(&outuap, 0, sizeof outuap);
1024 SCARG(&outuap, fd) = SCARG(uap, fd);
1025 SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
1026 SCARG(&outuap, msklen) = sizeof(sa);
1027 SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
1028 SCARG(&outuap, mtchlen) = sizeof(sa);
1029
1030 memset(&sa, 0, sizeof sa);
1031 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
1032 return (error);
1033 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
1034 return (error);
1035
1036 return nfssvc(l, &outuap, retval);
1037 #else
1038 return (ENOSYS);
1039 #endif
1040 }
1041 #endif /* NFSSERVER */
1042
1043 int
1044 sunos32_sys_ustat(struct lwp *l, const struct sunos32_sys_ustat_args *uap, register_t *retval)
1045 {
1046 /* {
1047 syscallarg(int) dev;
1048 syscallarg(sunos32_ustatp_t) buf;
1049 } */
1050 struct sunos_ustat us;
1051 int error;
1052
1053 memset(&us, 0, sizeof us);
1054
1055 /*
1056 * XXX: should set f_tfree and f_tinode at least
1057 * How do we translate dev -> fstat? (and then to sunos_ustat)
1058 */
1059
1060 if ((error = copyout(&us, SCARG_P32(uap, buf), sizeof us)) != 0)
1061 return (error);
1062 return 0;
1063 }
1064
1065 int
1066 sunos32_sys_quotactl(struct lwp *l, const struct sunos32_sys_quotactl_args *uap, register_t *retval)
1067 {
1068
1069 return EINVAL;
1070 }
1071
1072 int
1073 sunos32_sys_vhangup(struct lwp *l, const void *v, register_t *retval)
1074 {
1075 struct proc *p = l->l_proc;
1076 struct session *sp = p->p_session;
1077
1078 if (sp->s_ttyvp == 0)
1079 return 0;
1080
1081 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
1082 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
1083
1084 (void) ttywait(sp->s_ttyp);
1085 if (sp->s_ttyvp)
1086 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
1087 if (sp->s_ttyvp)
1088 vrele(sp->s_ttyvp);
1089 sp->s_ttyvp = NULL;
1090
1091 return 0;
1092 }
1093
1094 static int
1095 sunstatfs(struct statvfs *sp, void *sbuf)
1096 {
1097 struct sunos_statfs ssfs;
1098
1099 memset(&ssfs, 0, sizeof ssfs);
1100 ssfs.f_type = 0;
1101 ssfs.f_bsize = sp->f_bsize;
1102 ssfs.f_blocks = sp->f_blocks;
1103 ssfs.f_bfree = sp->f_bfree;
1104 ssfs.f_bavail = sp->f_bavail;
1105 ssfs.f_files = sp->f_files;
1106 ssfs.f_ffree = sp->f_ffree;
1107 ssfs.f_fsid = sp->f_fsidx;
1108 return copyout((void *)&ssfs, sbuf, sizeof ssfs);
1109 }
1110
1111 int
1112 sunos32_sys_statfs(struct lwp *l, const struct sunos32_sys_statfs_args *uap, register_t *retval)
1113 {
1114 /* {
1115 syscallarg(const netbsd32_charp) path;
1116 syscallarg(sunos32_statfsp_t) buf;
1117 } */
1118 struct mount *mp;
1119 struct statvfs *sp;
1120 int error;
1121 struct nameidata nd;
1122 struct sys_statvfs1_args ua;
1123
1124 SUNOS32TOP_UAP(path, const char);
1125
1126 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1127 SCARG(&ua, path));
1128 if ((error = namei(&nd)) != 0)
1129 return (error);
1130 mp = nd.ni_vp->v_mount;
1131 sp = &mp->mnt_stat;
1132 vrele(nd.ni_vp);
1133 if ((error = VFS_STATVFS(mp, sp)) != 0)
1134 return (error);
1135 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1136 return sunstatfs(sp, SCARG_P32(uap, buf));
1137 }
1138
1139 int
1140 sunos32_sys_fstatfs(struct lwp *l, const struct sunos32_sys_fstatfs_args *uap, register_t *retval)
1141 {
1142 /* {
1143 syscallarg(int) fd;
1144 syscallarg(sunos32_statfsp_t) buf;
1145 } */
1146 file_t *fp;
1147 struct mount *mp;
1148 struct statvfs *sp;
1149 int error;
1150
1151 /* fd_getvnode() will use the descriptor for us */
1152 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1153 return (error);
1154 mp = ((struct vnode *)fp->f_data)->v_mount;
1155 sp = &mp->mnt_stat;
1156 if ((error = VFS_STATVFS(mp, sp)) != 0)
1157 goto out;
1158 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1159 error = sunstatfs(sp, SCARG_P32(uap, buf));
1160 out:
1161 fd_putfile(SCARG(uap, fd));
1162 return (error);
1163 }
1164
1165 int
1166 sunos32_sys_exportfs(struct lwp *l, const struct sunos32_sys_exportfs_args *uap, register_t *retval)
1167 {
1168 /*
1169 * XXX: should perhaps translate into a mount(2)
1170 * with MOUNT_EXPORT?
1171 */
1172 return 0;
1173 }
1174
1175 int
1176 sunos32_sys_mknod(struct lwp *l, const struct sunos32_sys_mknod_args *uap, register_t *retval)
1177 {
1178 /* {
1179 syscallarg(const netbsd32_charp) path;
1180 syscallarg(int) mode;
1181 syscallarg(int) dev;
1182 } */
1183
1184 if (S_ISFIFO(SCARG(uap, mode)))
1185 return netbsd32_mkfifo(l, (const struct netbsd32_mkfifo_args *)uap, retval);
1186
1187 return netbsd32_mknod(l, (const struct netbsd32_mknod_args *)uap, retval);
1188 }
1189
1190 #define SUNOS_SC_ARG_MAX 1
1191 #define SUNOS_SC_CHILD_MAX 2
1192 #define SUNOS_SC_CLK_TCK 3
1193 #define SUNOS_SC_NGROUPS_MAX 4
1194 #define SUNOS_SC_OPEN_MAX 5
1195 #define SUNOS_SC_JOB_CONTROL 6
1196 #define SUNOS_SC_SAVED_IDS 7
1197 #define SUNOS_SC_VERSION 8
1198
1199 int
1200 sunos32_sys_sysconf(struct lwp *l, const struct sunos32_sys_sysconf_args *uap, register_t *retval)
1201 {
1202 /* {
1203 syscallarg(int) name;
1204 } */
1205 extern u_int maxfiles;
1206
1207 switch(SCARG(uap, name)) {
1208 case SUNOS_SC_ARG_MAX:
1209 *retval = ARG_MAX;
1210 break;
1211 case SUNOS_SC_CHILD_MAX:
1212 *retval = maxproc;
1213 break;
1214 case SUNOS_SC_CLK_TCK:
1215 *retval = 60; /* should this be `hz', ie. 100? */
1216 break;
1217 case SUNOS_SC_NGROUPS_MAX:
1218 *retval = NGROUPS_MAX;
1219 break;
1220 case SUNOS_SC_OPEN_MAX:
1221 *retval = maxfiles;
1222 break;
1223 case SUNOS_SC_JOB_CONTROL:
1224 *retval = 1;
1225 break;
1226 case SUNOS_SC_SAVED_IDS:
1227 #ifdef _POSIX_SAVED_IDS
1228 *retval = 1;
1229 #else
1230 *retval = 0;
1231 #endif
1232 break;
1233 case SUNOS_SC_VERSION:
1234 *retval = 198808;
1235 break;
1236 default:
1237 return EINVAL;
1238 }
1239 return 0;
1240 }
1241
1242 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */
1243 #define SUNOS_RLIM_NLIMITS 7
1244
1245 int
1246 sunos32_sys_getrlimit(struct lwp *l, const struct sunos32_sys_getrlimit_args *uap, register_t *retval)
1247 {
1248 /* {
1249 syscallarg(u_int) which;
1250 syscallarg(netbsd32_orlimitp_t) rlp;
1251 } */
1252 struct compat_43_netbsd32_ogetrlimit_args ua_43;
1253
1254 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1255 return EINVAL;
1256
1257 SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1258 SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1259
1260 return compat_43_netbsd32_ogetrlimit(l, &ua_43, retval);
1261 }
1262
1263 int
1264 sunos32_sys_setrlimit(struct lwp *l, const struct sunos32_sys_setrlimit_args *uap, register_t *retval)
1265 {
1266 /* {
1267 syscallarg(u_int) which;
1268 syscallarg(netbsd32_orlimitp_t) rlp;
1269 } */
1270 struct compat_43_netbsd32_osetrlimit_args ua_43;
1271
1272 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1273 return EINVAL;
1274
1275 SCARG(&ua_43, which) = SCARG(uap, which) == SUNOS_RLIMIT_NOFILE ? RLIMIT_NOFILE : SCARG(uap, which);
1276 SCARG(&ua_43, rlp) = SCARG(uap, rlp);
1277
1278 return compat_43_netbsd32_osetrlimit(l, &ua_43, retval);
1279 }
1280
1281 #if defined(PTRACE) || defined(_LKM)
1282 /* for the m68k machines */
1283 #ifndef PT_GETFPREGS
1284 #define PT_GETFPREGS -1
1285 #endif
1286 #ifndef PT_SETFPREGS
1287 #define PT_SETFPREGS -1
1288 #endif
1289
1290 static const int sreq2breq[] = {
1291 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1,
1292 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE,
1293 PT_KILL, -1, PT_ATTACH, PT_DETACH,
1294 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS
1295 };
1296 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1297 #endif
1298
1299 int
1300 sunos32_sys_ptrace(struct lwp *l, const struct sunos32_sys_ptrace_args *uap, register_t *retval)
1301 {
1302 #if defined(PTRACE) || defined(_LKM)
1303 /* {
1304 syscallarg(int) req;
1305 syscallarg(pid_t) pid;
1306 syscallarg(netbsd32_caddr_t) addr;
1307 syscallarg(int) data;
1308 syscallarg(netbsd32_charp) addr2;
1309 } */
1310 struct netbsd32_ptrace_args pa;
1311 int req;
1312
1313 #ifdef _LKM
1314 #define sys_ptrace sysent[SYS_ptrace].sy_call
1315 if (sys_ptrace == sys_nosys)
1316 return ENOSYS;
1317 #endif
1318
1319 req = SCARG(uap, req);
1320 if ((unsigned int)req >= nreqs)
1321 return (EINVAL);
1322
1323 req = sreq2breq[req];
1324 if (req == -1)
1325 return (EINVAL);
1326
1327 SCARG(&pa, req) = req;
1328 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1329 SCARG(&pa, addr) = SCARG(uap, addr);
1330 SCARG(&pa, data) = SCARG(uap, data);
1331
1332 return netbsd32_ptrace(l, &pa, retval);
1333 #else
1334 return (ENOSYS);
1335 #endif /* PTRACE || _LKM */
1336 }
1337
1338 /*
1339 * SunOS reboot system call (for compatibility).
1340 * Sun lets you pass in a boot string which the PROM
1341 * saves and provides to the next boot program.
1342 */
1343
1344 #define SUNOS_RB_ASKNAME 0x001
1345 #define SUNOS_RB_SINGLE 0x002
1346 #define SUNOS_RB_NOSYNC 0x004
1347 #define SUNOS_RB_HALT 0x008
1348 #define SUNOS_RB_DUMP 0x080
1349 #define SUNOS_RB_STRING 0x200
1350
1351 static struct sunos_howto_conv {
1352 int sun_howto;
1353 int bsd_howto;
1354 } sunos_howto_conv[] = {
1355 { SUNOS_RB_ASKNAME, RB_ASKNAME },
1356 { SUNOS_RB_SINGLE, RB_SINGLE },
1357 { SUNOS_RB_NOSYNC, RB_NOSYNC },
1358 { SUNOS_RB_HALT, RB_HALT },
1359 { SUNOS_RB_DUMP, RB_DUMP },
1360 { SUNOS_RB_STRING, RB_STRING },
1361 { 0x000, 0 },
1362 };
1363
1364 int
1365 sunos32_sys_reboot(struct lwp *l, const struct sunos32_sys_reboot_args *uap, register_t *retval)
1366 {
1367 /* {
1368 syscallarg(int) howto;
1369 syscallarg(netbsd32_charp) bootstr;
1370 } */
1371 struct sys_reboot_args ua;
1372 struct sunos_howto_conv *convp;
1373 int error, bsd_howto, sun_howto;
1374 char *bootstr;
1375
1376 if ((error = kauth_authorize_system(l->l_cred,
1377 KAUTH_SYSTEM_REBOOT, 0, NULL, NULL, NULL)) != 0)
1378 return (error);
1379
1380 /*
1381 * Convert howto bits to BSD format.
1382 */
1383 sun_howto = SCARG(uap, howto);
1384 bsd_howto = 0;
1385 convp = sunos_howto_conv;
1386 while (convp->sun_howto) {
1387 if (sun_howto & convp->sun_howto)
1388 bsd_howto |= convp->bsd_howto;
1389 convp++;
1390 }
1391
1392 /*
1393 * Sun RB_STRING (Get user supplied bootstring.)
1394 * If the machine supports passing a string to the
1395 * next booted kernel.
1396 */
1397 if (sun_howto & SUNOS_RB_STRING)
1398 bootstr = SCARG_P32(uap, bootstr);
1399 else
1400 bootstr = NULL;
1401
1402 SCARG(&ua, opt) = bsd_howto;
1403 SCARG(&ua, bootstr) = bootstr;
1404 return (sys_reboot(l, &ua, retval));
1405 }
1406
1407 /*
1408 * Generalized interface signal handler, 4.3-compatible.
1409 */
1410 /* ARGSUSED */
1411 int
1412 sunos32_sys_sigvec(struct lwp *l, const struct sunos32_sys_sigvec_args *uap, register_t *retval)
1413 {
1414 /* {
1415 syscallarg(int) signum;
1416 syscallarg(struct sigvec *) nsv;
1417 syscallarg(struct sigvec *) osv;
1418 } */
1419 struct netbsd32_sigvec sv;
1420 struct sigaction nsa, osa;
1421 int error;
1422
1423 if (SCARG_P32(uap, nsv)) {
1424 error = copyin(SCARG_P32(uap, nsv), &sv, sizeof(sv));
1425 if (error != 0)
1426 return (error);
1427
1428 /*
1429 * SunOS uses the mask 0x0004 as SV_RESETHAND
1430 * meaning: `reset to SIG_DFL on delivery'.
1431 * We support only the bits in: 0xF
1432 * (those bits are the same as ours)
1433 */
1434 if (sv.sv_flags & ~0xF)
1435 return (EINVAL);
1436
1437 sunos32_sigvec_to_sigaction(&sv, &nsa);
1438 }
1439 error = sigaction1(l, SCARG(uap, signum),
1440 SCARG_P32(uap, nsv) ? &nsa : 0,
1441 SCARG_P32(uap, osv) ? &osa : 0,
1442 NULL, 0);
1443 if (error != 0)
1444 return (error);
1445
1446 if (SCARG_P32(uap, osv)) {
1447 sunos32_sigvec_from_sigaction(&sv, &osa);
1448 error = copyout(&sv, SCARG_P32(uap, osv), sizeof(sv));
1449 if (error != 0)
1450 return (error);
1451 }
1452
1453 return (0);
1454 }
1455