update: seperate pool dirs and libmp fixes

This commit is contained in:
ngn
2024-08-04 14:47:14 +03:00
parent 6d175d34d8
commit 6ddbb6f99a
9 changed files with 117 additions and 91 deletions

View File

@ -1,6 +1,5 @@
#include <errno.h>
#include <ini.h>
#include <libmp/all.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -8,6 +7,7 @@
#include "config.h"
#include "intl.h"
#include "log.h"
#include "util.h"
#define option_count() sizeof(config.options) / sizeof(config.options[0])
@ -15,9 +15,8 @@ config_t config = {
.options =
{
{.key = "threads", .type = TYPE_INTEGER, .def.integer = 10},
{.key = "tmpdir", .type = TYPE_STRING, .def.string = "/tmp/pooler"},
{.key = "addr", .type = TYPE_STRING, .def.string = "0.0.0.0:5858"},
{.key = "dir", .type = TYPE_STRING, .def.string = NULL},
{.key = "log", .type = TYPE_STRING, .def.string = NULL},
},
.pools = NULL,
};
@ -111,6 +110,11 @@ int config_load_handler(void *data, const char *_section, const char *_key, cons
config.pools->host = strdup(value);
}
else if (eq(key, "dir")) {
free(config.pools->dir);
config.pools->dir = strdup(value);
}
else
goto unknown;
@ -124,7 +128,7 @@ unknown:
bool config_load(char *file) {
config_options_clear();
if (!exists(file) || !can_read(file)) {
if (!file_canread(file)) {
error(_("Failed to access the configuration file: %s"), file);
return false;
}
@ -143,7 +147,6 @@ bool config_load(char *file) {
pool = pool->next;
}
info(_("Loaded the configuration"));
return true;
}

View File

@ -23,6 +23,7 @@ typedef struct pool_config {
struct pool_config *next;
char *name;
char *host;
char *dir;
} pool_config_t;
typedef struct config {

View File

@ -1,2 +1,4 @@
#pragma once
#include <libintl.h>
#define _(x) gettext(x)

View File

@ -1,3 +1,4 @@
#pragma once
void error(char *fmt, ...);
void info(char *fmt, ...);

View File

@ -33,6 +33,7 @@
#include "config.h"
#include "intl.h"
#include "log.h"
#include "util.h"
void sockaddr_to_str(struct sockaddr *addr, char *str) {
struct sockaddr_in *ipv4;
@ -110,94 +111,81 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}
char *poolsdir = NULL, *addr = NULL, *logfile = NULL;
char *addr = NULL, *logfile = NULL, *tmpdir = NULL;
int ret = EXIT_FAILURE;
pool_config_t *pool = NULL;
size_t pool_count = 0;
lm_ctx_t ctx;
lm_ctx_init(&ctx);
if (!config_load(argv[1]))
goto end;
goto end_config;
if ((logfile = config_get_string("log")) != NULL) {
FILE *log = fopen(logfile, "a");
info(_("Loaded the configuration"));
if (NULL == log)
error(_("Failed to open the log file: %s"), strerror(errno));
else {
dup2(fileno(log), STDERR_FILENO);
dup2(fileno(log), STDOUT_FILENO);
fclose(log);
}
if ((tmpdir = config_get_string("tmpdir")) == NULL) {
error(_("Failed to get the temp directory configuration option (tmpdir)"));
goto end_config;
}
lm_ctx_init(&ctx, NULL, tmpdir, NULL);
addr = config_get_string("addr");
if (config_get_integer("threads") <= 0 || config_get_integer("threads") > 1000) {
error(_("Please specify a valid thread count (1-1000)"));
goto end;
}
if ((poolsdir = config_get_string("dir")) == NULL) {
error(_("Pool directory not specified"));
goto end;
}
if (!exists(poolsdir) && !can_read(poolsdir)) {
error(_("Cannot access to the pool directory: %s"), poolsdir);
goto end;
goto end_ctx;
}
if ((pool = config.pools) == NULL) {
error(_("Please specify at least one pool in the configuration"));
goto end;
}
if (!lm_ctx_set_data(&ctx, poolsdir)) {
error(_("Failed to use pool directory: %s"), lm_strerror());
goto end;
goto end_ctx;
}
while (NULL != pool) {
if (NULL == pool->host) {
error(_("Hostname not specified for pool, skipping: %s"), pool->name);
goto end;
goto end_ctx;
}
char url[strlen(pool->host) + 20];
sprintf(url, "mptp://%s", pool->host);
if (NULL == lm_ctx_pool_add(&ctx, pool->name, url)) {
error(_("Failed to add pool to the list: %s"), lm_strerror());
goto end;
if (!file_canread(pool->dir)) {
error(_("Failed access the pool directory of %s: %s"), pool->name, pool->dir);
goto end_ctx;
}
if (NULL == lm_ctx_pool_add(&ctx, pool->name, url, pool->dir)) {
error(_("Failed to add pool to the list: %s"), lm_strerror());
goto end_ctx;
}
info(_("%s: loaded the pool"), pool->name);
pool = pool->next;
}
if ((pool_count = lm_ctx_sync(&ctx, false, sync_callback, NULL)) < 0) {
error(_("Failed to sync the pools: %s"), lm_strerror());
goto end;
goto end_ctx;
}
if (pool_count == 0) {
error(_("None of the pools is available for serving"));
goto end;
goto end_ctx;
}
info(pool_count == 1 ? _("Serving %lu pool on %s") : _("Serving %lu pools on %s"), pool_count, addr);
if (!lm_ctx_serve(&ctx, addr, config_get_integer("threads"), serve_callback, NULL)) {
error(_("Failed to start the server: %s"), lm_strerror());
goto end;
goto end_ctx;
}
ret = EXIT_SUCCESS;
end:
end_ctx:
lm_ctx_free(&ctx);
end_config:
config_free();
return ret;
}

19
src/util.c Normal file
View File

@ -0,0 +1,19 @@
#include "util.h"
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
bool file_canread(char *p) {
return access(p, R_OK) == 0;
}
bool eq(char *s1, char *s2) {
if (NULL == s1 || NULL == s2)
return false;
if (strlen(s1) != strlen(s2))
return false;
return strcmp(s1, s2) == 0;
}

5
src/util.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <stdbool.h>
bool file_canread(char *p);
bool eq(char *s1, char *s2);