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,9 +1,9 @@
# default pooler configuration, please see the wiki page for more information # default pooler configuration, please see the wiki page for more information
# https://matterlinux.xyz/wiki/pooler # https://matterlinux.xyz/wiki/pooler
# log = /var/lib/pooler/pooler.log addr = 0.0.0.0:5858
dir = /var/lib/pooler threads = 10
addr = 0.0.0.0:5858
# [pool-name] # [pool-name]
# host = pool.example.com # host = pool.example.com
# dir = /srv/pools/pool-name

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-20 01:23+0300\n" "POT-Creation-Date: 2024-08-04 14:44+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,132 +17,139 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: src/config.c:120 #: src/config.c:124
#, c-format #, c-format
msgid "Unknown configuration option: %s/%s" msgid "Unknown configuration option: %s/%s"
msgstr "" msgstr ""
#: src/config.c:128 #: src/config.c:132
#, fuzzy, c-format #, fuzzy, c-format
msgid "Failed to access the configuration file: %s" msgid "Failed to access the configuration file: %s"
msgstr "Kayıt dosyasını açmak başarısız oldu: %s" msgstr "Kayıt dosyasını açmak başarısız oldu: %s"
#: src/config.c:133 #: src/config.c:137
#, fuzzy #, fuzzy
msgid "Failed to parse the configuration" msgid "Failed to parse the configuration"
msgstr "Kayıt dosyasını açmak başarısız oldu: %s" msgstr "Kayıt dosyasını açmak başarısız oldu: %s"
#: src/config.c:140 #: src/config.c:144
#, fuzzy, c-format #, fuzzy, c-format
msgid "Hostname not specified for the pool: %s" msgid "Hostname not specified for the pool: %s"
msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s" msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s"
#: src/config.c:146 #: src/main.c:59
msgid "Loaded the configuration"
msgstr ""
#: src/main.c:58
#, c-format #, c-format
msgid "%s: successfuly loaded the pool info" msgid "%s: successfuly loaded the pool info"
msgstr "%s: havuz bilgisi başarıyla yüklendi" msgstr "%s: havuz bilgisi başarıyla yüklendi"
#: src/main.c:62 #: src/main.c:63
#, c-format #, c-format
msgid "%s: failed to load the pool info (%s)" msgid "%s: failed to load the pool info (%s)"
msgstr "%s: havuz bilgisini yüklemek başarısız oldu (%s)" msgstr "%s: havuz bilgisini yüklemek başarısız oldu (%s)"
#: src/main.c:66 #: src/main.c:67
#, c-format #, c-format
msgid "%s: successfuly loaded the package list" msgid "%s: successfuly loaded the package list"
msgstr "%s: paket listesi başarıyla yüklendi" msgstr "%s: paket listesi başarıyla yüklendi"
#: src/main.c:70 #: src/main.c:71
#, c-format #, c-format
msgid "%s: failed to load the package list (%s)" msgid "%s: failed to load the package list (%s)"
msgstr "%s: paket listesini yüklemek başarısız oldu (%s)" msgstr "%s: paket listesini yüklemek başarısız oldu (%s)"
#: src/main.c:88 #: src/main.c:89
#, c-format #, c-format
msgid "Request from %s: PING (%s)" msgid "Request from %s: PING (%s)"
msgstr "" msgstr ""
#: src/main.c:92 #: src/main.c:93
#, c-format #, c-format
msgid "Request from %s: INFO (%s)" msgid "Request from %s: INFO (%s)"
msgstr "" msgstr ""
#: src/main.c:96 #: src/main.c:97
#, c-format #, c-format
msgid "Request from %s: LIST (%s)" msgid "Request from %s: LIST (%s)"
msgstr "" msgstr ""
#: src/main.c:100 #: src/main.c:101
#, c-format #, c-format
msgid "Request from %s: PULL (%s)" msgid "Request from %s: PULL (%s)"
msgstr "" msgstr ""
#: src/main.c:109 #: src/main.c:110
msgid "Configuration file not specified" msgid "Configuration file not specified"
msgstr "Konfigürasyon dosyası belirtilmedi" msgstr "Konfigürasyon dosyası belirtilmedi"
#: src/main.c:128 #: src/main.c:123
#, c-format msgid "Loaded the configuration"
msgid "Failed to open the log file: %s" msgstr ""
#: src/main.c:126
#, fuzzy
msgid "Failed to get the temp directory configuration option (tmpdir)"
msgstr "Kayıt dosyasını açmak başarısız oldu: %s" msgstr "Kayıt dosyasını açmak başarısız oldu: %s"
#: src/main.c:140 #: src/main.c:134
msgid "Please specify a valid thread count (1-1000)" msgid "Please specify a valid thread count (1-1000)"
msgstr "Lütfen geçerli bir işlem sayısı belirtin (1-1000)" msgstr "Lütfen geçerli bir işlem sayısı belirtin (1-1000)"
#: src/main.c:145 #: src/main.c:139
msgid "Pool directory not specified"
msgstr "Havuz dizini belirtilmemiş"
#: src/main.c:150
#, c-format
msgid "Cannot access to the pool directory: %s"
msgstr "Havuz dizinine erişilemiyor: %s"
#: src/main.c:155
msgid "Please specify at least one pool in the configuration" msgid "Please specify at least one pool in the configuration"
msgstr "Lütfen konfigürasyonda en az bir havuz belirtin" msgstr "Lütfen konfigürasyonda en az bir havuz belirtin"
#: src/main.c:160 #: src/main.c:145
#, c-format
msgid "Failed to use pool directory: %s"
msgstr "Havuz dizini kullanmak başarısız oldu: %s"
#: src/main.c:166
#, c-format #, c-format
msgid "Hostname not specified for pool, skipping: %s" msgid "Hostname not specified for pool, skipping: %s"
msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s" msgstr "Ana makine adı havuz konfigürasyonunda belirtilmedi, geçiliyor: %s"
#: src/main.c:174 #: src/main.c:153
#, fuzzy, c-format
msgid "Failed access the pool directory of %s: %s"
msgstr "Havuz dizinine erişilemiyor: %s"
#: src/main.c:158
#, c-format #, c-format
msgid "Failed to add pool to the list: %s" msgid "Failed to add pool to the list: %s"
msgstr "Havuzu listeye eklemek başarısız oldu: %s" msgstr "Havuzu listeye eklemek başarısız oldu: %s"
#: src/main.c:182 #: src/main.c:162
#, fuzzy, c-format
msgid "%s: loaded the pool"
msgstr "%s: havuz bilgisi başarıyla yüklendi"
#: src/main.c:167
#, c-format #, c-format
msgid "Failed to sync the pools: %s" msgid "Failed to sync the pools: %s"
msgstr "Havuzları sekronize etmek başarısız oldu: %s" msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/main.c:187 #: src/main.c:172
#, fuzzy #, fuzzy
msgid "None of the pools is available for serving" msgid "None of the pools is available for serving"
msgstr "Havuzların hiçbiri sunmak için müsait değil" msgstr "Havuzların hiçbiri sunmak için müsait değil"
#: src/main.c:191 #: src/main.c:176
#, c-format #, c-format
msgid "Serving %lu pool on %s" msgid "Serving %lu pool on %s"
msgstr "%lu tane havuz %s adresinde sunuluyor" msgstr "%lu tane havuz %s adresinde sunuluyor"
#: src/main.c:191 #: src/main.c:176
#, c-format #, c-format
msgid "Serving %lu pools on %s" msgid "Serving %lu pools on %s"
msgstr "%lu tane havuz %s adresinde sunuluyor" msgstr "%lu tane havuz %s adresinde sunuluyor"
#: src/main.c:194 #: src/main.c:179
#, c-format #, c-format
msgid "Failed to start the server: %s" msgid "Failed to start the server: %s"
msgstr "Sunucuyu başlatmak başarısız oldu: %s" msgstr "Sunucuyu başlatmak başarısız oldu: %s"
#, c-format
#~ msgid "Failed to open the log file: %s"
#~ msgstr "Kayıt dosyasını açmak başarısız oldu: %s"
#~ msgid "Pool directory not specified"
#~ msgstr "Havuz dizini belirtilmemiş"
#, c-format
#~ msgid "Failed to use pool directory: %s"
#~ msgstr "Havuz dizini kullanmak başarısız oldu: %s"

View File

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

View File

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

View File

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

View File

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

View File

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