From 5251544b6a8c895000841356714ffbae50231a7d Mon Sep 17 00:00:00 2001 From: zouzhimin Date: Mon, 25 Aug 2025 17:29:13 +0800 Subject: [PATCH] Coding style improvements to ticket.c (cherry picked from commit 935b54b61b0d35b342dee7396b38f09dd855dfb6) --- ...oding-style-improvements-to-ticket.c.patch | 1901 +++++++++++++++++ booth.spec | 11 +- 2 files changed, 1909 insertions(+), 3 deletions(-) create mode 100644 backport-Coding-style-improvements-to-ticket.c.patch diff --git a/backport-Coding-style-improvements-to-ticket.c.patch b/backport-Coding-style-improvements-to-ticket.c.patch new file mode 100644 index 0000000..a933424 --- /dev/null +++ b/backport-Coding-style-improvements-to-ticket.c.patch @@ -0,0 +1,1901 @@ +From 3356b4f23ee93c8d11379467424d9148574b0f0a Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 10:13:27 -0400 +Subject: [PATCH 1/8] Refactor: Add braces around conditional blocks. + +--- + src/ticket.c | 64 ++++++++++++++++++++++++++++++++++------------------ + 1 file changed, 42 insertions(+), 22 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index 53e5288..bb4db5b 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -51,9 +51,11 @@ extern int TIME_RES; + int check_max_len_valid(const char *s, int max) + { + int i; +- for(i=0; idelay_commit)) ++ if (!is_time_set(&tk->delay_commit)) { + return 0; ++ } + + if (is_past(&tk->delay_commit) || all_sites_replied(tk)) { + if (tk->leader == local) { +@@ -140,11 +143,13 @@ static int ticket_dangerous(struct ticket_config *tk) + + int ticket_write(struct ticket_config *tk) + { +- if (local->type != SITE) ++ if (local->type != SITE) { + return -EINVAL; ++ } + +- if (ticket_dangerous(tk)) ++ if (ticket_dangerous(tk)) { + return 1; ++ } + + if (tk->leader == local) { + if (tk->state != ST_LEADER) { +@@ -215,17 +220,20 @@ int check_attr_prereq(struct ticket_config *tk, grant_type_e grant_type) + for (el = g_list_first(tk->attr_prereqs); el; el = g_list_next(el)) + { + ap = (struct attr_prereq *)el->data; +- if (ap->grant_type != grant_type) ++ if (ap->grant_type != grant_type) { + continue; ++ } + geo_ap = (struct geo_attr *)g_hash_table_lookup(tk->attr, ap->attr_name); + switch(ap->op) { + case ATTR_OP_EQ: +- if (!attr_found(geo_ap, ap)) ++ if (!attr_found(geo_ap, ap)) { + goto fail; ++ } + break; + case ATTR_OP_NE: +- if (attr_found(geo_ap, ap)) ++ if (attr_found(geo_ap, ap)) { + goto fail; ++ } + break; + default: + break; +@@ -252,8 +260,9 @@ static int do_ext_prog(struct ticket_config *tk, + { + int rv = 0; + +- if (!tk_test.path) ++ if (!tk_test.path) { + return 0; ++ } + + switch(tk_test.progstate) { + case EXTPROG_IDLE: +@@ -293,8 +302,9 @@ static int acquire_ticket(struct ticket_config *tk, cmd_reason_t reason) + { + int rv; + +- if (reason == OR_ADMIN && check_attr_prereq(tk, GRANT_MANUAL)) ++ if (reason == OR_ADMIN && check_attr_prereq(tk, GRANT_MANUAL)) { + return RLT_ATTR_PREREQ; ++ } + + switch(do_ext_prog(tk, 0)) { + case 0: +@@ -325,8 +335,9 @@ static int do_grant_ticket(struct ticket_config *tk, int options) + + tk_log_info("granting ticket"); + +- if (tk->leader == local) ++ if (tk->leader == local) { + return RLT_SUCCESS; ++ } + if (is_owned(tk)) { + if (is_manual(tk) && (options & OPT_IMMEDIATE)) { + /* -F flag has been used while granting a manual ticket. +@@ -607,19 +618,21 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + } + } else { + if (!tk->leader || tk->leader == no_leader) { +- if (sender) ++ if (sender) { + tk_log_info("ticket is not granted"); +- else ++ } else { + tk_log_info("ticket is not granted (from CIB)"); ++ } + set_state(tk, ST_INIT); + } else { +- if (sender) ++ if (sender) { + tk_log_info("ticket granted to %s (says %s)", + site_string(tk->leader), + tk->leader == sender ? "they" : site_string(sender)); +- else ++ } else { + tk_log_info("ticket granted to %s (from CIB)", + site_string(tk->leader)); ++ } + set_state(tk, ST_FOLLOWER); + /* just make sure that we check the ticket soon */ + set_next_state(tk, ST_FOLLOWER); +@@ -661,15 +674,17 @@ int ticket_answer_list(struct booth_config *conf, int fd) + struct boothc_hdr_msg hdr; + + rv = list_ticket(conf, &data, &olen); +- if (rv < 0) ++ if (rv < 0) { + goto out; ++ } + + init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + olen); + rv = send_header_plus(fd, &hdr, data, olen); + + out: +- if (data) ++ if (data) { + free(data); ++ } + return rv; + } + +@@ -813,8 +828,9 @@ int leader_update_ticket(struct ticket_config *tk) + int rv = 0, rv2; + timetype now; + +- if (tk->ticket_updated >= 2) ++ if (tk->ticket_updated >= 2) { + return 0; ++ } + + /* for manual tickets, we don't set time expiration */ + if (!is_manual(tk)) { +@@ -1200,8 +1216,9 @@ static void update_acks( + req = ntohl(msg->header.request); + if (req != tk->last_request || + (tk->acks_expected != cmd && +- tk->acks_expected != OP_REJECTED)) ++ tk->acks_expected != OP_REJECTED)) { + return; ++ } + + /* got an ack! */ + tk->acks_received |= sender->bitmask; +@@ -1350,8 +1367,9 @@ void set_ticket_wakeup(struct ticket_config *tk) + + void schedule_election(struct ticket_config *tk, cmd_reason_t reason) + { +- if (local->type != SITE) ++ if (local->type != SITE) { + return; ++ } + + tk->election_reason = reason; + get_time(&tk->next_cron); +@@ -1374,8 +1392,9 @@ char *state_to_string(uint32_t state_ho) + static int current = 0; + + current ++; +- if (current >= sizeof(cache)/sizeof(cache[0])) ++ if (current >= sizeof(cache)/sizeof(cache[0])) { + current = 0; ++ } + + cur = cache + current; + +@@ -1422,8 +1441,9 @@ int send_msg ( + site_string(dest)); + } + +- if (in_msg) ++ if (in_msg) { + req = ntohl(in_msg->header.cmd); ++ } + + init_ticket_msg(&msg, cmd, req, RLT_SUCCESS, 0, valid_tk); + return booth_udp_send_auth(dest, &msg, sendmsglen(&msg)); +-- +2.25.1 + +From e210edf7cde9ad86f8dd6a847e90af93c9a9d5c6 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 10:26:10 -0400 +Subject: [PATCH 2/8] Refactor: Use a consistent style in function headers. + +--- + src/ticket.c | 30 ++++++++++-------------------- + 1 file changed, 10 insertions(+), 20 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index bb4db5b..7a21cc5 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -180,8 +180,7 @@ void save_committed_tkt(struct ticket_config *tk) + } + + +-static void ext_prog_failed(struct ticket_config *tk, +- int start_election) ++static void ext_prog_failed(struct ticket_config *tk, int start_election) + { + if (!is_manual(tk)) { + /* Give it to somebody else. +@@ -255,8 +254,7 @@ fail: + * != 0: executing program failed (or some other failure) + */ + +-static int do_ext_prog(struct ticket_config *tk, +- int start_election) ++static int do_ext_prog(struct ticket_config *tk, int start_election) + { + int rv = 0; + +@@ -753,7 +751,7 @@ reply_now: + } + + int notify_client(struct ticket_config *tk, int client_fd, +- struct boothc_ticket_msg *msg) ++ struct boothc_ticket_msg *msg) + { + struct boothc_ticket_msg omsg; + void (*deadfn) (int ci); +@@ -798,9 +796,9 @@ int notify_client(struct ticket_config *tk, int client_fd, + } + } + +-int ticket_broadcast(struct ticket_config *tk, +- cmd_request_t cmd, cmd_request_t expected_reply, +- cmd_result_t res, cmd_reason_t reason) ++int ticket_broadcast(struct ticket_config *tk, cmd_request_t cmd, ++ cmd_request_t expected_reply, cmd_result_t res, ++ cmd_reason_t reason) + { + struct boothc_ticket_msg msg; + +@@ -1202,12 +1200,8 @@ void tickets_log_info(struct booth_config *conf) + } + + +-static void update_acks( +- struct ticket_config *tk, +- struct booth_site *sender, +- struct booth_site *leader, +- struct boothc_ticket_msg *msg +- ) ++static void update_acks(struct ticket_config *tk, struct booth_site *sender, ++ struct booth_site *leader, struct boothc_ticket_msg *msg) + { + uint32_t cmd; + uint32_t req; +@@ -1418,12 +1412,8 @@ int send_reject(struct booth_site *dest, struct ticket_config *tk, + return booth_udp_send_auth(dest, &msg, sendmsglen(&msg)); + } + +-int send_msg ( +- int cmd, +- struct ticket_config *tk, +- struct booth_site *dest, +- struct boothc_ticket_msg *in_msg +- ) ++int send_msg(int cmd, struct ticket_config *tk, struct booth_site *dest, ++ struct boothc_ticket_msg *in_msg) + { + int req = 0; + struct ticket_config *valid_tk = tk; +-- +2.25.1 + +From 2062330f3c0c205dab8d0b45489077aa2fdfe084 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 10:26:47 -0400 +Subject: [PATCH 3/8] Refactor: Remove the unused TK_LINE definition. + +--- + src/ticket.c | 2 -- + 1 file changed, 2 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index 7a21cc5..e8697ac 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -43,8 +43,6 @@ + #include "request.h" + #include "manual.h" + +-#define TK_LINE 256 +- + extern int TIME_RES; + + /* Untrusted input, must fit (incl. \0) in a buffer of max chars. */ +-- +2.25.1 + +From 3f227400108fa2d9fb1758b4cf46bbce669df9e8 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 10:41:04 -0400 +Subject: [PATCH 4/8] Refactor: Be more consistent with indentation. + +--- + src/ticket.c | 194 ++++++++++++++++++++++----------------------------- + 1 file changed, 83 insertions(+), 111 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index e8697ac..d8201d3 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -119,8 +119,8 @@ static int ticket_dangerous(struct ticket_config *tk) + if (is_past(&tk->delay_commit) || all_sites_replied(tk)) { + if (tk->leader == local) { + tk_log_info("%s, committing to CIB", +- is_past(&tk->delay_commit) ? +- "ticket delay expired" : "all sites replied"); ++ is_past(&tk->delay_commit) ? ++ "ticket delay expired" : "all sites replied"); + } + time_reset(&tk->delay_commit); + no_log_delay_msg = 0; +@@ -152,7 +152,7 @@ int ticket_write(struct ticket_config *tk) + if (tk->leader == local) { + if (tk->state != ST_LEADER) { + tk_log_info("ticket state not yet consistent, " +- "delaying ticket grant to CIB"); ++ "delaying ticket grant to CIB"); + return 1; + } + pcmk_handler.grant_ticket(tk); +@@ -340,8 +340,8 @@ static int do_grant_ticket(struct ticket_config *tk, int options) + * The ticket will be granted and may end up being granted + * on multiple sites */ + tk_log_warn("manual ticket forced to be granted! be aware that " +- "you may end up having two sites holding the same manual " +- "ticket! revoke the ticket from the unnecessary site!"); ++ "you may end up having two sites holding the same manual " ++ "ticket! revoke the ticket from the unnecessary site!"); + } else { + return RLT_OVERGRANT; + } +@@ -351,8 +351,8 @@ static int do_grant_ticket(struct ticket_config *tk, int options) + + if (options & OPT_IMMEDIATE) { + tk_log_warn("granting ticket immediately! If there are " +- "unreachable sites, _hope_ you are sure that they don't " +- "have the ticket!"); ++ "unreachable sites, _hope_ you are sure that they don't " ++ "have the ticket!"); + time_reset(&tk->delay_commit); + } + +@@ -440,41 +440,34 @@ static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *le + /* Manual tickets doesn't have term_expires defined */ + ts = wall_ts(&tk->term_expires); + strftime(timeout_str, sizeof(timeout_str), "%F %T", +- localtime(&ts)); ++ localtime(&ts)); + } else { + strcpy(timeout_str, "INF"); + } + +- if (tk->leader == local && is_time_set(&tk->delay_commit) +- && !is_past(&tk->delay_commit)) { ++ if (tk->leader == local && is_time_set(&tk->delay_commit) && ++ !is_past(&tk->delay_commit)) { + ts = wall_ts(&tk->delay_commit); + strcpy(pending_str, " (commit pending until "); + strftime(pending_str + strlen(" (commit pending until "), +- sizeof(pending_str) - strlen(" (commit pending until ") - 1, +- "%F %T", localtime(&ts)); ++ sizeof(pending_str) - strlen(" (commit pending until ") - 1, ++ "%F %T", localtime(&ts)); + strcat(pending_str, ")"); + } else { + *pending_str = '\0'; + } + +- cp += snprintf(cp, +- alloc - (cp - data), +- "ticket: %s, leader: %s", +- tk->name, +- ticket_leader_string(tk)); ++ cp += snprintf(cp, alloc - (cp - data), ++ "ticket: %s, leader: %s", tk->name, ++ ticket_leader_string(tk)); + + if (is_owned(tk)) { +- cp += snprintf(cp, +- alloc - (cp - data), +- ", expires: %s%s", +- timeout_str, +- pending_str); ++ cp += snprintf(cp, alloc - (cp - data), ++ ", expires: %s%s", timeout_str, pending_str); + } + + if (is_manual(tk)) { +- cp += snprintf(cp, +- alloc - (cp - data), +- " [manual mode]"); ++ cp += snprintf(cp, alloc - (cp - data), " [manual mode]"); + } + + cp += snprintf(cp, alloc - (cp - data), "\n"); +@@ -489,29 +482,23 @@ static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *le + multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); + + if (multiple_grant_warning_length > 1) { +- cp += snprintf(cp, +- alloc - (cp - data), +- "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters +- tk->name); ++ cp += snprintf(cp, alloc - (cp - data), ++ "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters ++ tk->name); + + FOREACH_NODE(conf, site_index, site) { + if (tk->sites_where_granted[site_index] > 0) { +- cp += snprintf(cp, +- alloc - (cp - data), +- "%s", +- site_string(site)); ++ cp += snprintf(cp, alloc - (cp - data), ++ "%s", site_string(site)); + + if (--multiple_grant_warning_length > 0) { +- cp += snprintf(cp, +- alloc - (cp - data), +- ", "); ++ cp += snprintf(cp, alloc - (cp - data), ", "); + } + } + } + +- cp += snprintf(cp, +- alloc - (cp - data), +- ". Revoke the ticket from the faulty sites.\n"); // ~45 characters ++ cp += snprintf(cp, alloc - (cp - data), ++ ". Revoke the ticket from the faulty sites.\n"); // ~45 characters + } + } + +@@ -560,22 +547,22 @@ static void log_reacquire_reason(struct ticket_config *tk) + where_granted = "granted here"; + } else { + snprintf(buff, sizeof(buff), "granted to %s", +- site_string(tk->leader)); ++ site_string(tk->leader)); + where_granted = buff; + } + + if (!valid) { +- tk_log_warn("%s, but not valid " +- "anymore (will try to reacquire)", where_granted); ++ tk_log_warn("%s, but not valid anymore (will try to reacquire)", ++ where_granted); + } + if (tk->is_granted && tk->leader != local) { + if (tk->leader && tk->leader != no_leader) { + tk_log_error("granted here, but also %s, " +- "that's really too bad (will try to reacquire)", +- where_granted); ++ "that's really too bad (will try to reacquire)", ++ where_granted); + } else { + tk_log_warn("granted here, but we're " +- "not recorded as the grantee (will try to reacquire)"); ++ "not recorded as the grantee (will try to reacquire)"); + } + } + } +@@ -583,9 +570,8 @@ static void log_reacquire_reason(struct ticket_config *tk) + void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + { + if (tk->state == ST_CANDIDATE) { +- tk_log_info("learned from %s about " +- "newer ticket, stopping elections", +- site_string(sender)); ++ tk_log_info("learned from %s about newer ticket, stopping elections", ++ site_string(sender)); + /* there could be rejects coming from others; don't log + * warnings unnecessarily */ + tk->expect_more_rejects = 1; +@@ -596,11 +582,10 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + if (sender == tk->leader && term_time_left(tk)) { + if (tk->is_granted) { + tk_log_warn("ticket was granted here, " +- "but it's live at %s (revoking here)", +- site_string(sender)); ++ "but it's live at %s (revoking here)", ++ site_string(sender)); + } else { +- tk_log_info("ticket live at %s", +- site_string(sender)); ++ tk_log_info("ticket live at %s", site_string(sender)); + } + disown_ticket(tk); + ticket_write(tk); +@@ -623,11 +608,11 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + } else { + if (sender) { + tk_log_info("ticket granted to %s (says %s)", +- site_string(tk->leader), +- tk->leader == sender ? "they" : site_string(sender)); ++ site_string(tk->leader), ++ tk->leader == sender ? "they" : site_string(sender)); + } else { + tk_log_info("ticket granted to %s (from CIB)", +- site_string(tk->leader)); ++ site_string(tk->leader)); + } + set_state(tk, ST_FOLLOWER); + /* just make sure that we check the ticket soon */ +@@ -697,8 +682,7 @@ int process_client_request(struct booth_config *conf, struct client *req_client, + msg = (struct boothc_ticket_msg *)buf; + cmd = ntohl(msg->header.cmd); + if (!check_ticket(conf, msg->ticket.id, &tk)) { +- log_warn("client referenced unknown ticket %s", +- msg->ticket.id); ++ log_warn("client referenced unknown ticket %s", msg->ticket.id); + rv = RLT_INVALID_ARG; + goto reply_now; + } +@@ -707,22 +691,21 @@ int process_client_request(struct booth_config *conf, struct client *req_client, + * an already granted non-manual ticket */ + if ((!is_manual(tk) && (cmd == CMD_GRANT) && is_owned(tk))) { + log_warn("client wants to grant an (already granted!) ticket %s", +- msg->ticket.id); ++ msg->ticket.id); + + rv = RLT_OVERGRANT; + goto reply_now; + } + + if ((cmd == CMD_REVOKE) && !is_owned(tk)) { +- log_info("client wants to revoke a free ticket %s", +- msg->ticket.id); ++ log_info("client wants to revoke a free ticket %s", msg->ticket.id); + rv = RLT_TICKET_IDLE; + goto reply_now; + } + + if ((cmd == CMD_REVOKE) && tk->leader != local) { + tk_log_info("not granted here, redirect to %s", +- ticket_leader_string(tk)); ++ ticket_leader_string(tk)); + rv = RLT_REDIRECT; + goto reply_now; + } +@@ -738,7 +721,7 @@ int process_client_request(struct booth_config *conf, struct client *req_client, + * request for further processing */ + add_req(tk, req_client, msg); + tk_log_debug("queue request %s for client %d", +- state_to_string(cmd), req_client->fd); ++ state_to_string(cmd), req_client->fd); + rc = 0; /* we're not yet done with the message */ + } + +@@ -763,11 +746,11 @@ int notify_client(struct ticket_config *tk, int client_fd, + ci = find_client_by_fd(client_fd); + if (ci < 0) { + tk_log_info("client %d (request %s) left before being notified", +- client_fd, state_to_string(cmd)); ++ client_fd, state_to_string(cmd)); + return 0; + } + tk_log_debug("notifying client %d (request %s)", +- client_fd, state_to_string(cmd)); ++ client_fd, state_to_string(cmd)); + init_ticket_msg(&omsg, CL_RESULT, 0, rv, 0, tk); + rc = send_client_msg(client_fd, &omsg); + +@@ -780,10 +763,10 @@ int notify_client(struct ticket_config *tk, int client_fd, + * the client */ + if (rc) { + tk_log_debug("failed to notify client %d (request %s)", +- client_fd, state_to_string(cmd)); ++ client_fd, state_to_string(cmd)); + } else { + tk_log_debug("client %d (request %s) got final notification", +- client_fd, state_to_string(cmd)); ++ client_fd, state_to_string(cmd)); + } + req_client = clients + ci; + deadfn = req_client->deadfn; +@@ -802,9 +785,9 @@ int ticket_broadcast(struct ticket_config *tk, cmd_request_t cmd, + + init_ticket_msg(&msg, cmd, 0, res, reason, tk); + tk_log_debug("broadcasting '%s' (term=%d, valid=%d)", +- state_to_string(cmd), +- ntohl(msg.ticket.term), +- msg_term_time(&msg)); ++ state_to_string(cmd), ++ ntohl(msg.ticket.term), ++ msg_term_time(&msg)); + + tk->last_request = cmd; + if (expected_reply) { +@@ -877,11 +860,11 @@ static void log_lost_servers(struct booth_config *conf, struct ticket_config *tk + FOREACH_NODE(conf, i, n) { + if (!(tk->acks_received & n->bitmask)) { + tk_log_warn("%s %s didn't acknowledge our %s, " +- "will retry %d times", +- (n->type == ARBITRATOR ? "arbitrator" : "site"), +- site_string(n), +- state_to_string(tk->last_request), +- tk->retries); ++ "will retry %d times", ++ (n->type == ARBITRATOR ? "arbitrator" : "site"), ++ site_string(n), ++ state_to_string(tk->last_request), ++ tk->retries); + } + } + } +@@ -898,9 +881,8 @@ static void resend_msg(struct booth_config *conf, struct ticket_config *tk) + if (!(tk->acks_received & n->bitmask)) { + n->resend_cnt++; + tk_log_debug("resending %s to %s", +- state_to_string(tk->last_request), +- site_string(n) +- ); ++ state_to_string(tk->last_request), ++ site_string(n)); + send_msg(tk->last_request, tk, n, NULL); + } + } +@@ -922,8 +904,8 @@ static void handle_resends(struct booth_config *conf, struct ticket_config *tk) + /* try to reach some sites again if we just stepped down */ + if (tk->last_request == OP_VOTE_FOR) { + tk_log_warn("no answers to our VtFr request to step down (try #%d), " +- "we are alone", +- tk->retry_number); ++ "we are alone", ++ tk->retry_number); + goto just_resend; + } + +@@ -931,13 +913,12 @@ static void handle_resends(struct booth_config *conf, struct ticket_config *tk) + ack_cnt = count_bits(tk->acks_received) - 1; + if (!ack_cnt) { + tk_log_warn("no answers to our request (try #%d), " +- "we are alone", +- tk->retry_number); ++ "we are alone", ++ tk->retry_number); + } else { + tk_log_warn("not enough answers to our request (try #%d): " +- "only got %d answers", +- tk->retry_number, +- ack_cnt); ++ "only got %d answers", ++ tk->retry_number, ack_cnt); + } + } else { + log_lost_servers(conf, tk); +@@ -951,8 +932,7 @@ static int postpone_ticket_processing(struct ticket_config *tk) + { + extern timetype start_time; + +- return tk->start_postpone && +- (-time_left(&start_time) < tk->timeout); ++ return tk->start_postpone && (-time_left(&start_time) < tk->timeout); + } + + #define has_extprog_exited(tk) ((tk)->clu_test.progstate == EXTPROG_EXITED) +@@ -1046,8 +1026,8 @@ static void next_action(struct booth_config *conf, struct ticket_config *tk) + if (!is_manual(tk)) { + /* leader/ticket lost? and we didn't vote yet */ + tk_log_debug("leader: %s, voted_for: %s", +- site_string(tk->leader), +- site_string(tk->voted_for)); ++ site_string(tk->leader), ++ site_string(tk->voted_for)); + if (!tk->leader) { + if (!tk->voted_for || !tk->in_election) { + disown_ticket(tk); +@@ -1111,7 +1091,7 @@ static void ticket_cron(struct booth_config *conf, struct ticket_config *tk) + /* don't process the tickets too early after start */ + if (postpone_ticket_processing(tk)) { + tk_log_debug("ticket processing postponed (start_postpone=%d)", +- tk->start_postpone); ++ tk->start_postpone); + /* but run again soon */ + ticket_activate_timeout(tk); + return; +@@ -1136,9 +1116,8 @@ static void ticket_cron(struct booth_config *conf, struct ticket_config *tk) + * For automatic tickets, losing the ticket must happen + * in _every_ state. + */ +- if ((!is_manual(tk)) && +- is_owned(tk) && is_time_set(&tk->term_expires) +- && is_past(&tk->term_expires)) { ++ if (!is_manual(tk) && is_owned(tk) && is_time_set(&tk->term_expires) && ++ is_past(&tk->term_expires)) { + ticket_lost(tk); + goto out; + } +@@ -1161,7 +1140,7 @@ void process_tickets(struct booth_config *conf) + + FOREACH_TICKET(conf, i, tk) { + if (!has_extprog_exited(tk) && +- is_time_set(&tk->next_cron) && !is_past(&tk->next_cron)) { ++ is_time_set(&tk->next_cron) && !is_past(&tk->next_cron)) { + continue; + } + +@@ -1186,14 +1165,11 @@ void tickets_log_info(struct booth_config *conf) + + FOREACH_TICKET(conf, i, tk) { + ts = wall_ts(&tk->term_expires); +- tk_log_info("state '%s' " +- "term %d " +- "leader %s " +- "expires %-24.24s", +- state_to_string(tk->state), +- tk->current_term, +- ticket_leader_string(tk), +- ctime(&ts)); ++ tk_log_info("state '%s' term %d leader %s expires %-24.24s", ++ state_to_string(tk->state), ++ tk->current_term, ++ ticket_leader_string(tk), ++ ctime(&ts)); + } + } + +@@ -1207,8 +1183,7 @@ static void update_acks(struct ticket_config *tk, struct booth_site *sender, + cmd = ntohl(msg->header.cmd); + req = ntohl(msg->header.request); + if (req != tk->last_request || +- (tk->acks_expected != cmd && +- tk->acks_expected != OP_REJECTED)) { ++ (tk->acks_expected != cmd && tk->acks_expected != OP_REJECTED)) { + return; + } + +@@ -1216,9 +1191,8 @@ static void update_acks(struct ticket_config *tk, struct booth_site *sender, + tk->acks_received |= sender->bitmask; + + if (all_replied(tk) || +- /* we just stepped down, need only one site to start +- * elections */ +- (cmd == OP_REQ_VOTE && tk->last_request == OP_VOTE_FOR)) { ++ /* we just stepped down, need only one site to start elections */ ++ (cmd == OP_REQ_VOTE && tk->last_request == OP_VOTE_FOR)) { + no_resends(tk); + tk->start_postpone = 0; + set_ticket_wakeup(tk); +@@ -1237,7 +1211,7 @@ int ticket_recv(struct booth_config *conf, void *buf, struct booth_site *source) + + if (!check_ticket(conf, msg->ticket.id, &tk)) { + log_warn("got invalid ticket name %s from %s", +- msg->ticket.id, site_string(source)); ++ msg->ticket.id, site_string(source)); + source->invalid_cnt++; + return -EINVAL; + } +@@ -1404,8 +1378,7 @@ int send_reject(struct booth_site *dest, struct ticket_config *tk, + int req = ntohl(in_msg->header.cmd); + struct boothc_ticket_msg msg; + +- tk_log_debug("sending reject to %s", +- site_string(dest)); ++ tk_log_debug("sending reject to %s", site_string(dest)); + init_ticket_msg(&msg, OP_REJECTED, req, code, 0, tk); + return booth_udp_send_auth(dest, &msg, sendmsglen(&msg)); + } +@@ -1425,8 +1398,7 @@ int send_msg(int cmd, struct ticket_config *tk, struct booth_site *dest, + if (tk->state == ST_CANDIDATE && tk->last_valid_tk) { + valid_tk = tk->last_valid_tk; + } +- tk_log_info("sending status to %s", +- site_string(dest)); ++ tk_log_info("sending status to %s", site_string(dest)); + } + + if (in_msg) { +-- +2.25.1 + +From 55e67e7fd7d80e6b8d13a2ad23c366741bad2c5d Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 11:22:59 -0400 +Subject: [PATCH 5/8] Refactor: Unindent various code blocks. + +When we have a condition with a bunch of code under it, these can often +be rewritten to invert the condition and exit which allows all the +previously indented code to be un-indented. This can make the resulting +code easier to follow. + +I haven't changed every single one of these in ticket.c, only the spots +that I think makes it easier to see what's going on. +--- + src/ticket.c | 166 ++++++++++++++++++++++++++++----------------------- + 1 file changed, 91 insertions(+), 75 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index d8201d3..1e44d61 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -68,13 +68,15 @@ int find_ticket_by_name(struct booth_config *conf, const char *ticket, + } + + FOREACH_TICKET(conf, i, tk) { +- if (!strncmp(tk->name, ticket, sizeof(tk->name))) { +- if (found) { +- *found = tk; +- } ++ if (strncmp(tk->name, ticket, sizeof(tk->name))) { ++ continue; ++ } + +- return 1; ++ if (found) { ++ *found = tk; + } ++ ++ return 1; + } + + return 0; +@@ -184,24 +186,28 @@ static void ext_prog_failed(struct ticket_config *tk, int start_election) + /* Give it to somebody else. + * Just send a VOTE_FOR message, so the + * others can start elections. */ +- if (leader_and_valid(tk)) { +- save_committed_tkt(tk); +- reset_ticket(tk); +- ticket_write(tk); +- if (start_election) { +- ticket_broadcast(tk, OP_VOTE_FOR, OP_REQ_VOTE, RLT_SUCCESS, OR_LOCAL_FAIL); +- } ++ if (!leader_and_valid(tk)) { ++ return; ++ } ++ ++ save_committed_tkt(tk); ++ reset_ticket(tk); ++ ticket_write(tk); ++ if (start_election) { ++ ticket_broadcast(tk, OP_VOTE_FOR, OP_REQ_VOTE, RLT_SUCCESS, OR_LOCAL_FAIL); + } + } else { + /* There is not much we can do now because + * the manual ticket cannot be relocated. + * Just warn the user. */ +- if (tk->leader == local) { +- save_committed_tkt(tk); +- reset_ticket(tk); +- ticket_write(tk); +- log_error("external test failed on the specified machine, cannot acquire a manual ticket"); ++ if (tk->leader != local) { ++ return; + } ++ ++ save_committed_tkt(tk); ++ reset_ticket(tk); ++ ticket_write(tk); ++ log_error("external test failed on the specified machine, cannot acquire a manual ticket"); + } + } + +@@ -423,10 +429,12 @@ static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *le + FOREACH_TICKET(conf, i, tk) { + multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); + +- if (multiple_grant_warning_length > 1) { +- // 164: 55 + 45 + 2*number_of_multiple_sites + some margin +- alloc += 164 + BOOTH_NAME_LEN * (1+multiple_grant_warning_length); ++ if (multiple_grant_warning_length <= 1) { ++ continue; + } ++ ++ // 164: 55 + 45 + 2*number_of_multiple_sites + some margin ++ alloc += 164 + BOOTH_NAME_LEN * (1+multiple_grant_warning_length); + } + + data = malloc(alloc); +@@ -481,25 +489,29 @@ static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *le + FOREACH_TICKET(conf, i, tk) { + multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); + +- if (multiple_grant_warning_length > 1) { +- cp += snprintf(cp, alloc - (cp - data), +- "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters +- tk->name); ++ if (multiple_grant_warning_length <= 1) { ++ continue; ++ } + +- FOREACH_NODE(conf, site_index, site) { +- if (tk->sites_where_granted[site_index] > 0) { +- cp += snprintf(cp, alloc - (cp - data), +- "%s", site_string(site)); ++ cp += snprintf(cp, alloc - (cp - data), ++ "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters ++ tk->name); + +- if (--multiple_grant_warning_length > 0) { +- cp += snprintf(cp, alloc - (cp - data), ", "); +- } +- } ++ FOREACH_NODE(conf, site_index, site) { ++ if (tk->sites_where_granted[site_index] <= 0) { ++ continue; + } + + cp += snprintf(cp, alloc - (cp - data), +- ". Revoke the ticket from the faulty sites.\n"); // ~45 characters ++ "%s", site_string(site)); ++ ++ if (--multiple_grant_warning_length > 0) { ++ cp += snprintf(cp, alloc - (cp - data), ", "); ++ } + } ++ ++ cp += snprintf(cp, alloc - (cp - data), ++ ". Revoke the ticket from the faulty sites.\n"); // ~45 characters + } + + *pdata = data; +@@ -812,14 +824,12 @@ int leader_update_ticket(struct ticket_config *tk) + } + + /* for manual tickets, we don't set time expiration */ +- if (!is_manual(tk)) { +- if (tk->ticket_updated < 1) { +- tk->ticket_updated = 1; +- get_time(&now); +- copy_time(&now, &tk->last_renewal); +- set_future_time(&tk->term_expires, tk->term_duration); +- rv = ticket_broadcast(tk, OP_UPDATE, OP_ACK, RLT_SUCCESS, 0); +- } ++ if (!is_manual(tk) && tk->ticket_updated < 1) { ++ tk->ticket_updated = 1; ++ get_time(&now); ++ copy_time(&now, &tk->last_renewal); ++ set_future_time(&tk->term_expires, tk->term_duration); ++ rv = ticket_broadcast(tk, OP_UPDATE, OP_ACK, RLT_SUCCESS, 0); + } + + if (tk->ticket_updated < 2) { +@@ -858,14 +868,15 @@ static void log_lost_servers(struct booth_config *conf, struct ticket_config *tk + } + + FOREACH_NODE(conf, i, n) { +- if (!(tk->acks_received & n->bitmask)) { +- tk_log_warn("%s %s didn't acknowledge our %s, " +- "will retry %d times", +- (n->type == ARBITRATOR ? "arbitrator" : "site"), +- site_string(n), +- state_to_string(tk->last_request), +- tk->retries); ++ if (tk->acks_received & n->bitmask) { ++ continue; + } ++ ++ tk_log_warn("%s %s didn't acknowledge our %s, " ++ "will retry %d times", ++ (n->type == ARBITRATOR ? "arbitrator" : "site"), ++ site_string(n), state_to_string(tk->last_request), ++ tk->retries); + } + } + +@@ -878,13 +889,15 @@ static void resend_msg(struct booth_config *conf, struct ticket_config *tk) + ticket_broadcast(tk, tk->last_request, 0, RLT_SUCCESS, 0); + } else { + FOREACH_NODE(conf, i, n) { +- if (!(tk->acks_received & n->bitmask)) { +- n->resend_cnt++; +- tk_log_debug("resending %s to %s", +- state_to_string(tk->last_request), +- site_string(n)); +- send_msg(tk->last_request, tk, n, NULL); ++ if (tk->acks_received & n->bitmask) { ++ continue; + } ++ ++ n->resend_cnt++; ++ tk_log_debug("resending %s to %s", ++ state_to_string(tk->last_request), ++ site_string(n)); ++ send_msg(tk->last_request, tk, n, NULL); + } + ticket_activate_timeout(tk); + } +@@ -944,12 +957,14 @@ static void process_next_state(struct ticket_config *tk) + switch(tk->next_state) { + case ST_LEADER: + if (has_extprog_exited(tk)) { +- if (tk->state != ST_LEADER) { +- rv = acquire_ticket(tk, OR_ADMIN); +- if (rv != 0) { /* external program failed */ +- tk->outcome = rv; +- foreach_tkt_req(tk, notify_client); +- } ++ if (tk->state == ST_LEADER) { ++ break; ++ } ++ ++ rv = acquire_ticket(tk, OR_ADMIN); ++ if (rv != 0) { /* external program failed */ ++ tk->outcome = rv; ++ foreach_tkt_req(tk, notify_client); + } + } else { + log_reacquire_reason(tk); +@@ -1028,18 +1043,21 @@ static void next_action(struct booth_config *conf, struct ticket_config *tk) + tk_log_debug("leader: %s, voted_for: %s", + site_string(tk->leader), + site_string(tk->voted_for)); +- if (!tk->leader) { +- if (!tk->voted_for || !tk->in_election) { +- disown_ticket(tk); +- if (!new_election(tk, NULL, 1, OR_AGAIN)) { +- ticket_activate_timeout(tk); +- } +- } else { +- /* we should restart elections in case nothing +- * happens in the meantime */ +- tk->in_election = 0; ++ ++ if (tk->leader) { ++ break; ++ } ++ ++ if (!tk->voted_for || !tk->in_election) { ++ disown_ticket(tk); ++ if (!new_election(tk, NULL, 1, OR_AGAIN)) { + ticket_activate_timeout(tk); + } ++ } else { ++ /* we should restart elections in case nothing ++ * happens in the meantime */ ++ tk->in_election = 0; ++ ticket_activate_timeout(tk); + } + } else { + /* for manual tickets, also try to acquire ticket on grant +@@ -1072,12 +1090,10 @@ static void next_action(struct booth_config *conf, struct ticket_config *tk) + if (majority_of_bits(tk, tk->acks_received)) { + leader_update_ticket(tk); + } +- } else { ++ } else if (!do_ext_prog(tk, 1)) { + /* this is ticket renewal, run local test */ +- if (!do_ext_prog(tk, 1)) { +- ticket_broadcast(tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0); +- tk->ticket_updated = 0; +- } ++ ticket_broadcast(tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0); ++ tk->ticket_updated = 0; + } + break; + +-- +2.25.1 + +From f3a2cfefcaf40b29dc7c216e52cf8ed9a1b08182 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 11:59:56 -0400 +Subject: [PATCH 6/8] Refactor: Rewrite list_ticket to use GString for building + strings. + +* Skip all the counting at the beginning to decide how much memory to + allocate, and instead allocate a string that should typically be large + enough. However, GStrings can grow dynamically so it's not critical + we get this completely correct. + +* Remove all the snprintf calls that have to keep track of how much + they've already allocated. Instead, use g_string_append_printf or + g_string_append as appropriate. + +* Simplify the pending_str block. Use asprintf to build up this string + since we have to pass a static buffer to strftime. Then if we ever + created a pending string, append and free it later. + +* Simplify the multiple_grant_warning_length check to not use a prefix + decrement operator. We can just compare it to the number we actually + care about instead. + +* Return a char * and free the GString internally so callers don't have + to care about the implementation, and don't also return the length of + the string. Callers can check that when they need to know it. +--- + src/ticket.c | 106 +++++++++++++++++++++++---------------------------- + 1 file changed, 48 insertions(+), 58 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index 1e44d61..dcbdf03 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -409,40 +409,22 @@ static int number_sites_marked_as_granted(struct booth_config *conf, + } + + +-static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *len) ++static int list_ticket(struct booth_config *conf, char **pdata) + { ++ GString *s = NULL; + struct ticket_config *tk; + struct booth_site *site; + char timeout_str[64]; +- char pending_str[64]; +- char *data, *cp; ++ char *pending_str = NULL; + int i, site_index; +- size_t alloc; + time_t ts; +- int multiple_grant_warning_length = 0; + +- *pdata = NULL; +- *len = 0; ++ s = g_string_sized_new(BUFSIZ); + +- alloc = conf->ticket_count * (BOOTH_NAME_LEN * 2 + 128 + 16); +- +- FOREACH_TICKET(conf, i, tk) { +- multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); +- +- if (multiple_grant_warning_length <= 1) { +- continue; +- } +- +- // 164: 55 + 45 + 2*number_of_multiple_sites + some margin +- alloc += 164 + BOOTH_NAME_LEN * (1+multiple_grant_warning_length); +- } +- +- data = malloc(alloc); +- if (!data) { ++ if (s == NULL) { + return -ENOMEM; + } + +- cp = data; + FOREACH_TICKET(conf, i, tk) { + if ((!is_manual(tk)) && is_time_set(&tk->term_expires)) { + /* Manual tickets doesn't have term_expires defined */ +@@ -455,67 +437,76 @@ static int list_ticket(struct booth_config *conf, char **pdata, unsigned int *le + + if (tk->leader == local && is_time_set(&tk->delay_commit) && + !is_past(&tk->delay_commit)) { ++ char until_str[64]; ++ int rc; ++ + ts = wall_ts(&tk->delay_commit); +- strcpy(pending_str, " (commit pending until "); +- strftime(pending_str + strlen(" (commit pending until "), +- sizeof(pending_str) - strlen(" (commit pending until ") - 1, +- "%F %T", localtime(&ts)); +- strcat(pending_str, ")"); +- } else { +- *pending_str = '\0'; ++ strftime(until_str, sizeof(until_str), "%F %T", ++ localtime(&ts)); ++ rc = asprintf(&pending_str, " (commit pending until %s)", ++ until_str); ++ ++ if (rc < 0) { ++ g_string_free(s, TRUE); ++ return -ENOMEM; ++ } + } + +- cp += snprintf(cp, alloc - (cp - data), +- "ticket: %s, leader: %s", tk->name, +- ticket_leader_string(tk)); ++ g_string_append_printf(s, "ticket: %s, leader: %s", tk->name, ++ ticket_leader_string(tk)); + + if (is_owned(tk)) { +- cp += snprintf(cp, alloc - (cp - data), +- ", expires: %s%s", timeout_str, pending_str); ++ g_string_append_printf(s, ", expires: %s", timeout_str); ++ ++ if (pending_str != NULL) { ++ g_string_append(s, pending_str); ++ } + } + + if (is_manual(tk)) { +- cp += snprintf(cp, alloc - (cp - data), " [manual mode]"); ++ g_string_append(s, " [manual mode]"); + } + +- cp += snprintf(cp, alloc - (cp - data), "\n"); ++ g_string_append(s, "\n"); + +- if (alloc - (cp - data) <= 0) { +- free(data); +- return -ENOMEM; ++ if (pending_str != NULL) { ++ free(pending_str); ++ pending_str = NULL; + } + } + + FOREACH_TICKET(conf, i, tk) { +- multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); ++ int multiple_grant_warning_length = number_sites_marked_as_granted(conf, tk); + + if (multiple_grant_warning_length <= 1) { + continue; + } + +- cp += snprintf(cp, alloc - (cp - data), +- "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters +- tk->name); ++ g_string_append_printf(s, "\nWARNING: The ticket %s is granted to multiple sites: ", ++ tk->name); + + FOREACH_NODE(conf, site_index, site) { + if (tk->sites_where_granted[site_index] <= 0) { + continue; + } + +- cp += snprintf(cp, alloc - (cp - data), +- "%s", site_string(site)); ++ g_string_append(s, site_string(site)); + +- if (--multiple_grant_warning_length > 0) { +- cp += snprintf(cp, alloc - (cp - data), ", "); ++ multiple_grant_warning_length--; ++ if (multiple_grant_warning_length > 0) { ++ g_string_append(s, ", "); + } + } + +- cp += snprintf(cp, alloc - (cp - data), +- ". Revoke the ticket from the faulty sites.\n"); // ~45 characters ++ g_string_append(s, ". Revoke the ticket from the faulty sites.\n"); + } + +- *pdata = data; +- *len = cp - data; ++ *pdata = strdup(s->str); ++ g_string_free(s, TRUE); ++ ++ if (*pdata == NULL) { ++ return -ENOMEM; ++ } + + return 0; + } +@@ -661,21 +652,20 @@ int setup_ticket(struct booth_config *conf) + + int ticket_answer_list(struct booth_config *conf, int fd) + { +- char *data; ++ char *data = NULL; + int rv; +- unsigned int olen; + struct boothc_hdr_msg hdr; + +- rv = list_ticket(conf, &data, &olen); ++ rv = list_ticket(conf, &data); + if (rv < 0) { + goto out; + } + +- init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + olen); +- rv = send_header_plus(fd, &hdr, data, olen); ++ init_header(&hdr.header, CL_LIST, 0, 0, RLT_SUCCESS, 0, sizeof(hdr) + strlen(data)); ++ rv = send_header_plus(fd, &hdr, data, strlen(data)); + + out: +- if (data) { ++ if (data != NULL) { + free(data); + } + return rv; +-- +2.25.1 + +From 7a4b08be2da1a40b2766cf904ecf47fc69215ce2 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 12:08:57 -0400 +Subject: [PATCH 7/8] Refactor: Remove unnecessary parens. + +--- + src/ticket.c | 10 +++++----- + 1 file changed, 5 insertions(+), 5 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index dcbdf03..28baab4 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -426,7 +426,7 @@ static int list_ticket(struct booth_config *conf, char **pdata) + } + + FOREACH_TICKET(conf, i, tk) { +- if ((!is_manual(tk)) && is_time_set(&tk->term_expires)) { ++ if (!is_manual(tk) && is_time_set(&tk->term_expires)) { + /* Manual tickets doesn't have term_expires defined */ + ts = wall_ts(&tk->term_expires); + strftime(timeout_str, sizeof(timeout_str), "%F %T", +@@ -691,7 +691,7 @@ int process_client_request(struct booth_config *conf, struct client *req_client, + + /* Perform the initial check before granting + * an already granted non-manual ticket */ +- if ((!is_manual(tk) && (cmd == CMD_GRANT) && is_owned(tk))) { ++ if (!is_manual(tk) && cmd == CMD_GRANT && is_owned(tk)) { + log_warn("client wants to grant an (already granted!) ticket %s", + msg->ticket.id); + +@@ -699,13 +699,13 @@ int process_client_request(struct booth_config *conf, struct client *req_client, + goto reply_now; + } + +- if ((cmd == CMD_REVOKE) && !is_owned(tk)) { ++ if (cmd == CMD_REVOKE && !is_owned(tk)) { + log_info("client wants to revoke a free ticket %s", msg->ticket.id); + rv = RLT_TICKET_IDLE; + goto reply_now; + } + +- if ((cmd == CMD_REVOKE) && tk->leader != local) { ++ if (cmd == CMD_REVOKE && tk->leader != local) { + tk_log_info("not granted here, redirect to %s", + ticket_leader_string(tk)); + rv = RLT_REDIRECT; +@@ -756,7 +756,7 @@ int notify_client(struct ticket_config *tk, int client_fd, + init_ticket_msg(&omsg, CL_RESULT, 0, rv, 0, tk); + rc = send_client_msg(client_fd, &omsg); + +- if (rc == 0 && ((rv == RLT_MORE) || ++ if (rc == 0 && (rv == RLT_MORE || + (rv == RLT_CIB_PENDING && (options & OPT_WAIT_COMMIT)))) { + /* more to do here, keep the request */ + return 1; +-- +2.25.1 + +From e747e0d2523e3751c437e888a359d18c7d89c3b5 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Wed, 4 Sep 2024 12:31:31 -0400 +Subject: [PATCH 8/8] Refactor: Be more consistent with whitespace. + +--- + src/ticket.c | 90 +++++++++++++++++++++++++++++++++------------------- + 1 file changed, 57 insertions(+), 33 deletions(-) + +diff --git a/src/ticket.c b/src/ticket.c +index 28baab4..c9147d8 100644 +--- a/src/ticket.c ++++ b/src/ticket.c +@@ -1,17 +1,17 @@ +-/* ++/* + * Copyright (C) 2011 Jiaju Zhang + * Copyright (C) 2013-2014 Philipp Marek +- * ++ * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. +- * ++ * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. +- * ++ * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +@@ -48,12 +48,12 @@ extern int TIME_RES; + /* Untrusted input, must fit (incl. \0) in a buffer of max chars. */ + int check_max_len_valid(const char *s, int max) + { +- int i; +- for(i=0; idelay_commit) ? + "ticket delay expired" : "all sites replied"); + } ++ + time_reset(&tk->delay_commit); + no_log_delay_msg = 0; + return 0; +@@ -131,6 +132,7 @@ static int ticket_dangerous(struct ticket_config *tk) + + tdiff = time_left(&tk->delay_commit); + tk_log_debug("delay ticket commit for another " intfmt(tdiff)); ++ + if (!no_log_delay_msg) { + tk_log_info("delaying ticket commit to CIB for " intfmt(tdiff)); + tk_log_info("(or all sites are reached)"); +@@ -140,7 +142,6 @@ static int ticket_dangerous(struct ticket_config *tk) + return 1; + } + +- + int ticket_write(struct ticket_config *tk) + { + if (local->type != SITE) { +@@ -161,25 +162,25 @@ int ticket_write(struct ticket_config *tk) + } else { + pcmk_handler.revoke_ticket(tk); + } +- tk->update_cib = 0; + ++ tk->update_cib = 0; + return 0; + } + +- + void save_committed_tkt(struct ticket_config *tk) + { + if (!tk->last_valid_tk) { + tk->last_valid_tk = malloc(sizeof(struct ticket_config)); ++ + if (!tk->last_valid_tk) { + log_error("out of memory"); + return; + } + } ++ + memcpy(tk->last_valid_tk, tk, sizeof(struct ticket_config)); + } + +- + static void ext_prog_failed(struct ticket_config *tk, int start_election) + { + if (!is_manual(tk)) { +@@ -192,7 +193,8 @@ static void ext_prog_failed(struct ticket_config *tk, int start_election) + + save_committed_tkt(tk); + reset_ticket(tk); +- ticket_write(tk); ++ ticket_write(tk); ++ + if (start_election) { + ticket_broadcast(tk, OP_VOTE_FOR, OP_REQ_VOTE, RLT_SUCCESS, OR_LOCAL_FAIL); + } +@@ -206,7 +208,7 @@ static void ext_prog_failed(struct ticket_config *tk, int start_election) + + save_committed_tkt(tk); + reset_ticket(tk); +- ticket_write(tk); ++ ticket_write(tk); + log_error("external test failed on the specified machine, cannot acquire a manual ticket"); + } + } +@@ -222,26 +224,31 @@ int check_attr_prereq(struct ticket_config *tk, grant_type_e grant_type) + + for (el = g_list_first(tk->attr_prereqs); el; el = g_list_next(el)) + { +- ap = (struct attr_prereq *)el->data; ++ ap = (struct attr_prereq *) el->data; + if (ap->grant_type != grant_type) { + continue; + } +- geo_ap = (struct geo_attr *)g_hash_table_lookup(tk->attr, ap->attr_name); ++ ++ geo_ap = (struct geo_attr *) g_hash_table_lookup(tk->attr, ap->attr_name); ++ + switch(ap->op) { + case ATTR_OP_EQ: + if (!attr_found(geo_ap, ap)) { + goto fail; + } + break; ++ + case ATTR_OP_NE: + if (attr_found(geo_ap, ap)) { + goto fail; + } + break; ++ + default: + break; + } + } ++ + return 0; + + fail: +@@ -274,16 +281,19 @@ static int do_ext_prog(struct ticket_config *tk, int start_election) + ext_prog_failed(tk, start_election); + } + break; ++ + case EXTPROG_RUNNING: + /* should never get here, but just in case */ + rv = RUNCMD_MORE; + break; ++ + case EXTPROG_EXITED: + rv = tk_test_exit_status(tk); + if (rv) { + ext_prog_failed(tk, start_election); + } + break; ++ + case EXTPROG_IGNORE: + /* nothing to do here */ + break; +@@ -292,7 +302,6 @@ static int do_ext_prog(struct ticket_config *tk, int start_election) + return rv; + } + +- + /* Try to acquire a ticket + * Could be manual grant or after start (if the ticket is granted + * and still valid in the CIB) +@@ -312,9 +321,11 @@ static int acquire_ticket(struct ticket_config *tk, cmd_reason_t reason) + case 0: + /* everything fine */ + break; ++ + case RUNCMD_MORE: + /* need to wait for the outcome before starting elections */ + return 0; ++ + default: + return RLT_EXT_FAILED; + } +@@ -328,7 +339,6 @@ static int acquire_ticket(struct ticket_config *tk, cmd_reason_t reason) + return rv ? RLT_SYNC_FAIL : 0; + } + +- + /** Try to get the ticket for the local site. + * */ + static int do_grant_ticket(struct ticket_config *tk, int options) +@@ -340,6 +350,7 @@ static int do_grant_ticket(struct ticket_config *tk, int options) + if (tk->leader == local) { + return RLT_SUCCESS; + } ++ + if (is_owned(tk)) { + if (is_manual(tk) && (options & OPT_IMMEDIATE)) { + /* -F flag has been used while granting a manual ticket. +@@ -363,6 +374,7 @@ static int do_grant_ticket(struct ticket_config *tk, int options) + } + + rv = acquire_ticket(tk, OR_ADMIN); ++ + if (rv) { + time_reset(&tk->delay_commit); + return rv; +@@ -408,7 +420,6 @@ static int number_sites_marked_as_granted(struct booth_config *conf, + return result; + } + +- + static int list_ticket(struct booth_config *conf, char **pdata) + { + GString *s = NULL; +@@ -511,7 +522,6 @@ static int list_ticket(struct booth_config *conf, char **pdata) + return 0; + } + +- + void disown_ticket(struct ticket_config *tk) + { + set_leader(tk, NULL); +@@ -558,6 +568,7 @@ static void log_reacquire_reason(struct ticket_config *tk) + tk_log_warn("%s, but not valid anymore (will try to reacquire)", + where_granted); + } ++ + if (tk->is_granted && tk->leader != local) { + if (tk->leader && tk->leader != no_leader) { + tk_log_error("granted here, but also %s, " +@@ -590,6 +601,7 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + } else { + tk_log_info("ticket live at %s", site_string(sender)); + } ++ + disown_ticket(tk); + ticket_write(tk); + set_state(tk, ST_FOLLOWER); +@@ -598,6 +610,7 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + if (tk->state == ST_CANDIDATE) { + set_state(tk, ST_FOLLOWER); + } ++ + set_next_state(tk, ST_LEADER); + } + } else { +@@ -607,6 +620,7 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + } else { + tk_log_info("ticket is not granted (from CIB)"); + } ++ + set_state(tk, ST_INIT); + } else { + if (sender) { +@@ -617,6 +631,7 @@ void update_ticket_state(struct ticket_config *tk, struct booth_site *sender) + tk_log_info("ticket granted to %s (from CIB)", + site_string(tk->leader)); + } ++ + set_state(tk, ST_FOLLOWER); + /* just make sure that we check the ticket soon */ + set_next_state(tk, ST_FOLLOWER); +@@ -636,6 +651,7 @@ int setup_ticket(struct booth_config *conf) + if (!pcmk_handler.load_ticket(conf, tk)) { + update_ticket_state(tk, NULL); + } ++ + tk->update_cib = 1; + } + +@@ -649,7 +665,6 @@ int setup_ticket(struct booth_config *conf) + return 0; + } + +- + int ticket_answer_list(struct booth_config *conf, int fd) + { + char *data = NULL; +@@ -671,7 +686,6 @@ out: + return rv; + } + +- + int process_client_request(struct booth_config *conf, struct client *req_client, + void *buf) + { +@@ -746,11 +760,13 @@ int notify_client(struct ticket_config *tk, int client_fd, + options = ntohl(msg->header.options); + rv = tk->outcome; + ci = find_client_by_fd(client_fd); ++ + if (ci < 0) { + tk_log_info("client %d (request %s) left before being notified", + client_fd, state_to_string(cmd)); + return 0; + } ++ + tk_log_debug("notifying client %d (request %s)", + client_fd, state_to_string(cmd)); + init_ticket_msg(&omsg, CL_RESULT, 0, rv, 0, tk); +@@ -770,11 +786,14 @@ int notify_client(struct ticket_config *tk, int client_fd, + tk_log_debug("client %d (request %s) got final notification", + client_fd, state_to_string(cmd)); + } ++ + req_client = clients + ci; + deadfn = req_client->deadfn; +- if(deadfn) { ++ ++ if (deadfn) { + deadfn(ci); + } ++ + return 0; /* we're done with this request */ + } + } +@@ -792,14 +811,15 @@ int ticket_broadcast(struct ticket_config *tk, cmd_request_t cmd, + msg_term_time(&msg)); + + tk->last_request = cmd; ++ + if (expected_reply) { + expect_replies(tk, expected_reply); + } ++ + ticket_activate_timeout(tk); + return transport()->broadcast_auth(&msg, sendmsglen(&msg)); + } + +- + /* update the ticket on the leader, write it to the CIB, and + send out the update message to others with the new expiry + time +@@ -830,12 +850,14 @@ int leader_update_ticket(struct ticket_config *tk) + tk->outcome = RLT_SUCCESS; + foreach_tkt_req(tk, notify_client); + break; ++ + case 1: + if (tk->outcome != RLT_CIB_PENDING) { + tk->outcome = RLT_CIB_PENDING; + foreach_tkt_req(tk, notify_client); + } + break; ++ + default: + break; + } +@@ -844,7 +866,6 @@ int leader_update_ticket(struct ticket_config *tk) + return rv; + } + +- + static void log_lost_servers(struct booth_config *conf, struct ticket_config *tk) + { + struct booth_site *n; +@@ -889,6 +910,7 @@ static void resend_msg(struct booth_config *conf, struct ticket_config *tk) + site_string(n)); + send_msg(tk->last_request, tk, n, NULL); + } ++ + ticket_activate_timeout(tk); + } + } +@@ -914,6 +936,7 @@ static void handle_resends(struct booth_config *conf, struct ticket_config *tk) + + if (!majority_of_bits(tk, tk->acks_received)) { + ack_cnt = count_bits(tk->acks_received) - 1; ++ + if (!ack_cnt) { + tk_log_warn("no answers to our request (try #%d), " + "we are alone", +@@ -961,20 +984,24 @@ static void process_next_state(struct ticket_config *tk) + acquire_ticket(tk, OR_REACQUIRE); + } + break; ++ + case ST_INIT: + no_resends(tk); + start_revoke_ticket(tk); + tk->outcome = RLT_SUCCESS; + foreach_tkt_req(tk, notify_client); + break; ++ + /* wanting to be follower is not much of an ambition; no + * processing, just return; don't reset start_postpone until + * we got some replies to status */ + case ST_FOLLOWER: + return; ++ + default: + break; + } ++ + tk->start_postpone = 0; + } + +@@ -999,6 +1026,7 @@ static void ticket_lost(struct ticket_config *tk) + mark_ticket_as_revoked_from_leader(tk); + reset_ticket(tk); + set_state(tk, ST_FOLLOWER); ++ + if (local->type == SITE) { + ticket_write(tk); + schedule_election(tk, reason); +@@ -1085,6 +1113,7 @@ static void next_action(struct booth_config *conf, struct ticket_config *tk) + ticket_broadcast(tk, OP_HEARTBEAT, OP_ACK, RLT_SUCCESS, 0); + tk->ticket_updated = 0; + } ++ + break; + + default: +@@ -1137,7 +1166,6 @@ out: + } + } + +- + void process_tickets(struct booth_config *conf) + { + struct ticket_config *tk; +@@ -1154,6 +1182,7 @@ void process_tickets(struct booth_config *conf) + + copy_time(&tk->next_cron, &last_cron); + ticket_cron(conf, tk); ++ + if (time_cmp(&last_cron, &tk->next_cron, ==)) { + tk_log_debug("nobody set ticket wakeup"); + set_ticket_wakeup(tk); +@@ -1161,8 +1190,6 @@ void process_tickets(struct booth_config *conf) + } + } + +- +- + void tickets_log_info(struct booth_config *conf) + { + struct ticket_config *tk; +@@ -1179,7 +1206,6 @@ void tickets_log_info(struct booth_config *conf) + } + } + +- + static void update_acks(struct ticket_config *tk, struct booth_site *sender, + struct booth_site *leader, struct boothc_ticket_msg *msg) + { +@@ -1231,11 +1257,9 @@ int ticket_recv(struct booth_config *conf, void *buf, struct booth_site *source) + } + + update_acks(tk, source, leader, msg); +- + return raft_answer(tk, source, leader, msg); + } + +- + static void log_next_wakeup(struct ticket_config *tk) + { + int left; +@@ -1254,6 +1278,7 @@ void add_random_delay(struct ticket_config *tk) + + interval_add(&tk->next_cron, rand_time(min(1000, tk->timeout)), &tv); + ticket_next_cron_at(tk, &tv); ++ + if (ANYDEBUG) { + log_next_wakeup(tk); + } +@@ -1303,6 +1328,7 @@ void set_ticket_wakeup(struct ticket_config *tk) + interval_add(&tk->term_expires, tk->acquire_after, &tv); + ticket_next_cron_at(tk, &tv); + } ++ + break; + + default: +@@ -1322,7 +1348,7 @@ void set_ticket_wakeup(struct ticket_config *tk) + * will be solved promptly. + */ + tk_log_debug("manual ticket will be woken up after up to six minutes"); +- ticket_next_cron_in(tk, 60*TIME_RES); ++ ticket_next_cron_in(tk, 60 * TIME_RES); + + /* For manual tickets, no earlier timeout could be set in a similar + * way as it is done in a switch above for automatic tickets. +@@ -1336,7 +1362,6 @@ void set_ticket_wakeup(struct ticket_config *tk) + } + } + +- + void schedule_election(struct ticket_config *tk, cmd_reason_t reason) + { + if (local->type != SITE) { +@@ -1349,7 +1374,6 @@ void schedule_election(struct ticket_config *tk, cmd_reason_t reason) + add_random_delay(tk); + } + +- + int is_manual(struct ticket_config *tk) + { + return (tk->mode == TICKET_MODE_MANUAL) ? 1 : 0; +@@ -1377,7 +1401,6 @@ char *state_to_string(uint32_t state_ho) + return cur->c; + } + +- + int send_reject(struct booth_site *dest, struct ticket_config *tk, + cmd_result_t code, struct boothc_ticket_msg *in_msg) + { +@@ -1404,6 +1427,7 @@ int send_msg(int cmd, struct ticket_config *tk, struct booth_site *dest, + if (tk->state == ST_CANDIDATE && tk->last_valid_tk) { + valid_tk = tk->last_valid_tk; + } ++ + tk_log_info("sending status to %s", site_string(dest)); + } + +-- +2.25.1 + + + diff --git a/booth.spec b/booth.spec index 9843e56..2da809c 100644 --- a/booth.spec +++ b/booth.spec @@ -24,7 +24,7 @@ %bcond_with run_build_tests %bcond_with include_unit_test -%global release 9 +%global release 10 ## User and group to use for nonprivileged services (should be in sync with pacemaker) %global uname hacluster @@ -69,9 +69,11 @@ patch18: backport-Refactor-Use-asprintf-instead-of-a-static-buffer-in-.pa patch19: backport-Build-Remove-build-time-support-for-running-splint.patch patch20: backport-Build-Remove-unneeded-cruft-from-configure.ac.patch patch21: backport-Refactor-Remove-global-booth_conf-variable-in-find_t.patch -patch22: backport-Refactor-number_sites_marked_as_granted-is-now-a-sta.patch -Patch23: backport-Refactor-Remove-global-booth_conf-variable-in-ticket.patch +patch22: backport-Refactor-number_sites_marked_as_granted-is-now-a-sta.patch +Patch23: backport-Refactor-Remove-global-booth_conf-variable-in-ticket.patch Patch24: backport-Refactor-Mark-internal-functions-in-ticket.c-as-stat.patch +#https://github.com/ClusterLabs/booth/pull/153/files +Patch25: backport-Coding-style-improvements-to-ticket.c.patch # direct build process dependencies BuildRequires: autoconf @@ -320,6 +322,9 @@ VERBOSE=1 make check %{_usr}/lib/ocf/resource.d/booth/sharedrsc %changelog +* Mon Jun 30 2025 zouzhimin -1.2-10 +- Coding style improvements to ticket.c + * Tue Dec 03 2024 bizhiyuan -1.2-9 - Refactor: Remove global booth_conf variable in ticket.c. - Refactor: Mark internal functions in ticket.c as static. -- Gitee