sunos_misc.c revision 1.143.2.1 1 /* $NetBSD: sunos_misc.c,v 1.143.2.1 2010/06/12 18:38:01 riz 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.143.2.1 2010/06/12 18:38:01 riz 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 if (cookiebuf)
599 free(cookiebuf, M_TEMP);
600 cookiebuf = NULL;
601 goto again;
602 }
603 fp->f_offset = off; /* update the vnode offset */
604
605 eof:
606 *retval = SCARG(uap, nbytes) - resid;
607 out:
608 VOP_UNLOCK(vp, 0);
609 free(cookiebuf, M_TEMP);
610 free(buf, M_TEMP);
611 out1:
612 FILE_UNUSE(fp, l);
613 return (error);
614 }
615
616 #define SUNOS__MAP_NEW 0x80000000 /* if not, old mmap & cannot handle */
617
618 int
619 sunos_sys_mmap(l, v, retval)
620 struct lwp *l;
621 void *v;
622 register_t *retval;
623 {
624 struct sunos_sys_mmap_args *uap = v;
625 struct sys_mmap_args ouap;
626
627 /*
628 * Verify the arguments.
629 */
630 if (SCARG(uap, prot) & ~(PROT_READ|PROT_WRITE|PROT_EXEC))
631 return (EINVAL); /* XXX still needed? */
632
633 if ((SCARG(uap, flags) & SUNOS__MAP_NEW) == 0)
634 return (EINVAL);
635
636 SCARG(&ouap, flags) = SCARG(uap, flags) & ~SUNOS__MAP_NEW;
637 SCARG(&ouap, addr) = SCARG(uap, addr);
638 SCARG(&ouap, len) = SCARG(uap, len);
639 SCARG(&ouap, prot) = SCARG(uap, prot);
640 SCARG(&ouap, fd) = SCARG(uap, fd);
641 SCARG(&ouap, pos) = SCARG(uap, pos);
642
643 return (sys_mmap(l, &ouap, retval));
644 }
645
646 #define MC_SYNC 1
647 #define MC_LOCK 2
648 #define MC_UNLOCK 3
649 #define MC_ADVISE 4
650 #define MC_LOCKAS 5
651 #define MC_UNLOCKAS 6
652
653 int
654 sunos_sys_mctl(l, v, retval)
655 struct lwp *l;
656 void *v;
657 register_t *retval;
658 {
659 struct sunos_sys_mctl_args *uap = v;
660
661 switch (SCARG(uap, func)) {
662 case MC_ADVISE: /* ignore for now */
663 return (0);
664 case MC_SYNC: /* translate to msync */
665 return (sys___msync13(l, uap, retval));
666 default:
667 return (EINVAL);
668 }
669 }
670
671 int
672 sunos_sys_setsockopt(l, v, retval)
673 struct lwp *l;
674 void *v;
675 register_t *retval;
676 {
677 struct sunos_sys_setsockopt_args *uap = v;
678 struct proc *p = l->l_proc;
679 struct file *fp;
680 struct mbuf *m = NULL;
681 int error;
682
683 /* getsock() will use the descriptor for us */
684 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
685 return (error);
686 #define SO_DONTLINGER (~SO_LINGER)
687 if (SCARG(uap, name) == SO_DONTLINGER) {
688 m = m_get(M_WAIT, MT_SOOPTS);
689 mtod(m, struct linger *)->l_onoff = 0;
690 m->m_len = sizeof(struct linger);
691 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
692 SO_LINGER, m);
693 goto out;
694 }
695 if (SCARG(uap, level) == IPPROTO_IP) {
696 #define SUNOS_IP_MULTICAST_IF 2
697 #define SUNOS_IP_MULTICAST_TTL 3
698 #define SUNOS_IP_MULTICAST_LOOP 4
699 #define SUNOS_IP_ADD_MEMBERSHIP 5
700 #define SUNOS_IP_DROP_MEMBERSHIP 6
701 static const int ipoptxlat[] = {
702 IP_MULTICAST_IF,
703 IP_MULTICAST_TTL,
704 IP_MULTICAST_LOOP,
705 IP_ADD_MEMBERSHIP,
706 IP_DROP_MEMBERSHIP
707 };
708 if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
709 SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
710 SCARG(uap, name) =
711 ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
712 }
713 }
714 if (SCARG(uap, valsize) > MLEN) {
715 error = EINVAL;
716 goto out;
717 }
718 if (SCARG(uap, val)) {
719 m = m_get(M_WAIT, MT_SOOPTS);
720 error = copyin(SCARG(uap, val), mtod(m, caddr_t),
721 (u_int)SCARG(uap, valsize));
722 if (error) {
723 (void) m_free(m);
724 goto out;
725 }
726 m->m_len = SCARG(uap, valsize);
727 }
728 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
729 SCARG(uap, name), m);
730 out:
731 FILE_UNUSE(fp, l);
732 return (error);
733 }
734
735 static inline int sunos_sys_socket_common(struct lwp *, register_t *,
736 int type);
737 static inline int
738 sunos_sys_socket_common(l, retval, type)
739 struct lwp *l;
740 register_t *retval;
741 int type;
742 {
743 struct socket *so;
744 struct file *fp;
745 int error, fd;
746
747 /* getsock() will use the descriptor for us */
748 fd = (int)*retval;
749 if ((error = getsock(l->l_proc->p_fd, fd, &fp)) == 0) {
750 so = (struct socket *)fp->f_data;
751 if (type == SOCK_DGRAM)
752 so->so_options |= SO_BROADCAST;
753 }
754 FILE_UNUSE(fp, l);
755 return (error);
756 }
757
758 int
759 sunos_sys_socket(l, v, retval)
760 struct lwp *l;
761 void *v;
762 register_t *retval;
763 {
764 struct sunos_sys_socket_args /* {
765 syscallarg(int) domain;
766 syscallarg(int) type;
767 syscallarg(int) protocol;
768 } */ *uap = v;
769 int error;
770
771 error = compat_30_sys_socket(l, v, retval);
772 if (error)
773 return (error);
774 return sunos_sys_socket_common(l, retval, SCARG(uap, type));
775 }
776
777 int
778 sunos_sys_socketpair(l, v, retval)
779 struct lwp *l;
780 void *v;
781 register_t *retval;
782 {
783 struct sunos_sys_socketpair_args /* {
784 syscallarg(int) domain;
785 syscallarg(int) type;
786 syscallarg(int) protocol;
787 syscallarg(int *) rsv;
788 } */ *uap = v;
789 int error;
790
791 error = sys_socketpair(l, v, retval);
792 if (error)
793 return (error);
794 return sunos_sys_socket_common(l, retval, SCARG(uap, type));
795 }
796
797 /*
798 * XXX: This needs cleaning up.
799 */
800 int
801 sunos_sys_auditsys(l, v, retval)
802 struct lwp *l;
803 void *v;
804 register_t *retval;
805 {
806 return 0;
807 }
808
809 int
810 sunos_sys_uname(l, v, retval)
811 struct lwp *l;
812 void *v;
813 register_t *retval;
814 {
815 struct sunos_sys_uname_args *uap = v;
816 struct sunos_utsname sut;
817
818 memset(&sut, 0, sizeof(sut));
819
820 memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
821 memcpy(sut.nodename, hostname, sizeof(sut.nodename));
822 sut.nodename[sizeof(sut.nodename)-1] = '\0';
823 memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
824 memcpy(sut.version, "1", sizeof(sut.version) - 1);
825 memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
826
827 return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
828 sizeof(struct sunos_utsname));
829 }
830
831 int
832 sunos_sys_setpgrp(l, v, retval)
833 struct lwp *l;
834 void *v;
835 register_t *retval;
836 {
837 struct sunos_sys_setpgrp_args *uap = v;
838 struct proc *p = l->l_proc;
839
840 /*
841 * difference to our setpgid call is to include backwards
842 * compatibility to pre-setsid() binaries. Do setsid()
843 * instead of setpgid() in those cases where the process
844 * tries to create a new session the old way.
845 */
846 if (!SCARG(uap, pgid) &&
847 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
848 return sys_setsid(l, uap, retval);
849 else
850 return sys_setpgid(l, uap, retval);
851 }
852
853 int
854 sunos_sys_open(l, v, retval)
855 struct lwp *l;
856 void *v;
857 register_t *retval;
858 {
859 struct sunos_sys_open_args *uap = v;
860 struct proc *p = l->l_proc;
861 int smode, nmode;
862 int noctty;
863 int ret;
864
865 caddr_t sg = stackgap_init(p, 0);
866
867 /* convert mode into NetBSD mode */
868 smode = SCARG(uap, flags);
869 noctty = smode & 0x8000;
870 nmode = smode &
871 (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800);
872 nmode |= ((smode & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
873 nmode |= ((smode & 0x0080) ? O_SHLOCK : 0);
874 nmode |= ((smode & 0x0100) ? O_EXLOCK : 0);
875 nmode |= ((smode & 0x2000) ? O_FSYNC : 0);
876
877 if (nmode & O_CREAT)
878 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
879 else
880 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
881
882 SCARG(uap, flags) = nmode;
883 ret = sys_open(l, (struct sys_open_args *)uap, retval);
884
885 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
886 struct filedesc *fdp = p->p_fd;
887 struct file *fp;
888
889 fp = fd_getfile(fdp, *retval);
890 simple_unlock(&fp->f_slock);
891
892 /* ignore any error, just give it a try */
893 if (fp != NULL && fp->f_type == DTYPE_VNODE)
894 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, l);
895 }
896 return ret;
897 }
898
899 #if defined (NFSSERVER)
900 int
901 sunos_sys_nfssvc(l, v, retval)
902 struct lwp *l;
903 void *v;
904 register_t *retval;
905 {
906 #if 0
907 struct sunos_sys_nfssvc_args *uap = v;
908 struct proc *p = l->l_proc;
909 struct emul *e = p->p_emul;
910 struct sys_nfssvc_args outuap;
911 struct sockaddr sa;
912 int error;
913 caddr_t sg = stackgap_init(p, 0);
914
915 memset(&outuap, 0, sizeof outuap);
916 SCARG(&outuap, fd) = SCARG(uap, fd);
917 SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
918 SCARG(&outuap, msklen) = sizeof(sa);
919 SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
920 SCARG(&outuap, mtchlen) = sizeof(sa);
921
922 memset(&sa, 0, sizeof sa);
923 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
924 return (error);
925 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
926 return (error);
927
928 return nfssvc(l, &outuap, retval);
929 #else
930 return (ENOSYS);
931 #endif
932 }
933 #endif /* NFSSERVER */
934
935 int
936 sunos_sys_ustat(l, v, retval)
937 struct lwp *l;
938 void *v;
939 register_t *retval;
940 {
941 struct sunos_sys_ustat_args *uap = v;
942 struct sunos_ustat us;
943 int error;
944
945 memset(&us, 0, sizeof us);
946
947 /*
948 * XXX: should set f_tfree and f_tinode at least
949 * How do we translate dev -> fstat? (and then to sunos_ustat)
950 */
951
952 if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
953 return (error);
954 return 0;
955 }
956
957 int
958 sunos_sys_quotactl(l, v, retval)
959 struct lwp *l;
960 void *v;
961 register_t *retval;
962 {
963
964 return EINVAL;
965 }
966
967 int
968 sunos_sys_vhangup(l, v, retval)
969 struct lwp *l;
970 void *v;
971 register_t *retval;
972 {
973 struct proc *p = l->l_proc;
974 struct session *sp = p->p_session;
975
976 if (sp->s_ttyvp == 0)
977 return 0;
978
979 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
980 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
981
982 (void) ttywait(sp->s_ttyp);
983 if (sp->s_ttyvp)
984 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
985 if (sp->s_ttyvp)
986 vrele(sp->s_ttyvp);
987 sp->s_ttyvp = NULL;
988
989 return 0;
990 }
991
992 static int
993 sunstatfs(sp, buf)
994 struct statvfs *sp;
995 caddr_t buf;
996 {
997 struct sunos_statfs ssfs;
998
999 memset(&ssfs, 0, sizeof ssfs);
1000 ssfs.f_type = 0;
1001 ssfs.f_bsize = sp->f_bsize;
1002 ssfs.f_blocks = sp->f_blocks;
1003 ssfs.f_bfree = sp->f_bfree;
1004 ssfs.f_bavail = sp->f_bavail;
1005 ssfs.f_files = sp->f_files;
1006 ssfs.f_ffree = sp->f_ffree;
1007 ssfs.f_fsid = sp->f_fsidx;
1008 return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
1009 }
1010
1011 int
1012 sunos_sys_statfs(l, v, retval)
1013 struct lwp *l;
1014 void *v;
1015 register_t *retval;
1016 {
1017 struct sunos_sys_statfs_args *uap = v;
1018 struct proc *p = l->l_proc;
1019 struct mount *mp;
1020 struct statvfs *sp;
1021 int error;
1022 struct nameidata nd;
1023
1024 caddr_t sg = stackgap_init(p, 0);
1025 CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
1026
1027 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
1028 if ((error = namei(&nd)) != 0)
1029 return (error);
1030 mp = nd.ni_vp->v_mount;
1031 sp = &mp->mnt_stat;
1032 vrele(nd.ni_vp);
1033 if ((error = VFS_STATVFS(mp, sp, l)) != 0)
1034 return (error);
1035 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1036 return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
1037 }
1038
1039 int
1040 sunos_sys_fstatfs(l, v, retval)
1041 struct lwp *l;
1042 void *v;
1043 register_t *retval;
1044 {
1045 struct sunos_sys_fstatfs_args *uap = v;
1046 struct proc *p = l->l_proc;
1047 struct file *fp;
1048 struct mount *mp;
1049 struct statvfs *sp;
1050 int error;
1051
1052 /* getvnode() will use the descriptor for us */
1053 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1054 return (error);
1055 mp = ((struct vnode *)fp->f_data)->v_mount;
1056 sp = &mp->mnt_stat;
1057 if ((error = VFS_STATVFS(mp, sp, l)) != 0)
1058 goto out;
1059 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
1060 error = sunstatfs(sp, (caddr_t)SCARG(uap, buf));
1061 out:
1062 FILE_UNUSE(fp, l);
1063 return (error);
1064 }
1065
1066 int
1067 sunos_sys_exportfs(l, v, retval)
1068 struct lwp *l;
1069 void *v;
1070 register_t *retval;
1071 {
1072 /*
1073 * XXX: should perhaps translate into a mount(2)
1074 * with MOUNT_EXPORT?
1075 */
1076 return 0;
1077 }
1078
1079 int
1080 sunos_sys_mknod(l, v, retval)
1081 struct lwp *l;
1082 void *v;
1083 register_t *retval;
1084 {
1085 struct sunos_sys_mknod_args *uap = v;
1086 struct proc *p = l->l_proc;
1087
1088 caddr_t sg = stackgap_init(p, 0);
1089 CHECK_ALT_CREAT(l, &sg, SCARG(uap, path));
1090
1091 if (S_ISFIFO(SCARG(uap, mode)))
1092 return sys_mkfifo(l, uap, retval);
1093
1094 return sys_mknod(l, (struct sys_mknod_args *)uap, retval);
1095 }
1096
1097 #define SUNOS_SC_ARG_MAX 1
1098 #define SUNOS_SC_CHILD_MAX 2
1099 #define SUNOS_SC_CLK_TCK 3
1100 #define SUNOS_SC_NGROUPS_MAX 4
1101 #define SUNOS_SC_OPEN_MAX 5
1102 #define SUNOS_SC_JOB_CONTROL 6
1103 #define SUNOS_SC_SAVED_IDS 7
1104 #define SUNOS_SC_VERSION 8
1105
1106 int
1107 sunos_sys_sysconf(l, v, retval)
1108 struct lwp *l;
1109 void *v;
1110 register_t *retval;
1111 {
1112 struct sunos_sys_sysconf_args *uap = v;
1113 extern int maxfiles;
1114
1115 switch(SCARG(uap, name)) {
1116 case SUNOS_SC_ARG_MAX:
1117 *retval = ARG_MAX;
1118 break;
1119 case SUNOS_SC_CHILD_MAX:
1120 *retval = maxproc;
1121 break;
1122 case SUNOS_SC_CLK_TCK:
1123 *retval = 60; /* should this be `hz', ie. 100? */
1124 break;
1125 case SUNOS_SC_NGROUPS_MAX:
1126 *retval = NGROUPS_MAX;
1127 break;
1128 case SUNOS_SC_OPEN_MAX:
1129 *retval = maxfiles;
1130 break;
1131 case SUNOS_SC_JOB_CONTROL:
1132 *retval = 1;
1133 break;
1134 case SUNOS_SC_SAVED_IDS:
1135 #ifdef _POSIX_SAVED_IDS
1136 *retval = 1;
1137 #else
1138 *retval = 0;
1139 #endif
1140 break;
1141 case SUNOS_SC_VERSION:
1142 *retval = 198808;
1143 break;
1144 default:
1145 return EINVAL;
1146 }
1147 return 0;
1148 }
1149
1150 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */
1151 #define SUNOS_RLIM_NLIMITS 7
1152
1153 int
1154 sunos_sys_getrlimit(l, v, retval)
1155 struct lwp *l;
1156 void *v;
1157 register_t *retval;
1158 {
1159 struct sunos_sys_getrlimit_args *uap = v;
1160
1161 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1162 return EINVAL;
1163
1164 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1165 SCARG(uap, which) = RLIMIT_NOFILE;
1166
1167 return compat_43_sys_getrlimit(l, uap, retval);
1168 }
1169
1170 int
1171 sunos_sys_setrlimit(l, v, retval)
1172 struct lwp *l;
1173 void *v;
1174 register_t *retval;
1175 {
1176 struct sunos_sys_getrlimit_args *uap = v;
1177
1178 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1179 return EINVAL;
1180
1181 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1182 SCARG(uap, which) = RLIMIT_NOFILE;
1183
1184 return compat_43_sys_setrlimit(l, uap, retval);
1185 }
1186
1187 #if defined(PTRACE) || defined(_LKM)
1188 /* for the m68k machines */
1189 #ifndef PT_GETFPREGS
1190 #define PT_GETFPREGS -1
1191 #endif
1192 #ifndef PT_SETFPREGS
1193 #define PT_SETFPREGS -1
1194 #endif
1195
1196 static const int sreq2breq[] = {
1197 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1,
1198 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE,
1199 PT_KILL, -1, PT_ATTACH, PT_DETACH,
1200 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS
1201 };
1202 static const int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1203 #endif /* PTRACE || _LKM */
1204
1205 int
1206 sunos_sys_ptrace(l, v, retval)
1207 struct lwp *l;
1208 void *v;
1209 register_t *retval;
1210 {
1211 #if defined(PTRACE) || defined(_LKM)
1212 struct sunos_sys_ptrace_args *uap = v;
1213 struct sys_ptrace_args pa;
1214 int req;
1215
1216 #ifdef _LKM
1217 #define sys_ptrace sysent[SYS_ptrace].sy_call
1218 if (sys_ptrace == sys_nosys)
1219 return ENOSYS;
1220 #endif
1221
1222 req = SCARG(uap, req);
1223
1224 if (req < 0 || req >= nreqs)
1225 return (EINVAL);
1226
1227 req = sreq2breq[req];
1228 if (req == -1)
1229 return (EINVAL);
1230
1231 SCARG(&pa, req) = req;
1232 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1233 SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1234 SCARG(&pa, data) = SCARG(uap, data);
1235
1236 return sys_ptrace(l, &pa, retval);
1237 #else
1238 return ENOSYS;
1239 #endif /* PTRACE || _LKM */
1240 }
1241
1242 /*
1243 * SunOS reboot system call (for compatibility).
1244 * Sun lets you pass in a boot string which the PROM
1245 * saves and provides to the next boot program.
1246 */
1247
1248 #define SUNOS_RB_ASKNAME 0x001
1249 #define SUNOS_RB_SINGLE 0x002
1250 #define SUNOS_RB_NOSYNC 0x004
1251 #define SUNOS_RB_HALT 0x008
1252 #define SUNOS_RB_DUMP 0x080
1253 #define SUNOS_RB_STRING 0x200
1254
1255 static struct sunos_howto_conv {
1256 int sun_howto;
1257 int bsd_howto;
1258 } sunos_howto_conv[] = {
1259 { SUNOS_RB_ASKNAME, RB_ASKNAME },
1260 { SUNOS_RB_SINGLE, RB_SINGLE },
1261 { SUNOS_RB_NOSYNC, RB_NOSYNC },
1262 { SUNOS_RB_HALT, RB_HALT },
1263 { SUNOS_RB_DUMP, RB_DUMP },
1264 { SUNOS_RB_STRING, RB_STRING },
1265 { 0x000, 0 },
1266 };
1267
1268 int
1269 sunos_sys_reboot(l, v, retval)
1270 struct lwp *l;
1271 void *v;
1272 register_t *retval;
1273 {
1274 struct sunos_sys_reboot_args *uap = v;
1275 struct sys_reboot_args ua;
1276 struct sunos_howto_conv *convp;
1277 int error, bsd_howto, sun_howto;
1278 char *bootstr;
1279
1280 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_REBOOT,
1281 0, NULL, NULL, NULL)) != 0)
1282 return (error);
1283
1284 /*
1285 * Convert howto bits to BSD format.
1286 */
1287 sun_howto = SCARG(uap, howto);
1288 bsd_howto = 0;
1289 convp = sunos_howto_conv;
1290 while (convp->sun_howto) {
1291 if (sun_howto & convp->sun_howto)
1292 bsd_howto |= convp->bsd_howto;
1293 convp++;
1294 }
1295
1296 /*
1297 * Sun RB_STRING (Get user supplied bootstring.)
1298 * If the machine supports passing a string to the
1299 * next booted kernel.
1300 */
1301 if (sun_howto & SUNOS_RB_STRING) {
1302 char bs[128];
1303
1304 error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1305
1306 if (error)
1307 bootstr = NULL;
1308 else
1309 bootstr = bs;
1310 } else
1311 bootstr = NULL;
1312
1313 SCARG(&ua, opt) = bsd_howto;
1314 SCARG(&ua, bootstr) = bootstr;
1315 sys_reboot(l, &ua, retval);
1316 return(0);
1317 }
1318
1319 /*
1320 * Generalized interface signal handler, 4.3-compatible.
1321 */
1322 /* ARGSUSED */
1323 int
1324 sunos_sys_sigvec(l, v, retval)
1325 struct lwp *l;
1326 void *v;
1327 register_t *retval;
1328 {
1329 struct sunos_sys_sigvec_args /* {
1330 syscallarg(int) signum;
1331 syscallarg(struct sigvec *) nsv;
1332 syscallarg(struct sigvec *) osv;
1333 } */ *uap = v;
1334 struct sigvec nsv, osv;
1335 struct sigaction nsa, osa;
1336 int error;
1337 /*XXX*/extern void compat_43_sigvec_to_sigaction
1338 __P((const struct sigvec *, struct sigaction *));
1339 /*XXX*/extern void compat_43_sigaction_to_sigvec
1340 __P((const struct sigaction *, struct sigvec *));
1341
1342 if (SCARG(uap, nsv)) {
1343 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
1344 if (error != 0)
1345 return (error);
1346
1347 /*
1348 * SunOS uses the mask 0x0004 as SV_RESETHAND
1349 * meaning: `reset to SIG_DFL on delivery'.
1350 * We support only the bits in: 0xF
1351 * (those bits are the same as ours)
1352 */
1353 if (nsv.sv_flags & ~0xF)
1354 return (EINVAL);
1355
1356 compat_43_sigvec_to_sigaction(&nsv, &nsa);
1357 }
1358 error = sigaction1(l->l_proc, SCARG(uap, signum),
1359 SCARG(uap, nsv) ? &nsa : 0,
1360 SCARG(uap, osv) ? &osa : 0,
1361 NULL, 0);
1362 if (error != 0)
1363 return (error);
1364
1365 if (SCARG(uap, osv)) {
1366 compat_43_sigaction_to_sigvec(&osa, &osv);
1367 error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
1368 if (error != 0)
1369 return (error);
1370 }
1371
1372 return (0);
1373 }
1374