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