kernel - make POLLHUP conform better to OpenGroup
[dragonfly.git] / sys / kern / sys_generic.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
2494f282 40 * $DragonFly: src/sys/kern/sys_generic.c,v 1.49 2008/05/05 22:09:44 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/sysproto.h>
e5857bf7 48#include <sys/event.h>
984263bc
MD
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>
ba023347 58#include <sys/kern_syscall.h>
984263bc 59#include <sys/malloc.h>
a0c5fc96 60#include <sys/mapped_ioctl.h>
984263bc 61#include <sys/poll.h>
a0c5fc96 62#include <sys/queue.h>
984263bc 63#include <sys/resourcevar.h>
8b5c39bb 64#include <sys/socketops.h>
984263bc
MD
65#include <sys/sysctl.h>
66#include <sys/sysent.h>
67#include <sys/buf.h>
68#ifdef KTRACE
69#include <sys/ktrace.h>
70#endif
71#include <vm/vm.h>
72#include <vm/vm_page.h>
684a93c4 73
dadab5e9 74#include <sys/file2.h>
684a93c4 75#include <sys/mplock2.h>
ac62ea3c 76#include <sys/spinlock2.h>
984263bc
MD
77
78#include <machine/limits.h>
79
80static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
a0c5fc96 81static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
984263bc
MD
82static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
83MALLOC_DEFINE(M_IOV, "iov", "large iov's");
84
e5857bf7 85typedef struct kfd_set {
8acdf1cf 86 fd_mask fds_bits[2];
e5857bf7
SG
87} kfd_set;
88
89enum select_copyin_states {
90 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
91
92struct select_kevent_copyin_args {
93 kfd_set *read_set;
94 kfd_set *write_set;
95 kfd_set *except_set;
96 int active_set; /* One of select_copyin_states */
97 struct lwp *lwp; /* Pointer to our lwp */
98 int num_fds; /* Number of file descriptors (syscall arg) */
99 int proc_fds; /* Processed fd's (wraps) */
100 int error; /* Returned to userland */
101};
102
7fbfbe29
SG
103struct poll_kevent_copyin_args {
104 struct lwp *lwp;
105 struct pollfd *fds;
106 int nfds;
107 int pfds;
108 int error;
109};
110
ae7cb1b5 111static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
8acdf1cf 112 struct timespec *ts, int *res);
7fbfbe29
SG
113static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
114 int *res);
e54488bb
MD
115static int dofileread(int, struct file *, struct uio *, int, size_t *);
116static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
7f83ed38 117
984263bc
MD
118/*
119 * Read system call.
f832287e
MD
120 *
121 * MPSAFE
984263bc 122 */
984263bc 123int
753fd850 124sys_read(struct read_args *uap)
984263bc 125{
dadab5e9 126 struct thread *td = curthread;
ba023347
DRJ
127 struct uio auio;
128 struct iovec aiov;
984263bc
MD
129 int error;
130
e54488bb
MD
131 if ((ssize_t)uap->nbyte < 0)
132 error = EINVAL;
133
ba023347
DRJ
134 aiov.iov_base = uap->buf;
135 aiov.iov_len = uap->nbyte;
136 auio.uio_iov = &aiov;
137 auio.uio_iovcnt = 1;
138 auio.uio_offset = -1;
139 auio.uio_resid = uap->nbyte;
140 auio.uio_rw = UIO_READ;
141 auio.uio_segflg = UIO_USERSPACE;
142 auio.uio_td = td;
143
e54488bb 144 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
984263bc
MD
145 return(error);
146}
147
148/*
7f83ed38 149 * Positioned (Pread) read system call
f832287e
MD
150 *
151 * MPSAFE
984263bc 152 */
984263bc 153int
b09fd398 154sys_extpread(struct extpread_args *uap)
984263bc 155{
dadab5e9 156 struct thread *td = curthread;
ba023347
DRJ
157 struct uio auio;
158 struct iovec aiov;
984263bc 159 int error;
9ba76b73 160 int flags;
984263bc 161
e54488bb
MD
162 if ((ssize_t)uap->nbyte < 0)
163 return(EINVAL);
164
ba023347
DRJ
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_offset = uap->offset;
170 auio.uio_resid = uap->nbyte;
171 auio.uio_rw = UIO_READ;
172 auio.uio_segflg = UIO_USERSPACE;
173 auio.uio_td = td;
174
9ba76b73
MD
175 flags = uap->flags & O_FMASK;
176 if (uap->offset != (off_t)-1)
177 flags |= O_FOFFSET;
178
e54488bb 179 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
984263bc
MD
180 return(error);
181}
182
7f83ed38
MD
183/*
184 * Scatter read system call.
f832287e
MD
185 *
186 * MPSAFE
7f83ed38 187 */
984263bc 188int
753fd850 189sys_readv(struct readv_args *uap)
984263bc 190{
dadab5e9 191 struct thread *td = curthread;
984263bc 192 struct uio auio;
ba023347
DRJ
193 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
194 int error;
984263bc 195
ba023347 196 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 197 &auio.uio_resid);
ba023347
DRJ
198 if (error)
199 return (error);
200 auio.uio_iov = iov;
201 auio.uio_iovcnt = uap->iovcnt;
202 auio.uio_offset = -1;
984263bc
MD
203 auio.uio_rw = UIO_READ;
204 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 205 auio.uio_td = td;
984263bc 206
e54488bb 207 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
ba023347
DRJ
208
209 iovec_free(&iov, aiov);
984263bc
MD
210 return (error);
211}
212
7f83ed38
MD
213
214/*
215 * Scatter positioned read system call.
f832287e
MD
216 *
217 * MPSAFE
7f83ed38
MD
218 */
219int
b09fd398 220sys_extpreadv(struct extpreadv_args *uap)
7f83ed38
MD
221{
222 struct thread *td = curthread;
223 struct uio auio;
224 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
225 int error;
9ba76b73 226 int flags;
7f83ed38
MD
227
228 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 229 &auio.uio_resid);
7f83ed38
MD
230 if (error)
231 return (error);
232 auio.uio_iov = iov;
233 auio.uio_iovcnt = uap->iovcnt;
234 auio.uio_offset = uap->offset;
235 auio.uio_rw = UIO_READ;
236 auio.uio_segflg = UIO_USERSPACE;
237 auio.uio_td = td;
238
9ba76b73
MD
239 flags = uap->flags & O_FMASK;
240 if (uap->offset != (off_t)-1)
241 flags |= O_FOFFSET;
242
e54488bb 243 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
7f83ed38
MD
244
245 iovec_free(&iov, aiov);
246 return(error);
247}
248
f832287e
MD
249/*
250 * MPSAFE
251 */
984263bc 252int
e54488bb 253kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
984263bc 254{
dadab5e9
MD
255 struct thread *td = curthread;
256 struct proc *p = td->td_proc;
41c20dac 257 struct file *fp;
7f83ed38 258 int error;
984263bc 259
ba023347
DRJ
260 KKASSERT(p);
261
228b401d 262 fp = holdfp(p->p_fd, fd, FREAD);
ba023347 263 if (fp == NULL)
984263bc 264 return (EBADF);
9ba76b73 265 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
ba023347 266 error = ESPIPE;
7f83ed38
MD
267 } else {
268 error = dofileread(fd, fp, auio, flags, res);
984263bc 269 }
9f87144f 270 fdrop(fp);
7f83ed38
MD
271 return(error);
272}
273
274/*
275 * Common code for readv and preadv that reads data in
276 * from a file using the passed in uio, offset, and flags.
f832287e
MD
277 *
278 * MPALMOSTSAFE - ktrace needs help
7f83ed38
MD
279 */
280static int
e54488bb 281dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
7f83ed38 282{
7f83ed38 283 int error;
e54488bb 284 size_t len;
7f83ed38 285#ifdef KTRACE
fc9ae81d 286 struct thread *td = curthread;
7f83ed38
MD
287 struct iovec *ktriov = NULL;
288 struct uio ktruio;
289#endif
290
984263bc
MD
291#ifdef KTRACE
292 /*
293 * if tracing, save a copy of iovec
294 */
dadab5e9 295 if (KTRPOINT(td, KTR_GENIO)) {
ba023347
DRJ
296 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
297
984263bc 298 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
ba023347
DRJ
299 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
300 ktruio = *auio;
984263bc
MD
301 }
302#endif
ba023347 303 len = auio->uio_resid;
87de5057 304 error = fo_read(fp, auio, fp->f_cred, flags);
ba023347
DRJ
305 if (error) {
306 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
307 error == EINTR || error == EWOULDBLOCK))
308 error = 0;
309 }
984263bc
MD
310#ifdef KTRACE
311 if (ktriov != NULL) {
312 if (error == 0) {
313 ktruio.uio_iov = ktriov;
ba023347 314 ktruio.uio_resid = len - auio->uio_resid;
f832287e 315 get_mplock();
9fb04d14 316 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
f832287e 317 rel_mplock();
984263bc
MD
318 }
319 FREE(ktriov, M_TEMP);
320 }
321#endif
ba023347
DRJ
322 if (error == 0)
323 *res = len - auio->uio_resid;
7f83ed38
MD
324
325 return(error);
984263bc
MD
326}
327
328/*
329 * Write system call
f832287e
MD
330 *
331 * MPSAFE
984263bc 332 */
984263bc 333int
753fd850 334sys_write(struct write_args *uap)
984263bc 335{
dadab5e9 336 struct thread *td = curthread;
ba023347
DRJ
337 struct uio auio;
338 struct iovec aiov;
984263bc
MD
339 int error;
340
e54488bb
MD
341 if ((ssize_t)uap->nbyte < 0)
342 error = EINVAL;
343
ba023347
DRJ
344 aiov.iov_base = (void *)(uintptr_t)uap->buf;
345 aiov.iov_len = uap->nbyte;
346 auio.uio_iov = &aiov;
347 auio.uio_iovcnt = 1;
348 auio.uio_offset = -1;
349 auio.uio_resid = uap->nbyte;
350 auio.uio_rw = UIO_WRITE;
351 auio.uio_segflg = UIO_USERSPACE;
352 auio.uio_td = td;
353
e54488bb 354 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
dadab5e9 355
984263bc
MD
356 return(error);
357}
358
359/*
360 * Pwrite system call
f832287e
MD
361 *
362 * MPSAFE
984263bc 363 */
984263bc 364int
b09fd398 365sys_extpwrite(struct extpwrite_args *uap)
984263bc 366{
dadab5e9 367 struct thread *td = curthread;
ba023347
DRJ
368 struct uio auio;
369 struct iovec aiov;
984263bc 370 int error;
9ba76b73 371 int flags;
984263bc 372
e54488bb
MD
373 if ((ssize_t)uap->nbyte < 0)
374 error = EINVAL;
375
ba023347
DRJ
376 aiov.iov_base = (void *)(uintptr_t)uap->buf;
377 aiov.iov_len = uap->nbyte;
378 auio.uio_iov = &aiov;
379 auio.uio_iovcnt = 1;
380 auio.uio_offset = uap->offset;
381 auio.uio_resid = uap->nbyte;
382 auio.uio_rw = UIO_WRITE;
383 auio.uio_segflg = UIO_USERSPACE;
384 auio.uio_td = td;
385
9ba76b73
MD
386 flags = uap->flags & O_FMASK;
387 if (uap->offset != (off_t)-1)
388 flags |= O_FOFFSET;
e54488bb 389 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
984263bc
MD
390 return(error);
391}
392
f832287e
MD
393/*
394 * MPSAFE
395 */
ba023347 396int
753fd850 397sys_writev(struct writev_args *uap)
ba023347 398{
dadab5e9 399 struct thread *td = curthread;
984263bc 400 struct uio auio;
ba023347
DRJ
401 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
402 int error;
984263bc 403
ba023347 404 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 405 &auio.uio_resid);
ba023347
DRJ
406 if (error)
407 return (error);
408 auio.uio_iov = iov;
409 auio.uio_iovcnt = uap->iovcnt;
410 auio.uio_offset = -1;
984263bc
MD
411 auio.uio_rw = UIO_WRITE;
412 auio.uio_segflg = UIO_USERSPACE;
dadab5e9 413 auio.uio_td = td;
ba023347 414
e54488bb 415 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
ba023347
DRJ
416
417 iovec_free(&iov, aiov);
984263bc
MD
418 return (error);
419}
420
7f83ed38 421
984263bc 422/*
7f83ed38 423 * Gather positioned write system call
f832287e
MD
424 *
425 * MPSAFE
984263bc 426 */
984263bc 427int
b09fd398 428sys_extpwritev(struct extpwritev_args *uap)
7f83ed38
MD
429{
430 struct thread *td = curthread;
431 struct uio auio;
432 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
433 int error;
9ba76b73 434 int flags;
7f83ed38
MD
435
436 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
ef5c76d7 437 &auio.uio_resid);
7f83ed38
MD
438 if (error)
439 return (error);
440 auio.uio_iov = iov;
441 auio.uio_iovcnt = uap->iovcnt;
442 auio.uio_offset = uap->offset;
443 auio.uio_rw = UIO_WRITE;
444 auio.uio_segflg = UIO_USERSPACE;
445 auio.uio_td = td;
446
9ba76b73
MD
447 flags = uap->flags & O_FMASK;
448 if (uap->offset != (off_t)-1)
449 flags |= O_FOFFSET;
450
e54488bb 451 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
7f83ed38
MD
452
453 iovec_free(&iov, aiov);
454 return(error);
455}
456
f832287e
MD
457/*
458 * MPSAFE
459 */
7f83ed38 460int
e54488bb 461kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
984263bc 462{
dadab5e9
MD
463 struct thread *td = curthread;
464 struct proc *p = td->td_proc;
41c20dac 465 struct file *fp;
7f83ed38 466 int error;
984263bc 467
dadab5e9 468 KKASSERT(p);
dadab5e9 469
228b401d 470 fp = holdfp(p->p_fd, fd, FWRITE);
ba023347 471 if (fp == NULL)
984263bc 472 return (EBADF);
9ba76b73 473 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
ba023347 474 error = ESPIPE;
7f83ed38
MD
475 } else {
476 error = dofilewrite(fd, fp, auio, flags, res);
984263bc 477 }
7f83ed38 478
9f87144f 479 fdrop(fp);
7f83ed38
MD
480 return (error);
481}
482
483/*
484 * Common code for writev and pwritev that writes data to
485 * a file using the passed in uio, offset, and flags.
f832287e
MD
486 *
487 * MPALMOSTSAFE - ktrace needs help
7f83ed38
MD
488 */
489static int
e54488bb 490dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
7f83ed38
MD
491{
492 struct thread *td = curthread;
7278a846 493 struct lwp *lp = td->td_lwp;
7f83ed38 494 int error;
e54488bb 495 size_t len;
7f83ed38
MD
496#ifdef KTRACE
497 struct iovec *ktriov = NULL;
498 struct uio ktruio;
499#endif
500
984263bc
MD
501#ifdef KTRACE
502 /*
503 * if tracing, save a copy of iovec and uio
504 */
dadab5e9 505 if (KTRPOINT(td, KTR_GENIO)) {
ba023347
DRJ
506 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
507
984263bc 508 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
ba023347
DRJ
509 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
510 ktruio = *auio;
984263bc
MD
511 }
512#endif
ba023347 513 len = auio->uio_resid;
87de5057 514 error = fo_write(fp, auio, fp->f_cred, flags);
ba023347
DRJ
515 if (error) {
516 if (auio->uio_resid != len && (error == ERESTART ||
984263bc
MD
517 error == EINTR || error == EWOULDBLOCK))
518 error = 0;
7f83ed38 519 /* Socket layer is responsible for issuing SIGPIPE. */
f832287e
MD
520 if (error == EPIPE) {
521 get_mplock();
9fb04d14 522 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
f832287e
MD
523 rel_mplock();
524 }
984263bc 525 }
984263bc
MD
526#ifdef KTRACE
527 if (ktriov != NULL) {
528 if (error == 0) {
529 ktruio.uio_iov = ktriov;
ba023347 530 ktruio.uio_resid = len - auio->uio_resid;
f832287e 531 get_mplock();
9fb04d14 532 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
f832287e 533 rel_mplock();
984263bc
MD
534 }
535 FREE(ktriov, M_TEMP);
536 }
537#endif
ba023347
DRJ
538 if (error == 0)
539 *res = len - auio->uio_resid;
7f83ed38
MD
540
541 return(error);
984263bc
MD
542}
543
544/*
545 * Ioctl system call
3919ced0
MD
546 *
547 * MPALMOSTSAFE
984263bc 548 */
984263bc 549int
753fd850 550sys_ioctl(struct ioctl_args *uap)
984263bc 551{
3919ced0
MD
552 int error;
553
554 get_mplock();
555 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
556 rel_mplock();
557 return (error);
a0c5fc96
JS
558}
559
560struct ioctl_map_entry {
561 const char *subsys;
562 struct ioctl_map_range *cmd_ranges;
563 LIST_ENTRY(ioctl_map_entry) entries;
564};
565
25b5b94d
SS
566/*
567 * The true heart of all ioctl syscall handlers (native, emulation).
568 * If map != NULL, it will be searched for a matching entry for com,
569 * and appropriate conversions/conversion functions will be utilized.
570 */
a0c5fc96 571int
87baaf0c
MD
572mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
573 struct sysmsg *msg)
a0c5fc96 574{
dadab5e9
MD
575 struct thread *td = curthread;
576 struct proc *p = td->td_proc;
87de5057 577 struct ucred *cred;
41c20dac 578 struct file *fp;
a0c5fc96 579 struct ioctl_map_range *iomc = NULL;
984263bc 580 int error;
1fd87d54 581 u_int size;
a0c5fc96 582 u_long ocom = com;
984263bc
MD
583 caddr_t data, memp;
584 int tmp;
585#define STK_PARAMS 128
586 union {
587 char stkbuf[STK_PARAMS];
588 long align;
589 } ubuf;
590
dadab5e9 591 KKASSERT(p);
9910d07b 592 cred = td->td_ucred;
984263bc 593
228b401d
MD
594 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
595 if (fp == NULL)
a0c5fc96
JS
596 return(EBADF);
597
598 if (map != NULL) { /* obey translation map */
599 u_long maskcmd;
600 struct ioctl_map_entry *e;
601
602 maskcmd = com & map->mask;
603
604 LIST_FOREACH(e, &map->mapping, entries) {
605 for (iomc = e->cmd_ranges; iomc->start != 0 ||
25b5b94d
SS
606 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
607 iomc->mapfunc != NULL;
a0c5fc96
JS
608 iomc++) {
609 if (maskcmd >= iomc->start &&
610 maskcmd <= iomc->end)
611 break;
612 }
613
614 /* Did we find a match? */
615 if (iomc->start != 0 || iomc->maptocmd != 0 ||
25b5b94d 616 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
a0c5fc96
JS
617 break;
618 }
619
620 if (iomc == NULL ||
621 (iomc->start == 0 && iomc->maptocmd == 0
25b5b94d 622 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
6ea70f76 623 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
a0c5fc96
JS
624 map->sys, fd, maskcmd,
625 (int)((maskcmd >> 8) & 0xff),
626 (int)(maskcmd & 0xff));
228b401d
MD
627 error = EINVAL;
628 goto done;
a0c5fc96 629 }
984263bc 630
25b5b94d
SS
631 /*
632 * If it's a non-range one to one mapping, maptocmd should be
633 * correct. If it's a ranged one to one mapping, we pass the
634 * original value of com, and for a range mapped to a different
635 * range, we always need a mapping function to translate the
636 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
637 */
638 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
639 com = iomc->maptocmd;
640 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
641 if (iomc->mapfunc != NULL)
642 com = iomc->mapfunc(iomc->start, iomc->end,
643 iomc->start, iomc->end,
644 com, com);
645 } else {
646 if (iomc->mapfunc != NULL) {
647 com = iomc->mapfunc(iomc->start, iomc->end,
648 iomc->maptocmd, iomc->maptoend,
649 com, ocom);
650 } else {
6ea70f76 651 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
25b5b94d
SS
652 map->sys, fd, maskcmd,
653 (int)((maskcmd >> 8) & 0xff),
654 (int)(maskcmd & 0xff));
228b401d
MD
655 error = EINVAL;
656 goto done;
25b5b94d
SS
657 }
658 }
a0c5fc96
JS
659 }
660
661 switch (com) {
984263bc 662 case FIONCLEX:
228b401d
MD
663 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
664 goto done;
984263bc 665 case FIOCLEX:
228b401d
MD
666 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
667 goto done;
984263bc
MD
668 }
669
670 /*
671 * Interpret high order word to find amount of data to be
672 * copied to/from the user's address space.
673 */
674 size = IOCPARM_LEN(com);
228b401d
MD
675 if (size > IOCPARM_MAX) {
676 error = ENOTTY;
677 goto done;
678 }
984263bc 679
984263bc 680 if (size > sizeof (ubuf.stkbuf)) {
efda3bd0 681 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
984263bc
MD
682 data = memp;
683 } else {
d0d4a734 684 memp = NULL;
984263bc
MD
685 data = ubuf.stkbuf;
686 }
a0c5fc96
JS
687 if ((com & IOC_IN) != 0) {
688 if (size != 0) {
e54488bb 689 error = copyin(uspc_data, data, (size_t)size);
984263bc 690 if (error) {
a0c5fc96 691 if (memp != NULL)
efda3bd0 692 kfree(memp, M_IOCTLOPS);
228b401d 693 goto done;
984263bc
MD
694 }
695 } else {
a0c5fc96 696 *(caddr_t *)data = uspc_data;
984263bc 697 }
a0c5fc96 698 } else if ((com & IOC_OUT) != 0 && size) {
984263bc
MD
699 /*
700 * Zero the buffer so the user always
701 * gets back something deterministic.
702 */
e54488bb 703 bzero(data, (size_t)size);
a0c5fc96
JS
704 } else if ((com & IOC_VOID) != 0) {
705 *(caddr_t *)data = uspc_data;
984263bc
MD
706 }
707
708 switch (com) {
984263bc
MD
709 case FIONBIO:
710 if ((tmp = *(int *)data))
711 fp->f_flag |= FNONBLOCK;
712 else
713 fp->f_flag &= ~FNONBLOCK;
9ba76b73 714 error = 0;
984263bc
MD
715 break;
716
717 case FIOASYNC:
718 if ((tmp = *(int *)data))
719 fp->f_flag |= FASYNC;
720 else
721 fp->f_flag &= ~FASYNC;
87baaf0c 722 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
984263bc
MD
723 break;
724
725 default:
a0c5fc96
JS
726 /*
727 * If there is a override function,
728 * call it instead of directly routing the call
729 */
25b5b94d 730 if (map != NULL && iomc->wrapfunc != NULL)
87de5057 731 error = iomc->wrapfunc(fp, com, ocom, data, cred);
a0c5fc96 732 else
87baaf0c 733 error = fo_ioctl(fp, com, data, cred, msg);
984263bc
MD
734 /*
735 * Copy any data to user, size was
736 * already set and checked above.
737 */
a0c5fc96 738 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
e54488bb 739 error = copyout(data, uspc_data, (size_t)size);
984263bc
MD
740 break;
741 }
a0c5fc96 742 if (memp != NULL)
efda3bd0 743 kfree(memp, M_IOCTLOPS);
228b401d 744done:
9f87144f 745 fdrop(fp);
a0c5fc96
JS
746 return(error);
747}
748
749int
750mapped_ioctl_register_handler(struct ioctl_map_handler *he)
751{
752 struct ioctl_map_entry *ne;
753
754 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
755 he->subsys != NULL && *he->subsys != '\0');
756
efda3bd0 757 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
a0c5fc96
JS
758
759 ne->subsys = he->subsys;
760 ne->cmd_ranges = he->cmd_ranges;
761
762 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
763
764 return(0);
765}
766
767int
768mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
769{
770 struct ioctl_map_entry *ne;
771
772 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
773
774 LIST_FOREACH(ne, &he->map->mapping, entries) {
775 if (ne->cmd_ranges != he->cmd_ranges)
776 continue;
777 LIST_REMOVE(ne, entries);
efda3bd0 778 kfree(ne, M_IOCTLMAP);
a0c5fc96
JS
779 return(0);
780 }
781 return(EINVAL);
984263bc
MD
782}
783
784static int nselcoll; /* Select collisions since boot */
785int selwait;
786SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
fe24d605
MD
787static int nseldebug;
788SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
984263bc
MD
789
790/*
791 * Select system call.
3919ced0 792 *
e5857bf7 793 * MPSAFE
984263bc 794 */
984263bc 795int
753fd850 796sys_select(struct select_args *uap)
984263bc 797{
ae7cb1b5 798 struct timeval ktv;
e5857bf7 799 struct timespec *ktsp, kts;
ae7cb1b5
NT
800 int error;
801
802 /*
803 * Get timeout if any.
804 */
805 if (uap->tv != NULL) {
806 error = copyin(uap->tv, &ktv, sizeof (ktv));
807 if (error)
808 return (error);
e5857bf7
SG
809 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
810 ktsp = &kts;
ae7cb1b5 811 } else {
e5857bf7 812 ktsp = NULL;
ae7cb1b5
NT
813 }
814
815 /*
816 * Do real work.
817 */
e5857bf7 818 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
8acdf1cf 819 &uap->sysmsg_result);
ae7cb1b5
NT
820
821 return (error);
822}
823
824
825/*
826 * Pselect system call.
3919ced0
MD
827 *
828 * MPALMOSTSAFE
ae7cb1b5
NT
829 */
830int
831sys_pselect(struct pselect_args *uap)
832{
833 struct thread *td = curthread;
834 struct lwp *lp = td->td_lwp;
e5857bf7 835 struct timespec *ktsp, kts;
ae7cb1b5
NT
836 sigset_t sigmask;
837 int error;
838
839 /*
e5857bf7 840 * Get timeout if any.
ae7cb1b5
NT
841 */
842 if (uap->ts != NULL) {
843 error = copyin(uap->ts, &kts, sizeof (kts));
844 if (error)
845 return (error);
e5857bf7 846 ktsp = &kts;
ae7cb1b5 847 } else {
e5857bf7 848 ktsp = NULL;
ae7cb1b5
NT
849 }
850
851 /*
852 * Install temporary signal mask if any provided.
853 */
854 if (uap->sigmask != NULL) {
855 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
856 if (error)
857 return (error);
3919ced0 858 get_mplock();
ae7cb1b5
NT
859 lp->lwp_oldsigmask = lp->lwp_sigmask;
860 SIG_CANTMASK(sigmask);
861 lp->lwp_sigmask = sigmask;
3919ced0
MD
862 } else {
863 get_mplock();
ae7cb1b5
NT
864 }
865
866 /*
867 * Do real job.
868 */
e5857bf7 869 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
8acdf1cf 870 &uap->sysmsg_result);
ae7cb1b5
NT
871
872 if (uap->sigmask != NULL) {
873 /* doselect() responsible for turning ERESTART into EINTR */
874 KKASSERT(error != ERESTART);
875 if (error == EINTR) {
876 /*
877 * We can't restore the previous signal mask now
878 * because it could block the signal that interrupted
879 * us. So make a note to restore it after executing
880 * the handler.
881 */
882 lp->lwp_flag |= LWP_OLDMASK;
883 } else {
884 /*
885 * No handler to run. Restore previous mask immediately.
886 */
887 lp->lwp_sigmask = lp->lwp_oldsigmask;
888 }
889 }
3919ced0 890 rel_mplock();
ae7cb1b5
NT
891
892 return (error);
893}
894
e5857bf7 895static int
8acdf1cf 896select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
e5857bf7
SG
897{
898 struct select_kevent_copyin_args *skap = NULL;
8acdf1cf 899 struct kevent *kev;
e5857bf7
SG
900 int fd;
901 kfd_set *fdp = NULL;
902 short filter = 0;
903 u_int fflags = 0;
904
905 skap = (struct select_kevent_copyin_args *)arg;
906
8acdf1cf
MD
907 if (*events == maxevents)
908 return (0);
909
e5857bf7
SG
910 while (skap->active_set < COPYIN_DONE) {
911 switch (skap->active_set) {
912 case COPYIN_READ:
8acdf1cf
MD
913 /*
914 * Register descriptors for the read filter
915 */
e5857bf7
SG
916 fdp = skap->read_set;
917 filter = EVFILT_READ;
918 fflags = 0;
8acdf1cf
MD
919 if (fdp)
920 break;
921 ++skap->active_set;
922 skap->proc_fds = 0;
923 /* fall through */
e5857bf7 924 case COPYIN_WRITE:
8acdf1cf
MD
925 /*
926 * Register descriptors for the write filter
927 */
e5857bf7
SG
928 fdp = skap->write_set;
929 filter = EVFILT_WRITE;
930 fflags = 0;
8acdf1cf
MD
931 if (fdp)
932 break;
933 ++skap->active_set;
934 skap->proc_fds = 0;
935 /* fall through */
e5857bf7 936 case COPYIN_EXCEPT:
8acdf1cf
MD
937 /*
938 * Register descriptors for the exception filter
939 */
e5857bf7 940 fdp = skap->except_set;
73c344d3 941 filter = EVFILT_EXCEPT;
e5857bf7 942 fflags = NOTE_OOB;
8acdf1cf
MD
943 if (fdp)
944 break;
945 ++skap->active_set;
946 skap->proc_fds = 0;
947 /* fall through */
948 case COPYIN_DONE:
949 /*
950 * Nothing left to register
951 */
952 return(0);
953 /* NOT REACHED */
e5857bf7
SG
954 }
955
8acdf1cf
MD
956 while (skap->proc_fds < skap->num_fds) {
957 fd = skap->proc_fds;
e5857bf7 958 if (FD_ISSET(fd, fdp)) {
8acdf1cf
MD
959 kev = &kevp[*events];
960 EV_SET(kev, fd, filter,
961 EV_ADD|EV_ENABLE,
962 fflags, 0,
b4372719
MD
963 (void *)(uintptr_t)
964 skap->lwp->lwp_kqueue_serial);
e5857bf7 965 FD_CLR(fd, fdp);
8acdf1cf 966 ++*events;
22690e50
SG
967
968 if (nseldebug)
969 kprintf("select fd %d filter %d serial %d\n",
970 fd, filter, skap->lwp->lwp_kqueue_serial);
e5857bf7 971 }
8acdf1cf
MD
972 ++skap->proc_fds;
973 if (*events == maxevents)
e5857bf7
SG
974 return (0);
975 }
e5857bf7
SG
976 skap->active_set++;
977 skap->proc_fds = 0;
978 }
979
980 return (0);
981}
982
983static int
984select_copyout(void *arg, struct kevent *kevp, int count, int *res)
985{
986 struct select_kevent_copyin_args *skap;
987 struct kevent kev;
73c344d3 988 int i = 0;
e5857bf7
SG
989
990 skap = (struct select_kevent_copyin_args *)arg;
991
e5857bf7 992 for (i = 0; i < count; ++i) {
22690e50
SG
993 /*
994 * Filter out and delete spurious events
995 */
b4372719
MD
996 if ((u_int)(uintptr_t)kevp[i].udata !=
997 skap->lwp->lwp_kqueue_serial) {
8acdf1cf 998 kev = kevp[i];
e5857bf7 999 kev.flags = EV_DISABLE|EV_DELETE;
8acdf1cf 1000 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
22690e50 1001 if (nseldebug)
c9a1a153
SW
1002 kprintf("select fd %ju mismatched serial %d\n",
1003 (uintmax_t)kevp[i].ident,
1004 skap->lwp->lwp_kqueue_serial);
22690e50
SG
1005 continue;
1006 }
1007
1008 /*
1009 * Handle errors
1010 */
1011 if (kevp[i].flags & EV_ERROR) {
1012 switch(kevp[i].data) {
1013 case EBADF:
1014 /*
1015 * A bad file descriptor is considered a
1016 * fatal error for select, bail out.
1017 */
1018 skap->error = EBADF;
1019 *res = 0;
1020 return (1);
1021 break;
1022 default:
1023 /*
1024 * Select silently swallows any unknown errors
1025 * for descriptors in the read or write sets.
4fbb9324
SG
1026 *
1027 * ALWAYS filter out EOPNOTSUPP errors from
1028 * filters (at least until all filters support
1029 * EVFILT_EXCEPT)
22690e50
SG
1030 */
1031 if (kevp[i].filter != EVFILT_READ &&
4fbb9324
SG
1032 kevp[i].filter != EVFILT_WRITE &&
1033 kevp[i].data != EOPNOTSUPP) {
22690e50
SG
1034 skap->error = kevp[i].data;
1035 *res = 0;
1036 return (1);
1037 }
1038 break;
1039 }
1040 if (nseldebug)
c9a1a153
SW
1041 kprintf("select fd %ju filter %d error %jd\n",
1042 (uintmax_t)kevp[i].ident,
1043 kevp[i].filter,
1044 (intmax_t)kevp[i].data);
e5857bf7
SG
1045 continue;
1046 }
1047
73c344d3
SG
1048 switch (kevp[i].filter) {
1049 case EVFILT_READ:
e5857bf7
SG
1050 FD_SET(kevp[i].ident, skap->read_set);
1051 break;
73c344d3 1052 case EVFILT_WRITE:
e5857bf7
SG
1053 FD_SET(kevp[i].ident, skap->write_set);
1054 break;
73c344d3 1055 case EVFILT_EXCEPT:
e5857bf7
SG
1056 FD_SET(kevp[i].ident, skap->except_set);
1057 break;
1058 }
1059
8acdf1cf 1060 ++*res;
e5857bf7
SG
1061 }
1062
1063 return (0);
1064}
1065
ae7cb1b5 1066/*
8acdf1cf
MD
1067 * Copy select bits in from userland. Allocate kernel memory if the
1068 * set is large.
1069 */
1070static int
1071getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1072{
1073 int error;
1074
1075 if (in_set) {
1076 if (bytes < sizeof(*tmp_set))
1077 *out_set = tmp_set;
1078 else
1079 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1080 error = copyin(in_set, *out_set, bytes);
1081 } else {
1082 *out_set = NULL;
1083 error = 0;
1084 }
1085 return (error);
1086}
1087
1088/*
1089 * Copy returned select bits back out to userland.
1090 */
1091static int
1092putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1093{
1094 int error;
1095
1096 if (in_set) {
1097 error = copyout(in_set, out_set, bytes);
1098 } else {
1099 error = 0;
1100 }
1101 return (error);
1102}
1103
1104/*
ae7cb1b5
NT
1105 * Common code for sys_select() and sys_pselect().
1106 *
e5857bf7 1107 * in, out and ex are userland pointers. ts must point to validated
ae7cb1b5
NT
1108 * kernel-side timeout value or NULL for infinite timeout. res must
1109 * point to syscall return value.
1110 */
1111static int
e5857bf7 1112doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
8acdf1cf 1113 struct timespec *ts, int *res)
ae7cb1b5 1114{
41c20dac 1115 struct proc *p = curproc;
e5857bf7
SG
1116 struct select_kevent_copyin_args *kap, ka;
1117 int bytes, error;
8acdf1cf
MD
1118 kfd_set read_tmp;
1119 kfd_set write_tmp;
1120 kfd_set except_tmp;
984263bc 1121
8acdf1cf 1122 *res = 0;
ae7cb1b5 1123 if (nd < 0)
984263bc 1124 return (EINVAL);
679058fb
MD
1125 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1126 nd = p->p_fd->fd_nfiles;
984263bc 1127
e5857bf7
SG
1128 kap = &ka;
1129 kap->lwp = curthread->td_lwp;
1130 kap->num_fds = nd;
1131 kap->proc_fds = 0;
1132 kap->error = 0;
8acdf1cf 1133 kap->active_set = COPYIN_READ;
e6f31a83 1134
8acdf1cf
MD
1135 /*
1136 * Calculate bytes based on the number of __fd_mask[] array entries
1137 * multiplied by the size of __fd_mask.
1138 */
1139 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
984263bc 1140
d0d4a734
MD
1141 /* kap->read_set = NULL; not needed */
1142 kap->write_set = NULL;
1143 kap->except_set = NULL;
1144
8acdf1cf
MD
1145 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1146 if (error == 0)
1147 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1148 if (error == 0)
1149 error = getbits(bytes, except, &kap->except_set, &except_tmp);
e5857bf7 1150 if (error)
8acdf1cf
MD
1151 goto done;
1152
1153 /*
1154 * NOTE: Make sure the max events passed to kern_kevent() is
1155 * effectively unlimited. (nd * 3) accomplishes this.
1156 *
1157 * (*res) continues to increment as returned events are
1158 * loaded in.
1159 */
679058fb 1160 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
8acdf1cf
MD
1161 select_copyin, select_copyout, ts);
1162 if (error == 0)
1163 error = putbits(bytes, kap->read_set, read);
1164 if (error == 0)
1165 error = putbits(bytes, kap->write_set, write);
1166 if (error == 0)
1167 error = putbits(bytes, kap->except_set, except);
984263bc 1168
8acdf1cf 1169 /*
22690e50
SG
1170 * An error from an individual event that should be passed
1171 * back to userland (EBADF)
8acdf1cf
MD
1172 */
1173 if (kap->error)
1174 error = kap->error;
e5857bf7 1175
8acdf1cf
MD
1176 /*
1177 * Clean up.
1178 */
1179done:
1180 if (kap->read_set && kap->read_set != &read_tmp)
1181 kfree(kap->read_set, M_SELECT);
1182 if (kap->write_set && kap->write_set != &write_tmp)
1183 kfree(kap->write_set, M_SELECT);
1184 if (kap->except_set && kap->except_set != &except_tmp)
1185 kfree(kap->except_set, M_SELECT);
1186
22690e50 1187 kap->lwp->lwp_kqueue_serial += kap->num_fds;
73c344d3 1188
e5857bf7 1189 return (error);
984263bc
MD
1190}
1191
1192/*
1193 * Poll system call.
3919ced0 1194 *
7fbfbe29 1195 * MPSAFE
984263bc 1196 */
984263bc 1197int
753fd850 1198sys_poll(struct poll_args *uap)
984263bc 1199{
7fbfbe29
SG
1200 struct timespec ts, *tsp;
1201 int error;
984263bc 1202
ab2eb4eb 1203 if (uap->timeout != INFTIM) {
7fbfbe29
SG
1204 ts.tv_sec = uap->timeout / 1000;
1205 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1206 tsp = &ts;
984263bc 1207 } else {
7fbfbe29 1208 tsp = NULL;
984263bc 1209 }
e6f31a83 1210
7fbfbe29
SG
1211 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1212
984263bc
MD
1213 return (error);
1214}
1215
1216static int
7fbfbe29 1217poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
984263bc 1218{
7fbfbe29
SG
1219 struct poll_kevent_copyin_args *pkap;
1220 struct pollfd *pfd;
1221 struct kevent *kev;
1222 int kev_count;
1223
1224 pkap = (struct poll_kevent_copyin_args *)arg;
1225
1226 while (pkap->pfds < pkap->nfds) {
1227 pfd = &pkap->fds[pkap->pfds];
1228
1229 /* Clear return events */
1230 pfd->revents = 0;
1231
ffdd7c7c
SG
1232 /* Do not check if fd is equal to -1 */
1233 if (pfd->fd == -1) {
1234 ++pkap->pfds;
1235 continue;
1236 }
1237
7fbfbe29
SG
1238 kev_count = 0;
1239 if (pfd->events & (POLLIN | POLLRDNORM))
1240 kev_count++;
1241 if (pfd->events & (POLLOUT | POLLWRNORM))
1242 kev_count++;
1243 if (pfd->events & (POLLPRI | POLLRDBAND))
1244 kev_count++;
1245
1246 if (*events + kev_count > maxevents)
1247 return (0);
1248
fe24d605
MD
1249 /*
1250 * NOTE: A combined serial number and poll array index is
1251 * stored in kev->udata.
1252 */
7fbfbe29 1253 kev = &kevp[*events];
fe24d605 1254 if (pfd->events & (POLLIN | POLLRDNORM)) {
7fbfbe29 1255 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
b4372719
MD
1256 0, 0, (void *)(uintptr_t)
1257 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1258 }
1259 if (pfd->events & (POLLOUT | POLLWRNORM)) {
7fbfbe29 1260 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
b4372719
MD
1261 0, 0, (void *)(uintptr_t)
1262 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1263 }
1264 if (pfd->events & (POLLPRI | POLLRDBAND)) {
7fbfbe29 1265 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
fe24d605 1266 NOTE_OOB, 0,
b4372719
MD
1267 (void *)(uintptr_t)
1268 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
fe24d605
MD
1269 }
1270
1271 if (nseldebug) {
21ae0f4c
SG
1272 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1273 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1274 pkap->lwp->lwp_kqueue_serial);
fe24d605 1275 }
7fbfbe29
SG
1276
1277 ++pkap->pfds;
1278 (*events) += kev_count;
1279 }
1280
1281 return (0);
1282}
1283
1284static int
1285poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1286{
1287 struct poll_kevent_copyin_args *pkap;
1288 struct pollfd *pfd;
1289 struct kevent kev;
8c4ed426 1290 int count_res;
984263bc 1291 int i;
fe24d605 1292 u_int pi;
7fbfbe29
SG
1293
1294 pkap = (struct poll_kevent_copyin_args *)arg;
1295
1296 for (i = 0; i < count; ++i) {
fe24d605
MD
1297 /*
1298 * Extract the poll array index and delete spurious events.
1299 * We can easily tell if the serial number is incorrect
1300 * by checking whether the extracted index is out of range.
1301 */
b4372719
MD
1302 pi = (u_int)(uintptr_t)kevp[i].udata -
1303 (u_int)pkap->lwp->lwp_kqueue_serial;
ffdd7c7c 1304
fe24d605
MD
1305 if (pi >= pkap->nfds) {
1306 kev = kevp[i];
1307 kev.flags = EV_DISABLE|EV_DELETE;
1308 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1309 if (nseldebug)
21ae0f4c
SG
1310 kprintf("poll index %d out of range against serial %d\n",
1311 pi, pkap->lwp->lwp_kqueue_serial);
fe24d605
MD
1312 continue;
1313 }
1314 pfd = &pkap->fds[pi];
1315 if (kevp[i].ident == pfd->fd) {
3c2a46a7
SG
1316 /*
1317 * A single descriptor may generate an error against
8c4ed426
MD
1318 * more than one filter, make sure to set the
1319 * appropriate flags but do not increment (*res)
1320 * more than once.
3c2a46a7 1321 */
8c4ed426 1322 count_res = (pfd->revents == 0);
fe24d605
MD
1323 if (kevp[i].flags & EV_ERROR) {
1324 switch(kevp[i].data) {
fe24d605
MD
1325 case EBADF:
1326 /* Bad file descriptor */
8c4ed426 1327 if (count_res)
3c2a46a7 1328 ++*res;
fe24d605 1329 pfd->revents |= POLLNVAL;
7fbfbe29 1330 break;
fe24d605 1331 default:
22690e50
SG
1332 /*
1333 * Poll silently swallows any unknown
1334 * errors except in the case of POLLPRI
1335 * (OOB/urgent data).
2b7d1884
SG
1336 *
1337 * ALWAYS filter out EOPNOTSUPP errors
1338 * from filters, common applications
1339 * set POLLPRI|POLLRDBAND and most
1340 * filters do not support EVFILT_EXCEPT.
22690e50
SG
1341 */
1342 if (kevp[i].filter != EVFILT_READ &&
2b7d1884
SG
1343 kevp[i].filter != EVFILT_WRITE &&
1344 kevp[i].data != EOPNOTSUPP) {
8c4ed426 1345 if (count_res == 0)
22690e50
SG
1346 ++*res;
1347 pfd->revents |= POLLERR;
1348 }
7fbfbe29
SG
1349 break;
1350 }
b4372719
MD
1351 if (nseldebug) {
1352 kprintf("poll index %d fd %d "
c9a1a153 1353 "filter %d error %jd\n",
fe24d605 1354 pi, pfd->fd,
b4372719 1355 kevp[i].filter,
c9a1a153 1356 (intmax_t)kevp[i].data);
b4372719 1357 }
fe24d605
MD
1358 continue;
1359 }
7fbfbe29 1360
fe24d605
MD
1361 switch (kevp[i].filter) {
1362 case EVFILT_READ:
9a5d20db
MD
1363#if 0
1364 /*
1365 * EOF on the read side can indicate a
1366 * half-closed situation and not necessarily
1367 * a disconnect, so depend on the user
1368 * issuing a read() and getting 0 bytes back.
1369 */
1370 if (kevp[i].flags & EV_EOF)
1371 pfd->revents |= POLLHUP;
1372#endif
484efec7
SG
1373 if (pfd->events & POLLIN)
1374 pfd->revents |= POLLIN;
1375 if (pfd->events & POLLRDNORM)
1376 pfd->revents |= POLLRDNORM;
fe24d605
MD
1377 break;
1378 case EVFILT_WRITE:
9a5d20db
MD
1379 /*
1380 * As per the OpenGroup POLLHUP is mutually
1381 * exclusive with the writability flags. I
1382 * consider this a bit broken but...
1383 *
1384 * In this case a disconnect is implied even
1385 * for a half-closed (write side) situation.
1386 */
1387 if (kevp[i].flags & EV_EOF) {
1388 pfd->revents |= POLLHUP;
1389 } else {
1390 if (pfd->events & POLLOUT)
1391 pfd->revents |= POLLOUT;
1392 if (pfd->events & POLLWRNORM)
1393 pfd->revents |= POLLWRNORM;
1394 }
fe24d605
MD
1395 break;
1396 case EVFILT_EXCEPT:
9a5d20db
MD
1397 /*
1398 * EV_EOF should never be tagged for this
1399 * filter.
1400 */
484efec7
SG
1401 if (pfd->events & POLLPRI)
1402 pfd->revents |= POLLPRI;
1403 if (pfd->events & POLLRDBAND)
1404 pfd->revents |= POLLRDBAND;
fe24d605
MD
1405 break;
1406 }
1407
1408 if (nseldebug) {
21ae0f4c
SG
1409 kprintf("poll index %d/%d fd %d revents %08x\n",
1410 pi, pkap->nfds, pfd->fd, pfd->revents);
fe24d605
MD
1411 }
1412
8c4ed426
MD
1413 if (count_res && pfd->revents)
1414 ++*res;
fe24d605 1415 } else {
b4372719 1416 if (nseldebug) {
c9a1a153
SW
1417 kprintf("poll index %d mismatch %ju/%d\n",
1418 pi, (uintmax_t)kevp[i].ident, pfd->fd);
b4372719 1419 }
fe24d605 1420 }
984263bc 1421 }
7fbfbe29 1422
984263bc
MD
1423 return (0);
1424}
1425
7fbfbe29
SG
1426static int
1427dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1428{
7fbfbe29
SG
1429 struct poll_kevent_copyin_args ka;
1430 struct pollfd sfds[64];
679058fb 1431 int bytes;
7fbfbe29
SG
1432 int error;
1433
1434 *res = 0;
1435 if (nfds < 0)
1436 return (EINVAL);
679058fb
MD
1437
1438 /*
1439 * This is a bit arbitrary but we need to limit internal kmallocs.
1440 */
1441 if (nfds > maxfilesperproc * 2)
1442 nfds = maxfilesperproc * 2;
1443 bytes = sizeof(struct pollfd) * nfds;
7fbfbe29
SG
1444
1445 ka.lwp = curthread->td_lwp;
1446 ka.nfds = nfds;
1447 ka.pfds = 0;
1448 ka.error = 0;
1449
1450 if (ka.nfds < 64)
1451 ka.fds = sfds;
1452 else
1453 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1454
679058fb 1455 error = copyin(fds, ka.fds, bytes);
7fbfbe29 1456 if (error == 0)
3c2a46a7 1457 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
7fbfbe29
SG
1458 poll_copyin, poll_copyout, ts);
1459
1460 if (error == 0)
679058fb 1461 error = copyout(ka.fds, fds, bytes);
7fbfbe29
SG
1462
1463 if (ka.fds != sfds)
1464 kfree(ka.fds, M_SELECT);
1465
fe24d605
MD
1466 ka.lwp->lwp_kqueue_serial += nfds;
1467
7fbfbe29
SG
1468 return (error);
1469}
1470
8b5c39bb
SG
1471static int
1472socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1473{
1474 return (0);
1475}
1476
1477static int
1478socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1479{
1480 ++*res;
1481 return (0);
1482}
1483
1484extern struct fileops socketops;
1485int
1486socket_wait(struct socket *so, struct timespec *ts, int *res)
1487{
ac62ea3c 1488 struct thread *td = curthread;
8b5c39bb 1489 struct file *fp;
8b5c39bb
SG
1490 struct kqueue kq;
1491 struct kevent kev;
ac62ea3c 1492 int error, fd;
8b5c39bb 1493
ac62ea3c 1494 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
8b5c39bb
SG
1495 return (error);
1496
ac62ea3c
SG
1497 fp->f_type = DTYPE_SOCKET;
1498 fp->f_flag = FREAD | FWRITE;
8b5c39bb
SG
1499 fp->f_ops = &socketops;
1500 fp->f_data = so;
ac62ea3c 1501 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
8b5c39bb 1502
ac62ea3c
SG
1503 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1504 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
8b5c39bb
SG
1505 if ((error = kqueue_register(&kq, &kev)) != 0) {
1506 fdrop(fp);
1507 return (error);
1508 }
1509
1510 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1511 socket_wait_copyout, ts);
ac62ea3c
SG
1512
1513 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1514 kqueue_register(&kq, &kev);
1515 fp->f_ops = &badfileops;
8b5c39bb
SG
1516 fdrop(fp);
1517
1518 return (error);
1519}
1520
984263bc
MD
1521/*
1522 * OpenBSD poll system call.
1523 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
3919ced0
MD
1524 *
1525 * MPSAFE
984263bc 1526 */
984263bc 1527int
753fd850 1528sys_openbsd_poll(struct openbsd_poll_args *uap)
984263bc 1529{
753fd850 1530 return (sys_poll((struct poll_args *)uap));
984263bc
MD
1531}
1532
1533/*ARGSUSED*/
1534int
b13267a5 1535seltrue(cdev_t dev, int events)
984263bc 1536{
984263bc
MD
1537 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1538}