Skip to content

Commit

Permalink
Refactor: make booth_transport part of booth_config, finish "no globals"
Browse files Browse the repository at this point in the history
Rest of the globals is currently of negligible importance, so these
won't be immediately handled at this time.

Signed-off-by: Jan Pokorný <[email protected]>
  • Loading branch information
jnpkrn committed Jan 30, 2020
1 parent 55c3662 commit 389c5bb
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
int len, rv = -1;
char *msg = NULL;

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

if (!*cl.site)
site = local;
else {
Expand All @@ -175,7 +177,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
goto out_close;
}

tpt = booth_transport + TCP;
tpt = *conf_ptr->transport + TCP;

init_header(conf_ptr, &cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
sizeof(cl.attr_msg));
Expand Down
7 changes: 4 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,9 @@ static int parse_attr_prereq(char *val, struct ticket_config *tk)

extern int poll_timeout;

int read_config(struct booth_config **conf_pptr, const char *path, int type)
int read_config(struct booth_config **conf_pptr,
const booth_transport_table_t *transport, const char *path,
int type)
{
char line[1024];
FILE *fp;
Expand Down Expand Up @@ -546,14 +548,13 @@ int read_config(struct booth_config **conf_pptr, const char *path, int type)
memset(*conf_pptr, 0, sizeof(struct booth_config)
+ TICKET_ALLOC * sizeof(struct ticket_config));
ticket_size = TICKET_ALLOC;

(*conf_pptr)->transport = transport;

(*conf_pptr)->proto = UDP;
(*conf_pptr)->port = BOOTH_DEFAULT_PORT;
(*conf_pptr)->maxtimeskew = BOOTH_DEFAULT_MAX_TIME_SKEW;
(*conf_pptr)->authkey[0] = '\0';


/* Provide safe defaults. -1 is reserved, though. */
(*conf_pptr)->uid = -2;
(*conf_pptr)->gid = -2;
Expand Down
9 changes: 6 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ struct booth_config {
int ticket_count;
int ticket_allocated;
struct ticket_config *ticket;
};

extern struct booth_config *booth_conf;
const booth_transport_table_t *transport;
};

#define is_auth_req(b_) ((b_)->authkey[0] != '\0')

Expand All @@ -335,12 +335,15 @@ extern struct booth_config *booth_conf;
* Parse booth configuration file and store as structured data
*
* @param[inout] conf_ptr config object to free-alloc cycle & fill accordingly
* @param[in] transport transport handlers table
* @param[in] path where the configuration file is expected
* @param[in] type role currently being acted as
*
* @return 0 or negative value (-1 or -errno) on error
*/
int read_config(struct booth_config **conf_ptr, const char *path, int type);
int read_config(struct booth_config **conf_pptr,
const booth_transport_table_t *transport, const char *path,
int type);

/**
* @internal
Expand Down
7 changes: 4 additions & 3 deletions src/inline-fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ static inline void init_ticket_msg(struct booth_config *conf_ptr,
}
}

/* XXX uses globals: booth_transport, booth_conf */
static inline struct booth_transport const *transport(void)
static inline struct booth_transport const *transport(struct booth_config *conf_ptr)
{
return booth_transport + booth_conf->proto;
assert(conf_ptr != NULL && conf_ptr->transport != NULL);

return *conf_ptr->transport + conf_ptr->proto;
}

static inline const char *site_string(const struct booth_site *site)
Expand Down
30 changes: 18 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@

#define CLIENT_NALLOC 32

const booth_transport_table_t booth__transport;

static int daemonize = 1;
int enable_stderr = 0;
timetype start_time;

static struct booth_config *booth_conf;

