| 1 |
# SOME DESCRIPTIVE TITLE |
| 2 |
# Copyright (C) YEAR Free Software Foundation, Inc. |
| 3 |
# This file is distributed under the same license as the PACKAGE package. |
| 4 |
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 5 |
# |
| 6 |
#, fuzzy |
| 7 |
msgid "" |
| 8 |
msgstr "" |
| 9 |
"Project-Id-Version: PACKAGE VERSION\n" |
| 10 |
"POT-Creation-Date: 2026-06-05 06:42+0200\n" |
| 11 |
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
| 12 |
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
| 13 |
"Language-Team: LANGUAGE <[email protected]>\n" |
| 14 |
"Language: \n" |
| 15 |
"MIME-Version: 1.0\n" |
| 16 |
"Content-Type: text/plain; charset=UTF-8\n" |
| 17 |
"Content-Transfer-Encoding: 8bit\n" |
| 18 |
|
| 19 |
#. type: TH |
| 20 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 21 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 22 |
#, no-wrap |
| 23 |
msgid "SELECT_TUT" |
| 24 |
msgstr "" |
| 25 |
|
| 26 |
#. type: TH |
| 27 |
#: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 |
| 28 |
#: opensuse-tumbleweed |
| 29 |
#, no-wrap |
| 30 |
msgid "2026-02-08" |
| 31 |
msgstr "" |
| 32 |
|
| 33 |
#. type: TH |
| 34 |
#: archlinux debian-unstable |
| 35 |
#, no-wrap |
| 36 |
msgid "Linux man-pages 6.18" |
| 37 |
msgstr "" |
| 38 |
|
| 39 |
#. type: SH |
| 40 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 41 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 42 |
#, no-wrap |
| 43 |
msgid "NAME" |
| 44 |
msgstr "" |
| 45 |
|
| 46 |
#. type: Plain text |
| 47 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 48 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 49 |
msgid "select, pselect - synchronous I/O multiplexing" |
| 50 |
msgstr "" |
| 51 |
|
| 52 |
#. type: SH |
| 53 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 54 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 55 |
#, no-wrap |
| 56 |
msgid "LIBRARY" |
| 57 |
msgstr "" |
| 58 |
|
| 59 |
#. type: Plain text |
| 60 |
#: archlinux debian-unstable fedora-44 fedora-rawhide mageia-cauldron |
| 61 |
#: opensuse-leap-16-1 opensuse-tumbleweed |
| 62 |
msgid "Standard C library (I<libc>,\\ I<-lc>)" |
| 63 |
msgstr "" |
| 64 |
|
| 65 |
#. type: SH |
| 66 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 67 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 68 |
#, no-wrap |
| 69 |
msgid "SYNOPSIS" |
| 70 |
msgstr "" |
| 71 |
|
| 72 |
#. type: Plain text |
| 73 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 74 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 75 |
msgid "See B<select>(2)" |
| 76 |
msgstr "" |
| 77 |
|
| 78 |
#. type: SH |
| 79 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 80 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 81 |
#, no-wrap |
| 82 |
msgid "DESCRIPTION" |
| 83 |
msgstr "" |
| 84 |
|
| 85 |
#. type: Plain text |
| 86 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 87 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 88 |
msgid "" |
| 89 |
"The B<select>() and B<pselect>() system calls are used to efficiently " |
| 90 |
"monitor multiple file descriptors, to see if any of them is, or becomes, " |
| 91 |
"\"ready\"; that is, to see whether I/O becomes possible, or an \"exceptional " |
| 92 |
"condition\" has occurred on any of the file descriptors." |
| 93 |
msgstr "" |
| 94 |
|
| 95 |
#. type: Plain text |
| 96 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 97 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 98 |
msgid "" |
| 99 |
"This page provides background and tutorial information on the use of these " |
| 100 |
"system calls. For details of the arguments and semantics of B<select>() " |
| 101 |
"and B<pselect>(), see B<select>(2)." |
| 102 |
msgstr "" |
| 103 |
|
| 104 |
#. type: SS |
| 105 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 106 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 107 |
#, no-wrap |
| 108 |
msgid "Combining signal and data events" |
| 109 |
msgstr "" |
| 110 |
|
| 111 |
#. type: Plain text |
| 112 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 113 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 114 |
msgid "" |
| 115 |
"B<pselect>() is useful if you are waiting for a signal as well as for file " |
| 116 |
"descriptor(s) to become ready for I/O. Programs that receive signals " |
| 117 |
"normally use the signal handler only to raise a global flag. The global " |
| 118 |
"flag will indicate that the event must be processed in the main loop of the " |
| 119 |
"program. A signal will cause the B<select>() (or B<pselect>()) call to " |
| 120 |
"return with I<errno> set to B<EINTR>. This behavior is essential so that " |
| 121 |
"signals can be processed in the main loop of the program, otherwise " |
| 122 |
"B<select>() would block indefinitely." |
| 123 |
msgstr "" |
| 124 |
|
| 125 |
#. type: Plain text |
| 126 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 127 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 128 |
msgid "" |
| 129 |
"Now, somewhere in the main loop will be a conditional to check the global " |
| 130 |
"flag. So we must ask: what if a signal arrives after the conditional, but " |
| 131 |
"before the B<select>() call? The answer is that B<select>() would block " |
| 132 |
"indefinitely, even though an event is actually pending. This race condition " |
| 133 |
"is solved by the B<pselect>() call. This call can be used to set the " |
| 134 |
"signal mask to a set of signals that are to be received only within the " |
| 135 |
"B<pselect>() call. For instance, let us say that the event in question was " |
| 136 |
"the exit of a child process. Before the start of the main loop, we would " |
| 137 |
"block B<SIGCHLD> using B<sigprocmask>(2). Our B<pselect>() call would " |
| 138 |
"enable B<SIGCHLD> by using an empty signal mask. Our program would look " |
| 139 |
"like:" |
| 140 |
msgstr "" |
| 141 |
|
| 142 |
#. type: Plain text |
| 143 |
#: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 |
| 144 |
#: opensuse-tumbleweed |
| 145 |
#, no-wrap |
| 146 |
msgid "" |
| 147 |
"static volatile sig_atomic_t got_SIGCHLD = 0;\n" |
| 148 |
"\\&\n" |
| 149 |
"static void\n" |
| 150 |
"child_sig_handler(int sig)\n" |
| 151 |
"{\n" |
| 152 |
" got_SIGCHLD = 1;\n" |
| 153 |
"}\n" |
| 154 |
"\\&\n" |
| 155 |
"int\n" |
| 156 |
"main(int argc, char *argv[])\n" |
| 157 |
"{\n" |
| 158 |
" sigset_t sigmask, empty_mask;\n" |
| 159 |
" struct sigaction sa;\n" |
| 160 |
" fd_set readfds, writefds, exceptfds;\n" |
| 161 |
" int r;\n" |
| 162 |
"\\&\n" |
| 163 |
" sigemptyset(&sigmask);\n" |
| 164 |
" sigaddset(&sigmask, SIGCHLD);\n" |
| 165 |
" if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" |
| 166 |
" perror(\"sigprocmask\");\n" |
| 167 |
" exit(EXIT_FAILURE);\n" |
| 168 |
" }\n" |
| 169 |
"\\&\n" |
| 170 |
" sa.sa_flags = 0;\n" |
| 171 |
" sa.sa_handler = child_sig_handler;\n" |
| 172 |
" sigemptyset(&sa.sa_mask);\n" |
| 173 |
" if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" |
| 174 |
" perror(\"sigaction\");\n" |
| 175 |
" exit(EXIT_FAILURE);\n" |
| 176 |
" }\n" |
| 177 |
"\\&\n" |
| 178 |
" sigemptyset(&empty_mask);\n" |
| 179 |
"\\&\n" |
| 180 |
" for (;;) { /* main loop */\n" |
| 181 |
" /* Initialize readfds, writefds, and exceptfds\n" |
| 182 |
" before the pselect() call. (Code omitted.) */\n" |
| 183 |
"\\&\n" |
| 184 |
" r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" |
| 185 |
" NULL, &empty_mask);\n" |
| 186 |
" if (r == -1 && errno != EINTR) {\n" |
| 187 |
" /* Handle error */\n" |
| 188 |
" }\n" |
| 189 |
"\\&\n" |
| 190 |
" if (got_SIGCHLD) {\n" |
| 191 |
" got_SIGCHLD = 0;\n" |
| 192 |
"\\&\n" |
| 193 |
" /* Handle signalled event here; e.g., wait() for all\n" |
| 194 |
" terminated children. (Code omitted.) */\n" |
| 195 |
" }\n" |
| 196 |
"\\&\n" |
| 197 |
" /* main body of program */\n" |
| 198 |
" }\n" |
| 199 |
"}\n" |
| 200 |
msgstr "" |
| 201 |
|
| 202 |
#. type: SS |
| 203 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 204 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 205 |
#, no-wrap |
| 206 |
msgid "Practical" |
| 207 |
msgstr "" |
| 208 |
|
| 209 |
#. type: Plain text |
| 210 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 211 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 212 |
msgid "" |
| 213 |
"So what is the point of B<select>()? Can't I just read and write to my file " |
| 214 |
"descriptors whenever I want? The point of B<select>() is that it watches " |
| 215 |
"multiple descriptors at the same time and properly puts the process to sleep " |
| 216 |
"if there is no activity. UNIX programmers often find themselves in a " |
| 217 |
"position where they have to handle I/O from more than one file descriptor " |
| 218 |
"where the data flow may be intermittent. If you were to merely create a " |
| 219 |
"sequence of B<read>(2) and B<write>(2) calls, you would find that one of " |
| 220 |
"your calls may block waiting for data from/to a file descriptor, while " |
| 221 |
"another file descriptor is unused though ready for I/O. B<select>() " |
| 222 |
"efficiently copes with this situation." |
| 223 |
msgstr "" |
| 224 |
|
| 225 |
#. type: SS |
| 226 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 227 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 228 |
#, no-wrap |
| 229 |
msgid "Select law" |
| 230 |
msgstr "" |
| 231 |
|
| 232 |
#. type: Plain text |
| 233 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 234 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 235 |
msgid "" |
| 236 |
"Many people who try to use B<select>() come across behavior that is " |
| 237 |
"difficult to understand and produces nonportable or borderline results. For " |
| 238 |
"instance, the above program is carefully written not to block at any point, " |
| 239 |
"even though it does not set its file descriptors to nonblocking mode. It is " |
| 240 |
"easy to introduce subtle errors that will remove the advantage of using " |
| 241 |
"B<select>(), so here is a list of essentials to watch for when using " |
| 242 |
"B<select>()." |
| 243 |
msgstr "" |
| 244 |
|
| 245 |
#. type: TP |
| 246 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 247 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 248 |
#, no-wrap |
| 249 |
msgid "1." |
| 250 |
msgstr "" |
| 251 |
|
| 252 |
#. type: Plain text |
| 253 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 254 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 255 |
msgid "" |
| 256 |
"You should always try to use B<select>() without a timeout. Your program " |
| 257 |
"should have nothing to do if there is no data available. Code that depends " |
| 258 |
"on timeouts is not usually portable and is difficult to debug." |
| 259 |
msgstr "" |
| 260 |
|
| 261 |
#. type: TP |
| 262 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 263 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 264 |
#, no-wrap |
| 265 |
msgid "2." |
| 266 |
msgstr "" |
| 267 |
|
| 268 |
#. type: Plain text |
| 269 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 270 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 271 |
msgid "" |
| 272 |
"The value I<nfds> must be properly calculated for efficiency as explained " |
| 273 |
"above." |
| 274 |
msgstr "" |
| 275 |
|
| 276 |
#. type: TP |
| 277 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 278 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 279 |
#, no-wrap |
| 280 |
msgid "3." |
| 281 |
msgstr "" |
| 282 |
|
| 283 |
#. type: Plain text |
| 284 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 285 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 286 |
msgid "" |
| 287 |
"No file descriptor must be added to any set if you do not intend to check " |
| 288 |
"its result after the B<select>() call, and respond appropriately. See next " |
| 289 |
"rule." |
| 290 |
msgstr "" |
| 291 |
|
| 292 |
#. type: TP |
| 293 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 294 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 295 |
#, no-wrap |
| 296 |
msgid "4." |
| 297 |
msgstr "" |
| 298 |
|
| 299 |
#. type: Plain text |
| 300 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 301 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 302 |
msgid "" |
| 303 |
"After B<select>() returns, all file descriptors in all sets should be " |
| 304 |
"checked to see if they are ready." |
| 305 |
msgstr "" |
| 306 |
|
| 307 |
#. type: TP |
| 308 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 309 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 310 |
#, no-wrap |
| 311 |
msgid "5." |
| 312 |
msgstr "" |
| 313 |
|
| 314 |
#. type: Plain text |
| 315 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 316 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 317 |
msgid "" |
| 318 |
"The functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) do I<not> " |
| 319 |
"necessarily read/write the full amount of data that you have requested. If " |
| 320 |
"they do read/write the full amount, it's because you have a low traffic load " |
| 321 |
"and a fast stream. This is not always going to be the case. You should " |
| 322 |
"cope with the case of your functions managing to send or receive only a " |
| 323 |
"single byte." |
| 324 |
msgstr "" |
| 325 |
|
| 326 |
#. type: TP |
| 327 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 328 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 329 |
#, no-wrap |
| 330 |
msgid "6." |
| 331 |
msgstr "" |
| 332 |
|
| 333 |
#. type: Plain text |
| 334 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 335 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 336 |
msgid "" |
| 337 |
"Never read/write only in single bytes at a time unless you are really sure " |
| 338 |
"that you have a small amount of data to process. It is extremely " |
| 339 |
"inefficient not to read/write as much data as you can buffer each time. The " |
| 340 |
"buffers in the example below are 1024 bytes although they could easily be " |
| 341 |
"made larger." |
| 342 |
msgstr "" |
| 343 |
|
| 344 |
#. type: TP |
| 345 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 346 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 347 |
#, no-wrap |
| 348 |
msgid "7." |
| 349 |
msgstr "" |
| 350 |
|
| 351 |
#. Nonetheless, you should still cope with these errors for completeness. |
| 352 |
#. type: Plain text |
| 353 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 354 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 355 |
msgid "" |
| 356 |
"Calls to B<read>(2), B<recv>(2), B<write>(2), B<send>(2), and B<select>() " |
| 357 |
"can fail with the error B<EINTR>, and calls to B<read>(2), B<recv>(2), " |
| 358 |
"B<write>(2), and B<send>(2) can fail with I<errno> set to B<EAGAIN> " |
| 359 |
"(B<EWOULDBLOCK>). These results must be properly managed (not done properly " |
| 360 |
"above). If your program is not going to receive any signals, then it is " |
| 361 |
"unlikely you will get B<EINTR>. If your program does not set nonblocking I/" |
| 362 |
"O, you will not get B<EAGAIN>." |
| 363 |
msgstr "" |
| 364 |
|
| 365 |
#. type: TP |
| 366 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 367 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 368 |
#, no-wrap |
| 369 |
msgid "8." |
| 370 |
msgstr "" |
| 371 |
|
| 372 |
#. type: Plain text |
| 373 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 374 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 375 |
msgid "" |
| 376 |
"Never call B<read>(2), B<recv>(2), B<write>(2), or B<send>(2) with a buffer " |
| 377 |
"length of zero." |
| 378 |
msgstr "" |
| 379 |
|
| 380 |
#. type: TP |
| 381 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 382 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 383 |
#, no-wrap |
| 384 |
msgid "9." |
| 385 |
msgstr "" |
| 386 |
|
| 387 |
#. type: Plain text |
| 388 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 389 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 390 |
msgid "" |
| 391 |
"If the functions B<read>(2), B<recv>(2), B<write>(2), and B<send>(2) fail " |
| 392 |
"with errors other than those listed in B<7.>, or one of the input functions " |
| 393 |
"returns 0, indicating end of file, then you should I<not> pass that file " |
| 394 |
"descriptor to B<select>() again. In the example below, I close the file " |
| 395 |
"descriptor immediately, and then set it to -1 to prevent it being included " |
| 396 |
"in a set." |
| 397 |
msgstr "" |
| 398 |
|
| 399 |
#. type: TP |
| 400 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 401 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 402 |
#, no-wrap |
| 403 |
msgid "10." |
| 404 |
msgstr "" |
| 405 |
|
| 406 |
#. type: Plain text |
| 407 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 408 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 409 |
msgid "" |
| 410 |
"The timeout value must be initialized with each new call to B<select>(), " |
| 411 |
"since some operating systems modify the structure. B<pselect>() however " |
| 412 |
"does not modify its timeout structure." |
| 413 |
msgstr "" |
| 414 |
|
| 415 |
#. type: TP |
| 416 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 417 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 418 |
#, no-wrap |
| 419 |
msgid "11." |
| 420 |
msgstr "" |
| 421 |
|
| 422 |
#. "I have heard" does not fill me with confidence, and doesn't |
| 423 |
#. belong in a man page, so I've commented this point out. |
| 424 |
#. .TP |
| 425 |
#. 11. |
| 426 |
#. I have heard that the Windows socket layer does not cope with OOB data |
| 427 |
#. properly. |
| 428 |
#. It also does not cope with |
| 429 |
#. .BR select () |
| 430 |
#. calls when no file descriptors are set at all. |
| 431 |
#. Having no file descriptors set is a useful |
| 432 |
#. way to sleep the process with subsecond precision by using the timeout. |
| 433 |
#. (See further on.) |
| 434 |
#. type: Plain text |
| 435 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 436 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 437 |
msgid "" |
| 438 |
"Since B<select>() modifies its file descriptor sets, if the call is being " |
| 439 |
"used in a loop, then the sets must be reinitialized before each call." |
| 440 |
msgstr "" |
| 441 |
|
| 442 |
#. type: SH |
| 443 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 444 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 445 |
#, no-wrap |
| 446 |
msgid "RETURN VALUE" |
| 447 |
msgstr "" |
| 448 |
|
| 449 |
#. type: Plain text |
| 450 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 451 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 452 |
msgid "See B<select>(2)." |
| 453 |
msgstr "" |
| 454 |
|
| 455 |
#. type: SH |
| 456 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 457 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 458 |
#, no-wrap |
| 459 |
msgid "NOTES" |
| 460 |
msgstr "" |
| 461 |
|
| 462 |
#. type: Plain text |
| 463 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 464 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 465 |
msgid "" |
| 466 |
"Generally speaking, all operating systems that support sockets also support " |
| 467 |
"B<select>(). B<select>() can be used to solve many problems in a portable " |
| 468 |
"and efficient way that naive programmers try to solve in a more complicated " |
| 469 |
"manner using threads, forking, IPCs, signals, memory sharing, and so on." |
| 470 |
msgstr "" |
| 471 |
|
| 472 |
#. type: Plain text |
| 473 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 474 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 475 |
msgid "" |
| 476 |
"The B<poll>(2) system call has the same functionality as B<select>(), and " |
| 477 |
"is somewhat more efficient when monitoring sparse file descriptor sets. It " |
| 478 |
"is nowadays widely available, but historically was less portable than " |
| 479 |
"B<select>()." |
| 480 |
msgstr "" |
| 481 |
|
| 482 |
#. type: Plain text |
| 483 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 484 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 485 |
msgid "" |
| 486 |
"The Linux-specific B<epoll>(7) API provides an interface that is more " |
| 487 |
"efficient than B<select>(2) and B<poll>(2) when monitoring large numbers " |
| 488 |
"of file descriptors." |
| 489 |
msgstr "" |
| 490 |
|
| 491 |
#. type: SH |
| 492 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 493 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 494 |
#, no-wrap |
| 495 |
msgid "EXAMPLES" |
| 496 |
msgstr "" |
| 497 |
|
| 498 |
#. type: Plain text |
| 499 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 500 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 501 |
msgid "" |
| 502 |
"Here is an example that better demonstrates the true utility of " |
| 503 |
"B<select>(). The listing below is a TCP forwarding program that forwards " |
| 504 |
"from one TCP port to another." |
| 505 |
msgstr "" |
| 506 |
|
| 507 |
#. type: Plain text |
| 508 |
#: archlinux debian-unstable mageia-cauldron opensuse-leap-16-1 |
| 509 |
#: opensuse-tumbleweed |
| 510 |
#, no-wrap |
| 511 |
msgid "" |
| 512 |
"#include E<lt>arpa/inet.hE<gt>\n" |
| 513 |
"#include E<lt>errno.hE<gt>\n" |
| 514 |
"#include E<lt>netinet/in.hE<gt>\n" |
| 515 |
"#include E<lt>signal.hE<gt>\n" |
| 516 |
"#include E<lt>stdio.hE<gt>\n" |
| 517 |
"#include E<lt>stdlib.hE<gt>\n" |
| 518 |
"#include E<lt>string.hE<gt>\n" |
| 519 |
"#include E<lt>sys/select.hE<gt>\n" |
| 520 |
"#include E<lt>sys/socket.hE<gt>\n" |
| 521 |
"#include E<lt>sys/types.hE<gt>\n" |
| 522 |
"#include E<lt>unistd.hE<gt>\n" |
| 523 |
"\\&\n" |
| 524 |
"static int forward_port;\n" |
| 525 |
"\\&\n" |
| 526 |
"#undef max\n" |
| 527 |
"#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n" |
| 528 |
"\\&\n" |
| 529 |
"static int\n" |
| 530 |
"listen_socket(int listen_port)\n" |
| 531 |
"{\n" |
| 532 |
" int lfd;\n" |
| 533 |
" int yes;\n" |
| 534 |
" struct sockaddr_in addr;\n" |
| 535 |
"\\&\n" |
| 536 |
" lfd = socket(AF_INET, SOCK_STREAM, 0);\n" |
| 537 |
" if (lfd == -1) {\n" |
| 538 |
" perror(\"socket\");\n" |
| 539 |
" return -1;\n" |
| 540 |
" }\n" |
| 541 |
"\\&\n" |
| 542 |
" yes = 1;\n" |
| 543 |
" if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" |
| 544 |
" &yes, sizeof(yes)) == -1)\n" |
| 545 |
" {\n" |
| 546 |
" perror(\"setsockopt\");\n" |
| 547 |
" close(lfd);\n" |
| 548 |
" return -1;\n" |
| 549 |
" }\n" |
| 550 |
"\\&\n" |
| 551 |
" memset(&addr, 0, sizeof(addr));\n" |
| 552 |
" addr.sin_port = htons(listen_port);\n" |
| 553 |
" addr.sin_family = AF_INET;\n" |
| 554 |
" if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" |
| 555 |
" perror(\"bind\");\n" |
| 556 |
" close(lfd);\n" |
| 557 |
" return -1;\n" |
| 558 |
" }\n" |
| 559 |
"\\&\n" |
| 560 |
" printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" |
| 561 |
" listen(lfd, 10);\n" |
| 562 |
" return lfd;\n" |
| 563 |
"}\n" |
| 564 |
"\\&\n" |
| 565 |
"static int\n" |
| 566 |
"connect_socket(int connect_port, char *address)\n" |
| 567 |
"{\n" |
| 568 |
" int cfd;\n" |
| 569 |
" struct sockaddr_in addr;\n" |
| 570 |
"\\&\n" |
| 571 |
" cfd = socket(AF_INET, SOCK_STREAM, 0);\n" |
| 572 |
" if (cfd == -1) {\n" |
| 573 |
" perror(\"socket\");\n" |
| 574 |
" return -1;\n" |
| 575 |
" }\n" |
| 576 |
"\\&\n" |
| 577 |
" memset(&addr, 0, sizeof(addr));\n" |
| 578 |
" addr.sin_port = htons(connect_port);\n" |
| 579 |
" addr.sin_family = AF_INET;\n" |
| 580 |
"\\&\n" |
| 581 |
" if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" |
| 582 |
" fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" |
| 583 |
" close(cfd);\n" |
| 584 |
" return -1;\n" |
| 585 |
" }\n" |
| 586 |
"\\&\n" |
| 587 |
" if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" |
| 588 |
" perror(\"connect()\");\n" |
| 589 |
" shutdown(cfd, SHUT_RDWR);\n" |
| 590 |
" close(cfd);\n" |
| 591 |
" return -1;\n" |
| 592 |
" }\n" |
| 593 |
" return cfd;\n" |
| 594 |
"}\n" |
| 595 |
"\\&\n" |
| 596 |
"#define SHUT_FD1 do { \\[rs]\n" |
| 597 |
" if (fd1 E<gt>= 0) { \\[rs]\n" |
| 598 |
" shutdown(fd1, SHUT_RDWR); \\[rs]\n" |
| 599 |
" close(fd1); \\[rs]\n" |
| 600 |
" fd1 = -1; \\[rs]\n" |
| 601 |
" } \\[rs]\n" |
| 602 |
" } while (0)\n" |
| 603 |
"\\&\n" |
| 604 |
"#define SHUT_FD2 do { \\[rs]\n" |
| 605 |
" if (fd2 E<gt>= 0) { \\[rs]\n" |
| 606 |
" shutdown(fd2, SHUT_RDWR); \\[rs]\n" |
| 607 |
" close(fd2); \\[rs]\n" |
| 608 |
" fd2 = -1; \\[rs]\n" |
| 609 |
" } \\[rs]\n" |
| 610 |
" } while (0)\n" |
| 611 |
"\\&\n" |
| 612 |
"#define BUF_SIZE 1024\n" |
| 613 |
"\\&\n" |
| 614 |
"int\n" |
| 615 |
"main(int argc, char *argv[])\n" |
| 616 |
"{\n" |
| 617 |
" int h;\n" |
| 618 |
" int ready, nfds;\n" |
| 619 |
" int fd1 = -1, fd2 = -1;\n" |
| 620 |
" int buf1_avail = 0, buf1_written = 0;\n" |
| 621 |
" int buf2_avail = 0, buf2_written = 0;\n" |
| 622 |
" char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" |
| 623 |
" fd_set readfds, writefds, exceptfds;\n" |
| 624 |
" ssize_t nbytes;\n" |
| 625 |
"\\&\n" |
| 626 |
" if (argc != 4) {\n" |
| 627 |
" fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n" |
| 628 |
" \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n" |
| 629 |
" exit(EXIT_FAILURE);\n" |
| 630 |
" }\n" |
| 631 |
"\\&\n" |
| 632 |
" signal(SIGPIPE, SIG_IGN);\n" |
| 633 |
"\\&\n" |
| 634 |
" forward_port = atoi(argv[2]);\n" |
| 635 |
"\\&\n" |
| 636 |
" h = listen_socket(atoi(argv[1]));\n" |
| 637 |
" if (h == -1)\n" |
| 638 |
" exit(EXIT_FAILURE);\n" |
| 639 |
"\\&\n" |
| 640 |
" for (;;) {\n" |
| 641 |
" nfds = 0;\n" |
| 642 |
"\\&\n" |
| 643 |
" FD_ZERO(&readfds);\n" |
| 644 |
" FD_ZERO(&writefds);\n" |
| 645 |
" FD_ZERO(&exceptfds);\n" |
| 646 |
" FD_SET(h, &readfds);\n" |
| 647 |
" nfds = max(nfds, h);\n" |
| 648 |
"\\&\n" |
| 649 |
" if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n" |
| 650 |
" FD_SET(fd1, &readfds);\n" |
| 651 |
" /* Note: nfds is updated below, when fd1 is added to\n" |
| 652 |
" exceptfds. */\n" |
| 653 |
" if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n" |
| 654 |
" FD_SET(fd2, &readfds);\n" |
| 655 |
"\\&\n" |
| 656 |
" if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n" |
| 657 |
" FD_SET(fd1, &writefds);\n" |
| 658 |
" if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n" |
| 659 |
" FD_SET(fd2, &writefds);\n" |
| 660 |
"\\&\n" |
| 661 |
" if (fd1 E<gt> 0) {\n" |
| 662 |
" FD_SET(fd1, &exceptfds);\n" |
| 663 |
" nfds = max(nfds, fd1);\n" |
| 664 |
" }\n" |
| 665 |
" if (fd2 E<gt> 0) {\n" |
| 666 |
" FD_SET(fd2, &exceptfds);\n" |
| 667 |
" nfds = max(nfds, fd2);\n" |
| 668 |
" }\n" |
| 669 |
"\\&\n" |
| 670 |
" ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" |
| 671 |
"\\&\n" |
| 672 |
" if (ready == -1 && errno == EINTR)\n" |
| 673 |
" continue;\n" |
| 674 |
"\\&\n" |
| 675 |
" if (ready == -1) {\n" |
| 676 |
" perror(\"select()\");\n" |
| 677 |
" exit(EXIT_FAILURE);\n" |
| 678 |
" }\n" |
| 679 |
"\\&\n" |
| 680 |
" if (FD_ISSET(h, &readfds)) {\n" |
| 681 |
" socklen_t addrlen;\n" |
| 682 |
" struct sockaddr_in client_addr;\n" |
| 683 |
" int fd;\n" |
| 684 |
"\\&\n" |
| 685 |
" addrlen = sizeof(client_addr);\n" |
| 686 |
" memset(&client_addr, 0, addrlen);\n" |
| 687 |
" fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" |
| 688 |
" if (fd == -1) {\n" |
| 689 |
" perror(\"accept()\");\n" |
| 690 |
" } else {\n" |
| 691 |
" SHUT_FD1;\n" |
| 692 |
" SHUT_FD2;\n" |
| 693 |
" buf1_avail = buf1_written = 0;\n" |
| 694 |
" buf2_avail = buf2_written = 0;\n" |
| 695 |
" fd1 = fd;\n" |
| 696 |
" fd2 = connect_socket(forward_port, argv[3]);\n" |
| 697 |
" if (fd2 == -1)\n" |
| 698 |
" SHUT_FD1;\n" |
| 699 |
" else\n" |
| 700 |
" printf(\"connect from %s\\[rs]n\",\n" |
| 701 |
" inet_ntoa(client_addr.sin_addr));\n" |
| 702 |
"\\&\n" |
| 703 |
" /* Skip any events on the old, closed file\n" |
| 704 |
" descriptors. */\n" |
| 705 |
"\\&\n" |
| 706 |
" continue;\n" |
| 707 |
" }\n" |
| 708 |
" }\n" |
| 709 |
"\\&\n" |
| 710 |
" /* NB: read OOB data before normal reads. */\n" |
| 711 |
"\\&\n" |
| 712 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n" |
| 713 |
" char c;\n" |
| 714 |
"\\&\n" |
| 715 |
" nbytes = recv(fd1, &c, 1, MSG_OOB);\n" |
| 716 |
" if (nbytes E<lt> 1)\n" |
| 717 |
" SHUT_FD1;\n" |
| 718 |
" else\n" |
| 719 |
" send(fd2, &c, 1, MSG_OOB);\n" |
| 720 |
" }\n" |
| 721 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n" |
| 722 |
" char c;\n" |
| 723 |
"\\&\n" |
| 724 |
" nbytes = recv(fd2, &c, 1, MSG_OOB);\n" |
| 725 |
" if (nbytes E<lt> 1)\n" |
| 726 |
" SHUT_FD2;\n" |
| 727 |
" else\n" |
| 728 |
" send(fd1, &c, 1, MSG_OOB);\n" |
| 729 |
" }\n" |
| 730 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n" |
| 731 |
" nbytes = read(fd1, buf1 + buf1_avail,\n" |
| 732 |
" BUF_SIZE - buf1_avail);\n" |
| 733 |
" if (nbytes E<lt> 1)\n" |
| 734 |
" SHUT_FD1;\n" |
| 735 |
" else\n" |
| 736 |
" buf1_avail += nbytes;\n" |
| 737 |
" }\n" |
| 738 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n" |
| 739 |
" nbytes = read(fd2, buf2 + buf2_avail,\n" |
| 740 |
" BUF_SIZE - buf2_avail);\n" |
| 741 |
" if (nbytes E<lt> 1)\n" |
| 742 |
" SHUT_FD2;\n" |
| 743 |
" else\n" |
| 744 |
" buf2_avail += nbytes;\n" |
| 745 |
" }\n" |
| 746 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n" |
| 747 |
" nbytes = write(fd1, buf2 + buf2_written,\n" |
| 748 |
" buf2_avail - buf2_written);\n" |
| 749 |
" if (nbytes E<lt> 1)\n" |
| 750 |
" SHUT_FD1;\n" |
| 751 |
" else\n" |
| 752 |
" buf2_written += nbytes;\n" |
| 753 |
" }\n" |
| 754 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n" |
| 755 |
" nbytes = write(fd2, buf1 + buf1_written,\n" |
| 756 |
" buf1_avail - buf1_written);\n" |
| 757 |
" if (nbytes E<lt> 1)\n" |
| 758 |
" SHUT_FD2;\n" |
| 759 |
" else\n" |
| 760 |
" buf1_written += nbytes;\n" |
| 761 |
" }\n" |
| 762 |
"\\&\n" |
| 763 |
" /* Check if write data has caught read data. */\n" |
| 764 |
"\\&\n" |
| 765 |
" if (buf1_written == buf1_avail)\n" |
| 766 |
" buf1_written = buf1_avail = 0;\n" |
| 767 |
" if (buf2_written == buf2_avail)\n" |
| 768 |
" buf2_written = buf2_avail = 0;\n" |
| 769 |
"\\&\n" |
| 770 |
" /* One side has closed the connection, keep\n" |
| 771 |
" writing to the other side until empty. */\n" |
| 772 |
"\\&\n" |
| 773 |
" if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n" |
| 774 |
" SHUT_FD2;\n" |
| 775 |
" if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n" |
| 776 |
" SHUT_FD1;\n" |
| 777 |
" }\n" |
| 778 |
" exit(EXIT_SUCCESS);\n" |
| 779 |
"}\n" |
| 780 |
msgstr "" |
| 781 |
|
| 782 |
#. type: Plain text |
| 783 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 784 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 785 |
msgid "" |
| 786 |
"The above program properly forwards most kinds of TCP connections including " |
| 787 |
"OOB signal data transmitted by B<telnet> servers. It handles the tricky " |
| 788 |
"problem of having data flow in both directions simultaneously. You might " |
| 789 |
"think it more efficient to use a B<fork>(2) call and devote a thread to " |
| 790 |
"each stream. This becomes more tricky than you might suspect. Another idea " |
| 791 |
"is to set nonblocking I/O using B<fcntl>(2). This also has its problems " |
| 792 |
"because you end up using inefficient timeouts." |
| 793 |
msgstr "" |
| 794 |
|
| 795 |
#. type: Plain text |
| 796 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 797 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 798 |
msgid "" |
| 799 |
"The program does not handle more than one simultaneous connection at a time, " |
| 800 |
"although it could easily be extended to do this with a linked list of " |
| 801 |
"buffers\\[em]one for each connection. At the moment, new connections cause " |
| 802 |
"the current connection to be dropped." |
| 803 |
msgstr "" |
| 804 |
|
| 805 |
#. type: SH |
| 806 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 807 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 808 |
#, no-wrap |
| 809 |
msgid "SEE ALSO" |
| 810 |
msgstr "" |
| 811 |
|
| 812 |
#. type: Plain text |
| 813 |
#: archlinux debian-trixie debian-unstable fedora-44 fedora-rawhide |
| 814 |
#: mageia-cauldron opensuse-leap-16-1 opensuse-tumbleweed |
| 815 |
msgid "" |
| 816 |
"B<accept>(2), B<connect>(2), B<poll>(2), B<read>(2), B<recv>(2), " |
| 817 |
"B<select>(2), B<send>(2), B<sigprocmask>(2), B<write>(2), B<epoll>(7)" |
| 818 |
msgstr "" |
| 819 |
|
| 820 |
#. type: TH |
| 821 |
#: debian-trixie |
| 822 |
#, no-wrap |
| 823 |
msgid "2024-06-15" |
| 824 |
msgstr "" |
| 825 |
|
| 826 |
#. type: TH |
| 827 |
#: debian-trixie |
| 828 |
#, no-wrap |
| 829 |
msgid "Linux man-pages 6.9.1" |
| 830 |
msgstr "" |
| 831 |
|
| 832 |
#. type: Plain text |
| 833 |
#: debian-trixie |
| 834 |
msgid "Standard C library (I<libc>, I<-lc>)" |
| 835 |
msgstr "" |
| 836 |
|
| 837 |
#. type: Plain text |
| 838 |
#: debian-trixie fedora-44 fedora-rawhide |
| 839 |
#, no-wrap |
| 840 |
msgid "" |
| 841 |
"static volatile sig_atomic_t got_SIGCHLD = 0;\n" |
| 842 |
"\\&\n" |
| 843 |
"static void\n" |
| 844 |
"child_sig_handler(int sig)\n" |
| 845 |
"{\n" |
| 846 |
" got_SIGCHLD = 1;\n" |
| 847 |
"}\n" |
| 848 |
"\\&\n" |
| 849 |
"int\n" |
| 850 |
"main(int argc, char *argv[])\n" |
| 851 |
"{\n" |
| 852 |
" sigset_t sigmask, empty_mask;\n" |
| 853 |
" struct sigaction sa;\n" |
| 854 |
" fd_set readfds, writefds, exceptfds;\n" |
| 855 |
" int r;\n" |
| 856 |
"\\&\n" |
| 857 |
" sigemptyset(&sigmask);\n" |
| 858 |
" sigaddset(&sigmask, SIGCHLD);\n" |
| 859 |
" if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) {\n" |
| 860 |
" perror(\"sigprocmask\");\n" |
| 861 |
" exit(EXIT_FAILURE);\n" |
| 862 |
" }\n" |
| 863 |
"\\&\n" |
| 864 |
" sa.sa_flags = 0;\n" |
| 865 |
" sa.sa_handler = child_sig_handler;\n" |
| 866 |
" sigemptyset(&sa.sa_mask);\n" |
| 867 |
" if (sigaction(SIGCHLD, &sa, NULL) == -1) {\n" |
| 868 |
" perror(\"sigaction\");\n" |
| 869 |
" exit(EXIT_FAILURE);\n" |
| 870 |
" }\n" |
| 871 |
"\\&\n" |
| 872 |
" sigemptyset(&empty_mask);\n" |
| 873 |
"\\&\n" |
| 874 |
" for (;;) { /* main loop */\n" |
| 875 |
" /* Initialize readfds, writefds, and exceptfds\n" |
| 876 |
" before the pselect() call. (Code omitted.) */\n" |
| 877 |
"\\&\n" |
| 878 |
" r = pselect(nfds, &readfds, &writefds, &exceptfds,\n" |
| 879 |
" NULL, &empty_mask);\n" |
| 880 |
" if (r == -1 && errno != EINTR) {\n" |
| 881 |
" /* Handle error */\n" |
| 882 |
" }\n" |
| 883 |
"\\&\n" |
| 884 |
" if (got_SIGCHLD) {\n" |
| 885 |
" got_SIGCHLD = 0;\n" |
| 886 |
"\\&\n" |
| 887 |
" /* Handle signalled event here; e.g., wait() for all\n" |
| 888 |
" terminated children. (Code omitted.) */\n" |
| 889 |
" }\n" |
| 890 |
"\\&\n" |
| 891 |
" /* main body of program */\n" |
| 892 |
" }\n" |
| 893 |
"}\n" |
| 894 |
msgstr "" |
| 895 |
|
| 896 |
#. type: Plain text |
| 897 |
#: debian-trixie fedora-44 fedora-rawhide |
| 898 |
#, no-wrap |
| 899 |
msgid "" |
| 900 |
"#include E<lt>arpa/inet.hE<gt>\n" |
| 901 |
"#include E<lt>errno.hE<gt>\n" |
| 902 |
"#include E<lt>netinet/in.hE<gt>\n" |
| 903 |
"#include E<lt>signal.hE<gt>\n" |
| 904 |
"#include E<lt>stdio.hE<gt>\n" |
| 905 |
"#include E<lt>stdlib.hE<gt>\n" |
| 906 |
"#include E<lt>string.hE<gt>\n" |
| 907 |
"#include E<lt>sys/select.hE<gt>\n" |
| 908 |
"#include E<lt>sys/socket.hE<gt>\n" |
| 909 |
"#include E<lt>sys/types.hE<gt>\n" |
| 910 |
"#include E<lt>unistd.hE<gt>\n" |
| 911 |
"\\&\n" |
| 912 |
"static int forward_port;\n" |
| 913 |
"\\&\n" |
| 914 |
"#undef max\n" |
| 915 |
"#define max(x, y) ((x) E<gt> (y) ? (x) : (y))\n" |
| 916 |
"\\&\n" |
| 917 |
"static int\n" |
| 918 |
"listen_socket(int listen_port)\n" |
| 919 |
"{\n" |
| 920 |
" int lfd;\n" |
| 921 |
" int yes;\n" |
| 922 |
" struct sockaddr_in addr;\n" |
| 923 |
"\\&\n" |
| 924 |
" lfd = socket(AF_INET, SOCK_STREAM, 0);\n" |
| 925 |
" if (lfd == -1) {\n" |
| 926 |
" perror(\"socket\");\n" |
| 927 |
" return -1;\n" |
| 928 |
" }\n" |
| 929 |
"\\&\n" |
| 930 |
" yes = 1;\n" |
| 931 |
" if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR,\n" |
| 932 |
" &yes, sizeof(yes)) == -1)\n" |
| 933 |
" {\n" |
| 934 |
" perror(\"setsockopt\");\n" |
| 935 |
" close(lfd);\n" |
| 936 |
" return -1;\n" |
| 937 |
" }\n" |
| 938 |
"\\&\n" |
| 939 |
" memset(&addr, 0, sizeof(addr));\n" |
| 940 |
" addr.sin_port = htons(listen_port);\n" |
| 941 |
" addr.sin_family = AF_INET;\n" |
| 942 |
" if (bind(lfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" |
| 943 |
" perror(\"bind\");\n" |
| 944 |
" close(lfd);\n" |
| 945 |
" return -1;\n" |
| 946 |
" }\n" |
| 947 |
"\\&\n" |
| 948 |
" printf(\"accepting connections on port %d\\[rs]n\", listen_port);\n" |
| 949 |
" listen(lfd, 10);\n" |
| 950 |
" return lfd;\n" |
| 951 |
"}\n" |
| 952 |
"\\&\n" |
| 953 |
"static int\n" |
| 954 |
"connect_socket(int connect_port, char *address)\n" |
| 955 |
"{\n" |
| 956 |
" int cfd;\n" |
| 957 |
" struct sockaddr_in addr;\n" |
| 958 |
"\\&\n" |
| 959 |
" cfd = socket(AF_INET, SOCK_STREAM, 0);\n" |
| 960 |
" if (cfd == -1) {\n" |
| 961 |
" perror(\"socket\");\n" |
| 962 |
" return -1;\n" |
| 963 |
" }\n" |
| 964 |
"\\&\n" |
| 965 |
" memset(&addr, 0, sizeof(addr));\n" |
| 966 |
" addr.sin_port = htons(connect_port);\n" |
| 967 |
" addr.sin_family = AF_INET;\n" |
| 968 |
"\\&\n" |
| 969 |
" if (!inet_aton(address, (struct in_addr *) &addr.sin_addr.s_addr)) {\n" |
| 970 |
" fprintf(stderr, \"inet_aton(): bad IP address format\\[rs]n\");\n" |
| 971 |
" close(cfd);\n" |
| 972 |
" return -1;\n" |
| 973 |
" }\n" |
| 974 |
"\\&\n" |
| 975 |
" if (connect(cfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {\n" |
| 976 |
" perror(\"connect()\");\n" |
| 977 |
" shutdown(cfd, SHUT_RDWR);\n" |
| 978 |
" close(cfd);\n" |
| 979 |
" return -1;\n" |
| 980 |
" }\n" |
| 981 |
" return cfd;\n" |
| 982 |
"}\n" |
| 983 |
"\\&\n" |
| 984 |
"#define SHUT_FD1 do { \\[rs]\n" |
| 985 |
" if (fd1 E<gt>= 0) { \\[rs]\n" |
| 986 |
" shutdown(fd1, SHUT_RDWR); \\[rs]\n" |
| 987 |
" close(fd1); \\[rs]\n" |
| 988 |
" fd1 = -1; \\[rs]\n" |
| 989 |
" } \\[rs]\n" |
| 990 |
" } while (0)\n" |
| 991 |
"\\&\n" |
| 992 |
"#define SHUT_FD2 do { \\[rs]\n" |
| 993 |
" if (fd2 E<gt>= 0) { \\[rs]\n" |
| 994 |
" shutdown(fd2, SHUT_RDWR); \\[rs]\n" |
| 995 |
" close(fd2); \\[rs]\n" |
| 996 |
" fd2 = -1; \\[rs]\n" |
| 997 |
" } \\[rs]\n" |
| 998 |
" } while (0)\n" |
| 999 |
"\\&\n" |
| 1000 |
"#define BUF_SIZE 1024\n" |
| 1001 |
"\\&\n" |
| 1002 |
"int\n" |
| 1003 |
"main(int argc, char *argv[])\n" |
| 1004 |
"{\n" |
| 1005 |
" int h;\n" |
| 1006 |
" int ready, nfds;\n" |
| 1007 |
" int fd1 = -1, fd2 = -1;\n" |
| 1008 |
" int buf1_avail = 0, buf1_written = 0;\n" |
| 1009 |
" int buf2_avail = 0, buf2_written = 0;\n" |
| 1010 |
" char buf1[BUF_SIZE], buf2[BUF_SIZE];\n" |
| 1011 |
" fd_set readfds, writefds, exceptfds;\n" |
| 1012 |
" ssize_t nbytes;\n" |
| 1013 |
"\\&\n" |
| 1014 |
" if (argc != 4) {\n" |
| 1015 |
" fprintf(stderr, \"Usage\\[rs]n\\[rs]tfwd E<lt>listen-portE<gt> \"\n" |
| 1016 |
" \"E<lt>forward-to-portE<gt> E<lt>forward-to-ip-addressE<gt>\\[rs]n\");\n" |
| 1017 |
" exit(EXIT_FAILURE);\n" |
| 1018 |
" }\n" |
| 1019 |
"\\&\n" |
| 1020 |
" signal(SIGPIPE, SIG_IGN);\n" |
| 1021 |
"\\&\n" |
| 1022 |
" forward_port = atoi(argv[2]);\n" |
| 1023 |
"\\&\n" |
| 1024 |
" h = listen_socket(atoi(argv[1]));\n" |
| 1025 |
" if (h == -1)\n" |
| 1026 |
" exit(EXIT_FAILURE);\n" |
| 1027 |
"\\&\n" |
| 1028 |
" for (;;) {\n" |
| 1029 |
" nfds = 0;\n" |
| 1030 |
"\\&\n" |
| 1031 |
" FD_ZERO(&readfds);\n" |
| 1032 |
" FD_ZERO(&writefds);\n" |
| 1033 |
" FD_ZERO(&exceptfds);\n" |
| 1034 |
" FD_SET(h, &readfds);\n" |
| 1035 |
" nfds = max(nfds, h);\n" |
| 1036 |
"\\&\n" |
| 1037 |
" if (fd1 E<gt> 0 && buf1_avail E<lt> BUF_SIZE)\n" |
| 1038 |
" FD_SET(fd1, &readfds);\n" |
| 1039 |
" /* Note: nfds is updated below, when fd1 is added to\n" |
| 1040 |
" exceptfds. */\n" |
| 1041 |
" if (fd2 E<gt> 0 && buf2_avail E<lt> BUF_SIZE)\n" |
| 1042 |
" FD_SET(fd2, &readfds);\n" |
| 1043 |
"\\&\n" |
| 1044 |
" if (fd1 E<gt> 0 && buf2_avail - buf2_written E<gt> 0)\n" |
| 1045 |
" FD_SET(fd1, &writefds);\n" |
| 1046 |
" if (fd2 E<gt> 0 && buf1_avail - buf1_written E<gt> 0)\n" |
| 1047 |
" FD_SET(fd2, &writefds);\n" |
| 1048 |
"\\&\n" |
| 1049 |
" if (fd1 E<gt> 0) {\n" |
| 1050 |
" FD_SET(fd1, &exceptfds);\n" |
| 1051 |
" nfds = max(nfds, fd1);\n" |
| 1052 |
" }\n" |
| 1053 |
" if (fd2 E<gt> 0) {\n" |
| 1054 |
" FD_SET(fd2, &exceptfds);\n" |
| 1055 |
" nfds = max(nfds, fd2);\n" |
| 1056 |
" }\n" |
| 1057 |
"\\&\n" |
| 1058 |
" ready = select(nfds + 1, &readfds, &writefds, &exceptfds, NULL);\n" |
| 1059 |
"\\&\n" |
| 1060 |
" if (ready == -1 && errno == EINTR)\n" |
| 1061 |
" continue;\n" |
| 1062 |
"\\&\n" |
| 1063 |
" if (ready == -1) {\n" |
| 1064 |
" perror(\"select()\");\n" |
| 1065 |
" exit(EXIT_FAILURE);\n" |
| 1066 |
" }\n" |
| 1067 |
"\\&\n" |
| 1068 |
" if (FD_ISSET(h, &readfds)) {\n" |
| 1069 |
" socklen_t addrlen;\n" |
| 1070 |
" struct sockaddr_in client_addr;\n" |
| 1071 |
" int fd;\n" |
| 1072 |
"\\&\n" |
| 1073 |
" addrlen = sizeof(client_addr);\n" |
| 1074 |
" memset(&client_addr, 0, addrlen);\n" |
| 1075 |
" fd = accept(h, (struct sockaddr *) &client_addr, &addrlen);\n" |
| 1076 |
" if (fd == -1) {\n" |
| 1077 |
" perror(\"accept()\");\n" |
| 1078 |
" } else {\n" |
| 1079 |
" SHUT_FD1;\n" |
| 1080 |
" SHUT_FD2;\n" |
| 1081 |
" buf1_avail = buf1_written = 0;\n" |
| 1082 |
" buf2_avail = buf2_written = 0;\n" |
| 1083 |
" fd1 = fd;\n" |
| 1084 |
" fd2 = connect_socket(forward_port, argv[3]);\n" |
| 1085 |
" if (fd2 == -1)\n" |
| 1086 |
" SHUT_FD1;\n" |
| 1087 |
" else\n" |
| 1088 |
" printf(\"connect from %s\\[rs]n\",\n" |
| 1089 |
" inet_ntoa(client_addr.sin_addr));\n" |
| 1090 |
"\\&\n" |
| 1091 |
" /* Skip any events on the old, closed file\n" |
| 1092 |
" descriptors. */\n" |
| 1093 |
"\\&\n" |
| 1094 |
" continue;\n" |
| 1095 |
" }\n" |
| 1096 |
" }\n" |
| 1097 |
"\\&\n" |
| 1098 |
" /* NB: read OOB data before normal reads. */\n" |
| 1099 |
"\\&\n" |
| 1100 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &exceptfds)) {\n" |
| 1101 |
" char c;\n" |
| 1102 |
"\\&\n" |
| 1103 |
" nbytes = recv(fd1, &c, 1, MSG_OOB);\n" |
| 1104 |
" if (nbytes E<lt> 1)\n" |
| 1105 |
" SHUT_FD1;\n" |
| 1106 |
" else\n" |
| 1107 |
" send(fd2, &c, 1, MSG_OOB);\n" |
| 1108 |
" }\n" |
| 1109 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &exceptfds)) {\n" |
| 1110 |
" char c;\n" |
| 1111 |
"\\&\n" |
| 1112 |
" nbytes = recv(fd2, &c, 1, MSG_OOB);\n" |
| 1113 |
" if (nbytes E<lt> 1)\n" |
| 1114 |
" SHUT_FD2;\n" |
| 1115 |
" else\n" |
| 1116 |
" send(fd1, &c, 1, MSG_OOB);\n" |
| 1117 |
" }\n" |
| 1118 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &readfds)) {\n" |
| 1119 |
" nbytes = read(fd1, buf1 + buf1_avail,\n" |
| 1120 |
" BUF_SIZE - buf1_avail);\n" |
| 1121 |
" if (nbytes E<lt> 1)\n" |
| 1122 |
" SHUT_FD1;\n" |
| 1123 |
" else\n" |
| 1124 |
" buf1_avail += nbytes;\n" |
| 1125 |
" }\n" |
| 1126 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &readfds)) {\n" |
| 1127 |
" nbytes = read(fd2, buf2 + buf2_avail,\n" |
| 1128 |
" BUF_SIZE - buf2_avail);\n" |
| 1129 |
" if (nbytes E<lt> 1)\n" |
| 1130 |
" SHUT_FD2;\n" |
| 1131 |
" else\n" |
| 1132 |
" buf2_avail += nbytes;\n" |
| 1133 |
" }\n" |
| 1134 |
" if (fd1 E<gt> 0 && FD_ISSET(fd1, &writefds) && buf2_avail E<gt> 0) {\n" |
| 1135 |
" nbytes = write(fd1, buf2 + buf2_written,\n" |
| 1136 |
" buf2_avail - buf2_written);\n" |
| 1137 |
" if (nbytes E<lt> 1)\n" |
| 1138 |
" SHUT_FD1;\n" |
| 1139 |
" else\n" |
| 1140 |
" buf2_written += nbytes;\n" |
| 1141 |
" }\n" |
| 1142 |
" if (fd2 E<gt> 0 && FD_ISSET(fd2, &writefds) && buf1_avail E<gt> 0) {\n" |
| 1143 |
" nbytes = write(fd2, buf1 + buf1_written,\n" |
| 1144 |
" buf1_avail - buf1_written);\n" |
| 1145 |
" if (nbytes E<lt> 1)\n" |
| 1146 |
" SHUT_FD2;\n" |
| 1147 |
" else\n" |
| 1148 |
" buf1_written += nbytes;\n" |
| 1149 |
" }\n" |
| 1150 |
"\\&\n" |
| 1151 |
" /* Check if write data has caught read data. */\n" |
| 1152 |
"\\&\n" |
| 1153 |
" if (buf1_written == buf1_avail)\n" |
| 1154 |
" buf1_written = buf1_avail = 0;\n" |
| 1155 |
" if (buf2_written == buf2_avail)\n" |
| 1156 |
" buf2_written = buf2_avail = 0;\n" |
| 1157 |
"\\&\n" |
| 1158 |
" /* One side has closed the connection, keep\n" |
| 1159 |
" writing to the other side until empty. */\n" |
| 1160 |
"\\&\n" |
| 1161 |
" if (fd1 E<lt> 0 && buf1_avail - buf1_written == 0)\n" |
| 1162 |
" SHUT_FD2;\n" |
| 1163 |
" if (fd2 E<lt> 0 && buf2_avail - buf2_written == 0)\n" |
| 1164 |
" SHUT_FD1;\n" |
| 1165 |
" }\n" |
| 1166 |
" exit(EXIT_SUCCESS);\n" |
| 1167 |
"}\n" |
| 1168 |
msgstr "" |
| 1169 |
|
| 1170 |
#. type: TH |
| 1171 |
#: fedora-44 fedora-rawhide |
| 1172 |
#, no-wrap |
| 1173 |
msgid "2024-07-23" |
| 1174 |
msgstr "" |
| 1175 |
|
| 1176 |
#. type: TH |
| 1177 |
#: fedora-44 fedora-rawhide |
| 1178 |
#, no-wrap |
| 1179 |
msgid "Linux man-pages 6.13" |
| 1180 |
msgstr "" |
| 1181 |
|
| 1182 |
#. type: TH |
| 1183 |
#: mageia-cauldron |
| 1184 |
#, no-wrap |
| 1185 |
msgid "Linux man-pages 6.17" |
| 1186 |
msgstr "" |
| 1187 |
|
| 1188 |
#. type: TH |
| 1189 |
#: opensuse-leap-16-1 opensuse-tumbleweed |
| 1190 |
#, no-wrap |
| 1191 |
msgid "Linux man-pages (unreleased)" |
| 1192 |
msgstr "" |