source: rtems-libbsd/freebsd/sys/kern/sys_generic.c @ bceabc9

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since bceabc9 was bceabc9, checked in by Sebastian Huber <sebastian.huber@…>, on 10/09/13 at 20:42:09

Move files to match FreeBSD layout

  • Property mode set to 100644
File size: 35.8 KB
Line 
1#include <freebsd/machine/rtems-bsd-config.h>
2
3/*-
4 * Copyright (c) 1982, 1986, 1989, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *      @(#)sys_generic.c       8.5 (Berkeley) 1/21/94
37 */
38
39#include <freebsd/sys/cdefs.h>
40__FBSDID("$FreeBSD$");
41
42
43#include <freebsd/local/opt_compat.h>
44#include <freebsd/local/opt_ktrace.h>
45
46#include <freebsd/sys/param.h>
47#include <freebsd/sys/systm.h>
48#include <freebsd/sys/sysproto.h>
49#include <freebsd/sys/filedesc.h>
50#include <freebsd/sys/filio.h>
51#include <freebsd/sys/fcntl.h>
52#include <freebsd/sys/file.h>
53#include <freebsd/sys/proc.h>
54#include <freebsd/sys/signalvar.h>
55#include <freebsd/sys/socketvar.h>
56#include <freebsd/sys/uio.h>
57#include <freebsd/sys/kernel.h>
58#include <freebsd/sys/ktr.h>
59#include <freebsd/sys/limits.h>
60#include <freebsd/sys/malloc.h>
61#include <freebsd/sys/poll.h>
62#include <freebsd/sys/resourcevar.h>
63#include <freebsd/sys/selinfo.h>
64#include <freebsd/sys/sleepqueue.h>
65#include <freebsd/sys/syscallsubr.h>
66#include <freebsd/sys/sysctl.h>
67#include <freebsd/sys/sysent.h>
68#include <freebsd/sys/vnode.h>
69#include <freebsd/sys/bio.h>
70#ifndef __rtems__
71#include <freebsd/sys/buf.h>
72#endif
73#include <freebsd/sys/condvar.h>
74#ifdef KTRACE
75#include <freebsd/sys/ktrace.h>
76#endif
77
78#include <freebsd/security/audit/audit.h>
79
80#ifdef __rtems__
81typedef long fd_mask;
82#include <freebsd/vm/uma.h>
83#include <freebsd/sys/mutex.h>
84#include <freebsd/machine/rtems-bsd-symbols.h>
85#endif /* __rtems__ */
86
87static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
88static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
89#ifndef __rtems__
90MALLOC_DEFINE(M_IOV, "iov", "large iov's");
91#endif /* __rtems__ */
92
93static int      pollout(struct thread *, struct pollfd *, struct pollfd *,
94                    u_int);
95static int      pollscan(struct thread *, struct pollfd *, u_int);
96static int      pollrescan(struct thread *);
97static int      selscan(struct thread *, fd_mask **, fd_mask **, int);
98static int      selrescan(struct thread *, fd_mask **, fd_mask **);
99static void     selfdalloc(struct thread *, void *);
100static void     selfdfree(struct seltd *, struct selfd *);
101static int      dofileread(struct thread *, int, struct file *, struct uio *,
102                    off_t, int);
103static int      dofilewrite(struct thread *, int, struct file *, struct uio *,
104                    off_t, int);
105static void     doselwakeup(struct selinfo *, int);
106static void     seltdinit(struct thread *);
107static int      seltdwait(struct thread *, int);
108static void     seltdclear(struct thread *);
109
110/*
111 * One seltd per-thread allocated on demand as needed.
112 *
113 *      t - protected by st_mtx
114 *      k - Only accessed by curthread or read-only
115 */
116struct seltd {
117        STAILQ_HEAD(, selfd)    st_selq;        /* (k) List of selfds. */
118        struct selfd            *st_free1;      /* (k) free fd for read set. */
119        struct selfd            *st_free2;      /* (k) free fd for write set. */
120        struct mtx              st_mtx;         /* Protects struct seltd */
121        struct cv               st_wait;        /* (t) Wait channel. */
122        int                     st_flags;       /* (t) SELTD_ flags. */
123};
124
125#define SELTD_PENDING   0x0001                  /* We have pending events. */
126#define SELTD_RESCAN    0x0002                  /* Doing a rescan. */
127
128/*
129 * One selfd allocated per-thread per-file-descriptor.
130 *      f - protected by sf_mtx
131 */
132struct selfd {
133        STAILQ_ENTRY(selfd)     sf_link;        /* (k) fds owned by this td. */
134        TAILQ_ENTRY(selfd)      sf_threads;     /* (f) fds on this selinfo. */
135        struct selinfo          *sf_si;         /* (f) selinfo when linked. */
136        struct mtx              *sf_mtx;        /* Pointer to selinfo mtx. */
137        struct seltd            *sf_td;         /* (k) owning seltd. */
138        void                    *sf_cookie;     /* (k) fd or pollfd. */
139};
140
141static uma_zone_t selfd_zone;
142static struct mtx_pool *mtxpool_select;
143
144#ifndef _SYS_SYSPROTO_H_
145struct read_args {
146        int     fd;
147        void    *buf;
148        size_t  nbyte;
149};
150#endif
151#ifndef __rtems__
152int
153read(td, uap)
154        struct thread *td;
155        struct read_args *uap;
156{
157        struct uio auio;
158        struct iovec aiov;
159        int error;
160
161        if (uap->nbyte > INT_MAX)
162                return (EINVAL);
163        aiov.iov_base = uap->buf;
164        aiov.iov_len = uap->nbyte;
165        auio.uio_iov = &aiov;
166        auio.uio_iovcnt = 1;
167        auio.uio_resid = uap->nbyte;
168        auio.uio_segflg = UIO_USERSPACE;
169        error = kern_readv(td, uap->fd, &auio);
170        return(error);
171}
172#endif
173
174/*
175 * Positioned read system call
176 */
177#ifndef _SYS_SYSPROTO_H_
178struct pread_args {
179        int     fd;
180        void    *buf;
181        size_t  nbyte;
182        int     pad;
183        off_t   offset;
184};
185#endif
186int
187pread(td, uap)
188        struct thread *td;
189        struct pread_args *uap;
190{
191        struct uio auio;
192        struct iovec aiov;
193        int error;
194
195        if (uap->nbyte > INT_MAX)
196                return (EINVAL);
197        aiov.iov_base = uap->buf;
198        aiov.iov_len = uap->nbyte;
199        auio.uio_iov = &aiov;
200        auio.uio_iovcnt = 1;
201        auio.uio_resid = uap->nbyte;
202        auio.uio_segflg = UIO_USERSPACE;
203        error = kern_preadv(td, uap->fd, &auio, uap->offset);
204        return(error);
205}
206
207#ifndef __rtems__
208int
209freebsd6_pread(td, uap)
210        struct thread *td;
211        struct freebsd6_pread_args *uap;
212{
213        struct pread_args oargs;
214
215        oargs.fd = uap->fd;
216        oargs.buf = uap->buf;
217        oargs.nbyte = uap->nbyte;
218        oargs.offset = uap->offset;
219        return (pread(td, &oargs));
220}
221#endif /* __rtems__ */
222
223/*
224 * Scatter read system call.
225 */
226#ifndef _SYS_SYSPROTO_H_
227struct readv_args {
228        int     fd;
229        struct  iovec *iovp;
230        u_int   iovcnt;
231};
232#endif
233int
234readv(struct thread *td, struct readv_args *uap)
235{
236        struct uio *auio;
237        int error;
238
239        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
240        if (error)
241                return (error);
242        error = kern_readv(td, uap->fd, auio);
243        free(auio, M_IOV);
244        return (error);
245}
246
247int
248kern_readv(struct thread *td, int fd, struct uio *auio)
249{
250        struct file *fp;
251        int error;
252
253        error = fget_read(td, fd, &fp);
254        if (error)
255                return (error);
256        error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
257        fdrop(fp, td);
258        return (error);
259}
260
261/*
262 * Scatter positioned read system call.
263 */
264#ifndef _SYS_SYSPROTO_H_
265struct preadv_args {
266        int     fd;
267        struct  iovec *iovp;
268        u_int   iovcnt;
269        off_t   offset;
270};
271#endif
272int
273preadv(struct thread *td, struct preadv_args *uap)
274{
275        struct uio *auio;
276        int error;
277
278        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
279        if (error)
280                return (error);
281        error = kern_preadv(td, uap->fd, auio, uap->offset);
282        free(auio, M_IOV);
283        return (error);
284}
285
286int
287kern_preadv(td, fd, auio, offset)
288        struct thread *td;
289        int fd;
290        struct uio *auio;
291        off_t offset;
292{
293        struct file *fp;
294        int error;
295
296        error = fget_read(td, fd, &fp);
297        if (error)
298                return (error);
299        if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
300                error = ESPIPE;
301#ifndef __rtems__
302        else if (offset < 0 && fp->f_vnode->v_type != VCHR)
303                error = EINVAL;
304#endif /* __rtems__ */
305        else
306                error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
307        fdrop(fp, td);
308        return (error);
309}
310
311/*
312 * Common code for readv and preadv that reads data in
313 * from a file using the passed in uio, offset, and flags.
314 */
315static int
316dofileread(td, fd, fp, auio, offset, flags)
317        struct thread *td;
318        int fd;
319        struct file *fp;
320        struct uio *auio;
321        off_t offset;
322        int flags;
323{
324        ssize_t cnt;
325        int error;
326#ifdef KTRACE
327        struct uio *ktruio = NULL;
328#endif
329
330        /* Finish zero length reads right here */
331        if (auio->uio_resid == 0) {
332                td->td_retval[0] = 0;
333                return(0);
334        }
335        auio->uio_rw = UIO_READ;
336        auio->uio_offset = offset;
337        auio->uio_td = td;
338#ifdef KTRACE
339        if (KTRPOINT(td, KTR_GENIO))
340                ktruio = cloneuio(auio);
341#endif
342        cnt = auio->uio_resid;
343        if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
344                if (auio->uio_resid != cnt && (error == ERESTART ||
345                    error == EINTR || error == EWOULDBLOCK))
346                        error = 0;
347        }
348        cnt -= auio->uio_resid;
349#ifdef KTRACE
350        if (ktruio != NULL) {
351                ktruio->uio_resid = cnt;
352                ktrgenio(fd, UIO_READ, ktruio, error);
353        }
354#endif
355        td->td_retval[0] = cnt;
356        return (error);
357}
358
359#ifndef __rtems__
360#ifndef _SYS_SYSPROTO_H_
361struct write_args {
362        int     fd;
363        const void *buf;
364        size_t  nbyte;
365};
366#endif
367int
368write(td, uap)
369        struct thread *td;
370        struct write_args *uap;
371{
372        struct uio auio;
373        struct iovec aiov;
374        int error;
375
376        if (uap->nbyte > INT_MAX)
377                return (EINVAL);
378        aiov.iov_base = (void *)(uintptr_t)uap->buf;
379        aiov.iov_len = uap->nbyte;
380        auio.uio_iov = &aiov;
381        auio.uio_iovcnt = 1;
382        auio.uio_resid = uap->nbyte;
383        auio.uio_segflg = UIO_USERSPACE;
384        error = kern_writev(td, uap->fd, &auio);
385        return(error);
386}
387
388/*
389 * Positioned write system call.
390 */
391#ifndef _SYS_SYSPROTO_H_
392struct pwrite_args {
393        int     fd;
394        const void *buf;
395        size_t  nbyte;
396        int     pad;
397        off_t   offset;
398};
399#endif
400int
401pwrite(td, uap)
402        struct thread *td;
403        struct pwrite_args *uap;
404{
405        struct uio auio;
406        struct iovec aiov;
407        int error;
408
409        if (uap->nbyte > INT_MAX)
410                return (EINVAL);
411        aiov.iov_base = (void *)(uintptr_t)uap->buf;
412        aiov.iov_len = uap->nbyte;
413        auio.uio_iov = &aiov;
414        auio.uio_iovcnt = 1;
415        auio.uio_resid = uap->nbyte;
416        auio.uio_segflg = UIO_USERSPACE;
417        error = kern_pwritev(td, uap->fd, &auio, uap->offset);
418        return(error);
419}
420
421int
422freebsd6_pwrite(td, uap)
423        struct thread *td;
424        struct freebsd6_pwrite_args *uap;
425{
426        struct pwrite_args oargs;
427
428        oargs.fd = uap->fd;
429        oargs.buf = uap->buf;
430        oargs.nbyte = uap->nbyte;
431        oargs.offset = uap->offset;
432        return (pwrite(td, &oargs));
433}
434
435/*
436 * Gather write system call.
437 */
438#ifndef _SYS_SYSPROTO_H_
439struct writev_args {
440        int     fd;
441        struct  iovec *iovp;
442        u_int   iovcnt;
443};
444#endif
445int
446writev(struct thread *td, struct writev_args *uap)
447{
448        struct uio *auio;
449        int error;
450
451        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
452        if (error)
453                return (error);
454        error = kern_writev(td, uap->fd, auio);
455        free(auio, M_IOV);
456        return (error);
457}
458
459int
460kern_writev(struct thread *td, int fd, struct uio *auio)
461{
462        struct file *fp;
463        int error;
464
465        error = fget_write(td, fd, &fp);
466        if (error)
467                return (error);
468        error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
469        fdrop(fp, td);
470        return (error);
471}
472
473/*
474 * Gather positioned write system call.
475 */
476#ifndef _SYS_SYSPROTO_H_
477struct pwritev_args {
478        int     fd;
479        struct  iovec *iovp;
480        u_int   iovcnt;
481        off_t   offset;
482};
483#endif
484int
485pwritev(struct thread *td, struct pwritev_args *uap)
486{
487        struct uio *auio;
488        int error;
489
490        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
491        if (error)
492                return (error);
493        error = kern_pwritev(td, uap->fd, auio, uap->offset);
494        free(auio, M_IOV);
495        return (error);
496}
497
498int
499kern_pwritev(td, fd, auio, offset)
500        struct thread *td;
501        struct uio *auio;
502        int fd;
503        off_t offset;
504{
505        struct file *fp;
506        int error;
507
508        error = fget_write(td, fd, &fp);
509        if (error)
510                return (error);
511        if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
512                error = ESPIPE;
513        else if (offset < 0 && fp->f_vnode->v_type != VCHR)
514                error = EINVAL;
515        else
516                error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
517        fdrop(fp, td);
518        return (error);
519}
520
521/*
522 * Common code for writev and pwritev that writes data to
523 * a file using the passed in uio, offset, and flags.
524 */
525static int
526dofilewrite(td, fd, fp, auio, offset, flags)
527        struct thread *td;
528        int fd;
529        struct file *fp;
530        struct uio *auio;
531        off_t offset;
532        int flags;
533{
534        ssize_t cnt;
535        int error;
536#ifdef KTRACE
537        struct uio *ktruio = NULL;
538#endif
539
540        auio->uio_rw = UIO_WRITE;
541        auio->uio_td = td;
542        auio->uio_offset = offset;
543#ifdef KTRACE
544        if (KTRPOINT(td, KTR_GENIO))
545                ktruio = cloneuio(auio);
546#endif
547        cnt = auio->uio_resid;
548        if (fp->f_type == DTYPE_VNODE)
549                bwillwrite();
550        if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
551                if (auio->uio_resid != cnt && (error == ERESTART ||
552                    error == EINTR || error == EWOULDBLOCK))
553                        error = 0;
554                /* Socket layer is responsible for issuing SIGPIPE. */
555                if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
556                        PROC_LOCK(td->td_proc);
557                        tdksignal(td, SIGPIPE, NULL);
558                        PROC_UNLOCK(td->td_proc);
559                }
560        }
561        cnt -= auio->uio_resid;
562#ifdef KTRACE
563        if (ktruio != NULL) {
564                ktruio->uio_resid = cnt;
565                ktrgenio(fd, UIO_WRITE, ktruio, error);
566        }
567#endif
568        td->td_retval[0] = cnt;
569        return (error);
570}
571
572/*
573 * Truncate a file given a file descriptor.
574 *
575 * Can't use fget_write() here, since must return EINVAL and not EBADF if the
576 * descriptor isn't writable.
577 */
578int
579kern_ftruncate(td, fd, length)
580        struct thread *td;
581        int fd;
582        off_t length;
583{
584        struct file *fp;
585        int error;
586
587        AUDIT_ARG_FD(fd);
588        if (length < 0)
589                return (EINVAL);
590        error = fget(td, fd, &fp);
591        if (error)
592                return (error);
593        AUDIT_ARG_FILE(td->td_proc, fp);
594        if (!(fp->f_flag & FWRITE)) {
595                fdrop(fp, td);
596                return (EINVAL);
597        }
598        error = fo_truncate(fp, length, td->td_ucred, td);
599        fdrop(fp, td);
600        return (error);
601}
602
603#ifndef _SYS_SYSPROTO_H_
604struct ftruncate_args {
605        int     fd;
606        int     pad;
607        off_t   length;
608};
609#endif
610int
611ftruncate(td, uap)
612        struct thread *td;
613        struct ftruncate_args *uap;
614{
615
616        return (kern_ftruncate(td, uap->fd, uap->length));
617}
618
619#if defined(COMPAT_43)
620#ifndef _SYS_SYSPROTO_H_
621struct oftruncate_args {
622        int     fd;
623        long    length;
624};
625#endif
626int
627oftruncate(td, uap)
628        struct thread *td;
629        struct oftruncate_args *uap;
630{
631
632        return (kern_ftruncate(td, uap->fd, uap->length));
633}
634#endif /* COMPAT_43 */
635
636#ifndef _SYS_SYSPROTO_H_
637struct ioctl_args {
638        int     fd;
639        u_long  com;
640        caddr_t data;
641};
642#endif
643/* ARGSUSED */
644int
645ioctl(struct thread *td, struct ioctl_args *uap)
646{
647        u_long com;
648        int arg, error;
649        u_int size;
650        caddr_t data;
651
652        if (uap->com > 0xffffffff) {
653                printf(
654                    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
655                    td->td_proc->p_pid, td->td_name, uap->com);
656                uap->com &= 0xffffffff;
657        }
658        com = uap->com;
659
660        /*
661         * Interpret high order word to find amount of data to be
662         * copied to/from the user's address space.
663         */
664        size = IOCPARM_LEN(com);
665        if ((size > IOCPARM_MAX) ||
666            ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
667#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
668            ((com & IOC_OUT) && size == 0) ||
669#else
670            ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
671#endif
672            ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
673                return (ENOTTY);
674
675        if (size > 0) {
676                if (com & IOC_VOID) {
677                        /* Integer argument. */
678                        arg = (intptr_t)uap->data;
679                        data = (void *)&arg;
680                        size = 0;
681                } else
682                        data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
683        } else
684                data = (void *)&uap->data;
685        if (com & IOC_IN) {
686                error = copyin(uap->data, data, (u_int)size);
687                if (error) {
688                        if (size > 0)
689                                free(data, M_IOCTLOPS);
690                        return (error);
691                }
692        } else if (com & IOC_OUT) {
693                /*
694                 * Zero the buffer so the user always
695                 * gets back something deterministic.
696                 */
697                bzero(data, size);
698        }
699
700        error = kern_ioctl(td, uap->fd, com, data);
701
702        if (error == 0 && (com & IOC_OUT))
703                error = copyout(data, uap->data, (u_int)size);
704
705        if (size > 0)
706                free(data, M_IOCTLOPS);
707        return (error);
708}
709
710int
711kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
712{
713        struct file *fp;
714        struct filedesc *fdp;
715        int error;
716        int tmp;
717
718        AUDIT_ARG_FD(fd);
719        AUDIT_ARG_CMD(com);
720        if ((error = fget(td, fd, &fp)) != 0)
721                return (error);
722        if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
723                fdrop(fp, td);
724                return (EBADF);
725        }
726        fdp = td->td_proc->p_fd;
727        switch (com) {
728        case FIONCLEX:
729                FILEDESC_XLOCK(fdp);
730                fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
731                FILEDESC_XUNLOCK(fdp);
732                goto out;
733        case FIOCLEX:
734                FILEDESC_XLOCK(fdp);
735                fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
736                FILEDESC_XUNLOCK(fdp);
737                goto out;
738        case FIONBIO:
739                if ((tmp = *(int *)data))
740                        atomic_set_int(&fp->f_flag, FNONBLOCK);
741                else
742                        atomic_clear_int(&fp->f_flag, FNONBLOCK);
743                data = (void *)&tmp;
744                break;
745        case FIOASYNC:
746                if ((tmp = *(int *)data))
747                        atomic_set_int(&fp->f_flag, FASYNC);
748                else
749                        atomic_clear_int(&fp->f_flag, FASYNC);
750                data = (void *)&tmp;
751                break;
752        }
753
754        error = fo_ioctl(fp, com, data, td->td_ucred, td);
755out:
756        fdrop(fp, td);
757        return (error);
758}
759
760int
761poll_no_poll(int events)
762{
763        /*
764         * Return true for read/write.  If the user asked for something
765         * special, return POLLNVAL, so that clients have a way of
766         * determining reliably whether or not the extended
767         * functionality is present without hard-coding knowledge
768         * of specific filesystem implementations.
769         */
770        if (events & ~POLLSTANDARD)
771                return (POLLNVAL);
772
773        return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
774}
775
776int
777pselect(struct thread *td, struct pselect_args *uap)
778{
779        struct timespec ts;
780        struct timeval tv, *tvp;
781        sigset_t set, *uset;
782        int error;
783
784        if (uap->ts != NULL) {
785                error = copyin(uap->ts, &ts, sizeof(ts));
786                if (error != 0)
787                    return (error);
788                TIMESPEC_TO_TIMEVAL(&tv, &ts);
789                tvp = &tv;
790        } else
791                tvp = NULL;
792        if (uap->sm != NULL) {
793                error = copyin(uap->sm, &set, sizeof(set));
794                if (error != 0)
795                        return (error);
796                uset = &set;
797        } else
798                uset = NULL;
799        return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
800            uset, NFDBITS));
801}
802
803int
804kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
805    struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
806{
807        int error;
808
809        if (uset != NULL) {
810                error = kern_sigprocmask(td, SIG_SETMASK, uset,
811                    &td->td_oldsigmask, 0);
812                if (error != 0)
813                        return (error);
814                td->td_pflags |= TDP_OLDMASK;
815                /*
816                 * Make sure that ast() is called on return to
817                 * usermode and TDP_OLDMASK is cleared, restoring old
818                 * sigmask.
819                 */
820                thread_lock(td);
821                td->td_flags |= TDF_ASTPENDING;
822                thread_unlock(td);
823        }
824        error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
825        return (error);
826}
827
828#ifndef _SYS_SYSPROTO_H_
829struct select_args {
830        int     nd;
831        fd_set  *in, *ou, *ex;
832        struct  timeval *tv;
833};
834#endif
835int
836select(struct thread *td, struct select_args *uap)
837{
838        struct timeval tv, *tvp;
839        int error;
840
841        if (uap->tv != NULL) {
842                error = copyin(uap->tv, &tv, sizeof(tv));
843                if (error)
844                        return (error);
845                tvp = &tv;
846        } else
847                tvp = NULL;
848
849        return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
850            NFDBITS));
851}
852
853int
854kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
855    fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
856{
857        struct filedesc *fdp;
858        /*
859         * The magic 2048 here is chosen to be just enough for FD_SETSIZE
860         * infds with the new FD_SETSIZE of 1024, and more than enough for
861         * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
862         * of 256.
863         */
864        fd_mask s_selbits[howmany(2048, NFDBITS)];
865        fd_mask *ibits[3], *obits[3], *selbits, *sbp;
866        struct timeval atv, rtv, ttv;
867        int error, timo;
868        u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
869
870        if (nd < 0)
871                return (EINVAL);
872        fdp = td->td_proc->p_fd;
873        if (nd > fdp->fd_lastfile + 1)
874                nd = fdp->fd_lastfile + 1;
875
876        /*
877         * Allocate just enough bits for the non-null fd_sets.  Use the
878         * preallocated auto buffer if possible.
879         */
880        nfdbits = roundup(nd, NFDBITS);
881        ncpbytes = nfdbits / NBBY;
882        ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
883        nbufbytes = 0;
884        if (fd_in != NULL)
885                nbufbytes += 2 * ncpbytes;
886        if (fd_ou != NULL)
887                nbufbytes += 2 * ncpbytes;
888        if (fd_ex != NULL)
889                nbufbytes += 2 * ncpbytes;
890        if (nbufbytes <= sizeof s_selbits)
891                selbits = &s_selbits[0];
892        else
893                selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
894
895        /*
896         * Assign pointers into the bit buffers and fetch the input bits.
897         * Put the output buffers together so that they can be bzeroed
898         * together.
899         */
900        sbp = selbits;
901#define getbits(name, x) \
902        do {                                                            \
903                if (name == NULL) {                                     \
904                        ibits[x] = NULL;                                \
905                        obits[x] = NULL;                                \
906                } else {                                                \
907                        ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;   \
908                        obits[x] = sbp;                                 \
909                        sbp += ncpbytes / sizeof *sbp;                  \
910                        error = copyin(name, ibits[x], ncpubytes);      \
911                        if (error != 0)                                 \
912                                goto done;                              \
913                        bzero((char *)ibits[x] + ncpubytes,             \
914                            ncpbytes - ncpubytes);                      \
915                }                                                       \
916        } while (0)
917        getbits(fd_in, 0);
918        getbits(fd_ou, 1);
919        getbits(fd_ex, 2);
920#undef  getbits
921
922#if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
923        /*
924         * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
925         * we are running under 32-bit emulation. This should be more
926         * generic.
927         */
928#define swizzle_fdset(bits)                                             \
929        if (abi_nfdbits != NFDBITS && bits != NULL) {                   \
930                int i;                                                  \
931                for (i = 0; i < ncpbytes / sizeof *sbp; i++)            \
932                        bits[i] = (bits[i] >> 32) | (bits[i] << 32);    \
933        }
934#else
935#define swizzle_fdset(bits)
936#endif
937
938        /* Make sure the bit order makes it through an ABI transition */
939        swizzle_fdset(ibits[0]);
940        swizzle_fdset(ibits[1]);
941        swizzle_fdset(ibits[2]);
942       
943        if (nbufbytes != 0)
944                bzero(selbits, nbufbytes / 2);
945
946        if (tvp != NULL) {
947                atv = *tvp;
948                if (itimerfix(&atv)) {
949                        error = EINVAL;
950                        goto done;
951                }
952                getmicrouptime(&rtv);
953                timevaladd(&atv, &rtv);
954        } else {
955                atv.tv_sec = 0;
956                atv.tv_usec = 0;
957        }
958        timo = 0;
959        seltdinit(td);
960        /* Iterate until the timeout expires or descriptors become ready. */
961        for (;;) {
962                error = selscan(td, ibits, obits, nd);
963                if (error || td->td_retval[0] != 0)
964                        break;
965                if (atv.tv_sec || atv.tv_usec) {
966                        getmicrouptime(&rtv);
967                        if (timevalcmp(&rtv, &atv, >=))
968                                break;
969                        ttv = atv;
970                        timevalsub(&ttv, &rtv);
971                        timo = ttv.tv_sec > 24 * 60 * 60 ?
972                            24 * 60 * 60 * hz : tvtohz(&ttv);
973                }
974                error = seltdwait(td, timo);
975                if (error)
976                        break;
977                error = selrescan(td, ibits, obits);
978                if (error || td->td_retval[0] != 0)
979                        break;
980        }
981        seltdclear(td);
982
983done:
984        /* select is not restarted after signals... */
985        if (error == ERESTART)
986                error = EINTR;
987        if (error == EWOULDBLOCK)
988                error = 0;
989
990        /* swizzle bit order back, if necessary */
991        swizzle_fdset(obits[0]);
992        swizzle_fdset(obits[1]);
993        swizzle_fdset(obits[2]);
994#undef swizzle_fdset
995
996#define putbits(name, x) \
997        if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
998                error = error2;
999        if (error == 0) {
1000                int error2;
1001
1002                putbits(fd_in, 0);
1003                putbits(fd_ou, 1);
1004                putbits(fd_ex, 2);
1005#undef putbits
1006        }
1007        if (selbits != &s_selbits[0])
1008                free(selbits, M_SELECT);
1009
1010        return (error);
1011}
1012/*
1013 * Convert a select bit set to poll flags.
1014 *
1015 * The backend always returns POLLHUP/POLLERR if appropriate and we
1016 * return this as a set bit in any set.
1017 */
1018static int select_flags[3] = {
1019    POLLRDNORM | POLLHUP | POLLERR,
1020    POLLWRNORM | POLLHUP | POLLERR,
1021    POLLRDBAND | POLLERR
1022};
1023
1024/*
1025 * Compute the fo_poll flags required for a fd given by the index and
1026 * bit position in the fd_mask array.
1027 */
1028static __inline int
1029selflags(fd_mask **ibits, int idx, fd_mask bit)
1030{
1031        int flags;
1032        int msk;
1033
1034        flags = 0;
1035        for (msk = 0; msk < 3; msk++) {
1036                if (ibits[msk] == NULL)
1037                        continue;
1038                if ((ibits[msk][idx] & bit) == 0)
1039                        continue;
1040                flags |= select_flags[msk];
1041        }
1042        return (flags);
1043}
1044
1045/*
1046 * Set the appropriate output bits given a mask of fired events and the
1047 * input bits originally requested.
1048 */
1049static __inline int
1050selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1051{
1052        int msk;
1053        int n;
1054
1055        n = 0;
1056        for (msk = 0; msk < 3; msk++) {
1057                if ((events & select_flags[msk]) == 0)
1058                        continue;
1059                if (ibits[msk] == NULL)
1060                        continue;
1061                if ((ibits[msk][idx] & bit) == 0)
1062                        continue;
1063                /*
1064                 * XXX Check for a duplicate set.  This can occur because a
1065                 * socket calls selrecord() twice for each poll() call
1066                 * resulting in two selfds per real fd.  selrescan() will
1067                 * call selsetbits twice as a result.
1068                 */
1069                if ((obits[msk][idx] & bit) != 0)
1070                        continue;
1071                obits[msk][idx] |= bit;
1072                n++;
1073        }
1074
1075        return (n);
1076}
1077
1078/*
1079 * Traverse the list of fds attached to this thread's seltd and check for
1080 * completion.
1081 */
1082static int
1083selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1084{
1085        struct filedesc *fdp;
1086        struct selinfo *si;
1087        struct seltd *stp;
1088        struct selfd *sfp;
1089        struct selfd *sfn;
1090        struct file *fp;
1091        fd_mask bit;
1092        int fd, ev, n, idx;
1093
1094        fdp = td->td_proc->p_fd;
1095        stp = td->td_sel;
1096        n = 0;
1097        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1098                fd = (int)(uintptr_t)sfp->sf_cookie;
1099                si = sfp->sf_si;
1100                selfdfree(stp, sfp);
1101                /* If the selinfo wasn't cleared the event didn't fire. */
1102                if (si != NULL)
1103                        continue;
1104                if ((fp = fget_unlocked(fdp, fd)) == NULL)
1105                        return (EBADF);
1106                idx = fd / NFDBITS;
1107                bit = (fd_mask)1 << (fd % NFDBITS);
1108                ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1109                fdrop(fp, td);
1110                if (ev != 0)
1111                        n += selsetbits(ibits, obits, idx, bit, ev);
1112        }
1113        stp->st_flags = 0;
1114        td->td_retval[0] = n;
1115        return (0);
1116}
1117
1118/*
1119 * Perform the initial filedescriptor scan and register ourselves with
1120 * each selinfo.
1121 */
1122static int
1123selscan(td, ibits, obits, nfd)
1124        struct thread *td;
1125        fd_mask **ibits, **obits;
1126        int nfd;
1127{
1128        struct filedesc *fdp;
1129        struct file *fp;
1130        fd_mask bit;
1131        int ev, flags, end, fd;
1132        int n, idx;
1133
1134        fdp = td->td_proc->p_fd;
1135        n = 0;
1136        for (idx = 0, fd = 0; fd < nfd; idx++) {
1137                end = imin(fd + NFDBITS, nfd);
1138                for (bit = 1; fd < end; bit <<= 1, fd++) {
1139                        /* Compute the list of events we're interested in. */
1140                        flags = selflags(ibits, idx, bit);
1141                        if (flags == 0)
1142                                continue;
1143                        if ((fp = fget_unlocked(fdp, fd)) == NULL)
1144                                return (EBADF);
1145                        selfdalloc(td, (void *)(uintptr_t)fd);
1146                        ev = fo_poll(fp, flags, td->td_ucred, td);
1147                        fdrop(fp, td);
1148                        if (ev != 0)
1149                                n += selsetbits(ibits, obits, idx, bit, ev);
1150                }
1151        }
1152
1153        td->td_retval[0] = n;
1154        return (0);
1155}
1156#endif /* __rtems__ */
1157
1158#ifndef _SYS_SYSPROTO_H_
1159struct poll_args {
1160        struct pollfd *fds;
1161        u_int   nfds;
1162        int     timeout;
1163};
1164#endif
1165int
1166#ifdef __rtems__
1167kern_poll(td, uap)
1168#else
1169poll(td, uap)
1170#endif /* __rtems__ */
1171        struct thread *td;
1172        struct poll_args *uap;
1173{
1174        struct pollfd *bits;
1175        struct pollfd smallbits[32];
1176        struct timeval atv, rtv, ttv;
1177        int error = 0, timo;
1178        u_int nfds;
1179        size_t ni;
1180
1181        nfds = uap->nfds;
1182        if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1183                return (EINVAL);
1184        ni = nfds * sizeof(struct pollfd);
1185        if (ni > sizeof(smallbits))
1186                bits = malloc(ni, M_TEMP, M_WAITOK);
1187        else
1188                bits = smallbits;
1189        error = copyin(uap->fds, bits, ni);
1190        if (error)
1191                goto done;
1192        if (uap->timeout != INFTIM) {
1193                atv.tv_sec = uap->timeout / 1000;
1194                atv.tv_usec = (uap->timeout % 1000) * 1000;
1195                if (itimerfix(&atv)) {
1196                        error = EINVAL;
1197                        goto done;
1198                }
1199                getmicrouptime(&rtv);
1200                timevaladd(&atv, &rtv);
1201        } else {
1202                atv.tv_sec = 0;
1203                atv.tv_usec = 0;
1204        }
1205        timo = 0;
1206        seltdinit(td);
1207        /* Iterate until the timeout expires or descriptors become ready. */
1208        for (;;) {
1209                error = pollscan(td, bits, nfds);
1210                if (error || td->td_retval[0] != 0)
1211                        break;
1212                if (atv.tv_sec || atv.tv_usec) {
1213                        getmicrouptime(&rtv);
1214                        if (timevalcmp(&rtv, &atv, >=))
1215                                break;
1216                        ttv = atv;
1217                        timevalsub(&ttv, &rtv);
1218                        timo = ttv.tv_sec > 24 * 60 * 60 ?
1219                            24 * 60 * 60 * hz : tvtohz(&ttv);
1220                }
1221                error = seltdwait(td, timo);
1222                if (error)
1223                        break;
1224                error = pollrescan(td);
1225                if (error || td->td_retval[0] != 0)
1226                        break;
1227        }
1228        seltdclear(td);
1229
1230done:
1231        /* poll is not restarted after signals... */
1232        if (error == ERESTART)
1233                error = EINTR;
1234        if (error == EWOULDBLOCK)
1235                error = 0;
1236        if (error == 0) {
1237                error = pollout(td, bits, uap->fds, nfds);
1238                if (error)
1239                        goto out;
1240        }
1241out:
1242        if (ni > sizeof(smallbits))
1243                free(bits, M_TEMP);
1244        return (error);
1245}
1246
1247static int
1248pollrescan(struct thread *td)
1249{
1250        struct seltd *stp;
1251        struct selfd *sfp;
1252        struct selfd *sfn;
1253        struct selinfo *si;
1254        struct filedesc *fdp;
1255        struct file *fp;
1256        struct pollfd *fd;
1257        int n;
1258
1259        n = 0;
1260        fdp = td->td_proc->p_fd;
1261        stp = td->td_sel;
1262        FILEDESC_SLOCK(fdp);
1263        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1264                fd = (struct pollfd *)sfp->sf_cookie;
1265                si = sfp->sf_si;
1266                selfdfree(stp, sfp);
1267                /* If the selinfo wasn't cleared the event didn't fire. */
1268                if (si != NULL)
1269                        continue;
1270                fp = fdp->fd_ofiles[fd->fd];
1271                if (fp == NULL) {
1272                        fd->revents = POLLNVAL;
1273                        n++;
1274                        continue;
1275                }
1276                /*
1277                 * Note: backend also returns POLLHUP and
1278                 * POLLERR if appropriate.
1279                 */
1280                fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1281                if (fd->revents != 0)
1282                        n++;
1283        }
1284        FILEDESC_SUNLOCK(fdp);
1285        stp->st_flags = 0;
1286        td->td_retval[0] = n;
1287        return (0);
1288}
1289
1290
1291static int
1292pollout(td, fds, ufds, nfd)
1293        struct thread *td;
1294        struct pollfd *fds;
1295        struct pollfd *ufds;
1296        u_int nfd;
1297{
1298        int error = 0;
1299        u_int i = 0;
1300        u_int n = 0;
1301
1302        for (i = 0; i < nfd; i++) {
1303                error = copyout(&fds->revents, &ufds->revents,
1304                    sizeof(ufds->revents));
1305                if (error)
1306                        return (error);
1307                if (fds->revents != 0)
1308                        n++;
1309                fds++;
1310                ufds++;
1311        }
1312        td->td_retval[0] = n;
1313        return (0);
1314}
1315
1316static int
1317pollscan(td, fds, nfd)
1318        struct thread *td;
1319        struct pollfd *fds;
1320        u_int nfd;
1321{
1322        struct filedesc *fdp = td->td_proc->p_fd;
1323        int i;
1324        struct file *fp;
1325        int n = 0;
1326
1327        FILEDESC_SLOCK(fdp);
1328        for (i = 0; i < nfd; i++, fds++) {
1329                if (fds->fd >= fdp->fd_nfiles) {
1330                        fds->revents = POLLNVAL;
1331                        n++;
1332                } else if (fds->fd < 0) {
1333                        fds->revents = 0;
1334                } else {
1335                        fp = fdp->fd_ofiles[fds->fd];
1336                        if (fp == NULL) {
1337                                fds->revents = POLLNVAL;
1338                                n++;
1339                        } else {
1340                                /*
1341                                 * Note: backend also returns POLLHUP and
1342                                 * POLLERR if appropriate.
1343                                 */
1344                                selfdalloc(td, fds);
1345                                fds->revents = fo_poll(fp, fds->events,
1346                                    td->td_ucred, td);
1347                                /*
1348                                 * POSIX requires POLLOUT to be never
1349                                 * set simultaneously with POLLHUP.
1350                                 */
1351                                if ((fds->revents & POLLHUP) != 0)
1352                                        fds->revents &= ~POLLOUT;
1353
1354                                if (fds->revents != 0)
1355                                        n++;
1356                        }
1357                }
1358        }
1359        FILEDESC_SUNLOCK(fdp);
1360        td->td_retval[0] = n;
1361        return (0);
1362}
1363
1364/*
1365 * OpenBSD poll system call.
1366 *
1367 * XXX this isn't quite a true representation..  OpenBSD uses select ops.
1368 */
1369#ifndef _SYS_SYSPROTO_H_
1370struct openbsd_poll_args {
1371        struct pollfd *fds;
1372        u_int   nfds;
1373        int     timeout;
1374};
1375#endif
1376int
1377openbsd_poll(td, uap)
1378        register struct thread *td;
1379        register struct openbsd_poll_args *uap;
1380{
1381#ifdef __rtems__
1382        return (kern_poll(td, (struct poll_args *)uap));
1383#else
1384        return (poll(td, (struct poll_args *)uap));
1385#endif
1386}
1387
1388/*
1389 * XXX This was created specifically to support netncp and netsmb.  This
1390 * allows the caller to specify a socket to wait for events on.  It returns
1391 * 0 if any events matched and an error otherwise.  There is no way to
1392 * determine which events fired.
1393 */
1394int
1395selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1396{
1397        struct timeval atv, rtv, ttv;
1398        int error, timo;
1399
1400        if (tvp != NULL) {
1401                atv = *tvp;
1402                if (itimerfix(&atv))
1403                        return (EINVAL);
1404                getmicrouptime(&rtv);
1405                timevaladd(&atv, &rtv);
1406        } else {
1407                atv.tv_sec = 0;
1408                atv.tv_usec = 0;
1409        }
1410
1411        timo = 0;
1412        seltdinit(td);
1413        /*
1414         * Iterate until the timeout expires or the socket becomes ready.
1415         */
1416        for (;;) {
1417                selfdalloc(td, NULL);
1418                error = sopoll(so, events, NULL, td);
1419                /* error here is actually the ready events. */
1420                if (error)
1421                        return (0);
1422                if (atv.tv_sec || atv.tv_usec) {
1423                        getmicrouptime(&rtv);
1424                        if (timevalcmp(&rtv, &atv, >=)) {
1425                                seltdclear(td);
1426                                return (EWOULDBLOCK);
1427                        }
1428                        ttv = atv;
1429                        timevalsub(&ttv, &rtv);
1430                        timo = ttv.tv_sec > 24 * 60 * 60 ?
1431                            24 * 60 * 60 * hz : tvtohz(&ttv);
1432                }
1433                error = seltdwait(td, timo);
1434                seltdclear(td);
1435                if (error)
1436                        break;
1437        }
1438        /* XXX Duplicates ncp/smb behavior. */
1439        if (error == ERESTART)
1440                error = 0;
1441        return (error);
1442}
1443
1444/*
1445 * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1446 * have two select sets, one for read and another for write.
1447 */
1448static void
1449selfdalloc(struct thread *td, void *cookie)
1450{
1451        struct seltd *stp;
1452
1453        stp = td->td_sel;
1454        if (stp->st_free1 == NULL)
1455                stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1456        stp->st_free1->sf_td = stp;
1457        stp->st_free1->sf_cookie = cookie;
1458        if (stp->st_free2 == NULL)
1459                stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1460        stp->st_free2->sf_td = stp;
1461        stp->st_free2->sf_cookie = cookie;
1462}
1463
1464static void
1465selfdfree(struct seltd *stp, struct selfd *sfp)
1466{
1467        STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1468        mtx_lock(sfp->sf_mtx);
1469        if (sfp->sf_si)
1470                TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1471        mtx_unlock(sfp->sf_mtx);
1472        uma_zfree(selfd_zone, sfp);
1473}
1474
1475/*
1476 * Record a select request.
1477 */
1478void
1479selrecord(selector, sip)
1480        struct thread *selector;
1481        struct selinfo *sip;
1482{
1483        struct selfd *sfp;
1484        struct seltd *stp;
1485        struct mtx *mtxp;
1486
1487        stp = selector->td_sel;
1488        /*
1489         * Don't record when doing a rescan.
1490         */
1491        if (stp->st_flags & SELTD_RESCAN)
1492                return;
1493        /*
1494         * Grab one of the preallocated descriptors.
1495         */
1496        sfp = NULL;
1497        if ((sfp = stp->st_free1) != NULL)
1498                stp->st_free1 = NULL;
1499        else if ((sfp = stp->st_free2) != NULL)
1500                stp->st_free2 = NULL;
1501        else
1502                panic("selrecord: No free selfd on selq");
1503        mtxp = sip->si_mtx;
1504        if (mtxp == NULL)
1505                mtxp = mtx_pool_find(mtxpool_select, sip);
1506        /*
1507         * Initialize the sfp and queue it in the thread.
1508         */
1509        sfp->sf_si = sip;
1510        sfp->sf_mtx = mtxp;
1511        STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1512        /*
1513         * Now that we've locked the sip, check for initialization.
1514         */
1515        mtx_lock(mtxp);
1516        if (sip->si_mtx == NULL) {
1517                sip->si_mtx = mtxp;
1518                TAILQ_INIT(&sip->si_tdlist);
1519        }
1520        /*
1521         * Add this thread to the list of selfds listening on this selinfo.
1522         */
1523        TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1524        mtx_unlock(sip->si_mtx);
1525}
1526
1527/* Wake up a selecting thread. */
1528void
1529selwakeup(sip)
1530        struct selinfo *sip;
1531{
1532        doselwakeup(sip, -1);
1533}
1534
1535/* Wake up a selecting thread, and set its priority. */
1536void
1537selwakeuppri(sip, pri)
1538        struct selinfo *sip;
1539        int pri;
1540{
1541        doselwakeup(sip, pri);
1542}
1543
1544/*
1545 * Do a wakeup when a selectable event occurs.
1546 */
1547static void
1548doselwakeup(sip, pri)
1549        struct selinfo *sip;
1550        int pri;
1551{
1552        struct selfd *sfp;
1553        struct selfd *sfn;
1554        struct seltd *stp;
1555
1556        /* If it's not initialized there can't be any waiters. */
1557        if (sip->si_mtx == NULL)
1558                return;
1559        /*
1560         * Locking the selinfo locks all selfds associated with it.
1561         */
1562        mtx_lock(sip->si_mtx);
1563        TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1564                /*
1565                 * Once we remove this sfp from the list and clear the
1566                 * sf_si seltdclear will know to ignore this si.
1567                 */
1568                TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1569                sfp->sf_si = NULL;
1570                stp = sfp->sf_td;
1571                mtx_lock(&stp->st_mtx);
1572                stp->st_flags |= SELTD_PENDING;
1573                cv_broadcastpri(&stp->st_wait, pri);
1574                mtx_unlock(&stp->st_mtx);
1575        }
1576        mtx_unlock(sip->si_mtx);
1577}
1578
1579static void
1580seltdinit(struct thread *td)
1581{
1582        struct seltd *stp;
1583
1584        if ((stp = td->td_sel) != NULL)
1585                goto out;
1586        td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1587        mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1588        cv_init(&stp->st_wait, "select");
1589out:
1590        stp->st_flags = 0;
1591        STAILQ_INIT(&stp->st_selq);
1592}
1593
1594static int
1595seltdwait(struct thread *td, int timo)
1596{
1597        struct seltd *stp;
1598        int error;
1599
1600        stp = td->td_sel;
1601        /*
1602         * An event of interest may occur while we do not hold the seltd
1603         * locked so check the pending flag before we sleep.
1604         */
1605        mtx_lock(&stp->st_mtx);
1606        /*
1607         * Any further calls to selrecord will be a rescan.
1608         */
1609        stp->st_flags |= SELTD_RESCAN;
1610        if (stp->st_flags & SELTD_PENDING) {
1611                mtx_unlock(&stp->st_mtx);
1612                return (0);
1613        }
1614        if (timo > 0)
1615                error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1616        else
1617                error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1618        mtx_unlock(&stp->st_mtx);
1619
1620        return (error);
1621}
1622
1623void
1624seltdfini(struct thread *td)
1625{
1626        struct seltd *stp;
1627
1628        stp = td->td_sel;
1629        if (stp == NULL)
1630                return;
1631        if (stp->st_free1)
1632                uma_zfree(selfd_zone, stp->st_free1);
1633        if (stp->st_free2)
1634                uma_zfree(selfd_zone, stp->st_free2);
1635        td->td_sel = NULL;
1636        free(stp, M_SELECT);
1637}
1638
1639/*
1640 * Remove the references to the thread from all of the objects we were
1641 * polling.
1642 */
1643static void
1644seltdclear(struct thread *td)
1645{
1646        struct seltd *stp;
1647        struct selfd *sfp;
1648        struct selfd *sfn;
1649
1650        stp = td->td_sel;
1651        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1652                selfdfree(stp, sfp);
1653        stp->st_flags = 0;
1654}
1655
1656static void selectinit(void *);
1657SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1658static void
1659selectinit(void *dummy __unused)
1660{
1661
1662        selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1663            NULL, NULL, UMA_ALIGN_PTR, 0);
1664        mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1665}
Note: See TracBrowser for help on using the repository browser.