sunos_misc.c revision 1.67 1 /* $NetBSD: sunos_misc.c,v 1.67 1996/08/09 10:30:23 mrg 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 mtod(m, struct linger *)->l_onoff = 0;
575 m->m_len = sizeof(struct linger);
576 return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
577 SO_LINGER, m));
578 }
579 if (SCARG(uap, level) == IPPROTO_IP) {
580 #define SUNOS_IP_MULTICAST_IF 2
581 #define SUNOS_IP_MULTICAST_TTL 3
582 #define SUNOS_IP_MULTICAST_LOOP 4
583 #define SUNOS_IP_ADD_MEMBERSHIP 5
584 #define SUNOS_IP_DROP_MEMBERSHIP 6
585 static int ipoptxlat[] = {
586 IP_MULTICAST_IF,
587 IP_MULTICAST_TTL,
588 IP_MULTICAST_LOOP,
589 IP_ADD_MEMBERSHIP,
590 IP_DROP_MEMBERSHIP
591 };
592 if (SCARG(uap, name) >= SUNOS_IP_MULTICAST_IF &&
593 SCARG(uap, name) <= SUNOS_IP_DROP_MEMBERSHIP) {
594 SCARG(uap, name) =
595 ipoptxlat[SCARG(uap, name) - SUNOS_IP_MULTICAST_IF];
596 }
597 }
598 if (SCARG(uap, valsize) > MLEN)
599 return (EINVAL);
600 if (SCARG(uap, val)) {
601 m = m_get(M_WAIT, MT_SOOPTS);
602 error = copyin(SCARG(uap, val), mtod(m, caddr_t),
603 (u_int)SCARG(uap, valsize));
604 if (error) {
605 (void) m_free(m);
606 return (error);
607 }
608 m->m_len = SCARG(uap, valsize);
609 }
610 return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
611 SCARG(uap, name), m));
612 }
613
614 int
615 sunos_sys_fchroot(p, v, retval)
616 register struct proc *p;
617 void *v;
618 register_t *retval;
619 {
620 register struct sunos_sys_fchroot_args *uap = v;
621 register struct filedesc *fdp = p->p_fd;
622 register struct vnode *vp;
623 struct file *fp;
624 int error;
625
626 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
627 return (error);
628 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
629 return (error);
630 vp = (struct vnode *)fp->f_data;
631 VOP_LOCK(vp);
632 if (vp->v_type != VDIR)
633 error = ENOTDIR;
634 else
635 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
636 VOP_UNLOCK(vp);
637 if (error)
638 return (error);
639 VREF(vp);
640 if (fdp->fd_rdir != NULL)
641 vrele(fdp->fd_rdir);
642 fdp->fd_rdir = vp;
643 return (0);
644 }
645
646 /*
647 * XXX: This needs cleaning up.
648 */
649 int
650 sunos_sys_auditsys(p, v, retval)
651 struct proc *p;
652 void *v;
653 register_t *retval;
654 {
655 return 0;
656 }
657
658 int
659 sunos_sys_uname(p, v, retval)
660 struct proc *p;
661 void *v;
662 register_t *retval;
663 {
664 struct sunos_sys_uname_args *uap = v;
665 struct sunos_utsname sut;
666 extern char ostype[], machine[], osrelease[];
667
668 bzero(&sut, sizeof(sut));
669
670 bcopy(ostype, sut.sysname, sizeof(sut.sysname) - 1);
671 bcopy(hostname, sut.nodename, sizeof(sut.nodename));
672 sut.nodename[sizeof(sut.nodename)-1] = '\0';
673 bcopy(osrelease, sut.release, sizeof(sut.release) - 1);
674 bcopy("1", sut.version, sizeof(sut.version) - 1);
675 bcopy(machine, sut.machine, sizeof(sut.machine) - 1);
676
677 return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
678 sizeof(struct sunos_utsname));
679 }
680
681 int
682 sunos_sys_setpgrp(p, v, retval)
683 struct proc *p;
684 void *v;
685 register_t *retval;
686 {
687 struct sunos_sys_setpgrp_args *uap = v;
688
689 /*
690 * difference to our setpgid call is to include backwards
691 * compatibility to pre-setsid() binaries. Do setsid()
692 * instead of setpgid() in those cases where the process
693 * tries to create a new session the old way.
694 */
695 if (!SCARG(uap, pgid) &&
696 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
697 return sys_setsid(p, uap, retval);
698 else
699 return sys_setpgid(p, uap, retval);
700 }
701
702 int
703 sunos_sys_open(p, v, retval)
704 struct proc *p;
705 void *v;
706 register_t *retval;
707 {
708 struct sunos_sys_open_args *uap = v;
709 int l, r;
710 int noctty;
711 int ret;
712
713 caddr_t sg = stackgap_init(p->p_emul);
714 SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
715
716 /* convert mode into NetBSD mode */
717 l = SCARG(uap, flags);
718 noctty = l & 0x8000;
719 r = (l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
720 r |= ((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
721 r |= ((l & 0x0080) ? O_SHLOCK : 0);
722 r |= ((l & 0x0100) ? O_EXLOCK : 0);
723 r |= ((l & 0x2000) ? O_FSYNC : 0);
724
725 SCARG(uap, flags) = r;
726 ret = sys_open(p, (struct sys_open_args *)uap, retval);
727
728 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
729 struct filedesc *fdp = p->p_fd;
730 struct file *fp = fdp->fd_ofiles[*retval];
731
732 /* ignore any error, just give it a try */
733 if (fp->f_type == DTYPE_VNODE)
734 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
735 }
736 return ret;
737 }
738
739 #if defined (NFSSERVER)
740 int
741 sunos_sys_nfssvc(p, v, retval)
742 struct proc *p;
743 void *v;
744 register_t *retval;
745 {
746 #if 0
747 struct sunos_sys_nfssvc_args *uap = v;
748 struct emul *e = p->p_emul;
749 struct sys_nfssvc_args outuap;
750 struct sockaddr sa;
751 int error;
752
753 bzero(&outuap, sizeof outuap);
754 SCARG(&outuap, fd) = SCARG(uap, fd);
755 SCARG(&outuap, mskval) = STACKGAPBASE;
756 SCARG(&outuap, msklen) = sizeof sa;
757 SCARG(&outuap, mtchval) = SCARG(&outuap, mskval) + sizeof sa;
758 SCARG(&outuap, mtchlen) = sizeof sa;
759
760 bzero(&sa, sizeof sa);
761 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
762 return (error);
763 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
764 return (error);
765
766 return nfssvc(p, &outuap, retval);
767 #else
768 return (ENOSYS);
769 #endif
770 }
771 #endif /* NFSSERVER */
772
773 int
774 sunos_sys_ustat(p, v, retval)
775 struct proc *p;
776 void *v;
777 register_t *retval;
778 {
779 struct sunos_sys_ustat_args *uap = v;
780 struct sunos_ustat us;
781 int error;
782
783 bzero(&us, sizeof us);
784
785 /*
786 * XXX: should set f_tfree and f_tinode at least
787 * How do we translate dev -> fstat? (and then to sunos_ustat)
788 */
789
790 if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
791 return (error);
792 return 0;
793 }
794
795 int
796 sunos_sys_quotactl(p, v, retval)
797 struct proc *p;
798 void *v;
799 register_t *retval;
800 {
801
802 return EINVAL;
803 }
804
805 int
806 sunos_sys_vhangup(p, v, retval)
807 struct proc *p;
808 void *v;
809 register_t *retval;
810 {
811 struct session *sp = p->p_session;
812
813 if (sp->s_ttyvp == 0)
814 return 0;
815
816 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
817 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
818
819 (void) ttywait(sp->s_ttyp);
820 if (sp->s_ttyvp)
821 vgoneall(sp->s_ttyvp);
822 if (sp->s_ttyvp)
823 vrele(sp->s_ttyvp);
824 sp->s_ttyvp = NULL;
825
826 return 0;
827 }
828
829 static int
830 sunstatfs(sp, buf)
831 struct statfs *sp;
832 caddr_t buf;
833 {
834 struct sunos_statfs ssfs;
835
836 bzero(&ssfs, sizeof ssfs);
837 ssfs.f_type = 0;
838 ssfs.f_bsize = sp->f_bsize;
839 ssfs.f_blocks = sp->f_blocks;
840 ssfs.f_bfree = sp->f_bfree;
841 ssfs.f_bavail = sp->f_bavail;
842 ssfs.f_files = sp->f_files;
843 ssfs.f_ffree = sp->f_ffree;
844 ssfs.f_fsid = sp->f_fsid;
845 return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
846 }
847
848 int
849 sunos_sys_statfs(p, v, retval)
850 struct proc *p;
851 void *v;
852 register_t *retval;
853 {
854 struct sunos_sys_statfs_args *uap = v;
855 register struct mount *mp;
856 register struct statfs *sp;
857 int error;
858 struct nameidata nd;
859
860 caddr_t sg = stackgap_init(p->p_emul);
861 SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
862
863 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
864 if ((error = namei(&nd)) != 0)
865 return (error);
866 mp = nd.ni_vp->v_mount;
867 sp = &mp->mnt_stat;
868 vrele(nd.ni_vp);
869 if ((error = VFS_STATFS(mp, sp, p)) != 0)
870 return (error);
871 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
872 return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
873 }
874
875 int
876 sunos_sys_fstatfs(p, v, retval)
877 struct proc *p;
878 void *v;
879 register_t *retval;
880 {
881 struct sunos_sys_fstatfs_args *uap = v;
882 struct file *fp;
883 struct mount *mp;
884 register struct statfs *sp;
885 int error;
886
887 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
888 return (error);
889 mp = ((struct vnode *)fp->f_data)->v_mount;
890 sp = &mp->mnt_stat;
891 if ((error = VFS_STATFS(mp, sp, p)) != 0)
892 return (error);
893 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
894 return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
895 }
896
897 int
898 sunos_sys_exportfs(p, v, retval)
899 struct proc *p;
900 void *v;
901 register_t *retval;
902 {
903 /*
904 * XXX: should perhaps translate into a mount(2)
905 * with MOUNT_EXPORT?
906 */
907 return 0;
908 }
909
910 int
911 sunos_sys_mknod(p, v, retval)
912 struct proc *p;
913 void *v;
914 register_t *retval;
915 {
916 struct sunos_sys_mknod_args *uap = v;
917
918 caddr_t sg = stackgap_init(p->p_emul);
919 SUNOS_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
920
921 if (S_ISFIFO(SCARG(uap, mode)))
922 return sys_mkfifo(p, uap, retval);
923
924 return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
925 }
926
927 #define SUNOS_SC_ARG_MAX 1
928 #define SUNOS_SC_CHILD_MAX 2
929 #define SUNOS_SC_CLK_TCK 3
930 #define SUNOS_SC_NGROUPS_MAX 4
931 #define SUNOS_SC_OPEN_MAX 5
932 #define SUNOS_SC_JOB_CONTROL 6
933 #define SUNOS_SC_SAVED_IDS 7
934 #define SUNOS_SC_VERSION 8
935
936 int
937 sunos_sys_sysconf(p, v, retval)
938 struct proc *p;
939 void *v;
940 register_t *retval;
941 {
942 struct sunos_sys_sysconf_args *uap = v;
943 extern int maxfiles;
944
945 switch(SCARG(uap, name)) {
946 case SUNOS_SC_ARG_MAX:
947 *retval = ARG_MAX;
948 break;
949 case SUNOS_SC_CHILD_MAX:
950 *retval = maxproc;
951 break;
952 case SUNOS_SC_CLK_TCK:
953 *retval = 60; /* should this be `hz', ie. 100? */
954 break;
955 case SUNOS_SC_NGROUPS_MAX:
956 *retval = NGROUPS_MAX;
957 break;
958 case SUNOS_SC_OPEN_MAX:
959 *retval = maxfiles;
960 break;
961 case SUNOS_SC_JOB_CONTROL:
962 *retval = 1;
963 break;
964 case SUNOS_SC_SAVED_IDS:
965 #ifdef _POSIX_SAVED_IDS
966 *retval = 1;
967 #else
968 *retval = 0;
969 #endif
970 break;
971 case SUNOS_SC_VERSION:
972 *retval = 198808;
973 break;
974 default:
975 return EINVAL;
976 }
977 return 0;
978 }
979
980 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */
981 #define SUNOS_RLIM_NLIMITS 7
982
983 int
984 sunos_sys_getrlimit(p, v, retval)
985 struct proc *p;
986 void *v;
987 register_t *retval;
988 {
989 struct sunos_sys_getrlimit_args *uap = v;
990
991 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
992 return EINVAL;
993
994 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
995 SCARG(uap, which) = RLIMIT_NOFILE;
996
997 return compat_43_sys_getrlimit(p, uap, retval);
998 }
999
1000 int
1001 sunos_sys_setrlimit(p, v, retval)
1002 struct proc *p;
1003 void *v;
1004 register_t *retval;
1005 {
1006 struct sunos_sys_getrlimit_args *uap = v;
1007
1008 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1009 return EINVAL;
1010
1011 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1012 SCARG(uap, which) = RLIMIT_NOFILE;
1013
1014 return compat_43_sys_setrlimit(p, uap, retval);
1015 }
1016
1017 /* for the m68k machines */
1018 #ifndef PT_GETFPREGS
1019 #define PT_GETFPREGS -1
1020 #endif
1021 #ifndef PT_SETFPREGS
1022 #define PT_SETFPREGS -1
1023 #endif
1024
1025 static int sreq2breq[] = {
1026 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1,
1027 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE,
1028 PT_KILL, -1, PT_ATTACH, PT_DETACH,
1029 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS
1030 };
1031 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1032
1033 int
1034 sunos_sys_ptrace(p, v, retval)
1035 struct proc *p;
1036 void *v;
1037 register_t *retval;
1038 {
1039 struct sunos_sys_ptrace_args *uap = v;
1040 struct sys_ptrace_args pa;
1041 int req;
1042
1043 req = SCARG(uap, req);
1044
1045 if (req < 0 || req >= nreqs)
1046 return (EINVAL);
1047
1048 req = sreq2breq[req];
1049 if (req == -1)
1050 return (EINVAL);
1051
1052 SCARG(&pa, req) = req;
1053 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1054 SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1055 SCARG(&pa, data) = SCARG(uap, data);
1056
1057 return sys_ptrace(p, &pa, retval);
1058 }
1059
1060 static void
1061 sunos_pollscan(p, pl, nfd, retval)
1062 struct proc *p;
1063 struct sunos_pollfd *pl;
1064 int nfd;
1065 register_t *retval;
1066 {
1067 register struct filedesc *fdp = p->p_fd;
1068 register int msk, i;
1069 struct file *fp;
1070 int n = 0;
1071 static int flag[3] = { FREAD, FWRITE, 0 };
1072 static int pflag[3] = { SUNOS_POLLIN|SUNOS_POLLRDNORM,
1073 SUNOS_POLLOUT, SUNOS_POLLERR };
1074
1075 /*
1076 * XXX: We need to implement the rest of the flags.
1077 */
1078 for (i = 0; i < nfd; i++) {
1079 fp = fdp->fd_ofiles[pl[i].fd];
1080 if (fp == NULL) {
1081 if (pl[i].events & SUNOS_POLLNVAL) {
1082 pl[i].revents |= SUNOS_POLLNVAL;
1083 n++;
1084 }
1085 continue;
1086 }
1087 for (msk = 0; msk < 3; msk++) {
1088 if (pl[i].events & pflag[msk]) {
1089 if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
1090 pl[i].revents |=
1091 pflag[msk] & pl[i].events;
1092 n++;
1093 }
1094 }
1095 }
1096 }
1097 *retval = n;
1098 }
1099
1100
1101 /*
1102 * We are using the same mechanism as select only we encode/decode args
1103 * differently.
1104 */
1105 int
1106 sunos_sys_poll(p, v, retval)
1107 struct proc *p;
1108 void *v;
1109 register_t *retval;
1110 {
1111 struct sunos_sys_poll_args *uap = v;
1112 int i, s;
1113 int error, error2;
1114 size_t sz = sizeof(struct sunos_pollfd) * SCARG(uap, nfds);
1115 struct sunos_pollfd *pl;
1116 int msec = SCARG(uap, timeout);
1117 struct timeval atv;
1118 int timo;
1119 int ncoll;
1120 extern int nselcoll, selwait;
1121
1122 pl = (struct sunos_pollfd *) malloc(sz, M_TEMP, M_WAITOK);
1123
1124 if ((error = copyin(SCARG(uap, fds), pl, sz)) != 0)
1125 goto bad;
1126
1127 for (i = 0; i < SCARG(uap, nfds); i++)
1128 pl[i].revents = 0;
1129
1130 if (msec != -1) {
1131 atv.tv_sec = msec / 1000;
1132 atv.tv_usec = (msec - (atv.tv_sec * 1000)) * 1000;
1133
1134 if (itimerfix(&atv)) {
1135 error = EINVAL;
1136 goto done;
1137 }
1138 s = splclock();
1139 timeradd(&atv, &time, &atv);
1140 timo = hzto(&atv);
1141 /*
1142 * Avoid inadvertently sleeping forever.
1143 */
1144 if (timo == 0)
1145 timo = 1;
1146 splx(s);
1147 } else
1148 timo = 0;
1149
1150 retry:
1151 ncoll = nselcoll;
1152 p->p_flag |= P_SELECT;
1153 sunos_pollscan(p, pl, SCARG(uap, nfds), retval);
1154 if (*retval)
1155 goto done;
1156 s = splhigh();
1157 if (timo && timercmp(&time, &atv, >=)) {
1158 splx(s);
1159 goto done;
1160 }
1161 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
1162 splx(s);
1163 goto retry;
1164 }
1165 p->p_flag &= ~P_SELECT;
1166 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "sunos_poll", timo);
1167 splx(s);
1168 if (error == 0)
1169 goto retry;
1170
1171 done:
1172 p->p_flag &= ~P_SELECT;
1173 /* poll is not restarted after signals... */
1174 if (error == ERESTART)
1175 error = EINTR;
1176 if (error == EWOULDBLOCK)
1177 error = 0;
1178
1179 if ((error2 = copyout(pl, SCARG(uap, fds), sz)) != 0)
1180 error = error2;
1181
1182 bad:
1183 free((char *) pl, M_TEMP);
1184
1185 return (error);
1186 }
1187
1188 /*
1189 * SunOS reboot system call (for compatibility).
1190 * Sun lets you pass in a boot string which the PROM
1191 * saves and provides to the next boot program.
1192 */
1193 static struct sunos_howto_conv {
1194 int sun_howto;
1195 int bsd_howto;
1196 } sunos_howto_conv[] = {
1197 { 0x001, RB_ASKNAME },
1198 { 0x002, RB_SINGLE },
1199 { 0x004, RB_NOSYNC },
1200 { 0x008, RB_HALT },
1201 { 0x080, RB_DUMP },
1202 { 0x000, 0 },
1203 };
1204 #define SUNOS_RB_STRING 0x200
1205
1206 int
1207 sunos_sys_reboot(p, v, retval)
1208 struct proc *p;
1209 void *v;
1210 register_t *retval;
1211 {
1212 struct sunos_sys_reboot_args *uap = v;
1213 struct sunos_howto_conv *convp;
1214 int error, bsd_howto, sun_howto;
1215
1216 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
1217 return (error);
1218
1219 /*
1220 * Convert howto bits to BSD format.
1221 */
1222 sun_howto = SCARG(uap, howto);
1223 bsd_howto = 0;
1224 convp = sunos_howto_conv;
1225 while (convp->sun_howto) {
1226 if (sun_howto & convp->sun_howto)
1227 bsd_howto |= convp->bsd_howto;
1228 convp++;
1229 }
1230
1231 /*
1232 * Sun RB_STRING (Get user supplied bootstring.)
1233 * If the machine supports passing a string to the
1234 * next booted kernel.
1235 */
1236 if (sun_howto & SUNOS_RB_STRING) {
1237 char bs[128];
1238
1239 error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1240
1241 if (error) {
1242 #if 0
1243 return error;
1244 #endif
1245 bs = NULL:
1246 }
1247 } else
1248 bs = NULL;
1249
1250 boot(bsd_howto, bs);
1251
1252 return 0;
1253 }
1254
1255 /*
1256 * Generalized interface signal handler, 4.3-compatible.
1257 */
1258 /* ARGSUSED */
1259 int
1260 sunos_sys_sigvec(p, v, retval)
1261 struct proc *p;
1262 void *v;
1263 register_t *retval;
1264 {
1265 register struct sunos_sys_sigvec_args /* {
1266 syscallarg(int) signum;
1267 syscallarg(struct sigvec *) nsv;
1268 syscallarg(struct sigvec *) osv;
1269 } */ *uap = v;
1270 struct sigvec vec;
1271 register struct sigacts *ps = p->p_sigacts;
1272 register struct sigvec *sv;
1273 register int signum;
1274 int bit, error;
1275
1276 signum = SCARG(uap, signum);
1277 if (signum <= 0 || signum >= NSIG ||
1278 signum == SIGKILL || signum == SIGSTOP)
1279 return (EINVAL);
1280 sv = &vec;
1281 if (SCARG(uap, osv)) {
1282 *(sig_t *)&sv->sv_handler = ps->ps_sigact[signum];
1283 sv->sv_mask = ps->ps_catchmask[signum];
1284 bit = sigmask(signum);
1285 sv->sv_flags = 0;
1286 if ((ps->ps_sigonstack & bit) != 0)
1287 sv->sv_flags |= SV_ONSTACK;
1288 if ((ps->ps_sigintr & bit) != 0)
1289 sv->sv_flags |= SV_INTERRUPT;
1290 if ((ps->ps_sigreset & bit) != 0)
1291 sv->sv_flags |= SA_RESETHAND;
1292 sv->sv_mask &= ~bit;
1293 error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, osv),
1294 sizeof (vec));
1295 if (error)
1296 return (error);
1297 }
1298 if (SCARG(uap, nsv)) {
1299 error = copyin((caddr_t)SCARG(uap, nsv), (caddr_t)sv,
1300 sizeof (vec));
1301 if (error)
1302 return (error);
1303 /*
1304 * SunOS uses the mask 0x0004 as SV_RESETHAND
1305 * meaning: `reset to SIG_DFL on delivery'.
1306 * We support only the bits in: 0xF
1307 * (those bits are the same as ours)
1308 */
1309 if (sv->sv_flags & ~0xF)
1310 return (EINVAL);
1311 /* SunOS binaries have a user-mode trampoline. */
1312 sv->sv_flags |= SA_USERTRAMP;
1313 /* Convert sigvec:SV_INTERRUPT to sigaction:SA_RESTART */
1314 sv->sv_flags ^= SA_RESTART; /* same bit, inverted */
1315 setsigvec(p, signum, (struct sigaction *)sv);
1316 }
1317 return (0);
1318 }
1319