Debian Codegraph @

src/accesslog.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/types.h>
18 #include <sys/socket.h>
19
20 #include <time.h>
21 #include <inttypes.h>
22 #include <signal.h>
23
24 #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/
25 #include "http.h"cxx . . $ `<file>/include/kore/http.h`/
26
27 /*
28 * The worker will write accesslogs to its worker data structure which is
29 * held in shared memory.
30 *
31 * Each accesslog is prefixed with the internal domain ID (2 bytes) and
32 * the length of the log entry (2 bytes) (packed in kore_alog_header).
33 *
34 * The parent will every 10ms fetch the produced accesslogs from the workers
35 * and copy them to its own log buffer. Once this log buffer becomes full
36 * or 1 second has passed the parent will parse the logs and append them
37 * to the correct domain logbuffer which is eventually flushed to disk.
38 */
39
40 #define LOGBUF_SIZEcxx . . $ `src/accesslog.c:40:9`! (KORE_ACCESSLOG_BUFLENcxx . . $ `include/kore/kore.h:445:9`! * worker_count)
41 #define DOMAIN_LOGBUF_LENcxx . . $ `src/accesslog.c:41:9`! (1024 * 1024)
42 #define LOG_ENTRY_MINSIZE_GUESScxx . . $ `src/accesslog.c:42:9`! 90
43
44 static void accesslog_lockcxx . . $ accesslog_lock(8632b877e8ae27d6).(struct kore_workercxx . . $ kore_worker# *);
45 static void accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(struct kore_workercxx . . $ kore_worker# *);
46 static void accesslog_flush_cbcxx . . $ accesslog_flush_cb(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *);
47 static void accesslog_flushcxx . . $ accesslog_flush(69b0378392360ff9).(struct kore_domaincxx . . $ kore_domain# *, u_int64_tcxx . . $ u_int64_t#, int);
48
49 static u_int64_tcxx . . $ u_int64_t# time_cachecxx . . $ time_cache. = 0;
50 static char tbufcxx . . $ tbuf.[128] = { '\0' };
51
52 static struct kore_bufcxx . . $ kore_buf# *logbufcxx . . $ logbuf. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
53
54 void
55 kore_accesslog_worker_initcxx . . $ kore_accesslog_worker_init(49f6e7a06ebc5aa8).(void)
56 {
57 kore_domain_closelogscxx . . $ kore_domain_closelogs(49f6e7a06ebc5aa8).();
58 }
59
60 void
61 kore_accesslogcxx . . $ kore_accesslog(322c51a23f687f95).(struct http_requestcxx . . $ http_request# *req)
62 {
63 struct timespeccxx . . $ timespec# ts;
64 struct tmcxx . . $ tm# *tm;
65 u_int64_tcxx . . $ u_int64_t# now;
66 struct kore_alog_headercxx . . $ kore_alog_header# *hdr;
67 size_t avail;
68 time_tcxx . . $ time_t# curtime;
69 int len, attempts;
70 char addr[INET6_ADDRSTRLENcxx . . $ `<external>/9581057695539925927/in.h:245:9`!], *cn_value;
71 const char *ptr, *method, *http_version, *cn, *referer;
72
73 switch (req->methodcxx . . $ http_request#method.) {
74 case HTTP_METHOD_GETcxx . . $ `include/kore/http.h:229:9`!:
75 method = "GET";
76 break;
77 case HTTP_METHOD_POSTcxx . . $ `include/kore/http.h:230:9`!:
78 method = "POST";
79 break;
80 case HTTP_METHOD_PUTcxx . . $ `include/kore/http.h:231:9`!:
81 method = "PUT";
82 break;
83 case HTTP_METHOD_DELETEcxx . . $ `include/kore/http.h:232:9`!:
84 method = "DELETE";
85 break;
86 case HTTP_METHOD_HEADcxx . . $ `include/kore/http.h:233:9`!:
87 method = "HEAD";
88 break;
89 case HTTP_METHOD_PATCHcxx . . $ `include/kore/http.h:235:9`!:
90 method = "PATCH";
91 break;
92 default:
93 method = "UNKNOWN";
94 break;
95 }
96
97 if (req->flagscxx . . $ http_request#flags. & HTTP_VERSION_1_0cxx . . $ `include/kore/http.h:250:9`!)
98 http_version = "HTTP/1.0";
99 else
100 http_version = "HTTP/1.1";
101
102 if (req->referercxx . . $ http_request#referer. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
103 referer = req->referercxx . . $ http_request#referer.;
104 else
105 referer = "-";
106
107 if (req->agentcxx . . $ http_request#agent. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
108 req->agentcxx . . $ http_request#agent. = "-";
109
110 cn = "-";
111 cn_value = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
112
113 if (req->ownercxx . . $ http_request#owner.->tls_certcxx . . $ connection#tls_cert. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
114 if (kore_x509_subject_namecxx . . $ kore_x509_subject_name(9d8cf00306b7b334).(req->ownercxx . . $ http_request#owner., &cn_value,
115 KORE_X509_COMMON_NAME_ONLYcxx . . $ `include/kore/kore.h:121:9`!))
116 cn = cn_value;
117 }
118
119 switch (req->ownercxx . . $ http_request#owner.->familycxx . . $ connection#family.) {
120 case AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!:
121 ptr = inet_ntopcxx . . $ inet_ntop(6a923972b45bea03).(req->ownercxx . . $ http_request#owner.->familycxx . . $ connection#family.,
122 &(req->ownercxx . . $ http_request#owner.->addrcxx . . $ connection#addr..ipv4cxx . . $ connection#$anonymous_type_2#ipv4..sin_addrcxx . . $ sockaddr_in#sin_addr.), addr, sizeof(addr));
123 break;
124 case AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!:
125 ptr = inet_ntopcxx . . $ inet_ntop(6a923972b45bea03).(req->ownercxx . . $ http_request#owner.->familycxx . . $ connection#family.,
126 &(req->ownercxx . . $ http_request#owner.->addrcxx . . $ connection#addr..ipv6cxx . . $ connection#$anonymous_type_2#ipv6..sin6_addrcxx . . $ sockaddr_in6#sin6_addr.), addr, sizeof(addr));
127 break;
128 case AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!:
129 ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
130 break;
131 default:
132 fatalcxx . . $ fatal(f6bc48f0426f26ee).("unknown family %d", req->ownercxx . . $ http_request#owner.->familycxx . . $ connection#family.);
133 }
134
135 if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
136 addr[0] = '-';
137 addr[1] = '\0';
138 }
139
140 now = kore_time_mscxx . . $ kore_time_ms(f708dce9a8856c70).();
141 if ((now - time_cachecxx . . $ time_cache.) >= 1000) {
142 timecxx . . $ time(7f38134db6521d23).(&curtime);
143 tm = localtimecxx . . $ localtime(9477a1348abbbc5).(&curtime);
144 (void)strftimecxx . . $ strftime(98e75925167c4f1b).(tbufcxx . . $ tbuf., sizeof(tbufcxx . . $ tbuf.), "%d/%b/%Y:%H:%M:%S %z", tm);
145 time_cachecxx . . $ time_cache. = now;
146 }
147
148 attempts = 0;
149 ts.tv_seccxx . . $ timespec#tv_sec. = 0;
150 ts.tv_nseccxx . . $ timespec#tv_nsec. = 1000000;
151
152 for (;;) {
153 if (attempts++ > 1000) {
154 if (getppidcxx . . $ getppid(b126dc7c1de90089).() == 1) {
155 if (killcxx . . $ kill(9b79fb6aee4c0440).(workercxx . . $ worker.->pidcxx . . $ kore_worker#pid., SIGQUITcxx . . $ `<external>/11773118241360591791/signum-generic.h:57:9`!) == -1)
156 fatalcxx . . $ fatal(f6bc48f0426f26ee).("failed to shutdown");
157 return;
158 }
159
160 attempts = 0;
161 }
162
163 accesslog_lockcxx . . $ accesslog_lock(8632b877e8ae27d6).(workercxx . . $ worker.);
164
165 avail = KORE_ACCESSLOG_BUFLENcxx . . $ `include/kore/kore.h:445:9`! - workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset.;
166 if (avail < sizeof(*hdr) + LOG_ENTRY_MINSIZE_GUESScxx . . $ `src/accesslog.c:42:9`!) {
167 accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(workercxx . . $ worker.);
168 nanosleepcxx . . $ nanosleep(645c6fc2576d4126).(&ts, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
169 continue;
170 }
171
172 hdr = (struct kore_alog_headercxx . . $ kore_alog_header# *)
173 (workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..bufcxx . . $ kore_worker#$anonymous_type_17#buf. + workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset.);
174 workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. += sizeof(*hdr);
175
176 len = snprintfcxx . . $ snprintf(ea502d73e74c754c).(workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..bufcxx . . $ kore_worker#$anonymous_type_17#buf. + workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset., avail,
177 "%s - %s [%s] \"%s %s %s\" %d %" PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:105:10`!" \"%s\" \"%s\"\n",
178 addr, cn, tbufcxx . . $ tbuf., method, req->pathcxx . . $ http_request#path., http_version,
179 req->statuscxx . . $ http_request#status., req->content_lengthcxx . . $ http_request#content_length., referer, req->agentcxx . . $ http_request#agent.);
180 if (len == -1)
181 fatalcxx . . $ fatal(f6bc48f0426f26ee).("failed to create log entry");
182
183 if ((size_t)len >= avail) {
184 workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. -= sizeof(*hdr);
185 accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(workercxx . . $ worker.);
186 nanosleepcxx . . $ nanosleep(645c6fc2576d4126).(&ts, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
187 continue;
188 }
189
190 if ((size_t)len > USHRT_MAXcxx . . $ `<external>/67164224788392828/limits.h:113:10`!) {
191 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_WARNINGcxx . . $ `<external>/6168640806541382331/syslog.h:55:9`!,
192 "log entry length exceeds limit (%d)", len);
193 workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. -= sizeof(*hdr);
194 break;
195 }
196
197 hdr->loglencxx . . $ kore_alog_header#loglen. = len;
198 hdr->domaincxx . . $ kore_alog_header#domain. = req->rtcxx . . $ http_request#rt.->domcxx . . $ kore_route#dom.->idcxx . . $ kore_domain#id.;
199
200 workercxx . . $ worker.->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. += (size_t)len;
201 break;
202 }
203
204 kore_freecxx . . $ kore_free(5d22bdacc48458e8).(cn_value);
205 accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(workercxx . . $ worker.);
206 }
207
208 void
209 kore_accesslog_gathercxx . . $ kore_accesslog_gather(936bb941186e2394).(void *arg, u_int64_tcxx . . $ u_int64_t# now, int force)
210 {
211 int id;
212 struct kore_workercxx . . $ kore_worker# *kw;
213 struct kore_alog_headercxx . . $ kore_alog_header# *hdr;
214 struct kore_domaincxx . . $ kore_domain# *dom;
215 size_t off, remain;
216
217 if (logbufcxx . . $ logbuf. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
218 logbufcxx . . $ logbuf. = kore_buf_alloccxx . . $ kore_buf_alloc(ae982dcc82edde78).(LOGBUF_SIZEcxx . . $ `src/accesslog.c:40:9`!);
219
220 for (id = KORE_WORKER_BASEcxx . . $ `include/kore/kore.h:651:9`!; id < worker_countcxx . . $ worker_count.; id++) {
221 kw = kore_worker_datacxx . . $ kore_worker_data(a59abc2c9e0c50f4).(id);
222
223 accesslog_lockcxx . . $ accesslog_lock(8632b877e8ae27d6).(kw);
224
225 if (force || kw->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. >= KORE_ACCESSLOG_SYNCcxx . . $ `include/kore/kore.h:446:9`!) {
226 kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(logbufcxx . . $ logbuf., kw->lbcxx . . $ kore_worker#lb..bufcxx . . $ kore_worker#$anonymous_type_17#buf., kw->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset.);
227 kw->lbcxx . . $ kore_worker#lb..offsetcxx . . $ kore_worker#$anonymous_type_17#offset. = 0;
228 }
229
230 accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(kw);
231 }
232
233 if (force || logbufcxx . . $ logbuf.->offsetcxx . . $ kore_buf#offset. >= LOGBUF_SIZEcxx . . $ `src/accesslog.c:40:9`!) {
234 off = 0;
235 remain = logbufcxx . . $ logbuf.->offsetcxx . . $ kore_buf#offset.;
236
237 while (remain > 0) {
238 if (remain < sizeof(*hdr)) {
239 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!,
240 "invalid log buffer: (%zu remain)", remain);
241 break;
242 }
243
244 hdr = (struct kore_alog_headercxx . . $ kore_alog_header# *)(logbufcxx . . $ logbuf.->datacxx . . $ kore_buf#data. + off);
245 off += sizeof(*hdr);
246 remain -= sizeof(*hdr);
247
248 if (hdr->loglencxx . . $ kore_alog_header#loglen. > remain) {
249 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!,
250 "invalid log header: %u (%zu remain)",
251 hdr->loglencxx . . $ kore_alog_header#loglen., remain);
252 break;
253 }
254
255 if ((dom = kore_domain_byidcxx . . $ kore_domain_byid(d843cb0e8b808413).(hdr->domaincxx . . $ kore_alog_header#domain.)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
256 fatalcxx . . $ fatal(f6bc48f0426f26ee).("unknown domain id %u", hdr->domaincxx . . $ kore_alog_header#domain.);
257
258 if (dom->logbufcxx . . $ kore_domain#logbuf. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
259 dom->logbufcxx . . $ kore_domain#logbuf. = kore_buf_alloccxx . . $ kore_buf_alloc(ae982dcc82edde78).(DOMAIN_LOGBUF_LENcxx . . $ `src/accesslog.c:41:9`!);
260
261 kore_buf_appendcxx . . $ kore_buf_append(4909ebf68ec24f4d).(dom->logbufcxx . . $ kore_domain#logbuf., &logbufcxx . . $ logbuf.->datacxx . . $ kore_buf#data.[off],
262 hdr->loglencxx . . $ kore_alog_header#loglen.);
263
264 off += hdr->loglencxx . . $ kore_alog_header#loglen.;
265 remain -= hdr->loglencxx . . $ kore_alog_header#loglen.;
266
267 accesslog_flushcxx . . $ accesslog_flush(69b0378392360ff9).(dom, now, force);
268 }
269
270 kore_buf_resetcxx . . $ kore_buf_reset(2648f45d13d14762).(logbufcxx . . $ logbuf.);
271 }
272
273 if (force)
274 kore_domain_callbackcxx . . $ kore_domain_callback(2a5b70316e33bad0).(accesslog_flush_cbcxx . . $ accesslog_flush_cb(be08e46f67814b96).);
275 }
276
277 void
278 kore_accesslog_runcxx . . $ kore_accesslog_run(5500c4293b1bbef0).(void *arg, u_int64_tcxx . . $ u_int64_t# now)
279 {
280 static int ticks = 0;
281
282 kore_accesslog_gathercxx . . $ kore_accesslog_gather(936bb941186e2394).(arg, now, ticks++ % 100 ? 0 : 1);
283 }
284
285 static void
286 accesslog_flush_cbcxx . . $ accesslog_flush_cb(be08e46f67814b96).(struct kore_domaincxx . . $ kore_domain# *dom)
287 {
288 accesslog_flushcxx . . $ accesslog_flush(69b0378392360ff9).(dom, 0, 1);
289 }
290
291 static void
292 accesslog_flushcxx . . $ accesslog_flush(69b0378392360ff9).(struct kore_domaincxx . . $ kore_domain# *dom, u_int64_tcxx . . $ u_int64_t# now, int force)
293 {
294 ssize_tcxx . . $ ssize_t# written;
295
296 if (force && dom->logbufcxx . . $ kore_domain#logbuf. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
297 return;
298
299 if (force || dom->logbufcxx . . $ kore_domain#logbuf.->offsetcxx . . $ kore_buf#offset. >= DOMAIN_LOGBUF_LENcxx . . $ `src/accesslog.c:41:9`!) {
300 written = writecxx . . $ write(41184a59d38b4f65).(dom->accesslogcxx . . $ kore_domain#accesslog., dom->logbufcxx . . $ kore_domain#logbuf.->datacxx . . $ kore_buf#data.,
301 dom->logbufcxx . . $ kore_domain#logbuf.->offsetcxx . . $ kore_buf#offset.);
302 if (written == -1) {
303 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!)
304 return;
305 if (dom->logwarncxx . . $ kore_domain#logwarn. == 0 ||
306 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != dom->logerrcxx . . $ kore_domain#logerr.) {
307 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_NOTICEcxx . . $ `<external>/6168640806541382331/syslog.h:56:9`!,
308 "error writing log for %s (%s)",
309 dom->domaincxx . . $ kore_domain#domain., errno_scxx . . $ `include/kore/kore.h:100:9`!);
310 dom->logwarncxx . . $ kore_domain#logwarn. = now;
311 dom->logerrcxx . . $ kore_domain#logerr. = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
312 }
313 kore_buf_resetcxx . . $ kore_buf_reset(2648f45d13d14762).(dom->logbufcxx . . $ kore_domain#logbuf.);
314 return;
315 }
316
317 if ((size_t)written != dom->logbufcxx . . $ kore_domain#logbuf.->offsetcxx . . $ kore_buf#offset.) {
318 kore_logcxx . . $ kore_log(afcfcee7634adf7d).(LOG_ERRcxx . . $ `<external>/6168640806541382331/syslog.h:54:9`!, "partial accesslog write for %s",
319 dom->domaincxx . . $ kore_domain#domain.);
320 }
321
322 kore_buf_resetcxx . . $ kore_buf_reset(2648f45d13d14762).(dom->logbufcxx . . $ kore_domain#logbuf.);
323 }
324 }
325
326 static void
327 accesslog_lockcxx . . $ accesslog_lock(8632b877e8ae27d6).(struct kore_workercxx . . $ kore_worker# *kw)
328 {
329 for (;;) {
330 if (__sync_bool_compare_and_swapcxx . . $ __sync_bool_compare_and_swap_4(2bb2bf1fe70f5e29).(&kw->lbcxx . . $ kore_worker#lb..lockcxx . . $ kore_worker#$anonymous_type_17#lock., 0, 1))
331 break;
332 }
333 }
334
335 static void
336 accesslog_unlockcxx . . $ accesslog_unlock(8632b877e8ae27d6).(struct kore_workercxx . . $ kore_worker# *kw)
337 {
338 if (!__sync_bool_compare_and_swapcxx . . $ __sync_bool_compare_and_swap_4(2bb2bf1fe70f5e29).(&kw->lbcxx . . $ kore_worker#lb..lockcxx . . $ kore_worker#$anonymous_type_17#lock., 1, 0))
339 fatalcxx . . $ fatal(f6bc48f0426f26ee).("accesslog_unlock: failed to release");
340 }