Debian Codegraph @

src/worker.c

raw symbols SCIP
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/types.h>
19 #include <sys/stat.h>
20 #include <sys/shm.h>
21 #include <sys/wait.h>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
25
26 #include <fcntl.h>
27 #include <grp.h>
28 #include <pwd.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/
33
34 #if defined(KORE_USE_ACME)
35 #include "acme.h"
36 #endif
37
38 #if !defined(KORE_NO_HTTP)
39 #include "http.h"cxx . . $ `<file>/include/kore/http.h`/
40 #endif
41
42 #if defined(KORE_USE_PGSQL)
43 #include "pgsql.h"
44 #endif
45
46 #if defined(KORE_USE_TASKS)
47 #include "tasks.h"
48 #endif
49
50 #if defined(KORE_USE_PYTHON)
51 #include "python_api.h"
52 #endif
53
54 #if defined(KORE_USE_CURL)
55 #include "curl.h"
56 #endif
57
58 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
59 #include "seccomp.h"cxx . . $ `<file>/include/kore/seccomp.h`/
60 #endif
61
62 #define WORKER_SOLO_COUNTcxx . . $ `src/worker.c:62:9`! 3
63
64 #define WORKERcxx . . $ `src/worker.c:64:9`!(id) \
65 (struct kore_worker *)((u_int8_t *)kore_workers + \
66 (sizeof(struct kore_worker) * id))
67
68 struct wlockcxx . . $ wlock# {
69 volatile int lockcxx . . $ wlock#lock.;
70 pid_tcxx . . $ pid_t# currentcxx . . $ wlock#current.;
71 };
72
73 static int worker_trylockcxx . . $ worker_trylock(b126dc7c1de90089).(void);
74 static void worker_unlockcxx . . $ worker_unlock(49f6e7a06ebc5aa8).(void);
75 static void worker_reapercxx . . $ worker_reaper(3e454b8ccf442868).(pid_tcxx . . $ pid_t#, int);
76 static void worker_runtime_teardowncxx . . $ worker_runtime_teardown(49f6e7a06ebc5aa8).(void);
77 static void worker_runtime_configurecxx . . $ worker_runtime_configure(49f6e7a06ebc5aa8).(void);
78 static void worker_domain_checkcxx . . $ worker_domain_check(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *);
79
80 static struct kore_runtime_callcxx . . $ kore_runtime_call# *worker_runtime_signalcxx . . $ worker_runtime_signal(7a0c5ebe46220834).(void);
81
82 static inline int worker_acceptlock_obtaincxx . . $ worker_acceptlock_obtain(b126dc7c1de90089).(void);
83 static inline void worker_acceptlock_releasecxx . . $ worker_acceptlock_release(49f6e7a06ebc5aa8).(void);
84 static void worker_accept_availcxx . . $ worker_accept_avail(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *);
85
86 static void worker_entropy_recvcxx . . $ worker_entropy_recv(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *);
87 static void worker_keymgr_responsecxx . . $ worker_keymgr_response(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *, const void *);
88
89 static pid_tcxx . . $ pid_t# worker_pgrpcxx . . $ worker_pgrp.;
90 static int accept_availcxx . . $ accept_avail.;
91 static struct kore_workercxx . . $ kore_worker# *kore_workerscxx . . $ kore_workers.;
92 static int worker_no_lockcxx . . $ worker_no_lock.;
93 static int shm_accept_keycxx . . $ shm_accept_key.;
94 static struct wlockcxx . . $ wlock# *accept_lockcxx . . $ accept_lock.;
95
96 struct kore_workercxx . . $ kore_worker# *workercxx . . $ worker. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
97 u_int8_tcxx . . $ u_int8_t# worker_set_affinitycxx . . $ worker_set_affinity. = 1;
98 u_int32_tcxx . . $ u_int32_t# worker_accept_thresholdcxx . . $ worker_accept_threshold. = 16;
99 u_int32_tcxx . . $ u_int32_t# worker_rlimit_nofilescxx . . $ worker_rlimit_nofiles. = 768;
100 u_int32_tcxx . . $ u_int32_t# worker_max_connectionscxx . . $ worker_max_connections. = 512;
101 u_int32_tcxx . . $ u_int32_t# worker_active_connectionscxx . . $ worker_active_connections. = 0;
102 int worker_policycxx . . $ worker_policy. = KORE_WORKER_POLICY_RESTARTcxx . . $ `include/kore/kore.h:656:9`!;
103
104 int
105 kore_worker_initcxx . . $ kore_worker_init(b126dc7c1de90089).(void)
106 {
107 size_t len;
108 struct kore_workercxx . . $ kore_worker# *kw;
109 u_int16_tcxx . . $ u_int16_t# idx, id, cpu;
110
111 worker_no_lockcxx . . $ worker_no_lock. = 0;
112
113 if (worker_countcxx . . $ worker_count. == 0)
114 worker_countcxx . . $ worker_count. = cpu_countcxx . . $ cpu_count.;
115
116 /* Account for the keymgr/acme even if we don't end up starting it. */
117 worker_countcxx . . $ worker_count. += 2;
118
119 len = sizeof(*accept_lockcxx . . $ accept_lock.) +
120 (sizeof(struct kore_workercxx . . $ kore_worker#) * worker_countcxx . . $ worker_count.);
121
122 shm_accept_keycxx . . $ shm_accept_key. = shmgetcxx . . $ shmget(cc2002ebc19f167c).(IPC_PRIVATEcxx . . $ `<external>/13393576928612956856/ipc.h:38:9`!, len, IPC_CREATcxx . . $ `<external>/13393576928612956856/ipc.h:25:9`! | IPC_EXCLcxx . . $ `<external>/13393576928612956856/ipc.h:26:9`! | 0700);
123 if (shm_accept_keycxx . . $ shm_accept_key. == -1)
124 fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_worker_init(): shmget() %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
125 if ((accept_lockcxx . . $ accept_lock. = shmatcxx . . $ shmat(701fce1eb9cbdb9d).(shm_accept_keycxx . . $ shm_accept_key., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0)) == (void *)-1)
126 fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_worker_init(): shmat() %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
127
128 accept_lockcxx . . $ accept_lock.->lockcxx . . $ wlock#lock. = 0;
129 accept_lockcxx . . $ accept_lock.->currentcxx . . $ wlock#current. = 0;
130
131 kore_workerscxx . . $ kore_workers. = (struct kore_workercxx . . $ kore_worker# *)((u_int8_tcxx . . $ u_int8_t# *)accept_lockcxx . . $ accept_lock. +
132 sizeof(*accept_lockcxx . . $ accept_lock.));
133 memsetcxx . . $ memset(d1de1e4fb215f1c6).(kore_workerscxx . . $ kore_workers., 0, sizeof(struct kore_workercxx . . $ kore_worker#) * worker_countcxx . . $ worker_count.);
134
135 if (worker_countcxx . . $ worker_count. > cpu_countcxx . . $ cpu_count.)
136 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "more worker processes than cpu cores");
137
138 /* Setup log buffers. */
139 for (idx = KORE_WORKER_BASEcxx . . $ `include/kore/kore.h:651:9`!; idx < worker_countcxx . . $ worker_count.; idx++) {
140 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
141 kw->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. = 0;
142 }
143
144 if (!kore_quietcxx . . $ kore_quiet.)
145 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "starting worker processes");
146
147 if ((worker_pgrpcxx . . $ worker_pgrp. = getpgrpcxx . . $ getpgrp(b126dc7c1de90089).()) == -1)
148 fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: getpgrp(): %s", __func__, errno_scxx . . $ `include/kore/kore.h:100:9`!);
149
150 /* Now start all the workers. */
151 id = 1;
152 cpu = 1;
153 for (idx = KORE_WORKER_BASEcxx . . $ `include/kore/kore.h:651:9`!; idx < worker_countcxx . . $ worker_count.; idx++) {
154 if (cpu >= cpu_countcxx . . $ cpu_count.)
155 cpu = 0;
156 if (!kore_worker_spawncxx . . $ kore_worker_spawn(953416d95ce153a1).(idx, id++, cpu++))
157 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
158 }
159
160 if (kore_keymgr_activecxx . . $ kore_keymgr_active.) {
161 #if defined(KORE_USE_ACME)
162 /* The ACME process is only started if we need it. */
163 if (acme_domains) {
164 if (!kore_worker_spawn(KORE_WORKER_ACME_IDX,
165 KORE_WORKER_ACME, 0))
166 return (KORE_RESULT_ERROR);
167 }
168 #endif
169
170 /* Now we can start the keymgr. */
171 if (!kore_worker_spawncxx . . $ kore_worker_spawn(953416d95ce153a1).(KORE_WORKER_KEYMGR_IDXcxx . . $ `include/kore/kore.h:649:9`!,
172 KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!, 0))
173 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
174 }
175
176 if (!kore_quietcxx . . $ kore_quiet.)
177 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_INFOcxx . . $ `<external>/6168640806541382331/syslog.h:57:9`!, "all worker processes started");
178
179 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
180 }
181
182 int
183 kore_worker_spawncxx . . $ kore_worker_spawn(953416d95ce153a1).(u_int16_tcxx . . $ u_int16_t# idx, u_int16_tcxx . . $ u_int16_t# id, u_int16_tcxx . . $ u_int16_t# cpu)
184 {
185 int cnt;
186 struct kore_workercxx . . $ kore_worker# *kw;
187 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
188 int status;
189 #endif
190
191 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
192 kw->idcxx . . $ kore_worker#id. = id;
193 kw->cpucxx . . $ kore_worker#cpu. = cpu;
194 kw->runningcxx . . $ kore_worker#running. = 1;
195
196 kw->readycxx . . $ kore_worker#ready. = 0;
197 kw->has_lockcxx . . $ kore_worker#has_lock. = 0;
198 kw->active_routecxx . . $ kore_worker#active_route. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
199
200 if (socketpaircxx . . $ socketpair(669e4832ed6c1c49).(AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!, SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!, 0, kw->pipecxx . . $ kore_worker#pipe.) == -1)
201 fatalcxx . . $ fatal(f6bc48f0426f26ee).("socketpair(): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
202
203 if (!kore_connection_nonblockcxx . . $ kore_connection_nonblock(9b79fb6aee4c0440).(kw->pipecxx . . $ kore_worker#pipe.[0], 0) ||
204 !kore_connection_nonblockcxx . . $ kore_connection_nonblock(9b79fb6aee4c0440).(kw->pipecxx . . $ kore_worker#pipe.[1], 0))
205 fatalcxx . . $ fatal(f6bc48f0426f26ee).("could not set pipe fds to nonblocking: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
206
207 switch (id) {
208 case KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!:
209 kw->pscxx . . $ kore_worker#ps. = &keymgr_privsepcxx . . $ keymgr_privsep.;
210 break;
211 #if defined(KORE_USE_ACME)
212 case KORE_WORKER_ACME:
213 kw->ps = &acme_privsep;
214 break;
215 #endif
216 default:
217 kw->pscxx . . $ kore_worker#ps. = &worker_privsepcxx . . $ worker_privsep.;
218 break;
219 }
220
221 kw->pidcxx . . $ kore_worker#pid. = forkcxx . . $ fork(b126dc7c1de90089).();
222 if (kw->pidcxx . . $ kore_worker#pid. == -1)
223 fatalcxx . . $ fatal(f6bc48f0426f26ee).("could not spawn worker child: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
224
225 if (kw->pidcxx . . $ kore_worker#pid. == 0) {
226 kw->pidcxx . . $ kore_worker#pid. = getpidcxx . . $ getpid(b126dc7c1de90089).();
227 kore_worker_entrycxx . . $ kore_worker_entry(b12959a5c0a914ff).(kw);
228 exitcxx . . $ exit(3ca292c338df9bb3).(1);
229 } else {
230 for (cnt = 0; cnt < 50; cnt++) {
231 if (kw->readycxx . . $ kore_worker#ready. == 1)
232 break;
233 usleepcxx . . $ usleep(4c679297bf94b9eb).(100000);
234 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
235 /*
236 * If seccomp_tracing is enabled, make sure we
237 * handle the SIGSTOP from the child processes.
238 */
239 if (kore_seccomp_tracingcxx . . $ kore_seccomp_tracing.) {
240 if (waitpidcxx . . $ waitpid(1df93256b606fe1f).(kw->pidcxx . . $ kore_worker#pid., &status, WNOHANGcxx . . $ `<external>/1585314143251398045/waitflags.h:25:9`!) > 0)
241 kore_seccomp_tracecxx . . $ kore_seccomp_trace(9b79fb6aee4c0440).(kw->pidcxx . . $ kore_worker#pid., status);
242 }
243 #endif
244 }
245
246 if (kw->readycxx . . $ kore_worker#ready. == 0) {
247 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
248 "worker %d failed to start, shutting down",
249 kw->idcxx . . $ kore_worker#id.);
250
251 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
252 }
253 }
254
255 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
256 }
257
258 struct kore_workercxx . . $ kore_worker# *
259 kore_worker_datacxx . . $ kore_worker_data(a59abc2c9e0c50f4).(u_int8_tcxx . . $ u_int8_t# idx)
260 {
261 if (idx >= worker_countcxx . . $ worker_count.)
262 fatalcxx . . $ fatal(f6bc48f0426f26ee).("idx %u too large for worker count", idx);
263
264 return (WORKERcxx . . $ `src/worker.c:64:9`!(idx));
265 }
266
267 struct kore_workercxx . . $ kore_worker# *
268 kore_worker_data_byidcxx . . $ kore_worker_data_byid(b637c7c675a79ce6).(u_int16_tcxx . . $ u_int16_t# id)
269 {
270 struct kore_workercxx . . $ kore_worker# *kw;
271 u_int16_tcxx . . $ u_int16_t# idx;
272
273 for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) {
274 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
275 if (kw->idcxx . . $ kore_worker#id. == id)
276 return (kw);
277 }
278
279 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
280 }
281
282 void
283 kore_worker_shutdowncxx . . $ kore_worker_shutdown(49f6e7a06ebc5aa8).(void)
284 {
285 struct kore_workercxx . . $ kore_worker# *kw;
286 pid_tcxx . . $ pid_t# pid;
287 int status;
288 u_int16_tcxx . . $ u_int16_t# idx, done;
289
290 if (!kore_quietcxx . . $ kore_quiet.) {
291 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
292 "waiting for workers to drain and shutdown");
293 }
294
295 for (;;) {
296 for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) {
297 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
298 if (kw->runningcxx . . $ kore_worker#running. == 0)
299 continue;
300
301 if (kw->pidcxx . . $ kore_worker#pid. != 0) {
302 pid = waitpidcxx . . $ waitpid(1df93256b606fe1f).(kw->pidcxx . . $ kore_worker#pid., &status, 0);
303 if (pid == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ECHILDcxx . . $ `<external>/8987895939074593727/errno-base.h:14:9`!)
304 continue;
305
306 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
307 kore_seccomp_tracecxx . . $ kore_seccomp_trace(9b79fb6aee4c0440).(kw->pidcxx . . $ kore_worker#pid., status);
308 #endif
309
310 kw->pidcxx . . $ kore_worker#pid. = 0;
311 kw->runningcxx . . $ kore_worker#running. = 0;
312
313 kw->msgcxx . . $ kore_worker#msg.[0]->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. |= KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!;
314 net_recv_flushcxx . . $ net_recv_flush(d3ce87d3c6fa6963).(kw->msgcxx . . $ kore_worker#msg.[0]);
315
316 if (!kore_quietcxx . . $ kore_quiet.) {
317 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
318 "worker %s exited (%d)",
319 kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(kw->idcxx . . $ kore_worker#id.), status);
320 }
321 }
322 }
323
324 done = 0;
325 for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) {
326 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
327 if (kw->runningcxx . . $ kore_worker#running. == 0) {
328 done++;
329 continue;
330 }
331 }
332
333 if (done == worker_countcxx . . $ worker_count.)
334 break;
335 }
336
337 if (shmctlcxx . . $ shmctl(d70580a8cd75f83f).(shm_accept_keycxx . . $ shm_accept_key., IPC_RMIDcxx . . $ `<external>/13393576928612956856/ipc.h:30:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) == -1) {
338 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
339 "failed to deleted shm segment: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
340 }
341 }
342
343 void
344 kore_worker_dispatch_signalcxx . . $ kore_worker_dispatch_signal(d4f767463ce0a6b3).(int sig)
345 {
346 u_int16_tcxx . . $ u_int16_t# idx;
347 struct kore_workercxx . . $ kore_worker# *kw;
348
349 for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) {
350 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
351
352 if (kw->pidcxx . . $ kore_worker#pid. == -1 || kw->pidcxx . . $ kore_worker#pid. == 0)
353 continue;
354
355 if (killcxx . . $ kill(9b79fb6aee4c0440).(kw->pidcxx . . $ kore_worker#pid., sig) == -1) {
356 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "kill(%d, %d): %s",
357 kw->pidcxx . . $ kore_worker#pid., sig, errno_scxx . . $ `include/kore/kore.h:100:9`!);
358 }
359 }
360 }
361
362 void
363 kore_worker_privsepcxx . . $ kore_worker_privsep(49f6e7a06ebc5aa8).(void)
364 {
365 rlim_tcxx . . $ rlim_t# fd;
366 struct rlimitcxx . . $ rlimit# rl;
367 struct passwdcxx . . $ passwd# *pw;
368
369 if (workercxx . . $ worker. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
370 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("%s called with no worker", __func__);
371
372 pw = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
373
374 /* Must happen before chroot. */
375 if (workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_runascxx . . $ kore_privsep#skip_runas. == 0) {
376 if (workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->runascxx . . $ kore_privsep#runas. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
377 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("no runas user given for %s",
378 kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(workercxx . . $ worker.->idcxx . . $ kore_worker#id.));
379 }
380
381 if ((pw = getpwnamcxx . . $ getpwnam(826a6902c684e2bc).(workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->runascxx . . $ kore_privsep#runas.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
382 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot getpwnam(\"%s\") for user: %s",
383 workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->runascxx . . $ kore_privsep#runas., errno_scxx . . $ `include/kore/kore.h:100:9`!);
384 }
385 }
386
387 if (workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_chrootcxx . . $ kore_privsep#skip_chroot. == 0) {
388 if (chrootcxx . . $ chroot(5c4306d8553b3696).(workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->rootcxx . . $ kore_privsep#root.) == -1) {
389 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot chroot(\"%s\"): %s",
390 workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->rootcxx . . $ kore_privsep#root., errno_scxx . . $ `include/kore/kore.h:100:9`!);
391 }
392
393 if (chdircxx . . $ chdir(5c4306d8553b3696).("/") == -1)
394 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot chdir(\"/\"): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
395 } else {
396 if (chdircxx . . $ chdir(5c4306d8553b3696).(workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->rootcxx . . $ kore_privsep#root.) == -1) {
397 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot chdir(\"%s\"): %s",
398 workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->rootcxx . . $ kore_privsep#root., errno_scxx . . $ `include/kore/kore.h:100:9`!);
399 }
400 }
401
402 if (getrlimitcxx . . $ getrlimit(89a93d3463732be0).(RLIMIT_NOFILEcxx . . $ `<external>/1890803886155400780/resource.h:63:9`!, &rl) == -1) {
403 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "getrlimit(RLIMIT_NOFILE): %s", errno_scxx . . $ `include/kore/kore.h:100:9`!);
404 } else {
405 for (fd = 0; fd < rl.rlim_curcxx . . $ rlimit#rlim_cur.; fd++) {
406 if (fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(fd, F_GETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:166:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) != -1) {
407 worker_rlimit_nofilescxx . . $ worker_rlimit_nofiles.++;
408 }
409 }
410 }
411
412 rl.rlim_curcxx . . $ rlimit#rlim_cur. = worker_rlimit_nofilescxx . . $ worker_rlimit_nofiles.;
413 rl.rlim_maxcxx . . $ rlimit#rlim_max. = worker_rlimit_nofilescxx . . $ worker_rlimit_nofiles.;
414 if (setrlimitcxx . . $ setrlimit(506c7cfeadecd456).(RLIMIT_NOFILEcxx . . $ `<external>/1890803886155400780/resource.h:63:9`!, &rl) == -1) {
415 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "setrlimit(RLIMIT_NOFILE, %u): %s",
416 worker_rlimit_nofilescxx . . $ worker_rlimit_nofiles., errno_scxx . . $ `include/kore/kore.h:100:9`!);
417 }
418
419 if (workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_runascxx . . $ kore_privsep#skip_runas. == 0) {
420 if (setgroupscxx . . $ setgroups(5df7bf8b009a4101).(1, &pw->pw_gidcxx . . $ passwd#pw_gid.) ||
421 #if defined(__MACH__) || defined(NetBSD)
422 setgid(pw->pw_gid) || setegid(pw->pw_gid) ||
423 setuid(pw->pw_uid) || seteuid(pw->pw_uid))
424 #else
425 setresgidcxx . . $ setresgid(64d4c609941711dd).(pw->pw_gidcxx . . $ passwd#pw_gid., pw->pw_gidcxx . . $ passwd#pw_gid., pw->pw_gidcxx . . $ passwd#pw_gid.) ||
426 setresuidcxx . . $ setresuid(64d4c609941711dd).(pw->pw_uidcxx . . $ passwd#pw_uid., pw->pw_uidcxx . . $ passwd#pw_uid., pw->pw_uidcxx . . $ passwd#pw_uid.))
427 #endif
428 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("cannot drop privileges (%s)", errno_scxx . . $ `include/kore/kore.h:100:9`!);
429 }
430
431 kore_platform_sandboxcxx . . $ kore_platform_sandbox(49f6e7a06ebc5aa8).();
432 }
433
434 void
435 kore_worker_entrycxx . . $ kore_worker_entry(b12959a5c0a914ff).(struct kore_workercxx . . $ kore_worker# *kw)
436 {
437 struct kore_runtime_callcxx . . $ kore_runtime_call# *sigcall;
438 u_int64_tcxx . . $ u_int64_t# last_seed;
439 int quit, had_lock, sig;
440 u_int64_tcxx . . $ u_int64_t# netwait, now, next_timeo;
441
442 workercxx . . $ worker. = kw;
443
444 if (!kore_foregroundcxx . . $ kore_foreground.)
445 closelogcxx . . $ closelog(49f6e7a06ebc5aa8).();
446
447 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
448 kore_seccomp_tracemecxx . . $ kore_seccomp_traceme(49f6e7a06ebc5aa8).();
449 #endif
450
451 kore_platform_proctitlecxx . . $ kore_platform_proctitle(85c52e162fed56f9).(kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(kw->idcxx . . $ kore_worker#id.));
452
453 if (worker_set_affinitycxx . . $ worker_set_affinity. == 1)
454 kore_platform_worker_setcpucxx . . $ kore_platform_worker_setcpu(8632b877e8ae27d6).(kw);
455
456 kore_pidcxx . . $ kore_pid. = kw->pidcxx . . $ kore_worker#pid.;
457 kore_signal_setupcxx . . $ kore_signal_setup(49f6e7a06ebc5aa8).();
458
459 if (kw->idcxx . . $ kore_worker#id. == KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!) {
460 kore_keymgr_runcxx . . $ kore_keymgr_run(49f6e7a06ebc5aa8).();
461 exitcxx . . $ exit(3ca292c338df9bb3).(0);
462 }
463
464 #if defined(KORE_USE_ACME)
465 if (kw->id == KORE_WORKER_ACME) {
466 kore_acme_run();
467 exit(0);
468 }
469 #endif
470
471 net_initcxx . . $ net_init(49f6e7a06ebc5aa8).();
472 kore_connection_initcxx . . $ kore_connection_init(49f6e7a06ebc5aa8).();
473 kore_platform_event_initcxx . . $ kore_platform_event_init(49f6e7a06ebc5aa8).();
474 kore_msg_worker_initcxx . . $ kore_msg_worker_init(49f6e7a06ebc5aa8).();
475
476 #if defined(KORE_USE_TASKS)
477 kore_task_init();
478 #endif
479
480 kore_worker_privsepcxx . . $ kore_worker_privsep(49f6e7a06ebc5aa8).();
481
482 #if !defined(KORE_NO_HTTP)
483 http_initcxx . . $ http_init(49f6e7a06ebc5aa8).();
484 kore_filemap_resolve_pathscxx . . $ kore_filemap_resolve_paths(49f6e7a06ebc5aa8).();
485 kore_accesslog_worker_initcxx . . $ kore_accesslog_worker_init(49f6e7a06ebc5aa8).();
486 #endif
487 kore_timer_initcxx . . $ kore_timer_init(49f6e7a06ebc5aa8).();
488 kore_fileref_initcxx . . $ kore_fileref_init(49f6e7a06ebc5aa8).();
489 kore_tls_keymgr_initcxx . . $ kore_tls_keymgr_init(49f6e7a06ebc5aa8).();
490
491 quit = 0;
492 had_lock = 0;
493 next_timeo = 0;
494 accept_availcxx . . $ accept_avail. = 1;
495 worker_active_connectionscxx . . $ worker_active_connections. = 0;
496
497 last_seed = 0;
498
499 if (kore_keymgr_activecxx . . $ kore_keymgr_active.) {
500 kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_CRLcxx . . $ `include/kore/kore.h:668:9`!, worker_keymgr_responsecxx . . $ worker_keymgr_response(72fdcea79115518d).);
501 kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_ENTROPY_RESPcxx . . $ `include/kore/kore.h:665:9`!, worker_entropy_recvcxx . . $ worker_entropy_recv(72fdcea79115518d).);
502 kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_CERTIFICATEcxx . . $ `include/kore/kore.h:666:9`!, worker_keymgr_responsecxx . . $ worker_keymgr_response(72fdcea79115518d).);
503
504 if (workercxx . . $ worker.->restartedcxx . . $ kore_worker#restarted.) {
505 kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!,
506 KORE_MSG_CERTIFICATE_REQcxx . . $ `include/kore/kore.h:667:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
507 }
508 #if defined(KORE_USE_ACME)
509 kore_msg_register(KORE_ACME_CHALLENGE_SET_CERT,
510 worker_keymgr_response);
511 kore_msg_register(KORE_ACME_CHALLENGE_CLEAR_CERT,
512 worker_keymgr_response);
513 #endif
514 }
515
516 kore_msg_registercxx . . $ kore_msg_register(96a8e3ca44e422f9).(KORE_MSG_ACCEPT_AVAILABLEcxx . . $ `include/kore/kore.h:669:9`!, worker_accept_availcxx . . $ worker_accept_avail(72fdcea79115518d).);
517
518 if (nlistenerscxx . . $ nlisteners. == 0)
519 worker_no_lockcxx . . $ worker_no_lock. = 1;
520
521 worker_runtime_configurecxx . . $ worker_runtime_configure(49f6e7a06ebc5aa8).();
522
523 kore_module_onloadcxx . . $ kore_module_onload(49f6e7a06ebc5aa8).();
524 kore_domain_callbackcxx . . $ kore_domain_callback(2a5b70316e33bad0).(worker_domain_checkcxx . . $ worker_domain_check(be08e46f67814b96).);
525
526 kore_worker_startedcxx . . $ kore_worker_started(49f6e7a06ebc5aa8).();
527 workercxx . . $ worker.->restartedcxx . . $ kore_worker#restarted. = 0;
528
529 sigcall = worker_runtime_signalcxx . . $ worker_runtime_signal(7a0c5ebe46220834).();
530
531 for (;;) {
532 now = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).();
533
534 if (kore_keymgr_activecxx . . $ kore_keymgr_active. &&
535 (now - last_seed) > KORE_RESEED_TIMEcxx . . $ `include/kore/kore.h:98:9`!) {
536 kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`!,
537 KORE_MSG_ENTROPY_REQcxx . . $ `include/kore/kore.h:664:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
538 last_seed = now;
539 }
540
541 if (!workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. && accept_availcxx . . $ accept_avail.) {
542 if (worker_acceptlock_obtaincxx . . $ worker_acceptlock_obtain(b126dc7c1de90089).()) {
543 accept_availcxx . . $ accept_avail. = 0;
544 if (had_lock == 0) {
545 kore_platform_enable_acceptcxx . . $ kore_platform_enable_accept(49f6e7a06ebc5aa8).();
546 had_lock = 1;
547 }
548 }
549 }
550
551 netwait = kore_timer_next_runcxx . . $ kore_timer_next_run(ecdaa1632f49f591).(now);
552
553 if (netwait == KORE_WAIT_INFINITEcxx . . $ `include/kore/kore.h:97:9`!) {
554 if (sig_recvcxx . . $ sig_recv. != 0)
555 netwait = 10;
556 #if !defined(KORE_NO_HTTP)
557 if (http_request_countcxx . . $ http_request_count. > 0)
558 netwait = 100;
559 #endif
560 }
561
562 #if defined(KORE_USE_PYTHON)
563 if (kore_python_coro_pending())
564 netwait = 0;
565 #endif
566
567 kore_platform_event_waitcxx . . $ kore_platform_event_wait(2c44cd906ae4e829).(netwait);
568 now = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).();
569
570 if (workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock.)
571 worker_acceptlock_releasecxx . . $ worker_acceptlock_release(49f6e7a06ebc5aa8).();
572
573 if (!workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock.) {
574 if (had_lock == 1) {
575 had_lock = 0;
576 kore_platform_disable_acceptcxx . . $ kore_platform_disable_accept(49f6e7a06ebc5aa8).();
577 }
578 }
579
580 sig = sig_recvcxx . . $ sig_recv.;
581 if (sig != 0) {
582 switch (sig) {
583 case SIGHUPcxx . . $ `<external>/11773118241360591791/signum-generic.h:56:9`!:
584 kore_module_reloadcxx . . $ kore_module_reload(d4f767463ce0a6b3).(1);
585 break;
586 case SIGQUITcxx . . $ `<external>/11773118241360591791/signum-generic.h:57:9`!:
587 case SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!:
588 case SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`!:
589 quit = 1;
590 break;
591 case SIGCHLDcxx . . $ `<external>/1457673850468744829/signum-arch.h:41:9`!:
592 #if defined(KORE_USE_PYTHON)
593 kore_python_proc_reap();
594 #endif
595 break;
596 default:
597 break;
598 }
599
600 if (sigcall != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
601 kore_runtime_signalcxx . . $ kore_runtime_signal(dd2e1fd639e9b421).(sigcall, sig);
602
603 if (sig == sig_recvcxx . . $ sig_recv.)
604 sig_recvcxx . . $ sig_recv. = 0;
605 }
606
607 if (quit)
608 break;
609
610 kore_timer_runcxx . . $ kore_timer_run(2c44cd906ae4e829).(now);
611 #if defined(KORE_USE_CURL)
612 kore_curl_run_scheduled();
613 kore_curl_do_timeout();
614 #endif
615 #if !defined(KORE_NO_HTTP)
616 http_processcxx . . $ http_process(49f6e7a06ebc5aa8).();
617 #endif
618 #if defined(KORE_USE_PYTHON)
619 kore_python_coro_run();
620 #endif
621 if (next_timeo <= now) {
622 kore_connection_check_timeoutcxx . . $ kore_connection_check_timeout(2c44cd906ae4e829).(now);
623 next_timeo = now + 500;
624 }
625
626 kore_connection_prunecxx . . $ kore_connection_prune(d4f767463ce0a6b3).(KORE_CONNECTION_PRUNE_DISCONNECTcxx . . $ `include/kore/kore.h:214:9`!);
627 }
628
629 worker_runtime_teardowncxx . . $ worker_runtime_teardown(49f6e7a06ebc5aa8).();
630 kore_server_cleanupcxx . . $ kore_server_cleanup(49f6e7a06ebc5aa8).();
631
632 kore_platform_event_cleanupcxx . . $ kore_platform_event_cleanup(49f6e7a06ebc5aa8).();
633 kore_connection_cleanupcxx . . $ kore_connection_cleanup(49f6e7a06ebc5aa8).();
634 kore_domain_cleanupcxx . . $ kore_domain_cleanup(49f6e7a06ebc5aa8).();
635 kore_tls_cleanupcxx . . $ kore_tls_cleanup(49f6e7a06ebc5aa8).();
636 kore_module_cleanupcxx . . $ kore_module_cleanup(49f6e7a06ebc5aa8).();
637 #if !defined(KORE_NO_HTTP)
638 http_cleanupcxx . . $ http_cleanup(49f6e7a06ebc5aa8).();
639 #endif
640 net_cleanupcxx . . $ net_cleanup(49f6e7a06ebc5aa8).();
641
642 #if defined(KORE_USE_PYTHON)
643 kore_python_cleanup();
644 #endif
645
646 #if defined(KORE_USE_PGSQL)
647 kore_pgsql_sys_cleanup();
648 #endif
649
650 kore_mem_cleanupcxx . . $ kore_mem_cleanup(49f6e7a06ebc5aa8).();
651 exitcxx . . $ exit(3ca292c338df9bb3).(0);
652 }
653
654 void
655 kore_worker_reapcxx . . $ kore_worker_reap(49f6e7a06ebc5aa8).(void)
656 {
657 pid_tcxx . . $ pid_t# pid;
658 int status;
659
660 for (;;) {
661 pid = waitpidcxx . . $ waitpid(1df93256b606fe1f).(-worker_pgrpcxx . . $ worker_pgrp., &status, WNOHANGcxx . . $ `<external>/1585314143251398045/waitflags.h:25:9`!);
662
663 if (pid == -1) {
664 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ECHILDcxx . . $ `<external>/8987895939074593727/errno-base.h:14:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!) {
665 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!,
666 "%s: waitpid(): %s", __func__, errno_scxx . . $ `include/kore/kore.h:100:9`!);
667 }
668 break;
669 }
670
671 if (pid == 0)
672 break;
673
674 worker_reapercxx . . $ worker_reaper(3e454b8ccf442868).(pid, status);
675 }
676 }
677
678 void
679 kore_worker_make_busycxx . . $ kore_worker_make_busy(49f6e7a06ebc5aa8).(void)
680 {
681 if (worker_countcxx . . $ worker_count. == WORKER_SOLO_COUNTcxx . . $ `src/worker.c:62:9`! || worker_no_lockcxx . . $ worker_no_lock. == 1)
682 return;
683
684 if (workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock.) {
685 worker_unlockcxx . . $ worker_unlock(49f6e7a06ebc5aa8).();
686 workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. = 0;
687 kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_MSG_WORKER_ALLcxx . . $ `include/kore/kore.h:680:9`!,
688 KORE_MSG_ACCEPT_AVAILABLEcxx . . $ `include/kore/kore.h:669:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
689 }
690 }
691
692 int
693 kore_worker_keymgr_response_verifycxx . . $ kore_worker_keymgr_response_verify(8c8125758631253f).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data,
694 struct kore_domaincxx . . $ kore_domain# **out)
695 {
696 struct kore_servercxx . . $ kore_server# *srv;
697 struct kore_domaincxx . . $ kore_domain# *dom;
698 const struct kore_x509_msgcxx . . $ kore_x509_msg# *req;
699
700 if (msg->lengthcxx . . $ kore_msg#length. < sizeof(*req)) {
701 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!,
702 "short keymgr message (%zu)", msg->lengthcxx . . $ kore_msg#length.);
703 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
704 }
705
706 req = (const struct kore_x509_msgcxx . . $ kore_x509_msg# *)data;
707 if (msg->lengthcxx . . $ kore_msg#length. != (sizeof(*req) + req->data_lencxx . . $ kore_x509_msg#data_len.)) {
708 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!,
709 "invalid keymgr payload (%zu)", msg->lengthcxx . . $ kore_msg#length.);
710 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
711 }
712
713 if (req->domaincxx . . $ kore_x509_msg#domain.[KORE_DOMAINNAME_LENcxx . . $ `include/kore/kore.h:102:9`!] != '\0') {
714 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "domain not NUL-terminated");
715 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
716
717 }
718
719 if (out == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
720 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
721
722 dom = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
723
724 LIST_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:134:9`!(srv, &kore_servers, list) {
725 dom = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
726
727 if (srv->tlscxx . . $ kore_server#tls. == 0)
728 continue;
729
730 TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(dom, &srv->domains, list) {
731 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(dom->domaincxx . . $ kore_domain#domain., req->domaincxx . . $ kore_x509_msg#domain.))
732 break;
733 }
734
735 if (dom != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
736 break;
737 }
738
739 if (dom == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
740 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!,
741 "got keymgr response for domain that does not exist");
742 return (KORE_RESULT_ERRORcxx . . $ `include/kore/kore.h:87:9`!);
743 }
744
745 *out = dom;
746
747 return (KORE_RESULT_OKcxx . . $ `include/kore/kore.h:88:9`!);
748 }
749
750 void
751 kore_worker_startedcxx . . $ kore_worker_started(49f6e7a06ebc5aa8).(void)
752 {
753 const char *chroot;
754
755 if (workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_chrootcxx . . $ kore_privsep#skip_chroot.)
756 chroot = "root";
757 else
758 chroot = "chroot";
759
760 if (!kore_quietcxx . . $ kore_quiet.) {
761 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
762 "started (#%d %s=%s%s%s)",
763 getpidcxx . . $ getpid(b126dc7c1de90089).(), chroot, workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->rootcxx . . $ kore_privsep#root.,
764 workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_runascxx . . $ kore_privsep#skip_runas. ? "" : " user=",
765 workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->skip_runascxx . . $ kore_privsep#skip_runas. ? "" : workercxx . . $ worker.->pscxx . . $ kore_worker#ps.->runascxx . . $ kore_privsep#runas.);
766 }
767
768 workercxx . . $ worker.->readycxx . . $ kore_worker#ready. = 1;
769 }
770
771 static void
772 worker_runtime_configurecxx . . $ worker_runtime_configure(49f6e7a06ebc5aa8).(void)
773 {
774 struct kore_runtime_callcxx . . $ kore_runtime_call# *rcall;
775
776 rcall = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
777
778 #if defined(KORE_USE_PYTHON)
779 rcall = kore_runtime_getcall(KORE_PYTHON_WORKER_START_HOOK);
780 #endif
781
782 if (rcall == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
783 rcall = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).("kore_worker_configure");
784
785 if (rcall != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
786 kore_runtime_executecxx . . $ kore_runtime_execute(5bf3e738821b3e09).(rcall);
787 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rcall);
788 }
789 }
790
791 static struct kore_runtime_callcxx . . $ kore_runtime_call# *
792 worker_runtime_signalcxx . . $ worker_runtime_signal(7a0c5ebe46220834).(void)
793 {
794 struct kore_runtime_callcxx . . $ kore_runtime_call# *rcall;
795
796 rcall = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
797
798 #if defined(KORE_USE_PYTHON)
799 rcall = kore_runtime_getcall(KORE_PYTHON_SIGNAL_HOOK);
800 #endif
801
802 if (rcall == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
803 rcall = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).("kore_worker_signal");
804
805 return (rcall);
806 }
807
808 static void
809 worker_runtime_teardowncxx . . $ worker_runtime_teardown(49f6e7a06ebc5aa8).(void)
810 {
811 struct kore_runtime_callcxx . . $ kore_runtime_call# *rcall;
812
813 rcall = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
814
815 #if defined(KORE_USE_PYTHON)
816 rcall = kore_runtime_getcall(KORE_PYTHON_WORKER_STOP_HOOK);
817 #endif
818
819 if (rcall == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
820 rcall = kore_runtime_getcallcxx . . $ kore_runtime_getcall(b514b5951ac4d62f).("kore_worker_teardown");
821
822 if (rcall != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
823 kore_runtime_executecxx . . $ kore_runtime_execute(5bf3e738821b3e09).(rcall);
824 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(rcall);
825 }
826 }
827
828 static void
829 worker_domain_checkcxx . . $ worker_domain_check(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *dom)
830 {
831 struct statcxx . . $ stat# st;
832
833 if (dom->cafilecxx . . $ kore_domain#cafile. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
834 if (statcxx . . $ stat(a9cd1e23e96f423c).(dom->cafilecxx . . $ kore_domain#cafile., &st) == -1)
835 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("'%s': %s", dom->cafilecxx . . $ kore_domain#cafile., errno_scxx . . $ `include/kore/kore.h:100:9`!);
836 if (accesscxx . . $ access(cb7559238bb26814).(dom->cafilecxx . . $ kore_domain#cafile., R_OKcxx . . $ `<external>/12628407106812493757/unistd.h:281:9`!) == -1)
837 fatalxcxx . . $ fatalx(f6bc48f0426f26ee).("'%s': not readable", dom->cafilecxx . . $ kore_domain#cafile.);
838 }
839 }
840
841 static void
842 worker_reapercxx . . $ worker_reaper(3e454b8ccf442868).(pid_tcxx . . $ pid_t# pid, int status)
843 {
844 u_int16_tcxx . . $ u_int16_t# idx;
845 struct kore_workercxx . . $ kore_worker# *kw;
846 const char *func;
847
848 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
849 if (kore_seccomp_tracecxx . . $ kore_seccomp_trace(9b79fb6aee4c0440).(pid, status))
850 return;
851 #endif
852
853 for (idx = 0; idx < worker_countcxx . . $ worker_count.; idx++) {
854 kw = WORKERcxx . . $ `src/worker.c:64:9`!(idx);
855 if (kw->pidcxx . . $ kore_worker#pid. != pid)
856 continue;
857
858 kw->msgcxx . . $ kore_worker#msg.[0]->evtcxx . . $ connection#evt..flagscxx . . $ kore_event#flags. |= KORE_EVENT_READcxx . . $ `include/kore/kore.h:187:9`!;
859 net_recv_flushcxx . . $ net_recv_flush(d3ce87d3c6fa6963).(kw->msgcxx . . $ kore_worker#msg.[0]);
860
861 if (!kore_quietcxx . . $ kore_quiet.) {
862 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
863 "worker %s (%d) exited with status %d",
864 kore_worker_namecxx . . $ kore_worker_name(f7ae7e56280f18ae).(kw->idcxx . . $ kore_worker#id.), pid, status);
865 }
866
867 kw->runningcxx . . $ kore_worker#running. = 0;
868
869 if (WIFEXITEDcxx . . $ `<external>/3791503621068212355/wait.h:57:10`!(status) && WEXITSTATUScxx . . $ `<external>/3791503621068212355/wait.h:54:10`!(status) == 0) {
870 kw->pidcxx . . $ kore_worker#pid. = 0;
871 break;
872 }
873
874 func = "none";
875 #if !defined(KORE_NO_HTTP)
876 if (kw->active_routecxx . . $ kore_worker#active_route. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
877 func = kw->active_routecxx . . $ kore_worker#active_route.->funccxx . . $ kore_route#func.;
878 #endif
879 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
880 "worker %d (pid: %d) (hdlr: %s) gone",
881 kw->idcxx . . $ kore_worker#id., kw->pidcxx . . $ kore_worker#pid., func);
882
883 #if defined(__linux__cxx . . $ `<built-in>:401:9`!)
884 if (WIFSIGNALEDcxx . . $ `<external>/3791503621068212355/wait.h:58:10`!(status) && WTERMSIGcxx . . $ `<external>/3791503621068212355/wait.h:55:10`!(status) == SIGSYScxx . . $ `<external>/1457673850468744829/signum-arch.h:34:9`!) {
885 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
886 "worker %d died from sandbox violation", kw->idcxx . . $ kore_worker#id.);
887 }
888 #endif
889
890 if (kw->idcxx . . $ kore_worker#id. == KORE_WORKER_KEYMGRcxx . . $ `include/kore/kore.h:652:9`! ||
891 kw->idcxx . . $ kore_worker#id. == KORE_WORKER_ACMEcxx . . $ `include/kore/kore.h:653:9`!) {
892 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_CRITcxx . . $ `<external>/6168640806541382331/syslog.h:53:9`!,
893 "keymgr or acme process gone, stopping");
894 kw->pidcxx . . $ kore_worker#pid. = 0;
895 kore_quitcxx . . $ kore_quit. = KORE_QUIT_FATALcxx . . $ `include/kore/kore.h:85:9`!;
896 break;
897 }
898
899 if (kw->pidcxx . . $ kore_worker#pid. == accept_lockcxx . . $ accept_lock.->currentcxx . . $ wlock#current. &&
900 worker_no_lockcxx . . $ worker_no_lock. == 0)
901 worker_unlockcxx . . $ worker_unlock(49f6e7a06ebc5aa8).();
902
903 #if !defined(KORE_NO_HTTP)
904 if (kw->active_routecxx . . $ kore_worker#active_route. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
905 kw->active_routecxx . . $ kore_worker#active_route.->errorscxx . . $ kore_route#errors.++;
906 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
907 "hdlr %s has caused %d error(s)",
908 kw->active_routecxx . . $ kore_worker#active_route.->funccxx . . $ kore_route#func.,
909 kw->active_routecxx . . $ kore_worker#active_route.->errorscxx . . $ kore_route#errors.);
910 }
911 #endif
912
913 if (worker_policycxx . . $ worker_policy. == KORE_WORKER_POLICY_TERMINATEcxx . . $ `include/kore/kore.h:657:9`!) {
914 kw->pidcxx . . $ kore_worker#pid. = 0;
915 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
916 "worker policy is 'terminate', stopping");
917 kore_quitcxx . . $ kore_quit. = KORE_QUIT_FATALcxx . . $ `include/kore/kore.h:85:9`!;
918 break;
919 }
920
921 if (kore_quitcxx . . $ kore_quit. == KORE_QUIT_NONEcxx . . $ `include/kore/kore.h:83:9`!) {
922 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "restarting worker %d", kw->idcxx . . $ kore_worker#id.);
923 kw->restartedcxx . . $ kore_worker#restarted. = 1;
924 kore_msg_parent_removecxx . . $ kore_msg_parent_remove(8632b877e8ae27d6).(kw);
925
926 if (!kore_worker_spawncxx . . $ kore_worker_spawn(953416d95ce153a1).(idx, kw->idcxx . . $ kore_worker#id., kw->cpucxx . . $ kore_worker#cpu.)) {
927 kore_quitcxx . . $ kore_quit. = KORE_QUIT_FATALcxx . . $ `include/kore/kore.h:85:9`!;
928 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "failed to restart worker");
929 } else {
930 kore_msg_parent_addcxx . . $ kore_msg_parent_add(8632b877e8ae27d6).(kw);
931 }
932
933 break;
934 }
935 }
936 }
937
938 static inline void
939 worker_acceptlock_releasecxx . . $ worker_acceptlock_release(49f6e7a06ebc5aa8).(void)
940 {
941 if (worker_countcxx . . $ worker_count. == WORKER_SOLO_COUNTcxx . . $ `src/worker.c:62:9`! || worker_no_lockcxx . . $ worker_no_lock. == 1)
942 return;
943
944 if (workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. != 1)
945 return;
946
947 if (worker_active_connectionscxx . . $ worker_active_connections. < worker_max_connectionscxx . . $ worker_max_connections.) {
948 #if !defined(KORE_NO_HTTP)
949 if (http_request_countcxx . . $ http_request_count. < http_request_limitcxx . . $ http_request_limit.)
950 return;
951 #else
952 return;
953 #endif
954 }
955
956 #if defined(WORKER_DEBUG)
957 kore_log(LOG_DEBUG, "worker busy, releasing lock");
958 #endif
959
960 worker_unlockcxx . . $ worker_unlock(49f6e7a06ebc5aa8).();
961 workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. = 0;
962
963 kore_msg_sendcxx . . $ kore_msg_send(6afaa3cccbe95a50).(KORE_MSG_WORKER_ALLcxx . . $ `include/kore/kore.h:680:9`!, KORE_MSG_ACCEPT_AVAILABLEcxx . . $ `include/kore/kore.h:669:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
964 }
965
966 static inline int
967 worker_acceptlock_obtaincxx . . $ worker_acceptlock_obtain(b126dc7c1de90089).(void)
968 {
969 int r;
970
971 if (workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. == 1)
972 return (1);
973
974 if (worker_countcxx . . $ worker_count. == WORKER_SOLO_COUNTcxx . . $ `src/worker.c:62:9`! || worker_no_lockcxx . . $ worker_no_lock. == 1) {
975 workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. = 1;
976 return (1);
977 }
978
979 if (worker_active_connectionscxx . . $ worker_active_connections. >= worker_max_connectionscxx . . $ worker_max_connections.)
980 return (0);
981
982 #if !defined(KORE_NO_HTTP)
983 if (http_request_countcxx . . $ http_request_count. >= http_request_limitcxx . . $ http_request_limit.)
984 return (0);
985 #endif
986
987 r = 0;
988 if (worker_trylockcxx . . $ worker_trylock(b126dc7c1de90089).()) {
989 r = 1;
990 workercxx . . $ worker.->has_lockcxx . . $ kore_worker#has_lock. = 1;
991 #if defined(WORKER_DEBUG)
992 kore_log(LOG_DEBUG, "got lock");
993 #endif
994 }
995
996 return (r);
997 }
998
999 static int
1000 worker_trylockcxx . . $ worker_trylock(b126dc7c1de90089).(void)
1001 {
1002 if (!__sync_bool_compare_and_swapcxx . . $ __sync_bool_compare_and_swap_4(2bb2bf1fe70f5e29).(&(accept_lockcxx . . $ accept_lock.->lockcxx . . $ wlock#lock.), 0, 1))
1003 return (0);
1004
1005 accept_lockcxx . . $ accept_lock.->currentcxx . . $ wlock#current. = workercxx . . $ worker.->pidcxx . . $ kore_worker#pid.;
1006
1007 return (1);
1008 }
1009
1010 static void
1011 worker_unlockcxx . . $ worker_unlock(49f6e7a06ebc5aa8).(void)
1012 {
1013 accept_lockcxx . . $ accept_lock.->currentcxx . . $ wlock#current. = 0;
1014 if (!__sync_bool_compare_and_swapcxx . . $ __sync_bool_compare_and_swap_4(2bb2bf1fe70f5e29).(&(accept_lockcxx . . $ accept_lock.->lockcxx . . $ wlock#lock.), 1, 0))
1015 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!, "worker_unlock(): wasn't locked");
1016 }
1017
1018 static void
1019 worker_accept_availcxx . . $ worker_accept_avail(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data)
1020 {
1021 accept_availcxx . . $ accept_avail. = 1;
1022 }
1023
1024 static void
1025 worker_entropy_recvcxx . . $ worker_entropy_recv(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data)
1026 {
1027 if (msg->lengthcxx . . $ kore_msg#length. != 1024) {
1028 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!,
1029 "invalid entropy response (got:%zu - wanted:1024)",
1030 msg->lengthcxx . . $ kore_msg#length.);
1031 }
1032
1033 kore_tls_seedcxx . . $ kore_tls_seed(8c7266571c1d65cc).(data, msg->lengthcxx . . $ kore_msg#length.);
1034 }
1035
1036 static void
1037 worker_keymgr_responsecxx . . $ worker_keymgr_response(72fdcea79115518d).(struct kore_msgcxx . . $ kore_msg# *msg, const void *data)
1038 {
1039 struct kore_domaincxx . . $ kore_domain# *dom;
1040 const struct kore_x509_msgcxx . . $ kore_x509_msg# *req;
1041
1042 if (!kore_worker_keymgr_response_verifycxx . . $ kore_worker_keymgr_response_verify(8c8125758631253f).(msg, data, &dom))
1043 return;
1044
1045 req = (const struct kore_x509_msgcxx . . $ kore_x509_msg# *)data;
1046
1047 switch (msg->idcxx . . $ kore_msg#id.) {
1048 case KORE_MSG_CERTIFICATEcxx . . $ `include/kore/kore.h:666:9`!:
1049 kore_tls_domain_setupcxx . . $ kore_tls_domain_setup(c3871c97dc621353).(dom, KORE_PEM_CERT_CHAINcxx . . $ `include/kore/kore.h:123:9`!,
1050 req->datacxx . . $ kore_x509_msg#data., req->data_lencxx . . $ kore_x509_msg#data_len.);
1051 break;
1052 case KORE_MSG_CRLcxx . . $ `include/kore/kore.h:668:9`!:
1053 kore_tls_domain_crlcxx . . $ kore_tls_domain_crl(c5a41c3f71e20c15).(dom, req->datacxx . . $ kore_x509_msg#data., req->data_lencxx . . $ kore_x509_msg#data_len.);
1054 break;
1055 #if defined(KORE_USE_ACME)
1056 case KORE_ACME_CHALLENGE_SET_CERT:
1057 if (dom->tls_ctx == NULL) {
1058 kore_tls_domain_setup(dom, KORE_DER_CERT_DATA,
1059 req->data, req->data_len);
1060 }
1061
1062 kore_free(dom->acme_cert);
1063 dom->acme_cert_len = req->data_len;
1064 dom->acme_cert = kore_calloc(1, req->data_len);
1065 memcpy(dom->acme_cert, req->data, req->data_len);
1066
1067 kore_log(LOG_NOTICE, "[%s] tls-alpn-01 challenge active",
1068 dom->domain);
1069 dom->acme_challenge = 1;
1070 break;
1071 case KORE_ACME_CHALLENGE_CLEAR_CERT:
1072 dom->acme_cert_len = 0;
1073 dom->acme_challenge = 0;
1074
1075 kore_free(dom->acme_cert);
1076 dom->acme_cert = NULL;
1077
1078 kore_log(LOG_NOTICE, "[%s] tls-alpn-01 challenge disabled",
1079 dom->domain);
1080 break;
1081 #endif
1082 default:
1083 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!, "unknown keymgr request %u", msg->idcxx . . $ kore_msg#id.);
1084 break;
1085 }
1086 }