Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply find_* function refactorings #149

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static int read_server_reply(
return rv;
}

int do_attr_command(cmd_request_t cmd)
int do_attr_command(struct booth_config *conf, cmd_request_t cmd)
{
struct booth_site *site = NULL;
struct boothc_header *header;
Expand All @@ -160,7 +160,7 @@ int do_attr_command(cmd_request_t cmd)
if (!*cl.site)
site = local;
else {
if (!find_site_by_name(cl.site, &site, 1)) {
if (!find_site_by_name(conf, cl.site, &site, 1)) {
log_error("Site \"%s\" not configured.", cl.site);
goto out_close;
}
Expand Down
13 changes: 12 additions & 1 deletion src/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@

void print_geostore_usage(void);
int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd);
int do_attr_command(cmd_request_t cmd);

/**
* @internal
* Carry out a geo-atribute related command
*
* @param[in,out] conf config object to refer to
* @param[in] cmd what to perform
*
* @return 0 or negative value (-1 or -errno) on error
*/
int do_attr_command(struct booth_config *conf, cmd_request_t cmd);

int process_attr_request(struct client *req_client, void *buf);
int attr_recv(void *buf, struct booth_site *source);
int store_geo_attr(struct ticket_config *tk, const char *name, const char *val, int notime);
Expand Down
8 changes: 6 additions & 2 deletions src/booth.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
/* Says that another one should recover. */
#define TICKET_LOST CHAR2CONST('L', 'O', 'S', 'T')

struct booth_config;

typedef void (*workfn_t)(struct booth_config *conf, int ci);


typedef char boothc_site[BOOTH_NAME_LEN];
typedef char boothc_ticket[BOOTH_NAME_LEN];
Expand Down Expand Up @@ -338,7 +342,7 @@ struct client {
const struct booth_transport *transport;
struct boothc_ticket_msg *msg;
int offset; /* bytes read so far into msg */
void (*workfn)(int);
workfn_t workfn;
void (*deadfn)(int);
};

Expand All @@ -347,7 +351,7 @@ extern struct pollfd *pollfds;


int client_add(int fd, const struct booth_transport *tpt,
void (*workfn)(int ci), void (*deadfn)(int ci));
workfn_t workfn, void (*deadfn)(int ci));
int find_client_by_fd(int fd);
void safe_copy(char *dest, char *value, size_t buflen, const char *description);
int update_authkey(void);
Expand Down
30 changes: 18 additions & 12 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -991,16 +991,18 @@ int check_config(struct booth_config *conf, int type)
}


