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