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