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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
|
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2018 Joyent, Inc.
#
SUBDIRS = tests
include Makefile.subdirs
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2019 Joyent, Inc.
# Copyright 2022 Oxide Computer Company
#
include $(SRC)/Makefile.master
include $(SRC)/cmd/Makefile.cmd
include $(SRC)/cmd/Makefile.cmd.64
#
# Force c99 for everything
#
CSTD= $(CSTD_GNU99)
CPPFLAGS = -I$(COMPAT)/bhyve -I$(CONTRIB)/bhyve \
-I$(COMPAT)/bhyve/amd64 -I$(CONTRIB)/bhyve/amd64 \
$(CPPFLAGS.master) \
-I$(SRC)/cmd/bhyve/common \
-DWITHOUT_CAPSICUM
SMOFF += all_func_returns
CLOBBERFILES += $(PROG)
#
# Install related definitions
#
ROOTOPTPKG = $(ROOT)/opt/bhyve-tests
ROOTTESTS = $(ROOTOPTPKG)/tests
TESTDIR = $(ROOTTESTS)/$(TESTSUBDIR)
FILEMODE = 0555
LDLIBS = $(LDLIBS.cmd)
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2018 Joyent, Inc.
#
.KEEP_STATE:
all : TARGET += all
clean : TARGET += clean
clobber : TARGET += clobber
install : TARGET += install
all clean clobber install: $(SUBDIRS)
$(filter-out .WAIT,$(SUBDIRS)): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2018 Joyent, Inc.
# Copyright 2022 Oxide Computer Company
#
$(ROOTOPTPKG):
$(INS.dir)
$(ROOTTESTS): $(ROOTOPTPKG)
$(INS.dir)
$(TESTDIR): $(ROOTTESTS)
$(INS.dir)
$(TESTDIR)/%: % $(TESTDIR)
$(INS.file)
clean:
-$(RM) *.o $(CLEANFILES)
clobber: clean
-$(RM) $(CLOBBERFILES)
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2018 Joyent, Inc.
#
SUBDIRS = mevent
include ../Makefile.subdirs
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Copyright 2018 Joyent, Inc.
# Copyright 2022 Oxide Computer Company
# Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
#
TESTSUBDIR = mevent
PROG = \
lists_delete \
mevent_test \
read_disable \
read_pause \
read_requeue \
vnode_file \
vnode_zvol
SUPOBJS = mevent.o testlib.o
include ../../Makefile.com
CMDS = $(PROG:%=$(TESTDIR)/%)
$(CMDS) : FILEMODE = 0555
all: $(PROG)
install: $(TESTDIR) $(CMDS)
$(CMDS): $(PROG)
vnode_zvol : LDLIBS += -lzfs -lnvpair
mevent_test : LDLIBS += -lsocket
include ../../Makefile.targ
# Hammerhead: Cancel GNU Make built-in compile-and-link rule (%: %.c)
%: %.c
%: %.o $(SUPOBJS)
$(LINK.c) -o $@ $< $(SUPOBJS) $(LDLIBS)
$(POST_PROCESS)
mevent.o: ../../../common/mevent.c
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Test: lists.delete
* Assertion: mevent_delete() causes the total number of events to decrease
*
* Strategy: 1. Create a pipe.
* 2. Call mevent_add() to be notified of writes to the pipe. The
* callback will do nothing other than generate an error if it
* is called.
* 3. Create another pipe and add a read event watcher to it. The
* callback will signal a cv when called. A write to the pipe
* followed by a wait on the cv will ensure that async
* operations in mevent.c are complete. See flush_and_wait().
* 4. Call flush_and_wait(), then get event count.
* 5. Delete the event created in step 2.
* 6. Call flush_and_wait(), then get event count.
* 7. Verify result in step 6 is one less than result in step 4.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static int
get_count(void)
{
int global = -1, change = -1, del_pending = -1;
int total;
test_mevent_count_lists(&global, &change, &del_pending);
ASSERT_INT_NEQ(("count not set"), global, -1);
ASSERT_INT_NEQ(("count not set"), change, -1);
ASSERT_INT_NEQ(("count not set"), change, -1);
ASSERT_INT_EQ(("pending delete not processed"), del_pending, 0);
total = global + change + del_pending;
VERBOSE(("count = %d (%d + %d + %d)", total, global, change,
del_pending));
return (total);
}
static void
not_called_cb(int fd, enum ev_type ev, void *arg)
{
FAIL(("this callback should never be called"));
}
static void
flush_cb(int fd, enum ev_type ev, void *arg)
{
char buf[32];
/* Drain the pipe */
while (read(fd, buf, sizeof (buf)) > 0)
;
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
pthread_mutex_unlock(&mtx);
}
void
flush_and_wait(int fd)
{
uint8_t msg = 42;
/*
* Lock taken ahead of waking flush_cb so this thread doesn't race
* with the event thread.
*/
pthread_mutex_lock(&mtx);
if (write(fd, &msg, sizeof (msg)) != sizeof (msg)) {
FAIL(("bad write"));
}
/* Wait for it to be read */
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
}
int
main(int argc, const char *argv[])
{
int unused_pipe[2];
int flush_pipe[2];
struct mevent *unused_evp, *flush_evp;
int count1, count2;
start_test(argv[0], 5);
start_event_thread();
/*
* Create first pipe and related event
*/
if (pipe(unused_pipe) != 0) {
FAIL_ERRNO("pipe");
}
VERBOSE(("unused_pipe[] = { %d, %d }", unused_pipe[0], unused_pipe[1]));
if (fcntl(unused_pipe[0], F_SETFL, O_NONBLOCK) != 0) {
FAIL_ERRNO("set pipe nonblocking");
}
unused_evp = mevent_add(unused_pipe[0], EVF_READ, not_called_cb, NULL);
ASSERT_PTR_NEQ(("mevent_add"), unused_evp, NULL);
/*
* Create flush pipe and related event
*/
if (pipe(flush_pipe) != 0) {
FAIL_ERRNO("pipe");
}
VERBOSE(("flush_pipe[] = { %d, %d }", flush_pipe[0],
flush_pipe[1]));
if (fcntl(flush_pipe[0], F_SETFL, O_NONBLOCK) != 0) {
FAIL_ERRNO("set pipe nonblocking");
}
flush_evp = mevent_add(flush_pipe[0], EVF_READ, flush_cb, NULL);
ASSERT_PTR_NEQ(("mevent_add"), flush_evp, NULL);
/* Get count before delete. */
flush_and_wait(flush_pipe[1]);
count1 = get_count();
/*
* Delete the first event and flush a read after the delete is
* complete.
*/
if (mevent_delete(unused_evp) != 0) {
FAIL_ERRNO("mevent_delete");
}
/*
* Verify count decreased.
*/
flush_and_wait(flush_pipe[1]);
count2 = get_count();
if (count1 - 1 != count2) {
FAIL(("mevent_delete() did not decrease count by 1: "
"was %d, now %d", count1, count2));
}
PASS();
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
#include "../../../common/mevent.c"
#include "testlib.h"
/*
* Returns by reference the number of events on the global and change lists.
*
* Used by tests that wish to ensure that the event count changes as suggested
* by mevent_add() and mevent_delete(). Note that a delete does not immediately
* delete an event. Events that are pending delete are included in the change
* list until the next pass through the change list to process pending changes.
*/
void
test_mevent_count_lists(int *ret_global, int *ret_change, int *ret_del_pending)
{
struct mevent *mevp;
int global = 0;
int change = 0;
int del_pending = 0;
mevent_qlock();
LIST_FOREACH(mevp, &global_head, me_list) {
global++;
VERBOSE(("on global: type %d fd %d state %d", mevp->me_type,
mevp->me_fd, mevp->me_state));
}
LIST_FOREACH(mevp, &change_head, me_list) {
change++;
if (mevp->me_state == EV_DELETE) {
del_pending++;
}
VERBOSE(("on change: type %d fd %d state %d", mevp->me_type,
mevp->me_fd, mevp->me_state));
}
mevent_qunlock();
*ret_global = global;
*ret_change = change;
*ret_del_pending = del_pending;
}
void
set_mevent_file_poll_interval_ms(int ms)
{
mevent_file_poll_interval_ms = ms;
}
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2011 NetApp, Inc.
* 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 NETAPP, INC ``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 NETAPP, INC 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.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Test program for the micro event library. Set up a simple TCP echo
* service.
*
* cc mevent_test.c mevent.c -lpthread
*/
#include <sys/types.h>
#include <sys/stdint.h>
#ifdef __FreeBSD__
#include <sys/sysctl.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef __FreeBSD__
#include <machine/cpufunc.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "mevent.h"
#define TEST_PORT 4321
static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t accept_condvar = PTHREAD_COND_INITIALIZER;
static struct mevent *tevp;
#define MEVENT_ECHO
/* Number of timer events to capture */
#define TEVSZ 4096
uint64_t tevbuf[TEVSZ];
static void
timer_print(void)
{
uint64_t min, max, diff, sum;
#ifdef __FreeBSD__
uint64_t tsc_freq;
size_t len;
#endif
int j;
min = UINT64_MAX;
max = 0;
sum = 0;
#ifdef __FreeBSD__
len = sizeof(tsc_freq);
sysctlbyname("machdep.tsc_freq", &tsc_freq, &len, NULL, 0);
#endif
for (j = 1; j < TEVSZ; j++) {
#ifdef __FreeBSD__
/* Convert a tsc diff into microseconds */
diff = (tevbuf[j] - tevbuf[j-1]) * 1000000 / tsc_freq;
#else
diff = (tevbuf[j] - tevbuf[j-1]) / 1000;
#endif
sum += diff;
if (min > diff)
min = diff;
if (max < diff)
max = diff;
}
printf("timers done: usecs, min %ld, max %ld, mean %ld\n", min, max,
sum/(TEVSZ - 1));
}
static void
timer_callback(int fd, enum ev_type type, void *param)
{
static int i;
if (i >= TEVSZ)
abort();
#ifdef __FreeBSD__
tevbuf[i++] = rdtsc();
#else
tevbuf[i++] = gethrtime();
#endif
if (i == TEVSZ) {
mevent_delete(tevp);
timer_print();
}
}
#ifdef MEVENT_ECHO
struct esync {
pthread_mutex_t e_mt;
pthread_cond_t e_cond;
};
static void
echoer_callback(int fd, enum ev_type type, void *param)
{
struct esync *sync = param;
pthread_mutex_lock(&sync->e_mt);
pthread_cond_signal(&sync->e_cond);
pthread_mutex_unlock(&sync->e_mt);
}
static void *
echoer(void *param)
{
struct esync sync;
struct mevent *mev;
char buf[128];
int fd = (int)(uintptr_t) param;
int len;
pthread_mutex_init(&sync.e_mt, NULL);
pthread_cond_init(&sync.e_cond, NULL);
pthread_mutex_lock(&sync.e_mt);
mev = mevent_add(fd, EVF_READ, echoer_callback, &sync);
if (mev == NULL) {
printf("Could not allocate echoer event\n");
exit(4);
}
while (!pthread_cond_wait(&sync.e_cond, &sync.e_mt)) {
len = read(fd, buf, sizeof(buf));
if (len > 0) {
write(fd, buf, len);
write(0, buf, len);
} else {
break;
}
}
mevent_delete_close(mev);
pthread_mutex_unlock(&sync.e_mt);
pthread_mutex_destroy(&sync.e_mt);
pthread_cond_destroy(&sync.e_cond);
return (NULL);
}
#else
static void *
echoer(void *param)
{
char buf[128];
int fd = (int)(uintptr_t) param;
int len;
while ((len = read(fd, buf, sizeof(buf))) > 0) {
write(1, buf, len);
}
return (NULL);
}
#endif /* MEVENT_ECHO */
static void
acceptor_callback(int fd, enum ev_type type, void *param)
{
pthread_mutex_lock(&accept_mutex);
pthread_cond_signal(&accept_condvar);
pthread_mutex_unlock(&accept_mutex);
}
static void *
acceptor(void *param)
{
struct sockaddr_in sin;
pthread_t tid;
int news;
int s;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
exit(4);
}
#ifdef __FreeBSD__
sin.sin_len = sizeof(sin);
#endif
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(TEST_PORT);
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("cannot bind socket");
exit(4);
}
if (listen(s, 1) < 0) {
perror("cannot listen socket");
exit(4);
}
(void) mevent_add(s, EVF_READ, acceptor_callback, NULL);
pthread_mutex_lock(&accept_mutex);
while (!pthread_cond_wait(&accept_condvar, &accept_mutex)) {
news = accept(s, NULL, NULL);
if (news < 0) {
perror("accept error");
} else {
static int first = 1;
if (first) {
/*
* Start a timer
*/
first = 0;
tevp = mevent_add(1, EVF_TIMER, timer_callback,
NULL);
}
printf("incoming connection, spawning thread\n");
pthread_create(&tid, NULL, echoer,
(void *)(uintptr_t)news);
}
}
return (NULL);
}
int
main()
{
pthread_t tid;
pthread_create(&tid, NULL, acceptor, NULL);
mevent_dispatch();
return (0);
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Test: read.cancel
* Assertion: A read is not requeued if mevent_disable() is called while it is
* being handled.
*
* Strategy: 1. Create a pipe
* 2. Call mevent_add() to be notified of writes to the pipe. The
* callback will signal a cv.
* 3. Write to the pipe then wait for a wakeup.
* 4. From the read event callback, disable the event then awaken
* the main thread.
* 5. In the main thread, add a timer event that will awaken the
* main thread after a short delay.
* 5. Write to the pipe and wait to be awoken. The wakeup should
* come from the timer event, not the read event.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
typedef enum {
CB_NONE,
CB_READ,
CB_TIMER,
} lastwake_t;
static lastwake_t lastwake = CB_NONE;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static struct mevent *read_event;
static void
munch(int fd, enum ev_type ev, void *arg)
{
ssize_t nbytes;
char buf[32] = { 0 };
int err;
if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
FAIL_ERRNO("bad read");
}
VERBOSE(("read %ld bytes '%s'", nbytes, buf));
err = mevent_disable(read_event);
ASSERT_INT_EQ(("mevent_disable: ", strerror(err)), err, 0);
pthread_mutex_lock(&mtx);
ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_NONE);
lastwake = CB_READ;
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
static void
tick(int ms, enum ev_type ev, void *arg)
{
pthread_mutex_lock(&mtx);
ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_READ);
lastwake = CB_TIMER;
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
int
main(int argc, const char *argv[])
{
int pipefds[2];
struct mevent *timer;
ssize_t written;
char *msgs[] = { "first", "second" };
char *msg;
start_test(argv[0], 5);
start_event_thread();
if (pipe(pipefds) != 0) {
FAIL_ERRNO("pipe");
}
if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
FAIL_ERRNO("set pipe nonblocking");
}
/*
* First write
*/
msg = msgs[0];
read_event = mevent_add(pipefds[0], EVF_READ, munch, msg);
ASSERT_PTR_NEQ(("mevent_add pipefd"), read_event, NULL);
pthread_mutex_lock(&mtx);
written = write(pipefds[1], msg, strlen(msg));
if (written < 0) {
FAIL_ERRNO("bad write");
}
ASSERT_INT64_EQ(("write '%s' failed", msg), written, strlen(msg));
/*
* Wait for it to be read
*/
pthread_cond_wait(&cv, &mtx);
ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_READ);
pthread_mutex_unlock(&mtx);
/*
* Add timer, second write.
*/
msg = msgs[1];
timer = mevent_add(50, EVF_TIMER, tick, msg);
ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);
pthread_mutex_lock(&mtx);
written = write(pipefds[1], msg, strlen(msg));
if (written < 0) {
FAIL_ERRNO("bad write");
}
ASSERT_INT64_EQ(("write '%s' failed", msg), written, strlen(msg));
/*
* Wait for timer to expire
*/
pthread_cond_wait(&cv, &mtx);
ASSERT_INT_EQ(("wrong lastwake"), lastwake, CB_TIMER);
pthread_mutex_unlock(&mtx);
PASS();
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Test: read.pause
* Assertion: mevent_disable() can be used to pause reads.
*
* Strategy: 1. Create a pipe
* 2. Call mevent_add() to be notified of writes to the pipe. The
* callback will signal a cv.
* 3. In a loop, write to the pipe then wait on the cv.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static char cookie[] = "Chocolate chip with fudge stripes";
/*
* After this many bytes are sent, writes will get batched up, progress will be
* made on the write side via an interval timer
*/
const int pauseat = 8;
static void
munch(int fd, enum ev_type ev, void *arg)
{
static int i = 0;
char buf[sizeof (cookie)] = { 0 };
ssize_t nbytes;
ssize_t expected;
ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
/*
* For the first while, expect data to come a byte at a time. After the
* pause, we should get a burst with the rest of the data.
*/
if (i > pauseat) {
expected = strlen(cookie) - pauseat - 1;
} else {
expected = 1;
}
if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
FAIL_ERRNO("bad read");
}
VERBOSE(("read %ld bytes '%s'", nbytes, buf));
ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, expected);
if (expected == 1) {
ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
} else {
ASSERT_STR_EQ(("bad last half of cookie"), buf, &cookie[i]);
}
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
i++;
}
static void
tick(int ms, enum ev_type ev, void *arg)
{
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
int
main(int argc, const char *argv[])
{
int pipefds[2];
struct mevent *evp, *timer;
ssize_t written;
start_test(argv[0], 5);
start_event_thread();
if (pipe(pipefds) != 0) {
FAIL_ERRNO("pipe");
}
if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
FAIL_ERRNO("set pipe nonblocking");
}
evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
ASSERT_PTR_NEQ(("mevent_add pipefd"), evp, NULL);
for (int i = 0; cookie[i] != 0; i++) {
pthread_mutex_lock(&mtx);
written = write(pipefds[1], cookie + i, 1);
if (written < 0) {
FAIL_ERRNO("bad write");
}
ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
/* Wait for it to be read */
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
if (i == pauseat) {
timer = mevent_add(10, EVF_TIMER, tick,
&cookie[pauseat]);
ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);
VERBOSE(("disable munch"));
mevent_disable(evp);
}
}
pthread_mutex_lock(&mtx);
mevent_enable(evp);
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
PASS();
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Test: read.requeue
* Assertion: A sequence of writes turns into a sequence of events.
*
* Strategy: 1. Create a pipe
* 2. Call mevent_add() to be notified of writes to the pipe. The
* callback will signal a cv.
* 3. In a loop, write to the pipe then wait on the cv.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static char *cookie = "Chocolate chip with fudge stripes";
static void
munch(int fd, enum ev_type ev, void *arg)
{
static int i = 0;
char buf[8] = { 0 };
ssize_t nbytes;
ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
ASSERT_INT64_EQ(("bad read: %s", strerror(errno)), nbytes, 1);
}
VERBOSE(("read %ld bytes '%s'", nbytes, buf));
ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, 1);
ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
i++;
}
int
main(int argc, const char *argv[])
{
int pipefds[2];
struct mevent *evp;
start_test(argv[0], 5);
start_event_thread();
if (pipe(pipefds) != 0) {
FAIL_ERRNO("pipe");
}
if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
FAIL_ERRNO("set pipe nonblocking");
}
evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
ASSERT_PTR_NEQ(("mevent_add"), evp, NULL);
for (int i = 0; cookie[i] != '\0'; i++) {
ssize_t written;
pthread_mutex_lock(&mtx);
written = write(pipefds[1], cookie + i, 1);
if (written < 0) {
FAIL_ERRNO("bad write");
}
ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
/* Wait for it to be read */
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
}
PASS();
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
#include <pthread.h>
#include <signal.h>
#include <strings.h>
#include <unistd.h>
#include "testlib.h"
#include "mevent.h"
const char *testlib_prog;
boolean_t testlib_verbose;
static void
timed_out(int signo)
{
ASSERT_INT_EQ(("timeout signal"), signo, SIGALRM);
FAIL(("Timed out"));
}
void
start_test(const char *argv0, uint32_t timeout)
{
char *val;
testlib_prog = strrchr(argv0, '/');
if (testlib_prog == NULL) {
testlib_prog = argv0;
} else {
testlib_prog++;
}
testlib_verbose = ((val = getenv("TEST_VERBOSE")) != NULL) &&
val[0] != '\0';
signal(SIGALRM, timed_out);
alarm(timeout);
}
/* ARGSUSED */
static void *
event_thread(void *arg)
{
mevent_dispatch();
return (NULL);
}
void
start_event_thread(void)
{
pthread_t tid;
if (pthread_create(&tid, NULL, event_thread, NULL) != 0) {
FAIL_ERRNO("pthread_create");
}
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
#ifndef _TESTLIB_H_
#define _TESTLIB_H_
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "mevent.h"
#define EXIT_PASS 0
#define EXIT_FAIL 1
#define VERBOSE(msg) \
if (testlib_verbose) { \
(void) printf("VERBOSE %s: %s:%d %s: ", testlib_prog, \
__FILE__, __LINE__, __func__); \
(void) printf msg; \
(void) printf("\n"); \
}
#define FAIL_PROLOGUE() \
(void) printf("FAIL %s: %s:%d: ", testlib_prog, __FILE__, __LINE__)
#define FAIL(msg) \
{ \
FAIL_PROLOGUE(); \
(void) printf msg; \
(void) printf("\n"); \
exit(EXIT_FAIL); \
}
#define FAIL_ERRNO(msg) FAIL((msg ": %s", strerror(errno)))
#define PASS() \
{ \
(void) printf("PASS %s\n", testlib_prog); \
exit(EXIT_PASS); \
}
#define ASSERT_CMP(msg, got, cmp, exp, nfmt) \
if (!(got cmp exp)) { \
FAIL_PROLOGUE(); \
(void) printf msg; \
(void) printf(": %s=" nfmt " %s %s=" nfmt "\n", \
#got, got, #cmp, #exp, exp); \
exit(EXIT_FAIL); \
}
#define ASSERT_CHAR_EQ(msg, got, exp) ASSERT_CMP(msg, got, ==, exp, "%c")
#define ASSERT_INT_EQ(msg, got, exp) ASSERT_CMP(msg, got, ==, exp, "%d")
#define ASSERT_INT_NEQ(msg, got, exp) ASSERT_CMP(msg, got, !=, exp, "%d")
#define ASSERT_INT64_EQ(msg, got, exp) ASSERT_CMP(msg, got, ==, exp, "%ld")
#define ASSERT_INT64_NEQ(msg, got, exp) ASSERT_CMP(msg, got, !=, exp, "%ld")
#define ASSERT_PTR_EQ(msg, got, exp) ASSERT_CMP(msg, got, ==, exp, "%p")
#define ASSERT_PTR_NEQ(msg, got, exp) ASSERT_CMP(msg, got, !=, exp, "%p")
#define ASSERT_STR_EQ(msg, got, exp) \
if (strcmp(got, exp) != 0) { \
FAIL_PROLOGUE(); \
(void) printf msg; \
(void) printf(": %s='%s' != %s='%s'\n", \
#got, got, #exp, exp); \
exit(EXIT_FAIL); \
}
extern const char *testlib_prog;
extern boolean_t testlib_verbose;
extern void start_test(const char *, uint32_t);
extern void start_event_thread(void);
extern void test_mevent_count_lists(int *, int *, int *);
extern void set_mevent_file_poll_interval_ms(int);
#endif /* _TESTLIB_H_ */
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
static char *cookie = "Shortcake";
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static void
callback(int fd, enum ev_type ev, void *arg)
{
static off_t size = 0;
struct stat st;
ASSERT_INT_EQ(("bad event"), ev, EVF_VNODE);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
if (fstat(fd, &st) != 0)
FAIL_ERRNO("fstat failed");
ASSERT_INT64_NEQ(("File size has not changed"), size, st.st_size);
size = st.st_size;
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
static void
test_fd(int fd, char *tag)
{
struct mevent *evp;
int err;
evp = mevent_add_flags(fd, EVF_VNODE, EVFF_ATTRIB, callback, cookie);
ASSERT_PTR_NEQ(("%s: mevent_add", tag), evp, NULL);
for (uint_t i = 0; cookie[i] != '\0'; i++) {
ssize_t written;
if (i > 0) {
/*
* Check that no events are emitted for writes which do
* not alter the size.
*/
if (lseek(fd, -1, SEEK_CUR) == -1)
FAIL_ERRNO("lseek");
if (write(fd, "X", 1) == -1)
FAIL_ERRNO("write");
/*
* Allow time for the callback to fire if it is going
* to.
*/
VERBOSE(("Write within"));
usleep(100);
}
pthread_mutex_lock(&mtx);
written = write(fd, cookie + i, 1);
if (written < 0)
FAIL_ERRNO("bad write");
ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
VERBOSE(("Write extend"));
/* Wait for the size change to be processed */
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
/*
* This is a bit unsatisfactory but we need to allow time
* for mevent to re-associate the port or the next write could
* be missed.
*/
usleep(100);
}
err = mevent_disable(evp);
ASSERT_INT_EQ(("%s: mevent_disable: %s", tag, strerror(err)), err, 0);
(void) printf("PASS %s - %s\n", testlib_prog, tag);
}
int
main(int argc, const char **argv)
{
int fd;
start_test(argv[0], 20);
set_mevent_file_poll_interval_ms(500);
start_event_thread();
/* Test with a temporary file in /tmp */
char *template = strdup("/tmp/mevent.vnode.XXXXXX");
ASSERT_PTR_NEQ(("strdup"), template, NULL);
fd = mkstemp(template);
if (fd == -1)
FAIL_ERRNO("Couldn't create temporary file with mkstemp");
VERBOSE(("Opened temporary file at '%s'", template));
test_fd(fd, "temporary file");
/* Test with a file which is unlinked from the filesystem */
FILE *fp = tmpfile();
ASSERT_PTR_NEQ(("tmpfile"), fp, NULL);
fd = fileno(fp);
if (fd == -1)
FAIL_ERRNO("Couldn't get file descriptor for temporary file");
test_fd(fd, "anon file");
/*
* Defer to here to avoid generating a new event before the disable has
* been processed and the port deassociated.
*/
unlink(template);
free(template);
PASS();
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Joyent, Inc.
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
*/
#include <errno.h>
#include <fcntl.h>
#include <libzfs.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <zone.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "testlib.h"
#include "mevent.h"
#define MB (1024 * 1024)
static char *cookie = "Chocolate chip with fudge stripes";
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
static void
callback(int fd, enum ev_type ev, void *arg)
{
static off_t size = 0;
struct stat st;
ASSERT_INT_EQ(("bad event"), ev, EVF_VNODE);
ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
if (fstat(fd, &st) != 0)
FAIL_ERRNO("fstat failed");
ASSERT_INT64_NEQ(("Size has not changed"), size, st.st_size);
size = st.st_size;
pthread_mutex_lock(&mtx);
pthread_cond_signal(&cv);
VERBOSE(("wakeup"));
pthread_mutex_unlock(&mtx);
}
static void
destroy_zpool(libzfs_handle_t *zfshdl, zpool_handle_t *poolhdl,
zfs_handle_t *volhdl)
{
if (volhdl != NULL) {
if (zfs_destroy(volhdl, B_FALSE) != 0) {
FAIL(("Failed to destroy ZVOL - %s",
libzfs_error_description(zfshdl)));
}
}
if (poolhdl != NULL) {
if (zpool_destroy(poolhdl, testlib_prog) != 0) {
FAIL(("Failed to destroy ZPOOL - %s",
libzfs_error_description(zfshdl)));
}
}
}
static void
create_zpool(libzfs_handle_t *zfshdl, const char *pool, const char *file)
{
nvlist_t *nvroot, *props;
nvlist_t *vdevs[1];
nvroot = fnvlist_alloc();
props = fnvlist_alloc();
vdevs[0] = fnvlist_alloc();
fnvlist_add_string(vdevs[0], ZPOOL_CONFIG_PATH, file);
fnvlist_add_string(vdevs[0], ZPOOL_CONFIG_TYPE, VDEV_TYPE_FILE);
fnvlist_add_uint64(vdevs[0], ZPOOL_CONFIG_IS_LOG, 0);
fnvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT);
fnvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, vdevs, 1);
fnvlist_add_string(props,
zfs_prop_to_name(ZFS_PROP_MOUNTPOINT), ZFS_MOUNTPOINT_NONE);
if (zpool_create(zfshdl, pool, nvroot, NULL, props) != 0) {
FAIL(("Failed to create ZPOOL %s using %s - %s",
pool, file, libzfs_error_description(zfshdl)));
}
VERBOSE(("Created ZFS pool %s", pool));
}
static bool
create_zvol(libzfs_handle_t *zfshdl, const char *vol)
{
nvlist_t *volprops;
int err;
volprops = fnvlist_alloc();
fnvlist_add_uint64(volprops,
zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1 * MB);
err = zfs_create(zfshdl, vol, ZFS_TYPE_VOLUME, volprops);
if (err != 0) {
(void) printf("Failed to create ZVOL %s - %s",
vol, libzfs_error_description(zfshdl));
return (false);
}
VERBOSE(("Created ZVOL %s", vol));
return (true);
}
int
main(int argc, const char **argv)
{
libzfs_handle_t *zfshdl;
char *template, *pool, *vol, *backend;
struct mevent *evp;
zpool_handle_t *poolhdl = NULL;
zfs_handle_t *volhdl = NULL;
int err, fd;
start_test(argv[0], 10);
set_mevent_file_poll_interval_ms(1000);
if (getzoneid() != GLOBAL_ZONEID)
FAIL(("Can only be run in the global zone"));
if ((zfshdl = libzfs_init()) == NULL)
FAIL_ERRNO("Could not open ZFS library");
template = strdup("/tmp/mevent.vnode.zvol.XXXXXX");
ASSERT_PTR_NEQ(("strdup"), template, NULL);
fd = mkstemp(template);
if (fd == -1)
FAIL_ERRNO("Couldn't create temporary file with mkstemp");
VERBOSE(("Opened temporary file at '%s'", template));
err = asprintf(&pool, "mevent_test_%d", getpid());
ASSERT_INT_NEQ(("asprintf pool"), err, -1);
err = asprintf(&vol, "%s/test_zvol_%d", pool, getpid());
ASSERT_INT_NEQ(("asprintf vol"), err, -1);
err = asprintf(&backend, "/dev/zvol/rdsk/%s", vol);
ASSERT_INT_NEQ(("asprintf backend"), err, -1);
err = ftruncate(fd, 64 * MB);
if (err != 0)
FAIL_ERRNO("ftruncate");
(void) close(fd);
fd = -1;
/*
* Create the pool as late as possible to reduce the risk of leaving
* a test pool hanging around.
*/
create_zpool(zfshdl, pool, template);
if ((poolhdl = zpool_open(zfshdl, pool)) == NULL) {
(void) printf("Could not open ZPOOL - %s\n",
libzfs_error_description(zfshdl));
err = EXIT_FAIL;
goto out;
}
if (!create_zvol(zfshdl, vol)) {
err = EXIT_FAIL;
goto out;
}
if ((volhdl = zfs_open(zfshdl, vol, ZFS_TYPE_VOLUME)) == NULL) {
(void) printf("Could not open ZFS volume - %s\n",
libzfs_error_description(zfshdl));
err = EXIT_FAIL;
goto out;
}
if ((fd = open(backend, O_RDWR)) == -1) {
(void) printf("Failed to open '%s': %s\n",
backend, strerror(errno));
err = EXIT_FAIL;
goto out;
}
VERBOSE(("Opened backend %s", backend));
start_event_thread();
evp = mevent_add_flags(fd, EVF_VNODE, EVFF_ATTRIB, callback, cookie);
if (evp == NULL) {
(void) printf("mevent_add returned NULL\n");
err = EXIT_FAIL;
goto out;
}
for (uint_t i = 2; i < 4; i++) {
ssize_t written;
char buf[64];
/*
* Check that a write to the volume does not trigger an event.
*/
if (lseek(fd, 0, SEEK_SET) == -1)
FAIL_ERRNO("lseek");
written = write(fd, cookie, strlen(cookie));
if (written < 0)
FAIL_ERRNO("bad write");
ASSERT_INT64_EQ(("write cookie", i), written, strlen(cookie));
(void) snprintf(buf, sizeof (buf), "%llu", i * MB);
VERBOSE(("Setting volsize to %s", buf));
if (zfs_prop_set(volhdl,
zfs_prop_to_name(ZFS_PROP_VOLSIZE), buf) != 0) {
(void) printf("Failed to increase ZFS volume size\n");
pthread_mutex_unlock(&mtx);
err = EXIT_FAIL;
goto out;
}
/* Wait for the size change to be processed */
pthread_mutex_lock(&mtx);
pthread_cond_wait(&cv, &mtx);
pthread_mutex_unlock(&mtx);
}
(void) mevent_disable(evp);
err = EXIT_PASS;
out:
(void) close(fd);
destroy_zpool(zfshdl, poolhdl, volhdl);
(void) libzfs_fini(zfshdl);
(void) unlink(template);
if (err == EXIT_PASS)
PASS();
exit(err);
}
|