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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 8eb42e8 was 8eb42e8, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/13 at 08:32:52

Avoid references to kern_descrip.c

Use the standard RTEMS file descriptors instead.

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