feat: main.c — null conffile (now heap-allocated), add --conf-keep-n/--conf-stale-timeout flags, peers validation

This commit is contained in:
Rongsong Shen 2026-05-13 15:57:02 +00:00
parent 2f2bfa9a3c
commit e74fd2aac3

View file

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tlog.h>
#include "myconf.h"
@ -17,7 +18,9 @@ const char* argp_program_version
const char* argp_program_bug_address = BUG_EMAIL;
/* Long-option key (above ASCII to avoid collisions with short opts) */
#define OPT_PEERS 256
#define OPT_PEERS 256
#define OPT_CONF_KEEP_N 257
#define OPT_CONF_STALE_TIMEOUT 258
struct arguments {
char* myname;
@ -25,6 +28,8 @@ struct arguments {
char* workdir;
int verbose;
char* peers_str;
int conf_keep_n;
long conf_stale_timeout;
};
static struct argp_option options[] = {
@ -35,6 +40,10 @@ static struct argp_option options[] = {
{ "peers", OPT_PEERS, "PEERS", 0,
"HA peer list: comma-separated host:port pairs ordered by priority "
"(e.g. ctrl01:9990,ctrl02:9990). Index 0 = highest priority (primary)." },
{ "conf-keep-n", OPT_CONF_KEEP_N, "N", 0,
"Versioned config files to retain (default: 2)" },
{ "conf-stale-timeout", OPT_CONF_STALE_TIMEOUT, "SECS", 0,
"Seconds before saved config is flagged stale (0=disabled, default: 86400)" },
{ 0 }
};
@ -59,6 +68,12 @@ static error_t parse_opt(int key, char* arg, struct argp_state* state)
case OPT_PEERS:
arguments->peers_str = arg;
break;
case OPT_CONF_KEEP_N:
arguments->conf_keep_n = atoi(arg);
break;
case OPT_CONF_STALE_TIMEOUT:
arguments->conf_stale_timeout = atol(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
@ -76,12 +91,13 @@ int main(int argc, char* argv[])
char* loglevel = "info";
int logsize = 64 * 1024 * 1024;
int logcount = 10;
char conf_file[XCE_MAXPATH_LEN + 1];
char key_file[XCE_MAXPATH_LEN + 1];
char cert_file[XCE_MAXPATH_LEN + 1];
char myname[XCE_MAXNAME_LEN + 1];
memset(&arguments, 0x0, sizeof(arguments));
arguments.conf_keep_n = 2;
arguments.conf_stale_timeout = 86400;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
@ -111,18 +127,17 @@ int main(int argc, char* argv[])
INFO("My name is %s", arguments.myname);
memset(&conf_file, 0x0, sizeof(conf_file));
snprintf(conf_file, XCE_MAXPATH_LEN, "%s/%s/conf/current_config",
arguments.workdir, arguments.myname);
snprintf(key_file, XCE_MAXPATH_LEN, "%s/certs/xce.key", arguments.workdir);
snprintf(cert_file, XCE_MAXPATH_LEN, "%s/certs/xce.crt", arguments.workdir);
myconf.cluster_config = NULL;
myconf.conffile = conf_file;
myconf.certfile = cert_file;
myconf.keyfile = key_file;
myconf.workdir = arguments.workdir;
myconf.name = arguments.myname;
myconf.cluster_config = NULL;
myconf.conffile = NULL; /* set by load_primary_config() or fetch_config_from_primary() */
myconf.certfile = cert_file;
myconf.keyfile = key_file;
myconf.workdir = arguments.workdir;
myconf.name = arguments.myname;
myconf.conf_keep_n = arguments.conf_keep_n;
myconf.conf_stale_timeout = (time_t)arguments.conf_stale_timeout;
memset(&myconf.peers_cfg, 0, sizeof(myconf.peers_cfg));
if (arguments.peers_str) {
@ -141,6 +156,13 @@ int main(int argc, char* argv[])
sizeof(myconf.peers_cfg.identity) - 1);
INFO("HA mode: %d controller candidates, identity=%s",
myconf.peers_cfg.candidates_count, myconf.peers_cfg.identity);
for (int i = 0; i < myconf.peers_cfg.candidates_count; i++) {
if (myconf.peers_cfg.candidates[i].hostname[0] == '\0' ||
myconf.peers_cfg.candidates[i].port == 0) {
WARN("--peers: candidate %d has empty host or port 0 — "
"check peer string", i);
}
}
} else {
INFO("No --peers flag; running as single-node primary");
}