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

55-freebsd-126-freebsd-12
Last change on this file since 2ce13cf6 was bcdce02, checked in by Sebastian Huber <sebastian.huber@…>, on 08/21/18 at 11:47:02

Update to FreeBSD head 2018-06-01

Git mirror commit fb63610a69b0eb7f69a201ba05c4c1a7a2739cf9.

Update #3472.

  • Property mode set to 100644
File size: 44.8 KB
Line 
1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 1982, 1986, 1989, 1993
7 *      The Regents of the University of California.  All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *      @(#)sys_generic.c       8.5 (Berkeley) 1/21/94
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD$");
43
44#include <rtems/bsd/local/opt_capsicum.h>
45#include <rtems/bsd/local/opt_ktrace.h>
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/sysproto.h>
50#include <sys/capsicum.h>
51#include <sys/filedesc.h>
52#include <sys/filio.h>
53#include <sys/fcntl.h>
54#include <sys/file.h>
55#include <sys/lock.h>
56#include <sys/proc.h>
57#include <sys/signalvar.h>
58#include <sys/socketvar.h>
59#include <sys/uio.h>
60#include <sys/kernel.h>
61#include <sys/ktr.h>
62#include <sys/limits.h>
63#include <sys/malloc.h>
64#include <sys/poll.h>
65#include <sys/resourcevar.h>
66#include <sys/selinfo.h>
67#include <sys/sleepqueue.h>
68#include <sys/syscallsubr.h>
69#include <sys/sysctl.h>
70#include <sys/sysent.h>
71#include <sys/vnode.h>
72#include <sys/bio.h>
73#include <sys/buf.h>
74#include <sys/condvar.h>
75#ifdef KTRACE
76#include <sys/ktrace.h>
77#endif
78
79#include <security/audit/audit.h>
80#ifdef __rtems__
81#include <machine/rtems-bsd-syscall-api.h>
82#endif /* __rtems__ */
83
84/*
85 * The following macro defines how many bytes will be allocated from
86 * the stack instead of memory allocated when passing the IOCTL data
87 * structures from userspace and to the kernel. Some IOCTLs having
88 * small data structures are used very frequently and this small
89 * buffer on the stack gives a significant speedup improvement for
90 * those requests. The value of this define should be greater or equal
91 * to 64 bytes and should also be power of two. The data structure is
92 * currently hard-aligned to a 8-byte boundary on the stack. This
93 * should currently be sufficient for all supported platforms.
94 */
95#define SYS_IOCTL_SMALL_SIZE    128     /* bytes */
96#define SYS_IOCTL_SMALL_ALIGN   8       /* bytes */
97
98#ifndef __rtems__
99#ifdef __LP64__
100static int iosize_max_clamp = 0;
101SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
102    &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
103static int devfs_iosize_max_clamp = 1;
104SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
105    &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
106#endif
107
108/*
109 * Assert that the return value of read(2) and write(2) syscalls fits
110 * into a register.  If not, an architecture will need to provide the
111 * usermode wrappers to reconstruct the result.
112 */
113CTASSERT(sizeof(register_t) >= sizeof(size_t));
114
115static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
116#endif /* __rtems__ */
117static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
118#ifndef __rtems__
119MALLOC_DEFINE(M_IOV, "iov", "large iov's");
120#endif /* __rtems__ */
121
122static int      pollout(struct thread *, struct pollfd *, struct pollfd *,
123                    u_int);
124static int      pollscan(struct thread *, struct pollfd *, u_int);
125static int      pollrescan(struct thread *);
126static int      selscan(struct thread *, fd_mask **, fd_mask **, int);
127static int      selrescan(struct thread *, fd_mask **, fd_mask **);
128static void     selfdalloc(struct thread *, void *);
129static void     selfdfree(struct seltd *, struct selfd *);
130#ifndef __rtems__
131static int      dofileread(struct thread *, int, struct file *, struct uio *,
132                    off_t, int);
133static int      dofilewrite(struct thread *, int, struct file *, struct uio *,
134                    off_t, int);
135#endif /* __rtems__ */
136static void     doselwakeup(struct selinfo *, int);
137static void     seltdinit(struct thread *);
138static int      seltdwait(struct thread *, sbintime_t, sbintime_t);
139static void     seltdclear(struct thread *);
140
141/*
142 * One seltd per-thread allocated on demand as needed.
143 *
144 *      t - protected by st_mtx
145 *      k - Only accessed by curthread or read-only
146 */
147struct seltd {
148        STAILQ_HEAD(, selfd)    st_selq;        /* (k) List of selfds. */
149        struct selfd            *st_free1;      /* (k) free fd for read set. */
150        struct selfd            *st_free2;      /* (k) free fd for write set. */
151        struct mtx              st_mtx;         /* Protects struct seltd */
152        struct cv               st_wait;        /* (t) Wait channel. */
153        int                     st_flags;       /* (t) SELTD_ flags. */
154};
155
156#define SELTD_PENDING   0x0001                  /* We have pending events. */
157#define SELTD_RESCAN    0x0002                  /* Doing a rescan. */
158
159/*
160 * One selfd allocated per-thread per-file-descriptor.
161 *      f - protected by sf_mtx
162 */
163struct selfd {
164        STAILQ_ENTRY(selfd)     sf_link;        /* (k) fds owned by this td. */
165        TAILQ_ENTRY(selfd)      sf_threads;     /* (f) fds on this selinfo. */
166        struct selinfo          *sf_si;         /* (f) selinfo when linked. */
167        struct mtx              *sf_mtx;        /* Pointer to selinfo mtx. */
168        struct seltd            *sf_td;         /* (k) owning seltd. */
169        void                    *sf_cookie;     /* (k) fd or pollfd. */
170        u_int                   sf_refs;
171};
172
173static uma_zone_t selfd_zone;
174static struct mtx_pool *mtxpool_select;
175
176#ifndef __rtems__
177#ifdef __LP64__
178size_t
179devfs_iosize_max(void)
180{
181
182        return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
183            INT_MAX : SSIZE_MAX);
184}
185
186size_t
187iosize_max(void)
188{
189
190        return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
191            INT_MAX : SSIZE_MAX);
192}
193#endif
194
195#ifndef _SYS_SYSPROTO_H_
196struct read_args {
197        int     fd;
198        void    *buf;
199        size_t  nbyte;
200};
201#endif
202int
203sys_read(struct thread *td, struct read_args *uap)
204{
205        struct uio auio;
206        struct iovec aiov;
207        int error;
208
209        if (uap->nbyte > IOSIZE_MAX)
210                return (EINVAL);
211        aiov.iov_base = uap->buf;
212        aiov.iov_len = uap->nbyte;
213        auio.uio_iov = &aiov;
214        auio.uio_iovcnt = 1;
215        auio.uio_resid = uap->nbyte;
216        auio.uio_segflg = UIO_USERSPACE;
217        error = kern_readv(td, uap->fd, &auio);
218        return (error);
219}
220
221/*
222 * Positioned read system call
223 */
224#ifndef _SYS_SYSPROTO_H_
225struct pread_args {
226        int     fd;
227        void    *buf;
228        size_t  nbyte;
229        int     pad;
230        off_t   offset;
231};
232#endif
233int
234sys_pread(struct thread *td, struct pread_args *uap)
235{
236
237        return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
238}
239
240int
241kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
242{
243        struct uio auio;
244        struct iovec aiov;
245        int error;
246
247        if (nbyte > IOSIZE_MAX)
248                return (EINVAL);
249        aiov.iov_base = buf;
250        aiov.iov_len = nbyte;
251        auio.uio_iov = &aiov;
252        auio.uio_iovcnt = 1;
253        auio.uio_resid = nbyte;
254        auio.uio_segflg = UIO_USERSPACE;
255        error = kern_preadv(td, fd, &auio, offset);
256        return (error);
257}
258
259#if defined(COMPAT_FREEBSD6)
260int
261freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
262{
263
264        return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
265}
266#endif
267
268/*
269 * Scatter read system call.
270 */
271#ifndef _SYS_SYSPROTO_H_
272struct readv_args {
273        int     fd;
274        struct  iovec *iovp;
275        u_int   iovcnt;
276};
277#endif
278int
279sys_readv(struct thread *td, struct readv_args *uap)
280{
281        struct uio *auio;
282        int error;
283
284        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
285        if (error)
286                return (error);
287        error = kern_readv(td, uap->fd, auio);
288        free(auio, M_IOV);
289        return (error);
290}
291
292int
293kern_readv(struct thread *td, int fd, struct uio *auio)
294{
295        struct file *fp;
296        int error;
297
298        error = fget_read(td, fd, &cap_read_rights, &fp);
299        if (error)
300                return (error);
301        error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
302        fdrop(fp, td);
303        return (error);
304}
305
306/*
307 * Scatter positioned read system call.
308 */
309#ifndef _SYS_SYSPROTO_H_
310struct preadv_args {
311        int     fd;
312        struct  iovec *iovp;
313        u_int   iovcnt;
314        off_t   offset;
315};
316#endif
317int
318sys_preadv(struct thread *td, struct preadv_args *uap)
319{
320        struct uio *auio;
321        int error;
322
323        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
324        if (error)
325                return (error);
326        error = kern_preadv(td, uap->fd, auio, uap->offset);
327        free(auio, M_IOV);
328        return (error);
329}
330
331int
332kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
333{
334        struct file *fp;
335        int error;
336
337        error = fget_read(td, fd, &cap_pread_rights, &fp);
338        if (error)
339                return (error);
340        if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
341                error = ESPIPE;
342        else if (offset < 0 &&
343            (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
344                error = EINVAL;
345        else
346                error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
347        fdrop(fp, td);
348        return (error);
349}
350
351/*
352 * Common code for readv and preadv that reads data in
353 * from a file using the passed in uio, offset, and flags.
354 */
355static int
356dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio,
357    off_t offset, int flags)
358{
359        ssize_t cnt;
360        int error;
361#ifdef KTRACE
362        struct uio *ktruio = NULL;
363#endif
364
365        AUDIT_ARG_FD(fd);
366
367        /* Finish zero length reads right here */
368        if (auio->uio_resid == 0) {
369                td->td_retval[0] = 0;
370                return (0);
371        }
372        auio->uio_rw = UIO_READ;
373        auio->uio_offset = offset;
374        auio->uio_td = td;
375#ifdef KTRACE
376        if (KTRPOINT(td, KTR_GENIO))
377                ktruio = cloneuio(auio);
378#endif
379        cnt = auio->uio_resid;
380        if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
381                if (auio->uio_resid != cnt && (error == ERESTART ||
382                    error == EINTR || error == EWOULDBLOCK))
383                        error = 0;
384        }
385        cnt -= auio->uio_resid;
386#ifdef KTRACE
387        if (ktruio != NULL) {
388                ktruio->uio_resid = cnt;
389                ktrgenio(fd, UIO_READ, ktruio, error);
390        }
391#endif
392        td->td_retval[0] = cnt;
393        return (error);
394}
395
396#ifndef _SYS_SYSPROTO_H_
397struct write_args {
398        int     fd;
399        const void *buf;
400        size_t  nbyte;
401};
402#endif
403int
404sys_write(struct thread *td, struct write_args *uap)
405{
406        struct uio auio;
407        struct iovec aiov;
408        int error;
409
410        if (uap->nbyte > IOSIZE_MAX)
411                return (EINVAL);
412        aiov.iov_base = (void *)(uintptr_t)uap->buf;
413        aiov.iov_len = uap->nbyte;
414        auio.uio_iov = &aiov;
415        auio.uio_iovcnt = 1;
416        auio.uio_resid = uap->nbyte;
417        auio.uio_segflg = UIO_USERSPACE;
418        error = kern_writev(td, uap->fd, &auio);
419        return (error);
420}
421
422/*
423 * Positioned write system call.
424 */
425#ifndef _SYS_SYSPROTO_H_
426struct pwrite_args {
427        int     fd;
428        const void *buf;
429        size_t  nbyte;
430        int     pad;
431        off_t   offset;
432};
433#endif
434int
435sys_pwrite(struct thread *td, struct pwrite_args *uap)
436{
437
438        return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
439}
440
441int
442kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
443    off_t offset)
444{
445        struct uio auio;
446        struct iovec aiov;
447        int error;
448
449        if (nbyte > IOSIZE_MAX)
450                return (EINVAL);
451        aiov.iov_base = (void *)(uintptr_t)buf;
452        aiov.iov_len = nbyte;
453        auio.uio_iov = &aiov;
454        auio.uio_iovcnt = 1;
455        auio.uio_resid = nbyte;
456        auio.uio_segflg = UIO_USERSPACE;
457        error = kern_pwritev(td, fd, &auio, offset);
458        return (error);
459}
460
461#if defined(COMPAT_FREEBSD6)
462int
463freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
464{
465
466        return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
467}
468#endif
469
470/*
471 * Gather write system call.
472 */
473#ifndef _SYS_SYSPROTO_H_
474struct writev_args {
475        int     fd;
476        struct  iovec *iovp;
477        u_int   iovcnt;
478};
479#endif
480int
481sys_writev(struct thread *td, struct writev_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_writev(td, uap->fd, auio);
490        free(auio, M_IOV);
491        return (error);
492}
493
494int
495kern_writev(struct thread *td, int fd, struct uio *auio)
496{
497        struct file *fp;
498        int error;
499
500        error = fget_write(td, fd, &cap_write_rights, &fp);
501        if (error)
502                return (error);
503        error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
504        fdrop(fp, td);
505        return (error);
506}
507
508/*
509 * Gather positioned write system call.
510 */
511#ifndef _SYS_SYSPROTO_H_
512struct pwritev_args {
513        int     fd;
514        struct  iovec *iovp;
515        u_int   iovcnt;
516        off_t   offset;
517};
518#endif
519int
520sys_pwritev(struct thread *td, struct pwritev_args *uap)
521{
522        struct uio *auio;
523        int error;
524
525        error = copyinuio(uap->iovp, uap->iovcnt, &auio);
526        if (error)
527                return (error);
528        error = kern_pwritev(td, uap->fd, auio, uap->offset);
529        free(auio, M_IOV);
530        return (error);
531}
532
533int
534kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
535{
536        struct file *fp;
537        int error;
538
539        error = fget_write(td, fd, &cap_pwrite_rights, &fp);
540        if (error)
541                return (error);
542        if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
543                error = ESPIPE;
544        else if (offset < 0 &&
545            (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
546                error = EINVAL;
547        else
548                error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
549        fdrop(fp, td);
550        return (error);
551}
552
553/*
554 * Common code for writev and pwritev that writes data to
555 * a file using the passed in uio, offset, and flags.
556 */
557static int
558dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio,
559    off_t offset, int flags)
560{
561        ssize_t cnt;
562        int error;
563#ifdef KTRACE
564        struct uio *ktruio = NULL;
565#endif
566
567        AUDIT_ARG_FD(fd);
568        auio->uio_rw = UIO_WRITE;
569        auio->uio_td = td;
570        auio->uio_offset = offset;
571#ifdef KTRACE
572        if (KTRPOINT(td, KTR_GENIO))
573                ktruio = cloneuio(auio);
574#endif
575        cnt = auio->uio_resid;
576        if (fp->f_type == DTYPE_VNODE &&
577            (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
578                bwillwrite();
579        if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
580                if (auio->uio_resid != cnt && (error == ERESTART ||
581                    error == EINTR || error == EWOULDBLOCK))
582                        error = 0;
583                /* Socket layer is responsible for issuing SIGPIPE. */
584                if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
585                        PROC_LOCK(td->td_proc);
586                        tdsignal(td, SIGPIPE);
587                        PROC_UNLOCK(td->td_proc);
588                }
589        }
590        cnt -= auio->uio_resid;
591#ifdef KTRACE
592        if (ktruio != NULL) {
593                ktruio->uio_resid = cnt;
594                ktrgenio(fd, UIO_WRITE, ktruio, error);
595        }
596#endif
597        td->td_retval[0] = cnt;
598        return (error);
599}
600
601/*
602 * Truncate a file given a file descriptor.
603 *
604 * Can't use fget_write() here, since must return EINVAL and not EBADF if the
605 * descriptor isn't writable.
606 */
607int
608kern_ftruncate(struct thread *td, int fd, off_t length)
609{
610        struct file *fp;
611        int error;
612
613        AUDIT_ARG_FD(fd);
614        if (length < 0)
615                return (EINVAL);
616        error = fget(td, fd, &cap_ftruncate_rights, &fp);
617        if (error)
618                return (error);
619        AUDIT_ARG_FILE(td->td_proc, fp);
620        if (!(fp->f_flag & FWRITE)) {
621                fdrop(fp, td);
622                return (EINVAL);
623        }
624        error = fo_truncate(fp, length, td->td_ucred, td);
625        fdrop(fp, td);
626        return (error);
627}
628
629#ifndef _SYS_SYSPROTO_H_
630struct ftruncate_args {
631        int     fd;
632        int     pad;
633        off_t   length;
634};
635#endif
636int
637sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
638{
639
640        return (kern_ftruncate(td, uap->fd, uap->length));
641}
642
643#if defined(COMPAT_43)
644#ifndef _SYS_SYSPROTO_H_
645struct oftruncate_args {
646        int     fd;
647        long    length;
648};
649#endif
650int
651oftruncate(struct thread *td, struct oftruncate_args *uap)
652{
653
654        return (kern_ftruncate(td, uap->fd, uap->length));
655}
656#endif /* COMPAT_43 */
657
658#ifndef _SYS_SYSPROTO_H_
659struct ioctl_args {
660        int     fd;
661        u_long  com;
662        caddr_t data;
663};
664#endif
665/* ARGSUSED */
666int
667sys_ioctl(struct thread *td, struct ioctl_args *uap)
668{
669        u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
670        u_long com;
671        int arg, error;
672        u_int size;
673        caddr_t data;
674
675        if (uap->com > 0xffffffff) {
676                printf(
677                    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
678                    td->td_proc->p_pid, td->td_name, uap->com);
679                uap->com &= 0xffffffff;
680        }
681        com = uap->com;
682
683        /*
684         * Interpret high order word to find amount of data to be
685         * copied to/from the user's address space.
686         */
687        size = IOCPARM_LEN(com);
688        if ((size > IOCPARM_MAX) ||
689            ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
690#if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
691            ((com & IOC_OUT) && size == 0) ||
692#else
693            ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
694#endif
695            ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
696                return (ENOTTY);
697
698        if (size > 0) {
699                if (com & IOC_VOID) {
700                        /* Integer argument. */
701                        arg = (intptr_t)uap->data;
702                        data = (void *)&arg;
703                        size = 0;
704                } else {
705                        if (size > SYS_IOCTL_SMALL_SIZE)
706                                data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
707                        else
708                                data = smalldata;
709                }
710        } else
711                data = (void *)&uap->data;
712        if (com & IOC_IN) {
713                error = copyin(uap->data, data, (u_int)size);
714                if (error != 0)
715                        goto out;
716        } else if (com & IOC_OUT) {
717                /*
718                 * Zero the buffer so the user always
719                 * gets back something deterministic.
720                 */
721                bzero(data, size);
722        }
723
724        error = kern_ioctl(td, uap->fd, com, data);
725
726        if (error == 0 && (com & IOC_OUT))
727                error = copyout(data, uap->data, (u_int)size);
728
729out:
730        if (size > SYS_IOCTL_SMALL_SIZE)
731                free(data, M_IOCTLOPS);
732        return (error);
733}
734
735int
736kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
737{
738        struct file *fp;
739        struct filedesc *fdp;
740        int error, tmp, locked;
741
742        AUDIT_ARG_FD(fd);
743        AUDIT_ARG_CMD(com);
744
745        fdp = td->td_proc->p_fd;
746
747        switch (com) {
748        case FIONCLEX:
749        case FIOCLEX:
750                FILEDESC_XLOCK(fdp);
751                locked = LA_XLOCKED;
752                break;
753        default:
754#ifdef CAPABILITIES
755                FILEDESC_SLOCK(fdp);
756                locked = LA_SLOCKED;
757#else
758                locked = LA_UNLOCKED;
759#endif
760                break;
761        }
762
763#ifdef CAPABILITIES
764        if ((fp = fget_locked(fdp, fd)) == NULL) {
765                error = EBADF;
766                goto out;
767        }
768        if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
769                fp = NULL;      /* fhold() was not called yet */
770                goto out;
771        }
772        fhold(fp);
773        if (locked == LA_SLOCKED) {
774                FILEDESC_SUNLOCK(fdp);
775                locked = LA_UNLOCKED;
776        }
777#else
778        error = fget(td, fd, &cap_ioctl_rights, &fp);
779        if (error != 0) {
780                fp = NULL;
781                goto out;
782        }
783#endif
784        if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
785                error = EBADF;
786                goto out;
787        }
788
789        switch (com) {
790        case FIONCLEX:
791                fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
792                goto out;
793        case FIOCLEX:
794                fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
795                goto out;
796        case FIONBIO:
797                if ((tmp = *(int *)data))
798                        atomic_set_int(&fp->f_flag, FNONBLOCK);
799                else
800                        atomic_clear_int(&fp->f_flag, FNONBLOCK);
801                data = (void *)&tmp;
802                break;
803        case FIOASYNC:
804                if ((tmp = *(int *)data))
805                        atomic_set_int(&fp->f_flag, FASYNC);
806                else
807                        atomic_clear_int(&fp->f_flag, FASYNC);
808                data = (void *)&tmp;
809                break;
810        }
811
812        error = fo_ioctl(fp, com, data, td->td_ucred, td);
813out:
814        switch (locked) {
815        case LA_XLOCKED:
816                FILEDESC_XUNLOCK(fdp);
817                break;
818#ifdef CAPABILITIES
819        case LA_SLOCKED:
820                FILEDESC_SUNLOCK(fdp);
821                break;
822#endif
823        default:
824                FILEDESC_UNLOCK_ASSERT(fdp);
825                break;
826        }
827        if (fp != NULL)
828                fdrop(fp, td);
829        return (error);
830}
831#endif /* __rtems__ */
832
833int
834poll_no_poll(int events)
835{
836        /*
837         * Return true for read/write.  If the user asked for something
838         * special, return POLLNVAL, so that clients have a way of
839         * determining reliably whether or not the extended
840         * functionality is present without hard-coding knowledge
841         * of specific filesystem implementations.
842         */
843        if (events & ~POLLSTANDARD)
844                return (POLLNVAL);
845
846        return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
847}
848
849#ifndef __rtems__
850int
851sys_pselect(struct thread *td, struct pselect_args *uap)
852{
853        struct timespec ts;
854        struct timeval tv, *tvp;
855        sigset_t set, *uset;
856        int error;
857
858        if (uap->ts != NULL) {
859                error = copyin(uap->ts, &ts, sizeof(ts));
860                if (error != 0)
861                    return (error);
862                TIMESPEC_TO_TIMEVAL(&tv, &ts);
863                tvp = &tv;
864        } else
865                tvp = NULL;
866        if (uap->sm != NULL) {
867                error = copyin(uap->sm, &set, sizeof(set));
868                if (error != 0)
869                        return (error);
870                uset = &set;
871        } else
872                uset = NULL;
873        return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
874            uset, NFDBITS));
875}
876
877int
878kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
879    struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
880{
881        int error;
882
883        if (uset != NULL) {
884                error = kern_sigprocmask(td, SIG_SETMASK, uset,
885                    &td->td_oldsigmask, 0);
886                if (error != 0)
887                        return (error);
888                td->td_pflags |= TDP_OLDMASK;
889                /*
890                 * Make sure that ast() is called on return to
891                 * usermode and TDP_OLDMASK is cleared, restoring old
892                 * sigmask.
893                 */
894                thread_lock(td);
895                td->td_flags |= TDF_ASTPENDING;
896                thread_unlock(td);
897        }
898        error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
899        return (error);
900}
901
902#ifndef _SYS_SYSPROTO_H_
903struct select_args {
904        int     nd;
905        fd_set  *in, *ou, *ex;
906        struct  timeval *tv;
907};
908#endif
909int
910sys_select(struct thread *td, struct select_args *uap)
911{
912        struct timeval tv, *tvp;
913        int error;
914
915        if (uap->tv != NULL) {
916                error = copyin(uap->tv, &tv, sizeof(tv));
917                if (error)
918                        return (error);
919                tvp = &tv;
920        } else
921                tvp = NULL;
922
923        return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
924            NFDBITS));
925}
926#endif /* __rtems__ */
927
928/*
929 * In the unlikely case when user specified n greater then the last
930 * open file descriptor, check that no bits are set after the last
931 * valid fd.  We must return EBADF if any is set.
932 *
933 * There are applications that rely on the behaviour.
934 *
935 * nd is fd_lastfile + 1.
936 */
937static int
938select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
939{
940        char *addr, *oaddr;
941        int b, i, res;
942        uint8_t bits;
943
944        if (nd >= ndu || fd_in == NULL)
945                return (0);
946
947        oaddr = NULL;
948        bits = 0; /* silence gcc */
949        for (i = nd; i < ndu; i++) {
950                b = i / NBBY;
951#if BYTE_ORDER == LITTLE_ENDIAN
952                addr = (char *)fd_in + b;
953#else
954                addr = (char *)fd_in;
955                if (abi_nfdbits == NFDBITS) {
956                        addr += rounddown(b, sizeof(fd_mask)) +
957                            sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
958                } else {
959                        addr += rounddown(b, sizeof(uint32_t)) +
960                            sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
961                }
962#endif
963                if (addr != oaddr) {
964                        res = fubyte(addr);
965                        if (res == -1)
966                                return (EFAULT);
967                        oaddr = addr;
968                        bits = res;
969                }
970                if ((bits & (1 << (i % NBBY))) != 0)
971                        return (EBADF);
972        }
973        return (0);
974}
975
976int
977kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
978    fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
979{
980        struct filedesc *fdp;
981        /*
982         * The magic 2048 here is chosen to be just enough for FD_SETSIZE
983         * infds with the new FD_SETSIZE of 1024, and more than enough for
984         * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
985         * of 256.
986         */
987        fd_mask s_selbits[howmany(2048, NFDBITS)];
988        fd_mask *ibits[3], *obits[3], *selbits, *sbp;
989        struct timeval rtv;
990        sbintime_t asbt, precision, rsbt;
991        u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
992        int error, lf, ndu;
993
994        if (nd < 0)
995                return (EINVAL);
996#ifndef __rtems__
997        fdp = td->td_proc->p_fd;
998#endif /* __rtems__ */
999        ndu = nd;
1000#ifndef __rtems__
1001        lf = fdp->fd_lastfile;
1002#else /* __rtems__ */
1003        (void) fdp;
1004        lf = rtems_libio_number_iops;
1005#endif /* __rtems__ */
1006        if (nd > lf + 1)
1007                nd = lf + 1;
1008
1009        error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
1010        if (error != 0)
1011                return (error);
1012        error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
1013        if (error != 0)
1014                return (error);
1015        error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
1016        if (error != 0)
1017                return (error);
1018
1019        /*
1020         * Allocate just enough bits for the non-null fd_sets.  Use the
1021         * preallocated auto buffer if possible.
1022         */
1023        nfdbits = roundup(nd, NFDBITS);
1024        ncpbytes = nfdbits / NBBY;
1025        ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1026        nbufbytes = 0;
1027        if (fd_in != NULL)
1028                nbufbytes += 2 * ncpbytes;
1029        if (fd_ou != NULL)
1030                nbufbytes += 2 * ncpbytes;
1031        if (fd_ex != NULL)
1032                nbufbytes += 2 * ncpbytes;
1033        if (nbufbytes <= sizeof s_selbits)
1034                selbits = &s_selbits[0];
1035        else
1036                selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1037
1038        /*
1039         * Assign pointers into the bit buffers and fetch the input bits.
1040         * Put the output buffers together so that they can be bzeroed
1041         * together.
1042         */
1043        sbp = selbits;
1044#define getbits(name, x) \
1045        do {                                                            \
1046                if (name == NULL) {                                     \
1047                        ibits[x] = NULL;                                \
1048                        obits[x] = NULL;                                \
1049                } else {                                                \
1050                        ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;   \
1051                        obits[x] = sbp;                                 \
1052                        sbp += ncpbytes / sizeof *sbp;                  \
1053                        error = copyin(name, ibits[x], ncpubytes);      \
1054                        if (error != 0)                                 \
1055                                goto done;                              \
1056                        bzero((char *)ibits[x] + ncpubytes,             \
1057                            ncpbytes - ncpubytes);                      \
1058                }                                                       \
1059        } while (0)
1060        getbits(fd_in, 0);
1061        getbits(fd_ou, 1);
1062        getbits(fd_ex, 2);
1063#undef  getbits
1064
1065#if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1066        /*
1067         * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1068         * we are running under 32-bit emulation. This should be more
1069         * generic.
1070         */
1071#define swizzle_fdset(bits)                                             \
1072        if (abi_nfdbits != NFDBITS && bits != NULL) {                   \
1073                int i;                                                  \
1074                for (i = 0; i < ncpbytes / sizeof *sbp; i++)            \
1075                        bits[i] = (bits[i] >> 32) | (bits[i] << 32);    \
1076        }
1077#else
1078#define swizzle_fdset(bits)
1079#endif
1080
1081        /* Make sure the bit order makes it through an ABI transition */
1082        swizzle_fdset(ibits[0]);
1083        swizzle_fdset(ibits[1]);
1084        swizzle_fdset(ibits[2]);
1085       
1086        if (nbufbytes != 0)
1087                bzero(selbits, nbufbytes / 2);
1088
1089        precision = 0;
1090        if (tvp != NULL) {
1091                rtv = *tvp;
1092                if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1093                    rtv.tv_usec >= 1000000) {
1094                        error = EINVAL;
1095                        goto done;
1096                }
1097                if (!timevalisset(&rtv))
1098                        asbt = 0;
1099                else if (rtv.tv_sec <= INT32_MAX) {
1100                        rsbt = tvtosbt(rtv);
1101                        precision = rsbt;
1102                        precision >>= tc_precexp;
1103                        if (TIMESEL(&asbt, rsbt))
1104                                asbt += tc_tick_sbt;
1105                        if (asbt <= SBT_MAX - rsbt)
1106                                asbt += rsbt;
1107                        else
1108                                asbt = -1;
1109                } else
1110                        asbt = -1;
1111        } else
1112                asbt = -1;
1113        seltdinit(td);
1114        /* Iterate until the timeout expires or descriptors become ready. */
1115        for (;;) {
1116                error = selscan(td, ibits, obits, nd);
1117                if (error || td->td_retval[0] != 0)
1118                        break;
1119                error = seltdwait(td, asbt, precision);
1120                if (error)
1121                        break;
1122                error = selrescan(td, ibits, obits);
1123                if (error || td->td_retval[0] != 0)
1124                        break;
1125        }
1126        seltdclear(td);
1127
1128done:
1129        /* select is not restarted after signals... */
1130        if (error == ERESTART)
1131                error = EINTR;
1132        if (error == EWOULDBLOCK)
1133                error = 0;
1134
1135        /* swizzle bit order back, if necessary */
1136        swizzle_fdset(obits[0]);
1137        swizzle_fdset(obits[1]);
1138        swizzle_fdset(obits[2]);
1139#undef swizzle_fdset
1140
1141#define putbits(name, x) \
1142        if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1143                error = error2;
1144        if (error == 0) {
1145                int error2;
1146
1147                putbits(fd_in, 0);
1148                putbits(fd_ou, 1);
1149                putbits(fd_ex, 2);
1150#undef putbits
1151        }
1152        if (selbits != &s_selbits[0])
1153                free(selbits, M_SELECT);
1154
1155        return (error);
1156}
1157#ifdef __rtems__
1158int
1159select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds,
1160    struct timeval *timeout)
1161{
1162        struct thread *td = rtems_bsd_get_curthread_or_null();
1163        int error;
1164
1165        if (td != NULL) {
1166                error = kern_select(td, nfds, readfds, writefds, errorfds,
1167                    timeout, NFDBITS);
1168        } else {
1169                error = ENOMEM;
1170        }
1171
1172        if (error == 0) {
1173                return td->td_retval[0];
1174        } else {
1175                rtems_set_errno_and_return_minus_one(error);
1176        }
1177}
1178#endif /* __rtems__ */
1179
1180/*
1181 * Convert a select bit set to poll flags.
1182 *
1183 * The backend always returns POLLHUP/POLLERR if appropriate and we
1184 * return this as a set bit in any set.
1185 */
1186static int select_flags[3] = {
1187    POLLRDNORM | POLLHUP | POLLERR,
1188    POLLWRNORM | POLLHUP | POLLERR,
1189    POLLRDBAND | POLLERR
1190};
1191
1192/*
1193 * Compute the fo_poll flags required for a fd given by the index and
1194 * bit position in the fd_mask array.
1195 */
1196static __inline int
1197selflags(fd_mask **ibits, int idx, fd_mask bit)
1198{
1199        int flags;
1200        int msk;
1201
1202        flags = 0;
1203        for (msk = 0; msk < 3; msk++) {
1204                if (ibits[msk] == NULL)
1205                        continue;
1206                if ((ibits[msk][idx] & bit) == 0)
1207                        continue;
1208                flags |= select_flags[msk];
1209        }
1210        return (flags);
1211}
1212
1213/*
1214 * Set the appropriate output bits given a mask of fired events and the
1215 * input bits originally requested.
1216 */
1217static __inline int
1218selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1219{
1220        int msk;
1221        int n;
1222
1223        n = 0;
1224        for (msk = 0; msk < 3; msk++) {
1225                if ((events & select_flags[msk]) == 0)
1226                        continue;
1227                if (ibits[msk] == NULL)
1228                        continue;
1229                if ((ibits[msk][idx] & bit) == 0)
1230                        continue;
1231                /*
1232                 * XXX Check for a duplicate set.  This can occur because a
1233                 * socket calls selrecord() twice for each poll() call
1234                 * resulting in two selfds per real fd.  selrescan() will
1235                 * call selsetbits twice as a result.
1236                 */
1237                if ((obits[msk][idx] & bit) != 0)
1238                        continue;
1239                obits[msk][idx] |= bit;
1240                n++;
1241        }
1242
1243        return (n);
1244}
1245
1246static __inline int
1247getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1248{
1249
1250        return (fget_unlocked(fdp, fd, &cap_event_rights, fpp, NULL));
1251}
1252
1253/*
1254 * Traverse the list of fds attached to this thread's seltd and check for
1255 * completion.
1256 */
1257static int
1258selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1259{
1260        struct filedesc *fdp;
1261        struct selinfo *si;
1262        struct seltd *stp;
1263        struct selfd *sfp;
1264        struct selfd *sfn;
1265        struct file *fp;
1266        fd_mask bit;
1267        int fd, ev, n, idx;
1268        int error;
1269
1270#ifndef __rtems__
1271        fdp = td->td_proc->p_fd;
1272#else /* __rtems__ */
1273        fdp = NULL;
1274#endif /* __rtems__ */
1275        stp = td->td_sel;
1276        n = 0;
1277        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1278                fd = (int)(uintptr_t)sfp->sf_cookie;
1279                si = sfp->sf_si;
1280                selfdfree(stp, sfp);
1281                /* If the selinfo wasn't cleared the event didn't fire. */
1282                if (si != NULL)
1283                        continue;
1284                error = getselfd_cap(fdp, fd, &fp);
1285                if (error)
1286                        return (error);
1287                idx = fd / NFDBITS;
1288                bit = (fd_mask)1 << (fd % NFDBITS);
1289                ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1290                fdrop(fp, td);
1291                if (ev != 0)
1292                        n += selsetbits(ibits, obits, idx, bit, ev);
1293        }
1294        stp->st_flags = 0;
1295        td->td_retval[0] = n;
1296        return (0);
1297}
1298
1299/*
1300 * Perform the initial filedescriptor scan and register ourselves with
1301 * each selinfo.
1302 */
1303static int
1304selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
1305{
1306        struct filedesc *fdp;
1307        struct file *fp;
1308        fd_mask bit;
1309        int ev, flags, end, fd;
1310        int n, idx;
1311        int error;
1312
1313#ifndef __rtems__
1314        fdp = td->td_proc->p_fd;
1315#else /* __rtems__ */
1316        fdp = NULL;
1317#endif /* __rtems__ */
1318        n = 0;
1319        for (idx = 0, fd = 0; fd < nfd; idx++) {
1320                end = imin(fd + NFDBITS, nfd);
1321                for (bit = 1; fd < end; bit <<= 1, fd++) {
1322                        /* Compute the list of events we're interested in. */
1323                        flags = selflags(ibits, idx, bit);
1324                        if (flags == 0)
1325                                continue;
1326                        error = getselfd_cap(fdp, fd, &fp);
1327                        if (error)
1328                                return (error);
1329                        selfdalloc(td, (void *)(uintptr_t)fd);
1330                        ev = fo_poll(fp, flags, td->td_ucred, td);
1331                        fdrop(fp, td);
1332                        if (ev != 0)
1333                                n += selsetbits(ibits, obits, idx, bit, ev);
1334                }
1335        }
1336
1337        td->td_retval[0] = n;
1338        return (0);
1339}
1340
1341#ifdef __rtems__
1342static int kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1343    struct timespec *tsp, sigset_t *uset);
1344
1345static
1346#endif /* __rtems__ */
1347int
1348sys_poll(struct thread *td, struct poll_args *uap)
1349{
1350        struct timespec ts, *tsp;
1351
1352        if (uap->timeout != INFTIM) {
1353                if (uap->timeout < 0)
1354                        return (EINVAL);
1355                ts.tv_sec = uap->timeout / 1000;
1356                ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1357                tsp = &ts;
1358        } else
1359                tsp = NULL;
1360
1361        return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1362}
1363
1364int
1365kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1366    struct timespec *tsp, sigset_t *uset)
1367{
1368        struct pollfd *bits;
1369        struct pollfd smallbits[32];
1370        sbintime_t sbt, precision, tmp;
1371        time_t over;
1372        struct timespec ts;
1373        int error;
1374        size_t ni;
1375
1376        precision = 0;
1377        if (tsp != NULL) {
1378                if (tsp->tv_sec < 0)
1379                        return (EINVAL);
1380                if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1381                        return (EINVAL);
1382                if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1383                        sbt = 0;
1384                else {
1385                        ts = *tsp;
1386                        if (ts.tv_sec > INT32_MAX / 2) {
1387                                over = ts.tv_sec - INT32_MAX / 2;
1388                                ts.tv_sec -= over;
1389                        } else
1390                                over = 0;
1391                        tmp = tstosbt(ts);
1392                        precision = tmp;
1393                        precision >>= tc_precexp;
1394                        if (TIMESEL(&sbt, tmp))
1395                                sbt += tc_tick_sbt;
1396                        sbt += tmp;
1397                }
1398        } else
1399                sbt = -1;
1400
1401#ifndef __rtems__
1402        if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1403#else /* __rtems__ */
1404        if (nfds > rtems_libio_number_iops)
1405#endif /* __rtems__ */
1406                return (EINVAL);
1407        ni = nfds * sizeof(struct pollfd);
1408        if (ni > sizeof(smallbits))
1409                bits = malloc(ni, M_TEMP, M_WAITOK);
1410        else
1411                bits = smallbits;
1412        error = copyin(fds, bits, ni);
1413        if (error)
1414                goto done;
1415
1416#ifndef __rtems__
1417        if (uset != NULL) {
1418                error = kern_sigprocmask(td, SIG_SETMASK, uset,
1419                    &td->td_oldsigmask, 0);
1420                if (error)
1421                        goto done;
1422                td->td_pflags |= TDP_OLDMASK;
1423                /*
1424                 * Make sure that ast() is called on return to
1425                 * usermode and TDP_OLDMASK is cleared, restoring old
1426                 * sigmask.
1427                 */
1428                thread_lock(td);
1429                td->td_flags |= TDF_ASTPENDING;
1430                thread_unlock(td);
1431        }
1432#endif /* __rtems__ */
1433
1434        seltdinit(td);
1435        /* Iterate until the timeout expires or descriptors become ready. */
1436        for (;;) {
1437                error = pollscan(td, bits, nfds);
1438                if (error || td->td_retval[0] != 0)
1439                        break;
1440                error = seltdwait(td, sbt, precision);
1441                if (error)
1442                        break;
1443                error = pollrescan(td);
1444                if (error || td->td_retval[0] != 0)
1445                        break;
1446        }
1447        seltdclear(td);
1448
1449done:
1450        /* poll is not restarted after signals... */
1451        if (error == ERESTART)
1452                error = EINTR;
1453        if (error == EWOULDBLOCK)
1454                error = 0;
1455        if (error == 0) {
1456                error = pollout(td, bits, fds, nfds);
1457                if (error)
1458                        goto out;
1459        }
1460out:
1461        if (ni > sizeof(smallbits))
1462                free(bits, M_TEMP);
1463        return (error);
1464}
1465#ifdef __rtems__
1466int
1467poll(struct pollfd fds[], nfds_t nfds, int timeout)
1468{
1469        struct thread *td = rtems_bsd_get_curthread_or_null();
1470        struct poll_args ua = {
1471                .fds = &fds[0],
1472                .nfds = nfds,
1473                .timeout = timeout
1474        };
1475        int error;
1476
1477        if (td != NULL) {
1478                error = sys_poll(td, &ua);
1479        } else {
1480                error = ENOMEM;
1481        }
1482
1483        if (error == 0) {
1484                return td->td_retval[0];
1485        } else {
1486                rtems_set_errno_and_return_minus_one(error);
1487        }
1488}
1489#endif /* __rtems__ */
1490
1491#ifndef __rtems__
1492int
1493sys_ppoll(struct thread *td, struct ppoll_args *uap)
1494{
1495        struct timespec ts, *tsp;
1496        sigset_t set, *ssp;
1497        int error;
1498
1499        if (uap->ts != NULL) {
1500                error = copyin(uap->ts, &ts, sizeof(ts));
1501                if (error)
1502                        return (error);
1503                tsp = &ts;
1504        } else
1505                tsp = NULL;
1506        if (uap->set != NULL) {
1507                error = copyin(uap->set, &set, sizeof(set));
1508                if (error)
1509                        return (error);
1510                ssp = &set;
1511        } else
1512                ssp = NULL;
1513        /*
1514         * fds is still a pointer to user space. kern_poll() will
1515         * take care of copyin that array to the kernel space.
1516         */
1517
1518        return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1519}
1520#endif /* __rtems__ */
1521
1522static int
1523pollrescan(struct thread *td)
1524{
1525        struct seltd *stp;
1526        struct selfd *sfp;
1527        struct selfd *sfn;
1528        struct selinfo *si;
1529        struct filedesc *fdp;
1530        struct file *fp;
1531        struct pollfd *fd;
1532        int n;
1533
1534        n = 0;
1535#ifndef __rtems__
1536        fdp = td->td_proc->p_fd;
1537#else /* __rtems__ */
1538        fdp = NULL;
1539#endif /* __rtems__ */
1540        stp = td->td_sel;
1541        FILEDESC_SLOCK(fdp);
1542        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1543                fd = (struct pollfd *)sfp->sf_cookie;
1544                si = sfp->sf_si;
1545                selfdfree(stp, sfp);
1546                /* If the selinfo wasn't cleared the event didn't fire. */
1547                if (si != NULL)
1548                        continue;
1549#ifndef __rtems__
1550                fp = fdp->fd_ofiles[fd->fd].fde_file;
1551#else /* __rtems__ */
1552                fget_unlocked(fdp, fd->fd, NULL, &fp, NULL);
1553#endif /* __rtems__ */
1554#ifdef CAPABILITIES
1555                if (fp == NULL ||
1556                    cap_check(cap_rights(fdp, fd->fd), &cap_event_rights) != 0)
1557#else
1558                if (fp == NULL)
1559#endif
1560                {
1561                        fd->revents = POLLNVAL;
1562                        n++;
1563                        continue;
1564                }
1565
1566                /*
1567                 * Note: backend also returns POLLHUP and
1568                 * POLLERR if appropriate.
1569                 */
1570                fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1571#ifdef __rtems__
1572                rtems_libio_iop_drop(&fp->f_io);
1573#endif /* __rtems__ */
1574                if (fd->revents != 0)
1575                        n++;
1576        }
1577        FILEDESC_SUNLOCK(fdp);
1578        stp->st_flags = 0;
1579        td->td_retval[0] = n;
1580        return (0);
1581}
1582
1583
1584static int
1585pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
1586{
1587        int error = 0;
1588        u_int i = 0;
1589        u_int n = 0;
1590
1591        for (i = 0; i < nfd; i++) {
1592                error = copyout(&fds->revents, &ufds->revents,
1593                    sizeof(ufds->revents));
1594                if (error)
1595                        return (error);
1596                if (fds->revents != 0)
1597                        n++;
1598                fds++;
1599                ufds++;
1600        }
1601        td->td_retval[0] = n;
1602        return (0);
1603}
1604
1605static int
1606pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
1607{
1608#ifndef __rtems__
1609        struct filedesc *fdp = td->td_proc->p_fd;
1610#else /* __rtems__ */
1611        struct filedesc *fdp = NULL;
1612#endif /* __rtems__ */
1613        struct file *fp;
1614        int i, n = 0;
1615
1616        FILEDESC_SLOCK(fdp);
1617        for (i = 0; i < nfd; i++, fds++) {
1618#ifndef __rtems__
1619                if (fds->fd > fdp->fd_lastfile) {
1620#else /* __rtems__ */
1621                if ((uint32_t)fds->fd >= rtems_libio_number_iops) {
1622#endif /* __rtems__ */
1623                        fds->revents = POLLNVAL;
1624                        n++;
1625                } else if (fds->fd < 0) {
1626                        fds->revents = 0;
1627                } else {
1628#ifndef __rtems__
1629                        fp = fdp->fd_ofiles[fds->fd].fde_file;
1630#else /* __rtems__ */
1631                        fget_unlocked(fdp, fds->fd, NULL, &fp, NULL);
1632#endif /* __rtems__ */
1633#ifdef CAPABILITIES
1634                        if (fp == NULL ||
1635                            cap_check(cap_rights(fdp, fds->fd), &cap_event_rights) != 0)
1636#else
1637                        if (fp == NULL)
1638#endif
1639                        {
1640                                fds->revents = POLLNVAL;
1641                                n++;
1642                        } else {
1643                                /*
1644                                 * Note: backend also returns POLLHUP and
1645                                 * POLLERR if appropriate.
1646                                 */
1647                                selfdalloc(td, fds);
1648                                fds->revents = fo_poll(fp, fds->events,
1649                                    td->td_ucred, td);
1650#ifdef __rtems__
1651                                rtems_libio_iop_drop(&fp->f_io);
1652#endif /* __rtems__ */
1653                                /*
1654                                 * POSIX requires POLLOUT to be never
1655                                 * set simultaneously with POLLHUP.
1656                                 */
1657                                if ((fds->revents & POLLHUP) != 0)
1658                                        fds->revents &= ~POLLOUT;
1659
1660                                if (fds->revents != 0)
1661                                        n++;
1662                        }
1663                }
1664        }
1665        FILEDESC_SUNLOCK(fdp);
1666        td->td_retval[0] = n;
1667        return (0);
1668}
1669
1670#ifndef __rtems__
1671/*
1672 * XXX This was created specifically to support netncp and netsmb.  This
1673 * allows the caller to specify a socket to wait for events on.  It returns
1674 * 0 if any events matched and an error otherwise.  There is no way to
1675 * determine which events fired.
1676 */
1677int
1678selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1679{
1680        struct timeval rtv;
1681        sbintime_t asbt, precision, rsbt;
1682        int error;
1683
1684        precision = 0;  /* stupid gcc! */
1685        if (tvp != NULL) {
1686                rtv = *tvp;
1687                if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1688                    rtv.tv_usec >= 1000000)
1689                        return (EINVAL);
1690                if (!timevalisset(&rtv))
1691                        asbt = 0;
1692                else if (rtv.tv_sec <= INT32_MAX) {
1693                        rsbt = tvtosbt(rtv);
1694                        precision = rsbt;
1695                        precision >>= tc_precexp;
1696                        if (TIMESEL(&asbt, rsbt))
1697                                asbt += tc_tick_sbt;
1698                        if (asbt <= SBT_MAX - rsbt)
1699                                asbt += rsbt;
1700                        else
1701                                asbt = -1;
1702                } else
1703                        asbt = -1;
1704        } else
1705                asbt = -1;
1706        seltdinit(td);
1707        /*
1708         * Iterate until the timeout expires or the socket becomes ready.
1709         */
1710        for (;;) {
1711                selfdalloc(td, NULL);
1712                error = sopoll(so, events, NULL, td);
1713                /* error here is actually the ready events. */
1714                if (error)
1715                        return (0);
1716                error = seltdwait(td, asbt, precision);
1717                if (error)
1718                        break;
1719        }
1720        seltdclear(td);
1721        /* XXX Duplicates ncp/smb behavior. */
1722        if (error == ERESTART)
1723                error = 0;
1724        return (error);
1725}
1726#endif /* __rtems__ */
1727
1728/*
1729 * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1730 * have two select sets, one for read and another for write.
1731 */
1732static void
1733selfdalloc(struct thread *td, void *cookie)
1734{
1735        struct seltd *stp;
1736
1737        stp = td->td_sel;
1738        if (stp->st_free1 == NULL)
1739                stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1740        stp->st_free1->sf_td = stp;
1741        stp->st_free1->sf_cookie = cookie;
1742        if (stp->st_free2 == NULL)
1743                stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1744        stp->st_free2->sf_td = stp;
1745        stp->st_free2->sf_cookie = cookie;
1746}
1747
1748static void
1749selfdfree(struct seltd *stp, struct selfd *sfp)
1750{
1751        STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1752        if (sfp->sf_si != NULL) {
1753                mtx_lock(sfp->sf_mtx);
1754                if (sfp->sf_si != NULL) {
1755                        TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1756                        refcount_release(&sfp->sf_refs);
1757                }
1758                mtx_unlock(sfp->sf_mtx);
1759        }
1760        if (refcount_release(&sfp->sf_refs))
1761                uma_zfree(selfd_zone, sfp);
1762}
1763
1764/* Drain the waiters tied to all the selfd belonging the specified selinfo. */
1765void
1766seldrain(struct selinfo *sip)
1767{
1768
1769        /*
1770         * This feature is already provided by doselwakeup(), thus it is
1771         * enough to go for it.
1772         * Eventually, the context, should take care to avoid races
1773         * between thread calling select()/poll() and file descriptor
1774         * detaching, but, again, the races are just the same as
1775         * selwakeup().
1776         */
1777        doselwakeup(sip, -1);
1778}
1779
1780/*
1781 * Record a select request.
1782 */
1783void
1784selrecord(struct thread *selector, struct selinfo *sip)
1785{
1786        struct selfd *sfp;
1787        struct seltd *stp;
1788        struct mtx *mtxp;
1789
1790        stp = selector->td_sel;
1791        /*
1792         * Don't record when doing a rescan.
1793         */
1794        if (stp->st_flags & SELTD_RESCAN)
1795                return;
1796        /*
1797         * Grab one of the preallocated descriptors.
1798         */
1799        sfp = NULL;
1800        if ((sfp = stp->st_free1) != NULL)
1801                stp->st_free1 = NULL;
1802        else if ((sfp = stp->st_free2) != NULL)
1803                stp->st_free2 = NULL;
1804        else
1805                panic("selrecord: No free selfd on selq");
1806        mtxp = sip->si_mtx;
1807        if (mtxp == NULL)
1808                mtxp = mtx_pool_find(mtxpool_select, sip);
1809        /*
1810         * Initialize the sfp and queue it in the thread.
1811         */
1812        sfp->sf_si = sip;
1813        sfp->sf_mtx = mtxp;
1814        refcount_init(&sfp->sf_refs, 2);
1815        STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1816        /*
1817         * Now that we've locked the sip, check for initialization.
1818         */
1819        mtx_lock(mtxp);
1820        if (sip->si_mtx == NULL) {
1821                sip->si_mtx = mtxp;
1822                TAILQ_INIT(&sip->si_tdlist);
1823        }
1824        /*
1825         * Add this thread to the list of selfds listening on this selinfo.
1826         */
1827        TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1828        mtx_unlock(sip->si_mtx);
1829}
1830
1831/* Wake up a selecting thread. */
1832void
1833selwakeup(struct selinfo *sip)
1834{
1835        doselwakeup(sip, -1);
1836}
1837
1838/* Wake up a selecting thread, and set its priority. */
1839void
1840selwakeuppri(struct selinfo *sip, int pri)
1841{
1842        doselwakeup(sip, pri);
1843}
1844
1845/*
1846 * Do a wakeup when a selectable event occurs.
1847 */
1848static void
1849doselwakeup(struct selinfo *sip, int pri)
1850{
1851        struct selfd *sfp;
1852        struct selfd *sfn;
1853        struct seltd *stp;
1854
1855        /* If it's not initialized there can't be any waiters. */
1856        if (sip->si_mtx == NULL)
1857                return;
1858        /*
1859         * Locking the selinfo locks all selfds associated with it.
1860         */
1861        mtx_lock(sip->si_mtx);
1862        TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1863                /*
1864                 * Once we remove this sfp from the list and clear the
1865                 * sf_si seltdclear will know to ignore this si.
1866                 */
1867                TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1868                sfp->sf_si = NULL;
1869                stp = sfp->sf_td;
1870                mtx_lock(&stp->st_mtx);
1871                stp->st_flags |= SELTD_PENDING;
1872                cv_broadcastpri(&stp->st_wait, pri);
1873                mtx_unlock(&stp->st_mtx);
1874                if (refcount_release(&sfp->sf_refs))
1875                        uma_zfree(selfd_zone, sfp);
1876        }
1877        mtx_unlock(sip->si_mtx);
1878}
1879
1880static void
1881seltdinit(struct thread *td)
1882{
1883        struct seltd *stp;
1884
1885        if ((stp = td->td_sel) != NULL)
1886                goto out;
1887        td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1888        mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1889        cv_init(&stp->st_wait, "select");
1890out:
1891        stp->st_flags = 0;
1892        STAILQ_INIT(&stp->st_selq);
1893}
1894
1895static int
1896seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1897{
1898        struct seltd *stp;
1899        int error;
1900
1901        stp = td->td_sel;
1902        /*
1903         * An event of interest may occur while we do not hold the seltd
1904         * locked so check the pending flag before we sleep.
1905         */
1906        mtx_lock(&stp->st_mtx);
1907        /*
1908         * Any further calls to selrecord will be a rescan.
1909         */
1910        stp->st_flags |= SELTD_RESCAN;
1911        if (stp->st_flags & SELTD_PENDING) {
1912                mtx_unlock(&stp->st_mtx);
1913                return (0);
1914        }
1915        if (sbt == 0)
1916                error = EWOULDBLOCK;
1917        else if (sbt != -1)
1918                error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1919                    sbt, precision, C_ABSOLUTE);
1920        else
1921                error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1922        mtx_unlock(&stp->st_mtx);
1923
1924        return (error);
1925}
1926
1927void
1928seltdfini(struct thread *td)
1929{
1930        struct seltd *stp;
1931
1932        stp = td->td_sel;
1933        if (stp == NULL)
1934                return;
1935        if (stp->st_free1)
1936                uma_zfree(selfd_zone, stp->st_free1);
1937        if (stp->st_free2)
1938                uma_zfree(selfd_zone, stp->st_free2);
1939        td->td_sel = NULL;
1940        cv_destroy(&stp->st_wait);
1941        mtx_destroy(&stp->st_mtx);
1942        free(stp, M_SELECT);
1943}
1944
1945/*
1946 * Remove the references to the thread from all of the objects we were
1947 * polling.
1948 */
1949static void
1950seltdclear(struct thread *td)
1951{
1952        struct seltd *stp;
1953        struct selfd *sfp;
1954        struct selfd *sfn;
1955
1956        stp = td->td_sel;
1957        STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1958                selfdfree(stp, sfp);
1959        stp->st_flags = 0;
1960}
1961
1962static void selectinit(void *);
1963SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1964static void
1965selectinit(void *dummy __unused)
1966{
1967
1968        selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1969            NULL, NULL, UMA_ALIGN_PTR, 0);
1970        mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1971}
1972
1973#ifndef __rtems__
1974/*
1975 * Set up a syscall return value that follows the convention specified for
1976 * posix_* functions.
1977 */
1978int
1979kern_posix_error(struct thread *td, int error)
1980{
1981
1982        if (error <= 0)
1983                return (error);
1984        td->td_errno = error;
1985        td->td_pflags |= TDP_NERRNO;
1986        td->td_retval[0] = error;
1987        return (0);
1988}
1989#endif /* __rtems__ */
1990#ifdef __rtems__
1991#include <machine/rtems-bsd-thread.h>
1992
1993#undef ticks
1994
1995#include <rtems/score/objectimpl.h>
1996#include <rtems/score/threadimpl.h>
1997
1998#include <rtems/bsd/util.h>
1999
2000static void
2001force_select_timeout(Thread_Control *thread)
2002{
2003        struct thread *td = rtems_bsd_get_thread(thread);
2004
2005        if (td != NULL) {
2006                struct seltd *stp = td->td_sel;
2007
2008                cv_broadcastpri(&stp->st_wait, 0);
2009        }
2010}
2011
2012rtems_status_code rtems_bsd_force_select_timeout(rtems_id task_id)
2013{
2014        Thread_Control *thread;
2015        ISR_lock_Context lock_context;
2016
2017        thread = _Thread_Get(task_id, &lock_context);
2018        if (thread == NULL) {
2019#if defined(RTEMS_MULTIPROCESSING)
2020                if (_Thread_MP_Is_remote(id)) {
2021                        return (RTEMS_ILLEGAL_ON_REMOTE_OBJECT);
2022                }
2023#endif
2024
2025                return (RTEMS_INVALID_ID);
2026        }
2027
2028        _ISR_lock_ISR_enable(&lock_context);
2029        force_select_timeout(thread);
2030        return (RTEMS_SUCCESSFUL);
2031}
2032#endif /* __rtems__ */
Note: See TracBrowser for help on using the repository browser.