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