sunos_misc.c revision 1.114 1 /* $NetBSD: sunos_misc.c,v 1.114 2002/03/16 20:43:56 christos Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)sunos_misc.c 8.1 (Berkeley) 6/18/93
45 *
46 * Header: sunos_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
47 */
48
49 /*
50 * SunOS compatibility module.
51 *
52 * SunOS system calls that are implemented differently in BSD are
53 * handled here.
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: sunos_misc.c,v 1.114 2002/03/16 20:43:56 christos 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, 0);
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(p, &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, 0);
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, 0);
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, 0);
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, 0);
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, 0);
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, 0);
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, 0);
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(p, &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(p, &sg, sizeof(na));
362 na.version = NFS_ARGSVERSION;
363 na.addr = stackgap_alloc(p, &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 static __inline__ int sunos_sys_socket_common(struct proc *, register_t *,
736 int type);
737 static __inline__ int
738 sunos_sys_socket_common(p, retval, type)
739 struct proc *p;
740 register_t *retval;
741 int type;
742 {
743 struct socket *so;
744 struct file *fp;
745 int error, fd;
746
747 /* getsock() will use the descriptor for us */
748 fd = (int)*retval;
749 if ((error = getsock(p->p_fd, fd, &fp)) == 0) {
750 so = (struct socket *)fp->f_data;
751 if (type == SOCK_DGRAM)
752 so->so_options |= SO_BROADCAST;
753 }
754 FILE_UNUSE(fp, p);
755 return (error);
756 }
757
758 int
759 sunos_sys_socket(p, v, retval)
760 struct proc *p;
761 void *v;
762 register_t *retval;
763 {
764 struct sunos_sys_socket_args /* {
765 syscallarg(int) domain;
766 syscallarg(int) type;
767 syscallarg(int) protocol;
768 } */ *uap = v;
769 int error;
770
771 error = sys_socket(p, v, retval);
772 if (error)
773 return (error);
774 return sunos_sys_socket_common(p, retval, SCARG(uap, type));
775 }
776
777 int
778 sunos_sys_socketpair(p, v, retval)
779 struct proc *p;
780 void *v;
781 register_t *retval;
782 {
783 struct sunos_sys_socketpair_args /* {
784 syscallarg(int) domain;
785 syscallarg(int) type;
786 syscallarg(int) protocol;
787 syscallarg(int *) rsv;
788 } */ *uap = v;
789 int error;
790
791 error = sys_socketpair(p, v, retval);
792 if (error)
793 return (error);
794 return sunos_sys_socket_common(p, retval, SCARG(uap, type));
795 }
796
797 /*
798 * XXX: This needs cleaning up.
799 */
800 int
801 sunos_sys_auditsys(p, v, retval)
802 struct proc *p;
803 void *v;
804 register_t *retval;
805 {
806 return 0;
807 }
808
809 int
810 sunos_sys_uname(p, v, retval)
811 struct proc *p;
812 void *v;
813 register_t *retval;
814 {
815 struct sunos_sys_uname_args *uap = v;
816 struct sunos_utsname sut;
817
818 memset(&sut, 0, sizeof(sut));
819
820 memcpy(sut.sysname, ostype, sizeof(sut.sysname) - 1);
821 memcpy(sut.nodename, hostname, sizeof(sut.nodename));
822 sut.nodename[sizeof(sut.nodename)-1] = '\0';
823 memcpy(sut.release, osrelease, sizeof(sut.release) - 1);
824 memcpy(sut.version, "1", sizeof(sut.version) - 1);
825 memcpy(sut.machine, machine, sizeof(sut.machine) - 1);
826
827 return copyout((caddr_t)&sut, (caddr_t)SCARG(uap, name),
828 sizeof(struct sunos_utsname));
829 }
830
831 int
832 sunos_sys_setpgrp(p, v, retval)
833 struct proc *p;
834 void *v;
835 register_t *retval;
836 {
837 struct sunos_sys_setpgrp_args *uap = v;
838
839 /*
840 * difference to our setpgid call is to include backwards
841 * compatibility to pre-setsid() binaries. Do setsid()
842 * instead of setpgid() in those cases where the process
843 * tries to create a new session the old way.
844 */
845 if (!SCARG(uap, pgid) &&
846 (!SCARG(uap, pid) || SCARG(uap, pid) == p->p_pid))
847 return sys_setsid(p, uap, retval);
848 else
849 return sys_setpgid(p, uap, retval);
850 }
851
852 int
853 sunos_sys_open(p, v, retval)
854 struct proc *p;
855 void *v;
856 register_t *retval;
857 {
858 struct sunos_sys_open_args *uap = v;
859 int l, r;
860 int noctty;
861 int ret;
862
863 caddr_t sg = stackgap_init(p, 0);
864
865 /* convert mode into NetBSD mode */
866 l = SCARG(uap, flags);
867 noctty = l & 0x8000;
868 r = (l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
869 r |= ((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
870 r |= ((l & 0x0080) ? O_SHLOCK : 0);
871 r |= ((l & 0x0100) ? O_EXLOCK : 0);
872 r |= ((l & 0x2000) ? O_FSYNC : 0);
873
874 if (r & O_CREAT)
875 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
876 else
877 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
878
879 SCARG(uap, flags) = r;
880 ret = sys_open(p, (struct sys_open_args *)uap, retval);
881
882 if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
883 struct filedesc *fdp = p->p_fd;
884 struct file *fp;
885
886 fp = fd_getfile(fdp, *retval);
887
888 /* ignore any error, just give it a try */
889 if (fp != NULL && fp->f_type == DTYPE_VNODE)
890 (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
891 }
892 return ret;
893 }
894
895 #if defined (NFSSERVER)
896 int
897 sunos_sys_nfssvc(p, v, retval)
898 struct proc *p;
899 void *v;
900 register_t *retval;
901 {
902 #if 0
903 struct sunos_sys_nfssvc_args *uap = v;
904 struct emul *e = p->p_emul;
905 struct sys_nfssvc_args outuap;
906 struct sockaddr sa;
907 int error;
908 caddr_t sg = stackgap_init(p, 0);
909
910 memset(&outuap, 0, sizeof outuap);
911 SCARG(&outuap, fd) = SCARG(uap, fd);
912 SCARG(&outuap, mskval) = stackgap_alloc(p, &sg, sizeof(sa));
913 SCARG(&outuap, msklen) = sizeof(sa);
914 SCARG(&outuap, mtchval) = stackgap_alloc(p, &sg, sizeof(sa));
915 SCARG(&outuap, mtchlen) = sizeof(sa);
916
917 memset(&sa, 0, sizeof sa);
918 if (error = copyout(&sa, SCARG(&outuap, mskval), SCARG(&outuap, msklen)))
919 return (error);
920 if (error = copyout(&sa, SCARG(&outuap, mtchval), SCARG(&outuap, mtchlen)))
921 return (error);
922
923 return nfssvc(p, &outuap, retval);
924 #else
925 return (ENOSYS);
926 #endif
927 }
928 #endif /* NFSSERVER */
929
930 int
931 sunos_sys_ustat(p, v, retval)
932 struct proc *p;
933 void *v;
934 register_t *retval;
935 {
936 struct sunos_sys_ustat_args *uap = v;
937 struct sunos_ustat us;
938 int error;
939
940 memset(&us, 0, sizeof us);
941
942 /*
943 * XXX: should set f_tfree and f_tinode at least
944 * How do we translate dev -> fstat? (and then to sunos_ustat)
945 */
946
947 if ((error = copyout(&us, SCARG(uap, buf), sizeof us)) != 0)
948 return (error);
949 return 0;
950 }
951
952 int
953 sunos_sys_quotactl(p, v, retval)
954 struct proc *p;
955 void *v;
956 register_t *retval;
957 {
958
959 return EINVAL;
960 }
961
962 int
963 sunos_sys_vhangup(p, v, retval)
964 struct proc *p;
965 void *v;
966 register_t *retval;
967 {
968 struct session *sp = p->p_session;
969
970 if (sp->s_ttyvp == 0)
971 return 0;
972
973 if (sp->s_ttyp && sp->s_ttyp->t_session == sp && sp->s_ttyp->t_pgrp)
974 pgsignal(sp->s_ttyp->t_pgrp, SIGHUP, 1);
975
976 (void) ttywait(sp->s_ttyp);
977 if (sp->s_ttyvp)
978 VOP_REVOKE(sp->s_ttyvp, REVOKEALL);
979 if (sp->s_ttyvp)
980 vrele(sp->s_ttyvp);
981 sp->s_ttyvp = NULL;
982
983 return 0;
984 }
985
986 static int
987 sunstatfs(sp, buf)
988 struct statfs *sp;
989 caddr_t buf;
990 {
991 struct sunos_statfs ssfs;
992
993 memset(&ssfs, 0, sizeof ssfs);
994 ssfs.f_type = 0;
995 ssfs.f_bsize = sp->f_bsize;
996 ssfs.f_blocks = sp->f_blocks;
997 ssfs.f_bfree = sp->f_bfree;
998 ssfs.f_bavail = sp->f_bavail;
999 ssfs.f_files = sp->f_files;
1000 ssfs.f_ffree = sp->f_ffree;
1001 ssfs.f_fsid = sp->f_fsid;
1002 return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
1003 }
1004
1005 int
1006 sunos_sys_statfs(p, v, retval)
1007 struct proc *p;
1008 void *v;
1009 register_t *retval;
1010 {
1011 struct sunos_sys_statfs_args *uap = v;
1012 struct mount *mp;
1013 struct statfs *sp;
1014 int error;
1015 struct nameidata nd;
1016
1017 caddr_t sg = stackgap_init(p, 0);
1018 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
1019
1020 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1021 if ((error = namei(&nd)) != 0)
1022 return (error);
1023 mp = nd.ni_vp->v_mount;
1024 sp = &mp->mnt_stat;
1025 vrele(nd.ni_vp);
1026 if ((error = VFS_STATFS(mp, sp, p)) != 0)
1027 return (error);
1028 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1029 return sunstatfs(sp, (caddr_t)SCARG(uap, buf));
1030 }
1031
1032 int
1033 sunos_sys_fstatfs(p, v, retval)
1034 struct proc *p;
1035 void *v;
1036 register_t *retval;
1037 {
1038 struct sunos_sys_fstatfs_args *uap = v;
1039 struct file *fp;
1040 struct mount *mp;
1041 struct statfs *sp;
1042 int error;
1043
1044 /* getvnode() will use the descriptor for us */
1045 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1046 return (error);
1047 mp = ((struct vnode *)fp->f_data)->v_mount;
1048 sp = &mp->mnt_stat;
1049 if ((error = VFS_STATFS(mp, sp, p)) != 0)
1050 goto out;
1051 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1052 error = sunstatfs(sp, (caddr_t)SCARG(uap, buf));
1053 out:
1054 FILE_UNUSE(fp, p);
1055 return (error);
1056 }
1057
1058 int
1059 sunos_sys_exportfs(p, v, retval)
1060 struct proc *p;
1061 void *v;
1062 register_t *retval;
1063 {
1064 /*
1065 * XXX: should perhaps translate into a mount(2)
1066 * with MOUNT_EXPORT?
1067 */
1068 return 0;
1069 }
1070
1071 int
1072 sunos_sys_mknod(p, v, retval)
1073 struct proc *p;
1074 void *v;
1075 register_t *retval;
1076 {
1077 struct sunos_sys_mknod_args *uap = v;
1078
1079 caddr_t sg = stackgap_init(p, 0);
1080 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
1081
1082 if (S_ISFIFO(SCARG(uap, mode)))
1083 return sys_mkfifo(p, uap, retval);
1084
1085 return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
1086 }
1087
1088 #define SUNOS_SC_ARG_MAX 1
1089 #define SUNOS_SC_CHILD_MAX 2
1090 #define SUNOS_SC_CLK_TCK 3
1091 #define SUNOS_SC_NGROUPS_MAX 4
1092 #define SUNOS_SC_OPEN_MAX 5
1093 #define SUNOS_SC_JOB_CONTROL 6
1094 #define SUNOS_SC_SAVED_IDS 7
1095 #define SUNOS_SC_VERSION 8
1096
1097 int
1098 sunos_sys_sysconf(p, v, retval)
1099 struct proc *p;
1100 void *v;
1101 register_t *retval;
1102 {
1103 struct sunos_sys_sysconf_args *uap = v;
1104 extern int maxfiles;
1105
1106 switch(SCARG(uap, name)) {
1107 case SUNOS_SC_ARG_MAX:
1108 *retval = ARG_MAX;
1109 break;
1110 case SUNOS_SC_CHILD_MAX:
1111 *retval = maxproc;
1112 break;
1113 case SUNOS_SC_CLK_TCK:
1114 *retval = 60; /* should this be `hz', ie. 100? */
1115 break;
1116 case SUNOS_SC_NGROUPS_MAX:
1117 *retval = NGROUPS_MAX;
1118 break;
1119 case SUNOS_SC_OPEN_MAX:
1120 *retval = maxfiles;
1121 break;
1122 case SUNOS_SC_JOB_CONTROL:
1123 *retval = 1;
1124 break;
1125 case SUNOS_SC_SAVED_IDS:
1126 #ifdef _POSIX_SAVED_IDS
1127 *retval = 1;
1128 #else
1129 *retval = 0;
1130 #endif
1131 break;
1132 case SUNOS_SC_VERSION:
1133 *retval = 198808;
1134 break;
1135 default:
1136 return EINVAL;
1137 }
1138 return 0;
1139 }
1140
1141 #define SUNOS_RLIMIT_NOFILE 6 /* Other RLIMIT_* are the same */
1142 #define SUNOS_RLIM_NLIMITS 7
1143
1144 int
1145 sunos_sys_getrlimit(p, v, retval)
1146 struct proc *p;
1147 void *v;
1148 register_t *retval;
1149 {
1150 struct sunos_sys_getrlimit_args *uap = v;
1151
1152 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1153 return EINVAL;
1154
1155 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1156 SCARG(uap, which) = RLIMIT_NOFILE;
1157
1158 return compat_43_sys_getrlimit(p, uap, retval);
1159 }
1160
1161 int
1162 sunos_sys_setrlimit(p, v, retval)
1163 struct proc *p;
1164 void *v;
1165 register_t *retval;
1166 {
1167 struct sunos_sys_getrlimit_args *uap = v;
1168
1169 if (SCARG(uap, which) >= SUNOS_RLIM_NLIMITS)
1170 return EINVAL;
1171
1172 if (SCARG(uap, which) == SUNOS_RLIMIT_NOFILE)
1173 SCARG(uap, which) = RLIMIT_NOFILE;
1174
1175 return compat_43_sys_setrlimit(p, uap, retval);
1176 }
1177
1178 /* for the m68k machines */
1179 #ifndef PT_GETFPREGS
1180 #define PT_GETFPREGS -1
1181 #endif
1182 #ifndef PT_SETFPREGS
1183 #define PT_SETFPREGS -1
1184 #endif
1185
1186 static int sreq2breq[] = {
1187 PT_TRACE_ME, PT_READ_I, PT_READ_D, -1,
1188 PT_WRITE_I, PT_WRITE_D, -1, PT_CONTINUE,
1189 PT_KILL, -1, PT_ATTACH, PT_DETACH,
1190 PT_GETREGS, PT_SETREGS, PT_GETFPREGS, PT_SETFPREGS
1191 };
1192 static int nreqs = sizeof(sreq2breq) / sizeof(sreq2breq[0]);
1193
1194 int
1195 sunos_sys_ptrace(p, v, retval)
1196 struct proc *p;
1197 void *v;
1198 register_t *retval;
1199 {
1200 struct sunos_sys_ptrace_args *uap = v;
1201 struct sys_ptrace_args pa;
1202 int req;
1203
1204 req = SCARG(uap, req);
1205
1206 if (req < 0 || req >= nreqs)
1207 return (EINVAL);
1208
1209 req = sreq2breq[req];
1210 if (req == -1)
1211 return (EINVAL);
1212
1213 SCARG(&pa, req) = req;
1214 SCARG(&pa, pid) = (pid_t)SCARG(uap, pid);
1215 SCARG(&pa, addr) = (caddr_t)SCARG(uap, addr);
1216 SCARG(&pa, data) = SCARG(uap, data);
1217
1218 return sys_ptrace(p, &pa, retval);
1219 }
1220
1221 /*
1222 * SunOS reboot system call (for compatibility).
1223 * Sun lets you pass in a boot string which the PROM
1224 * saves and provides to the next boot program.
1225 */
1226
1227 #define SUNOS_RB_ASKNAME 0x001
1228 #define SUNOS_RB_SINGLE 0x002
1229 #define SUNOS_RB_NOSYNC 0x004
1230 #define SUNOS_RB_HALT 0x008
1231 #define SUNOS_RB_DUMP 0x080
1232 #define SUNOS_RB_STRING 0x200
1233
1234 static struct sunos_howto_conv {
1235 int sun_howto;
1236 int bsd_howto;
1237 } sunos_howto_conv[] = {
1238 { SUNOS_RB_ASKNAME, RB_ASKNAME },
1239 { SUNOS_RB_SINGLE, RB_SINGLE },
1240 { SUNOS_RB_NOSYNC, RB_NOSYNC },
1241 { SUNOS_RB_HALT, RB_HALT },
1242 { SUNOS_RB_DUMP, RB_DUMP },
1243 { SUNOS_RB_STRING, RB_STRING },
1244 { 0x000, 0 },
1245 };
1246
1247 int
1248 sunos_sys_reboot(p, v, retval)
1249 struct proc *p;
1250 void *v;
1251 register_t *retval;
1252 {
1253 struct sunos_sys_reboot_args *uap = v;
1254 struct sys_reboot_args ua;
1255 struct sunos_howto_conv *convp;
1256 int error, bsd_howto, sun_howto;
1257 char *bootstr;
1258
1259 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1260 return (error);
1261
1262 /*
1263 * Convert howto bits to BSD format.
1264 */
1265 sun_howto = SCARG(uap, howto);
1266 bsd_howto = 0;
1267 convp = sunos_howto_conv;
1268 while (convp->sun_howto) {
1269 if (sun_howto & convp->sun_howto)
1270 bsd_howto |= convp->bsd_howto;
1271 convp++;
1272 }
1273
1274 /*
1275 * Sun RB_STRING (Get user supplied bootstring.)
1276 * If the machine supports passing a string to the
1277 * next booted kernel.
1278 */
1279 if (sun_howto & SUNOS_RB_STRING) {
1280 char bs[128];
1281
1282 error = copyinstr(SCARG(uap, bootstr), bs, sizeof(bs), 0);
1283
1284 if (error)
1285 bootstr = NULL;
1286 else
1287 bootstr = bs;
1288 } else
1289 bootstr = NULL;
1290
1291 SCARG(&ua, opt) = bsd_howto;
1292 SCARG(&ua, bootstr) = bootstr;
1293 sys_reboot(p, &ua, retval);
1294 return(0);
1295 }
1296
1297 /*
1298 * Generalized interface signal handler, 4.3-compatible.
1299 */
1300 /* ARGSUSED */
1301 int
1302 sunos_sys_sigvec(p, v, retval)
1303 struct proc *p;
1304 void *v;
1305 register_t *retval;
1306 {
1307 struct sunos_sys_sigvec_args /* {
1308 syscallarg(int) signum;
1309 syscallarg(struct sigvec *) nsv;
1310 syscallarg(struct sigvec *) osv;
1311 } */ *uap = v;
1312 struct sigvec nsv, osv;
1313 struct sigaction nsa, osa;
1314 int error;
1315 /*XXX*/extern void compat_43_sigvec_to_sigaction
1316 __P((const struct sigvec *, struct sigaction *));
1317 /*XXX*/extern void compat_43_sigaction_to_sigvec
1318 __P((const struct sigaction *, struct sigvec *));
1319
1320 if (SCARG(uap, nsv)) {
1321 error = copyin(SCARG(uap, nsv), &nsv, sizeof(nsv));
1322 if (error != 0)
1323 return (error);
1324
1325 /*
1326 * SunOS uses the mask 0x0004 as SV_RESETHAND
1327 * meaning: `reset to SIG_DFL on delivery'.
1328 * We support only the bits in: 0xF
1329 * (those bits are the same as ours)
1330 */
1331 if (nsv.sv_flags & ~0xF)
1332 return (EINVAL);
1333
1334 compat_43_sigvec_to_sigaction(&nsv, &nsa);
1335 }
1336 error = sigaction1(p, SCARG(uap, signum),
1337 SCARG(uap, nsv) ? &nsa : 0,
1338 SCARG(uap, osv) ? &osa : 0);
1339 if (error != 0)
1340 return (error);
1341
1342 if (SCARG(uap, osv)) {
1343 compat_43_sigaction_to_sigvec(&osa, &osv);
1344 error = copyout(&osv, SCARG(uap, osv), sizeof(osv));
1345 if (error != 0)
1346 return (error);
1347 }
1348
1349 return (0);
1350 }
1351