Skip to content

Commit a7e8db8

Browse files
committed
Refactor: rest of inline-fn.h to no longer rely on global booth_conf var
The only exception, transport(), is kept since it is intertwined with another global, hence dealing with it is postponed for now. Signed-off-by: Jan Pokorný <[email protected]>
1 parent a432db1 commit a7e8db8

File tree

7 files changed

+73
-59
lines changed

7 files changed

+73
-59
lines changed

src/attr.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
177177

178178
tpt = booth_transport + TCP;
179179

180-
init_header(&cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
181-
sizeof(cl.attr_msg));
180+
init_header(conf_ptr, &cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
181+
sizeof(cl.attr_msg));
182182

183183
rv = tpt->open(site);
184184
if (rv < 0)
@@ -369,8 +369,8 @@ static cmd_result_t attr_get(struct booth_config *conf_ptr,
369369
return RLT_SYNC_FAIL;
370370
}
371371
g_string_printf(attr_val, "%s\n", a->val);
372-
init_header(&hdr.header, ATTR_GET, 0, 0, RLT_SUCCESS, 0,
373-
sizeof(hdr) + attr_val->len);
372+
init_header(conf_ptr, &hdr.header, ATTR_GET, 0, 0, RLT_SUCCESS, 0,
373+
sizeof(hdr) + attr_val->len);
374374
if (send_header_plus(conf_ptr, fd, &hdr, attr_val->str, attr_val->len))
375375
rv = RLT_SYNC_FAIL;
376376
if (attr_val)
@@ -397,8 +397,8 @@ static cmd_result_t attr_list(struct booth_config *conf_ptr,
397397
}
398398
g_hash_table_foreach(tk->attr, append_attr, data);
399399

400-
init_header(&hdr.header, ATTR_LIST, 0, 0, RLT_SUCCESS, 0,
401-
sizeof(hdr) + data->len);
400+
init_header(conf_ptr, &hdr.header, ATTR_LIST, 0, 0, RLT_SUCCESS, 0,
401+
sizeof(hdr) + data->len);
402402
rv = send_header_plus(conf_ptr, fd, &hdr, data->str, data->len);
403403

404404
if (data)
@@ -444,7 +444,7 @@ int process_attr_request(struct booth_config *conf_ptr,
444444
}
445445

446446
reply_now:
447-
init_header(&hdr.header, CL_RESULT, 0, 0, rv, 0, sizeof(hdr));
447+
init_header(conf_ptr, &hdr.header, CL_RESULT, 0, 0, rv, 0, sizeof(hdr));
448448
send_header_plus(conf_ptr, req_client->fd, &hdr, NULL, 0);
449449
return 1;
450450
}

