/
kern_event.c
2283 lines (1976 loc) · 49.9 KB
/
kern_event.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: kern_event.c,v 1.198 2023/08/20 15:13:43 visa Exp $ */
/*-
* Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD: src/sys/kern/kern_event.c,v 1.22 2001/02/23 20:32:42 jlemon Exp $
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/pledge.h>
#include <sys/malloc.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/fcntl.h>
#include <sys/queue.h>
#include <sys/event.h>
#include <sys/eventvar.h>
#include <sys/ktrace.h>
#include <sys/pool.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <sys/time.h>
#include <sys/timeout.h>
#include <sys/vnode.h>
#include <sys/wait.h>
#ifdef DIAGNOSTIC
#define KLIST_ASSERT_LOCKED(kl) do { \
if ((kl)->kl_ops != NULL) \
(kl)->kl_ops->klo_assertlk((kl)->kl_arg); \
else \
KERNEL_ASSERT_LOCKED(); \
} while (0)
#else
#define KLIST_ASSERT_LOCKED(kl) ((void)(kl))
#endif
int dokqueue(struct proc *, int, register_t *);
struct kqueue *kqueue_alloc(struct filedesc *);
void kqueue_terminate(struct proc *p, struct kqueue *);
void KQREF(struct kqueue *);
void KQRELE(struct kqueue *);
void kqueue_purge(struct proc *, struct kqueue *);
int kqueue_sleep(struct kqueue *, struct timespec *);
int kqueue_read(struct file *, struct uio *, int);
int kqueue_write(struct file *, struct uio *, int);
int kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
struct proc *p);
int kqueue_kqfilter(struct file *fp, struct knote *kn);
int kqueue_stat(struct file *fp, struct stat *st, struct proc *p);
int kqueue_close(struct file *fp, struct proc *p);
void kqueue_wakeup(struct kqueue *kq);
#ifdef KQUEUE_DEBUG
void kqueue_do_check(struct kqueue *kq, const char *func, int line);
#define kqueue_check(kq) kqueue_do_check((kq), __func__, __LINE__)
#else
#define kqueue_check(kq) do {} while (0)
#endif
static int filter_attach(struct knote *kn);
static void filter_detach(struct knote *kn);
static int filter_event(struct knote *kn, long hint);
static int filter_modify(struct kevent *kev, struct knote *kn);
static int filter_process(struct knote *kn, struct kevent *kev);
static void kqueue_expand_hash(struct kqueue *kq);
static void kqueue_expand_list(struct kqueue *kq, int fd);
static void kqueue_task(void *);
static int klist_lock(struct klist *);
static void klist_unlock(struct klist *, int);
const struct fileops kqueueops = {
.fo_read = kqueue_read,
.fo_write = kqueue_write,
.fo_ioctl = kqueue_ioctl,
.fo_kqfilter = kqueue_kqfilter,
.fo_stat = kqueue_stat,
.fo_close = kqueue_close
};
void knote_attach(struct knote *kn);
void knote_detach(struct knote *kn);
void knote_drop(struct knote *kn, struct proc *p);
void knote_enqueue(struct knote *kn);
void knote_dequeue(struct knote *kn);
int knote_acquire(struct knote *kn, struct klist *, int);
void knote_release(struct knote *kn);
void knote_activate(struct knote *kn);
void knote_remove(struct proc *p, struct kqueue *kq, struct knlist **plist,
int idx, int purge);
void filt_kqdetach(struct knote *kn);
int filt_kqueue(struct knote *kn, long hint);
int filt_kqueuemodify(struct kevent *kev, struct knote *kn);
int filt_kqueueprocess(struct knote *kn, struct kevent *kev);
int filt_kqueue_common(struct knote *kn, struct kqueue *kq);
int filt_procattach(struct knote *kn);
void filt_procdetach(struct knote *kn);
int filt_proc(struct knote *kn, long hint);
int filt_fileattach(struct knote *kn);
void filt_timerexpire(void *knx);
int filt_timerattach(struct knote *kn);
void filt_timerdetach(struct knote *kn);
int filt_timermodify(struct kevent *kev, struct knote *kn);
int filt_timerprocess(struct knote *kn, struct kevent *kev);
void filt_seltruedetach(struct knote *kn);
const struct filterops kqread_filtops = {
.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
.f_attach = NULL,
.f_detach = filt_kqdetach,
.f_event = filt_kqueue,
.f_modify = filt_kqueuemodify,
.f_process = filt_kqueueprocess,
};
const struct filterops proc_filtops = {
.f_flags = 0,
.f_attach = filt_procattach,
.f_detach = filt_procdetach,
.f_event = filt_proc,
};
const struct filterops file_filtops = {
.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
.f_attach = filt_fileattach,
.f_detach = NULL,
.f_event = NULL,
};
const struct filterops timer_filtops = {
.f_flags = 0,
.f_attach = filt_timerattach,
.f_detach = filt_timerdetach,
.f_event = NULL,
.f_modify = filt_timermodify,
.f_process = filt_timerprocess,
};
struct pool knote_pool;
struct pool kqueue_pool;
struct mutex kqueue_klist_lock = MUTEX_INITIALIZER(IPL_MPFLOOR);
int kq_ntimeouts = 0;
int kq_timeoutmax = (4 * 1024);
#define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
/*
* Table for all system-defined filters.
*/
const struct filterops *const sysfilt_ops[] = {
&file_filtops, /* EVFILT_READ */
&file_filtops, /* EVFILT_WRITE */
NULL, /*&aio_filtops,*/ /* EVFILT_AIO */
&file_filtops, /* EVFILT_VNODE */
&proc_filtops, /* EVFILT_PROC */
&sig_filtops, /* EVFILT_SIGNAL */
&timer_filtops, /* EVFILT_TIMER */
&file_filtops, /* EVFILT_DEVICE */
&file_filtops, /* EVFILT_EXCEPT */
};
void
KQREF(struct kqueue *kq)
{
refcnt_take(&kq->kq_refcnt);
}
void
KQRELE(struct kqueue *kq)
{
struct filedesc *fdp;
if (refcnt_rele(&kq->kq_refcnt) == 0)
return;
fdp = kq->kq_fdp;
if (rw_status(&fdp->fd_lock) == RW_WRITE) {
LIST_REMOVE(kq, kq_next);
} else {
fdplock(fdp);
LIST_REMOVE(kq, kq_next);
fdpunlock(fdp);
}
KASSERT(TAILQ_EMPTY(&kq->kq_head));
KASSERT(kq->kq_nknotes == 0);
free(kq->kq_knlist, M_KEVENT, kq->kq_knlistsize *
sizeof(struct knlist));
hashfree(kq->kq_knhash, KN_HASHSIZE, M_KEVENT);
klist_free(&kq->kq_klist);
pool_put(&kqueue_pool, kq);
}
void
kqueue_init(void)
{
pool_init(&kqueue_pool, sizeof(struct kqueue), 0, IPL_MPFLOOR,
PR_WAITOK, "kqueuepl", NULL);
pool_init(&knote_pool, sizeof(struct knote), 0, IPL_MPFLOOR,
PR_WAITOK, "knotepl", NULL);
}
void
kqueue_init_percpu(void)
{
pool_cache_init(&knote_pool);
}
int
filt_fileattach(struct knote *kn)
{
struct file *fp = kn->kn_fp;
return fp->f_ops->fo_kqfilter(fp, kn);
}
int
kqueue_kqfilter(struct file *fp, struct knote *kn)
{
struct kqueue *kq = kn->kn_fp->f_data;
if (kn->kn_filter != EVFILT_READ)
return (EINVAL);
kn->kn_fop = &kqread_filtops;
klist_insert(&kq->kq_klist, kn);
return (0);
}
void
filt_kqdetach(struct knote *kn)
{
struct kqueue *kq = kn->kn_fp->f_data;
klist_remove(&kq->kq_klist, kn);
}
int
filt_kqueue_common(struct knote *kn, struct kqueue *kq)
{
MUTEX_ASSERT_LOCKED(&kq->kq_lock);
kn->kn_data = kq->kq_count;
return (kn->kn_data > 0);
}
int
filt_kqueue(struct knote *kn, long hint)
{
struct kqueue *kq = kn->kn_fp->f_data;
int active;
mtx_enter(&kq->kq_lock);
active = filt_kqueue_common(kn, kq);
mtx_leave(&kq->kq_lock);
return (active);
}
int
filt_kqueuemodify(struct kevent *kev, struct knote *kn)
{
struct kqueue *kq = kn->kn_fp->f_data;
int active;
mtx_enter(&kq->kq_lock);
knote_assign(kev, kn);
active = filt_kqueue_common(kn, kq);
mtx_leave(&kq->kq_lock);
return (active);
}
int
filt_kqueueprocess(struct knote *kn, struct kevent *kev)
{
struct kqueue *kq = kn->kn_fp->f_data;
int active;
mtx_enter(&kq->kq_lock);
if (kev != NULL && (kn->kn_flags & EV_ONESHOT))
active = 1;
else
active = filt_kqueue_common(kn, kq);
if (active)
knote_submit(kn, kev);
mtx_leave(&kq->kq_lock);
return (active);
}
int
filt_procattach(struct knote *kn)
{
struct process *pr;
int s;
if ((curproc->p_p->ps_flags & PS_PLEDGE) &&
(curproc->p_p->ps_pledge & PLEDGE_PROC) == 0)
return pledge_fail(curproc, EPERM, PLEDGE_PROC);
if (kn->kn_id > PID_MAX)
return ESRCH;
pr = prfind(kn->kn_id);
if (pr == NULL)
return (ESRCH);
/* exiting processes can't be specified */
if (pr->ps_flags & PS_EXITING)
return (ESRCH);
kn->kn_ptr.p_process = pr;
kn->kn_flags |= EV_CLEAR; /* automatically set */
/*
* internal flag indicating registration done by kernel
*/
if (kn->kn_flags & EV_FLAG1) {
kn->kn_data = kn->kn_sdata; /* ppid */
kn->kn_fflags = NOTE_CHILD;
kn->kn_flags &= ~EV_FLAG1;
}
s = splhigh();
klist_insert_locked(&pr->ps_klist, kn);
splx(s);
return (0);
}
/*
* The knote may be attached to a different process, which may exit,
* leaving nothing for the knote to be attached to. So when the process
* exits, the knote is marked as DETACHED and also flagged as ONESHOT so
* it will be deleted when read out. However, as part of the knote deletion,
* this routine is called, so a check is needed to avoid actually performing
* a detach, because the original process does not exist any more.
*/
void
filt_procdetach(struct knote *kn)
{
struct kqueue *kq = kn->kn_kq;
struct process *pr = kn->kn_ptr.p_process;
int s, status;
mtx_enter(&kq->kq_lock);
status = kn->kn_status;
mtx_leave(&kq->kq_lock);
if (status & KN_DETACHED)
return;
s = splhigh();
klist_remove_locked(&pr->ps_klist, kn);
splx(s);
}
int
filt_proc(struct knote *kn, long hint)
{
struct kqueue *kq = kn->kn_kq;
u_int event;
/*
* mask off extra data
*/
event = (u_int)hint & NOTE_PCTRLMASK;
/*
* if the user is interested in this event, record it.
*/
if (kn->kn_sfflags & event)
kn->kn_fflags |= event;
/*
* process is gone, so flag the event as finished and remove it
* from the process's klist
*/
if (event == NOTE_EXIT) {
struct process *pr = kn->kn_ptr.p_process;
int s;
mtx_enter(&kq->kq_lock);
kn->kn_status |= KN_DETACHED;
mtx_leave(&kq->kq_lock);
s = splhigh();
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
kn->kn_data = W_EXITCODE(pr->ps_xexit, pr->ps_xsig);
klist_remove_locked(&pr->ps_klist, kn);
splx(s);
return (1);
}
/*
* process forked, and user wants to track the new process,
* so attach a new knote to it, and immediately report an
* event with the parent's pid.
*/
if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
struct kevent kev;
int error;
/*
* register knote with new process.
*/
memset(&kev, 0, sizeof(kev));
kev.ident = hint & NOTE_PDATAMASK; /* pid */
kev.filter = kn->kn_filter;
kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
kev.fflags = kn->kn_sfflags;
kev.data = kn->kn_id; /* parent */
kev.udata = kn->kn_udata; /* preserve udata */
error = kqueue_register(kq, &kev, 0, NULL);
if (error)
kn->kn_fflags |= NOTE_TRACKERR;
}
return (kn->kn_fflags != 0);
}
#define NOTE_TIMER_UNITMASK \
(NOTE_SECONDS|NOTE_MSECONDS|NOTE_USECONDS|NOTE_NSECONDS)
static int
filt_timervalidate(int sfflags, int64_t sdata, struct timespec *ts)
{
if (sfflags & ~(NOTE_TIMER_UNITMASK | NOTE_ABSTIME))
return (EINVAL);
switch (sfflags & NOTE_TIMER_UNITMASK) {
case NOTE_SECONDS:
ts->tv_sec = sdata;
ts->tv_nsec = 0;
break;
case NOTE_MSECONDS:
ts->tv_sec = sdata / 1000;
ts->tv_nsec = (sdata % 1000) * 1000000;
break;
case NOTE_USECONDS:
ts->tv_sec = sdata / 1000000;
ts->tv_nsec = (sdata % 1000000) * 1000;
break;
case NOTE_NSECONDS:
ts->tv_sec = sdata / 1000000000;
ts->tv_nsec = sdata % 1000000000;
break;
default:
return (EINVAL);
}
return (0);
}
static void
filt_timeradd(struct knote *kn, struct timespec *ts)
{
struct timespec expiry, now;
struct timeout *to = kn->kn_hook;
int tticks;
if (kn->kn_sfflags & NOTE_ABSTIME) {
nanotime(&now);
if (timespeccmp(ts, &now, >)) {
timespecsub(ts, &now, &expiry);
/* XXX timeout_abs_ts with CLOCK_REALTIME */
timeout_add(to, tstohz(&expiry));
} else {
/* Expire immediately. */
filt_timerexpire(kn);
}
return;
}
tticks = tstohz(ts);
/* Remove extra tick from tstohz() if timeout has fired before. */
if (timeout_triggered(to))
tticks--;
timeout_add(to, (tticks > 0) ? tticks : 1);
}
void
filt_timerexpire(void *knx)
{
struct timespec ts;
struct knote *kn = knx;
struct kqueue *kq = kn->kn_kq;
kn->kn_data++;
mtx_enter(&kq->kq_lock);
knote_activate(kn);
mtx_leave(&kq->kq_lock);
if ((kn->kn_flags & EV_ONESHOT) == 0 &&
(kn->kn_sfflags & NOTE_ABSTIME) == 0) {
(void)filt_timervalidate(kn->kn_sfflags, kn->kn_sdata, &ts);
filt_timeradd(kn, &ts);
}
}
/*
* data contains amount of time to sleep
*/
int
filt_timerattach(struct knote *kn)
{
struct timespec ts;
struct timeout *to;
int error;
error = filt_timervalidate(kn->kn_sfflags, kn->kn_sdata, &ts);
if (error != 0)
return (error);
if (kq_ntimeouts > kq_timeoutmax)
return (ENOMEM);
kq_ntimeouts++;
if ((kn->kn_sfflags & NOTE_ABSTIME) == 0)
kn->kn_flags |= EV_CLEAR; /* automatically set */
to = malloc(sizeof(*to), M_KEVENT, M_WAITOK);
timeout_set(to, filt_timerexpire, kn);
kn->kn_hook = to;
filt_timeradd(kn, &ts);
return (0);
}
void
filt_timerdetach(struct knote *kn)
{
struct timeout *to;
to = (struct timeout *)kn->kn_hook;
timeout_del_barrier(to);
free(to, M_KEVENT, sizeof(*to));
kq_ntimeouts--;
}
int
filt_timermodify(struct kevent *kev, struct knote *kn)
{
struct timespec ts;
struct kqueue *kq = kn->kn_kq;
struct timeout *to = kn->kn_hook;
int error;
error = filt_timervalidate(kev->fflags, kev->data, &ts);
if (error != 0) {
kev->flags |= EV_ERROR;
kev->data = error;
return (0);
}
/* Reset the timer. Any pending events are discarded. */
timeout_del_barrier(to);
mtx_enter(&kq->kq_lock);
if (kn->kn_status & KN_QUEUED)
knote_dequeue(kn);
kn->kn_status &= ~KN_ACTIVE;
mtx_leave(&kq->kq_lock);
kn->kn_data = 0;
knote_assign(kev, kn);
/* Reinit timeout to invoke tick adjustment again. */
timeout_set(to, filt_timerexpire, kn);
filt_timeradd(kn, &ts);
return (0);
}
int
filt_timerprocess(struct knote *kn, struct kevent *kev)
{
int active, s;
s = splsoftclock();
active = (kn->kn_data != 0);
if (active)
knote_submit(kn, kev);
splx(s);
return (active);
}
/*
* filt_seltrue:
*
* This filter "event" routine simulates seltrue().
*/
int
filt_seltrue(struct knote *kn, long hint)
{
/*
* We don't know how much data can be read/written,
* but we know that it *can* be. This is about as
* good as select/poll does as well.
*/
kn->kn_data = 0;
return (1);
}
int
filt_seltruemodify(struct kevent *kev, struct knote *kn)
{
knote_assign(kev, kn);
return (kn->kn_fop->f_event(kn, 0));
}
int
filt_seltrueprocess(struct knote *kn, struct kevent *kev)
{
int active;
active = kn->kn_fop->f_event(kn, 0);
if (active)
knote_submit(kn, kev);
return (active);
}
/*
* This provides full kqfilter entry for device switch tables, which
* has same effect as filter using filt_seltrue() as filter method.
*/
void
filt_seltruedetach(struct knote *kn)
{
/* Nothing to do */
}
const struct filterops seltrue_filtops = {
.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
.f_attach = NULL,
.f_detach = filt_seltruedetach,
.f_event = filt_seltrue,
.f_modify = filt_seltruemodify,
.f_process = filt_seltrueprocess,
};
int
seltrue_kqfilter(dev_t dev, struct knote *kn)
{
switch (kn->kn_filter) {
case EVFILT_READ:
case EVFILT_WRITE:
kn->kn_fop = &seltrue_filtops;
break;
default:
return (EINVAL);
}
/* Nothing more to do */
return (0);
}
static int
filt_dead(struct knote *kn, long hint)
{
if (kn->kn_filter == EVFILT_EXCEPT) {
/*
* Do not deliver event because there is no out-of-band data.
* However, let HUP condition pass for poll(2).
*/
if ((kn->kn_flags & __EV_POLL) == 0) {
kn->kn_flags |= EV_DISABLE;
return (0);
}
}
kn->kn_flags |= (EV_EOF | EV_ONESHOT);
if (kn->kn_flags & __EV_POLL)
kn->kn_flags |= __EV_HUP;
kn->kn_data = 0;
return (1);
}
static void
filt_deaddetach(struct knote *kn)
{
/* Nothing to do */
}
const struct filterops dead_filtops = {
.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
.f_attach = NULL,
.f_detach = filt_deaddetach,
.f_event = filt_dead,
.f_modify = filt_seltruemodify,
.f_process = filt_seltrueprocess,
};
static int
filt_badfd(struct knote *kn, long hint)
{
kn->kn_flags |= (EV_ERROR | EV_ONESHOT);
kn->kn_data = EBADF;
return (1);
}
/* For use with kqpoll. */
const struct filterops badfd_filtops = {
.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
.f_attach = NULL,
.f_detach = filt_deaddetach,
.f_event = filt_badfd,
.f_modify = filt_seltruemodify,
.f_process = filt_seltrueprocess,
};
static int
filter_attach(struct knote *kn)
{
int error;
if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
error = kn->kn_fop->f_attach(kn);
} else {
KERNEL_LOCK();
error = kn->kn_fop->f_attach(kn);
KERNEL_UNLOCK();
}
return (error);
}
static void
filter_detach(struct knote *kn)
{
if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
kn->kn_fop->f_detach(kn);
} else {
KERNEL_LOCK();
kn->kn_fop->f_detach(kn);
KERNEL_UNLOCK();
}
}
static int
filter_event(struct knote *kn, long hint)
{
if ((kn->kn_fop->f_flags & FILTEROP_MPSAFE) == 0)
KERNEL_ASSERT_LOCKED();
return (kn->kn_fop->f_event(kn, hint));
}
static int
filter_modify(struct kevent *kev, struct knote *kn)
{
int active, s;
if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
active = kn->kn_fop->f_modify(kev, kn);
} else {
KERNEL_LOCK();
if (kn->kn_fop->f_modify != NULL) {
active = kn->kn_fop->f_modify(kev, kn);
} else {
s = splhigh();
active = knote_modify(kev, kn);
splx(s);
}
KERNEL_UNLOCK();
}
return (active);
}
static int
filter_process(struct knote *kn, struct kevent *kev)
{
int active, s;
if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
active = kn->kn_fop->f_process(kn, kev);
} else {
KERNEL_LOCK();
if (kn->kn_fop->f_process != NULL) {
active = kn->kn_fop->f_process(kn, kev);
} else {
s = splhigh();
active = knote_process(kn, kev);
splx(s);
}
KERNEL_UNLOCK();
}
return (active);
}
/*
* Initialize the current thread for poll/select system call.
* num indicates the number of serials that the system call may utilize.
* After this function, the valid range of serials is
* p_kq_serial <= x < p_kq_serial + num.
*/
void
kqpoll_init(unsigned int num)
{
struct proc *p = curproc;
struct filedesc *fdp;
if (p->p_kq == NULL) {
p->p_kq = kqueue_alloc(p->p_fd);
p->p_kq_serial = arc4random();
fdp = p->p_fd;
fdplock(fdp);
LIST_INSERT_HEAD(&fdp->fd_kqlist, p->p_kq, kq_next);
fdpunlock(fdp);
}
if (p->p_kq_serial + num < p->p_kq_serial) {
/* Serial is about to wrap. Clear all attached knotes. */
kqueue_purge(p, p->p_kq);
p->p_kq_serial = 0;
}
}
/*
* Finish poll/select system call.
* num must have the same value that was used with kqpoll_init().
*/
void
kqpoll_done(unsigned int num)
{
struct proc *p = curproc;
struct kqueue *kq = p->p_kq;
KASSERT(p->p_kq != NULL);
KASSERT(p->p_kq_serial + num >= p->p_kq_serial);
p->p_kq_serial += num;
/*
* Because of kn_pollid key, a thread can in principle allocate
* up to O(maxfiles^2) knotes by calling poll(2) repeatedly
* with suitably varying pollfd arrays.
* Prevent such a large allocation by clearing knotes eagerly
* if there are too many of them.
*
* A small multiple of kq_knlistsize should give enough margin
* that eager clearing is infrequent, or does not happen at all,
* with normal programs.
* A single pollfd entry can use up to three knotes.
* Typically there is no significant overlap of fd and events
* between different entries in the pollfd array.
*/
if (kq->kq_nknotes > 4 * kq->kq_knlistsize)
kqueue_purge(p, kq);
}
void
kqpoll_exit(void)
{
struct proc *p = curproc;
if (p->p_kq == NULL)
return;
kqueue_purge(p, p->p_kq);
kqueue_terminate(p, p->p_kq);
KASSERT(p->p_kq->kq_refcnt.r_refs == 1);
KQRELE(p->p_kq);
p->p_kq = NULL;
}
struct kqueue *
kqueue_alloc(struct filedesc *fdp)
{
struct kqueue *kq;
kq = pool_get(&kqueue_pool, PR_WAITOK | PR_ZERO);
refcnt_init(&kq->kq_refcnt);
kq->kq_fdp = fdp;
TAILQ_INIT(&kq->kq_head);
mtx_init(&kq->kq_lock, IPL_HIGH);
task_set(&kq->kq_task, kqueue_task, kq);
klist_init_mutex(&kq->kq_klist, &kqueue_klist_lock);
return (kq);
}
int
dokqueue(struct proc *p, int flags, register_t *retval)
{
struct filedesc *fdp = p->p_fd;
struct kqueue *kq;
struct file *fp;
int cloexec, error, fd;
cloexec = (flags & O_CLOEXEC) ? UF_EXCLOSE : 0;
kq = kqueue_alloc(fdp);
fdplock(fdp);
error = falloc(p, &fp, &fd);
if (error)
goto out;
fp->f_flag = FREAD | FWRITE | (flags & FNONBLOCK);
fp->f_type = DTYPE_KQUEUE;
fp->f_ops = &kqueueops;
fp->f_data = kq;
*retval = fd;
LIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_next);
kq = NULL;
fdinsert(fdp, fd, cloexec, fp);
FRELE(fp, p);
out:
fdpunlock(fdp);
if (kq != NULL)
pool_put(&kqueue_pool, kq);
return (error);
}
int
sys_kqueue(struct proc *p, void *v, register_t *retval)
{
return (dokqueue(p, 0, retval));
}
int
sys_kqueue1(struct proc *p, void *v, register_t *retval)
{
struct sys_kqueue1_args /* {
syscallarg(int) flags;
} */ *uap = v;
if (SCARG(uap, flags) & ~(O_CLOEXEC | FNONBLOCK))
return (EINVAL);
return (dokqueue(p, SCARG(uap, flags), retval));
}
int
sys_kevent(struct proc *p, void *v, register_t *retval)
{
struct kqueue_scan_state scan;
struct filedesc* fdp = p->p_fd;
struct sys_kevent_args /* {
syscallarg(int) fd;
syscallarg(const struct kevent *) changelist;
syscallarg(int) nchanges;
syscallarg(struct kevent *) eventlist;
syscallarg(int) nevents;
syscallarg(const struct timespec *) timeout;
} */ *uap = v;
struct kevent *kevp;
struct kqueue *kq;
struct file *fp;
struct timespec ts;
struct timespec *tsp = NULL;
int i, n, nerrors, error;
int ready, total;
struct kevent kev[KQ_NEVENTS];
if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
return (EBADF);
if (fp->f_type != DTYPE_KQUEUE) {
error = EBADF;
goto done;
}
if (SCARG(uap, timeout) != NULL) {
error = copyin(SCARG(uap, timeout), &ts, sizeof(ts));
if (error)
goto done;
#ifdef KTRACE
if (KTRPOINT(p, KTR_STRUCT))