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