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