Multi processing -> multi threading

This commit is contained in:
2022-12-18 22:18:11 +01:00
parent 44913c1e0e
commit 45514f90ca
13 changed files with 464 additions and 515 deletions

View File

@ -31,9 +31,6 @@
#include <arpa/inet.h>
volatile sig_atomic_t server_keep_alive = 1;
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
static const char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"};
host_config_t *get_host_config(const char *host) {
@ -49,14 +46,15 @@ host_config_t *get_host_config(const char *host) {
return NULL;
}
/*
void client_terminate(int _) {
server_keep_alive = 0;
}
*/
int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long client_num, unsigned int req_num, const char *restrict log_client_prefix) {
struct timespec begin, end;
long ret;
int client_keep_alive;
char buf0[1024], buf1[1024];
char msg_buf[8192], msg_pre_buf_1[4096], msg_pre_buf_2[4096], err_msg[256];
@ -94,7 +92,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
if (ret <= 0) {
if (errno != 0) return 1;
client_keep_alive = 0;
cctx->c_keep_alive = 0;
res.status = http_get_status(408);
goto respond;
}
@ -103,7 +101,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
http_req req;
ret = http_receive_request(client, &req);
if (ret != 0) {
client_keep_alive = 0;
cctx->c_keep_alive = 0;
if (ret < 0) {
goto abort;
} else if (ret == 1) {
@ -122,7 +120,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
}
hdr_connection = http_get_header_field(&req.hdr, "Connection");
client_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
cctx->c_keep_alive = (hdr_connection != NULL && (strstr(hdr_connection, "keep-alive") != NULL || strstr(hdr_connection, "Keep-Alive") != NULL));
host_ptr = http_get_header_field(&req.hdr, "Host");
if (host_ptr != NULL && strlen(host_ptr) > 255) {
host[0] = 0;
@ -142,7 +140,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
strcpy(host, host_ptr);
}
sprintf(log_req_prefix, "[%6i][%s%*s%s]%s", getpid(), BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
sprintf(log_req_prefix, "[%s%*s%s]%s", BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
logger_set_prefix(log_req_prefix);
info(BLD_STR "%s %s", req.method, req.uri);
@ -254,7 +252,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
goto respond;
}
if ((ret = cache_init_uri(&uri)) != 0) {
if ((ret = cache_init_uri(conf->cache, &uri)) != 0) {
res.status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with internal file cache.");
goto respond;
@ -271,7 +269,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
if (uri.meta->filename_comp_br[0] != 0 && strstr(accept_encoding, "br") != NULL) {
file = fopen(uri.meta->filename_comp_br, "rb");
if (file == NULL) {
cache_filename_comp_invalid(uri.filename);
cache_mark_dirty(conf->cache, uri.filename);
} else {
http_add_header_field(&res.hdr, "Content-Encoding", "br");
enc = COMPRESS_BR;
@ -279,7 +277,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
} else if (uri.meta->filename_comp_gz[0] != 0 && strstr(accept_encoding, "gzip") != NULL) {
file = fopen(uri.meta->filename_comp_gz, "rb");
if (file == NULL) {
cache_filename_comp_invalid(uri.filename);
cache_mark_dirty(conf->cache, uri.filename);
} else {
http_add_header_field(&res.hdr, "Content-Encoding", "gzip");
enc = COMPRESS_GZ;
@ -614,7 +612,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
http_remove_header_field(&res.hdr, "Content-Length", HTTP_REMOVE_ALL);
http_add_header_field(&res.hdr, "Content-Length", buf0);
} else if (http_get_header_field(&res.hdr, "Transfer-Encoding") == NULL) {
server_keep_alive = 0;
cctx->s_keep_alive = 0;
}
}
@ -624,7 +622,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
http_remove_header_field(&res.hdr, "Connection", HTTP_REMOVE_ALL);
http_remove_header_field(&res.hdr, "Keep-Alive", HTTP_REMOVE_ALL);
if (server_keep_alive && client_keep_alive) {
if (cctx->s_keep_alive && cctx->c_keep_alive) {
http_add_header_field(&res.hdr, "Connection", "keep-alive");
sprintf(buf0, "timeout=%i, max=%i", CLIENT_TIMEOUT, REQ_PER_CONNECTION);
http_add_header_field(&res.hdr, "Keep-Alive", buf0);
@ -648,7 +646,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
info("Upgrading connection to WebSocket connection");
ret = ws_handle_connection(client, &proxy);
if (ret != 0) {
client_keep_alive = 0;
cctx->c_keep_alive = 0;
close_proxy = 1;
}
info("WebSocket connection closed");
@ -696,7 +694,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
}
if (ret < 0) {
client_keep_alive = 0;
cctx->c_keep_alive = 0;
}
}
@ -718,12 +716,12 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien
}
http_free_req(&req);
http_free_res(&res);
return !client_keep_alive;
return 0;
}
int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
struct timespec begin, end;
int ret, req_num;
int ret;
char buf[1024];
clock_gettime(CLOCK_MONOTONIC, &begin);
@ -760,12 +758,10 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
ctx->cc[0] != 0 ? ctx->cc : "N/A");
client_timeout.tv_sec = CLIENT_TIMEOUT;
client_timeout.tv_usec = 0;
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &client_timeout, sizeof(client_timeout)) < 0)
goto set_timeout_err;
if (setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) < 0) {
set_timeout_err:
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
if (setsockopt(client->socket, SOL_SOCKET, SO_RCVTIMEO, &client_timeout, sizeof(client_timeout)) == -1 ||
setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) == -1)
{
error("Unable to set timeout for socket");
return 1;
}
@ -786,9 +782,10 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
}
}
req_num = 0;
ret = 0;
while (ret == 0 && server_keep_alive && req_num < REQ_PER_CONNECTION) {
int req_num = 0;
ctx->s_keep_alive = 1;
ctx->c_keep_alive = 1;
while (ctx->c_keep_alive && ctx->s_keep_alive && req_num < REQ_PER_CONNECTION) {
ret = client_request_handler(ctx, client, client_num, req_num++, log_client_prefix);
logger_set_prefix(log_conn_prefix);
}
@ -808,15 +805,17 @@ int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long cli
return 0;
}
int client_handler(sock *client, unsigned long client_num) {
void *client_handler(sock *client) {
struct sockaddr_in6 *server_addr;
struct sockaddr_storage server_addr_storage;
client_ctx_t ctx;
char log_client_prefix[256], log_conn_prefix[512];
signal(SIGINT, client_terminate);
signal(SIGTERM, client_terminate);
logger_set_name("client");
//signal(SIGINT, client_terminate);
//signal(SIGTERM, client_terminate);
inet_ntop(client->addr.ipv6.sin6_family, &client->addr.ipv6.sin6_addr, ctx._c_addr, sizeof(ctx._c_addr));
if (strncmp(ctx._c_addr, "::ffff:", 7) == 0) {
@ -836,13 +835,15 @@ int client_handler(sock *client, unsigned long client_num) {
}
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET6_ADDRSTRLEN, ctx.addr,
ntohs(server_addr->sin6_port), CLR_STR, color_table[0], INET6_ADDRSTRLEN, ctx.addr,
ntohs(client->addr.ipv6.sin6_port), CLR_STR);
sprintf(log_conn_prefix, "[%6i][%*s]%s", getpid(), INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
sprintf(log_conn_prefix, "[%*s]%s", INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
logger_set_prefix(log_conn_prefix);
info("Started child process with PID %i", getpid());
info("Started thread");
return client_connection_handler(&ctx, client, client_num, log_conn_prefix, log_client_prefix);
client_connection_handler(&ctx, client, 0, log_conn_prefix, log_client_prefix);
return NULL;
}

View File

@ -18,6 +18,8 @@
typedef struct {
char *addr;
char *s_addr;
unsigned char s_keep_alive:1;
unsigned char c_keep_alive:1;
char cc[3];
char host[256];
char geoip[GEOIP_MAX_JSON_SIZE + 1];
@ -27,6 +29,6 @@ typedef struct {
host_config_t *get_host_config(const char *host);
int client_handler(sock *client, unsigned long client_num);
void *client_handler(sock *client);
#endif //SESIMOS_CLIENT_H

View File

@ -6,24 +6,40 @@
* @date 2020-12-19
*/
#include "../server.h"
#include "../logger.h"
#include "cache.h"
#include "utils.h"
#include "compress.h"
#include "config.h"
#include <stdio.h>
#include <magic.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <openssl/evp.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <semaphore.h>
#define CACHE_BUF_SIZE 16
int cache_continue = 1;
magic_t magic;
cache_entry *cache;
static pthread_t thread;
static sem_t sem_free, sem_used, sem_lock;
typedef struct {
int rd;
int wr;
cache_entry_t *msgs[CACHE_BUF_SIZE];
} buf_t;
static buf_t buffer;
static int magic_init(void) {
if ((magic = magic_open(MAGIC_MIME)) == NULL) {
@ -39,276 +55,285 @@ static int magic_init(void) {
return 0;
}
void cache_process_term(int _) {
cache_continue = 0;
static void cache_free(void) {
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config_t *hc = &config.hosts[i];
if (hc->type == CONFIG_TYPE_UNSET) break;
if (hc->type != CONFIG_TYPE_LOCAL) continue;
munmap(hc->cache, sizeof(cache_t));
}
}
int cache_process(void) {
errno = 0;
signal(SIGINT, cache_process_term);
signal(SIGTERM, cache_process_term);
static cache_entry_t *cache_get_entry(cache_t *cache, const char *filename) {
// search entry
cache_entry_t *entry;
for (int i = 0; i < CACHE_ENTRIES; i++) {
entry = &cache->entries[i];
if (entry->filename[0] == 0) break;
if (strcmp(entry->filename, filename) == 0) {
// found
return entry;
}
}
// not found
return NULL;
}
static cache_entry_t *cache_get_new_entry(cache_t *cache) {
// search empty slot
cache_entry_t *entry;
for (int i = 0; i < CACHE_ENTRIES; i++) {
entry = &cache->entries[i];
if (entry->filename[0] == 0)
return entry;
}
// not found
return NULL;
}
static void cache_process_entry(cache_entry_t *entry) {
char buf[16384], comp_buf[16384], filename_comp_gz[256], filename_comp_br[256];
info("Hashing file %s", entry->filename);
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx, EVP_sha1());
FILE *file = fopen(entry->filename, "rb");
int compress = mime_is_compressible(entry->meta.type);
compress_ctx comp_ctx;
FILE *comp_file_gz = NULL, *comp_file_br = NULL;
if (compress) {
sprintf(buf, "%.*s/.sesimos", entry->webroot_len, entry->filename);
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
error("Unable to create directory %s", buf);
goto comp_err;
}
sprintf(buf, "%.*s/.sesimos/cache", entry->webroot_len, entry->filename);
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
error("Unable to create directory %s", buf);
goto comp_err;
}
errno = 0;
char *rel_path = entry->filename + entry->webroot_len + 1;
for (int j = 0; j < strlen(rel_path); j++) {
char ch = rel_path[j];
if (ch == '/') ch = '_';
buf[j] = ch;
}
buf[strlen(rel_path)] = 0;
int p_len_gz = snprintf(filename_comp_gz, sizeof(filename_comp_gz),
"%.*s/.sesimos/cache/%s.gz",
entry->webroot_len, entry->filename, buf);
int p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
"%.*s/.sesimos/cache/%s.br",
entry->webroot_len, entry->filename, buf);
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
error("Unable to open cached file: File name for compressed file too long");
goto comp_err;
}
info("Compressing file %s", entry->filename);
comp_file_gz = fopen(filename_comp_gz, "wb");
comp_file_br = fopen(filename_comp_br, "wb");
if (comp_file_gz == NULL || comp_file_br == NULL) {
error("Unable to open cached file");
comp_err:
compress = 0;
} else {
if ((compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR)) != 0) {
error("Unable to init compression");
compress = 0;
fclose(comp_file_gz);
fclose(comp_file_br);
}
}
}
unsigned long read;
while ((read = fread(buf, 1, sizeof(buf), file)) > 0) {
EVP_DigestUpdate(ctx, buf, read);
if (compress) {
unsigned long avail_in, avail_out;
avail_in = read;
do {
avail_out = sizeof(comp_buf);
compress_compress_mode(&comp_ctx, COMPRESS_GZ, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_gz);
} while (avail_in != 0 || avail_out != sizeof(comp_buf));
avail_in = read;
do {
avail_out = sizeof(comp_buf);
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, sizeof(comp_buf) - avail_out, comp_file_br);
} while (avail_in != 0 || avail_out != sizeof(comp_buf));
}
}
if (compress) {
compress_free(&comp_ctx);
fclose(comp_file_gz);
fclose(comp_file_br);
info("Finished compressing file %s", entry->filename);
strcpy(entry->meta.filename_comp_gz, filename_comp_gz);
strcpy(entry->meta.filename_comp_br, filename_comp_br);
} else {
memset(entry->meta.filename_comp_gz, 0, sizeof(entry->meta.filename_comp_gz));
memset(entry->meta.filename_comp_br, 0, sizeof(entry->meta.filename_comp_br));
}
unsigned int md_len;
unsigned char hash[EVP_MAX_MD_SIZE];
EVP_DigestFinal(ctx, hash, &md_len);
EVP_MD_CTX_free(ctx);
memset(entry->meta.etag, 0, sizeof(entry->meta.etag));
for (int j = 0; j < md_len; j++) {
sprintf(entry->meta.etag + j * 2, "%02x", hash[j]);
}
fclose(file);
entry->flags &= !CACHE_DIRTY;
info("Finished hashing file %s", entry->filename);
}
static void *cache_thread(void *arg) {
logger_set_name("cache");
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), 0);
if (shm_id < 0) {
critical("Unable to create cache shared memory");
return -1;
}
shmdt(cache);
errno = 0;
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
critical("Unable to attach cache shared memory (rw)");
return -2;
}
cache = shm_rw;
if (mkdir("/var/sesimos/", 0755) < 0 && errno != EEXIST) {
critical("Unable to create directory '/var/sesimos/'");
return -3;
}
if (mkdir("/var/sesimos/server/", 0755) < 0 && errno != EEXIST) {
critical("Unable to create directory '/var/sesimos/server/'");
return -3;
}
FILE *cache_file = fopen("/var/sesimos/server/cache", "rb");
if (cache_file != NULL) {
fread(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
fclose(cache_file);
}
errno = 0;
for (int i = 0; i < CACHE_ENTRIES; i++) {
cache[i].is_updating = 0;
}
FILE *file;
char buf[CACHE_BUF_SIZE], comp_buf[CACHE_BUF_SIZE], filename_comp_gz[256], filename_comp_br[256];
unsigned long read;
int compress;
EVP_MD_CTX *ctx;
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int md_len;
int cache_changed = 0;
int p_len_gz, p_len_br;
int ret;
while (cache_continue) {
for (int i = 0; i < CACHE_ENTRIES; i++) {
if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) {
cache[i].is_updating = 1;
info("Hashing file %s", cache[i].filename);
ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx, EVP_sha1());
file = fopen(cache[i].filename, "rb");
compress = mime_is_compressible(cache[i].meta.type);
compress_ctx comp_ctx;
FILE *comp_file_gz = NULL;
FILE *comp_file_br = NULL;
if (compress) {
sprintf(buf, "%.*s/.sesimos", cache[i].webroot_len, cache[i].filename);
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
error("Unable to create directory %s", buf);
goto comp_err;
}
sprintf(buf, "%.*s/.sesimos/cache", cache[i].webroot_len, cache[i].filename);
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
error("Unable to create directory %s", buf);
goto comp_err;
}
errno = 0;
char *rel_path = cache[i].filename + cache[i].webroot_len + 1;
for (int j = 0; j < strlen(rel_path); j++) {
char ch = rel_path[j];
if (ch == '/') ch = '_';
buf[j] = ch;
}
buf[strlen(rel_path)] = 0;
p_len_gz = snprintf(filename_comp_gz, sizeof(filename_comp_gz),
"%.*s/.sesimos/cache/%s.gz",
cache[i].webroot_len, cache[i].filename, buf);
p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
"%.*s/.sesimos/cache/%s.br",
cache[i].webroot_len, cache[i].filename, buf);
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
error("Unable to open cached file: File name for compressed file too long");
goto comp_err;
}
info("Compressing file %s", cache[i].filename);
comp_file_gz = fopen(filename_comp_gz, "wb");
comp_file_br = fopen(filename_comp_br, "wb");
if (comp_file_gz == NULL || comp_file_br == NULL) {
error("Unable to open cached file");
comp_err:
compress = 0;
} else {
ret = compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR);
if (ret != 0) {
error("Unable to init compression");
compress = 0;
fclose(comp_file_gz);
fclose(comp_file_br);
}
}
}
while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
EVP_DigestUpdate(ctx, buf, read);
if (compress) {
unsigned long avail_in, avail_out;
avail_in = read;
do {
avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
avail_in = read;
do {
avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
}
}
if (compress) {
compress_free(&comp_ctx);
fclose(comp_file_gz);
fclose(comp_file_br);
info("Finished compressing file %s", cache[i].filename);
strcpy(cache[i].meta.filename_comp_gz, filename_comp_gz);
strcpy(cache[i].meta.filename_comp_br, filename_comp_br);
} else {
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
}
EVP_DigestFinal(ctx, hash, &md_len);
EVP_MD_CTX_free(ctx);
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
for (int j = 0; j < md_len; j++) {
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
}
fclose(file);
info("Finished hashing file %s", cache[i].filename);
cache[i].is_updating = 0;
cache_changed = 1;
while (alive) {
pthread_testcancel();
if (sem_wait(&sem_used) != 0) {
if (errno == EINTR) {
continue;
} else {
error("Unable to lock semaphore");
break;
}
}
if (cache_changed) {
cache_changed = 0;
cache_file = fopen("/var/sesimos/server/cache", "wb");
if (cache_file == NULL) {
critical("Unable to open cache file");
return -1;
}
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
fclose(cache_file);
} else {
sleep(1);
}
cache_entry_t *entry = buffer.msgs[buffer.wr];
buffer.wr = (buffer.wr + 1) % CACHE_BUF_SIZE;
cache_process_entry(entry);
// unlock slot in buffer
sem_post(&sem_free);
}
return 0;
cache_free();
return NULL;
}
int cache_init(void) {
errno = 0;
if (magic_init() != 0) {
return -1;
}
char buf[512];
int ret, fd;
if ((ret = magic_init()) != 0)
return ret;
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), IPC_CREAT | IPC_EXCL | 0600);
if (shm_id < 0) {
critical("Unable to create cache shared memory");
return -2;
}
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config_t *hc = &config.hosts[i];
if (hc->type == CONFIG_TYPE_UNSET) break;
if (hc->type != CONFIG_TYPE_LOCAL) continue;
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
if (shm == (void *) -1) {
critical("Unable to attach cache shared memory (ro)");
return -3;
}
cache = shm;
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
critical("Unable to attach cache shared memory (rw)");
return -4;
}
cache = shm_rw;
memset(cache, 0, CACHE_ENTRIES * sizeof(cache_entry));
shmdt(shm_rw);
cache = shm;
errno = 0;
pid_t pid = fork();
if (pid == 0) {
// child
if (cache_process() == 0) {
return 0;
} else {
return -6;
sprintf(buf, "%s/.sesimos/metadata", hc->local.webroot);
if ((fd = open(buf, O_CREAT | O_RDWR, 0600)) == -1) {
critical("Unable to open file %s", buf);
return 1;
}
} else if (pid > 0) {
// parent
info("Started child process with PID %i as cache-updater", pid);
return pid;
} else {
critical("Unable to create child process");
return -5;
}
}
int cache_unload(void) {
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
if (shm_id < 0) {
critical("Unable to get cache shared memory id");
shmdt(cache);
return -1;
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
critical("Unable to configure cache shared memory");
shmdt(cache);
if (ftruncate(fd, sizeof(cache_t)) == -1) {
critical("Unable to truncate file %s", buf);
return 1;
}
if ((hc->cache = mmap(NULL, sizeof(cache_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == NULL) {
critical("Unable to map file %s", buf);
close(fd);
return 1;
}
close(fd);
}
// try to initialize all three semaphores
if (sem_init(&sem_lock, 0, 1) != 0|| sem_init(&sem_free, 0, 1) != 0 || sem_init(&sem_used, 0, 0) != 0) {
critical("Unable to initialize semaphore");
return -1;
}
shmdt(cache);
errno = 0;
// initialize read/write heads
buffer.rd = 0;
buffer.wr = 0;
pthread_create(&thread, NULL, cache_thread, NULL);
return 0;
}
static int cache_update_entry(int entry_num, const char *filename, const char *webroot) {
void *cache_ro = cache;
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
error("Unable to attach cache shared memory (rw)");
return -1;
}
cache = shm_rw;
int cache_join(void) {
return pthread_join(thread, NULL);
}
static void cache_mark_entry_dirty(cache_entry_t *entry) {
if (entry->flags & CACHE_DIRTY)
return;
memset(entry->meta.etag, 0, sizeof(entry->meta.etag));
memset(entry->meta.filename_comp_gz, 0, sizeof(entry->meta.filename_comp_gz));
memset(entry->meta.filename_comp_br, 0, sizeof(entry->meta.filename_comp_br));
entry->flags |= CACHE_DIRTY;
try_again_free:
if (sem_wait(&sem_free) != 0) {
if (errno == EINTR) {
goto try_again_free;
} else {
error("Unable to lock semaphore");
}
return;
}
// try to lock buffer
try_again_lock:
if (sem_wait(&sem_lock) != 0) {
if (errno == EINTR) {
goto try_again_lock;
} else {
error("Unable to lock semaphore");
}
return;
}
// write to buffer
buffer.msgs[buffer.rd] = entry;
buffer.rd = (buffer.rd + 1) % CACHE_BUF_SIZE;
// unlock buffer
sem_post(&sem_lock);
// unlock slot in buffer for logger
sem_post(&sem_used);
}
static int cache_update_entry(cache_entry_t *entry, const char *filename, const char *webroot) {
struct stat statbuf;
stat(filename, &statbuf);
memcpy(&cache[entry_num].meta.stat, &statbuf, sizeof(statbuf));
memcpy(&entry->meta.stat, &statbuf, sizeof(statbuf));
cache[entry_num].webroot_len = (unsigned char) strlen(webroot);
strcpy(cache[entry_num].filename, filename);
entry->webroot_len = (unsigned char) strlen(webroot);
strcpy(entry->filename, filename);
magic_setflags(magic, MAGIC_MIME_TYPE);
const char *type = magic_file(magic, filename);
char type_new[24];
char type_new[URI_TYPE_SIZE];
sprintf(type_new, "%s", type);
if (strncmp(type, "text/", 5) == 0) {
if (strcmp(filename + strlen(filename) - 4, ".css") == 0) {
@ -317,87 +342,48 @@ static int cache_update_entry(int entry_num, const char *filename, const char *w
sprintf(type_new, "application/javascript");
}
}
strcpy(cache[entry_num].meta.type, type_new);
strcpy(entry->meta.type, type_new);
magic_setflags(magic, MAGIC_MIME_ENCODING);
strcpy(cache[entry_num].meta.charset, magic_file(magic, filename));
strcpy(entry->meta.charset, magic_file(magic, filename));
memset(cache[entry_num].meta.etag, 0, sizeof(cache[entry_num].meta.etag));
memset(cache[entry_num].meta.filename_comp_gz, 0, sizeof(cache[entry_num].meta.filename_comp_gz));
memset(cache[entry_num].meta.filename_comp_br, 0, sizeof(cache[entry_num].meta.filename_comp_br));
cache[entry_num].is_updating = 0;
cache_mark_entry_dirty(entry);
shmdt(shm_rw);
cache = cache_ro;
errno = 0;
return 0;
}
int cache_filename_comp_invalid(const char *filename) {
void *cache_ro = cache;
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
error("Unable to attach cache shared memory (rw)");
return -1;
}
cache = shm_rw;
int i;
for (i = 0; i < CACHE_ENTRIES; i++) {
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(filename) &&
strcmp(cache[i].filename, filename) == 0) {
if (cache[i].is_updating) {
return 0;
} else {
break;
}
}
}
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
cache[i].is_updating = 0;
shmdt(shm_rw);
cache = cache_ro;
return 0;
void cache_mark_dirty(cache_t *cache, const char *filename) {
cache_entry_t *entry = cache_get_entry(cache, filename);
if (entry) cache_mark_entry_dirty(entry);
}
int cache_init_uri(http_uri *uri) {
if (uri->filename == NULL) {
int cache_init_uri(cache_t *cache, http_uri *uri) {
if (uri->filename == NULL)
return 0;
}
int i;
for (i = 0; i < CACHE_ENTRIES; i++) {
if (cache[i].filename[0] != 0 && strlen(cache[i].filename) == strlen(uri->filename) &&
strcmp(cache[i].filename, uri->filename) == 0) {
uri->meta = &cache[i].meta;
if (cache[i].is_updating) {
return 0;
} else {
break;
}
}
}
if (uri->meta == NULL) {
for (i = 0; i < CACHE_ENTRIES; i++) {
if (cache[i].filename[0] == 0) {
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
return -1;
}
uri->meta = &cache[i].meta;
break;
cache_entry_t *entry = cache_get_entry(cache, uri->filename);
if (entry == NULL) {
// no entry found -> create new entry
entry = cache_get_new_entry(cache);
if (entry) {
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) {
return -1;
}
uri->meta = &entry->meta;
} else {
warning("No empty cache entry slot found");
}
} else {
uri->meta = &entry->meta;
if (entry->flags & CACHE_DIRTY)
return 0;
// check, if file has changed
struct stat statbuf;
stat(uri->filename, &statbuf);
if (memcmp(&uri->meta->stat.st_mtime, &statbuf.st_mtime, sizeof(statbuf.st_mtime)) != 0) {
if (cache_update_entry(i, uri->filename, uri->webroot) != 0) {
// modify time has changed
if (cache_update_entry(entry, uri->filename, uri->webroot) != 0) {
return -1;
}
}

View File

@ -11,9 +11,9 @@
#include "uri.h"
#define CACHE_SHM_KEY 255641
#define CACHE_ENTRIES 1024
#define CACHE_BUF_SIZE 16384
#define CACHE_DIRTY 1
#ifndef CACHE_MAGIC_FILE
# define CACHE_MAGIC_FILE "/usr/share/file/misc/magic.mgc"
@ -23,24 +23,22 @@
typedef struct {
char filename[256];
unsigned char webroot_len;
unsigned char is_updating:1;
meta_data meta;
} cache_entry;
unsigned char flags;
metadata_t meta;
} cache_entry_t;
extern cache_entry *cache;
extern int cache_continue;
void cache_process_term(int _);
int cache_process(void);
typedef struct {
char sig[6];
unsigned char ver;
cache_entry_t entries[CACHE_ENTRIES];
} cache_t;
int cache_init(void);
int cache_unload(void);
int cache_join(void);
int cache_filename_comp_invalid(const char *filename);
void cache_mark_dirty(cache_t *cache, const char *filename);
int cache_init_uri(http_uri *uri);
int cache_init_uri(cache_t *cache, http_uri *uri);
#endif //SESIMOS_CACHE_H

View File

@ -13,7 +13,6 @@
#include <string.h>
#include <stdlib.h>
config_t config;
char geoip_dir[256], dns_server[256];
@ -24,6 +23,8 @@ int config_load(const char *filename) {
return -1;
}
memset(&config, 0, sizeof(config));
int i = 0;
int j = 0;
int line_num = 0;
@ -159,7 +160,7 @@ int config_load(const char *filename) {
while (source[0] == ' ' || source[0] == '\t') source++;
if (strlen(source) == 0) {
err:
critical("Unable to parse config file (line_num %i)", line_num);
critical("Unable to parse config file (line %i)", line);
return -2;
}

View File

@ -10,6 +10,7 @@
#define SESIMOS_CONFIG_H
#include "uri.h"
#include "cache.h"
#define CONFIG_MAX_HOST_CONFIG 64
#define CONFIG_MAX_CERT_CONFIG 64
@ -28,6 +29,7 @@ typedef struct {
char name[256];
char cert_name[256];
int cert;
cache_t *cache;
union {
struct {
char hostname[256];

View File

@ -405,8 +405,8 @@ const char *http_get_status_color(const http_status *status) {
}
char *http_format_date(time_t time, char *buf, size_t size) {
struct tm *timeinfo = gmtime(&time);
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", timeinfo);
struct tm timeinfo;
strftime(buf, size, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&time, &timeinfo));
return buf;
}

View File

@ -9,6 +9,7 @@
#include "uri.h"
#include "utils.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@ -20,12 +21,16 @@ int path_is_directory(const char *path) {
int path_is_file(const char *path) {
struct stat statbuf;
return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode) == 0;
int ret = stat(path, &statbuf);
errno = 0;
return ret == 0 && S_ISDIR(statbuf.st_mode) == 0;
}
int path_exists(const char *path) {
struct stat statbuf;
return stat(path, &statbuf) == 0;
int ret = stat(path, &statbuf);
errno = 0;
return ret == 0;
}
int uri_init(http_uri *uri, const char *webroot, const char *uri_str, int dir_mode) {

View File

@ -16,7 +16,7 @@
#define URI_DIR_MODE_LIST 2
#define URI_DIR_MODE_INFO 3
#define URI_ETAG_SIZE 64 // SHA256 size (hex)
#define URI_ETAG_SIZE 65 // SHA256 size (hex)
#define URI_TYPE_SIZE 64
#define URI_CHARSET_SIZE 16
@ -27,7 +27,7 @@ typedef struct {
char filename_comp_gz[256];
char filename_comp_br[256];
struct stat stat;
} meta_data;
} metadata_t;
typedef struct {
char *webroot; // "/srv/www/www.test.org"
@ -37,7 +37,7 @@ typedef struct {
char *query; // "username=test"
char *filename; // "/account/index.php"
char *uri; // "/account/login?username=test"
meta_data *meta;
metadata_t *meta;
unsigned int is_static:1;
unsigned int is_dir:1;
} http_uri;

View File

@ -36,6 +36,7 @@ typedef struct {
log_msg_t msgs[LOG_BUF_SIZE];
} buf_t;
static pthread_t thread;
static volatile sig_atomic_t logger_alive = 0;
static sem_t sem_buf, sem_buf_free, sem_buf_used;
static buf_t buffer;
@ -149,31 +150,6 @@ static void logger_destroy(void) {
sem_destroy(&sem_buf_used);
}
static int logger_init(void) {
int ret;
// try to initialize all three semaphores
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, LOG_BUF_SIZE) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
err("Unable to initialize semaphore");
logger_destroy();
return -1;
}
// initialize read/write heads
buffer.rd = 0;
buffer.wr = 0;
// initialize thread specific values (keys)
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
errno = ret;
err("Unable to initialize thread specific values");
logger_destroy();
return -1;
}
return 0;
}
static int logger_remaining(void) {
int val = 0;
sem_getvalue(&sem_buf_used, &val);
@ -200,14 +176,14 @@ void logger_set_name(const char *restrict name) {
}
void logger_set_prefix(const char *restrict prefix) {
if (key_name == -1) {
if (key_prefix == -1) {
// not initialized
strncpy(global_prefix, prefix, sizeof(global_prefix));
} else {
int ret;
void *ptr = pthread_getspecific(key_name);
void *ptr = pthread_getspecific(key_prefix);
if (!ptr) {
ptr = malloc(LOG_PREFIX_LEN);
pthread_setspecific(key_prefix, ptr);
if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) {
errno = ret;
err("Unable to set thread specific values");
@ -218,14 +194,7 @@ void logger_set_prefix(const char *restrict prefix) {
}
}
void logger_stop(void) {
logger_alive = 0;
}
void logger_thread(void) {
if (logger_init() != 0)
return;
static void *logger_thread(void *arg) {
logger_set_name("logger");
logger_alive = 1;
@ -243,11 +212,47 @@ void logger_thread(void) {
log_msg_t *msg = &buffer.msgs[buffer.wr];
buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE;
printf("[%s]%s %s\n", msg->name, (msg->prefix[0] != 0) ? msg->prefix : "", msg->txt);
printf("%s[%-6s][%-6s]%s%s %s\n",
(msg->lvl <= LOG_ERROR) ? ERR_STR : ((msg->lvl <= LOG_WARNING) ? WRN_STR : ""),
(msg->name[0] != 0) ? (char *) msg->name : "", level_keywords[msg->lvl], CLR_STR,
(msg->prefix[0] != 0) ? (char *) msg->prefix : "", msg->txt);
// unlock slot in buffer
sem_post(&sem_buf_free);
}
logger_destroy();
return NULL;
}
int logger_init(void) {
int ret;
// try to initialize all three semaphores
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, LOG_BUF_SIZE) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
err("Unable to initialize semaphore");
logger_destroy();
return -1;
}
// initialize read/write heads
buffer.rd = 0;
buffer.wr = 0;
// initialize thread specific values (keys)
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
errno = ret;
err("Unable to initialize thread specific values");
logger_destroy();
return -1;
}
pthread_create(&thread, NULL, logger_thread, NULL);
return 0;
}
void logger_stop(void) {
logger_alive = 0;
}

View File

@ -33,7 +33,7 @@ void logger_set_name(const char *restrict name);
void logger_set_prefix(const char *restrict prefix);
void logger_thread(void);
int logger_init(void);
void logger_stop(void);

View File

@ -27,7 +27,6 @@
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <wait.h>
#include <sys/types.h>
#include <openssl/err.h>
#include <openssl/pem.h>
@ -35,11 +34,20 @@
#include <openssl/conf.h>
volatile sig_atomic_t active = 1;
volatile sig_atomic_t alive = 1;
const char *config_file;
int sockets[NUM_SOCKETS];
pid_t children[MAX_CHILDREN];
SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
static int sockets[NUM_SOCKETS];
static sock clients[MAX_CHILDREN];
static pthread_t children[MAX_CHILDREN];
static SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
static int clean() {
remove("/var/sesimos/server/cache");
rmdir("/var/sesimos/server/");
return 0;
}
static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
@ -50,42 +58,38 @@ static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
return SSL_TLSEXT_ERR_OK;
}
void terminate_forcefully(int sig) {
static void accept_cb() {
}
static void accept_err_cb() {
}
static void terminate_forcefully(int sig) {
fprintf(stderr, "\n");
notice("Terminating forcefully!");
int status = 0;
int ret;
int kills = 0;
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
if (ret < 0) {
if ((ret = pthread_kill(children[i], SIGKILL)) < 0) {
errno = ret;
error("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
error("Child process with PID %i terminated with exit code %i", ret, status);
}
} else {
kill(children[i], SIGKILL);
kills++;
errno = 0;
}
}
}
if (kills > 0) {
notice("Killed %i child process(es)", kills);
}
cache_unload();
geoip_free();
exit(2);
}
void terminate_gracefully(int sig) {
static void terminate_gracefully(int sig) {
fprintf(stderr, "\n");
notice("Terminating gracefully...");
active = 0;
alive = 0;
signal(SIGINT, terminate_forcefully);
signal(SIGTERM, terminate_forcefully);
@ -94,54 +98,19 @@ void terminate_gracefully(int sig) {
close(sockets[i]);
}
int status = 0;
int wait_num = 0;
int ret;
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
ret = pthread_kill(children[i], SIGKILL);
if (ret < 0) {
critical("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
} else {
kill(children[i], SIGTERM);
wait_num++;
}
}
}
if (wait_num > 0) {
notice("Waiting for %i child process(es)...", wait_num);
}
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, 0);
if (ret < 0) {
critical("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
}
}
}
if (wait_num > 0) {
// Wait another 50 ms to let child processes write to stdout/stderr
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
nanosleep(&ts, &ts);
}
info("Goodbye");
cache_unload();
geoip_free();
exit(0);
}
@ -153,9 +122,6 @@ int main(int argc, char *const argv[]) {
long client_num = 0;
int ret;
int client_fd;
sock client;
memset(sockets, 0, sizeof(sockets));
memset(children, 0, sizeof(children));
@ -164,6 +130,8 @@ int main(int argc, char *const argv[]) {
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(443)}
};
logger_init();
logger_set_name("server");
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0 || setvbuf(stderr, NULL, _IOLBF, 0) != 0) {
@ -239,14 +207,10 @@ int main(int argc, char *const argv[]) {
return 1;
}
ret = cache_init();
if (ret < 0) {
if ((ret = cache_init()) != 0) {
geoip_free();
if (ret == -1) critical("Unable to initialize cache");
return 1;
} else if (ret != 0) {
children[0] = ret; // pid
} else {
return 0;
}
for (int i = 0; i < CONFIG_MAX_CERT_CONFIG; i++) {
@ -265,27 +229,21 @@ int main(int argc, char *const argv[]) {
if (SSL_CTX_use_certificate_chain_file(ctx, conf->full_chain) != 1) {
critical("Unable to load certificate chain file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->full_chain);
cache_unload();
geoip_free();
return 1;
}
if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 1) {
critical("Unable to load private key file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->priv_key);
cache_unload();
geoip_free();
return 1;
}
}
client.ctx = contexts[0];
proxy_preload();
for (int i = 0; i < NUM_SOCKETS; i++) {
if (listen(sockets[i], LISTEN_BACKLOG) < 0) {
critical("Unable to listen on socket %i", i);
cache_unload();
geoip_free();
return 1;
}
@ -299,7 +257,7 @@ int main(int argc, char *const argv[]) {
errno = 0;
notice("Ready to accept connections");
while (active) {
while (alive) {
ready_sockets_num = poll(poll_fds, NUM_SOCKETS, 1000);
if (ready_sockets_num < 0) {
critical("Unable to poll sockets");
@ -309,56 +267,48 @@ int main(int argc, char *const argv[]) {
for (int i = 0; i < NUM_SOCKETS; i++) {
if (poll_fds[i].revents & POLLIN) {
socklen_t addr_len = sizeof(client.addr);
client_fd = accept(sockets[i], &client.addr.sock, &addr_len);
int j;
for (j = 0; j < MAX_CHILDREN; j++) {
if (children[j] == 0) break;
}
sock *client = &clients[j];
client->ctx = contexts[0];
socklen_t addr_len = sizeof(client->addr);
int client_fd = accept(sockets[i], &client->addr.sock, &addr_len);
if (client_fd < 0) {
critical("Unable to accept connection");
continue;
}
pid_t pid = fork();
if (pid == 0) {
// child
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
client.socket = client_fd;
client.enc = (i == 1);
return client_handler(&client, client_num);
} else if (pid > 0) {
// parent
client_num++;
close(client_fd);
for (int j = 0; j < MAX_CHILDREN; j++) {
if (children[j] == 0) {
children[j] = pid;
break;
}
}
} else {
client->socket = client_fd;
client->enc = (i == 1);
ret = pthread_create(&children[j], NULL, (void *(*)(void *)) &client_handler, client);
if (ret != 0) {
errno = ret;
critical("Unable to create child process");
}
client_num++;
}
}
// TODO outsource in thread
int status = 0;
/*
void *ret_val = NULL;
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
ret = pthread_timed(children[i], &ret_val);
if (ret < 0) {
critical("Unable to wait for child process (PID %i)", children[i]);
critical("Unable to wait for thread (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
}
}
}
*/
}
cache_unload();
geoip_free();
return 0;
}

View File

@ -14,17 +14,16 @@
#include <signal.h>
#define NUM_SOCKETS 2
#define MAX_CHILDREN 1024
#define MAX_CHILDREN 64
#define LISTEN_BACKLOG 16
#define REQ_PER_CONNECTION 200
#define CLIENT_TIMEOUT 3600
#define SERVER_TIMEOUT_INIT 4
#define SERVER_TIMEOUT 3600
extern int sockets[NUM_SOCKETS];
extern pid_t children[MAX_CHILDREN];
#define CNX_HANDLER_WORKERS 8
#define REQ_HANDLER_WORKERS 16
extern volatile sig_atomic_t server_keep_alive;
extern struct timeval client_timeout;
extern volatile sig_atomic_t alive;
#endif //SESIMOS_SERVER_H