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