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