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
|
# SOME DESCRIPTIVE TITLE
# Copyright (C) YEAR Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-03-01 17:07+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#. type: TH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "SELECT_TUT"
msgstr ""
#. type: TH
#: archlinux fedora-40 fedora-rawhide mageia-cauldron
#, no-wrap
msgid "2023-10-31"
msgstr ""
#. type: TH
#: archlinux fedora-40 fedora-rawhide mageia-cauldron
#, no-wrap
msgid "Linux man-pages 6.06"
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "NAME"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid "select, pselect - synchronous I/O multiplexing"
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "LIBRARY"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid "Standard C library (I<libc>, I<-lc>)"
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "SYNOPSIS"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid "See B<select>(2)"
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "DESCRIPTION"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The B<select>() and B<pselect>() system calls are used to efficiently "
"monitor multiple file descriptors, to see if any of them is, or becomes, "
"\"ready\"; that is, to see whether I/O becomes possible, or an \"exceptional "
"condition\" has occurred on any of the file descriptors."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"This page provides background and tutorial information on the use of these "
"system calls. For details of the arguments and semantics of B<select>() "
"and B<pselect>(), see B<select>(2)."
msgstr ""
#. type: SS
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "Combining signal and data events"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"B<pselect>() is useful if you are waiting for a signal as well as for file "
"descriptor(s) to become ready for I/O. Programs that receive signals "
"normally use the signal handler only to raise a global flag. The global "
"flag will indicate that the event must be processed in the main loop of the "
"program. A signal will cause the B<select>() (or B<pselect>()) call to "
"return with I<errno> set to B<EINTR>. This behavior is essential so that "
"signals can be processed in the main loop of the program, otherwise "
"B<select>() would block indefinitely."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Now, somewhere in the main loop will be a conditional to check the global "
"flag. So we must ask: what if a signal arrives after the conditional, but "
"before the B<select>() call? The answer is that B<select>() would block "
"indefinitely, even though an event is actually pending. This race condition "
"is solved by the B<pselect>() call. This call can be used to set the "
"signal mask to a set of signals that are to be received only within the "
"B<pselect>() call. For instance, let us say that the event in question was "
"the exit of a child process. Before the start of the main loop, we would "
"block B<SIGCHLD> using B<sigprocmask>(2). Our B<pselect>() call would "
"enable B<SIGCHLD> by using an empty signal mask. Our program would look "
"like:"
msgstr ""
#. type: Plain text
#: archlinux debian-unstable fedora-40 fedora-rawhide mageia-cauldron
#: opensuse-tumbleweed
#, no-wrap
msgid ""
"static volatile sig_atomic_t got_SIGCHLD = 0;\n"
"\\&\n"
"static void\n"
"child_sig_handler(int sig)\n"
"{\n"
" got_SIGCHLD = 1;\n"
"}\n"
"\\&\n"
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" sigset_t sigmask, empty_mask;\n"
" struct sigaction sa;\n"
" fd_set readfds, writefds, exceptfds;\n"
" int r;\n"
"\\&\n"
" sigemptyset(&sigmask);\n"
" sigaddset(&sigmask, SIGCHLD);\n"
" if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
" perror(\"sigprocmask\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
"\\&\n"
" sa.sa_flags = 0;\n"
" sa.sa_handler = child_sig_handler;\n"
" sigemptyset(&sa.sa_mask);\n"
" if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
" perror(\"sigaction\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
"\\&\n"
" sigemptyset(&empty_mask);\n"
"\\&\n"
" for (;;) { /* main loop */\n"
" /* Initialize readfds, writefds, and exceptfds\n"
" before the pselect() call. (Code omitted.) */\n"
"\\&\n"
" r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
" NULL, &empty_mask);\n"
" if (r == -1 && errno != EINTR) {\n"
" /* Handle error */\n"
" }\n"
"\\&\n"
" if (got_SIGCHLD) {\n"
" got_SIGCHLD = 0;\n"
"\\&\n"
" /* Handle signalled event here; e.g., wait() for all\n"
" terminated children. (Code omitted.) */\n"
" }\n"
"\\&\n"
" /* main body of program */\n"
" }\n"
"}\n"
msgstr ""
#. type: SS
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "Practical"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"So what is the point of B<select>()? Can't I just read and write to my file "
"descriptors whenever I want? The point of B<select>() is that it watches "
"multiple descriptors at the same time and properly puts the process to sleep "
"if there is no activity. UNIX programmers often find themselves in a "
"position where they have to handle I/O from more than one file descriptor "
"where the data flow may be intermittent. If you were to merely create a "
"sequence of B<read>(2) and B<write>(2) calls, you would find that one of "
"your calls may block waiting for data from/to a file descriptor, while "
"another file descriptor is unused though ready for I/O. B<select>() "
"efficiently copes with this situation."
msgstr ""
#. type: SS
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "Select law"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Many people who try to use B<select>() come across behavior that is "
"difficult to understand and produces nonportable or borderline results. For "
"instance, the above program is carefully written not to block at any point, "
"even though it does not set its file descriptors to nonblocking mode. It is "
"easy to introduce subtle errors that will remove the advantage of using "
"B<select>(), so here is a list of essentials to watch for when using "
"B<select>()."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "1."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"You should always try to use B<select>() without a timeout. Your program "
"should have nothing to do if there is no data available. Code that depends "
"on timeouts is not usually portable and is difficult to debug."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "2."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The value I<nfds> must be properly calculated for efficiency as explained "
"above."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "3."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"No file descriptor must be added to any set if you do not intend to check "
"its result after the B<select>() call, and respond appropriately. See next "
"rule."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "4."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"After B<select>() returns, all file descriptors in all sets should be "
"checked to see if they are ready."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "5."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) do I<not> "
"necessarily read/write the full amount of data that you have requested. If "
"they do read/write the full amount, it's because you have a low traffic load "
"and a fast stream. This is not always going to be the case. You should "
"cope with the case of your functions managing to send or receive only a "
"single byte."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "6."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Never read/write only in single bytes at a time unless you are really sure "
"that you have a small amount of data to process. It is extremely "
"inefficient not to read/write as much data as you can buffer each time. The "
"buffers in the example below are 1024 bytes although they could easily be "
"made larger."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "7."
msgstr ""
#. Nonetheless, you should still cope with these errors for completeness.
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Calls to B<read>(2), B<recv>(2), B<write>(2), B<send>(2), and B<select>() "
"can fail with the error B<EINTR>, and calls to B<read>(2), B<recv>(2), "
"B<write>(2), and B<send>(2) can fail with I<errno> set to B<EAGAIN> "
"(B<EWOULDBLOCK>). These results must be properly managed (not done properly "
"above). If your program is not going to receive any signals, then it is "
"unlikely you will get B<EINTR>. If your program does not set nonblocking I/"
"O, you will not get B<EAGAIN>."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "8."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Never call B<read>(2), B<recv>(2), B<write>(2), or B<send>(2) with a buffer "
"length of zero."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "9."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"If the functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) fail "
"with errors other than those listed in B<7.>, or one of the input functions "
"returns 0, indicating end of file, then you should I<not> pass that file "
"descriptor to B<select>() again. In the example below, I close the file "
"descriptor immediately, and then set it to -1 to prevent it being included "
"in a set."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "10."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The timeout value must be initialized with each new call to B<select>(), "
"since some operating systems modify the structure. B<pselect>() however "
"does not modify its timeout structure."
msgstr ""
#. type: TP
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "11."
msgstr ""
#. "I have heard" does not fill me with confidence, and doesn't
#. belong in a man page, so I've commented this point out.
#. .TP
#. 11.
#. I have heard that the Windows socket layer does not cope with OOB data
#. properly.
#. It also does not cope with
#. .BR select ()
#. calls when no file descriptors are set at all.
#. Having no file descriptors set is a useful
#. way to sleep the process with subsecond precision by using the timeout.
#. (See further on.)
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Since B<select>() modifies its file descriptor sets, if the call is being "
"used in a loop, then the sets must be reinitialized before each call."
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "RETURN VALUE"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid "See B<select>(2)."
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "NOTES"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Generally speaking, all operating systems that support sockets also support "
"B<select>(). B<select>() can be used to solve many problems in a portable "
"and efficient way that naive programmers try to solve in a more complicated "
"manner using threads, forking, IPCs, signals, memory sharing, and so on."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The B<poll>(2) system call has the same functionality as B<select>(), and "
"is somewhat more efficient when monitoring sparse file descriptor sets. It "
"is nowadays widely available, but historically was less portable than "
"B<select>()."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The Linux-specific B<epoll>(7) API provides an interface that is more "
"efficient than B<select>(2) and B<poll>(2) when monitoring large numbers "
"of file descriptors."
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "EXAMPLES"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"Here is an example that better demonstrates the true utility of "
"B<select>(). The listing below is a TCP forwarding program that forwards "
"from one TCP port to another."
msgstr ""
#. type: Plain text
#: archlinux debian-unstable fedora-40 fedora-rawhide mageia-cauldron
#: opensuse-tumbleweed
#, no-wrap
msgid ""
"#include E<lt>arpa/inet.hE<gt>\n"
"#include E<lt>errno.hE<gt>\n"
"#include E<lt>netinet/in.hE<gt>\n"
"#include E<lt>signal.hE<gt>\n"
"#include E<lt>stdio.hE<gt>\n"
"#include E<lt>stdlib.hE<gt>\n"
"#include E<lt>string.hE<gt>\n"
"#include E<lt>sys/select.hE<gt>\n"
"#include E<lt>sys/socket.hE<gt>\n"
"#include E<lt>unistd.hE<gt>\n"
"\\&\n"
"static int forward_port;\n"
"\\&\n"
"#undef max\n"
"#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
"\\&\n"
"static int\n"
"listen_socket(int listen_port)\n"
"{\n"
" int lfd;\n"
" int yes;\n"
" struct sockaddr_in addr;\n"
"\\&\n"
" lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
" if (lfd == -1) {\n"
" perror(\"socket\");\n"
" return -1;\n"
" }\n"
"\\&\n"
" yes = 1;\n"
" if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
" &yes, sizeof(yes)) == -1)\n"
" {\n"
" perror(\"setsockopt\");\n"
" close(lfd);\n"
" return -1;\n"
" }\n"
"\\&\n"
" memset(&addr, 0, sizeof(addr));\n"
" addr.sin_port = htons(listen_port);\n"
" addr.sin_family = AF_INET;\n"
" if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
" perror(\"bind\");\n"
" close(lfd);\n"
" return -1;\n"
" }\n"
"\\&\n"
" printf(\"accepting connections on port %d\\en\", listen_port);\n"
" listen(lfd, 10);\n"
" return lfd;\n"
"}\n"
"\\&\n"
"static int\n"
"connect_socket(int connect_port, char *address)\n"
"{\n"
" int cfd;\n"
" struct sockaddr_in addr;\n"
"\\&\n"
" cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
" if (cfd == -1) {\n"
" perror(\"socket\");\n"
" return -1;\n"
" }\n"
"\\&\n"
" memset(&addr, 0, sizeof(addr));\n"
" addr.sin_port = htons(connect_port);\n"
" addr.sin_family = AF_INET;\n"
"\\&\n"
" if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
" fprintf(stderr, \"inet_aton(): bad IP address format\\en\");\n"
" close(cfd);\n"
" return -1;\n"
" }\n"
"\\&\n"
" if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
" perror(\"connect()\");\n"
" shutdown(cfd, SHUT_RDWR);\n"
" close(cfd);\n"
" return -1;\n"
" }\n"
" return cfd;\n"
"}\n"
"\\&\n"
"#define SHUT_FD1 do { \\e\n"
" if (fd1 E<gt>= 0) { \\e\n"
" shutdown(fd1, SHUT_RDWR); \\e\n"
" close(fd1); \\e\n"
" fd1 = -1; \\e\n"
" } \\e\n"
" } while (0)\n"
"\\&\n"
"#define SHUT_FD2 do { \\e\n"
" if (fd2 E<gt>= 0) { \\e\n"
" shutdown(fd2, SHUT_RDWR); \\e\n"
" close(fd2); \\e\n"
" fd2 = -1; \\e\n"
" } \\e\n"
" } while (0)\n"
"\\&\n"
"#define BUF_SIZE 1024\n"
"\\&\n"
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" int h;\n"
" int ready, nfds;\n"
" int fd1 = -1, fd2 = -1;\n"
" int buf1_avail = 0, buf1_written = 0;\n"
" int buf2_avail = 0, buf2_written = 0;\n"
" char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
" fd_set readfds, writefds, exceptfds;\n"
" ssize_t nbytes;\n"
"\\&\n"
" if (argc != 4) {\n"
" fprintf(stderr, \"Usage\\en\\etfwd E<lt>listen-portE<gt> \"\n"
" \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\en\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
"\\&\n"
" signal(SIGPIPE, SIG_IGN);\n"
"\\&\n"
" forward_port = atoi(argv[2]);\n"
"\\&\n"
" h = listen_socket(atoi(argv[1]));\n"
" if (h == -1)\n"
" exit(EXIT_FAILURE);\n"
"\\&\n"
" for (;;) {\n"
" nfds = 0;\n"
"\\&\n"
" FD_ZERO(&readfds);\n"
" FD_ZERO(&writefds);\n"
" FD_ZERO(&exceptfds);\n"
" FD_SET(h, &readfds);\n"
" nfds = max(nfds, h);\n"
"\\&\n"
" if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
" FD_SET(fd1, &readfds);\n"
" /* Note: nfds is updated below, when fd1 is added to\n"
" exceptfds. */\n"
" if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
" FD_SET(fd2, &readfds);\n"
"\\&\n"
" if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
" FD_SET(fd1, &writefds);\n"
" if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
" FD_SET(fd2, &writefds);\n"
"\\&\n"
" if (fd1 E<gt> 0) {\n"
" FD_SET(fd1, &exceptfds);\n"
" nfds = max(nfds, fd1);\n"
" }\n"
" if (fd2 E<gt> 0) {\n"
" FD_SET(fd2, &exceptfds);\n"
" nfds = max(nfds, fd2);\n"
" }\n"
"\\&\n"
" ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
"\\&\n"
" if (ready == -1 && errno == EINTR)\n"
" continue;\n"
"\\&\n"
" if (ready == -1) {\n"
" perror(\"select()\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
"\\&\n"
" if (FD_ISSET(h, &readfds)) {\n"
" socklen_t addrlen;\n"
" struct sockaddr_in client_addr;\n"
" int fd;\n"
"\\&\n"
" addrlen = sizeof(client_addr);\n"
" memset(&client_addr, 0, addrlen);\n"
" fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
" if (fd == -1) {\n"
" perror(\"accept()\");\n"
" } else {\n"
" SHUT_FD1;\n"
" SHUT_FD2;\n"
" buf1_avail = buf1_written = 0;\n"
" buf2_avail = buf2_written = 0;\n"
" fd1 = fd;\n"
" fd2 = connect_socket(forward_port, argv[3]);\n"
" if (fd2 == -1)\n"
" SHUT_FD1;\n"
" else\n"
" printf(\"connect from %s\\en\",\n"
" inet_ntoa(client_addr.sin_addr));\n"
"\\&\n"
" /* Skip any events on the old, closed file\n"
" descriptors. */\n"
"\\&\n"
" continue;\n"
" }\n"
" }\n"
"\\&\n"
" /* NB: read OOB data before normal reads. */\n"
"\\&\n"
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
" char c;\n"
"\\&\n"
" nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" send(fd2, &c, 1, MSG_OOB);\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
" char c;\n"
"\\&\n"
" nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" send(fd1, &c, 1, MSG_OOB);\n"
" }\n"
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
" nbytes = read(fd1, buf1 + buf1_avail,\n"
" BUF_SIZE - buf1_avail);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" buf1_avail += nbytes;\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
" nbytes = read(fd2, buf2 + buf2_avail,\n"
" BUF_SIZE - buf2_avail);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" buf2_avail += nbytes;\n"
" }\n"
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
" nbytes = write(fd1, buf2 + buf2_written,\n"
" buf2_avail - buf2_written);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" buf2_written += nbytes;\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
" nbytes = write(fd2, buf1 + buf1_written,\n"
" buf1_avail - buf1_written);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" buf1_written += nbytes;\n"
" }\n"
"\\&\n"
" /* Check if write data has caught read data. */\n"
"\\&\n"
" if (buf1_written == buf1_avail)\n"
" buf1_written = buf1_avail = 0;\n"
" if (buf2_written == buf2_avail)\n"
" buf2_written = buf2_avail = 0;\n"
"\\&\n"
" /* One side has closed the connection, keep\n"
" writing to the other side until empty. */\n"
"\\&\n"
" if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
" SHUT_FD2;\n"
" if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
" SHUT_FD1;\n"
" }\n"
" exit(EXIT_SUCCESS);\n"
"}\n"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The above program properly forwards most kinds of TCP connections including "
"OOB signal data transmitted by B<telnet> servers. It handles the tricky "
"problem of having data flow in both directions simultaneously. You might "
"think it more efficient to use a B<fork>(2) call and devote a thread to "
"each stream. This becomes more tricky than you might suspect. Another idea "
"is to set nonblocking I/O using B<fcntl>(2). This also has its problems "
"because you end up using inefficient timeouts."
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"The program does not handle more than one simultaneous connection at a time, "
"although it could easily be extended to do this with a linked list of "
"buffers\\[em]one for each connection. At the moment, new connections cause "
"the current connection to be dropped."
msgstr ""
#. type: SH
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
#, no-wrap
msgid "SEE ALSO"
msgstr ""
#. type: Plain text
#: archlinux debian-bookworm debian-unstable fedora-40 fedora-rawhide
#: mageia-cauldron opensuse-leap-15-6 opensuse-tumbleweed
msgid ""
"B<accept>(2), B<connect>(2), B<poll>(2), B<read>(2), B<recv>(2), "
"B<select>(2), B<send>(2), B<sigprocmask>(2), B<write>(2), B<epoll>(7)"
msgstr ""
#. type: TH
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid "2023-02-05"
msgstr ""
#. type: TH
#: debian-bookworm
#, no-wrap
msgid "Linux man-pages 6.03"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid "static volatile sig_atomic_t got_SIGCHLD = 0;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"static void\n"
"child_sig_handler(int sig)\n"
"{\n"
" got_SIGCHLD = 1;\n"
"}\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" sigset_t sigmask, empty_mask;\n"
" struct sigaction sa;\n"
" fd_set readfds, writefds, exceptfds;\n"
" int r;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" sigemptyset(&sigmask);\n"
" sigaddset(&sigmask, SIGCHLD);\n"
" if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n"
" perror(\"sigprocmask\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" sa.sa_flags = 0;\n"
" sa.sa_handler = child_sig_handler;\n"
" sigemptyset(&sa.sa_mask);\n"
" if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n"
" perror(\"sigaction\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " sigemptyset(&empty_mask);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" for (;;) { /* main loop */\n"
" /* Initialize readfds, writefds, and exceptfds\n"
" before the pselect() call. (Code omitted.) */\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" r = pselect(nfds, &readfds, &writefds, &exceptfds,\n"
" NULL, &empty_mask);\n"
" if (r == -1 && errno != EINTR) {\n"
" /* Handle error */\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (got_SIGCHLD) {\n"
" got_SIGCHLD = 0;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" /* Handle signalled event here; e.g., wait() for all\n"
" terminated children. (Code omitted.) */\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" /* main body of program */\n"
" }\n"
"}\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"#include E<lt>arpa/inet.hE<gt>\n"
"#include E<lt>errno.hE<gt>\n"
"#include E<lt>netinet/in.hE<gt>\n"
"#include E<lt>signal.hE<gt>\n"
"#include E<lt>stdio.hE<gt>\n"
"#include E<lt>stdlib.hE<gt>\n"
"#include E<lt>string.hE<gt>\n"
"#include E<lt>sys/select.hE<gt>\n"
"#include E<lt>sys/socket.hE<gt>\n"
"#include E<lt>unistd.hE<gt>\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid "static int forward_port;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"#undef max\n"
"#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"static int\n"
"listen_socket(int listen_port)\n"
"{\n"
" int lfd;\n"
" int yes;\n"
" struct sockaddr_in addr;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" lfd = socket(AF_INET, SOCK_STREAM, 0);\n"
" if (lfd == -1) {\n"
" perror(\"socket\");\n"
" return -1;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" yes = 1;\n"
" if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n"
" &yes, sizeof(yes)) == -1)\n"
" {\n"
" perror(\"setsockopt\");\n"
" close(lfd);\n"
" return -1;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" memset(&addr, 0, sizeof(addr));\n"
" addr.sin_port = htons(listen_port);\n"
" addr.sin_family = AF_INET;\n"
" if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
" perror(\"bind\");\n"
" close(lfd);\n"
" return -1;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" printf(\"accepting connections on port %d\\en\", listen_port);\n"
" listen(lfd, 10);\n"
" return lfd;\n"
"}\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"static int\n"
"connect_socket(int connect_port, char *address)\n"
"{\n"
" int cfd;\n"
" struct sockaddr_in addr;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" cfd = socket(AF_INET, SOCK_STREAM, 0);\n"
" if (cfd == -1) {\n"
" perror(\"socket\");\n"
" return -1;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" memset(&addr, 0, sizeof(addr));\n"
" addr.sin_port = htons(connect_port);\n"
" addr.sin_family = AF_INET;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n"
" fprintf(stderr, \"inet_aton(): bad IP address format\\en\");\n"
" close(cfd);\n"
" return -1;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n"
" perror(\"connect()\");\n"
" shutdown(cfd, SHUT_RDWR);\n"
" close(cfd);\n"
" return -1;\n"
" }\n"
" return cfd;\n"
"}\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"#define SHUT_FD1 do { \\e\n"
" if (fd1 E<gt>= 0) { \\e\n"
" shutdown(fd1, SHUT_RDWR); \\e\n"
" close(fd1); \\e\n"
" fd1 = -1; \\e\n"
" } \\e\n"
" } while (0)\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"#define SHUT_FD2 do { \\e\n"
" if (fd2 E<gt>= 0) { \\e\n"
" shutdown(fd2, SHUT_RDWR); \\e\n"
" close(fd2); \\e\n"
" fd2 = -1; \\e\n"
" } \\e\n"
" } while (0)\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid "#define BUF_SIZE 1024\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
"int\n"
"main(int argc, char *argv[])\n"
"{\n"
" int h;\n"
" int ready, nfds;\n"
" int fd1 = -1, fd2 = -1;\n"
" int buf1_avail = 0, buf1_written = 0;\n"
" int buf2_avail = 0, buf2_written = 0;\n"
" char buf1[BUF_SIZE], buf2[BUF_SIZE];\n"
" fd_set readfds, writefds, exceptfds;\n"
" ssize_t nbytes;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (argc != 4) {\n"
" fprintf(stderr, \"Usage\\en\\etfwd E<lt>listen-portE<gt> \"\n"
" \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\en\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " signal(SIGPIPE, SIG_IGN);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " forward_port = atoi(argv[2]);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" h = listen_socket(atoi(argv[1]));\n"
" if (h == -1)\n"
" exit(EXIT_FAILURE);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" for (;;) {\n"
" nfds = 0;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" FD_ZERO(&readfds);\n"
" FD_ZERO(&writefds);\n"
" FD_ZERO(&exceptfds);\n"
" FD_SET(h, &readfds);\n"
" nfds = max(nfds, h);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n"
" FD_SET(fd1, &readfds);\n"
" /* Note: nfds is updated below, when fd1 is added to\n"
" exceptfds. */\n"
" if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n"
" FD_SET(fd2, &readfds);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n"
" FD_SET(fd1, &writefds);\n"
" if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n"
" FD_SET(fd2, &writefds);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (fd1 E<gt> 0) {\n"
" FD_SET(fd1, &exceptfds);\n"
" nfds = max(nfds, fd1);\n"
" }\n"
" if (fd2 E<gt> 0) {\n"
" FD_SET(fd2, &exceptfds);\n"
" nfds = max(nfds, fd2);\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (ready == -1 && errno == EINTR)\n"
" continue;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (ready == -1) {\n"
" perror(\"select()\");\n"
" exit(EXIT_FAILURE);\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (FD_ISSET(h, &readfds)) {\n"
" socklen_t addrlen;\n"
" struct sockaddr_in client_addr;\n"
" int fd;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" addrlen = sizeof(client_addr);\n"
" memset(&client_addr, 0, addrlen);\n"
" fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n"
" if (fd == -1) {\n"
" perror(\"accept()\");\n"
" } else {\n"
" SHUT_FD1;\n"
" SHUT_FD2;\n"
" buf1_avail = buf1_written = 0;\n"
" buf2_avail = buf2_written = 0;\n"
" fd1 = fd;\n"
" fd2 = connect_socket(forward_port, argv[3]);\n"
" if (fd2 == -1)\n"
" SHUT_FD1;\n"
" else\n"
" printf(\"connect from %s\\en\",\n"
" inet_ntoa(client_addr.sin_addr));\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" /* Skip any events on the old, closed file\n"
" descriptors. */\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" continue;\n"
" }\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " /* NB: read OOB data before normal reads. */\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n"
" char c;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" nbytes = recv(fd1, &c, 1, MSG_OOB);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" send(fd2, &c, 1, MSG_OOB);\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n"
" char c;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" nbytes = recv(fd2, &c, 1, MSG_OOB);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" send(fd1, &c, 1, MSG_OOB);\n"
" }\n"
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n"
" nbytes = read(fd1, buf1 + buf1_avail,\n"
" BUF_SIZE - buf1_avail);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" buf1_avail += nbytes;\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n"
" nbytes = read(fd2, buf2 + buf2_avail,\n"
" BUF_SIZE - buf2_avail);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" buf2_avail += nbytes;\n"
" }\n"
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n"
" nbytes = write(fd1, buf2 + buf2_written,\n"
" buf2_avail - buf2_written);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD1;\n"
" else\n"
" buf2_written += nbytes;\n"
" }\n"
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n"
" nbytes = write(fd2, buf1 + buf1_written,\n"
" buf1_avail - buf1_written);\n"
" if (nbytes E<lt> 1)\n"
" SHUT_FD2;\n"
" else\n"
" buf1_written += nbytes;\n"
" }\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid " /* Check if write data has caught read data. */\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (buf1_written == buf1_avail)\n"
" buf1_written = buf1_avail = 0;\n"
" if (buf2_written == buf2_avail)\n"
" buf2_written = buf2_avail = 0;\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" /* One side has closed the connection, keep\n"
" writing to the other side until empty. */\n"
msgstr ""
#. type: Plain text
#: debian-bookworm opensuse-leap-15-6
#, no-wrap
msgid ""
" if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n"
" SHUT_FD2;\n"
" if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n"
" SHUT_FD1;\n"
" }\n"
" exit(EXIT_SUCCESS);\n"
"}\n"
msgstr ""
#. type: TH
#: debian-unstable opensuse-tumbleweed
#, no-wrap
msgid "2023-05-03"
msgstr ""
#. type: TH
#: debian-unstable opensuse-tumbleweed
#, no-wrap
msgid "Linux man-pages 6.05.01"
msgstr ""
#. type: TH
#: opensuse-leap-15-6
#, no-wrap
msgid "Linux man-pages 6.04"
msgstr ""
|