static int get_other_site(struct booth_site **node)
static int get_other_site(struct booth_config *conf,
struct booth_site **node)
{
struct booth_site *n;
int i;

*node = NULL;
if (!booth_conf)
if (conf == NULL) {
return 0;
}

FOREACH_NODE(i, n) {
FOREACH_NODE(conf, i, n) {
if (n != local && n->type == SITE) {
if (!*node) {
*node = n;
Expand All @@ -1014,18 +1016,21 @@ static int get_other_site(struct booth_site **node)
}


int find_site_by_name(char *site, struct booth_site **node, int any_type)
int find_site_by_name(struct booth_config *conf, const char *site,
struct booth_site **node, int any_type)
{
struct booth_site *n;
int i;

if (!booth_conf)
if (conf == NULL) {
return 0;
}

if (!strcmp(site, OTHER_SITE))
return get_other_site(node);
if (!strcmp(site, OTHER_SITE)) {
return get_other_site(conf, node);
}

FOREACH_NODE(i, n) {
FOREACH_NODE(conf, i, n) {
if ((n->type == SITE || any_type) &&
strncmp(n->addr_string, site, sizeof(n->addr_string)) == 0) {
*node = n;
Expand All @@ -1036,7 +1041,8 @@ int find_site_by_name(char *site, struct booth_site **node, int any_type)
return 0;
}

int find_site_by_id(uint32_t site_id, struct booth_site **node)
int find_site_by_id(struct booth_config *conf,
uint32_t site_id, struct booth_site **node)
{
struct booth_site *n;
int i;
Expand All @@ -1046,10 +1052,11 @@ int find_site_by_id(uint32_t site_id, struct booth_site **node)
return 1;
}

if (!booth_conf)
if (conf == NULL) {
return 0;
}

FOREACH_NODE(i, n) {
FOREACH_NODE(conf, i, n) {
if (n->site_id == site_id) {
*node = n;
return 1;
Expand All @@ -1059,7 +1066,6 @@ int find_site_by_id(uint32_t site_id, struct booth_site **node)
return 0;
}


const char *type_to_string(int type)
{
switch (type)
Expand Down
40 changes: 38 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
#include "booth.h"
#include "timer.h"
#include "raft.h"

/* Forward declaration of the booth config structure that will be defined later.
* This is necessary here because transport.h references booth_config, but this
* file also references transport_layer_t. We need some way to break the
* circular dependency.
*
* This also means that config.h must always be included before transport.h in
* any source files.
*/
struct booth_config;

#include "transport.h"


Expand Down Expand Up @@ -355,8 +366,33 @@ int read_config(struct booth_config **conf, const char *path, int type);
*/
int check_config(struct booth_config *conf, int type);

int find_site_by_name(char *site, struct booth_site **node, int any_type);
int find_site_by_id(uint32_t site_id, struct booth_site **node);
/**
* @internal
* Find site in booth configuration by resolved host name
*
* @param[in,out] conf config object to refer to
* @param[in] site name to match against previously resolved host names
* @param[out] node relevant tracked data when found
* @param[in] any_type whether or not to consider also non-site members
*
* @return 0 if nothing found, or 1 when found (node assigned accordingly)
*/
int find_site_by_name(struct booth_config *conf, const char *site,
struct booth_site **node, int any_type);


/**
* @internal
* Find site in booth configuration by a hash (id)
*
* @param[in,out] conf config object to refer to
* @param[in] site_id hash (id) to match against previously resolved ones
* @param[out] node relevant tracked data when found
*
* @return 0 if nothing found, or 1 when found (node assigned accordingly)
*/
int find_site_by_id(struct booth_config *conf, uint32_t site_id,
struct booth_site **node);

const char *type_to_string(int type);

Expand Down
2 changes: 1 addition & 1 deletion src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void wait_child(int sig)
/* use waitpid(2) and not wait(2) in order not to interfere
* with popen(2)/pclose(2) and system(2) used in pacemaker.c
*/
FOREACH_TICKET(i, tk) {
_FOREACH_TICKET(i, tk) {
if (tk_test.path && tk_test.pid > 0 &&
(tk_test.progstate == EXTPROG_RUNNING ||
tk_test.progstate == EXTPROG_IGNORE) &&
Expand Down
33 changes: 17 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ static void client_dead(int ci)
}

int client_add(int fd, const struct booth_transport *tpt,
void (*workfn)(int ci),
void (*deadfn)(int ci))
workfn_t workfn, void (*deadfn)(int ci))
{
int i;
struct client *c;
Expand Down Expand Up @@ -233,7 +232,7 @@ static int format_peers(char **pdata, unsigned int *len)
return -ENOMEM;

cp = data;
FOREACH_NODE(i, s) {
_FOREACH_NODE(i, s) {
if (s == local)
continue;
strftime(time_str, sizeof(time_str), "%F %T",
Expand Down Expand Up @@ -394,15 +393,15 @@ static int setup_config(struct booth_config **conf, int type)

/* Set "local" pointer, ignoring errors. */
if (cl.type == DAEMON && cl.site[0]) {
if (!find_site_by_name(cl.site, &local, 1)) {
if (!find_site_by_name(booth_conf, cl.site, &local, 1)) {
log_error("Cannot find \"%s\" in the configuration.",
cl.site);
return -EINVAL;
}
local->local = 1;
} else
find_myself(NULL, type == CLIENT || type == GEOSTORE);

} else {
find_myself(booth_conf, NULL, type == CLIENT || type == GEOSTORE);
}

rv = check_config(booth_conf, type);
if (rv < 0)
Expand Down Expand Up @@ -521,17 +520,18 @@ static int process_signals(void)

static int loop(int fd)
{
void (*workfn) (int ci);
workfn_t workfn;
void (*deadfn) (int ci);
int rv, i;

rv = setup_transport();
if (rv < 0)
goto fail;

rv = setup_ticket();
if (rv < 0)
rv = setup_ticket(booth_conf);
if (rv < 0) {
goto fail;
}

rv = write_daemon_state(fd, BOOTHD_STARTED);
if (rv != 0) {
Expand Down Expand Up @@ -559,8 +559,9 @@ static int loop(int fd)

if (pollfds[i].revents & POLLIN) {
workfn = clients[i].workfn;
if (workfn)
workfn(i);
if (workfn) {
workfn(booth_conf, i);
}
}
if (pollfds[i].revents &
(POLLERR | POLLHUP | POLLNVAL)) {
Expand Down Expand Up @@ -708,7 +709,7 @@ static int query_get_string_answer(cmd_request_t cmd)

if (!*cl.site)
site = local;
else if (!find_site_by_name(cl.site, &site, 1)) {
else if (!find_site_by_name(booth_conf, cl.site, &site, 1)) {
log_error("cannot find site \"%s\"", cl.site);
rv = ENOENT;
goto out;
Expand Down Expand Up @@ -782,7 +783,7 @@ static int do_command(cmd_request_t cmd)
if (!*cl.site)
site = local;
else {
if (!find_site_by_name(cl.site, &site, 1)) {
if (!find_site_by_name(booth_conf, cl.site, &site, 1)) {
log_error("Site \"%s\" not configured.", cl.site);
goto out_close;
}
Expand Down Expand Up @@ -837,7 +838,7 @@ static int do_command(cmd_request_t cmd)
if (rv == 1) {
tpt->close(site);
leader_id = ntohl(reply.ticket.leader);
if (!find_site_by_id(leader_id, &site)) {
if (!find_site_by_id(booth_conf, leader_id, &site)) {
log_error("Message with unknown redirect site %x received", leader_id);
rv = -1;
goto out_close;
Expand Down Expand Up @@ -1617,7 +1618,7 @@ static int do_attr(struct booth_config **conf)

case ATTR_SET:
case ATTR_DEL:
rv = do_attr_command(cl.op);
rv = do_attr_command(booth_conf, cl.op);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/manual.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#include "manual.h"

#include "config.h"
#include "transport.h"
#include "ticket.h"
#include "config.h"
#include "log.h"
#include "request.h"

Expand Down
Loading