src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ struct booth_config {
328328

329329
extern struct booth_config *booth_conf;
330330

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

333333
/**
334334
* @internal

src/inline-fn.h

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ inline static int is_resend(struct ticket_config *tk)
7777
}
7878

7979

80-
static inline void init_header_bare(struct boothc_header *h) {
80+
static inline void init_header_bare(struct booth_config *conf_ptr,
81+
struct boothc_header *h) {
8182
timetype now;
8283

8384
assert(local && local->site_id);
8485
h->magic = htonl(BOOTHC_MAGIC);
8586
h->version = htonl(BOOTHC_VERSION);
8687
h->from = htonl(local->site_id);
87-
if (is_auth_req()) {
88+
if (is_auth_req(conf_ptr)) {
8889
get_time(&now);
8990
h->opts = htonl(BOOTH_OPT_AUTH);
9091
h->secs = htonl(secs_since_epoch(&now));
@@ -100,13 +101,14 @@ static inline void init_header_bare(struct boothc_header *h) {
100101
*/
101102
#define sendmsglen(msg) ntohl((msg)->header.length)
102103

103-
static inline void init_header(struct boothc_header *h,
104-
int cmd, int request, int options,
105-
int result, int reason, int data_len)
104+
static inline void init_header(struct booth_config *conf_ptr,
105+
struct boothc_header *h, int cmd, int request,
106+
int options, int result, int reason,
107+
int data_len)
106108
{
107-
init_header_bare(h);
109+
init_header_bare(conf_ptr, h);
108110
h->length = htonl(data_len -
109-
(is_auth_req() ? 0 : sizeof(struct hmac)));
111+
(is_auth_req(conf_ptr) ? 0 : sizeof(struct hmac)));
110112
h->cmd = htonl(cmd);
111113
h->request = htonl(request);
112114
h->options = htonl(options);
@@ -125,13 +127,15 @@ extern int TIME_RES, TIME_MULT;
125127
#define set_msg_term_time(msg, tk) \
126128
(msg)->ticket.term_valid_for = htonl(term_time_left(tk)*TIME_MULT/TIME_RES)
127129

128-
static inline void init_ticket_msg(struct boothc_ticket_msg *msg,
129-
int cmd, int request, int rv, int reason,
130-
struct ticket_config *tk)
130+
static inline void init_ticket_msg(struct booth_config *conf_ptr,
131+
struct boothc_ticket_msg *msg, int cmd,
132+
int request, int rv, int reason,
133+
struct ticket_config *tk)
131134
{
132135
assert(sizeof(msg->ticket.id) == sizeof(tk->name));
133136

134-
init_header(&msg->header, cmd, request, 0, rv, reason, sizeof(*msg));
137+
init_header(conf_ptr, &msg->header, cmd, request, 0, rv, reason,
138+
sizeof(*msg));
135139

136140
if (!tk) {
137141
memset(&msg->ticket, 0, sizeof(msg->ticket));
@@ -146,13 +150,12 @@ static inline void init_ticket_msg(struct boothc_ticket_msg *msg,
146150
}
147151
}
148152

149-
153+
/* XXX uses globals: booth_transport, booth_conf */
150154
static inline struct booth_transport const *transport(void)
151155
{
152156
return booth_transport + booth_conf->proto;
153157
}
154158

155-
156159
static inline const char *site_string(const struct booth_site *site)
157160
{
158161
return site ? site->addr_string : "NONE";
@@ -291,24 +294,32 @@ static inline int count_bits(uint64_t val) {
291294
return __builtin_popcount(val);
292295
}
293296

294-
static inline int majority_of_bits(struct ticket_config *tk, uint64_t val)
297+
static inline int majority_of_bits(struct booth_config *conf_ptr,
298+
struct ticket_config *tk, uint64_t val)
295299
{
300+
assert(conf_ptr != NULL);
301+
296302
/* Use ">" to get majority decision, even for an even number
297303
* of participants. */
298-
return count_bits(val) * 2 >
299-
booth_conf->site_count;
304+
return count_bits(val) * 2 > conf_ptr->site_count;
300305
}
301306

302307

303-
static inline int all_replied(struct ticket_config *tk)
308+
static inline int all_replied(struct booth_config *conf_ptr,
309+
struct ticket_config *tk)
304310
{
305-
return !(tk->acks_received ^ booth_conf->all_bits);
311+
assert(conf_ptr != NULL);
312+
313+
return !(tk->acks_received ^ conf_ptr->all_bits);
306314
}
307315

308-
static inline int all_sites_replied(struct ticket_config *tk)
316+
static inline int all_sites_replied(struct booth_config *conf_ptr,
317+
struct ticket_config *tk)
309318
{
310-
return !((tk->acks_received & booth_conf->sites_bits) ^ booth_conf->sites_bits);
311-
}
319+
assert(conf_ptr != NULL);
312320

321+
return !((tk->acks_received & conf_ptr->sites_bits) \
322+
^ conf_ptr->sites_bits);
323+
}
313324

314325
#endif

src/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ void list_peers(struct booth_config *conf_ptr, int fd)
268268
if (format_peers(&data, &olen) < 0)
269269
goto out;
270270

271-
init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + olen);
271+
init_header(conf_ptr, &hdr.header, CL_LIST, 0, 0, RLT_SUCCESS,
272+
0, sizeof(hdr) + olen);
272273
(void) send_header_plus(conf_ptr, fd, &hdr, data, olen);
273274

274275
out:
@@ -358,7 +359,7 @@ static int setup_config(struct booth_config **conf_pptr, int type)
358359
if (rv < 0)
359360
goto out;
360361

361-
if (is_auth_req()) {
362+
if (is_auth_req(booth_conf)) {
362363
rv = read_authkey();
363364
if (rv < 0)
364365
goto out;
@@ -661,7 +662,7 @@ static int query_get_string_answer(cmd_request_t cmd)
661662
header = (struct boothc_header *)request;
662663
data = NULL;
663664

664-
init_header(header, cmd, 0, cl.options, 0, 0, msg_size);
665+
init_header(booth_conf, header, cmd, 0, cl.options, 0, 0, msg_size);
665666

666667
if (!*cl.site)
667668
site = local;
@@ -773,7 +774,7 @@ static int do_command(cmd_request_t cmd)
773774
}
774775

775776
redirect:
776-
init_header(&cl.msg.header, cmd, 0, cl.options, 0, 0, sizeof(cl.msg));
777+
init_header(booth_conf, &cl.msg.header, cmd, 0, cl.options, 0, 0, sizeof(cl.msg));
777778

778779
rv = tpt->open(site);
779780
if (rv < 0)

src/raft.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void won_elections(struct booth_config *conf_ptr,
144144
time_reset(&tk->election_end);
145145
tk->voted_for = NULL;
146146

147-
if (is_time_set(&tk->delay_commit) && all_sites_replied(tk)) {
147+
if (is_time_set(&tk->delay_commit) && all_sites_replied(conf_ptr, tk)) {
148148
time_reset(&tk->delay_commit);
149149
tk_log_debug("reset delay commit as all sites replied");
150150
}
@@ -477,7 +477,7 @@ static int process_ACK(struct booth_config *conf_ptr,
477477
term == tk->current_term &&
478478
leader == tk->leader) {
479479

480-
if (majority_of_bits(tk, tk->acks_received)) {
480+
if (majority_of_bits(conf_ptr, tk, tk->acks_received)) {
481481
/* OK, at least half of the nodes are reachable;
482482
* Update the ticket and send update messages out
483483
*/
@@ -727,7 +727,8 @@ static int answer_REQ_VOTE(struct booth_config *conf_ptr,
727727
}
728728

729729

730-
init_ticket_msg(&omsg, OP_VOTE_FOR, OP_REQ_VOTE, RLT_SUCCESS, 0, tk);
730+
init_ticket_msg(conf_ptr, &omsg, OP_VOTE_FOR, OP_REQ_VOTE,
731+
RLT_SUCCESS, 0, tk);
731732
omsg.ticket.leader = htonl(get_node_id(tk->voted_for));
732733
return booth_udp_send_auth(conf_ptr, sender, &omsg, sendmsglen(&omsg));
733734
}

src/ticket.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int ticket_dangerous(struct booth_config *conf_ptr,
128128
if (!is_time_set(&tk->delay_commit))
129129
return 0;
130130

131-
if (is_past(&tk->delay_commit) || all_sites_replied(tk)) {
131+
if (is_past(&tk->delay_commit) || all_sites_replied(conf_ptr, tk)) {
132132
if (tk->leader == local) {
133133
tk_log_info("%s, committing to CIB",
134134
is_past(&tk->delay_commit) ?
@@ -702,7 +702,8 @@ int ticket_answer_list(struct booth_config *conf_ptr, int fd)
702702
if (rv < 0)
703703
goto out;
704704

705-
init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + olen);
705+
init_header(conf_ptr, &hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0,
706+
sizeof(hdr) + olen);
706707
rv = send_header_plus(conf_ptr, fd, &hdr, data, olen);
707708

708709
out:
@@ -769,7 +770,7 @@ int process_client_request(struct booth_config *conf_ptr,
769770
}
770771

771772
reply_now:
772-
init_ticket_msg(&omsg, CL_RESULT, 0, rv, 0, tk);
773+
init_ticket_msg(conf_ptr, &omsg, CL_RESULT, 0, rv, 0, tk);
773774
send_client_msg(conf_ptr, req_client->fd, &omsg);
774775
return rc;
775776
}
@@ -794,7 +795,7 @@ int notify_client(struct booth_config *conf_ptr, struct ticket_config *tk,
794795
}
795796
tk_log_debug("notifying client %d (request %s)",
796797
client_fd, state_to_string(cmd));
797-
init_ticket_msg(&omsg, CL_RESULT, 0, rv, 0, tk);
798+
init_ticket_msg(conf_ptr, &omsg, CL_RESULT, 0, rv, 0, tk);
798799
rc = send_client_msg(conf_ptr, client_fd, &omsg);
799800

800801
if (rc == 0 && ((rv == RLT_MORE) ||
@@ -827,7 +828,7 @@ int ticket_broadcast(struct booth_config *conf_ptr,
827828
{
828829
struct boothc_ticket_msg msg;
829830

830-
init_ticket_msg(&msg, cmd, 0, res, reason, tk);
831+
init_ticket_msg(conf_ptr, &msg, cmd, 0, res, reason, tk);
831832
tk_log_debug("broadcasting '%s' (term=%d, valid=%d)",
832833
state_to_string(cmd),
833834
ntohl(msg.ticket.term),
@@ -963,7 +964,7 @@ static void handle_resends(struct booth_config *conf_ptr,
963964
goto just_resend;
964965
}
965966

966-
if (!majority_of_bits(tk, tk->acks_received)) {
967+
if (!majority_of_bits(conf_ptr, tk, tk->acks_received)) {
967968
ack_cnt = count_bits(tk->acks_received) - 1;
968969
if (!ack_cnt) {
969970
tk_log_warn("no answers to our request (try #%d), "
@@ -1130,7 +1131,7 @@ static void next_action(struct booth_config *conf_ptr,
11301131
/* timeout or ticket renewal? */
11311132
if (tk->acks_expected) {
11321133
handle_resends(conf_ptr, tk);
1133-
if (majority_of_bits(tk, tk->acks_received)) {
1134+
if (majority_of_bits(conf_ptr, tk, tk->acks_received)) {
11341135
leader_update_ticket(conf_ptr, tk);
11351136
}
11361137
} else {
@@ -1240,12 +1241,10 @@ void tickets_log_info(struct booth_config *conf_ptr)
12401241
}
12411242
}
12421243

1243-
static void update_acks(
1244-
struct ticket_config *tk,
1245-
struct booth_site *sender,
1246-
struct booth_site *leader,
1247-
struct boothc_ticket_msg *msg
1248-
)
1244+
1245+
static void update_acks(struct booth_config *conf_ptr, struct ticket_config *tk,
1246+
struct booth_site *sender, struct booth_site *leader,
1247+
struct boothc_ticket_msg *msg)
12491248
{
12501249
uint32_t cmd;
12511250
uint32_t req;
@@ -1260,7 +1259,7 @@ static void update_acks(
12601259
/* got an ack! */
12611260
tk->acks_received |= sender->bitmask;
12621261

1263-
if (all_replied(tk) ||
1262+
if (all_replied(conf_ptr, tk) ||
12641263
/* we just stepped down, need only one site to start
12651264
* elections */
12661265
(cmd == OP_REQ_VOTE && tk->last_request == OP_VOTE_FOR)) {
@@ -1296,7 +1295,7 @@ int ticket_recv(struct booth_config *conf_ptr, void *buf,
12961295
return -EINVAL;
12971296
}
12981297

1299-
update_acks(tk, source, leader, msg);
1298+
update_acks(conf_ptr, tk, source, leader, msg);
13001299

13011300
return raft_answer(conf_ptr, tk, source, leader, msg);
13021301
}
@@ -1440,6 +1439,7 @@ char *state_to_string(uint32_t state_ho)
14401439
return cur->c;
14411440
}
14421441

1442+
14431443
int send_reject(struct booth_config *conf_ptr, struct booth_site *dest,
14441444
struct ticket_config *tk, cmd_result_t code,
14451445
struct boothc_ticket_msg *in_msg)
@@ -1449,7 +1449,7 @@ int send_reject(struct booth_config *conf_ptr, struct booth_site *dest,
14491449

14501450
tk_log_debug("sending reject to %s",
14511451
site_string(dest));
1452-
init_ticket_msg(&msg, OP_REJECTED, req, code, 0, tk);
1452+
init_ticket_msg(conf_ptr, &msg, OP_REJECTED, req, code, 0, tk);
14531453
return booth_udp_send_auth(conf_ptr, dest, &msg, sendmsglen(&msg));
14541454
}
14551455

@@ -1475,6 +1475,6 @@ int send_msg(struct booth_config *conf_ptr, int cmd, struct ticket_config *tk,
14751475
if (in_msg)
14761476
req = ntohl(in_msg->header.cmd);
14771477

1478-
init_ticket_msg(&msg, cmd, req, RLT_SUCCESS, 0, valid_tk);
1478+
init_ticket_msg(conf_ptr, &msg, cmd, req, RLT_SUCCESS, 0, valid_tk);
14791479
return booth_udp_send_auth(conf_ptr, dest, &msg, sendmsglen(&msg));
14801480
}

src/transport.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ static void process_connection(struct booth_config *conf_ptr, int ci)
490490
return;
491491

492492
send_err:
493-
init_header(&err_reply.header, CL_RESULT, 0, 0, errc, 0, sizeof(err_reply));
493+
init_header(conf_ptr, &err_reply.header, CL_RESULT, 0, 0, errc, 0,
494+
sizeof(err_reply));
494495
send_client_msg(conf_ptr, req_cl->fd, &err_reply);
495496

496497
kill:
@@ -686,7 +687,7 @@ static int add_hmac(struct booth_config *conf_ptr, void *data, int len)
686687

687688
assert(conf_ptr != NULL);
688689

689-
if (!is_auth_req())
690+
if (!is_auth_req(conf_ptr))
690691
return 0;
691692

692693
payload_len = len - sizeof(struct hmac);
@@ -739,7 +740,7 @@ static int booth_tcp_recv_auth(struct booth_config *conf_ptr,
739740
return got;
740741
}
741742
total = got;
742-
if (is_auth_req()) {
743+
if (is_auth_req(conf_ptr)) {
743744
got = booth_tcp_recv(from, (unsigned char *)buf+payload_len, sizeof(struct hmac));
744745
if (got != sizeof(struct hmac)
745746
|| check_auth(conf_ptr, from, buf, len)) {
@@ -1055,11 +1056,11 @@ int check_auth(struct booth_config *conf_ptr, struct booth_site *from,
10551056
int payload_len;
10561057
struct hmac *hp;
10571058

1058-
if (!is_auth_req())
1059-
return 0;
1060-
10611059
assert(conf_ptr != NULL);
10621060

1061+
if (!is_auth_req(conf_ptr))
1062+
return 0;
1063+
10631064
payload_len = len - sizeof(struct hmac);
10641065
if (payload_len < 0) {
10651066
log_error("%s: failed to authenticate, packet too short (size:%d)",

0 commit comments

Comments
 (0)