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