| 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 | * Linux epoll(4) based backend |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * as published by the Free Software Foundation; either version 2 |
| 12 | * of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | */ |
| 23 | |
| 24 | #include "common/setup_before.h"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 25 | #ifdef HAVE_EPOLLcxx . . $ `src/common/setup_before.h:409:10`! |
| 26 | #include "fdwatch_epoll.h"cxx . . $ `<file>/src/common/fdwatch_epoll.h`/ |
| 27 | |
| 28 | #include <cstring> |
| 29 | |
| 30 | #include "common/eventlog.h"cxx . . $ `<file>/src/common/eventlog.h`/ |
| 31 | #include "fdwatch.h"cxx . . $ `<file>/src/common/fdwatch.h`/ |
| 32 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 33 | |
| 34 | namespace pvpgncxx . . $ pvpgn/ |
| 35 | { |
| 36 | |
| 37 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#FDWEpollBackend(d4f767463ce0a6b3).(int nfds_) |
| 38 | :FDWBackendcxx . . $ pvpgn/FDWBackend#(nfds_), srcxx . . $ pvpgn/FDWEpollBackend#sr.(0) |
| 39 | { |
| 40 | if ((epfdcxx . . $ pvpgn/FDWEpollBackend#epfd. = epoll_create(nfdscxx . . $ pvpgn/FDWBackend#nfds.)) < 0) |
| 41 | throw InitErrorcxx . . $ pvpgn/FDWBackend#InitError#("failed to open epoll device"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 42 | epeventscxx . . $ pvpgn/FDWEpollBackend#epevents..resetcxx . . $ pvpgn/scoped_array#reset(ebd0a1552f8ce24f).(new struct epoll_eventcxx . . $ epoll_event#[nfdscxx . . $ pvpgn/FDWBackend#nfds.]); |
| 43 | |
| 44 | stdcxx . . $ std/::memset(epeventscxx . . $ pvpgn/FDWEpollBackend#epevents..getcxx . . $ pvpgn/scoped_array#get(5a2a78a048fb49a8).(), 0, sizeof(struct epoll_eventcxx . . $ epoll_event#) * nfdscxx . . $ pvpgn/FDWBackend#nfds.); |
| 45 | |
| 46 | INFO1cxx . . $ `src/common/eventlog.h:84:9`!("fdwatch epoll() based layer initialized (max {} sockets)", nfds); |
| 47 | } |
| 48 | |
| 49 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::~cxx . . $ pvpgn/FDWEpollBackend#`~FDWEpollBackend`(ced63f7c635d850d).FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#() throw() |
| 50 | {} |
| 51 | |
| 52 | int |
| 53 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::addcxx . . $ pvpgn/FDWEpollBackend#add(b223e862534b1804).(int idx, unsigned rw) |
| 54 | { |
| 55 | // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {} rw: {}", fd, rw); |
| 56 | |
| 57 | struct epoll_eventcxx . . $ epoll_event# tmpev; |
| 58 | stdcxx . . $ std/::memset(&tmpev, 0, sizeof(tmpev)); |
| 59 | tmpev.eventscxx . . $ epoll_event#events. = 0; |
| 60 | if (rw & fdwatch_type_readcxx . . $ pvpgn/fdwatch_type_read.) |
| 61 | tmpev.eventscxx . . $ epoll_event#events. |= EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`!; |
| 62 | if (rw & fdwatch_type_writecxx . . $ pvpgn/fdwatch_type_write.) |
| 63 | tmpev.eventscxx . . $ epoll_event#events. |= EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`!; |
| 64 | |
| 65 | int op = fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(fdw_fds + idx) ? EPOLL_CTL_MODcxx . . $ `<external>/7997773257684133104/epoll.h:74:9`! : EPOLL_CTL_ADDcxx . . $ `<external>/7997773257684133104/epoll.h:72:9`!; |
| 66 | |
| 67 | tmpev.datacxx . . $ epoll_event#data..fdcxx . . $ epoll_data#fd. = idx; |
| 68 | if (epoll_ctl(epfdcxx . . $ pvpgn/FDWEpollBackend#epfd., op, fdw_fdcxx . . $ `src/common/fdwatch.h:56:9`!(fdw_fds + idx), &tmpev)) { |
| 69 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("got error from epoll_ctl()"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::delcxx . . $ pvpgn/FDWEpollBackend#del(7864480464b09eea).(int idx) |
| 78 | { |
| 79 | // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {}", fd); |
| 80 | if (srcxx . . $ pvpgn/FDWEpollBackend#sr. > 0) |
| 81 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("BUG: called while still handling sockets"); |
| 82 | |
| 83 | if (fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(fdw_fds + idx)) { |
| 84 | struct epoll_eventcxx . . $ epoll_event# tmpev; |
| 85 | stdcxx . . $ std/::memset(&tmpev, 0, sizeof(tmpev)); |
| 86 | tmpev.eventscxx . . $ epoll_event#events. = 0; |
| 87 | tmpev.datacxx . . $ epoll_event#data..fdcxx . . $ epoll_data#fd. = idx; |
| 88 | if (epoll_ctl(epfdcxx . . $ pvpgn/FDWEpollBackend#epfd., EPOLL_CTL_DELcxx . . $ `<external>/7997773257684133104/epoll.h:73:9`!, fdw_fdcxx . . $ `src/common/fdwatch.h:56:9`!(fdw_fds + idx), &tmpev)) { |
| 89 | ERROR0cxx . . $ `src/common/eventlog.h:73:9`!("got error from epoll_ctl()"); |
| 90 | return -1; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::watchcxx . . $ pvpgn/FDWEpollBackend#watch(a9e4e82268fd7bfa).(long timeout_msec) |
| 99 | { |
| 100 | return (srcxx . . $ pvpgn/FDWEpollBackend#sr. = epoll_wait(epfdcxx . . $ pvpgn/FDWEpollBackend#epfd., epeventscxx . . $ pvpgn/FDWEpollBackend#epevents..getcxx . . $ pvpgn/scoped_array#get(5a2a78a048fb49a8).(), fdw_maxconscxx . . $ pvpgn/fdw_maxcons., timeout_msec)); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | FDWEpollBackendcxx . . $ pvpgn/FDWEpollBackend#::handlecxx . . $ pvpgn/FDWEpollBackend#handle(49f6e7a06ebc5aa8).() |
| 105 | { |
| 106 | // eventlog(eventlog_level_trace, __FUNCTION__, "called"); |
| 107 | for (struct epoll_eventcxx . . $ epoll_event# *ev = epeventscxx . . $ pvpgn/FDWEpollBackend#epevents..getcxx . . $ pvpgn/scoped_array#get(5a2a78a048fb49a8).(); srcxx . . $ pvpgn/FDWEpollBackend#sr.; srcxx . . $ pvpgn/FDWEpollBackend#sr.--, ev++) |
| 108 | { |
| 109 | // eventlog(eventlog_level_trace, __FUNCTION__, "checking {} ident: {} read: {} write: {}", i, kqevents[i].ident, kqevents[i].filter & EVFILT_READ, kqevents[i].filter & EVFILT_WRITE); |
| 110 | t_fdwatch_fdcxx . . $ pvpgn/t_fdwatch_fd# *cfd = fdw_fdscxx . . $ pvpgn/fdw_fds. + ev->datacxx . . $ epoll_event#data..fdcxx . . $ epoll_data#fd.; |
| 111 | |
| 112 | if (fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(cfd) & fdwatch_type_readcxx . . $ pvpgn/fdwatch_type_read. && ev->eventscxx . . $ epoll_event#events. & (EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`! | EPOLLERRcxx . . $ `<external>/7997773257684133104/epoll.h:55:9`! | EPOLLHUPcxx . . $ `<external>/7997773257684133104/epoll.h:57:9`!)) |
| 113 | if (fdw_hndcxx . . $ `src/common/fdwatch.h:59:9`!(cfd) (fdw_datacxx . . $ `src/common/fdwatch.h:58:9`!(cfd), fdwatch_type_readcxx . . $ pvpgn/fdwatch_type_read.) == -2) |
| 114 | continue; |
| 115 | |
| 116 | if (fdw_rwcxx . . $ `src/common/fdwatch.h:57:9`!(cfd) & fdwatch_type_writecxx . . $ pvpgn/fdwatch_type_write. && ev->eventscxx . . $ epoll_event#events. & (EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`! | EPOLLERRcxx . . $ `<external>/7997773257684133104/epoll.h:55:9`! | EPOLLHUPcxx . . $ `<external>/7997773257684133104/epoll.h:57:9`!)) |
| 117 | fdw_hndcxx . . $ `src/common/fdwatch.h:59:9`!(cfd) (fdw_datacxx . . $ `src/common/fdwatch.h:58:9`!(cfd), fdwatch_type_writecxx . . $ pvpgn/fdwatch_type_write.); |
| 118 | } |
| 119 | srcxx . . $ pvpgn/FDWEpollBackend#sr. = 0; |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | #endif /* HAVE_EPOLL */ |