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