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