Skip to content

Commit 2cd80a7

Browse files
committed
Refactor: make booth_transport part of booth_config, finish "no globals"
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]>
1 parent 87234d7 commit 2cd80a7

File tree

8 files changed

+54
-34
lines changed

8 files changed

+54
-34
lines changed

src/attr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
157157
int len, rv = -1;
158158
char *msg = NULL;
159159

160+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
161+
160162
if (!*cl.site)
161163
site = local;
162164
else {
@@ -175,7 +177,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
175177
goto out_close;
176178
}
177179

178-
tpt = booth_transport + TCP;
180+
tpt = *conf_ptr->transport + TCP;
179181

180182
init_header(conf_ptr, &cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
181183
sizeof(cl.attr_msg));

src/config.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ static int parse_attr_prereq(char *val, struct ticket_config *tk)
512512

513513
extern int poll_timeout;
514514

515-
int read_config(struct booth_config **conf_pptr, const char *path, int type)
515+
int read_config(struct booth_config **conf_pptr,
516+
const booth_transport_table_t *transport, const char *path,
517+
int type)
516518
{
517519
char line[1024];
518520
FILE *fp;
@@ -546,14 +548,13 @@ int read_config(struct booth_config **conf_pptr, const char *path, int type)
546548
memset(*conf_pptr, 0, sizeof(struct booth_config)
547549
+ TICKET_ALLOC * sizeof(struct ticket_config));
548550
ticket_size = TICKET_ALLOC;
549-
551+
(*conf_pptr)->transport = transport;
550552

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

556-
557558
/* Provide safe defaults. -1 is reserved, though. */
558559
(*conf_pptr)->uid = -2;
559560
(*conf_pptr)->gid = -2;

src/config.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ struct booth_config {
324324
int ticket_count;
325325
int ticket_allocated;
326326
struct ticket_config *ticket;
327-
};
328327

329-
extern struct booth_config *booth_conf;
328+
const booth_transport_table_t *transport;
329+
};
330330

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

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

345348
/**
346349
* @internal

src/inline-fn.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ static inline void init_ticket_msg(struct booth_config *conf_ptr,
150150
}
151151
}
152152

153-
/* XXX uses globals: booth_transport, booth_conf */
154-
static inline struct booth_transport const *transport(void)
153+
static inline struct booth_transport const *transport(struct booth_config *conf_ptr)
155154
{
156-
return booth_transport + booth_conf->proto;
155+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
156+
157+
return *conf_ptr->transport + conf_ptr->proto;
157158
}
158159

159160
static inline const char *site_string(const struct booth_site *site)

src/main.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@
7474

7575
#define CLIENT_NALLOC 32
7676

77+
const booth_transport_table_t booth__transport;
78+
7779
static int daemonize = 1;
7880
int enable_stderr = 0;
7981
timetype start_time;
8082

83+
static struct booth_config *booth_conf;
8184

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

106109
int poll_timeout;
107-
108-
109-
110-
struct booth_config *booth_conf;
111110
struct command_line cl;
112111

113112
static void client_alloc(void)
@@ -365,7 +364,7 @@ static int setup_config(struct booth_config **conf_pptr, int type)
365364

366365
assert(conf_pptr != NULL);
367366

368-
rv = read_config(conf_pptr, cl.configfile, type);
367+
rv = read_config(conf_pptr, &booth__transport, cl.configfile, type);
369368
if (rv < 0)
370369
goto out;
371370

@@ -413,17 +412,20 @@ static int setup_config(struct booth_config **conf_pptr, int type)
413412
return rv;
414413
}
415414