/** Structure for "clients".
* Filehandles with incoming data get registered here (and in pollfds),
Expand All @@ -104,10 +107,6 @@ typedef enum
} BOOTH_DAEMON_STATE;

int poll_timeout;



struct booth_config *booth_conf;
struct command_line cl;

static void client_alloc(void)
Expand Down Expand Up @@ -365,7 +364,7 @@ static int setup_config(struct booth_config **conf_pptr, int type)

assert(conf_pptr != NULL);

rv = read_config(conf_pptr, cl.configfile, type);
rv = read_config(conf_pptr, &booth__transport, cl.configfile, type);
if (rv < 0)
goto out;

Expand Down Expand Up @@ -413,17 +412,20 @@ static int setup_config(struct booth_config **conf_pptr, int type)
return rv;
}

static int setup_transport(void)
static int setup_transport(struct booth_config *conf_ptr)
{
int rv;

rv = transport()->init(message_recv);
assert(conf_ptr != NULL && conf_ptr->transport != NULL);

rv = transport(conf_ptr)->init(conf_ptr, message_recv);
if (rv < 0) {
log_error("failed to init booth_transport %s", transport()->name);
log_error("failed to init booth_transport %s",
transport(conf_ptr)->name);
goto out;
}

rv = booth_transport[TCP].init(NULL);
rv = (*conf_ptr->transport)[TCP].init(conf_ptr, NULL);
if (rv < 0) {
log_error("failed to init booth_transport[TCP]");
goto out;
Expand Down Expand Up @@ -497,7 +499,7 @@ static int loop(struct booth_config *conf_ptr, int fd)
void (*deadfn) (int ci);
int rv, i;

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

Expand Down Expand Up @@ -661,6 +663,8 @@ static int query_get_string_answer(struct booth_config *conf_ptr,
size_t msg_size;
void *request;

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

if (cl.type == GEOSTORE) {
test_reply_f = test_attr_reply;
msg_size = sizeof(cl.attr_msg);
Expand All @@ -683,7 +687,7 @@ static int query_get_string_answer(struct booth_config *conf_ptr,
goto out;
}

tpt = booth_transport + TCP;
tpt = *conf_ptr->transport + TCP;
rv = tpt->open(site);
if (rv < 0)
goto out_close;
Expand Down Expand Up @@ -740,6 +744,8 @@ static int do_command(struct booth_config *conf_ptr,
int reply_cnt = 0, msg_logged = 0;
const char *op_str = "";

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

if (cmd == CMD_GRANT)
op_str = "grant";
else if (cmd == CMD_REVOKE)
Expand All @@ -749,7 +755,7 @@ static int do_command(struct booth_config *conf_ptr,
site = NULL;

/* Always use TCP for client - at least for now. */
tpt = booth_transport + TCP;
tpt = *conf_ptr->transport + TCP;

if (!*cl.site)
site = local;
Expand Down
3 changes: 2 additions & 1 deletion src/ticket.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ int ticket_broadcast(struct booth_config *conf_ptr,
expect_replies(tk, expected_reply);
}
ticket_activate_timeout(tk);
return transport()->broadcast_auth(conf_ptr, &msg, sendmsglen(&msg));
return transport(conf_ptr)->broadcast_auth(conf_ptr, &msg,
sendmsglen(&msg));
}


Expand Down
24 changes: 15 additions & 9 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,19 +567,21 @@ int setup_tcp_listener(int test_only)
return s;
}

static int booth_tcp_init(void * unused __attribute__((unused)))
static int booth_tcp_init(struct booth_config *conf_ptr,
void * unused __attribute__((unused)))
{
int rv;

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

if (get_local_id() < 0)
return -1;

rv = setup_tcp_listener(0);
if (rv < 0)
return rv;

client_add(rv, booth_transport + TCP,
process_tcp_listener, NULL);
client_add(rv, *conf_ptr->transport + TCP, process_tcp_listener, NULL);

return 0;
}
Expand Down Expand Up @@ -851,18 +853,19 @@ static void process_recv(struct booth_config *conf_ptr, int ci)
}
}

static int booth_udp_init(void *f)
static int booth_udp_init(struct booth_config *conf_ptr, void *f)
{
int rv;

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

rv = setup_udp_server();
if (rv < 0)
return rv;

deliver_fn = f;
client_add(local->udp_fd,
booth_transport + UDP,
process_recv, NULL);
client_add(local->udp_fd, *conf_ptr->transport + UDP, process_recv,
NULL);

return 0;
}
Expand Down Expand Up @@ -935,7 +938,8 @@ static int booth_udp_exit(void)
}

/* SCTP transport layer has not been developed yet */
static int booth_sctp_init(void *f __attribute__((unused)))
static int booth_sctp_init(struct booth_config *conf_ptr __attribute__((unused)),
void *f __attribute__((unused)))
{
return 0;
}
Expand Down Expand Up @@ -963,7 +967,9 @@ static int return_0(void)
{
return 0;
}
const struct booth_transport booth_transport[TRANSPORT_ENTRIES] = {

/* semi-hidden, only main.c to have a knowledge about this */
const booth_transport_table_t booth__transport = {
[TCP] = {
.name = "TCP",
.init = booth_tcp_init,
Expand Down
4 changes: 2 additions & 2 deletions src/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ typedef enum {

struct booth_transport {
const char *name;
int (*init) (void *);
int (*init) (struct booth_config *, void *);
int (*open) (struct booth_site *);
int (*send) (struct booth_config *, struct booth_site *, void *, int);
int (*send_auth) (struct booth_config *, struct booth_site *, void *, int);
Expand All @@ -57,7 +57,7 @@ struct booth_transport {
int (*exit) (void);
};

extern const struct booth_transport booth_transport[TRANSPORT_ENTRIES];
typedef struct booth_transport booth_transport_table_t[TRANSPORT_ENTRIES];

/**
* @internal
Expand Down

0 comments on commit 389c5bb

Please sign in to comment.