sunos_misc.c revision 1.6 1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)sun_misc.c 8.1 (Berkeley) 6/18/93
43 *
44 * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
45 * $Id: sunos_misc.c,v 1.6 1993/11/10 11:46:38 deraadt Exp $
46 */
47
48 /*
49 * SunOS compatibility module.
50 *
51 * SunOS system calls that are implemented differently in BSD are
52 * handled here.
53 */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <ufs/dir.h>
58 #include <sys/proc.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/ioctl.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/mman.h>
65 #include <sys/mount.h>
66 #include <sys/resource.h>
67 #include <sys/resourcevar.h>
68 #include <sys/signal.h>
69 #include <sys/signalvar.h>
70 #include <sys/socket.h>
71 #include <sys/vnode.h>
72 #include <sys/uio.h>
73 #include <sys/wait.h>
74
75 #include <sys/specdev.h>
76
77 #include <vm/vm.h>
78
79 struct sun_wait4_args {
80 int pid;
81 int *status;
82 int options;
83 struct rusage *rusage;
84 };
85 sun_wait4(p, uap, retval)
86 struct proc *p;
87 struct sun_wait4_args *uap;
88 int *retval;
89 {
90
91 if (uap->pid == 0)
92 uap->pid = WAIT_ANY;
93 return (wait4(p, uap, retval));
94 }
95
96 struct sun_creat_args {
97 char *fname;
98 int fmode;
99 };
100 sun_creat(p, uap, retval)
101 struct proc *p;
102 struct sun_creat_args *uap;
103 int *retval;
104 {
105 struct args {
106 char *fname;
107 int mode;
108 int crtmode;
109 } openuap;
110
111 openuap.fname = uap->fname;
112 openuap.crtmode = uap->fmode;
113 openuap.mode = O_WRONLY | O_CREAT | O_TRUNC;
114 return (open(p, &openuap, retval));
115 }
116
117 struct sun_execv_args {
118 char *fname;
119 char **argp;
120 char **envp; /* pseudo */
121 };
122 sun_execv(p, uap, retval)
123 struct proc *p;
124 struct sun_execv_args *uap;
125 int *retval;
126 {
127
128 uap->envp = NULL;
129 return (execve(p, uap, retval));
130 }
131
132 struct sun_omsync_args {
133 caddr_t addr;
134 int len;
135 int flags;
136 };
137 sun_omsync(p, uap, retval)
138 struct proc *p;
139 struct sun_omsync_args *uap;
140 int *retval;
141 {
142
143 if (uap->flags)
144 return (EINVAL);
145 return (msync(p, uap, retval));
146 }
147
148 struct sun_unmount_args {
149 char *name;
150 int flags; /* pseudo */
151 };
152 sun_unmount(p, uap, retval)
153 struct proc *p;
154 struct sun_unmount_args *uap;
155 int *retval;
156 {
157
158 uap->flags = 0;
159 return (unmount(p, uap, retval));
160 }
161
162 static int
163 gettype(tptr)
164 int *tptr;
165 {
166 int type, error;
167 char in[20];
168
169 if (error = copyinstr((caddr_t)*tptr, in, sizeof in, (u_int *)0))
170 return (error);
171 if (strcmp(in, "4.2") == 0 || strcmp(in, "ufs") == 0)
172 type = MOUNT_UFS;
173 else if (strcmp(in, "nfs") == 0)
174 type = MOUNT_NFS;
175 else
176 return (EINVAL);
177 *tptr = type;
178 return (0);
179 }
180
181 #define SUNM_RDONLY 0x01 /* mount fs read-only */
182 #define SUNM_NOSUID 0x02 /* mount fs with setuid disallowed */
183 #define SUNM_NEWTYPE 0x04 /* type is string (char *), not int */
184 #define SUNM_GRPID 0x08 /* (bsd semantics; ignored) */
185 #define SUNM_REMOUNT 0x10 /* update existing mount */
186 #define SUNM_NOSUB 0x20 /* prevent submounts (rejected) */
187 #define SUNM_MULTI 0x40 /* (ignored) */
188 #define SUNM_SYS5 0x80 /* Sys 5-specific semantics (rejected) */
189
190 struct sun_mount_args {
191 int type;
192 char *dir;
193 int flags;
194 caddr_t data;
195 };
196 sun_mount(p, uap, retval)
197 struct proc *p;
198 struct sun_mount_args *uap;
199 int *retval;
200 {
201 int oflags = uap->flags, nflags, error;
202
203 if (oflags & (SUNM_NOSUB | SUNM_SYS5))
204 return (EINVAL);
205 if (oflags & SUNM_NEWTYPE && (error = gettype(&uap->type)))
206 return (error);
207 nflags = 0;
208 if (oflags & SUNM_RDONLY)
209 nflags |= MNT_RDONLY;
210 if (oflags & SUNM_NOSUID)
211 nflags |= MNT_NOSUID;
212 if (oflags & SUNM_REMOUNT)
213 nflags |= MNT_UPDATE;
214 uap->flags = nflags;
215 return (mount(p, uap, retval));
216 }
217
218 struct sun_sigpending_args {
219 int *mask;
220 };
221 sun_sigpending(p, uap, retval)
222 struct proc *p;
223 struct sun_sigpending_args *uap;
224 int *retval;
225 {
226 int mask = p->p_sig & p->p_sigmask;
227
228 return (copyout((caddr_t)&mask, (caddr_t)uap->mask, sizeof(int)));
229 }
230
231 /* TDR: Temporary until sys/dir.h, include/dirent.h and sys/dirent.h are fixed */
232 struct dirent {
233 u_long d_fileno; /* file number of entry */
234 u_short d_reclen; /* length of this record */
235 u_short d_namlen; /* length of string in d_name */
236 char d_name[255 + 1]; /* name must be no longer than this */
237 };
238
239 /*
240 * Here is the sun layout. (Compare the BSD layout in <sys/dirent.h>.)
241 * We can assume big-endian, so the BSD d_type field is just the high
242 * byte of the SunOS d_namlen field, after adjusting for the extra "long".
243 */
244 struct sun_dirent {
245 long d_off;
246 u_long d_fileno;
247 u_short d_reclen;
248 u_short d_namlen;
249 char d_name[256];
250 };
251
252 /*
253 * Read Sun-style directory entries. We suck them into kernel space so
254 * that they can be massaged before being copied out to user code. Like
255 * SunOS, we squish out `empty' entries.
256 *
257 * This is quite ugly, but what do you expect from compatibility code?
258 */
259 struct sun_getdents_args {
260 int fd;
261 char *buf;
262 int nbytes;
263 };
264 sun_getdents(p, uap, retval)
265 struct proc *p;
266 register struct sun_getdents_args *uap;
267 int *retval;
268 {
269 register struct vnode *vp;
270 register caddr_t inp, buf; /* BSD-format */
271 register int len, reclen; /* BSD-format */
272 register caddr_t outp; /* Sun-format */
273 register int resid; /* Sun-format */
274 struct file *fp;
275 struct uio auio;
276 struct iovec aiov;
277 off_t off; /* true file offset */
278 long soff; /* Sun file offset */
279 int buflen, error, eofflag;
280 #define BSD_DIRENT(cp) ((struct dirent *)(cp))
281 #define SUN_RECLEN(reclen) (reclen + sizeof(long))
282
283 if ((error = getvnode(p->p_fd, uap->fd, &fp)) != 0)
284 return (error);
285 if ((fp->f_flag & FREAD) == 0)
286 return (EBADF);
287 vp = (struct vnode *)fp->f_data;
288 if (vp->v_type != VDIR) /* XXX vnode readdir op should do this */
289 return (EINVAL);
290 buflen = min(MAXBSIZE, uap->nbytes);
291 buf = malloc(buflen, M_TEMP, M_WAITOK);
292 VOP_LOCK(vp);
293 off = fp->f_offset;
294 again:
295 aiov.iov_base = buf;
296 aiov.iov_len = buflen;
297 auio.uio_iov = &aiov;
298 auio.uio_iovcnt = 1;
299 auio.uio_rw = UIO_READ;
300 auio.uio_segflg = UIO_SYSSPACE;
301 auio.uio_procp = p;
302 auio.uio_resid = buflen;
303 auio.uio_offset = off;
304 /*
305 * First we read into the malloc'ed buffer, then
306 * we massage it into user space, one record at a time.
307 */
308 if (error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, 0))
309 goto out;
310 inp = buf;
311 outp = uap->buf;
312 resid = uap->nbytes;
313 if ((len = buflen - auio.uio_resid) == 0)
314 goto eof;
315 for (; len > 0; len -= reclen) {
316 reclen = ((struct dirent *)inp)->d_reclen;
317 if (reclen & 3)
318 panic("sun_getdents");
319 off += reclen; /* each entry points to next */
320 if (BSD_DIRENT(inp)->d_fileno == 0) {
321 inp += reclen; /* it is a hole; squish it out */
322 continue;
323 }
324 if (reclen > len || resid < SUN_RECLEN(reclen)) {
325 /* entry too big for buffer, so just stop */
326 outp++;
327 break;
328 }
329 /*
330 * Massage in place to make a Sun-shaped dirent (otherwise
331 * we have to worry about touching user memory outside of
332 * the copyout() call).
333 */
334 BSD_DIRENT(inp)->d_reclen = SUN_RECLEN(reclen);
335 #if notdef
336 BSD_DIRENT(inp)->d_type = 0; /* 4.4 specific */
337 #endif
338 soff = off;
339 if ((error = copyout((caddr_t)&soff, outp, sizeof soff)) != 0 ||
340 (error = copyout(inp, outp + sizeof soff, reclen)) != 0)
341 goto out;
342 /* advance past this real entry */
343 inp += reclen;
344 /* advance output past Sun-shaped entry */
345 outp += SUN_RECLEN(reclen);
346 resid -= SUN_RECLEN(reclen);
347 }
348 /* if we squished out the whole block, try again */
349 if (outp == uap->buf)
350 goto again;
351 fp->f_offset = off; /* update the vnode offset */
352 eof:
353 *retval = uap->nbytes - resid;
354 out:
355 VOP_UNLOCK(vp);
356 free(buf, M_TEMP);
357 return (error);
358 }
359
360 /*
361 * The Sun bit-style arguments are not in the same order as the
362 * NetBSD ones. We must remap them the bits.
363 */
364
365 #if defined(sparc)
366 #define DEVZERO makedev(3, 12) /* major,minor of /dev/zero */
367 #endif
368 #if defined(sun3) || defined(amiga) || defined(mac) || defined(hp300)
369 #define DEVZERO makedev(2, 12) /* major,minor of /dev/zero */
370 #endif
371
372 #define SUN_PROT_READ 1
373 #define SUN_PROT_WRITE 2
374 #define SUN_PROT_EXEC 4
375 #define SUN_PROT_UALL (SUN_PROT_READ | SUN_PROT_WRITE | SUN_PROT_EXEC)
376
377 #define SUN__MAP_NEW 0x80000000 /* if not, old mmap & cannot handle */
378
379 #define SUN_MAP_SHARED 1
380 #define SUN_MAP_PRIVATE 2
381 #define SUN_MAP_TYPE 0xf
382 #define SUN_MAP_FIXED 0x10
383
384 struct sun_mmap_args {
385 caddr_t addr;
386 size_t len;
387 int prot;
388 int flags;
389 int fd;
390 off_t off;
391 };
392 sun_mmap(p, uap, retval)
393 register struct proc *p;
394 register struct sun_mmap_args *uap;
395 int *retval;
396 {
397 register int flags, prot, newflags, newprot;
398 register struct filedesc *fdp;
399 register struct file *fp;
400 register struct vnode *vp;
401
402 /*
403 * Verify and re-map the arguments.
404 */
405 prot = uap->prot;
406 newprot = 0;
407 if (uap->prot & ~SUN_PROT_UALL)
408 return (EINVAL);
409 if (uap->prot & SUN_PROT_READ)
410 newprot |= PROT_READ;
411 if (uap->prot & SUN_PROT_WRITE)
412 newprot |= PROT_WRITE;
413 if (uap->prot & SUN_PROT_EXEC)
414 newprot |= PROT_EXEC;
415
416 flags = uap->flags;
417 newflags = 0;
418 if ((flags & SUN__MAP_NEW) == 0)
419 return (EINVAL);
420
421 switch (flags & SUN_MAP_TYPE) {
422 case SUN_MAP_SHARED:
423 newflags |= MAP_SHARED;
424 break;
425 case SUN_MAP_PRIVATE:
426 newflags |= MAP_PRIVATE;
427 break;
428 default:
429 return (EINVAL);
430 }
431
432 if (flags & SUN_MAP_FIXED)
433 newflags |= MAP_FIXED;
434
435 /*
436 * Special case: if fd refers to /dev/zero, map as MAP_ANON. (XXX)
437 */
438 fdp = p->p_fd;
439 if ((unsigned)uap->fd < fdp->fd_nfiles && /*XXX*/
440 (fp = fdp->fd_ofiles[uap->fd]) != NULL && /*XXX*/
441 fp->f_type == DTYPE_VNODE && /*XXX*/
442 (vp = (struct vnode *)fp->f_data)->v_type == VCHR && /*XXX*/
443 vp->v_rdev == DEVZERO) { /*XXX*/
444 newflags |= MAP_ANON;
445 uap->fd = -1;
446 } else
447 newflags |= MAP_FILE;
448
449 /* All done, fix up fields and go. */
450 uap->flags = newflags;
451 uap->prot = newprot;
452 return (smmap(p, uap, retval));
453 }
454
455 #define MC_SYNC 1
456 #define MC_LOCK 2
457 #define MC_UNLOCK 3
458 #define MC_ADVISE 4
459 #define MC_LOCKAS 5
460 #define MC_UNLOCKAS 6
461
462 struct sun_mctl_args {
463 caddr_t addr;
464 size_t len;
465 int func;
466 void *arg;
467 };
468 sun_mctl(p, uap, retval)
469 register struct proc *p;
470 register struct sun_mctl_args *uap;
471 int *retval;
472 {
473
474 switch (uap->func) {
475
476 case MC_ADVISE: /* ignore for now */
477 return (0);
478
479 case MC_SYNC: /* translate to msync */
480 return (msync(p, uap, retval));
481
482 default:
483 return (EINVAL);
484 }
485 }
486
487 struct sun_setsockopt_args {
488 int s;
489 int level;
490 int name;
491 caddr_t val;
492 int valsize;
493 };
494 sun_setsockopt(p, uap, retval)
495 struct proc *p;
496 register struct sun_setsockopt_args *uap;
497 int *retval;
498 {
499 struct file *fp;
500 struct mbuf *m = NULL;
501 int error;
502
503 if (error = getsock(p->p_fd, uap->s, &fp))
504 return (error);
505 #define SO_DONTLINGER (~SO_LINGER)
506 if (uap->name == SO_DONTLINGER) {
507 m = m_get(M_WAIT, MT_SOOPTS);
508 if (m == NULL)
509 return (ENOBUFS);
510 mtod(m, struct linger *)->l_onoff = 0;
511 m->m_len = sizeof(struct linger);
512 return (sosetopt((struct socket *)fp->f_data, uap->level,
513 SO_LINGER, m));
514 }
515 if (uap->valsize > MLEN)
516 return (EINVAL);
517 if (uap->val) {
518 m = m_get(M_WAIT, MT_SOOPTS);
519 if (m == NULL)
520 return (ENOBUFS);
521 if (error = copyin(uap->val, mtod(m, caddr_t),
522 (u_int)uap->valsize)) {
523 (void) m_free(m);
524 return (error);
525 }
526 m->m_len = uap->valsize;
527 }
528 return (sosetopt((struct socket *)fp->f_data, uap->level,
529 uap->name, m));
530 }
531
532 struct sun_fchroot_args {
533 int fdes;
534 };
535 sun_fchroot(p, uap, retval)
536 register struct proc *p;
537 register struct sun_fchroot_args *uap;
538 int *retval;
539 {
540 register struct filedesc *fdp = p->p_fd;
541 register struct vnode *vp;
542 struct file *fp;
543 int error;
544
545 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
546 return (error);
547 if ((error = getvnode(fdp, uap->fdes, &fp)) != 0)
548 return (error);
549 vp = (struct vnode *)fp->f_data;
550 VOP_LOCK(vp);
551 if (vp->v_type != VDIR)
552 error = ENOTDIR;
553 else
554 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
555 VOP_UNLOCK(vp);
556 if (error)
557 return (error);
558 VREF(vp);
559 if (fdp->fd_rdir != NULL)
560 vrele(fdp->fd_rdir);
561 fdp->fd_rdir = vp;
562 return (0);
563 }
564
565 /*
566 * XXX: This needs cleaning up.
567 */
568 auditsys(...)
569 {
570 return 0;
571 }
572