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