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