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