| 1 | /* |
| 2 | * Abstraction API/layer for the various ways PvPGN can inspect sockets state |
| 3 | * 2003 (C) |
| 4 | * |
| 5 | * Code is based on the ideas found in thttpd project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 23 | #include "fdwatch.h"cxx . . $ `<file>/src/common/fdwatch.h`/ |
| 24 | |
| 25 | #include <cstring> |
| 26 | |
| 27 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 28 | #include "common/rlimit.h"cxx . . $ `<file>/src/common/rlimit.h`/ |
| 29 | #include "fdwatch_select.h"cxx . . $ `<file>/src/common/fdwatch_select.h`/ |
| 30 | #include "fdwatch_poll.h"cxx . . $ `<file>/src/common/fdwatch_poll.h`/ |
| 31 | #include "fdwatch_kqueue.h"cxx . . $ `<file>/src/common/fdwatch_kqueue.h`/ |
| 32 | #include "fdwatch_epoll.h"cxx . . $ `<file>/src/common/fdwatch_epoll.h`/ |
| 33 | #include "fdwbackend.h"cxx . . $ `<file>/src/common/fdwbackend.h`/ |
| 34 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 35 | |
| 36 | namespace pvpgncxx . . $ pvpgn/ |
| 37 | { |
| 38 | |
| 39 | unsigned fdw_maxconscxx . . $ pvpgn/fdw_maxcons.; |
| 40 | t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd# *fdw_fdscxx . . $ pvpgn/fdw_fds. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 41 | |
| 42 | namespacecxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/ |
| 43 | { |
| 44 | |
| 45 | FDWBackendcxx . . $ pvpgn/FDWBackend# * fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 46 | typedef elistcxx . . $ pvpgn/elist#<t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd#> FDWListcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/FDWList#; |
| 47 | FDWListcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/FDWList# freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist.(&t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd#::freelistcxx . . $ pvpgn/t_fdwatch_fd#freelist.); |
| 48 | FDWListcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/FDWList# uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist.(&t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd#::uselistcxx . . $ pvpgn/t_fdwatch_fd#uselist.); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | extern int fdwatch_initcxx . . $ pvpgn/fdwatch_init(7864480464b09eea).(int maxcons) |
| 53 | { |
| 54 | unsigned i; |
| 55 | int maxsys; |
| 56 | |
| 57 | maxsys = get_socket_limitcxx . . $ pvpgn/get_socket_limit(b126dc7c1de90089).(); |
| 58 | if (maxsys > 0) maxcons = (maxcons < maxsys) ? maxcons : maxsys; |
| 59 | if (maxcons < 32) { |
| 60 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_fatalcxx . . $ pvpgn/eventlog_level_fatal., __FUNCTION__, "too few sockets available ({})", maxcons); |
| 61 | return -1; |
| 62 | } |
| 63 | fdw_maxconscxx . . $ pvpgn/fdw_maxcons. = maxcons; |
| 64 | |
| 65 | fdw_fdscxx . . $ pvpgn/fdw_fds. = new t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd#[fdw_maxconscxx . . $ pvpgn/fdw_maxcons.]; |
| 66 | /* add all slots to the freelist */ |
| 67 | for (i = 0; i < fdw_maxconscxx . . $ pvpgn/fdw_maxcons.; i++) |
| 68 | freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..push_backcxx . . $ pvpgn/elist#push_back(8e935d9c2e576e3a).(fdw_fdscxx . . $ pvpgn/fdw_fds.[i]); |
| 69 | |
| 70 | #ifdef HAVE_EPOLLcxx . . $ `src/common/setup_before.h:409:10`! |
| 71 | try { |
| 72 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = new FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#(fdw_maxconscxx . . $ pvpgn/fdw_maxcons.); |
| 73 | return 0; |
| 74 | } |
| 75 | catch (const FDWBackendcxx . . $ pvpgn/FDWBackend#::InitErrorcxx . . $ pvpgn/FDWBackend#InitError#&) { |
| 76 | } |
| 77 | #endif |
| 78 | #ifdef HAVE_KQUEUE |
| 79 | try { |
| 80 | fdw = new FDWKqueueBackend(fdw_maxcons); |
| 81 | return 0; |
| 82 | } |
| 83 | catch (const FDWBackend::InitError&) { |
| 84 | } |
| 85 | #endif |
| 86 | #ifdef HAVE_POLLcxx . . $ `build/config.h:62:9`! |
| 87 | try { |
| 88 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = new FDWPollBackendcxx . . $ pvpgn/FDWPollBackend#(fdw_maxconscxx . . $ pvpgn/fdw_maxcons.); |
| 89 | return 0; |
| 90 | } |
| 91 | catch (const FDWBackendcxx . . $ pvpgn/FDWBackend#::InitErrorcxx . . $ pvpgn/FDWBackend#InitError#&) { |
| 92 | } |
| 93 | #endif |
| 94 | #ifdef HAVE_SELECTcxx . . $ `build/config.h:65:9`! |
| 95 | try { |
| 96 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = new FDWSelectBackendcxx . . $ pvpgn/FDWSelectBackend#(fdw_maxconscxx . . $ pvpgn/fdw_maxcons.); |
| 97 | return 0; |
| 98 | } |
| 99 | catch (const FDWBackendcxx . . $ pvpgn/FDWBackend#::InitErrorcxx . . $ pvpgn/FDWBackend#InitError#&) { |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | eventlogcxx . . $ pvpgn/eventlog(f1d8dd72586f1e25).(eventlog_level_fatalcxx . . $ pvpgn/eventlog_level_fatal., __FUNCTION__, "Found no working fdwatch layer"); |
| 104 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 105 | fdwatch_closecxx . . $ pvpgn/fdwatch_close(b126dc7c1de90089).(); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | extern int fdwatch_closecxx . . $ pvpgn/fdwatch_close(b126dc7c1de90089).(void) |
| 110 | { |
| 111 | if (fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.) { delete fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.; fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; } |
| 112 | if (fdw_fdscxx . . $ pvpgn/fdw_fds.) { delete[] fdw_fdscxx . . $ pvpgn/fdw_fds.; fdw_fdscxx . . $ pvpgn/fdw_fds. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; } |
| 113 | freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..clearcxx . . $ pvpgn/elist#clear(49f6e7a06ebc5aa8).(); |
| 114 | uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist..clearcxx . . $ pvpgn/elist#clear(49f6e7a06ebc5aa8).(); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | extern int fdwatch_add_fdcxx . . $ pvpgn/fdwatch_add_fd(559ef48a7724bde6).(int fd, unsigned rw, fdwatch_handlercxx . . $ pvpgn/fdwatch_handler# h, void *data) |
| 120 | { |
| 121 | /* max sockets reached */ |
| 122 | if (freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..emptycxx . . $ pvpgn/elist#empty(c9857a73d68e59ba).()) return -1; |
| 123 | |
| 124 | t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd# *cfd = &freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..frontcxx . . $ pvpgn/elist#front(c93303b41ac66b6d).(); |
| 125 | fdw_fdcxx . . $ `src/common/fdwatch.h:56:9`!(cfd) = fd; |
| 126 | |
| 127 | if (fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.->addcxx . . $ pvpgn/FDWBackend#add(b223e862534b1804).(fdw_idxcxx . . $ `src/common/fdwatch.h:55:9`!(cfd), rw)) return -1; |
| 128 | |
| 129 | /* add it to used sockets list, remove it from free list */ |
| 130 | uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist..push_backcxx . . $ pvpgn/elist#push_back(8e935d9c2e576e3a).(*cfd); |
| 131 | freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..removecxx . . $ pvpgn/elist#remove(8e935d9c2e576e3a).(*cfd); |
| 132 | |
| 133 | fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(cfd) = rw; |
| 134 | fdw_datacxx . . $ `src/common/fdwatch.h:58:9`!(cfd) = data; |
| 135 | fdw_hndcxx . . $ `src/common/fdwatch.h:59:9`!(cfd) = h; |
| 136 | |
| 137 | return fdw_idxcxx . . $ `src/common/fdwatch.h:55:9`!(cfd); |
| 138 | } |
| 139 | |
| 140 | extern int fdwatch_update_fdcxx . . $ pvpgn/fdwatch_update_fd(b223e862534b1804).(int idx, unsigned rw) |
| 141 | { |
| 142 | if (idx < 0 || idx >= fdw_maxconscxx . . $ pvpgn/fdw_maxcons.) { |
| 143 | ERROR2cxx . . $ `src/common/eventlog.h:75:9`!("out of bounds idx [{}] (max: {})", idx, fdw_maxcons); |
| 144 | return -1; |
| 145 | } |
| 146 | /* do not allow completly reset the access because then backend codes |
| 147 | * can get confused */ |
| 148 | if (!rw) { |
| 149 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("tried to reset rw, not allowed"); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | if (!fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(fdw_fds + idx)) { |
| 154 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("found reseted rw"); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | if (fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.->addcxx . . $ pvpgn/FDWBackend#add(b223e862534b1804).(idx, rw)) return -1; |
| 159 | fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(&fdw_fds[idx]) = rw; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | extern int fdwatch_del_fdcxx . . $ pvpgn/fdwatch_del_fd(7864480464b09eea).(int idx) |
| 165 | { |
| 166 | if (idx < 0 || idx >= fdw_maxconscxx . . $ pvpgn/fdw_maxcons.) { |
| 167 | ERROR2cxx . . $ `src/common/eventlog.h:75:9`!("out of bounds idx [{}] (max: {})", idx, fdw_maxcons); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd# *cfd = fdw_fdscxx . . $ pvpgn/fdw_fds. + idx; |
| 172 | if (!fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(cfd)) { |
| 173 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("found reseted rw"); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.->delcxx . . $ pvpgn/FDWBackend#del(7864480464b09eea).(idx); |
| 178 | |
| 179 | /* remove it from uselist, add it to freelist */ |
| 180 | uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist..removecxx . . $ pvpgn/elist#remove(8e935d9c2e576e3a).(*cfd); |
| 181 | freelistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/freelist..push_backcxx . . $ pvpgn/elist#push_back(8e935d9c2e576e3a).(*cfd); |
| 182 | |
| 183 | fdw_fdcxx . . $ `src/common/fdwatch.h:56:9`!(cfd) = 0; |
| 184 | fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(cfd) = 0; |
| 185 | fdw_datacxx . . $ `src/common/fdwatch.h:58:9`!(cfd) = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 186 | fdw_hndcxx . . $ `src/common/fdwatch.h:59:9`!(cfd) = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | extern int fdwatchcxx . . $ pvpgn/fdwatch(a9e4e82268fd7bfa).(long timeout_msec) |
| 192 | { |
| 193 | return fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.->watchcxx . . $ pvpgn/FDWBackend#watch(a9e4e82268fd7bfa).(timeout_msec); |
| 194 | } |
| 195 | |
| 196 | extern void fdwatch_handlecxx . . $ pvpgn/fdwatch_handle(49f6e7a06ebc5aa8).(void) |
| 197 | { |
| 198 | fdwcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/fdw.->handlecxx . . $ pvpgn/FDWBackend#handle(49f6e7a06ebc5aa8).(); |
| 199 | } |
| 200 | |
| 201 | extern void fdwatch_traversecxx . . $ pvpgn/fdwatch_traverse(8b1391bcbb30f1df).(t_fdw_cbcxx . . $ pvpgn/t_fdw_cb# cb, void *data) |
| 202 | { |
| 203 | for (FDWListcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/FDWList#::iteratorcxx . . $ pvpgn/elist#iterator# it(uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist..begincxx . . $ pvpgn/elist#begin(31f1ba2fb26ae483).()); it !=cxx . . $ pvpgn/elist#iterator#`operator!=`(296d7d1a0f763a4). uselistcxx . . $ pvpgn/`$anonymous_namespace_src/common/fdwatch.cpp`/uselist..endcxx . . $ pvpgn/elist#end(31f1ba2fb26ae483).(); ++cxx . . $ pvpgn/elist#iterator#operator++(6c041a7bc1227c87).it) { |
| 204 | if (cb(&*cxx . . $ pvpgn/elist#iterator#`operator*`(c93303b41ac66b6d).it, data)) break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } |