Document MAKEOBJDIRPREFIX.
[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.27 2006/05/06 06:38:38 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 struct file*
88 holdfp(fdp, fd, flag)
89         struct filedesc* fdp;
90         int fd, flag;
91 {
92         struct file* fp;
93
94         if (((u_int)fd) >= fdp->fd_nfiles ||
95             (fp = fdp->fd_files[fd].fp) == NULL ||
96             (fp->f_flag & flag) == 0) {
97                 return (NULL);
98         }
99         fhold(fp);
100         return (fp);
101 }
102
103 /*
104  * Read system call.
105  */
106 int
107 read(struct read_args *uap)
108 {
109         struct thread *td = curthread;
110         struct uio auio;
111         struct iovec aiov;
112         int error;
113
114         aiov.iov_base = uap->buf;
115         aiov.iov_len = uap->nbyte;
116         auio.uio_iov = &aiov;
117         auio.uio_iovcnt = 1;
118         auio.uio_offset = -1;
119         auio.uio_resid = uap->nbyte;
120         auio.uio_rw = UIO_READ;
121         auio.uio_segflg = UIO_USERSPACE;
122         auio.uio_td = td;
123
124         if (auio.uio_resid < 0)
125                 error = EINVAL;
126         else
127                 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
128         return(error);
129 }
130
131 /*
132  * Positioned (Pread) read system call
133  */
134 int
135 pread(struct pread_args *uap)
136 {
137         struct thread *td = curthread;
138         struct uio auio;
139         struct iovec aiov;
140         int error;
141
142         aiov.iov_base = uap->buf;
143         aiov.iov_len = uap->nbyte;
144         auio.uio_iov = &aiov;
145         auio.uio_iovcnt = 1;
146         auio.uio_offset = uap->offset;
147         auio.uio_resid = uap->nbyte;
148         auio.uio_rw = UIO_READ;
149         auio.uio_segflg = UIO_USERSPACE;
150         auio.uio_td = td;
151
152         if (auio.uio_resid < 0)
153                 error = EINVAL;
154         else
155                 error = kern_preadv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
156         return(error);
157 }
158
159 /*
160  * Scatter read system call.
161  */
162 int
163 readv(struct readv_args *uap)
164 {
165         struct thread *td = curthread;
166         struct uio auio;
167         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
168         int error;
169
170         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
171                              &auio.uio_resid);
172         if (error)
173                 return (error);
174         auio.uio_iov = iov;
175         auio.uio_iovcnt = uap->iovcnt;
176         auio.uio_offset = -1;
177         auio.uio_rw = UIO_READ;
178         auio.uio_segflg = UIO_USERSPACE;
179         auio.uio_td = td;
180
181         error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_result);
182
183         iovec_free(&iov, aiov);
184         return (error);
185 }
186
187
188 /*
189  * Scatter positioned read system call.
190  */
191 int
192 preadv(struct preadv_args *uap)
193 {
194         struct thread *td = curthread;
195         struct uio auio;
196         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
197         int error;
198
199         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
200                              &auio.uio_resid);
201         if (error)
202                 return (error);
203         auio.uio_iov = iov;
204         auio.uio_iovcnt = uap->iovcnt;
205         auio.uio_offset = uap->offset;
206         auio.uio_rw = UIO_READ;
207         auio.uio_segflg = UIO_USERSPACE;
208         auio.uio_td = td;
209
210         error = kern_preadv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
211
212         iovec_free(&iov, aiov);
213         return(error);
214 }
215
216 int
217 kern_preadv(int fd, struct uio *auio, int flags, int *res)
218 {
219         struct thread *td = curthread;
220         struct proc *p = td->td_proc;
221         struct file *fp;
222         struct filedesc *fdp = p->p_fd;
223         int error;
224
225         KKASSERT(p);
226
227         fp = holdfp(fdp, fd, FREAD);
228         if (fp == NULL)
229                 return (EBADF);
230         if (flags & FOF_OFFSET && fp->f_type != DTYPE_VNODE) {
231                 error = ESPIPE;
232         } else if (auio->uio_resid < 0) {
233                 error = EINVAL;
234         } else {
235                 error = dofileread(fd, fp, auio, flags, res);
236         }
237         fdrop(fp);
238         return(error);
239 }
240
241 /*
242  * Common code for readv and preadv that reads data in
243  * from a file using the passed in uio, offset, and flags.
244  */
245 static int
246 dofileread(int fd, struct file *fp, struct uio *auio, int flags, int *res)
247 {
248         struct thread *td = curthread;
249         struct proc *p = td->td_proc;
250         int error;
251         int len;
252 #ifdef KTRACE
253         struct iovec *ktriov = NULL;
254         struct uio ktruio;
255 #endif
256
257 #ifdef KTRACE
258         /*
259          * if tracing, save a copy of iovec
260          */
261         if (KTRPOINT(td, KTR_GENIO))  {
262                 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
263
264                 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
265                 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
266                 ktruio = *auio;
267         }
268 #endif
269         len = auio->uio_resid;
270         error = fo_read(fp, auio, fp->f_cred, flags);
271         if (error) {
272                 if (auio->uio_resid != len && (error == ERESTART ||
273                     error == EINTR || error == EWOULDBLOCK))
274                         error = 0;
275         }
276 #ifdef KTRACE
277         if (ktriov != NULL) {
278                 if (error == 0) {
279                         ktruio.uio_iov = ktriov;
280                         ktruio.uio_resid = len - auio->uio_resid;
281                         ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
282                 }
283                 FREE(ktriov, M_TEMP);
284         }
285 #endif
286         if (error == 0)
287                 *res = len - auio->uio_resid;
288
289         return(error);
290 }
291
292 /*
293  * Write system call
294  */
295 int
296 write(struct write_args *uap)
297 {
298         struct thread *td = curthread;
299         struct uio auio;
300         struct iovec aiov;
301         int error;
302
303         aiov.iov_base = (void *)(uintptr_t)uap->buf;
304         aiov.iov_len = uap->nbyte;
305         auio.uio_iov = &aiov;
306         auio.uio_iovcnt = 1;
307         auio.uio_offset = -1;
308         auio.uio_resid = uap->nbyte;
309         auio.uio_rw = UIO_WRITE;
310         auio.uio_segflg = UIO_USERSPACE;
311         auio.uio_td = td;
312
313         if (auio.uio_resid < 0)
314                 error = EINVAL;
315         else
316                 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
317
318         return(error);
319 }
320
321 /*
322  * Pwrite system call
323  */
324 int
325 pwrite(struct pwrite_args *uap)
326 {
327         struct thread *td = curthread;
328         struct uio auio;
329         struct iovec aiov;
330         int error;
331
332         aiov.iov_base = (void *)(uintptr_t)uap->buf;
333         aiov.iov_len = uap->nbyte;
334         auio.uio_iov = &aiov;
335         auio.uio_iovcnt = 1;
336         auio.uio_offset = uap->offset;
337         auio.uio_resid = uap->nbyte;
338         auio.uio_rw = UIO_WRITE;
339         auio.uio_segflg = UIO_USERSPACE;
340         auio.uio_td = td;
341
342         if (auio.uio_resid < 0)
343                 error = EINVAL;
344         else
345                 error = kern_pwritev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
346
347         return(error);
348 }
349
350 int
351 writev(struct writev_args *uap)
352 {
353         struct thread *td = curthread;
354         struct uio auio;
355         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
356         int error;
357
358         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
359                              &auio.uio_resid);
360         if (error)
361                 return (error);
362         auio.uio_iov = iov;
363         auio.uio_iovcnt = uap->iovcnt;
364         auio.uio_offset = -1;
365         auio.uio_rw = UIO_WRITE;
366         auio.uio_segflg = UIO_USERSPACE;
367         auio.uio_td = td;
368
369         error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_result);
370
371         iovec_free(&iov, aiov);
372         return (error);
373 }
374
375
376 /*
377  * Gather positioned write system call
378  */
379 int
380 pwritev(struct pwritev_args *uap)
381 {
382         struct thread *td = curthread;
383         struct uio auio;
384         struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
385         int error;
386
387         error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
388                              &auio.uio_resid);
389         if (error)
390                 return (error);
391         auio.uio_iov = iov;
392         auio.uio_iovcnt = uap->iovcnt;
393         auio.uio_offset = uap->offset;
394         auio.uio_rw = UIO_WRITE;
395         auio.uio_segflg = UIO_USERSPACE;
396         auio.uio_td = td;
397
398         error = kern_pwritev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
399
400         iovec_free(&iov, aiov);
401         return(error);
402 }
403
404 int
405 kern_pwritev(int fd, struct uio *auio, int flags, int *res)
406 {
407         struct thread *td = curthread;
408         struct proc *p = td->td_proc;
409         struct file *fp;
410         struct filedesc *fdp = p->p_fd;
411         int error;
412
413         KKASSERT(p);
414
415         fp = holdfp(fdp, fd, FWRITE);
416         if (fp == NULL)
417                 return (EBADF);
418         else if ((flags & FOF_OFFSET) && fp->f_type != DTYPE_VNODE) {
419                 error = ESPIPE;
420         } else {
421                 error = dofilewrite(fd, fp, auio, flags, res);
422         }
423         
424         fdrop(fp);
425         return (error);
426 }
427
428 /*
429  * Common code for writev and pwritev that writes data to
430  * a file using the passed in uio, offset, and flags.
431  */
432 static int
433 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, int *res)
434 {       
435         struct thread *td = curthread;
436         struct proc *p = td->td_proc;
437         int error;
438         int len;
439 #ifdef KTRACE
440         struct iovec *ktriov = NULL;
441         struct uio ktruio;
442 #endif
443
444 #ifdef KTRACE
445         /*
446          * if tracing, save a copy of iovec and uio
447          */
448         if (KTRPOINT(td, KTR_GENIO))  {
449                 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
450
451                 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
452                 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
453                 ktruio = *auio;
454         }
455 #endif
456         len = auio->uio_resid;
457         if (fp->f_type == DTYPE_VNODE)
458                 bwillwrite();
459         error = fo_write(fp, auio, fp->f_cred, flags);
460         if (error) {
461                 if (auio->uio_resid != len && (error == ERESTART ||
462                     error == EINTR || error == EWOULDBLOCK))
463                         error = 0;
464                 /* Socket layer is responsible for issuing SIGPIPE. */
465                 if (error == EPIPE)
466                         psignal(p, SIGPIPE);
467         }
468 #ifdef KTRACE
469         if (ktriov != NULL) {
470                 if (error == 0) {
471                         ktruio.uio_iov = ktriov;
472                         ktruio.uio_resid = len - auio->uio_resid;
473                         ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
474                 }
475                 FREE(ktriov, M_TEMP);
476         }
477 #endif
478         if (error == 0)
479                 *res = len - auio->uio_resid;
480
481         return(error);
482 }
483
484 /*
485  * Ioctl system call
486  */
487 /* ARGSUSED */
488 int
489 ioctl(struct ioctl_args *uap)
490 {
491         return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
492 }
493
494 struct ioctl_map_entry {
495         const char *subsys;
496         struct ioctl_map_range *cmd_ranges;
497         LIST_ENTRY(ioctl_map_entry) entries;
498 };
499
500 /*
501  * The true heart of all ioctl syscall handlers (native, emulation).
502  * If map != NULL, it will be searched for a matching entry for com,
503  * and appropriate conversions/conversion functions will be utilized.
504  */
505 int
506 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
507 {
508         struct thread *td = curthread;
509         struct proc *p = td->td_proc;
510         struct ucred *cred;
511         struct file *fp;
512         struct filedesc *fdp;
513         struct ioctl_map_range *iomc = NULL;
514         int error;
515         u_int size;
516         u_long ocom = com;
517         caddr_t data, memp;
518         int tmp;
519 #define STK_PARAMS      128
520         union {
521             char stkbuf[STK_PARAMS];
522             long align;
523         } ubuf;
524
525         KKASSERT(p);
526         cred = p->p_ucred;
527         fdp = p->p_fd;
528         if ((u_int)fd >= fdp->fd_nfiles ||
529             (fp = fdp->fd_files[fd].fp) == NULL)
530                 return(EBADF);
531
532         if ((fp->f_flag & (FREAD | FWRITE)) == 0)
533                 return(EBADF);
534
535         if (map != NULL) {      /* obey translation map */
536                 u_long maskcmd;
537                 struct ioctl_map_entry *e;
538
539                 maskcmd = com & map->mask;
540
541                 LIST_FOREACH(e, &map->mapping, entries) {
542                         for (iomc = e->cmd_ranges; iomc->start != 0 ||
543                              iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
544                              iomc->mapfunc != NULL;
545                              iomc++) {
546                                 if (maskcmd >= iomc->start &&
547                                     maskcmd <= iomc->end)
548                                         break;
549                         }
550
551                         /* Did we find a match? */
552                         if (iomc->start != 0 || iomc->maptocmd != 0 ||
553                             iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
554                                 break;
555                 }
556
557                 if (iomc == NULL ||
558                     (iomc->start == 0 && iomc->maptocmd == 0
559                      && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
560                         printf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
561                                map->sys, fd, maskcmd,
562                                (int)((maskcmd >> 8) & 0xff),
563                                (int)(maskcmd & 0xff));
564                         return(EINVAL);
565                 }
566
567                 /*
568                  * If it's a non-range one to one mapping, maptocmd should be
569                  * correct. If it's a ranged one to one mapping, we pass the
570                  * original value of com, and for a range mapped to a different
571                  * range, we always need a mapping function to translate the
572                  * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
573                  */
574                 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
575                         com = iomc->maptocmd;
576                 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
577                         if (iomc->mapfunc != NULL)
578                                 com = iomc->mapfunc(iomc->start, iomc->end,
579                                                     iomc->start, iomc->end,
580                                                     com, com);
581                 } else {
582                         if (iomc->mapfunc != NULL) {
583                                 com = iomc->mapfunc(iomc->start, iomc->end,
584                                                     iomc->maptocmd, iomc->maptoend,
585                                                     com, ocom);
586                         } else {
587                                 printf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
588                                        map->sys, fd, maskcmd,
589                                        (int)((maskcmd >> 8) & 0xff),
590                                        (int)(maskcmd & 0xff));
591                                 return(EINVAL);
592                         }
593                 }
594         }
595
596         switch (com) {
597         case FIONCLEX:
598                 fdp->fd_files[fd].fileflags &= ~UF_EXCLOSE;
599                 return(0);
600         case FIOCLEX:
601                 fdp->fd_files[fd].fileflags |= UF_EXCLOSE;
602                 return(0);
603         }
604
605         /*
606          * Interpret high order word to find amount of data to be
607          * copied to/from the user's address space.
608          */
609         size = IOCPARM_LEN(com);
610         if (size > IOCPARM_MAX)
611                 return(ENOTTY);
612
613         fhold(fp);
614
615         memp = NULL;
616         if (size > sizeof (ubuf.stkbuf)) {
617                 memp = malloc(size, M_IOCTLOPS, M_WAITOK);
618                 data = memp;
619         } else {
620                 data = ubuf.stkbuf;
621         }
622         if ((com & IOC_IN) != 0) {
623                 if (size != 0) {
624                         error = copyin(uspc_data, data, (u_int)size);
625                         if (error) {
626                                 if (memp != NULL)
627                                         free(memp, M_IOCTLOPS);
628                                 fdrop(fp);
629                                 return(error);
630                         }
631                 } else {
632                         *(caddr_t *)data = uspc_data;
633                 }
634         } else if ((com & IOC_OUT) != 0 && size) {
635                 /*
636                  * Zero the buffer so the user always
637                  * gets back something deterministic.
638                  */
639                 bzero(data, size);
640         } else if ((com & IOC_VOID) != 0) {
641                 *(caddr_t *)data = uspc_data;
642         }
643
644         switch (com) {
645
646         case FIONBIO:
647                 if ((tmp = *(int *)data))
648                         fp->f_flag |= FNONBLOCK;
649                 else
650                         fp->f_flag &= ~FNONBLOCK;
651                 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, cred);
652                 break;
653
654         case FIOASYNC:
655                 if ((tmp = *(int *)data))
656                         fp->f_flag |= FASYNC;
657                 else
658                         fp->f_flag &= ~FASYNC;
659                 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred);
660                 break;
661
662         default:
663                 /*
664                  *  If there is a override function,
665                  *  call it instead of directly routing the call
666                  */
667                 if (map != NULL && iomc->wrapfunc != NULL)
668                         error = iomc->wrapfunc(fp, com, ocom, data, cred);
669                 else
670                         error = fo_ioctl(fp, com, data, cred);
671                 /*
672                  * Copy any data to user, size was
673                  * already set and checked above.
674                  */
675                 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
676                         error = copyout(data, uspc_data, (u_int)size);
677                 break;
678         }
679         if (memp != NULL)
680                 free(memp, M_IOCTLOPS);
681         fdrop(fp);
682         return(error);
683 }
684
685 int
686 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
687 {
688         struct ioctl_map_entry *ne;
689
690         KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
691                  he->subsys != NULL && *he->subsys != '\0');
692
693         ne = malloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
694
695         ne->subsys = he->subsys;
696         ne->cmd_ranges = he->cmd_ranges;
697
698         LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
699
700         return(0);
701 }
702
703 int
704 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
705 {
706         struct ioctl_map_entry *ne;
707
708         KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
709
710         LIST_FOREACH(ne, &he->map->mapping, entries) {
711                 if (ne->cmd_ranges != he->cmd_ranges)
712                         continue;
713                 LIST_REMOVE(ne, entries);
714                 free(ne, M_IOCTLMAP);
715                 return(0);
716         }
717         return(EINVAL);
718 }
719
720 static int      nselcoll;       /* Select collisions since boot */
721 int     selwait;
722 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
723
724 /*
725  * Select system call.
726  */
727 int
728 select(struct select_args *uap)
729 {
730         struct proc *p = curproc;
731
732         /*
733          * The magic 2048 here is chosen to be just enough for FD_SETSIZE
734          * infds with the new FD_SETSIZE of 1024, and more than enough for
735          * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
736          * of 256.
737          */
738         fd_mask s_selbits[howmany(2048, NFDBITS)];
739         fd_mask *ibits[3], *obits[3], *selbits, *sbp;
740         struct timeval atv, rtv, ttv;
741         int ncoll, error, timo;
742         u_int nbufbytes, ncpbytes, nfdbits;
743
744         if (uap->nd < 0)
745                 return (EINVAL);
746         if (uap->nd > p->p_fd->fd_nfiles)
747                 uap->nd = p->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
748
749         /*
750          * Allocate just enough bits for the non-null fd_sets.  Use the
751          * preallocated auto buffer if possible.
752          */
753         nfdbits = roundup(uap->nd, NFDBITS);
754         ncpbytes = nfdbits / NBBY;
755         nbufbytes = 0;
756         if (uap->in != NULL)
757                 nbufbytes += 2 * ncpbytes;
758         if (uap->ou != NULL)
759                 nbufbytes += 2 * ncpbytes;
760         if (uap->ex != NULL)
761                 nbufbytes += 2 * ncpbytes;
762         if (nbufbytes <= sizeof s_selbits)
763                 selbits = &s_selbits[0];
764         else
765                 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
766
767         /*
768          * Assign pointers into the bit buffers and fetch the input bits.
769          * Put the output buffers together so that they can be bzeroed
770          * together.
771          */
772         sbp = selbits;
773 #define getbits(name, x) \
774         do {                                                            \
775                 if (uap->name == NULL)                                  \
776                         ibits[x] = NULL;                                \
777                 else {                                                  \
778                         ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;   \
779                         obits[x] = sbp;                                 \
780                         sbp += ncpbytes / sizeof *sbp;                  \
781                         error = copyin(uap->name, ibits[x], ncpbytes);  \
782                         if (error != 0)                                 \
783                                 goto done;                              \
784                 }                                                       \
785         } while (0)
786         getbits(in, 0);
787         getbits(ou, 1);
788         getbits(ex, 2);
789 #undef  getbits
790         if (nbufbytes != 0)
791                 bzero(selbits, nbufbytes / 2);
792
793         if (uap->tv) {
794                 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
795                         sizeof (atv));
796                 if (error)
797                         goto done;
798                 if (itimerfix(&atv)) {
799                         error = EINVAL;
800                         goto done;
801                 }
802                 getmicrouptime(&rtv);
803                 timevaladd(&atv, &rtv);
804         } else {
805                 atv.tv_sec = 0;
806                 atv.tv_usec = 0;
807         }
808         timo = 0;
809 retry:
810         ncoll = nselcoll;
811         p->p_flag |= P_SELECT;
812         error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
813         if (error || uap->sysmsg_result)
814                 goto done;
815         if (atv.tv_sec || atv.tv_usec) {
816                 getmicrouptime(&rtv);
817                 if (timevalcmp(&rtv, &atv, >=)) 
818                         goto done;
819                 ttv = atv;
820                 timevalsub(&ttv, &rtv);
821                 timo = ttv.tv_sec > 24 * 60 * 60 ?
822                     24 * 60 * 60 * hz : tvtohz_high(&ttv);
823         }
824         crit_enter();
825         if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
826                 crit_exit();
827                 goto retry;
828         }
829         p->p_flag &= ~P_SELECT;
830
831         error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
832         
833         crit_exit();
834         if (error == 0)
835                 goto retry;
836 done:
837         p->p_flag &= ~P_SELECT;
838         /* select is not restarted after signals... */
839         if (error == ERESTART)
840                 error = EINTR;
841         if (error == EWOULDBLOCK)
842                 error = 0;
843 #define putbits(name, x) \
844         if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
845                 error = error2;
846         if (error == 0) {
847                 int error2;
848
849                 putbits(in, 0);
850                 putbits(ou, 1);
851                 putbits(ex, 2);
852 #undef putbits
853         }
854         if (selbits != &s_selbits[0])
855                 free(selbits, M_SELECT);
856         return (error);
857 }
858
859 static int
860 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
861 {
862         struct filedesc *fdp = p->p_fd;
863         int msk, i, fd;
864         fd_mask bits;
865         struct file *fp;
866         int n = 0;
867         /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
868         static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
869
870         for (msk = 0; msk < 3; msk++) {
871                 if (ibits[msk] == NULL)
872                         continue;
873                 for (i = 0; i < nfd; i += NFDBITS) {
874                         bits = ibits[msk][i/NFDBITS];
875                         /* ffs(int mask) not portable, fd_mask is long */
876                         for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
877                                 if (!(bits & 1))
878                                         continue;
879                                 fp = fdp->fd_files[fd].fp;
880                                 if (fp == NULL)
881                                         return (EBADF);
882                                 if (fo_poll(fp, flag[msk], fp->f_cred)) {
883                                         obits[msk][(fd)/NFDBITS] |=
884                                             ((fd_mask)1 << ((fd) % NFDBITS));
885                                         n++;
886                                 }
887                         }
888                 }
889         }
890         *res = n;
891         return (0);
892 }
893
894 /*
895  * Poll system call.
896  */
897 int
898 poll(struct poll_args *uap)
899 {
900         struct pollfd *bits;
901         struct pollfd smallbits[32];
902         struct timeval atv, rtv, ttv;
903         int ncoll, error = 0, timo;
904         u_int nfds;
905         size_t ni;
906         struct proc *p = curproc;
907
908         nfds = uap->nfds;
909         /*
910          * This is kinda bogus.  We have fd limits, but that is not
911          * really related to the size of the pollfd array.  Make sure
912          * we let the process use at least FD_SETSIZE entries and at
913          * least enough for the current limits.  We want to be reasonably
914          * safe, but not overly restrictive.
915          */
916         if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
917                 return (EINVAL);
918         ni = nfds * sizeof(struct pollfd);
919         if (ni > sizeof(smallbits))
920                 bits = malloc(ni, M_TEMP, M_WAITOK);
921         else
922                 bits = smallbits;
923         error = copyin(uap->fds, bits, ni);
924         if (error)
925                 goto done;
926         if (uap->timeout != INFTIM) {
927                 atv.tv_sec = uap->timeout / 1000;
928                 atv.tv_usec = (uap->timeout % 1000) * 1000;
929                 if (itimerfix(&atv)) {
930                         error = EINVAL;
931                         goto done;
932                 }
933                 getmicrouptime(&rtv);
934                 timevaladd(&atv, &rtv);
935         } else {
936                 atv.tv_sec = 0;
937                 atv.tv_usec = 0;
938         }
939         timo = 0;
940 retry:
941         ncoll = nselcoll;
942         p->p_flag |= P_SELECT;
943         error = pollscan(p, bits, nfds, &uap->sysmsg_result);
944         if (error || uap->sysmsg_result)
945                 goto done;
946         if (atv.tv_sec || atv.tv_usec) {
947                 getmicrouptime(&rtv);
948                 if (timevalcmp(&rtv, &atv, >=))
949                         goto done;
950                 ttv = atv;
951                 timevalsub(&ttv, &rtv);
952                 timo = ttv.tv_sec > 24 * 60 * 60 ?
953                     24 * 60 * 60 * hz : tvtohz_high(&ttv);
954         } 
955         crit_enter();
956         if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
957                 crit_exit();
958                 goto retry;
959         }
960         p->p_flag &= ~P_SELECT;
961         error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
962         crit_exit();
963         if (error == 0)
964                 goto retry;
965 done:
966         p->p_flag &= ~P_SELECT;
967         /* poll is not restarted after signals... */
968         if (error == ERESTART)
969                 error = EINTR;
970         if (error == EWOULDBLOCK)
971                 error = 0;
972         if (error == 0) {
973                 error = copyout(bits, uap->fds, ni);
974                 if (error)
975                         goto out;
976         }
977 out:
978         if (ni > sizeof(smallbits))
979                 free(bits, M_TEMP);
980         return (error);
981 }
982
983 static int
984 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
985 {
986         struct filedesc *fdp = p->p_fd;
987         int i;
988         struct file *fp;
989         int n = 0;
990
991         for (i = 0; i < nfd; i++, fds++) {
992                 if (fds->fd >= fdp->fd_nfiles) {
993                         fds->revents = POLLNVAL;
994                         n++;
995                 } else if (fds->fd < 0) {
996                         fds->revents = 0;
997                 } else {
998                         fp = fdp->fd_files[fds->fd].fp;
999                         if (fp == NULL) {
1000                                 fds->revents = POLLNVAL;
1001                                 n++;
1002                         } else {
1003                                 /*
1004                                  * Note: backend also returns POLLHUP and
1005                                  * POLLERR if appropriate.
1006                                  */
1007                                 fds->revents = fo_poll(fp, fds->events,
1008                                                         fp->f_cred);
1009                                 if (fds->revents != 0)
1010                                         n++;
1011                         }
1012                 }
1013         }
1014         *res = n;
1015         return (0);
1016 }
1017
1018 /*
1019  * OpenBSD poll system call.
1020  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
1021  */
1022 int
1023 openbsd_poll(struct openbsd_poll_args *uap)
1024 {
1025         return (poll((struct poll_args *)uap));
1026 }
1027
1028 /*ARGSUSED*/
1029 int
1030 seltrue(dev_t dev, int events, struct thread *td)
1031 {
1032         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1033 }
1034
1035 /*
1036  * Record a select request.  A global wait must be used since a process/thread
1037  * might go away after recording its request.
1038  */
1039 void
1040 selrecord(struct thread *selector, struct selinfo *sip)
1041 {
1042         struct proc *p;
1043         pid_t mypid;
1044
1045         if ((p = selector->td_proc) == NULL)
1046                 panic("selrecord: thread needs a process");
1047
1048         mypid = p->p_pid;
1049         if (sip->si_pid == mypid)
1050                 return;
1051         if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1052             p->p_wchan == (caddr_t)&selwait) {
1053                 sip->si_flags |= SI_COLL;
1054         } else {
1055                 sip->si_pid = mypid;
1056         }
1057 }
1058
1059 /*
1060  * Do a wakeup when a selectable event occurs.
1061  */
1062 void
1063 selwakeup(struct selinfo *sip)
1064 {
1065         struct proc *p;
1066
1067         if (sip->si_pid == 0)
1068                 return;
1069         if (sip->si_flags & SI_COLL) {
1070                 nselcoll++;
1071                 sip->si_flags &= ~SI_COLL;
1072                 wakeup((caddr_t)&selwait);      /* YYY fixable */
1073         }
1074         p = pfind(sip->si_pid);
1075         sip->si_pid = 0;
1076         if (p != NULL) {
1077                 crit_enter();
1078                 if (p->p_wchan == (caddr_t)&selwait) {
1079                         /*
1080                          * Flag the process to break the tsleep when 
1081                          * setrunnable is called, but only call setrunnable
1082                          * here if the process is not in a stopped state.
1083                          */
1084                         p->p_flag |= P_BREAKTSLEEP;
1085                         if ((p->p_flag & P_STOPPED) == 0)
1086                                 setrunnable(p);
1087                 } else if (p->p_flag & P_SELECT) {
1088                         p->p_flag &= ~P_SELECT;
1089                 }
1090                 crit_exit();
1091         }
1092 }
1093