416-
static int setup_transport(void)
415+
static int setup_transport(struct booth_config *conf_ptr)
417416
{
418417
int rv;
419418

420-
rv = transport()->init(message_recv);
419+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
420+
421+
rv = transport(conf_ptr)->init(conf_ptr, message_recv);
421422
if (rv < 0) {
422-
log_error("failed to init booth_transport %s", transport()->name);
423+
log_error("failed to init booth_transport %s",
424+
transport(conf_ptr)->name);
423425
goto out;
424426
}
425427

426-
rv = booth_transport[TCP].init(NULL);
428+
rv = (*conf_ptr->transport)[TCP].init(conf_ptr, NULL);
427429
if (rv < 0) {
428430
log_error("failed to init booth_transport[TCP]");
429431
goto out;
@@ -497,7 +499,7 @@ static int loop(struct booth_config *conf_ptr, int fd)
497499
void (*deadfn) (int ci);
498500
int rv, i;
499501

500-
rv = setup_transport();
502+
rv = setup_transport(conf_ptr);
501503
if (rv < 0)
502504
goto fail;
503505

@@ -661,6 +663,8 @@ static int query_get_string_answer(struct booth_config *conf_ptr,
661663
size_t msg_size;
662664
void *request;
663665

666+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
667+
664668
if (cl.type == GEOSTORE) {
665669
test_reply_f = test_attr_reply;
666670
msg_size = sizeof(cl.attr_msg);
@@ -683,7 +687,7 @@ static int query_get_string_answer(struct booth_config *conf_ptr,
683687
goto out;
684688
}
685689

686-
tpt = booth_transport + TCP;
690+
tpt = *conf_ptr->transport + TCP;
687691
rv = tpt->open(site);
688692
if (rv < 0)
689693
goto out_close;
@@ -740,6 +744,8 @@ static int do_command(struct booth_config *conf_ptr,
740744
int reply_cnt = 0, msg_logged = 0;
741745
const char *op_str = "";
742746

747+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
748+
743749
if (cmd == CMD_GRANT)
744750
op_str = "grant";
745751
else if (cmd == CMD_REVOKE)
@@ -749,7 +755,7 @@ static int do_command(struct booth_config *conf_ptr,
749755
site = NULL;
750756

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

754760
if (!*cl.site)
755761
site = local;

src/ticket.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ int ticket_broadcast(struct booth_config *conf_ptr,
839839
expect_replies(tk, expected_reply);
840840
}
841841
ticket_activate_timeout(tk);
842-
return transport()->broadcast_auth(conf_ptr, &msg, sendmsglen(&msg));
842+
return transport(conf_ptr)->broadcast_auth(conf_ptr, &msg,
843+
sendmsglen(&msg));
843844
}
844845

845846

src/transport.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,19 +567,21 @@ int setup_tcp_listener(int test_only)
567567
return s;
568568
}
569569

570-
static int booth_tcp_init(void * unused __attribute__((unused)))
570+
static int booth_tcp_init(struct booth_config *conf_ptr,
571+
void * unused __attribute__((unused)))
571572
{
572573
int rv;
573574

575+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
576+
574577
if (get_local_id() < 0)
575578
return -1;
576579

577580
rv = setup_tcp_listener(0);
578581
if (rv < 0)
579582
return rv;
580583

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

584586
return 0;
585587
}
@@ -851,18 +853,19 @@ static void process_recv(struct booth_config *conf_ptr, int ci)
851853
}
852854
}
853855

854-
static int booth_udp_init(void *f)
856+
static int booth_udp_init(struct booth_config *conf_ptr, void *f)
855857
{
856858
int rv;
857859

860+
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
861+
858862
rv = setup_udp_server();
859863
if (rv < 0)
860864
return rv;
861865

862866
deliver_fn = f;
863-
client_add(local->udp_fd,
864-
booth_transport + UDP,
865-
process_recv, NULL);
867+
client_add(local->udp_fd, *conf_ptr->transport + UDP, process_recv,
868+
NULL);
866869

867870
return 0;
868871
}
@@ -935,7 +938,8 @@ static int booth_udp_exit(void)
935938
}
936939

937940
/* SCTP transport layer has not been developed yet */
938-
static int booth_sctp_init(void *f __attribute__((unused)))
941+
static int booth_sctp_init(struct booth_config *conf_ptr __attribute__((unused)),
942+
void *f __attribute__((unused)))
939943
{
940944
return 0;
941945
}
@@ -963,7 +967,9 @@ static int return_0(void)
963967
{
964968
return 0;
965969
}
966-
const struct booth_transport booth_transport[TRANSPORT_ENTRIES] = {
970+
971+
/* semi-hidden, only main.c to have a knowledge about this */
972+
const booth_transport_table_t booth__transport = {
967973
[TCP] = {
968974
.name = "TCP",
969975
.init = booth_tcp_init,

src/transport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ typedef enum {
4545

4646
struct booth_transport {
4747
const char *name;
48-
int (*init) (void *);
48+
int (*init) (struct booth_config *, void *);
4949
int (*open) (struct booth_site *);
5050
int (*send) (struct booth_config *, struct booth_site *, void *, int);
5151
int (*send_auth) (struct booth_config *, struct booth_site *, void *, int);
@@ -57,7 +57,7 @@ struct booth_transport {
5757
int (*exit) (void);
5858
};
5959

60-
extern const struct booth_transport booth_transport[TRANSPORT_ENTRIES];
60+
typedef struct booth_transport booth_transport_table_t[TRANSPORT_ENTRIES];
6161

6262
/**
6363
* @internal

0 commit comments

Comments
 (0)