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