Skip to content

Commit

Permalink
Refactor: get rid of "struct command_line" as a global variable
Browse files Browse the repository at this point in the history
At that occasion, make "struct booth_config" contain "path_to_self"
as it is handy to have it there and not to rely on presence of the
former struct.

Signed-off-by: Jan Pokorný <[email protected]>
  • Loading branch information
jnpkrn committed Jan 30, 2020
1 parent 389c5bb commit 2246488
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 172 deletions.
32 changes: 17 additions & 15 deletions src/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ void print_geostore_usage(void)
* ticket, attr name, attr value
*/

int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)
int test_attr_reply(struct command_line *cl, cmd_result_t reply_code)
{
int rv = 0;
const char *op_str = "";

switch (cmd) {
switch (cl->type) {
case ATTR_SET: op_str = "set"; break;
case ATTR_GET: op_str = "get"; break;
case ATTR_LIST: op_str = "list"; break;
Expand All @@ -86,7 +86,7 @@ int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)

case RLT_SYNC_SUCC:
case RLT_SUCCESS:
if (cmd == ATTR_SET)
if (cl->type == ATTR_SET)
log_info("%s succeeded!", op_str);
rv = 0;
break;
Expand All @@ -98,13 +98,13 @@ int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)

case RLT_INVALID_ARG:
log_error("ticket \"%s\" does not exist",
cl.attr_msg.attr.tkt_id);
cl->attr_msg.attr.tkt_id);
rv = 1;
break;

case RLT_NO_SUCH_ATTR:
log_error("attribute \"%s\" not set",
cl.attr_msg.attr.name);
cl->attr_msg.attr.name);
rv = 1;
break;

Expand Down Expand Up @@ -149,7 +149,7 @@ static int read_server_reply(
return rv;
}

int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
int do_attr_command(struct command_line *cl, struct booth_config *conf_ptr)
{
struct booth_site *site = NULL;
struct boothc_header *header;
Expand All @@ -158,12 +158,13 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
char *msg = NULL;

assert(conf_ptr != NULL && conf_ptr->transport != NULL);
assert(cl != NULL);

if (!*cl.site)
if (*cl->site == '\0')
site = local;
else {
if (!find_site_by_name(conf_ptr, cl.site, &site, 1)) {
log_error("Site \"%s\" not configured.", cl.site);
if (!find_site_by_name(conf_ptr, cl->site, &site, 1)) {
log_error("Site \"%s\" not configured.", cl->site);
goto out_close;
}
}
Expand All @@ -172,21 +173,22 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
if (site == local) {
log_error("We're just an arbitrator, no attributes here.");
} else {
log_error("%s is just an arbitrator, no attributes there.", cl.site);
log_error("%s is just an arbitrator, no attributes there.",
cl->site);
}
goto out_close;
}

tpt = *conf_ptr->transport + TCP;

init_header(conf_ptr, &cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
sizeof(cl.attr_msg));
init_header(conf_ptr, &cl->attr_msg.header, cl->type, 0, cl->options,
0, 0, sizeof(cl->attr_msg));

rv = tpt->open(site);
if (rv < 0)
goto out_close;

rv = tpt->send(conf_ptr, site, &cl.attr_msg, sendmsglen(&cl.attr_msg));
rv = tpt->send(conf_ptr, site, &cl->attr_msg, sendmsglen(&cl->attr_msg));
if (rv < 0)
goto out_close;

Expand All @@ -201,7 +203,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
header = (struct boothc_header *)msg;
if (rv < 0) {
if (rv == -1)
(void)test_attr_reply(ntohl(header->result), cmd);
(void) test_attr_reply(cl, ntohl(header->result));
goto out_close;
}
len = ntohl(header->length);
Expand All @@ -217,7 +219,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
rv = -1;
goto out_close;
}
rv = test_attr_reply(ntohl(header->result), cmd);
rv = test_attr_reply(cl, ntohl(header->result));

out_close:
if (tpt && site)
Expand Down
16 changes: 13 additions & 3 deletions src/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@
#include <glib.h>

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

/**
* @internal
* Late handling of the response towards the client
*
* @param[in] cl parsed command line form
* @param[in] reply_code what the inner handling returns
*
* @return 0 on success, -1 on failure, 1 when "cannot serve"
*/
int test_attr_reply(struct command_line *cl, cmd_result_t reply_code);

/**
* @internal
* Carry out a geo-atribute related command
*
* @param[in] cl parsed command line structure
* @param[inout] conf_ptr 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_ptr, cmd_request_t cmd);
int do_attr_command(struct command_line *cl, struct booth_config *conf_ptr);

/**
* @internal
Expand Down
17 changes: 14 additions & 3 deletions src/booth.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,20 @@ int client_add(int fd, const struct booth_transport *tpt,
void (*deadfn)(int ci));

int find_client_by_fd(int fd);
void safe_copy(char *dest, char *value, size_t buflen, const char *description);

/**
* @internal
* Like strncpy, but with explicit protection and better diagnostics
*
* @param[out] dest where to copy the string to
* @param[in] value where to copy the string from
* @param[in] buflen nmaximum size of #dest (incl. trailing '\0', or sizeof)
* @param[in] description how to refer to the target as
*
* @return number of clients tracked (incl. this one)
*/
void safe_copy(char *dest, const char *value, size_t buflen,
const char *description);

/**
* @internal
Expand Down Expand Up @@ -395,8 +408,6 @@ struct command_line {
struct boothc_ticket_msg msg;
struct boothc_attr_msg attr_msg;
};
extern struct command_line cl;



/* http://gcc.gnu.org/onlinedocs/gcc/Typeof.html */
Expand Down
4 changes: 4 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,10 @@ int read_config(struct booth_config **conf_pptr,
goto out;
}

safe_copy((*conf_pptr)->path_to_self, path,
sizeof((*conf_pptr)->path_to_self),
"path to config file itself");

poll_timeout = min(POLL_TIMEOUT, min_timeout/10);
if (!poll_timeout)
poll_timeout = POLL_TIMEOUT;
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ struct booth_config {
int ticket_allocated;
struct ticket_config *ticket;

char path_to_self[BOOTH_PATH_LEN];

const booth_transport_table_t *transport;
};

Expand Down
2 changes: 1 addition & 1 deletion src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int set_booth_env(struct booth_config *conf_ptr,
rv = setenv("BOOTH_TICKET", tk->name, 1) ||
setenv("BOOTH_LOCAL", local->addr_string, 1) ||
setenv("BOOTH_CONF_NAME", conf_ptr->name, 1) ||
setenv("BOOTH_CONF_PATH", cl.configfile, 1) ||
setenv("BOOTH_CONF_PATH", conf_ptr->path_to_self, 1) ||
setenv("BOOTH_TICKET_EXPIRES", expires, 1);

if (rv) {
Expand Down
Loading

0 comments on commit 2246488

Please sign in to comment.