diff --git a/src/config.c b/src/config.c index a533da39..f1613ebb 100644 --- a/src/config.c +++ b/src/config.c @@ -986,8 +986,7 @@ static int get_other_site(struct booth_site **node) if (!booth_conf) return 0; - for (i = 0; i < booth_conf->site_count; i++) { - n = booth_conf->site + i; + FOREACH_NODE(i, n) { if (n != local && n->type == SITE) { if (!*node) { *node = n; @@ -1012,8 +1011,7 @@ int find_site_by_name(char *site, struct booth_site **node, int any_type) if (!strcmp(site, OTHER_SITE)) return get_other_site(node); - for (i = 0; i < booth_conf->site_count; i++) { - n = booth_conf->site + i; + FOREACH_NODE(i, n) { if ((n->type == SITE || any_type) && strncmp(n->addr_string, site, sizeof(n->addr_string)) == 0) { *node = n; @@ -1037,8 +1035,7 @@ int find_site_by_id(uint32_t site_id, struct booth_site **node) if (!booth_conf) return 0; - for (i = 0; i < booth_conf->site_count; i++) { - n = booth_conf->site + i; + FOREACH_NODE(i, n) { if (n->site_id == site_id) { *node = n; return 1; diff --git a/src/handler.c b/src/handler.c index a12857eb..9d74f0f6 100644 --- a/src/handler.c +++ b/src/handler.c @@ -130,10 +130,10 @@ void wait_child(int sig) int i, status; struct ticket_config *tk; - /* use waitpid(2) and not wait(2) in order not to interfear + /* use waitpid(2) and not wait(2) in order not to interfere * with popen(2)/pclose(2) and system(2) used in pacemaker.c */ - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { if (tk_test.path && tk_test.pid > 0 && (tk_test.progstate == EXTPROG_RUNNING || tk_test.progstate == EXTPROG_IGNORE) && diff --git a/src/main.c b/src/main.c index 71932fac..2c96c685 100644 --- a/src/main.c +++ b/src/main.c @@ -233,7 +233,7 @@ static int format_peers(char **pdata, unsigned int *len) return -ENOMEM; cp = data; - foreach_node(i, s) { + FOREACH_NODE(i, s) { if (s == local) continue; strftime(time_str, sizeof(time_str), "%F %T", diff --git a/src/raft.c b/src/raft.c index 462fc3b0..dbe773c7 100644 --- a/src/raft.c +++ b/src/raft.c @@ -40,8 +40,9 @@ inline static void clear_election(struct ticket_config *tk) tk_log_debug("clear election"); tk->votes_received = 0; - foreach_node(i, site) + FOREACH_NODE(i, site) { tk->votes_for[site->index] = NULL; + } } @@ -161,7 +162,7 @@ static int is_tie(struct ticket_config *tk) int count[MAX_NODES] = { 0, }; int max_votes = 0, max_cnt = 0; - for(i=0; isite_count; i++) { + for (i = 0; i < booth_conf->site_count; i++) { v = tk->votes_for[i]; if (!v) continue; @@ -169,7 +170,7 @@ static int is_tie(struct ticket_config *tk) max_votes = max(max_votes, count[v->index]); } - for(i=0; isite_count; i++) { + for (i = 0; i < booth_conf->site_count; i++) { if (count[i] == max_votes) max_cnt++; } @@ -183,8 +184,7 @@ static struct booth_site *majority_votes(struct ticket_config *tk) struct booth_site *v; int count[MAX_NODES] = { 0, }; - - for(i=0; isite_count; i++) { + for (i = 0; i < booth_conf->site_count; i++) { v = tk->votes_for[i]; if (!v || v == no_leader) continue; diff --git a/src/ticket.c b/src/ticket.c index 622a9d15..6673a2ec 100644 --- a/src/ticket.c +++ b/src/ticket.c @@ -59,16 +59,16 @@ int check_max_len_valid(const char *s, int max) int find_ticket_by_name(const char *ticket, struct ticket_config **found) { + struct ticket_config *tk; int i; if (found) *found = NULL; - for (i = 0; i < booth_conf->ticket_count; i++) { - if (!strncmp(booth_conf->ticket[i].name, ticket, - sizeof(booth_conf->ticket[i].name))) { + FOREACH_TICKET(i, tk) { + if (!strncmp(tk->name, ticket, sizeof(tk->name))) { if (found) - *found = booth_conf->ticket + i; + *found = tk; return 1; } } @@ -392,6 +392,7 @@ int do_revoke_ticket(struct ticket_config *tk) int list_ticket(char **pdata, unsigned int *len) { struct ticket_config *tk; + struct booth_site *site; char timeout_str[64]; char pending_str[64]; char *data, *cp; @@ -405,7 +406,7 @@ int list_ticket(char **pdata, unsigned int *len) alloc = booth_conf->ticket_count * (BOOTH_NAME_LEN * 2 + 128 + 16); - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { multiple_grant_warning_length = number_sites_marked_as_granted(tk); if (multiple_grant_warning_length > 1) { @@ -419,7 +420,7 @@ int list_ticket(char **pdata, unsigned int *len) return -ENOMEM; cp = data; - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { if ((!is_manual(tk)) && is_time_set(&tk->term_expires)) { /* Manual tickets doesn't have term_expires defined */ ts = wall_ts(&tk->term_expires); @@ -467,7 +468,7 @@ int list_ticket(char **pdata, unsigned int *len) } } - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { multiple_grant_warning_length = number_sites_marked_as_granted(tk); if (multiple_grant_warning_length > 1) { @@ -476,12 +477,12 @@ int list_ticket(char **pdata, unsigned int *len) "\nWARNING: The ticket %s is granted to multiple sites: ", // ~55 characters tk->name); - for(site_index=0; site_indexsite_count; ++site_index) { + FOREACH_NODE(site_index, site) { if (tk->sites_where_granted[site_index] > 0) { cp += snprintf(cp, alloc - (cp - data), "%s", - site_string(&(booth_conf->site[site_index]))); + site_string(site)); if (--multiple_grant_warning_length > 0) { cp += snprintf(cp, @@ -632,7 +633,7 @@ int setup_ticket(void) struct ticket_config *tk; int i; - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { reset_ticket(tk); if (local->type == SITE) { @@ -859,8 +860,7 @@ static void log_lost_servers(struct ticket_config *tk) */ return; - for (i = 0; i < booth_conf->site_count; i++) { - n = booth_conf->site + i; + FOREACH_NODE(i, n) { if (!(tk->acks_received & n->bitmask)) { tk_log_warn("%s %s didn't acknowledge our %s, " "will retry %d times", @@ -880,8 +880,7 @@ static void resend_msg(struct ticket_config *tk) if (!(tk->acks_received ^ local->bitmask)) { ticket_broadcast(tk, tk->last_request, 0, RLT_SUCCESS, 0); } else { - for (i = 0; i < booth_conf->site_count; i++) { - n = booth_conf->site + i; + FOREACH_NODE(i, n) { if (!(tk->acks_received & n->bitmask)) { n->resend_cnt++; tk_log_debug("resending %s to %s", @@ -1145,7 +1144,7 @@ void process_tickets(void) int i; timetype last_cron; - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { if (!has_extprog_exited(tk) && is_time_set(&tk->next_cron) && !is_past(&tk->next_cron)) continue; @@ -1169,7 +1168,7 @@ void tickets_log_info(void) int i; time_t ts; - foreach_ticket(i, tk) { + FOREACH_TICKET(i, tk) { ts = wall_ts(&tk->term_expires); tk_log_info("state '%s' " "term %d " @@ -1365,8 +1364,9 @@ int is_manual(struct ticket_config *tk) int number_sites_marked_as_granted(struct ticket_config *tk) { int i, result = 0; + struct booth_site *ignored __attribute__((unused)); - for(i=0; isite_count; ++i) { + FOREACH_NODE(i, ignored) { result += tk->sites_where_granted[i]; } diff --git a/src/ticket.h b/src/ticket.h index e36e323c..d95cf6ed 100644 --- a/src/ticket.h +++ b/src/ticket.h @@ -35,8 +35,15 @@ extern int TIME_RES; #define DEFAULT_RETRIES 10 -#define foreach_ticket(i_,t_) for(i_=0; (t_=booth_conf->ticket+i_, i_ticket_count); i_++) -#define foreach_node(i_,n_) for(i_=0; (n_=booth_conf->site+i_, i_site_count); i_++) +#define FOREACH_TICKET(i_, t_) \ + for (i_ = 0; \ + (t_ = booth_conf->ticket + i_, i_ < booth_conf->ticket_count); \ + i_++) + +#define FOREACH_NODE(i_, n_) \ + for (i_ = 0; \ + (n_ = booth_conf->site + i_, i_ < booth_conf->site_count); \ + i_++) #define set_leader(tk, who) do { \ if (who == NULL) { \ diff --git a/src/transport.c b/src/transport.c index 8267c961..cd40e991 100644 --- a/src/transport.c +++ b/src/transport.c @@ -98,8 +98,7 @@ static int find_address(unsigned char ipaddr[BOOTH_IPADDR_LEN], /* One bit left to check means ignore 7 lowest bits. */ mask = ~( (1 << (8 - bits_left)) -1); - for (i = 0; i < booth_conf->site_count; i++) { - node = booth_conf->site + i; + FOREACH_NODE(i, node) { if (family != node->family) continue; n_a = node_to_addr_pointer(node); @@ -896,7 +895,7 @@ static int booth_udp_broadcast_auth(void *buf, int len) return rv; rvs = 0; - foreach_node(i, site) { + FOREACH_NODE(i, site) { if (site != local) { rv = booth_udp_send(site, buf, len); if (!rvs)