Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / kern / sys_generic.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)sys_generic.c       8.5 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
40  * $DragonFly: src/sys/kern/sys_generic.c,v 1.46 2007/08/15 03:15:06 dillon Exp $
41  */
42
43 #include "opt_ktrace.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
51 #include <sys/file.h>
52 #include <sys/proc.h>
53 #include <sys/signalvar.h>
54 #include <sys/socketvar.h>
55 #include <sys/uio.h>
56 #include <sys/kernel.h>
57 #include <sys/kern_syscall.h>
58 #include <sys/malloc.h>
59 #include <sys/mapped_ioctl.h>
60 #include <sys/poll.h>
61 #include <sys/queue.h>
62 #include <sys/resourcevar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/buf.h>
66 #ifdef KTRACE
67 #include <sys/ktrace.h>
68 #endif
69 #include <vm/vm.h>
70 #include <vm/vm_page.h>
71 #include <sys/file2.h>
72
73 #include <machine/limits.h>
74
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
79
80 static int      pollscan (struct proc *, struct pollfd *, u_int, int *);
81 static int      selscan (struct proc *, fd_mask **, fd_mask **,
82                         int, int *);
83 static int      dofileread(int, struct file *, struct uio *, int, int *);
84 static int      dofilewrite(int, struct file *, struct uio *, int, int *);
85
86 /*
87  * Read system call.
88  *
89  * MPSAFE
90  */
91 int
92 sys_read(struct read_args *uap)
93 {
94         struct thread *td = curthread;
95         struct uio auio;
96         struct iovec aiov;
97         int error;
98
99         aiov.iov_base = uap->buf;
100         aiov.iov_len = uap->nbyte;
101         auio.uio_iov = &aiov;
102         auio.uio_iovcnt = 1;
103         auio.uio_offset = -1;
104         auio.uio_resid = uap->nbyte;
105         auio.uio_rw = UIO_READ;
106         auio.uio_segflg = UIO_USERSPACE;
107         auio.uio_td = td;
108
109         if (auio.uio_resid < 0)
110                 error = EINVAL;
111         else
112                 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
113         return(error);
114 }
115
116 /*
117  * Positioned (Pread) read system call
118  *
119  * MPSAFE
120  */
121 int
122 sys_extpread(struct extpread_args *uap)
123 {
124         struct thread *td = curthread;
125         struct uio auio;
126         struct iovec aiov;
127         int error;
128         int flags;
129
130         aiov.iov_base = uap->buf;
131         aiov.iov_len = uap->nbyte;
132         auio.uio_iov = &aiov;
133         auio.uio_iovcnt = 1;
134         auio.uio_offset = uap->offset;
135         auio.uio_resid = uap->nbyte;
136         auio.uio_rw = UIO_READ;
137         auio.uio_segflg = UIO_USERSPACE;
138         auio.uio_td = td;
139
140         flags = uap->flags & O_FMASK;
141         if (uap->offset != (off_t)-1)
142                 flags |= O_FOFFSET;
143
144         if (auio.uio_resid < 0)
145                 error = EINVAL;
146         else
147                 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
148         return(error);
149 }
150
151 /*
152  * Scatter read system call.
153  *
154  * MPSAFE
155  */
156 int
157 sys_readv(struct readv_args *uap)
158 {
159         struct thread *td = curthread;
160         struct uio auio;
161         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
162         int error;
163
164         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
165                              &auio.uio_resid);
166         if (error)
167                 return (error);
168         auio.uio_iov = iov;
169         auio.uio_iovcnt = uap->iovcnt;
170         auio.uio_offset = -1;
171         auio.uio_rw = UIO_READ;
172         auio.uio_segflg = UIO_USERSPACE;
173         auio.uio_td = td;
174
175         error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
176
177         iovec_free(&iov, aiov);
178         return (error);
179 }
180
181
182 /*
183  * Scatter positioned read system call.
184  *
185  * MPSAFE
186  */
187 int
188 sys_extpreadv(struct extpreadv_args *uap)
189 {
190         struct thread *td = curthread;
191         struct uio auio;
192         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
193         int error;
194         int flags;
195
196         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
197                              &auio.uio_resid);
198         if (error)
199                 return (error);
200         auio.uio_iov = iov;
201         auio.uio_iovcnt = uap->iovcnt;
202         auio.uio_offset = uap->offset;
203         auio.uio_rw = UIO_READ;
204         auio.uio_segflg = UIO_USERSPACE;
205         auio.uio_td = td;
206
207         flags = uap->flags & O_FMASK;
208         if (uap->offset != (off_t)-1)
209                 flags |= O_FOFFSET;
210
211         error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_result);
212
213         iovec_free(&iov, aiov);
214         return(error);
215 }
216
217 /*
218  * MPSAFE
219  */
220 int
221 kern_preadv(int fd, struct uio *auio, int flags, int *res)
222 {
223         struct thread *td = curthread;
224         struct proc *p = td->td_proc;
225         struct file *fp;
226         int error;
227
228         KKASSERT(p);
229
230         fp = holdfp(p->p_fd, fd, FREAD);
231         if (fp == NULL)
232                 return (EBADF);
233         if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
234                 error = ESPIPE;
235         } else if (auio->uio_resid < 0) {
236                 error = EINVAL;
237         } else {
238                 error = dofileread(fd, fp, auio, flags, res);
239         }
240         fdrop(fp);
241         return(error);
242 }
243
244 /*
245  * Common code for readv and preadv that reads data in
246  * from a file using the passed in uio, offset, and flags.
247  *
248  * MPALMOSTSAFE - ktrace needs help
249  */
250 static int
251 dofileread(int fd, struct file *fp, struct uio *auio, int flags, int *res)
252 {
253         struct thread *td = curthread;
254         struct proc *p = td->td_proc;
255         int error;
256         int len;
257 #ifdef KTRACE
258         struct iovec *ktriov = NULL;
259         struct uio ktruio;
260 #endif
261
262 #ifdef KTRACE
263         /*
264          * if tracing, save a copy of iovec
265          */
266         if (KTRPOINT(td, KTR_GENIO))  {
267                 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
268
269                 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
270                 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
271                 ktruio = *auio;
272         }
273 #endif
274         len = auio->uio_resid;
275         error = fo_read(fp, auio, fp->f_cred, flags);
276         if (error) {
277                 if (auio->uio_resid != len && (error == ERESTART ||
278                     error == EINTR || error == EWOULDBLOCK))
279                         error = 0;
280         }
281 #ifdef KTRACE
282         if (ktriov != NULL) {
283                 if (error == 0) {
284                         ktruio.uio_iov = ktriov;
285                         ktruio.uio_resid = len - auio->uio_resid;
286                         get_mplock();
287                         ktrgenio(p, fd, UIO_READ, &ktruio, error);
288                         rel_mplock();
289                 }
290                 FREE(ktriov, M_TEMP);
291         }
292 #endif
293         if (error == 0)
294                 *res = len - auio->uio_resid;
295
296         return(error);
297 }
298
299 /*
300  * Write system call
301  *
302  * MPSAFE
303  */
304 int
305 sys_write(struct write_args *uap)
306 {
307         struct thread *td = curthread;
308         struct uio auio;
309         struct iovec aiov;
310         int error;
311
312         aiov.iov_base = (void *)(uintptr_t)uap->buf;
313         aiov.iov_len = uap->nbyte;
314         auio.uio_iov = &aiov;
315         auio.uio_iovcnt = 1;
316         auio.uio_offset = -1;
317         auio.uio_resid = uap->nbyte;
318         auio.uio_rw = UIO_WRITE;
319         auio.uio_segflg = UIO_USERSPACE;
320         auio.uio_td = td;
321
322         if (auio.uio_resid < 0)
323                 error = EINVAL;
324         else
325                 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
326
327         return(error);
328 }
329
330 /*
331  * Pwrite system call
332  *
333  * MPSAFE
334  */
335 int
336 sys_extpwrite(struct extpwrite_args *uap)
337 {
338         struct thread *td = curthread;
339         struct uio auio;
340         struct iovec aiov;
341         int error;
342         int flags;
343
344         aiov.iov_base = (void *)(uintptr_t)uap->buf;
345         aiov.iov_len = uap->nbyte;
346         auio.uio_iov = &aiov;
347         auio.uio_iovcnt = 1;
348         auio.uio_offset = uap->offset;
349         auio.uio_resid = uap->nbyte;
350         auio.uio_rw = UIO_WRITE;
351         auio.uio_segflg = UIO_USERSPACE;
352         auio.uio_td = td;
353
354         flags = uap->flags & O_FMASK;
355         if (uap->offset != (off_t)-1)
356                 flags |= O_FOFFSET;
357
358         if (auio.uio_resid < 0)
359                 error = EINVAL;
360         else
361                 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
362
363         return(error);
364 }
365
366 /*
367  * MPSAFE
368  */
369 int
370 sys_writev(struct writev_args *uap)
371 {
372         struct thread *td = curthread;
373         struct uio auio;
374         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
375         int error;
376
377         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
378                              &auio.uio_resid);
379         if (error)
380                 return (error);
381         auio.uio_iov = iov;
382         auio.uio_iovcnt = uap->iovcnt;
383         auio.uio_offset = -1;
384         auio.uio_rw = UIO_WRITE;
385         auio.uio_segflg = UIO_USERSPACE;
386         auio.uio_td = td;
387
388         error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
389
390         iovec_free(&iov, aiov);
391         return (error);
392 }
393
394
395 /*
396  * Gather positioned write system call
397  *
398  * MPSAFE
399  */
400 int
401 sys_extpwritev(struct extpwritev_args *uap)
402 {
403         struct thread *td = curthread;
404         struct uio auio;
405         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
406         int error;
407         int flags;
408
409         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
410                              &auio.uio_resid);
411         if (error)
412                 return (error);
413         auio.uio_iov = iov;
414         auio.uio_iovcnt = uap->iovcnt;
415         auio.uio_offset = uap->offset;
416         auio.uio_rw = UIO_WRITE;
417         auio.uio_segflg = UIO_USERSPACE;
418         auio.uio_td = td;
419
420         flags = uap->flags & O_FMASK;
421         if (uap->offset != (off_t)-1)
422                 flags |= O_FOFFSET;
423
424         error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_result);
425
426         iovec_free(&iov, aiov);
427         return(error);
428 }
429
430 /*
431  * MPSAFE
432  */
433 int
434 kern_pwritev(int fd, struct uio *auio, int flags, int *res)
435 {
436         struct thread *td = curthread;
437         struct proc *p = td->td_proc;
438         struct file *fp;
439         int error;
440
441         KKASSERT(p);
442
443         fp = holdfp(p->p_fd, fd, FWRITE);
444         if (fp == NULL)
445                 return (EBADF);
446         else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
447                 error = ESPIPE;
448         } else {
449                 error = dofilewrite(fd, fp, auio, flags, res);
450         }
451         
452         fdrop(fp);
453         return (error);
454 }
455
456 /*
457  * Common code for writev and pwritev that writes data to
458  * a file using the passed in uio, offset, and flags.
459  *
460  * MPALMOSTSAFE - ktrace needs help
461  */
462 static int
463 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, int *res)
464 {       
465         struct thread *td = curthread;
466         struct lwp *lp = td->td_lwp;
467         struct proc *p = td->td_proc;
468         int error;
469         int len;
470 #ifdef KTRACE
471         struct iovec *ktriov = NULL;
472         struct uio ktruio;
473 #endif
474
475 #ifdef KTRACE
476         /*
477          * if tracing, save a copy of iovec and uio
478          */
479         if (KTRPOINT(td, KTR_GENIO))  {
480                 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
481
482                 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
483                 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
484                 ktruio = *auio;
485         }
486 #endif
487         len = auio->uio_resid;
488         if (fp->f_type == DTYPE_VNODE)
489                 bwillwrite();
490         error = fo_write(fp, auio, fp->f_cred, flags);
491         if (error) {
492                 if (auio->uio_resid != len && (error == ERESTART ||
493                     error == EINTR || error == EWOULDBLOCK))
494                         error = 0;
495                 /* Socket layer is responsible for issuing SIGPIPE. */
496                 if (error == EPIPE) {
497                         get_mplock();
498                         lwpsignal(p, lp, SIGPIPE);
499                         rel_mplock();
500                 }
501         }
502 #ifdef KTRACE
503         if (ktriov != NULL) {
504                 if (error == 0) {
505                         ktruio.uio_iov = ktriov;
506                         ktruio.uio_resid = len - auio->uio_resid;
507                         get_mplock();
508                         ktrgenio(p, fd, UIO_WRITE, &ktruio, error);
509                         rel_mplock();
510                 }
511                 FREE(ktriov, M_TEMP);
512         }
513 #endif
514         if (error == 0)
515                 *res = len - auio->uio_resid;
516
517         return(error);
518 }
519
520 /*
521  * Ioctl system call
522  */
523 /* ARGSUSED */
524 int
525 sys_ioctl(struct ioctl_args *uap)
526 {
527         return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
528 }
529
530 struct ioctl_map_entry {
531         const char *subsys;
532         struct ioctl_map_range *cmd_ranges;
533         LIST_ENTRY(ioctl_map_entry) entries;
534 };
535
536 /*
537  * The true heart of all ioctl syscall handlers (native, emulation).
538  * If map != NULL, it will be searched for a matching entry for com,
539  * and appropriate conversions/conversion functions will be utilized.
540  */
541 int
542 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
543 {
544         struct thread *td = curthread;
545         struct proc *p = td->td_proc;
546         struct ucred *cred;
547         struct file *fp;
548         struct ioctl_map_range *iomc = NULL;
549         int error;
550         u_int size;
551         u_long ocom = com;
552         caddr_t data, memp;
553         int tmp;
554 #define STK_PARAMS      128
555         union {
556             char stkbuf[STK_PARAMS];
557             long align;
558         } ubuf;
559
560         KKASSERT(p);
561         cred = p->p_ucred;
562
563         fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
564         if (fp == NULL)
565                 return(EBADF);
566
567         if (map != NULL) {      /* obey translation map */
568                 u_long maskcmd;
569                 struct ioctl_map_entry *e;
570
571                 maskcmd = com & map->mask;
572
573                 LIST_FOREACH(e, &map->mapping, entries) {
574                         for (iomc = e->cmd_ranges; iomc->start != 0 ||
575                              iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
576                              iomc->mapfunc != NULL;
577                              iomc++) {
578                                 if (maskcmd >= iomc->start &&
579                                     maskcmd <= iomc->end)
580                                         break;
581                         }
582
583                         /* Did we find a match? */
584                         if (iomc->start != 0 || iomc->maptocmd != 0 ||
585                             iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
586                                 break;
587                 }
588
589                 if (iomc == NULL ||
590                     (iomc->start == 0 && iomc->maptocmd == 0
591                      && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
592                         kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
593                                map->sys, fd, maskcmd,
594                                (int)((maskcmd >> 8) & 0xff),
595                                (int)(maskcmd & 0xff));
596                         error = EINVAL;
597                         goto done;
598                 }
599
600                 /*
601                  * If it's a non-range one to one mapping, maptocmd should be
602                  * correct. If it's a ranged one to one mapping, we pass the
603                  * original value of com, and for a range mapped to a different
604                  * range, we always need a mapping function to translate the
605                  * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
606                  */
607                 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
608                         com = iomc->maptocmd;
609                 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
610                         if (iomc->mapfunc != NULL)
611                                 com = iomc->mapfunc(iomc->start, iomc->end,
612                                                     iomc->start, iomc->end,
613                                                     com, com);
614                 } else {
615                         if (iomc->mapfunc != NULL) {
616                                 com = iomc->mapfunc(iomc->start, iomc->end,
617                                                     iomc->maptocmd, iomc->maptoend,
618                                                     com, ocom);
619                         } else {
620                                 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
621                                        map->sys, fd, maskcmd,
622                                        (int)((maskcmd >> 8) & 0xff),
623                                        (int)(maskcmd & 0xff));
624                                 error = EINVAL;
625                                 goto done;
626                         }
627                 }
628         }
629
630         switch (com) {
631         case FIONCLEX:
632                 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
633                 goto done;
634         case FIOCLEX:
635                 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
636                 goto done;
637         }
638
639         /*
640          * Interpret high order word to find amount of data to be
641          * copied to/from the user's address space.
642          */
643         size = IOCPARM_LEN(com);
644         if (size > IOCPARM_MAX) {
645                 error = ENOTTY;
646                 goto done;
647         }
648
649         memp = NULL;
650         if (size > sizeof (ubuf.stkbuf)) {
651                 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
652                 data = memp;
653         } else {
654                 data = ubuf.stkbuf;
655         }
656         if ((com & IOC_IN) != 0) {
657                 if (size != 0) {
658                         error = copyin(uspc_data, data, (u_int)size);
659                         if (error) {
660                                 if (memp != NULL)
661                                         kfree(memp, M_IOCTLOPS);
662                                 goto done;
663                         }
664                 } else {
665                         *(caddr_t *)data = uspc_data;
666                 }
667         } else if ((com & IOC_OUT) != 0 && size) {
668                 /*
669                  * Zero the buffer so the user always
670                  * gets back something deterministic.
671                  */
672                 bzero(data, size);
673         } else if ((com & IOC_VOID) != 0) {
674                 *(caddr_t *)data = uspc_data;
675         }
676
677         switch (com) {
678         case FIONBIO:
679                 if ((tmp = *(int *)data))
680                         fp->f_flag |= FNONBLOCK;
681                 else
682                         fp->f_flag &= ~FNONBLOCK;
683                 error = 0;
684                 break;
685
686         case FIOASYNC:
687                 if ((tmp = *(int *)data))
688                         fp->f_flag |= FASYNC;
689                 else
690                         fp->f_flag &= ~FASYNC;
691                 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
692                 break;
693
694         default:
695                 /*
696                  *  If there is a override function,
697                  *  call it instead of directly routing the call
698                  */
699                 if (map != NULL && iomc->wrapfunc != NULL)
700                         error = iomc->wrapfunc(fp, com, ocom, data, cred);
701                 else
702                         error = fo_ioctl(fp, com, data, cred);
703                 /*
704                  * Copy any data to user, size was
705                  * already set and checked above.
706                  */
707                 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
708                         error = copyout(data, uspc_data, (u_int)size);
709                 break;
710         }
711         if (memp != NULL)
712                 kfree(memp, M_IOCTLOPS);
713 done:
714         fdrop(fp);
715         return(error);
716 }
717
718 int
719 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
720 {
721         struct ioctl_map_entry *ne;
722
723         KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
724                  he->subsys != NULL && *he->subsys != '\0');
725
726         ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
727
728         ne->subsys = he->subsys;
729         ne->cmd_ranges = he->cmd_ranges;
730
731         LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
732
733         return(0);
734 }
735
736 int
737 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
738 {
739         struct ioctl_map_entry *ne;
740
741         KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
742
743         LIST_FOREACH(ne, &he->map->mapping, entries) {
744                 if (ne->cmd_ranges != he->cmd_ranges)
745                         continue;
746                 LIST_REMOVE(ne, entries);
747                 kfree(ne, M_IOCTLMAP);
748                 return(0);
749         }
750         return(EINVAL);
751 }
752
753 static int      nselcoll;       /* Select collisions since boot */
754 int     selwait;
755 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
756
757 /*
758  * Select system call.
759  */
760 int
761 sys_select(struct select_args *uap)
762 {
763         struct lwp *lp = curthread->td_lwp;
764         struct proc *p = curproc;
765
766         /*
767          * The magic 2048 here is chosen to be just enough for FD_SETSIZE
768          * infds with the new FD_SETSIZE of 1024, and more than enough for
769          * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
770          * of 256.
771          */
772         fd_mask s_selbits[howmany(2048, NFDBITS)];
773         fd_mask *ibits[3], *obits[3], *selbits, *sbp;
774         struct timeval atv, rtv, ttv;
775         int ncoll, error, timo;
776         u_int nbufbytes, ncpbytes, nfdbits;
777
778         if (uap->nd < 0)
779                 return (EINVAL);
780         if (uap->nd > p->p_fd->fd_nfiles)
781                 uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
782
783         /*
784          * Allocate just enough bits for the non-null fd_sets.  Use the
785          * preallocated auto buffer if possible.
786          */
787         nfdbits = roundup(uap->nd, NFDBITS);
788         ncpbytes = nfdbits / NBBY;
789         nbufbytes = 0;
790         if (uap->in != NULL)
791                 nbufbytes += 2 * ncpbytes;
792         if (uap->ou != NULL)
793                 nbufbytes += 2 * ncpbytes;
794         if (uap->ex != NULL)
795                 nbufbytes += 2 * ncpbytes;
796         if (nbufbytes <= sizeof s_selbits)
797                 selbits = &s_selbits[0];
798         else
799                 selbits = kmalloc(nbufbytes, M_SELECT, M_WAITOK);
800
801         /*
802          * Assign pointers into the bit buffers and fetch the input bits.
803          * Put the output buffers together so that they can be bzeroed
804          * together.
805          */
806         sbp = selbits;
807 #define getbits(name, x) \
808         do {                                                            \
809                 if (uap->name == NULL)                                  \
810                         ibits[x] = NULL;                                \
811                 else {                                                  \
812                         ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;   \
813                         obits[x] = sbp;                                 \
814                         sbp += ncpbytes / sizeof *sbp;                  \
815                         error = copyin(uap->name, ibits[x], ncpbytes);  \
816                         if (error != 0)                                 \
817                                 goto done;                              \
818                 }                                                       \
819         } while (0)
820         getbits(in, 0);
821         getbits(ou, 1);
822         getbits(ex, 2);
823 #undef  getbits
824         if (nbufbytes != 0)
825                 bzero(selbits, nbufbytes / 2);
826
827         if (uap->tv) {
828                 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
829                         sizeof (atv));
830                 if (error)
831                         goto done;
832                 if (itimerfix(&atv)) {
833                         error = EINVAL;
834                         goto done;
835                 }
836                 getmicrouptime(&rtv);
837                 timevaladd(&atv, &rtv);
838         } else {
839                 atv.tv_sec = 0;
840                 atv.tv_usec = 0;
841         }
842         timo = 0;
843 retry:
844         ncoll = nselcoll;
845         lp->lwp_flag |= LWP_SELECT;
846         error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
847         if (error || uap->sysmsg_result)
848                 goto done;
849         if (atv.tv_sec || atv.tv_usec) {
850                 getmicrouptime(&rtv);
851                 if (timevalcmp(&rtv, &atv, >=)) 
852                         goto done;
853                 ttv = atv;
854                 timevalsub(&ttv, &rtv);
855                 timo = ttv.tv_sec > 24 * 60 * 60 ?
856                     24 * 60 * 60 * hz : tvtohz_high(&ttv);
857         }
858         crit_enter();
859         if ((lp->lwp_flag & LWP_SELECT) == 0 || nselcoll != ncoll) {
860                 crit_exit();
861                 goto retry;
862         }
863         lp->lwp_flag &= ~LWP_SELECT;
864
865         error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
866         
867         crit_exit();
868         if (error == 0)
869                 goto retry;
870 done:
871         lp->lwp_flag &= ~LWP_SELECT;
872         /* select is not restarted after signals... */
873         if (error == ERESTART)
874                 error = EINTR;
875         if (error == EWOULDBLOCK)
876                 error = 0;
877 #define putbits(name, x) \
878         if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
879                 error = error2;
880         if (error == 0) {
881                 int error2;
882
883                 putbits(in, 0);
884                 putbits(ou, 1);
885                 putbits(ex, 2);
886 #undef putbits
887         }
888         if (selbits != &s_selbits[0])
889                 kfree(selbits, M_SELECT);
890         return (error);
891 }
892
893 static int
894 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
895 {
896         int msk, i, fd;
897         fd_mask bits;
898         struct file *fp;
899         int n = 0;
900         /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
901         static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
902
903         for (msk = 0; msk < 3; msk++) {
904                 if (ibits[msk] == NULL)
905                         continue;
906                 for (i = 0; i < nfd; i += NFDBITS) {
907                         bits = ibits[msk][i/NFDBITS];
908                         /* ffs(int mask) not portable, fd_mask is long */
909                         for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
910                                 if (!(bits & 1))
911                                         continue;
912                                 fp = holdfp(p->p_fd, fd, -1);
913                                 if (fp == NULL)
914                                         return (EBADF);
915                                 if (fo_poll(fp, flag[msk], fp->f_cred)) {
916                                         obits[msk][(fd)/NFDBITS] |=
917                                             ((fd_mask)1 << ((fd) % NFDBITS));
918                                         n++;
919                                 }
920                                 fdrop(fp);
921                         }
922                 }
923         }
924         *res = n;
925         return (0);
926 }
927
928 /*
929  * Poll system call.
930  */
931 int
932 sys_poll(struct poll_args *uap)
933 {
934         struct pollfd *bits;
935         struct pollfd smallbits[32];
936         struct timeval atv, rtv, ttv;
937         int ncoll, error = 0, timo;
938         u_int nfds;
939         size_t ni;
940         struct lwp *lp = curthread->td_lwp;
941         struct proc *p = curproc;
942
943         nfds = uap->nfds;
944         /*
945          * This is kinda bogus.  We have fd limits, but that is not
946          * really related to the size of the pollfd array.  Make sure
947          * we let the process use at least FD_SETSIZE entries and at
948          * least enough for the current limits.  We want to be reasonably
949          * safe, but not overly restrictive.
950          */
951         if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
952                 return (EINVAL);
953         ni = nfds * sizeof(struct pollfd);
954         if (ni > sizeof(smallbits))
955                 bits = kmalloc(ni, M_TEMP, M_WAITOK);
956         else
957                 bits = smallbits;
958         error = copyin(uap->fds, bits, ni);
959         if (error)
960                 goto done;
961         if (uap->timeout != INFTIM) {
962                 atv.tv_sec = uap->timeout / 1000;
963                 atv.tv_usec = (uap->timeout % 1000) * 1000;
964                 if (itimerfix(&atv)) {
965                         error = EINVAL;
966                         goto done;
967                 }
968                 getmicrouptime(&rtv);
969                 timevaladd(&atv, &rtv);
970         } else {
971                 atv.tv_sec = 0;
972                 atv.tv_usec = 0;
973         }
974         timo = 0;
975 retry:
976         ncoll = nselcoll;
977         lp->lwp_flag |= LWP_SELECT;
978         error = pollscan(p, bits, nfds, &uap->sysmsg_result);
979         if (error || uap->sysmsg_result)
980                 goto done;
981         if (atv.tv_sec || atv.tv_usec) {
982                 getmicrouptime(&rtv);
983                 if (timevalcmp(&rtv, &atv, >=))
984                         goto done;
985                 ttv = atv;
986                 timevalsub(&ttv, &rtv);
987                 timo = ttv.tv_sec > 24 * 60 * 60 ?
988                     24 * 60 * 60 * hz : tvtohz_high(&ttv);
989         } 
990         crit_enter();
991         if ((lp->lwp_flag & LWP_SELECT) == 0 || nselcoll != ncoll) {
992                 crit_exit();
993                 goto retry;
994         }
995         lp->lwp_flag &= ~LWP_SELECT;
996         error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
997         crit_exit();
998         if (error == 0)
999                 goto retry;
1000 done:
1001         lp->lwp_flag &= ~LWP_SELECT;
1002         /* poll is not restarted after signals... */
1003         if (error == ERESTART)
1004                 error = EINTR;
1005         if (error == EWOULDBLOCK)
1006                 error = 0;
1007         if (error == 0) {
1008                 error = copyout(bits, uap->fds, ni);
1009                 if (error)
1010                         goto out;
1011         }
1012 out:
1013         if (ni > sizeof(smallbits))
1014                 kfree(bits, M_TEMP);
1015         return (error);
1016 }
1017
1018 static int
1019 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
1020 {
1021         int i;
1022         struct file *fp;
1023         int n = 0;
1024
1025         for (i = 0; i < nfd; i++, fds++) {
1026                 if (fds->fd >= p->p_fd->fd_nfiles) {
1027                         fds->revents = POLLNVAL;
1028                         n++;
1029                 } else if (fds->fd < 0) {
1030                         fds->revents = 0;
1031                 } else {
1032                         fp = holdfp(p->p_fd, fds->fd, -1);
1033                         if (fp == NULL) {
1034                                 fds->revents = POLLNVAL;
1035                                 n++;
1036                         } else {
1037                                 /*
1038                                  * Note: backend also returns POLLHUP and
1039                                  * POLLERR if appropriate.
1040                                  */
1041                                 fds->revents = fo_poll(fp, fds->events,
1042                                                         fp->f_cred);
1043                                 if (fds->revents != 0)
1044                                         n++;
1045                                 fdrop(fp);
1046                         }
1047                 }
1048         }
1049         *res = n;
1050         return (0);
1051 }
1052
1053 /*
1054  * OpenBSD poll system call.
1055  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
1056  */
1057 int
1058 sys_openbsd_poll(struct openbsd_poll_args *uap)
1059 {
1060         return (sys_poll((struct poll_args *)uap));
1061 }
1062
1063 /*ARGSUSED*/
1064 int
1065 seltrue(cdev_t dev, int events)
1066 {
1067         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1068 }
1069
1070 /*
1071  * Record a select request.  A global wait must be used since a process/thread
1072  * might go away after recording its request.
1073  */
1074 void
1075 selrecord(struct thread *selector, struct selinfo *sip)
1076 {
1077         struct proc *p;
1078         struct lwp *lp = NULL;
1079
1080         if (selector->td_lwp == NULL)
1081                 panic("selrecord: thread needs a process");
1082
1083         if (sip->si_pid == selector->td_proc->p_pid &&
1084             sip->si_tid == selector->td_lwp->lwp_tid)
1085                 return;
1086         if (sip->si_pid && (p = pfind(sip->si_pid)))
1087                 lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1088         if (lp != NULL && lp->lwp_wchan == (caddr_t)&selwait) {
1089                 sip->si_flags |= SI_COLL;
1090         } else {
1091                 sip->si_pid = selector->td_proc->p_pid;
1092                 sip->si_tid = selector->td_lwp->lwp_tid;
1093         }
1094 }
1095
1096 /*
1097  * Do a wakeup when a selectable event occurs.
1098  */
1099 void
1100 selwakeup(struct selinfo *sip)
1101 {
1102         struct proc *p;
1103         struct lwp *lp = NULL;
1104
1105         if (sip->si_pid == 0)
1106                 return;
1107         if (sip->si_flags & SI_COLL) {
1108                 nselcoll++;
1109                 sip->si_flags &= ~SI_COLL;
1110                 wakeup((caddr_t)&selwait);      /* YYY fixable */
1111         }
1112         p = pfind(sip->si_pid);
1113         sip->si_pid = 0;
1114         if (p == NULL)
1115                 return;
1116         lp = lwp_rb_tree_RB_LOOKUP(&p->p_lwp_tree, sip->si_tid);
1117         if (lp == NULL)
1118                 return;
1119
1120         crit_enter();
1121         if (lp->lwp_wchan == (caddr_t)&selwait) {
1122                 /*
1123                  * Flag the process to break the tsleep when
1124                  * setrunnable is called, but only call setrunnable
1125                  * here if the process is not in a stopped state.
1126                  */
1127                 lp->lwp_flag |= LWP_BREAKTSLEEP;
1128                 if (p->p_stat != SSTOP)
1129                         setrunnable(lp);
1130         } else if (lp->lwp_flag & LWP_SELECT) {
1131                 lp->lwp_flag &= ~LWP_SELECT;
1132         }
1133         crit_exit();
1134 }
1135