| 1 | /* |
| 2 | * Copyright (c) 2013-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/param.h> |
| 18 | #include <sys/epoll.h> |
| 19 | #include <sys/sendfile.h> |
| 20 | #include <sys/syscall.h> |
| 21 | |
| 22 | #include <sched.h> |
| 23 | |
| 24 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 25 | #include "seccomp.h"cxx . . $ `<file>/include/kore/seccomp.h`/ |
| 26 | |
| 27 | #if defined(KORE_USE_PGSQL) |
| 28 | #include "pgsql.h" |
| 29 | #endif |
| 30 | |
| 31 | #if defined(KORE_USE_TASKS) |
| 32 | #include "tasks.h" |
| 33 | #endif |
| 34 | |
| 35 | static int efdcxx . . $ efd. = -1; |
| 36 | static u_int32_tcxx . . $ u_int32_t# event_countcxx . . $ event_count. = 0; |
| 37 | static struct epoll_eventcxx . . $ epoll_event# *eventscxx . . $ events. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 38 | |
| 39 | void |
| 40 | kore_platform_initcxx . . $ kore_platform_init(49f6e7a06ebc5aa8).(void) |
| 41 | { |
| 42 | long n; |
| 43 | |
| 44 | kore_seccomp_initcxx . . $ kore_seccomp_init(49f6e7a06ebc5aa8).(); |
| 45 | |
| 46 | if ((n = sysconfcxx . . $ sysconf(dc1f09c09fbff20f).(_SC_NPROCESSORS_ONLNcxx . . $ `<external>/6393649343499073842/confname.h:253:9`!)) == -1) { |
| 47 | cpu_countcxx . . $ cpu_count. = 1; |
| 48 | } else { |
| 49 | cpu_countcxx . . $ cpu_count. = (u_int16_tcxx . . $ u_int16_t#)n; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | kore_platform_worker_setcpucxx . . $ kore_platform_worker_setcpu(8632b877e8ae27d6).(struct kore_workercxx . . $ kore_worker# *kw) |
| 55 | { |
| 56 | cpu_set_tcxx . . $ cpu_set_t# cpuset; |
| 57 | |
| 58 | CPU_ZEROcxx . . $ `<external>/17522079545615609418/sched.h:96:10`!(&cpuset); |
| 59 | CPU_SETcxx . . $ `<external>/17522079545615609418/sched.h:92:10`!(kw->cpu, &cpuset); |
| 60 | |
| 61 | if (sched_setaffinitycxx . . $ sched_setaffinity(48631566441f33fb).(0, sizeof(cpu_set_tcxx . . $ cpu_set_t#), &cpuset) == -1) |
| 62 | kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "kore_worker_setcpu(): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | kore_platform_event_initcxx . . $ kore_platform_event_init(49f6e7a06ebc5aa8).(void) |
| 67 | { |
| 68 | if (efdcxx . . $ efd. != -1) |
| 69 | closecxx . . $ close(7864480464b09eea).(efdcxx . . $ efd.); |
| 70 | if (eventscxx . . $ events. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 71 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(eventscxx . . $ events.); |
| 72 | |
| 73 | if ((efdcxx . . $ efd. = epoll_createcxx . . $ epoll_create(7864480464b09eea).(10000)) == -1) |
| 74 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("epoll_create(): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 75 | |
| 76 | event_countcxx . . $ event_count. = worker_max_connectionscxx . . $ worker_max_connections. + nlistenerscxx . . $ nlisteners.; |
| 77 | eventscxx . . $ events. = kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(event_countcxx . . $ event_count., sizeof(struct epoll_eventcxx . . $ epoll_event#)); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | kore_platform_event_cleanupcxx . . $ kore_platform_event_cleanup(49f6e7a06ebc5aa8).(void) |
| 82 | { |
| 83 | if (efdcxx . . $ efd. != -1) { |
| 84 | closecxx . . $ close(7864480464b09eea).(efdcxx . . $ efd.); |
| 85 | efdcxx . . $ efd. = -1; |
| 86 | } |
| 87 | |
| 88 | if (eventscxx . . $ events. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 89 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(eventscxx . . $ events.); |
| 90 | eventscxx . . $ events. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | kore_platform_event_waitcxx . . $ kore_platform_event_wait(2c44cd906ae4e829).(u_int64_tcxx . . $ u_int64_t# timer) |
| 96 | { |
| 97 | u_int32_tcxx . . $ u_int32_t# r; |
| 98 | struct kore_eventcxx . . $ kore_event# *evt; |
| 99 | int n, i, timeo; |
| 100 | |
| 101 | if (timer == KORE_WAIT_INFINITEcxx . . $ `include/kore/kore.h:97:9`!) |
| 102 | timeo = -1; |
| 103 | else |
| 104 | timeo = timer; |
| 105 | |
| 106 | n = epoll_waitcxx . . $ epoll_wait(50c76d20e903e766).(efdcxx . . $ efd., eventscxx . . $ events., event_countcxx . . $ event_count., timeo); |
| 107 | if (n == -1) { |
| 108 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) |
| 109 | return; |
| 110 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("epoll_wait(): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 111 | } |
| 112 | |
| 113 | r = 0; |
| 114 | for (i = 0; i < n; i++) { |
| 115 | if (eventscxx . . $ events.[i].datacxx . . $ epoll_event#data..ptrcxx . . $ epoll_data#ptr. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 116 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("events[%d].data.ptr == NULL", i); |
| 117 | |
| 118 | r = 0; |
| 119 | evt = (struct kore_eventcxx . . $ kore_event# *)eventscxx . . $ events.[i].datacxx . . $ epoll_event#data..ptrcxx . . $ epoll_data#ptr.; |
| 120 | |
| 121 | if (eventscxx . . $ events.[i].eventscxx . . $ epoll_event#events. & EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`!) |
| 122 | evt->flagscxx . . $ kore_event#flags. |= KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!; |
| 123 | |
| 124 | if (eventscxx . . $ events.[i].eventscxx . . $ epoll_event#events. & EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`!) |
| 125 | evt->flagscxx . . $ kore_event#flags. |= KORE_EVENT_WRITEcxx . . $ `include/kore/kore.h:188:9`!; |
| 126 | |
| 127 | if (eventscxx . . $ events.[i].eventscxx . . $ epoll_event#events. & EPOLLERRcxx . . $ `<external>/7997773257684133104/epoll.h:55:9`! || |
| 128 | eventscxx . . $ events.[i].eventscxx . . $ epoll_event#events. & EPOLLHUPcxx . . $ `<external>/7997773257684133104/epoll.h:57:9`! || |
| 129 | eventscxx . . $ events.[i].eventscxx . . $ epoll_event#events. & EPOLLRDHUPcxx . . $ `<external>/7997773257684133104/epoll.h:59:9`!) |
| 130 | r = 1; |
| 131 | |
| 132 | evt->handlecxx . . $ kore_event#handle.(eventscxx . . $ events.[i].datacxx . . $ epoll_event#data..ptrcxx . . $ epoll_data#ptr., r); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | kore_platform_event_level_allcxx . . $ kore_platform_event_level_all(6ca3d68f5c5f0975).(int fd, void *c) |
| 138 | { |
| 139 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(fd, EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`! | EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`! | EPOLLRDHUPcxx . . $ `<external>/7997773257684133104/epoll.h:59:9`!, 0, c); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | kore_platform_event_level_readcxx . . $ kore_platform_event_level_read(6ca3d68f5c5f0975).(int fd, void *c) |
| 144 | { |
| 145 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(fd, EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`! | EPOLLRDHUPcxx . . $ `<external>/7997773257684133104/epoll.h:59:9`!, 0, c); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | kore_platform_event_allcxx . . $ kore_platform_event_all(6ca3d68f5c5f0975).(int fd, void *c) |
| 150 | { |
| 151 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(fd, |
| 152 | EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`! | EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`! | EPOLLRDHUPcxx . . $ `<external>/7997773257684133104/epoll.h:59:9`! | EPOLLETcxx . . $ `<external>/7997773257684133104/epoll.h:67:9`!, 0, c); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(int fd, int type, int flags, void *udata) |
| 157 | { |
| 158 | struct epoll_eventcxx . . $ epoll_event# evt; |
| 159 | |
| 160 | evt.eventscxx . . $ epoll_event#events. = type; |
| 161 | evt.datacxx . . $ epoll_event#data..ptrcxx . . $ epoll_data#ptr. = udata; |
| 162 | if (epoll_ctlcxx . . $ epoll_ctl(9943be275b30aff9).(efdcxx . . $ efd., EPOLL_CTL_ADDcxx . . $ `<external>/7997773257684133104/epoll.h:72:9`!, fd, &evt) == -1) { |
| 163 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EEXISTcxx . . $ `<external>/8987895939074593727/errno-base.h:21:9`!) { |
| 164 | if (epoll_ctlcxx . . $ epoll_ctl(9943be275b30aff9).(efdcxx . . $ efd., EPOLL_CTL_MODcxx . . $ `<external>/7997773257684133104/epoll.h:74:9`!, fd, &evt) == -1) |
| 165 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("epoll_ctl() MOD: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 166 | } else { |
| 167 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("epoll_ctl() ADD: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | kore_platform_schedule_readcxx . . $ kore_platform_schedule_read(6ca3d68f5c5f0975).(int fd, void *data) |
| 174 | { |
| 175 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(fd, EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`! | EPOLLETcxx . . $ `<external>/7997773257684133104/epoll.h:67:9`!, 0, data); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | kore_platform_schedule_writecxx . . $ kore_platform_schedule_write(6ca3d68f5c5f0975).(int fd, void *data) |
| 180 | { |
| 181 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(fd, EPOLLOUTcxx . . $ `<external>/7997773257684133104/epoll.h:43:9`! | EPOLLETcxx . . $ `<external>/7997773257684133104/epoll.h:67:9`!, 0, data); |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | kore_platform_disable_readcxx . . $ kore_platform_disable_read(d4f767463ce0a6b3).(int fd) |
| 186 | { |
| 187 | if (epoll_ctlcxx . . $ epoll_ctl(9943be275b30aff9).(efdcxx . . $ efd., EPOLL_CTL_DELcxx . . $ `<external>/7997773257684133104/epoll.h:73:9`!, fd, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) |
| 188 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_platform_disable_read: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | kore_platform_enable_acceptcxx . . $ kore_platform_enable_accept(49f6e7a06ebc5aa8).(void) |
| 193 | { |
| 194 | struct listenercxx . . $ listener# *l; |
| 195 | struct kore_servercxx . . $ kore_server# *srv; |
| 196 | |
| 197 | LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) { |
| 198 | LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(l, &srv->listeners, list) |
| 199 | kore_platform_event_schedulecxx . . $ kore_platform_event_schedule(69825027728dcc55).(l->fdcxx . . $ listener#fd., EPOLLINcxx . . $ `<external>/7997773257684133104/epoll.h:39:9`!, 0, l); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void |
| 204 | kore_platform_disable_acceptcxx . . $ kore_platform_disable_accept(49f6e7a06ebc5aa8).(void) |
| 205 | { |
| 206 | struct listenercxx . . $ listener# *l; |
| 207 | struct kore_servercxx . . $ kore_server# *srv; |
| 208 | |
| 209 | LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) { |
| 210 | LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(l, &srv->listeners, list) { |
| 211 | if (epoll_ctlcxx . . $ epoll_ctl(9943be275b30aff9).(efdcxx . . $ efd., EPOLL_CTL_DELcxx . . $ `<external>/7997773257684133104/epoll.h:73:9`!, l->fdcxx . . $ listener#fd., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) { |
| 212 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_platform_disable_accept: %s", |
| 213 | errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | kore_platform_proctitlecxx . . $ kore_platform_proctitle(85c52e162fed56f9).(const char *title) |
| 221 | { |
| 222 | kore_proctitlecxx . . $ kore_proctitle(85c52e162fed56f9).(title); |
| 223 | } |
| 224 | |
| 225 | #if defined(KORE_USE_PLATFORM_SENDFILEcxx . . $ `include/kore/kore.h:56:9`!) |
| 226 | int |
| 227 | kore_platform_sendfilecxx . . $ kore_platform_sendfile(aa82428a214d4f3b).(struct connectioncxx . . $ connection# *c, struct netbufcxx . . $ netbuf# *nb) |
| 228 | { |
| 229 | off_tcxx . . $ off_t# smin; |
| 230 | ssize_tcxx . . $ ssize_t# sent; |
| 231 | size_t len, prevoff; |
| 232 | |
| 233 | prevoff = nb->fd_offcxx . . $ netbuf#fd_off.; |
| 234 | smin = nb->fd_lencxx . . $ netbuf#fd_len. - nb->fd_offcxx . . $ netbuf#fd_off.; |
| 235 | len = MINcxx . . $ `<external>/1231830743887005719/param.h:102:9`!(SENDFILE_PAYLOAD_MAXcxx . . $ `include/kore/kore.h:110:9`!, smin); |
| 236 | |
| 237 | resend: |
| 238 | sent = sendfilecxx . . $ sendfile(6a7fd511aaf5ed25).(c->fdcxx . . $ connection#fd., nb->file_refcxx . . $ netbuf#file_ref.->fdcxx . . $ kore_fileref#fd., &nb->fd_offcxx . . $ netbuf#fd_off., len); |
| 239 | if (sent == -1) { |
| 240 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!) { |
| 241 | c->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. &= ~KORE_EVENT_WRITEcxx . . $ `include/kore/kore.h:188:9`!; |
| 242 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 243 | } |
| 244 | |
| 245 | return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!); |
| 246 | } |
| 247 | |
| 248 | if (nb->fd_offcxx . . $ netbuf#fd_off. - prevoff != (size_t)len) |
| 249 | goto resend; |
| 250 | |
| 251 | if (sent == 0 || nb->fd_offcxx . . $ netbuf#fd_off. == nb->fd_lencxx . . $ netbuf#fd_len.) { |
| 252 | net_remove_netbufcxx . . $ net_remove_netbuf(59f25c31eed43fe1).(c, nb); |
| 253 | c->snbcxx . . $ connection#snb. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 254 | } |
| 255 | |
| 256 | return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!); |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | void |
| 261 | kore_platform_sandboxcxx . . $ kore_platform_sandbox(49f6e7a06ebc5aa8).(void) |
| 262 | { |
| 263 | kore_seccomp_enablecxx . . $ kore_seccomp_enable(49f6e7a06ebc5aa8).(); |
| 264 | } |