fix: ignore closed connection, don't use mmap in ftob

This commit is contained in:
ruti 2024-05-04 16:31:47 +03:00
parent 74e90e9568
commit e1b952cef6
4 changed files with 29 additions and 22 deletions

22
conev.c
View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h>
struct poolhd *init_pool(int count) struct poolhd *init_pool(int count)
@ -33,6 +34,7 @@ struct poolhd *init_pool(int count)
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
pool->links[i] = &(pool->items[i]); pool->links[i] = &(pool->items[i]);
} }
memset(pool->items, 0, sizeof(*pool->items));
return pool; return pool;
} }
@ -40,9 +42,14 @@ struct poolhd *init_pool(int count)
struct eval *add_event(struct poolhd *pool, enum eid type, struct eval *add_event(struct poolhd *pool, enum eid type,
int fd, int e) int fd, int e)
{ {
if (pool->count >= pool->max) if (pool->count >= pool->max) {
return 0; return 0;
}
struct eval *val = pool->links[pool->count]; struct eval *val = pool->links[pool->count];
if (pool->iters &&
val->del_iter == pool->iters) {
return 0;
}
memset(val, 0, sizeof(*val)); memset(val, 0, sizeof(*val));
val->fd = fd; val->fd = fd;
@ -72,7 +79,7 @@ struct eval *add_event(struct poolhd *pool, enum eid type,
void del_event(struct poolhd *pool, struct eval *val) void del_event(struct poolhd *pool, struct eval *val)
{ {
if (!val->fd) { if (val->del_iter) {
return; return;
} }
if (val->buff.data) { if (val->buff.data) {
@ -81,6 +88,7 @@ void del_event(struct poolhd *pool, struct eval *val)
} }
close(val->fd); close(val->fd);
val->fd = 0; val->fd = 0;
val->del_iter = pool->iters;
pool->count--; pool->count--;
struct eval *ev = pool->links[pool->count]; struct eval *ev = pool->links[pool->count];
@ -147,6 +155,11 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *type)
} }
*offs = i - 1; *offs = i - 1;
*type = pool->pevents[i].events; *type = pool->pevents[i].events;
if (pool->iters == UINT_MAX) {
pool->iters = 0;
}
pool->iters++;
return pool->pevents[i].data.ptr; return pool->pevents[i].data.ptr;
} }
@ -181,6 +194,11 @@ struct eval *next_event(struct poolhd *pool, int *offs, int *typel)
pool->pevents[i].revents = 0; pool->pevents[i].revents = 0;
*offs = i - 1; *offs = i - 1;
*typel = type; *typel = type;
if (pool->iters == UINT_MAX) {
pool->iters = 0;
}
pool->iters++;
return pool->links[i]; return pool->links[i];
} }
} }

View file

@ -80,6 +80,7 @@ struct eval {
#ifndef NOEPOLL #ifndef NOEPOLL
uint32_t events; uint32_t events;
#endif #endif
unsigned int del_iter;
}; };
struct poolhd { struct poolhd {
@ -93,6 +94,7 @@ struct poolhd {
#else #else
struct pollfd *pevents; struct pollfd *pevents;
#endif #endif
unsigned int iters;
}; };
struct poolhd *init_pool(int count); struct poolhd *init_pool(int count);

23
main.c
View file

@ -221,13 +221,6 @@ char *ftob(const char *str, ssize_t *sl)
if (fseek(file, 0, SEEK_SET)) { if (fseek(file, 0, SEEK_SET)) {
break; break;
} }
#ifndef _WIN32
buffer = mmap(0, size, PROT_READ, MAP_PRIVATE, fileno(file), 0);
if (buffer == MAP_FAILED) {
buffer = 0;
break;
}
#else
if (!(buffer = malloc(size))) { if (!(buffer = malloc(size))) {
break; break;
} }
@ -235,7 +228,6 @@ char *ftob(const char *str, ssize_t *sl)
free(buffer); free(buffer);
buffer = 0; buffer = 0;
} }
#endif
} while (0); } while (0);
if (buffer) { if (buffer) {
*sl = size; *sl = size;
@ -390,11 +382,6 @@ void *add(void **root, int *n, size_t ss)
return p; return p;
} }
#ifndef _WIN32
#define FREE_MAP(p, s) munmap(p, s)
#else
#define FREE_MAP(p, s) free(p)
#endif
void clear_params(void) void clear_params(void)
{ {
@ -420,12 +407,12 @@ void clear_params(void)
free(s.tlsrec); free(s.tlsrec);
s.tlsrec = 0; s.tlsrec = 0;
} }
if (s.fake_data.data) { if (s.fake_data.data != 0) {
FREE_MAP(s.fake_data.data, s.fake_data.size); free(s.fake_data.data);
s.fake_data.data = 0; s.fake_data.data = 0;
} }
if (s.file_ptr) { if (s.file_ptr != 0) {
FREE_MAP(s.file_ptr, s.file_size); free(s.file_ptr);
s.file_ptr = 0; s.file_ptr = 0;
} }
} }
@ -433,7 +420,7 @@ void clear_params(void)
params.dp = 0; params.dp = 0;
} }
if (oob_data.data != oob_char) { if (oob_data.data != oob_char) {
FREE_MAP(oob_data.data, oob_data.size); free(oob_data.data);
oob_data.data = oob_char; oob_data.data = oob_char;
} }
} }

View file

@ -809,7 +809,7 @@ int event_loop(int srvfd)
} }
LOG(LOG_L, "new event: fd: %d, evt: %s\n", val->fd, eid_name[val->type]); LOG(LOG_L, "new event: fd: %d, evt: %s\n", val->fd, eid_name[val->type]);
if (!val->fd) { if (val->del_iter) {
continue; continue;
} }
switch (val->type) { switch (val->type) {