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