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