]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/sys_generic.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / bsd / kern / sys_generic.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
8f6c56a5 11 *
6601e61a
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b
A
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
61 */
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/filedesc.h>
66#include <sys/ioctl.h>
91447636
A
67#include <sys/file_internal.h>
68#include <sys/proc_internal.h>
1c79356b 69#include <sys/socketvar.h>
91447636
A
70#if KTRACE
71#include <sys/uio_internal.h>
72#else
1c79356b 73#include <sys/uio.h>
91447636 74#endif
1c79356b
A
75#include <sys/kernel.h>
76#include <sys/stat.h>
77#include <sys/malloc.h>
91447636 78#include <sys/sysproto.h>
1c79356b 79
91447636 80#include <sys/mount_internal.h>
1c79356b
A
81#include <sys/protosw.h>
82#include <sys/ev.h>
83#include <sys/user.h>
84#include <sys/kdebug.h>
91447636
A
85#include <sys/poll.h>
86#include <sys/event.h>
87#include <sys/eventvar.h>
88
89#include <mach/mach_types.h>
90#include <kern/kern_types.h>
1c79356b 91#include <kern/assert.h>
91447636
A
92#include <kern/kalloc.h>
93#include <kern/thread.h>
94#include <kern/clock.h>
1c79356b
A
95
96#include <sys/mbuf.h>
97#include <sys/socket.h>
98#include <sys/socketvar.h>
99#include <sys/errno.h>
55e303ae 100#include <sys/syscall.h>
91447636 101#include <sys/pipe.h>
1c79356b 102
e5568f75
A
103#include <bsm/audit_kernel.h>
104
1c79356b
A
105#include <net/if.h>
106#include <net/route.h>
107
108#include <netinet/in.h>
109#include <netinet/in_systm.h>
110#include <netinet/ip.h>
111#include <netinet/in_pcb.h>
112#include <netinet/ip_var.h>
113#include <netinet/ip6.h>
114#include <netinet/tcp.h>
115#include <netinet/tcp_fsm.h>
116#include <netinet/tcp_seq.h>
117#include <netinet/tcp_timer.h>
118#include <netinet/tcp_var.h>
119#include <netinet/tcpip.h>
120#include <netinet/tcp_debug.h>
0b4e3aa0
A
121/* for wait queue based select */
122#include <kern/wait_queue.h>
91447636 123#include <kern/kalloc.h>
9bccf70c
A
124#if KTRACE
125#include <sys/ktrace.h>
126#endif
91447636
A
127#include <sys/vnode_internal.h>
128
129int rd_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
130int wr_uio(struct proc *p, int fdes, uio_t uio, user_ssize_t *retval);
131extern void *get_bsduthreadarg(thread_t);
132extern int *get_bsduthreadrval(thread_t);
133
134__private_extern__ int dofileread(struct proc *p, struct fileproc *fp, int fd,
135 user_addr_t bufp, user_size_t nbyte,
136 off_t offset, int flags, user_ssize_t *retval);
137__private_extern__ int dofilewrite(struct proc *p, struct fileproc *fp, int fd,
138 user_addr_t bufp, user_size_t nbyte,
139 off_t offset, int flags, user_ssize_t *retval);
140__private_extern__ int preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_vnode);
141__private_extern__ void donefileread(struct proc *p, struct fileproc *fp_ret, int fd);
9bccf70c 142
91447636
A
143#if NETAT
144extern int appletalk_inited;
145#endif /* NETAT */
1c79356b 146
91447636
A
147#define f_flag f_fglob->fg_flag
148#define f_type f_fglob->fg_type
149#define f_msgcount f_fglob->fg_msgcount
150#define f_cred f_fglob->fg_cred
151#define f_ops f_fglob->fg_ops
152#define f_offset f_fglob->fg_offset
153#define f_data f_fglob->fg_data
1c79356b
A
154/*
155 * Read system call.
156 */
9bccf70c 157int
1c79356b
A
158read(p, uap, retval)
159 struct proc *p;
160 register struct read_args *uap;
91447636 161 user_ssize_t *retval;
9bccf70c 162{
91447636 163 struct fileproc *fp;
9bccf70c 164 int error;
91447636
A
165 int fd = uap->fd;
166
167 if ( (error = preparefileread(p, &fp, fd, 0)) )
168 return (error);
9bccf70c 169
9bccf70c 170 error = dofileread(p, fp, uap->fd, uap->cbuf, uap->nbyte,
91447636
A
171 (off_t)-1, 0, retval);
172
173 donefileread(p, fp, fd);
174
175 return (error);
9bccf70c
A
176}
177
178/*
179 * Pread system call
180 */
9bccf70c
A
181int
182pread(p, uap, retval)
183 struct proc *p;
184 register struct pread_args *uap;
91447636 185 user_ssize_t *retval;
9bccf70c 186{
91447636
A
187 struct fileproc *fp;
188 int fd = uap->fd;
9bccf70c
A
189 int error;
190
91447636
A
191 if ( (error = preparefileread(p, &fp, fd, 1)) )
192 return (error);
193
194 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
195 uap->offset, FOF_OFFSET, retval);
55e303ae 196
91447636
A
197 donefileread(p, fp, fd);
198
55e303ae
A
199 if (!error)
200 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pread) | DBG_FUNC_NONE),
201 uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
202
91447636 203 return (error);
9bccf70c
A
204}
205
206/*
207 * Code common for read and pread
208 */
91447636
A
209
210void
211donefileread(struct proc *p, struct fileproc *fp, int fd)
212{
213 proc_fdlock(p);
214
215 fp->f_flags &= ~FP_INCHRREAD;
216
217 fp_drop(p, fd, fp, 1);
218 proc_fdunlock(p);
219}
220
221int
222preparefileread(struct proc *p, struct fileproc **fp_ret, int fd, int check_for_pread)
223{
224 vnode_t vp;
225 int error;
226 struct fileproc *fp;
227
228 proc_fdlock(p);
229
230 error = fp_lookup(p, fd, &fp, 1);
231
232 if (error) {
233 proc_fdunlock(p);
234 return (error);
235 }
236 if ((fp->f_flag & FREAD) == 0) {
237 error = EBADF;
238 goto out;
239 }
240 if (check_for_pread && (fp->f_type != DTYPE_VNODE)) {
241 error = ESPIPE;
242 goto out;
243 }
244 if (fp->f_type == DTYPE_VNODE) {
245 vp = (struct vnode *)fp->f_fglob->fg_data;
246
247 if (vp->v_type == VCHR)
248 fp->f_flags |= FP_INCHRREAD;
249 }
250
251 *fp_ret = fp;
252
253 proc_fdunlock(p);
254 return (0);
255
256out:
257 fp_drop(p, fd, fp, 1);
258 proc_fdunlock(p);
259 return (error);
260}
261
262
55e303ae 263__private_extern__ int
91447636 264dofileread(p, fp, fd, bufp, nbyte, offset, flags, retval)
9bccf70c 265 struct proc *p;
91447636 266 struct fileproc *fp;
9bccf70c 267 int fd, flags;
91447636
A
268 user_addr_t bufp;
269 user_size_t nbyte;
9bccf70c 270 off_t offset;
91447636 271 user_ssize_t *retval;
1c79356b 272{
91447636
A
273 uio_t auio;
274 user_ssize_t bytecnt;
275 long error = 0;
276 char uio_buf[ UIO_SIZEOF(1) ];
9bccf70c 277#if KTRACE
91447636
A
278 uio_t ktruio = NULL;
279 char ktr_uio_buf[ UIO_SIZEOF(1) ];
9bccf70c
A
280 int didktr = 0;
281#endif
1c79356b 282
91447636 283 // LP64todo - do we want to raise this?
9bccf70c
A
284 if (nbyte > INT_MAX)
285 return (EINVAL);
91447636
A
286
287 if (IS_64BIT_PROCESS(p)) {
288 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ,
289 &uio_buf[0], sizeof(uio_buf));
290 } else {
291 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ,
292 &uio_buf[0], sizeof(uio_buf));
293 }
294 uio_addiov(auio, bufp, nbyte);
295
9bccf70c
A
296#if KTRACE
297 /*
298 * if tracing, save a copy of iovec
299 */
300 if (KTRPOINT(p, KTR_GENIO)) {
9bccf70c 301 didktr = 1;
91447636
A
302
303 if (IS_64BIT_PROCESS(p)) {
304 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_READ,
305 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
306 } else {
307 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_READ,
308 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
309 }
310 uio_addiov(ktruio, bufp, nbyte);
9bccf70c
A
311 }
312#endif
91447636 313 bytecnt = nbyte;
9bccf70c 314
91447636
A
315 if ((error = fo_read(fp, auio, fp->f_cred, flags, p))) {
316 if (uio_resid(auio) != bytecnt && (error == ERESTART ||
9bccf70c
A
317 error == EINTR || error == EWOULDBLOCK))
318 error = 0;
319 }
91447636 320 bytecnt -= uio_resid(auio);
9bccf70c
A
321#if KTRACE
322 if (didktr && error == 0) {
91447636
A
323 uio_setresid(ktruio, bytecnt);
324 ktrgenio(p->p_tracep, fd, UIO_READ, ktruio, error);
9bccf70c
A
325 }
326#endif
91447636
A
327
328 *retval = bytecnt;
329
9bccf70c 330 return (error);
1c79356b
A
331}
332
9bccf70c
A
333/*
334 * Scatter read system call.
335 */
9bccf70c 336int
1c79356b
A
337readv(p, uap, retval)
338 struct proc *p;
339 register struct readv_args *uap;
91447636 340 user_ssize_t *retval;
1c79356b 341{
91447636 342 uio_t auio = NULL;
1c79356b 343 int error;
91447636
A
344 int size_of_iovec;
345 struct user_iovec *iovp;
346
347 /* Verify range bedfore calling uio_create() */
348 if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
349 return (EINVAL);
350
351 /* allocate a uio large enough to hold the number of iovecs passed */
352 auio = uio_create(uap->iovcnt, 0,
353 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
354 UIO_READ);
355
356 /* get location of iovecs within the uio. then copyin the iovecs from
357 * user space.
358 */
359 iovp = uio_iovsaddr(auio);
360 if (iovp == NULL) {
361 error = ENOMEM;
362 goto ExitThisRoutine;
363 }
364 size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
365 error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
366 if (error) {
367 goto ExitThisRoutine;
368 }
369
370 /* finalize uio_t for use and do the IO
371 */
372 uio_calculateresid(auio);
373 error = rd_uio(p, uap->fd, auio, retval);
374
375ExitThisRoutine:
376 if (auio != NULL) {
377 uio_free(auio);
378 }
1c79356b
A
379 return (error);
380}
381
382/*
383 * Write system call
384 */
9bccf70c 385int
1c79356b
A
386write(p, uap, retval)
387 struct proc *p;
388 register struct write_args *uap;
91447636 389 user_ssize_t *retval;
1c79356b 390{
91447636 391 struct fileproc *fp;
9bccf70c 392 int error;
91447636 393 int fd = uap->fd;
9bccf70c 394
91447636
A
395 error = fp_lookup(p,fd,&fp,0);
396 if (error)
397 return(error);
398 if ((fp->f_flag & FWRITE) == 0) {
399 error = EBADF;
400 } else {
401 error = dofilewrite(p, fp, uap->fd, uap->cbuf, uap->nbyte,
9bccf70c 402 (off_t)-1, 0, retval);
91447636
A
403 }
404 if (error == 0)
405 fp_drop_written(p, fd, fp);
406 else
407 fp_drop(p, fd, fp, 0);
9bccf70c
A
408 return(error);
409}
410
411/*
91447636 412 * pwrite system call
9bccf70c 413 */
9bccf70c
A
414int
415pwrite(p, uap, retval)
416 struct proc *p;
417 register struct pwrite_args *uap;
91447636 418 user_ssize_t *retval;
9bccf70c 419{
91447636 420 struct fileproc *fp;
9bccf70c 421 int error;
91447636
A
422 int fd = uap->fd;
423
424 error = fp_lookup(p,fd,&fp,0);
425 if (error)
426 return(error);
9bccf70c 427
91447636
A
428 if ((fp->f_flag & FWRITE) == 0) {
429 error = EBADF;
430 } else {
431 if (fp->f_type != DTYPE_VNODE) {
432 error = ESPIPE;
433 } else {
434 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
435 uap->offset, FOF_OFFSET, retval);
436 }
9bccf70c 437 }
91447636
A
438 if (error == 0)
439 fp_drop_written(p, fd, fp);
440 else
441 fp_drop(p, fd, fp, 0);
55e303ae
A
442
443 if (!error)
444 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO, SYS_pwrite) | DBG_FUNC_NONE),
445 uap->fd, uap->nbyte, (unsigned int)((uap->offset >> 32)), (unsigned int)(uap->offset), 0);
446
9bccf70c
A
447 return(error);
448}
449
55e303ae 450__private_extern__ int
91447636 451dofilewrite(p, fp, fd, bufp, nbyte, offset, flags, retval)
9bccf70c 452 struct proc *p;
91447636 453 struct fileproc *fp;
9bccf70c 454 int fd, flags;
91447636
A
455 user_addr_t bufp;
456 user_size_t nbyte;
9bccf70c 457 off_t offset;
91447636 458 user_ssize_t *retval;
9bccf70c 459{
91447636
A
460 uio_t auio;
461 long error = 0;
462 user_ssize_t bytecnt;
463 char uio_buf[ UIO_SIZEOF(1) ];
9bccf70c 464#if KTRACE
91447636 465 uio_t ktruio;
9bccf70c 466 int didktr = 0;
91447636 467 char ktr_uio_buf[ UIO_SIZEOF(1) ];
9bccf70c 468#endif
91447636
A
469
470 // LP64todo - do we want to raise this?
9bccf70c
A
471 if (nbyte > INT_MAX)
472 return (EINVAL);
91447636
A
473
474 if (IS_64BIT_PROCESS(p)) {
475 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE,
476 &uio_buf[0], sizeof(uio_buf));
477 } else {
478 auio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE,
479 &uio_buf[0], sizeof(uio_buf));
480 }
481 uio_addiov(auio, bufp, nbyte);
482
9bccf70c
A
483#if KTRACE
484 /*
485 * if tracing, save a copy of iovec and uio
486 */
487 if (KTRPOINT(p, KTR_GENIO)) {
9bccf70c 488 didktr = 1;
91447636
A
489
490 if (IS_64BIT_PROCESS(p)) {
491 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE64, UIO_WRITE,
492 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
493 } else {
494 ktruio = uio_createwithbuffer(1, offset, UIO_USERSPACE32, UIO_WRITE,
495 &ktr_uio_buf[0], sizeof(ktr_uio_buf));
496 }
497 uio_addiov(ktruio, bufp, nbyte);
9bccf70c
A
498 }
499#endif
91447636
A
500 bytecnt = nbyte;
501 if ((error = fo_write(fp, auio, fp->f_cred, flags, p))) {
502 if (uio_resid(auio) != bytecnt && (error == ERESTART ||
9bccf70c
A
503 error == EINTR || error == EWOULDBLOCK))
504 error = 0;
55e303ae
A
505 /* The socket layer handles SIGPIPE */
506 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
507 psignal(p, SIGPIPE);
9bccf70c 508 }
91447636 509 bytecnt -= uio_resid(auio);
9bccf70c
A
510#if KTRACE
511 if (didktr && error == 0) {
91447636
A
512 uio_setresid(ktruio, bytecnt);
513 ktrgenio(p->p_tracep, fd, UIO_WRITE, ktruio, error);
9bccf70c
A
514 }
515#endif
91447636
A
516 *retval = bytecnt;
517
9bccf70c 518 return (error);
1c79356b 519}
9bccf70c
A
520
521/*
522 * Gather write system call
523 */
9bccf70c 524int
1c79356b
A
525writev(p, uap, retval)
526 struct proc *p;
527 register struct writev_args *uap;
91447636 528 user_ssize_t *retval;
1c79356b 529{
91447636 530 uio_t auio = NULL;
1c79356b 531 int error;
91447636
A
532 int size_of_iovec;
533 struct user_iovec *iovp;
534
535 /* Verify range bedfore calling uio_create() */
536 if (uap->iovcnt <= 0 || uap->iovcnt > UIO_MAXIOV)
537 return (EINVAL);
538
539 /* allocate a uio large enough to hold the number of iovecs passed */
540 auio = uio_create(uap->iovcnt, 0,
541 (IS_64BIT_PROCESS(p) ? UIO_USERSPACE64 : UIO_USERSPACE32),
542 UIO_WRITE);
543
544 /* get location of iovecs within the uio. then copyin the iovecs from
545 * user space.
546 */
547 iovp = uio_iovsaddr(auio);
548 if (iovp == NULL) {
549 error = ENOMEM;
550 goto ExitThisRoutine;
551 }
552 size_of_iovec = (IS_64BIT_PROCESS(p) ? sizeof(struct user_iovec) : sizeof(struct iovec));
553 error = copyin(uap->iovp, (caddr_t)iovp, (uap->iovcnt * size_of_iovec));
554 if (error) {
555 goto ExitThisRoutine;
556 }
557
558 /* finalize uio_t for use and do the IO
559 */
560 uio_calculateresid(auio);
561 error = wr_uio(p, uap->fd, auio, retval);
562
563ExitThisRoutine:
564 if (auio != NULL) {
565 uio_free(auio);
566 }
1c79356b
A
567 return (error);
568}
569
91447636 570
9bccf70c 571int
91447636 572wr_uio(p, fdes, uio, retval)
1c79356b
A
573 struct proc *p;
574 int fdes;
91447636
A
575 register uio_t uio;
576 user_ssize_t *retval;
1c79356b 577{
91447636
A
578 struct fileproc *fp;
579 int error;
580 user_ssize_t count;
9bccf70c 581#if KTRACE
91447636 582 struct iovec_64 *ktriov = NULL;
9bccf70c
A
583 struct uio ktruio;
584 int didktr = 0;
585 u_int iovlen;
586#endif
1c79356b 587
91447636
A
588 error = fp_lookup(p,fdes,&fp,0);
589 if (error)
590 return(error);
1c79356b 591
91447636
A
592 if ((fp->f_flag & FWRITE) == 0) {
593 error = EBADF;
594 goto out;
1c79356b 595 }
91447636
A
596 count = uio_resid(uio);
597#if KTRACE
598 /*
599 * if tracing, save a copy of iovec
600 */
601 if (KTRPOINT(p, KTR_GENIO)) {
602 iovlen = uio->uio_iovcnt *
603 (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
604 MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
605 if (ktriov != NULL) {
606 bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
607 ktruio = *uio;
608 didktr = 1;
1c79356b 609 }
91447636
A
610 }
611#endif
612 error = fo_write(fp, uio, fp->f_cred, 0, p);
613 if (error) {
614 if (uio_resid(uio) != count && (error == ERESTART ||
615 error == EINTR || error == EWOULDBLOCK))
616 error = 0;
617 /* The socket layer handles SIGPIPE */
618 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
619 psignal(p, SIGPIPE);
620 }
621 *retval = count - uio_resid(uio);
622
623#if KTRACE
624 if (didktr) {
625 if (error == 0) {
626 ktruio.uio_iovs.iov64p = ktriov;
627 uio_setresid(&ktruio, *retval);
628 ktrgenio(p->p_tracep, fdes, UIO_WRITE, &ktruio, error);
1c79356b 629 }
91447636 630 FREE(ktriov, M_TEMP);
1c79356b 631 }
91447636
A
632#endif
633
634out:
635 if ( (error == 0) )
636 fp_drop_written(p, fdes, fp);
637 else
638 fp_drop(p, fdes, fp, 0);
639 return(error);
640}
641
642
643int
644rd_uio(p, fdes, uio, retval)
645 struct proc *p;
646 int fdes;
647 register uio_t uio;
648 user_ssize_t *retval;
649{
650 struct fileproc *fp;
651 int error;
652 user_ssize_t count;
653#if KTRACE
654 struct iovec_64 *ktriov = NULL;
655 struct uio ktruio;
656 int didktr = 0;
657 u_int iovlen;
658#endif
659
660 if ( (error = preparefileread(p, &fp, fdes, 0)) )
661 return (error);
662
663 count = uio_resid(uio);
9bccf70c
A
664#if KTRACE
665 /*
666 * if tracing, save a copy of iovec
667 */
668 if (KTRPOINT(p, KTR_GENIO)) {
91447636
A
669 iovlen = uio->uio_iovcnt *
670 (IS_64BIT_PROCESS(p) ? sizeof (struct iovec_64) : sizeof (struct iovec_32));
671 MALLOC(ktriov, struct iovec_64 *, iovlen, M_TEMP, M_WAITOK);
672 if (ktriov != NULL) {
673 bcopy((caddr_t)uio->uio_iovs.iov64p, (caddr_t)ktriov, iovlen);
674 ktruio = *uio;
675 didktr = 1;
676 }
9bccf70c
A
677 }
678#endif
91447636 679 error = fo_read(fp, uio, fp->f_cred, 0, p);
9bccf70c 680
91447636
A
681 if (error) {
682 if (uio_resid(uio) != count && (error == ERESTART ||
683 error == EINTR || error == EWOULDBLOCK))
684 error = 0;
1c79356b 685 }
91447636 686 *retval = count - uio_resid(uio);
9bccf70c
A
687
688#if KTRACE
689 if (didktr) {
690 if (error == 0) {
91447636
A
691 ktruio.uio_iovs.iov64p = ktriov;
692 uio_setresid(&ktruio, *retval);
693 ktrgenio(p->p_tracep, fdes, UIO_READ, &ktruio, error);
9bccf70c
A
694 }
695 FREE(ktriov, M_TEMP);
696 }
697#endif
91447636 698 donefileread(p, fp, fdes);
9bccf70c 699
91447636 700 return (error);
1c79356b
A
701}
702
703/*
704 * Ioctl system call
91447636 705 *
1c79356b 706 */
9bccf70c 707int
91447636 708ioctl(struct proc *p, register struct ioctl_args *uap, __unused register_t *retval)
1c79356b 709{
91447636 710 struct fileproc *fp;
1c79356b 711 register u_long com;
91447636 712 int error = 0;
1c79356b 713 register u_int size;
91447636
A
714 caddr_t datap, memp;
715 boolean_t is64bit;
1c79356b
A
716 int tmp;
717#define STK_PARAMS 128
718 char stkbuf[STK_PARAMS];
91447636 719 int fd = uap->fd;
1c79356b 720
e5568f75 721 AUDIT_ARG(fd, uap->fd);
91447636 722 AUDIT_ARG(cmd, CAST_DOWN(int, uap->com)); /* LP64todo: uap->com is a user-land long */
e5568f75 723 AUDIT_ARG(addr, uap->data);
91447636
A
724
725 is64bit = proc_is64bit(p);
726
727 proc_fdlock(p);
728 error = fp_lookup(p,fd,&fp,1);
729 if (error) {
730 proc_fdunlock(p);
731 return(error);
732 }
1c79356b 733
e5568f75 734 AUDIT_ARG(file, p, fp);
91447636
A
735
736 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
737 error = EBADF;
738 goto out;
739 }
1c79356b 740
9bccf70c
A
741#if NETAT
742 /*
743 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
1c79356b
A
744 * while implementing an ATioctl system call
745 */
1c79356b 746 {
1c79356b 747 if (appletalk_inited && ((uap->com & 0x0000FFFF) == 0xff99)) {
91447636 748 u_long fixed_command;
1c79356b
A
749#ifdef APPLETALK_DEBUG
750 kprintf("ioctl: special AppleTalk \n");
751#endif
91447636
A
752 datap = &stkbuf[0];
753 *(user_addr_t *)datap = uap->data;
754 fixed_command = _IOW(0, 0xff99, uap->data);
755 error = fo_ioctl(fp, fixed_command, datap, p);
756 goto out;
1c79356b
A
757 }
758 }
759
760#endif /* NETAT */
761
762
763 switch (com = uap->com) {
764 case FIONCLEX:
765 *fdflags(p, uap->fd) &= ~UF_EXCLOSE;
91447636
A
766 error =0;
767 goto out;
1c79356b
A
768 case FIOCLEX:
769 *fdflags(p, uap->fd) |= UF_EXCLOSE;
91447636
A
770 error =0;
771 goto out;
1c79356b
A
772 }
773
774 /*
775 * Interpret high order word to find amount of data to be
776 * copied to/from the user's address space.
777 */
778 size = IOCPARM_LEN(com);
91447636
A
779 if (size > IOCPARM_MAX) {
780 error = ENOTTY;
781 goto out;
782 }
1c79356b
A
783 memp = NULL;
784 if (size > sizeof (stkbuf)) {
91447636
A
785 proc_fdunlock(p);
786 if ((memp = (caddr_t)kalloc(size)) == 0) {
787 proc_fdlock(p);
788 error = ENOMEM;
789 goto out;
790 }
791 proc_fdlock(p);
792 datap = memp;
1c79356b 793 } else
91447636 794 datap = &stkbuf[0];
1c79356b
A
795 if (com&IOC_IN) {
796 if (size) {
91447636
A
797 proc_fdunlock(p);
798 error = copyin(uap->data, datap, size);
1c79356b
A
799 if (error) {
800 if (memp)
801 kfree(memp, size);
91447636
A
802 proc_fdlock(p);
803 goto out;
1c79356b 804 }
91447636
A
805 proc_fdlock(p);
806 } else {
807 /* XXX - IOC_IN and no size? we should proably return an error here!! */
808 if (is64bit) {
809 *(user_addr_t *)datap = uap->data;
810 }
811 else {
812 *(uint32_t *)datap = (uint32_t)uap->data;
813 }
814 }
1c79356b
A
815 } else if ((com&IOC_OUT) && size)
816 /*
817 * Zero the buffer so the user always
818 * gets back something deterministic.
819 */
91447636
A
820 bzero(datap, size);
821 else if (com&IOC_VOID) {
822 /* XXX - this is odd since IOC_VOID means no parameters */
823 if (is64bit) {
824 *(user_addr_t *)datap = uap->data;
825 }
826 else {
827 *(uint32_t *)datap = (uint32_t)uap->data;
828 }
829 }
1c79356b
A
830
831 switch (com) {
832
833 case FIONBIO:
91447636 834 if ( (tmp = *(int *)datap) )
1c79356b
A
835 fp->f_flag |= FNONBLOCK;
836 else
837 fp->f_flag &= ~FNONBLOCK;
9bccf70c 838 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
1c79356b
A
839 break;
840
841 case FIOASYNC:
91447636 842 if ( (tmp = *(int *)datap) )
1c79356b
A
843 fp->f_flag |= FASYNC;
844 else
845 fp->f_flag &= ~FASYNC;
9bccf70c 846 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
1c79356b
A
847 break;
848
849 case FIOSETOWN:
91447636 850 tmp = *(int *)datap;
1c79356b
A
851 if (fp->f_type == DTYPE_SOCKET) {
852 ((struct socket *)fp->f_data)->so_pgid = tmp;
853 error = 0;
854 break;
855 }
91447636
A
856 if (fp->f_type == DTYPE_PIPE) {
857 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
858 break;
859 }
1c79356b
A
860 if (tmp <= 0) {
861 tmp = -tmp;
862 } else {
863 struct proc *p1 = pfind(tmp);
864 if (p1 == 0) {
865 error = ESRCH;
866 break;
867 }
868 tmp = p1->p_pgrp->pg_id;
869 }
9bccf70c 870 error = fo_ioctl(fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
1c79356b
A
871 break;
872
873 case FIOGETOWN:
874 if (fp->f_type == DTYPE_SOCKET) {
875 error = 0;
91447636 876 *(int *)datap = ((struct socket *)fp->f_data)->so_pgid;
1c79356b
A
877 break;
878 }
91447636
A
879 error = fo_ioctl(fp, TIOCGPGRP, datap, p);
880 *(int *)datap = -*(int *)datap;
1c79356b
A
881 break;
882
883 default:
91447636 884 error = fo_ioctl(fp, com, datap, p);
1c79356b
A
885 /*
886 * Copy any data to user, size was
887 * already set and checked above.
888 */
889 if (error == 0 && (com&IOC_OUT) && size)
91447636 890 error = copyout(datap, uap->data, (u_int)size);
1c79356b
A
891 break;
892 }
91447636 893 proc_fdunlock(p);
1c79356b
A
894 if (memp)
895 kfree(memp, size);
91447636
A
896 proc_fdlock(p);
897out:
898 fp_drop(p, fd, fp, 1);
899 proc_fdunlock(p);
900 return(error);
1c79356b
A
901}
902
1c79356b 903int selwait, nselcoll;
0b4e3aa0
A
904#define SEL_FIRSTPASS 1
905#define SEL_SECONDPASS 2
9bccf70c
A
906extern int selcontinue(int error);
907extern int selprocess(int error, int sel_pass);
908static int selscan(struct proc *p, struct _select * sel,
91447636 909 int nfd, register_t *retval, int sel_pass, wait_queue_sub_t wqsub);
9bccf70c 910static int selcount(struct proc *p, u_int32_t *ibits, u_int32_t *obits,
91447636
A
911 int nfd, int * count);
912static int seldrop(struct proc *p, u_int32_t *ibits, int nfd);
9bccf70c 913extern uint64_t tvtoabstime(struct timeval *tvp);
1c79356b
A
914
915/*
916 * Select system call.
917 */
9bccf70c 918int
91447636 919select(struct proc *p, struct select_args *uap, register_t *retval)
1c79356b 920{
9bccf70c 921 int error = 0;
0b4e3aa0 922 u_int ni, nw, size;
91447636 923 thread_t th_act;
1c79356b
A
924 struct uthread *uth;
925 struct _select *sel;
926 int needzerofill = 1;
0b4e3aa0 927 int count = 0;
1c79356b 928
91447636 929 th_act = current_thread();
1c79356b 930 uth = get_bsdthread_info(th_act);
91447636 931 sel = &uth->uu_select;
1c79356b
A
932 retval = (int *)get_bsduthreadrval(th_act);
933 *retval = 0;
934
0b4e3aa0 935 if (uap->nd < 0) {
1c79356b 936 return (EINVAL);
0b4e3aa0 937 }
1c79356b
A
938
939 if (uap->nd > p->p_fd->fd_nfiles)
940 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
941
942 nw = howmany(uap->nd, NFDBITS);
943 ni = nw * sizeof(fd_mask);
944
945 /*
946 * if this is the first select by the thread
947 * allocate the space for bits.
948 */
949 if (sel->nbytes == 0) {
950 sel->nbytes = 3 * ni;
91447636
A
951 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
952 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
953 if ((sel->ibits == NULL) || (sel->obits == NULL))
954 panic("select out of memory");
1c79356b
A
955 needzerofill = 0;
956 }
957
958 /*
959 * if the previously allocated space for the bits
960 * is smaller than what is requested. Reallocate.
961 */
962 if (sel->nbytes < (3 * ni)) {
963 sel->nbytes = (3 * ni);
964 FREE(sel->ibits, M_TEMP);
965 FREE(sel->obits, M_TEMP);
91447636
A
966 MALLOC(sel->ibits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
967 MALLOC(sel->obits, u_int32_t *, sel->nbytes, M_TEMP, M_WAITOK | M_ZERO);
968 if ((sel->ibits == NULL) || (sel->obits == NULL))
969 panic("select out of memory");
1c79356b
A
970 needzerofill = 0;
971 }
972
973 if (needzerofill) {
974 bzero((caddr_t)sel->ibits, sel->nbytes);
975 bzero((caddr_t)sel->obits, sel->nbytes);
976 }
977
978 /*
979 * get the bits from the user address space
980 */
981#define getbits(name, x) \
982 do { \
91447636 983 if (uap->name && (error = copyin(uap->name, \
1c79356b
A
984 (caddr_t)&sel->ibits[(x) * nw], ni))) \
985 goto continuation; \
986 } while (0)
987
988 getbits(in, 0);
989 getbits(ou, 1);
990 getbits(ex, 2);
991#undef getbits
992
993 if (uap->tv) {
9bccf70c 994 struct timeval atv;
91447636
A
995 if (IS_64BIT_PROCESS(p)) {
996 struct user_timeval atv64;
997 error = copyin(uap->tv, (caddr_t)&atv64, sizeof(atv64));
998 /* Loses resolution - assume timeout < 68 years */
999 atv.tv_sec = atv64.tv_sec;
1000 atv.tv_usec = atv64.tv_usec;
1001 } else {
1002 error = copyin(uap->tv, (caddr_t)&atv, sizeof(atv));
1003 }
1c79356b
A
1004 if (error)
1005 goto continuation;
9bccf70c 1006 if (itimerfix(&atv)) {
1c79356b
A
1007 error = EINVAL;
1008 goto continuation;
1009 }
0b4e3aa0 1010
9bccf70c
A
1011 clock_absolutetime_interval_to_deadline(
1012 tvtoabstime(&atv), &sel->abstime);
1013 }
1014 else
1015 sel->abstime = 0;
1016
91447636 1017 if ( (error = selcount(p, sel->ibits, sel->obits, uap->nd, &count)) ) {
0b4e3aa0
A
1018 goto continuation;
1019 }
1020
0b4e3aa0 1021 sel->count = count;
91447636 1022 size = SIZEOF_WAITQUEUE_SET + (count * SIZEOF_WAITQUEUE_LINK);
0b4e3aa0 1023 if (sel->allocsize) {
91447636 1024 if (sel->wqset == 0)
0b4e3aa0
A
1025 panic("select: wql memory smashed");
1026 /* needed for the select now */
1027 if (size > sel->allocsize) {
91447636 1028 kfree(sel->wqset, sel->allocsize);
0b4e3aa0 1029 sel->allocsize = size;
91447636
A
1030 sel->wqset = (wait_queue_set_t)kalloc(size);
1031 if (sel->wqset == (wait_queue_set_t)NULL)
0b4e3aa0 1032 panic("failed to allocate memory for waitqueue\n");
0b4e3aa0
A
1033 }
1034 } else {
1035 sel->count = count;
1036 sel->allocsize = size;
91447636
A
1037 sel->wqset = (wait_queue_set_t)kalloc(sel->allocsize);
1038 if (sel->wqset == (wait_queue_set_t)NULL)
0b4e3aa0 1039 panic("failed to allocate memory for waitqueue\n");
0b4e3aa0 1040 }
91447636
A
1041 bzero(sel->wqset, size);
1042 sel->wql = (char *)sel->wqset + SIZEOF_WAITQUEUE_SET;
1043 wait_queue_set_init(sel->wqset, (SYNC_POLICY_FIFO | SYNC_POLICY_PREPOST));
0b4e3aa0 1044
1c79356b 1045continuation:
9bccf70c 1046 return selprocess(error, SEL_FIRSTPASS);
0b4e3aa0
A
1047}
1048
1049int
1050selcontinue(int error)
1051{
9bccf70c 1052 return selprocess(error, SEL_SECONDPASS);
1c79356b
A
1053}
1054
1055int
91447636 1056selprocess(int error, int sel_pass)
1c79356b 1057{
9bccf70c 1058 int ncoll;
1c79356b 1059 u_int ni, nw;
91447636 1060 thread_t th_act;
1c79356b
A
1061 struct uthread *uth;
1062 struct proc *p;
1063 struct select_args *uap;
1064 int *retval;
1065 struct _select *sel;
0b4e3aa0 1066 int unwind = 1;
9bccf70c 1067 int prepost = 0;
0b4e3aa0
A
1068 int somewakeup = 0;
1069 int doretry = 0;
9bccf70c 1070 wait_result_t wait_result;
1c79356b
A
1071
1072 p = current_proc();
91447636 1073 th_act = current_thread();
1c79356b
A
1074 uap = (struct select_args *)get_bsduthreadarg(th_act);
1075 retval = (int *)get_bsduthreadrval(th_act);
1076 uth = get_bsdthread_info(th_act);
91447636 1077 sel = &uth->uu_select;
1c79356b 1078
0b4e3aa0
A
1079 /* if it is first pass wait queue is not setup yet */
1080 if ((error != 0) && (sel_pass == SEL_FIRSTPASS))
1081 unwind = 0;
1082 if (sel->count == 0)
1083 unwind = 0;
1c79356b 1084retry:
0b4e3aa0 1085 if (error != 0) {
1c79356b 1086 goto done;
0b4e3aa0
A
1087 }
1088
1c79356b
A
1089 ncoll = nselcoll;
1090 p->p_flag |= P_SELECT;
0b4e3aa0
A
1091 /* skip scans if the select is just for timeouts */
1092 if (sel->count) {
1093 if (sel_pass == SEL_FIRSTPASS)
91447636 1094 wait_queue_sub_clearrefs(sel->wqset);
0b4e3aa0 1095
91447636 1096 error = selscan(p, sel, uap->nd, retval, sel_pass, sel->wqset);
0b4e3aa0
A
1097 if (error || *retval) {
1098 goto done;
1099 }
1100 if (prepost) {
1101 /* if the select of log, then we canwakeup and discover some one
1102 * else already read the data; go toselct again if time permits
1103 */
1104 prepost = 0;
1105 doretry = 1;
1106 }
1107 if (somewakeup) {
1108 somewakeup = 0;
1109 doretry = 1;
1110 }
1111 }
1112
9bccf70c
A
1113 if (uap->tv) {
1114 uint64_t now;
1115
1116 clock_get_uptime(&now);
1117 if (now >= sel->abstime)
1118 goto done;
1c79356b 1119 }
0b4e3aa0
A
1120
1121 if (doretry) {
1122 /* cleanup obits and try again */
1123 doretry = 0;
1124 sel_pass = SEL_FIRSTPASS;
1125 goto retry;
1126 }
1127
1c79356b
A
1128 /*
1129 * To effect a poll, the timeout argument should be
1130 * non-nil, pointing to a zero-valued timeval structure.
1131 */
9bccf70c 1132 if (uap->tv && sel->abstime == 0) {
1c79356b
A
1133 goto done;
1134 }
0b4e3aa0
A
1135
1136 /* No spurious wakeups due to colls,no need to check for them */
1137 if ((sel_pass == SEL_SECONDPASS) || ((p->p_flag & P_SELECT) == 0)) {
1138 sel_pass = SEL_FIRSTPASS;
1c79356b
A
1139 goto retry;
1140 }
0b4e3aa0 1141
1c79356b
A
1142 p->p_flag &= ~P_SELECT;
1143
0b4e3aa0
A
1144 /* if the select is just for timeout skip check */
1145 if (sel->count &&(sel_pass == SEL_SECONDPASS))
1146 panic("selprocess: 2nd pass assertwaiting");
1147
1148 /* Wait Queue Subordinate has waitqueue as first element */
91447636
A
1149 wait_result = wait_queue_assert_wait((wait_queue_t)sel->wqset,
1150 &selwait, THREAD_ABORTSAFE, sel->abstime);
9bccf70c
A
1151 if (wait_result != THREAD_AWAKENED) {
1152 /* there are no preposted events */
91447636
A
1153 error = tsleep1(NULL, PSOCK | PCATCH,
1154 "select", 0, selcontinue);
0b4e3aa0
A
1155 } else {
1156 prepost = 1;
1157 error = 0;
1158 }
1159
1160 sel_pass = SEL_SECONDPASS;
1161 if (error == 0) {
1162 if (!prepost)
1163 somewakeup =1;
1c79356b 1164 goto retry;
0b4e3aa0 1165 }
1c79356b 1166done:
91447636
A
1167 if (unwind) {
1168 wait_subqueue_unlink_all(sel->wqset);
1169 seldrop(p, sel->ibits, uap->nd);
1170 }
1c79356b
A
1171 p->p_flag &= ~P_SELECT;
1172 /* select is not restarted after signals... */
1173 if (error == ERESTART)
1174 error = EINTR;
1175 if (error == EWOULDBLOCK)
1176 error = 0;
1c79356b
A
1177 nw = howmany(uap->nd, NFDBITS);
1178 ni = nw * sizeof(fd_mask);
1179
1180#define putbits(name, x) \
1181 do { \
91447636
A
1182 if (uap->name && (error2 = \
1183 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1c79356b
A
1184 error = error2; \
1185 } while (0)
1186
1187 if (error == 0) {
1188 int error2;
1189
1190 putbits(in, 0);
1191 putbits(ou, 1);
1192 putbits(ex, 2);
1193#undef putbits
1194 }
1c79356b 1195 return(error);
1c79356b
A
1196}
1197
1198static int
91447636 1199selscan(p, sel, nfd, retval, sel_pass, wqsub)
1c79356b 1200 struct proc *p;
0b4e3aa0 1201 struct _select *sel;
1c79356b
A
1202 int nfd;
1203 register_t *retval;
0b4e3aa0 1204 int sel_pass;
91447636 1205 wait_queue_sub_t wqsub;
1c79356b
A
1206{
1207 register struct filedesc *fdp = p->p_fd;
1208 register int msk, i, j, fd;
1209 register u_int32_t bits;
91447636 1210 struct fileproc *fp;
1c79356b 1211 int n = 0;
0b4e3aa0 1212 int nc = 0;
1c79356b
A
1213 static int flag[3] = { FREAD, FWRITE, 0 };
1214 u_int32_t *iptr, *optr;
1215 u_int nw;
0b4e3aa0
A
1216 u_int32_t *ibits, *obits;
1217 char * wql;
0b4e3aa0 1218 char * wql_ptr;
1c79356b
A
1219
1220 /*
1221 * Problems when reboot; due to MacOSX signal probs
1222 * in Beaker1C ; verify that the p->p_fd is valid
1223 */
1224 if (fdp == NULL) {
1225 *retval=0;
1226 return(EIO);
1227 }
0b4e3aa0
A
1228 ibits = sel->ibits;
1229 obits = sel->obits;
1230 wql = sel->wql;
1231
1c79356b
A
1232 nw = howmany(nfd, NFDBITS);
1233
0b4e3aa0 1234 nc = 0;
91447636 1235 proc_fdlock(p);
0b4e3aa0 1236
91447636 1237 if (sel->count) {
0b4e3aa0
A
1238 for (msk = 0; msk < 3; msk++) {
1239 iptr = (u_int32_t *)&ibits[msk * nw];
1240 optr = (u_int32_t *)&obits[msk * nw];
91447636 1241
0b4e3aa0
A
1242 for (i = 0; i < nfd; i += NFDBITS) {
1243 bits = iptr[i/NFDBITS];
91447636 1244
0b4e3aa0
A
1245 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1246 bits &= ~(1 << j);
1247 fp = fdp->fd_ofiles[fd];
91447636 1248
0b4e3aa0
A
1249 if (fp == NULL ||
1250 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
91447636 1251 proc_fdunlock(p);
0b4e3aa0
A
1252 return(EBADF);
1253 }
91447636 1254 if (sel_pass == SEL_SECONDPASS) {
0b4e3aa0 1255 wql_ptr = (char *)0;
91447636
A
1256 fp->f_flags &= ~FP_INSELECT;
1257 fp->f_waddr = (void *)0;
1258 } else {
1259 wql_ptr = (wql + nc * SIZEOF_WAITQUEUE_LINK);
1260 fp->f_flags |= FP_INSELECT;
1261 fp->f_waddr = (void *)wqsub;
1262 }
1263 if (fp->f_ops && fo_select(fp, flag[msk], wql_ptr, p)) {
0b4e3aa0
A
1264 optr[fd/NFDBITS] |= (1 << (fd % NFDBITS));
1265 n++;
1266 }
1267 nc++;
1268 }
1269 }
1270 }
0b4e3aa0 1271 }
91447636 1272 proc_fdunlock(p);
1c79356b
A
1273 *retval = n;
1274 return (0);
1275}
1276
91447636
A
1277static int poll_callback(struct kqueue *, struct kevent *, void *);
1278
1279struct poll_continue_args {
1280 user_addr_t pca_fds;
1281 u_int pca_nfds;
1282 u_int pca_rfds;
1283};
1284
9bccf70c 1285int
91447636 1286poll(struct proc *p, struct poll_args *uap, register_t *retval)
1c79356b 1287{
91447636
A
1288 struct poll_continue_args *cont;
1289 struct pollfd *fds;
1290 struct kqueue *kq;
1291 struct timeval atv;
1292 int ncoll, error = 0;
1293 u_int nfds = uap->nfds;
1294 u_int rfds = 0;
1295 u_int i;
1296 size_t ni;
1c79356b 1297
91447636
A
1298 /*
1299 * This is kinda bogus. We have fd limits, but that is not
1300 * really related to the size of the pollfd array. Make sure
1301 * we let the process use at least FD_SETSIZE entries and at
1302 * least enough for the current limits. We want to be reasonably
1303 * safe, but not overly restrictive.
1304 */
1305 if (nfds > OPEN_MAX ||
1306 (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE))
1307 return (EINVAL);
1c79356b 1308
91447636
A
1309 kq = kqueue_alloc(p);
1310 if (kq == NULL)
1311 return (EAGAIN);
1312
1313 ni = nfds * sizeof(struct pollfd) + sizeof(struct poll_continue_args);
1314 MALLOC(cont, struct poll_continue_args *, ni, M_TEMP, M_WAITOK);
1315 if (NULL == cont) {
1316 error = EAGAIN;
1317 goto out;
1318 }
1319
1320 fds = (struct pollfd *)&cont[1];
1321 error = copyin(uap->fds, fds, nfds * sizeof(struct pollfd));
1322 if (error)
1323 goto out;
1324
1325 if (uap->timeout != -1) {
1326 struct timeval rtv;
1327
1328 atv.tv_sec = uap->timeout / 1000;
1329 atv.tv_usec = (uap->timeout % 1000) * 1000;
1330 if (itimerfix(&atv)) {
1331 error = EINVAL;
1332 goto out;
1333 }
1334 getmicrouptime(&rtv);
1335 timevaladd(&atv, &rtv);
1336 } else {
1337 atv.tv_sec = 0;
1338 atv.tv_usec = 0;
1339 }
1340
1341 /* JMM - all this P_SELECT stuff is bogus */
1342 ncoll = nselcoll;
1343 p->p_flag |= P_SELECT;
1344
1345 for (i = 0; i < nfds; i++) {
1346 short events = fds[i].events;
1347 struct kevent kev;
1348 int kerror = 0;
1349
1350 /* per spec, ignore fd values below zero */
1351 if (fds[i].fd < 0) {
1352 fds[i].revents = 0;
1353 continue;
1354 }
1355
1356 /* convert the poll event into a kqueue kevent */
1357 kev.ident = fds[i].fd;
1358 kev.flags = EV_ADD | EV_ONESHOT | EV_POLL;
1359 kev.fflags = NOTE_LOWAT;
1360 kev.data = 1; /* efficiency be damned: any data should trigger */
1361 kev.udata = CAST_USER_ADDR_T(&fds[i]);
1362
1363 /* Handle input events */
1364 if (events & ( POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND )) {
1365 kev.filter = EVFILT_READ;
1366 if (!(events & ( POLLIN | POLLRDNORM )))
1367 kev.flags |= EV_OOBAND;
1368 kerror = kevent_register(kq, &kev, p);
1369 }
1370
1371 /* Handle output events */
1372 if (kerror == 0 &&
1373 events & ( POLLOUT | POLLWRNORM | POLLWRBAND )) {
1374 kev.filter = EVFILT_WRITE;
1375 kerror = kevent_register(kq, &kev, p);
1376 }
1377
1378 /* Handle BSD extension vnode events */
1379 if (kerror == 0 &&
1380 events & ( POLLEXTEND | POLLATTRIB | POLLNLINK | POLLWRITE )) {
1381 kev.filter = EVFILT_VNODE;
1382 kev.fflags = 0;
1383 if (events & POLLEXTEND)
1384 kev.fflags |= NOTE_EXTEND;
1385 if (events & POLLATTRIB)
1386 kev.fflags |= NOTE_ATTRIB;
1387 if (events & POLLNLINK)
1388 kev.fflags |= NOTE_LINK;
1389 if (events & POLLWRITE)
1390 kev.fflags |= NOTE_WRITE;
1391 kerror = kevent_register(kq, &kev, p);
1392 }
1393
1394 if (kerror != 0) {
1395 fds[i].revents = POLLNVAL;
1396 rfds++;
1397 } else
1398 fds[i].revents = 0;
1399 }
1400
1401 /* Did we have any trouble registering? */
1402 if (rfds > 0)
1403 goto done;
1404
1405 /* scan for, and possibly wait for, the kevents to trigger */
1406 cont->pca_fds = uap->fds;
1407 cont->pca_nfds = nfds;
1408 cont->pca_rfds = rfds;
1409 error = kevent_scan(kq, poll_callback, NULL, cont, &atv, p);
1410 rfds = cont->pca_rfds;
1411
1412 done:
1413 p->p_flag &= ~P_SELECT;
1414 /* poll is not restarted after signals... */
1415 if (error == ERESTART)
1416 error = EINTR;
1417 if (error == EWOULDBLOCK)
1418 error = 0;
1419 if (error == 0) {
1420 error = copyout(fds, uap->fds, nfds * sizeof(struct pollfd));
1421 *retval = rfds;
1422 }
1423
1424 out:
1425 if (NULL != cont)
1426 FREE(cont, M_TEMP);
1427
1428 kqueue_dealloc(kq, p);
1429 return (error);
1430}
1431
1432static int
1433poll_callback(__unused struct kqueue *kq, struct kevent *kevp, void *data)
1434{
1435 struct poll_continue_args *cont = (struct poll_continue_args *)data;
1436 struct pollfd *fds = CAST_DOWN(struct pollfd *, kevp->udata);
ff6e181a
A
1437 short mask;
1438
91447636
A
1439 /* convert the results back into revents */
1440 if (kevp->flags & EV_EOF)
1441 fds->revents |= POLLHUP;
1442 if (kevp->flags & EV_ERROR)
1443 fds->revents |= POLLERR;
1444 cont->pca_rfds++;
1445
1446 switch (kevp->filter) {
1447 case EVFILT_READ:
ff6e181a
A
1448 if (fds->revents & POLLHUP)
1449 mask = (POLLIN | POLLRDNORM | POLLPRI | POLLRDBAND );
1450 else {
1451 mask = 0;
1452 if (kevp->data != 0)
1453 mask |= (POLLIN | POLLRDNORM );
1454 if (kevp->flags & EV_OOBAND)
1455 mask |= ( POLLPRI | POLLRDBAND );
1456 }
1457 fds->revents |= (fds->events & mask);
91447636
A
1458 break;
1459
1460 case EVFILT_WRITE:
1461 if (!(fds->revents & POLLHUP))
1462 fds->revents |= (fds->events & ( POLLOUT | POLLWRNORM | POLLWRBAND ));
1463 break;
1464
1465 case EVFILT_PROC:
1466 if (kevp->fflags & NOTE_EXTEND)
1467 fds->revents |= (fds->events & POLLEXTEND);
1468 if (kevp->fflags & NOTE_ATTRIB)
1469 fds->revents |= (fds->events & POLLATTRIB);
1470 if (kevp->fflags & NOTE_LINK)
1471 fds->revents |= (fds->events & POLLNLINK);
1472 if (kevp->fflags & NOTE_WRITE)
1473 fds->revents |= (fds->events & POLLWRITE);
1474 break;
1475 }
1476 return 0;
1477}
1478
1479int
1480seltrue(__unused dev_t dev, __unused int flag, __unused struct proc *p)
1481{
1482
1483 return (1);
1484}
1485
1486static int
1487selcount(struct proc *p, u_int32_t *ibits, __unused u_int32_t *obits,
1488 int nfd, int *count)
1489{
1490 register struct filedesc *fdp = p->p_fd;
1491 register int msk, i, j, fd;
1492 register u_int32_t bits;
1493 struct fileproc *fp;
0b4e3aa0 1494 int n = 0;
91447636 1495 u_int32_t *iptr;
0b4e3aa0 1496 u_int nw;
91447636
A
1497 int error=0;
1498 int dropcount;
0b4e3aa0
A
1499
1500 /*
1501 * Problems when reboot; due to MacOSX signal probs
1502 * in Beaker1C ; verify that the p->p_fd is valid
1503 */
1504 if (fdp == NULL) {
1505 *count=0;
0b4e3aa0
A
1506 return(EIO);
1507 }
0b4e3aa0
A
1508 nw = howmany(nfd, NFDBITS);
1509
91447636 1510 proc_fdlock(p);
0b4e3aa0
A
1511 for (msk = 0; msk < 3; msk++) {
1512 iptr = (u_int32_t *)&ibits[msk * nw];
1513 for (i = 0; i < nfd; i += NFDBITS) {
1514 bits = iptr[i/NFDBITS];
1515 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1516 bits &= ~(1 << j);
1517 fp = fdp->fd_ofiles[fd];
1518 if (fp == NULL ||
1519 (fdp->fd_ofileflags[fd] & UF_RESERVED)) {
1520 *count=0;
91447636
A
1521 error = EBADF;
1522 goto bad;
0b4e3aa0 1523 }
91447636 1524 fp->f_iocount++;
0b4e3aa0
A
1525 n++;
1526 }
1527 }
1528 }
91447636
A
1529 proc_fdunlock(p);
1530
0b4e3aa0 1531 *count = n;
91447636
A
1532 return (0);
1533bad:
1534 dropcount = 0;
1535
1536 if (n== 0)
1537 goto out;
1538 /* undo the iocounts */
1539 for (msk = 0; msk < 3; msk++) {
1540 iptr = (u_int32_t *)&ibits[msk * nw];
1541 for (i = 0; i < nfd; i += NFDBITS) {
1542 bits = iptr[i/NFDBITS];
1543 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1544 bits &= ~(1 << j);
1545 fp = fdp->fd_ofiles[fd];
1546 if (dropcount >= n)
1547 goto out;
1548 fp->f_iocount--;
1549
1550 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1551 p->p_fpdrainwait = 0;
1552 wakeup(&p->p_fpdrainwait);
1553 }
1554 dropcount++;
1555 }
1556 }
1557 }
1558out:
1559 proc_fdunlock(p);
1560 return(error);
1561}
1562
1563static int
1564seldrop(p, ibits, nfd)
1565 struct proc *p;
1566 u_int32_t *ibits;
1567 int nfd;
1568{
1569 register struct filedesc *fdp = p->p_fd;
1570 register int msk, i, j, fd;
1571 register u_int32_t bits;
1572 struct fileproc *fp;
1573 int n = 0;
1574 u_int32_t *iptr;
1575 u_int nw;
1576
1577 /*
1578 * Problems when reboot; due to MacOSX signal probs
1579 * in Beaker1C ; verify that the p->p_fd is valid
1580 */
1581 if (fdp == NULL) {
1582 return(EIO);
1583 }
1584
1585 nw = howmany(nfd, NFDBITS);
1586
1587
1588 proc_fdlock(p);
1589 for (msk = 0; msk < 3; msk++) {
1590 iptr = (u_int32_t *)&ibits[msk * nw];
1591 for (i = 0; i < nfd; i += NFDBITS) {
1592 bits = iptr[i/NFDBITS];
1593 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
1594 bits &= ~(1 << j);
1595 fp = fdp->fd_ofiles[fd];
1596 if (fp == NULL
1597#if 0
1598 /* if you are here then it is being closed */
1599 || (fdp->fd_ofileflags[fd] & UF_RESERVED)
1600#endif
1601 ) {
1602 proc_fdunlock(p);
1603 return(EBADF);
1604 }
1605 n++;
1606 fp->f_iocount--;
1607 fp->f_flags &= ~FP_INSELECT;
1608
1609 if (p->p_fpdrainwait && fp->f_iocount == 0) {
1610 p->p_fpdrainwait = 0;
1611 wakeup(&p->p_fpdrainwait);
1612 }
1613 }
1614 }
1615 }
1616 proc_fdunlock(p);
0b4e3aa0
A
1617 return (0);
1618}
1619
1c79356b
A
1620/*
1621 * Record a select request.
1622 */
1623void
91447636 1624selrecord(__unused struct proc *selector, struct selinfo *sip, void * p_wql)
1c79356b 1625{
91447636 1626 thread_t cur_act = current_thread();
0b4e3aa0 1627 struct uthread * ut = get_bsdthread_info(cur_act);
1c79356b 1628
0b4e3aa0
A
1629 /* need to look at collisions */
1630
1631 if ((p_wql == (void *)0) && ((sip->si_flags & SI_INITED) == 0)) {
1c79356b
A
1632 return;
1633 }
0b4e3aa0
A
1634
1635 /*do not record if this is second pass of select */
1636 if((p_wql == (void *)0)) {
1637 return;
1c79356b
A
1638 }
1639
0b4e3aa0 1640 if ((sip->si_flags & SI_INITED) == 0) {
55e303ae 1641 wait_queue_init(&sip->si_wait_queue, SYNC_POLICY_FIFO);
0b4e3aa0
A
1642 sip->si_flags |= SI_INITED;
1643 sip->si_flags &= ~SI_CLEAR;
1644 }
1645
1646 if (sip->si_flags & SI_RECORDED) {
1647 sip->si_flags |= SI_COLL;
1648 } else
1649 sip->si_flags &= ~SI_COLL;
1650
1651 sip->si_flags |= SI_RECORDED;
91447636
A
1652 if (!wait_queue_member(&sip->si_wait_queue, ut->uu_select.wqset))
1653 wait_queue_link_noalloc(&sip->si_wait_queue, ut->uu_select.wqset,
1654 (wait_queue_link_t)p_wql);
0b4e3aa0 1655
1c79356b
A
1656 return;
1657}
1658
1659void
1660selwakeup(sip)
1661 register struct selinfo *sip;
1662{
1c79356b 1663
0b4e3aa0 1664 if ((sip->si_flags & SI_INITED) == 0) {
1c79356b 1665 return;
0b4e3aa0 1666 }
1c79356b
A
1667
1668 if (sip->si_flags & SI_COLL) {
1669 nselcoll++;
1670 sip->si_flags &= ~SI_COLL;
0b4e3aa0
A
1671#if 0
1672 /* will not support */
1673 //wakeup((caddr_t)&selwait);
1674#endif
1c79356b 1675 }
1c79356b 1676
0b4e3aa0 1677 if (sip->si_flags & SI_RECORDED) {
55e303ae 1678 wait_queue_wakeup_all(&sip->si_wait_queue, &selwait, THREAD_AWAKENED);
0b4e3aa0 1679 sip->si_flags &= ~SI_RECORDED;
1c79356b 1680 }
1c79356b 1681
1c79356b
A
1682}
1683
1684void
1685selthreadclear(sip)
1686 register struct selinfo *sip;
1687{
1c79356b 1688
0b4e3aa0
A
1689 if ((sip->si_flags & SI_INITED) == 0) {
1690 return;
1691 }
1692 if (sip->si_flags & SI_RECORDED) {
1693 selwakeup(sip);
1694 sip->si_flags &= ~(SI_RECORDED | SI_COLL);
1c79356b 1695 }
0b4e3aa0 1696 sip->si_flags |= SI_CLEAR;
55e303ae 1697 wait_queue_unlinkall_nofree(&sip->si_wait_queue);
1c79356b
A
1698}
1699
1700
91447636
A
1701
1702
6601e61a
A
1703#define DBG_EVENT 0x10
1704
91447636
A
1705#define DBG_POST 0x10
1706#define DBG_WATCH 0x11
1707#define DBG_WAIT 0x12
1708#define DBG_MOD 0x13
1709#define DBG_EWAKEUP 0x14
1710#define DBG_ENQUEUE 0x15
1711#define DBG_DEQUEUE 0x16
1712
1713#define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1714#define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1715#define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1716#define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1717#define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1718#define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1719#define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1720
1721
1722#define EVPROCDEQUE(p, evq) do { \
1723 proc_lock(p); \
1724 if (evq->ee_flags & EV_QUEUED) { \
1725 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1726 evq->ee_flags &= ~EV_QUEUED; \
1727 } \
1728 proc_unlock(p); \
1729} while (0);
1730
1c79356b
A
1731
1732/*
1733 * called upon socket close. deque and free all events for
91447636 1734 * the socket... socket must be locked by caller.
1c79356b 1735 */
9bccf70c 1736void
1c79356b
A
1737evsofree(struct socket *sp)
1738{
91447636
A
1739 struct eventqelt *evq, *next;
1740 proc_t p;
1741
1742 if (sp == NULL)
1743 return;
1c79356b 1744
91447636
A
1745 for (evq = sp->so_evlist.tqh_first; evq != NULL; evq = next) {
1746 next = evq->ee_slist.tqe_next;
1747 p = evq->ee_proc;
1c79356b 1748
91447636
A
1749 if (evq->ee_flags & EV_QUEUED) {
1750 EVPROCDEQUE(p, evq);
1751 }
1752 TAILQ_REMOVE(&sp->so_evlist, evq, ee_slist); // remove from socket q
1753 FREE(evq, M_TEMP);
1754 }
1c79356b
A
1755}
1756
1757
91447636
A
1758/*
1759 * called upon pipe close. deque and free all events for
1760 * the pipe... pipe must be locked by caller
1761 */
1762void
1763evpipefree(struct pipe *cpipe)
1764{
1765 struct eventqelt *evq, *next;
1766 proc_t p;
1c79356b 1767
91447636
A
1768 for (evq = cpipe->pipe_evlist.tqh_first; evq != NULL; evq = next) {
1769 next = evq->ee_slist.tqe_next;
1770 p = evq->ee_proc;
1c79356b 1771
91447636
A
1772 EVPROCDEQUE(p, evq);
1773
1774 TAILQ_REMOVE(&cpipe->pipe_evlist, evq, ee_slist); // remove from pipe q
1775 FREE(evq, M_TEMP);
1776 }
1777}
1c79356b
A
1778
1779
1780/*
91447636
A
1781 * enqueue this event if it's not already queued. wakeup
1782 * the proc if we do queue this event to it...
1783 * entered with proc lock held... we drop it before
1784 * doing the wakeup and return in that state
1c79356b 1785 */
91447636
A
1786static void
1787evprocenque(struct eventqelt *evq)
1c79356b 1788{
91447636
A
1789 proc_t p;
1790
1791 assert(evq);
1792 p = evq->ee_proc;
1793
1794 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_START, evq, evq->ee_flags, evq->ee_eventmask,0,0);
1795
1796 proc_lock(p);
1797
1798 if (evq->ee_flags & EV_QUEUED) {
1799 proc_unlock(p);
1800
1801 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1802 return;
1803 }
1804 evq->ee_flags |= EV_QUEUED;
1805
1806 TAILQ_INSERT_TAIL(&p->p_evlist, evq, ee_plist);
1807
1808 proc_unlock(p);
1809
1810 wakeup(&p->p_evlist);
1811
1812 KERNEL_DEBUG(DBG_MISC_ENQUEUE|DBG_FUNC_END, 0,0,0,0,0);
1c79356b
A
1813}
1814
91447636 1815
1c79356b 1816/*
91447636 1817 * pipe lock must be taken by the caller
1c79356b 1818 */
9bccf70c 1819void
91447636 1820postpipeevent(struct pipe *pipep, int event)
1c79356b 1821{
91447636
A
1822 int mask;
1823 struct eventqelt *evq;
1824
1825 if (pipep == NULL)
1826 return;
1827 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, event,0,0,1,0);
1828
1829 for (evq = pipep->pipe_evlist.tqh_first;
1830 evq != NULL; evq = evq->ee_slist.tqe_next) {
1831
1832 if (evq->ee_eventmask == 0)
1833 continue;
1834 mask = 0;
1835
1836 switch (event & (EV_RWBYTES | EV_RCLOSED | EV_WCLOSED)) {
1837
1838 case EV_RWBYTES:
1839 if ((evq->ee_eventmask & EV_RE) && pipep->pipe_buffer.cnt) {
1840 mask |= EV_RE;
1841 evq->ee_req.er_rcnt = pipep->pipe_buffer.cnt;
1842 }
1843 if ((evq->ee_eventmask & EV_WR) &&
1844 (pipep->pipe_buffer.size - pipep->pipe_buffer.cnt) >= PIPE_BUF) {
1845
1846 if (pipep->pipe_state & PIPE_EOF) {
1847 mask |= EV_WR|EV_RESET;
1848 break;
1849 }
1850 mask |= EV_WR;
1851 evq->ee_req.er_wcnt = pipep->pipe_buffer.size - pipep->pipe_buffer.cnt;
1852 }
1853 break;
1854
1855 case EV_WCLOSED:
1856 case EV_RCLOSED:
1857 if ((evq->ee_eventmask & EV_RE)) {
1858 mask |= EV_RE|EV_RCLOSED;
1859 }
1860 if ((evq->ee_eventmask & EV_WR)) {
1861 mask |= EV_WR|EV_WCLOSED;
1862 }
1863 break;
1864
1865 default:
1866 return;
1867 }
1868 if (mask) {
1869 /*
1870 * disarm... postevents are nops until this event is 'read' via
1871 * waitevent and then re-armed via modwatch
1872 */
1873 evq->ee_eventmask = 0;
1874
1875 /*
1876 * since events are disarmed until after the waitevent
1877 * the ee_req.er_xxxx fields can't change once we've
1878 * inserted this event into the proc queue...
1879 * therefore, the waitevent will see a 'consistent'
1880 * snapshot of the event, even though it won't hold
1881 * the pipe lock, and we're updating the event outside
1882 * of the proc lock, which it will hold
1883 */
1884 evq->ee_req.er_eventbits |= mask;
1885
1886 KERNEL_DEBUG(DBG_MISC_POST, evq, evq->ee_req.er_eventbits, mask, 1,0);
1887
1888 evprocenque(evq);
1889 }
1890 }
1891 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, 0,0,0,1,0);
1c79356b
A
1892}
1893
91447636 1894
1c79356b 1895/*
91447636
A
1896 * given either a sockbuf or a socket run down the
1897 * event list and queue ready events found...
1898 * the socket must be locked by the caller
1c79356b 1899 */
91447636
A
1900void
1901postevent(struct socket *sp, struct sockbuf *sb, int event)
1c79356b 1902{
91447636
A
1903 int mask;
1904 struct eventqelt *evq;
1905 struct tcpcb *tp;
1906
1907 if (sb)
1908 sp = sb->sb_so;
1909 if (sp == NULL)
1910 return;
1911
1912 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_START, (int)sp, event, 0, 0, 0);
1913
1914 for (evq = sp->so_evlist.tqh_first;
1915 evq != NULL; evq = evq->ee_slist.tqe_next) {
1916
1917 if (evq->ee_eventmask == 0)
1918 continue;
1919 mask = 0;
1920
1921 /* ready for reading:
1922 - byte cnt >= receive low water mark
1923 - read-half of conn closed
1924 - conn pending for listening sock
1925 - socket error pending
1926
1927 ready for writing
1928 - byte cnt avail >= send low water mark
1929 - write half of conn closed
1930 - socket error pending
1931 - non-blocking conn completed successfully
1932
1933 exception pending
1934 - out of band data
1935 - sock at out of band mark
1936 */
1937
1938 switch (event & EV_DMASK) {
1939
1940 case EV_OOB:
1941 if ((evq->ee_eventmask & EV_EX)) {
1942 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
1943 mask |= EV_EX|EV_OOB;
1944 }
1945 break;
1946
1947 case EV_RWBYTES|EV_OOB:
1948 if ((evq->ee_eventmask & EV_EX)) {
1949 if (sp->so_oobmark || ((sp->so_state & SS_RCVATMARK)))
1950 mask |= EV_EX|EV_OOB;
1951 }
1952 /*
1953 * fall into the next case
1954 */
1955 case EV_RWBYTES:
1956 if ((evq->ee_eventmask & EV_RE) && soreadable(sp)) {
1957 if (sp->so_error) {
1958 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1959 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1960 (tp->t_state == TCPS_CLOSED)) {
1961 mask |= EV_RE|EV_RESET;
1962 break;
1963 }
1964 }
1965 }
1966 mask |= EV_RE;
1967 evq->ee_req.er_rcnt = sp->so_rcv.sb_cc;
1968
1969 if (sp->so_state & SS_CANTRCVMORE) {
1970 mask |= EV_FIN;
1971 break;
1972 }
1973 }
1974 if ((evq->ee_eventmask & EV_WR) && sowriteable(sp)) {
1975 if (sp->so_error) {
1976 if ((sp->so_type == SOCK_STREAM) && ((sp->so_error == ECONNREFUSED) || (sp->so_error == ECONNRESET))) {
1977 if ((sp->so_pcb == 0) || (((struct inpcb *)sp->so_pcb)->inp_state == INPCB_STATE_DEAD) || !(tp = sototcpcb(sp)) ||
1978 (tp->t_state == TCPS_CLOSED)) {
1979 mask |= EV_WR|EV_RESET;
1980 break;
1981 }
1982 }
1983 }
1984 mask |= EV_WR;
1985 evq->ee_req.er_wcnt = sbspace(&sp->so_snd);
1986 }
1987 break;
1988
1989 case EV_RCONN:
1990 if ((evq->ee_eventmask & EV_RE)) {
1991 mask |= EV_RE|EV_RCONN;
1992 evq->ee_req.er_rcnt = sp->so_qlen + 1; // incl this one
1993 }
1994 break;
1995
1996 case EV_WCONN:
1997 if ((evq->ee_eventmask & EV_WR)) {
1998 mask |= EV_WR|EV_WCONN;
1999 }
2000 break;
2001
2002 case EV_RCLOSED:
2003 if ((evq->ee_eventmask & EV_RE)) {
2004 mask |= EV_RE|EV_RCLOSED;
2005 }
2006 break;
2007
2008 case EV_WCLOSED:
2009 if ((evq->ee_eventmask & EV_WR)) {
2010 mask |= EV_WR|EV_WCLOSED;
2011 }
2012 break;
2013
2014 case EV_FIN:
2015 if (evq->ee_eventmask & EV_RE) {
2016 mask |= EV_RE|EV_FIN;
2017 }
2018 break;
2019
2020 case EV_RESET:
2021 case EV_TIMEOUT:
2022 if (evq->ee_eventmask & EV_RE) {
2023 mask |= EV_RE | event;
2024 }
2025 if (evq->ee_eventmask & EV_WR) {
2026 mask |= EV_WR | event;
2027 }
2028 break;
2029
2030 default:
2031 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, -1, 0, 0, 0);
2032 return;
2033 } /* switch */
2034
2035 KERNEL_DEBUG(DBG_MISC_POST, (int)evq, evq->ee_eventmask, evq->ee_req.er_eventbits, mask, 0);
2036
2037 if (mask) {
2038 /*
2039 * disarm... postevents are nops until this event is 'read' via
2040 * waitevent and then re-armed via modwatch
2041 */
2042 evq->ee_eventmask = 0;
2043
2044 /*
2045 * since events are disarmed until after the waitevent
2046 * the ee_req.er_xxxx fields can't change once we've
2047 * inserted this event into the proc queue...
2048 * since waitevent can't see this event until we
2049 * enqueue it, waitevent will see a 'consistent'
2050 * snapshot of the event, even though it won't hold
2051 * the socket lock, and we're updating the event outside
2052 * of the proc lock, which it will hold
2053 */
2054 evq->ee_req.er_eventbits |= mask;
2055
2056 evprocenque(evq);
2057 }
2058 }
2059 KERNEL_DEBUG(DBG_MISC_POST|DBG_FUNC_END, (int)sp, 0, 0, 0, 0);
1c79356b
A
2060}
2061
1c79356b
A
2062
2063/*
2064 * watchevent system call. user passes us an event to watch
2065 * for. we malloc an event object, initialize it, and queue
2066 * it to the open socket. when the event occurs, postevent()
2067 * will enque it back to our proc where we can retrieve it
2068 * via waitevent().
2069 *
2070 * should this prevent duplicate events on same socket?
2071 */
2072int
91447636 2073watchevent(proc_t p, struct watchevent_args *uap, __unused int *retval)
1c79356b 2074{
91447636
A
2075 struct eventqelt *evq = (struct eventqelt *)0;
2076 struct eventqelt *np = NULL;
2077 struct eventreq *erp;
2078 struct fileproc *fp = NULL;
2079 int error;
2080
2081 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_START, 0,0,0,0,0);
2082
2083 // get a qelt and fill with users req
2084 MALLOC(evq, struct eventqelt *, sizeof(struct eventqelt), M_TEMP, M_WAITOK);
2085
2086 if (evq == NULL)
2087 panic("can't MALLOC evq");
2088 erp = &evq->ee_req;
2089
2090 // get users request pkt
2091 if ( (error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2092 sizeof(struct eventreq))) ) {
2093 FREE(evq, M_TEMP);
2094
2095 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2096 return(error);
2097 }
2098 KERNEL_DEBUG(DBG_MISC_WATCH, erp->er_handle,uap->u_eventmask,evq,0,0);
2099
2100 // validate, freeing qelt if errors
2101 error = 0;
2102 proc_fdlock(p);
2103
2104 if (erp->er_type != EV_FD) {
2105 error = EINVAL;
2106 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2107 error = EBADF;
2108 } else if (fp->f_type == DTYPE_SOCKET) {
2109 socket_lock((struct socket *)fp->f_data, 1);
2110 np = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2111 } else if (fp->f_type == DTYPE_PIPE) {
2112 PIPE_LOCK((struct pipe *)fp->f_data);
2113 np = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2114 } else {
2115 fp_drop(p, erp->er_handle, fp, 1);
2116 error = EINVAL;
2117 }
2118 proc_fdunlock(p);
2119
2120 if (error) {
2121 FREE(evq, M_TEMP);
2122
2123 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, error,0,0,0,0);
2124 return(error);
2125 }
2126
2127 /*
2128 * only allow one watch per file per proc
2129 */
2130 for ( ; np != NULL; np = np->ee_slist.tqe_next) {
2131 if (np->ee_proc == p) {
2132 if (fp->f_type == DTYPE_SOCKET)
2133 socket_unlock((struct socket *)fp->f_data, 1);
2134 else
2135 PIPE_UNLOCK((struct pipe *)fp->f_data);
2136 fp_drop(p, erp->er_handle, fp, 0);
2137 FREE(evq, M_TEMP);
2138
2139 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2140 return(EINVAL);
2141 }
2142 }
2143 erp->er_ecnt = erp->er_rcnt = erp->er_wcnt = erp->er_eventbits = 0;
2144 evq->ee_proc = p;
2145 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2146 evq->ee_flags = 0;
2147
2148 if (fp->f_type == DTYPE_SOCKET) {
2149 TAILQ_INSERT_TAIL(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2150 postevent((struct socket *)fp->f_data, 0, EV_RWBYTES); // catch existing events
2151
2152 socket_unlock((struct socket *)fp->f_data, 1);
2153 } else {
2154 TAILQ_INSERT_TAIL(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2155 postpipeevent((struct pipe *)fp->f_data, EV_RWBYTES);
2156
2157 PIPE_UNLOCK((struct pipe *)fp->f_data);
2158 }
2159 fp_drop_event(p, erp->er_handle, fp);
2160
2161 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, 0,0,0,0,0);
2162 return(0);
1c79356b
A
2163}
2164
91447636 2165
1c79356b
A
2166
2167/*
2168 * waitevent system call.
2169 * grabs the next waiting event for this proc and returns
2170 * it. if no events, user can request to sleep with timeout
2171 * or poll mode (tv=NULL);
2172 */
2173int
91447636 2174waitevent(proc_t p, struct waitevent_args *uap, int *retval)
1c79356b 2175{
91447636
A
2176 int error = 0;
2177 struct eventqelt *evq;
2178 struct eventreq er;
9bccf70c 2179 uint64_t abstime, interval;
1c79356b
A
2180
2181 if (uap->tv) {
9bccf70c
A
2182 struct timeval atv;
2183
91447636 2184 error = copyin(CAST_USER_ADDR_T(uap->tv), (caddr_t)&atv, sizeof (atv));
1c79356b 2185 if (error)
9bccf70c 2186 return(error);
1c79356b
A
2187 if (itimerfix(&atv)) {
2188 error = EINVAL;
2189 return(error);
2190 }
9bccf70c 2191 interval = tvtoabstime(&atv);
91447636
A
2192 } else
2193 interval = 0;
9bccf70c
A
2194
2195 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_START, 0,0,0,0,0);
1c79356b 2196
91447636 2197 proc_lock(p);
1c79356b 2198retry:
91447636
A
2199 if ((evq = p->p_evlist.tqh_first) != NULL) {
2200 /*
2201 * found one... make a local copy while it's still on the queue
2202 * to prevent it from changing while in the midst of copying
2203 * don't want to hold the proc lock across a copyout because
2204 * it might block on a page fault at the target in user space
2205 */
2206 bcopy((caddr_t)&evq->ee_req, (caddr_t)&er, sizeof (struct eventreq));
2207
2208 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist);
2209
2210 evq->ee_flags &= ~EV_QUEUED;
1c79356b 2211
91447636
A
2212 proc_unlock(p);
2213
2214 error = copyout((caddr_t)&er, CAST_USER_ADDR_T(uap->u_req), sizeof(struct eventreq));
2215
2216 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,
2217 evq->ee_req.er_handle,evq->ee_req.er_eventbits,evq,0);
9bccf70c
A
2218 return (error);
2219 }
2220 else {
2221 if (uap->tv && interval == 0) {
91447636 2222 proc_unlock(p);
9bccf70c 2223 *retval = 1; // poll failed
9bccf70c 2224
91447636 2225 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, error,0,0,0,0);
9bccf70c
A
2226 return (error);
2227 }
9bccf70c 2228 if (interval != 0)
55e303ae 2229 clock_absolutetime_interval_to_deadline(interval, &abstime);
91447636
A
2230 else
2231 abstime = 0;
9bccf70c
A
2232
2233 KERNEL_DEBUG(DBG_MISC_WAIT, 1,&p->p_evlist,0,0,0);
91447636
A
2234
2235 error = msleep1(&p->p_evlist, &p->p_mlock, (PSOCK | PCATCH), "waitevent", abstime);
2236
9bccf70c 2237 KERNEL_DEBUG(DBG_MISC_WAIT, 2,&p->p_evlist,0,0,0);
91447636 2238
9bccf70c
A
2239 if (error == 0)
2240 goto retry;
2241 if (error == ERESTART)
2242 error = EINTR;
2243 if (error == EWOULDBLOCK) {
2244 *retval = 1;
2245 error = 0;
2246 }
2247 }
91447636 2248 proc_unlock(p);
9bccf70c
A
2249
2250 KERNEL_DEBUG(DBG_MISC_WAIT|DBG_FUNC_END, 0,0,0,0,0);
9bccf70c 2251 return (error);
1c79356b
A
2252}
2253
1c79356b
A
2254
2255/*
2256 * modwatch system call. user passes in event to modify.
2257 * if we find it we reset the event bits and que/deque event
2258 * it needed.
2259 */
2260int
91447636 2261modwatch(proc_t p, struct modwatch_args *uap, __unused int *retval)
1c79356b 2262{
91447636
A
2263 struct eventreq er;
2264 struct eventreq *erp = &er;
2265 struct eventqelt *evq;
2266 int error;
2267 struct fileproc *fp;
2268 int flag;
2269
2270 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_START, 0,0,0,0,0);
2271
2272 /*
2273 * get user's request pkt
2274 */
2275 if ((error = copyin(CAST_USER_ADDR_T(uap->u_req), (caddr_t)erp,
2276 sizeof(struct eventreq)))) {
2277 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2278 return(error);
2279 }
2280 proc_fdlock(p);
2281
2282 if (erp->er_type != EV_FD) {
2283 error = EINVAL;
2284 } else if ((error = fp_lookup(p, erp->er_handle, &fp, 1)) != 0) {
2285 error = EBADF;
2286 } else if (fp->f_type == DTYPE_SOCKET) {
2287 socket_lock((struct socket *)fp->f_data, 1);
2288 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2289 } else if (fp->f_type == DTYPE_PIPE) {
2290 PIPE_LOCK((struct pipe *)fp->f_data);
2291 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2292 } else {
2293 fp_drop(p, erp->er_handle, fp, 1);
2294 error = EINVAL;
2295 }
2296
2297 if (error) {
2298 proc_fdunlock(p);
2299 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, error,0,0,0,0);
2300 return(error);
2301 }
2302
2303 if ((uap->u_eventmask == EV_RM) && (fp->f_flags & FP_WAITEVENT)) {
2304 fp->f_flags &= ~FP_WAITEVENT;
2305 }
2306 proc_fdunlock(p);
2307
2308 // locate event if possible
2309 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2310 if (evq->ee_proc == p)
2311 break;
2312 }
2313 if (evq == NULL) {
2314 if (fp->f_type == DTYPE_SOCKET)
2315 socket_unlock((struct socket *)fp->f_data, 1);
2316 else
2317 PIPE_UNLOCK((struct pipe *)fp->f_data);
2318 fp_drop(p, erp->er_handle, fp, 0);
2319 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, EINVAL,0,0,0,0);
2320 return(EINVAL);
2321 }
2322 KERNEL_DEBUG(DBG_MISC_MOD, erp->er_handle,uap->u_eventmask,evq,0,0);
2323
2324 if (uap->u_eventmask == EV_RM) {
2325 EVPROCDEQUE(p, evq);
2326
2327 if (fp->f_type == DTYPE_SOCKET) {
2328 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2329 socket_unlock((struct socket *)fp->f_data, 1);
2330 } else {
2331 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2332 PIPE_UNLOCK((struct pipe *)fp->f_data);
2333 }
2334 fp_drop(p, erp->er_handle, fp, 0);
2335 FREE(evq, M_TEMP);
2336 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, 0,0,0,0,0);
2337 return(0);
2338 }
2339 switch (uap->u_eventmask & EV_MASK) {
1c79356b 2340
91447636
A
2341 case 0:
2342 flag = 0;
2343 break;
2344
2345 case EV_RE:
2346 case EV_WR:
2347 case EV_RE|EV_WR:
2348 flag = EV_RWBYTES;
2349 break;
2350
2351 case EV_EX:
2352 flag = EV_OOB;
2353 break;
2354
2355 case EV_EX|EV_RE:
2356 case EV_EX|EV_WR:
2357 case EV_EX|EV_RE|EV_WR:
2358 flag = EV_OOB|EV_RWBYTES;
2359 break;
2360
2361 default:
2362 if (fp->f_type == DTYPE_SOCKET)
2363 socket_unlock((struct socket *)fp->f_data, 1);
2364 else
2365 PIPE_UNLOCK((struct pipe *)fp->f_data);
2366 fp_drop(p, erp->er_handle, fp, 0);
2367 KERNEL_DEBUG(DBG_MISC_WATCH|DBG_FUNC_END, EINVAL,0,0,0,0);
2368 return(EINVAL);
2369 }
2370 /*
2371 * since we're holding the socket/pipe lock, the event
2372 * cannot go from the unqueued state to the queued state
2373 * however, it can go from the queued state to the unqueued state
2374 * since that direction is protected by the proc_lock...
2375 * so do a quick check for EV_QUEUED w/o holding the proc lock
2376 * since by far the common case will be NOT EV_QUEUED, this saves
2377 * us taking the proc_lock the majority of the time
2378 */
2379 if (evq->ee_flags & EV_QUEUED) {
2380 /*
2381 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2382 */
2383 EVPROCDEQUE(p, evq);
2384 }
2385 /*
2386 * while the event is off the proc queue and
2387 * we're holding the socket/pipe lock
2388 * it's safe to update these fields...
2389 */
2390 evq->ee_req.er_eventbits = 0;
2391 evq->ee_eventmask = uap->u_eventmask & EV_MASK;
2392
2393 if (fp->f_type == DTYPE_SOCKET) {
2394 postevent((struct socket *)fp->f_data, 0, flag);
2395 socket_unlock((struct socket *)fp->f_data, 1);
2396 }
2397 else {
2398 postpipeevent((struct pipe *)fp->f_data, flag);
2399 PIPE_UNLOCK((struct pipe *)fp->f_data);
2400 }
2401 fp_drop(p, erp->er_handle, fp, 0);
2402 KERNEL_DEBUG(DBG_MISC_MOD|DBG_FUNC_END, evq->ee_req.er_handle,evq->ee_eventmask,fp->f_data,flag,0);
2403 return(0);
1c79356b 2404}
91447636
A
2405
2406/* this routine is called from the close of fd with proc_fdlock held */
2407int
2408waitevent_close(struct proc *p, struct fileproc *fp)
2409{
2410 struct eventqelt *evq;
2411
2412
2413 fp->f_flags &= ~FP_WAITEVENT;
2414
2415 if (fp->f_type == DTYPE_SOCKET) {
2416 socket_lock((struct socket *)fp->f_data, 1);
2417 evq = ((struct socket *)fp->f_data)->so_evlist.tqh_first;
2418 }
2419 else if (fp->f_type == DTYPE_PIPE) {
2420 PIPE_LOCK((struct pipe *)fp->f_data);
2421 evq = ((struct pipe *)fp->f_data)->pipe_evlist.tqh_first;
2422 }
2423 else {
2424 return(EINVAL);
2425 }
2426 proc_fdunlock(p);
2427
2428
2429 // locate event if possible
2430 for ( ; evq != NULL; evq = evq->ee_slist.tqe_next) {
2431 if (evq->ee_proc == p)
2432 break;
2433 }
2434 if (evq == NULL) {
2435 if (fp->f_type == DTYPE_SOCKET)
2436 socket_unlock((struct socket *)fp->f_data, 1);
2437 else
2438 PIPE_UNLOCK((struct pipe *)fp->f_data);
2439
2440 proc_fdlock(p);
2441
2442 return(EINVAL);
2443 }
2444 EVPROCDEQUE(p, evq);
2445
2446 if (fp->f_type == DTYPE_SOCKET) {
2447 TAILQ_REMOVE(&((struct socket *)fp->f_data)->so_evlist, evq, ee_slist);
2448 socket_unlock((struct socket *)fp->f_data, 1);
2449 } else {
2450 TAILQ_REMOVE(&((struct pipe *)fp->f_data)->pipe_evlist, evq, ee_slist);
2451 PIPE_UNLOCK((struct pipe *)fp->f_data);
2452 }
2453 FREE(evq, M_TEMP);
2454
2455 proc_fdlock(p);
2456
2457 return(0);
2458}
2459