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