Skip to content

Commit 333c237

Browse files
jnpkrnclumens
authored andcommitted
Refactor: read_config to no longer rely on global booth_conf variable
Also, apply the same recursively to both the underlying and superjacent call graph expansions (respective static functions). Signed-off-by: Jan Pokorný <[email protected]>
1 parent 8fed0f6 commit 333c237

File tree

3 files changed

+128
-99
lines changed

3 files changed

+128
-99
lines changed

src/config.c

Lines changed: 83 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,27 @@
3838

3939
static int ticket_size = 0;
4040

41-
static int ticket_realloc(void)
41+
static int ticket_realloc(struct booth_config *conf)
4242
{
4343
const int added = 5;
4444
int had, want;
4545
void *p;
4646

47-
had = booth_conf->ticket_allocated;
47+
assert(conf != NULL);
48+
49+
had = conf->ticket_allocated;
4850
want = had + added;
4951

50-
p = realloc(booth_conf->ticket,
51-
sizeof(struct ticket_config) * want);
52+
p = realloc(conf->ticket, sizeof(struct ticket_config) * want);
5253
if (!p) {
5354
log_error("can't alloc more tickets");
5455
return -ENOMEM;
5556
}
5657

57-
booth_conf->ticket = p;
58-
memset(booth_conf->ticket + had, 0,
59-
sizeof(struct ticket_config) * added);
60-
booth_conf->ticket_allocated = want;
58+
conf->ticket = p;
59+
memset(conf->ticket + had, 0,
60+
sizeof(struct ticket_config) * added);
61+
conf->ticket_allocated = want;
6162

6263
return 0;
6364
}
@@ -115,26 +116,28 @@ static void hostname_to_ip(char * hostname)
115116
freeaddrinfo(result);
116117
}
117118

118-
static int add_site(char *addr_string, int type)
119+
static int add_site(struct booth_config *conf, char *addr_string, int type)
119120
{
120121
int rv;
121122
struct booth_site *site;
122123
uLong nid;
123124
uint32_t mask;
124125
int i;
125126

127+
assert(conf != NULL);
128+
126129
rv = 1;
127-
if (booth_conf->site_count == MAX_NODES) {
130+
if (conf->site_count == MAX_NODES) {
128131
log_error("too many nodes");
129132
goto out;
130133
}
131-
if (strnlen(addr_string, sizeof(booth_conf->site[0].addr_string))
132-
>= sizeof(booth_conf->site[0].addr_string)) {
134+
if (strnlen(addr_string, sizeof(conf->site[0].addr_string))
135+
>= sizeof(conf->site[0].addr_string)) {
133136
log_error("site address \"%s\" too long", addr_string);
134137
goto out;
135138
}
136139

137-
site = booth_conf->site + booth_conf->site_count;
140+
site = conf->site + conf->site_count;
138141

139142
site->family = AF_INET;
140143
site->type = type;
@@ -153,17 +156,18 @@ static int add_site(char *addr_string, int type)
153156
hostname_to_ip(site->addr_string);
154157
}
155158

156-
site->index = booth_conf->site_count;
157-
site->bitmask = 1 << booth_conf->site_count;
159+
site->index = conf->site_count;
160+
site->bitmask = 1 << conf->site_count;
158161
/* Catch site overflow */
159162
assert(site->bitmask);
160-
booth_conf->all_bits |= site->bitmask;
161-
if (type == SITE)
162-
booth_conf->sites_bits |= site->bitmask;
163+
conf->all_bits |= site->bitmask;
164+
if (type == SITE) {
165+
conf->sites_bits |= site->bitmask;
166+
}
163167

164168
site->tcp_fd = -1;
165169

166-
booth_conf->site_count++;
170+
conf->site_count++;
167171

168172
rv = 0;
169173
memset(&site->sa6, 0, sizeof(site->sa6));
@@ -185,7 +189,7 @@ static int add_site(char *addr_string, int type)
185189

186190
site->family = AF_INET;
187191
site->sa4.sin_family = site->family;
188-
site->sa4.sin_port = htons(booth_conf->port);
192+
site->sa4.sin_port = htons(conf->port);
189193
site->saddrlen = sizeof(site->sa4);
190194
site->addrlen = sizeof(site->sa4.sin_addr);
191195
site->site_id = crc32(nid, (void*)&site->sa4.sin_addr, site->addrlen);
@@ -197,7 +201,7 @@ static int add_site(char *addr_string, int type)
197201
site->family = AF_INET6;
198202
site->sa6.sin6_family = site->family;
199203
site->sa6.sin6_flowinfo = 0;
200-
site->sa6.sin6_port = htons(booth_conf->port);
204+
site->sa6.sin6_port = htons(conf->port);
201205
site->saddrlen = sizeof(site->sa6);
202206
site->addrlen = sizeof(site->sa6.sin6_addr);
203207
site->site_id = crc32(nid, (void*)&site->sa6.sin6_addr, site->addrlen);
@@ -215,11 +219,12 @@ static int add_site(char *addr_string, int type)
215219

216220

217221
/* Test for collisions with other sites */
218-
for(i=0; i<site->index; i++)
219-
if (booth_conf->site[i].site_id == site->site_id) {
222+
for (i = 0; i < site->index; i++) {
223+
if (conf->site[i].site_id == site->site_id) {
220224
log_error("Got a site-ID collision. Please file a bug on https://github.com/ClusterLabs/booth/issues/new, attaching the configuration file.");
221225
exit(1);
222226
}
227+
}
223228

224229
out:
225230
return rv;
@@ -260,22 +265,24 @@ static inline int is_end_of_line(char *cp)
260265
}
261266

262267

263-
static int add_ticket(const char *name, struct ticket_config **tkp,
264-
const struct ticket_config *def)
268+
static int add_ticket(struct booth_config *conf, const char *name,
269+
struct ticket_config **tkp, const struct ticket_config *def)
265270
{
266271
int rv;
267272
struct ticket_config *tk;
268273

274+
assert(conf != NULL);
269275

270-
if (booth_conf->ticket_count == booth_conf->ticket_allocated) {
271-
rv = ticket_realloc();
272-
if (rv < 0)
276+
if (conf->ticket_count == conf->ticket_allocated) {
277+
rv = ticket_realloc(conf);
278+
if (rv < 0) {
273279
return rv;
280+
}
274281
}
275282

276283

277-
tk = booth_conf->ticket + booth_conf->ticket_count;
278-
booth_conf->ticket_count++;
284+
tk = conf->ticket + conf->ticket_count;
285+
conf->ticket_count++;
279286

280287
if (!check_max_len_valid(name, sizeof(tk->name))) {
281288
log_error("ticket name \"%s\" too long.", name);
@@ -538,7 +545,7 @@ static int parse_attr_prereq(char *val, struct ticket_config *tk)
538545

539546
extern int poll_timeout;
540547

541-
int read_config(const char *path, int type)
548+
int read_config(struct booth_config **conf, const char *path, int type)
542549
{
543550
char line[1024];
544551
char error_str_buf[1024];
@@ -553,38 +560,41 @@ int read_config(const char *path, int type)
553560
struct ticket_config defaults = { { 0 } };
554561
struct ticket_config *current_tk = NULL;
555562

563+
assert(conf != NULL);
564+
free(*conf);
556565

557566
fp = fopen(path, "r");
558567
if (!fp) {
559568
log_error("failed to open %s: %s", path, strerror(errno));
569+
*conf = NULL;
560570
return -1;
561571
}
562572

563-
booth_conf = malloc(sizeof(struct booth_config)
573+
*conf = malloc(sizeof(struct booth_config)
564574
+ TICKET_ALLOC * sizeof(struct ticket_config));
565-
if (!booth_conf) {
575+
if (*conf == NULL) {
566576
fclose(fp);
567577
log_error("failed to alloc memory for booth config");
568578
return -ENOMEM;
569579
}
570-
memset(booth_conf, 0, sizeof(struct booth_config)
580+
memset(*conf, 0, sizeof(struct booth_config)
571581
+ TICKET_ALLOC * sizeof(struct ticket_config));
572582
ticket_size = TICKET_ALLOC;
573583

574584

575-
booth_conf->proto = UDP;
576-
booth_conf->port = BOOTH_DEFAULT_PORT;
577-
booth_conf->maxtimeskew = BOOTH_DEFAULT_MAX_TIME_SKEW;
578-
booth_conf->authkey[0] = '\0';
585+
(*conf)->proto = UDP;
586+
(*conf)->port = BOOTH_DEFAULT_PORT;
587+
(*conf)->maxtimeskew = BOOTH_DEFAULT_MAX_TIME_SKEW;
588+
(*conf)->authkey[0] = '\0';
579589

580590

581591
/* Provide safe defaults. -1 is reserved, though. */
582-
booth_conf->uid = -2;
583-
booth_conf->gid = -2;
584-
strcpy(booth_conf->site_user, "hacluster");
585-
strcpy(booth_conf->site_group, "haclient");
586-
strcpy(booth_conf->arb_user, "nobody");
587-
strcpy(booth_conf->arb_group, "nobody");
592+
(*conf)->uid = -2;
593+
(*conf)->gid = -2;
594+
strcpy((*conf)->site_user, "hacluster");
595+
strcpy((*conf)->site_group, "haclient");
596+
strcpy((*conf)->arb_user, "nobody");
597+
strcpy((*conf)->arb_group, "nobody");
588598

589599
parse_weights("", defaults.weight);
590600
defaults.clu_test.path = NULL;
@@ -694,11 +704,11 @@ int read_config(const char *path, int type)
694704
goto err;
695705
}
696706

697-
if (strcasecmp(val, "UDP") == 0)
698-
booth_conf->proto = UDP;
699-
else if (strcasecmp(val, "SCTP") == 0)
700-
booth_conf->proto = SCTP;
701-
else {
707+
if (strcasecmp(val, "UDP") == 0) {
708+
(*conf)->proto = UDP;
709+
} else if (strcasecmp(val, "SCTP") == 0) {
710+
(*conf)->proto = SCTP;
711+
} else {
702712
(void)snprintf(error_str_buf, sizeof(error_str_buf),
703713
"invalid transport protocol \"%s\"", val);
704714
error = error_str_buf;
@@ -709,61 +719,63 @@ int read_config(const char *path, int type)
709719
}
710720

711721
if (strcmp(key, "port") == 0) {
712-
booth_conf->port = atoi(val);
722+
(*conf)->port = atoi(val);
713723
continue;
714724
}
715725

716726
if (strcmp(key, "name") == 0) {
717-
safe_copy(booth_conf->name,
727+
safe_copy((*conf)->name,
718728
val, BOOTH_NAME_LEN,
719729
"name");
720730
continue;
721731
}
722732

723733
#if HAVE_LIBGNUTLS || HAVE_LIBGCRYPT || HAVE_LIBMHASH
724734
if (strcmp(key, "authfile") == 0) {
725-
safe_copy(booth_conf->authfile,
735+
safe_copy((*conf)->authfile,
726736
val, BOOTH_PATH_LEN,
727737
"authfile");
728738
continue;
729739
}
730740

731741
if (strcmp(key, "maxtimeskew") == 0) {
732-
booth_conf->maxtimeskew = atoi(val);
742+
(*conf)->maxtimeskew = atoi(val);
733743
continue;
734744
}
735745
#endif
736746

737747
if (strcmp(key, "site") == 0) {
738-
if (add_site(val, SITE))
748+
if (add_site(*conf, val, SITE)) {
739749
goto err;
750+
}
740751
continue;
741752
}
742753

743754
if (strcmp(key, "arbitrator") == 0) {
744-
if (add_site(val, ARBITRATOR))
755+
if (add_site(*conf, val, ARBITRATOR)) {
745756
goto err;
757+
}
746758
continue;
747759
}
748760

749761
if (strcmp(key, "site-user") == 0) {
750-
safe_copy(booth_conf->site_user, optarg, BOOTH_NAME_LEN,
751-
"site-user");
762+
safe_copy((*conf)->site_user, optarg, BOOTH_NAME_LEN,
763+
"site-user");
752764
continue;
753765
}
754766
if (strcmp(key, "site-group") == 0) {
755-
safe_copy(booth_conf->site_group, optarg, BOOTH_NAME_LEN,
756-
"site-group");
767+
safe_copy((*conf)->site_group, optarg, BOOTH_NAME_LEN,
768+
"site-group");
757769
continue;
758770
}
759771
if (strcmp(key, "arbitrator-user") == 0) {
760-
safe_copy(booth_conf->arb_user, optarg, BOOTH_NAME_LEN,
761-
"arbitrator-user");
772+
safe_copy((*conf)->arb_user, optarg, BOOTH_NAME_LEN,
773+
"arbitrator-user");
762774
continue;
763775
}
764776
if (strcmp(key, "arbitrator-group") == 0) {
765-
safe_copy(booth_conf->arb_group, optarg, BOOTH_NAME_LEN,
766-
"arbitrator-group");
777+
safe_copy((*conf)->arb_group, optarg, BOOTH_NAME_LEN,
778+
"arbitrator-group");
767779
continue;
768780
}
769781

@@ -781,7 +793,8 @@ int read_config(const char *path, int type)
781793
}
782794
if (!strcmp(val, "__defaults__")) {
783795
current_tk = &defaults;
784-
} else if (add_ticket(val, &current_tk, &defaults)) {
796+
} else if (add_ticket(*conf, val, &current_tk,
797+
&defaults)) {
785798
goto err;
786799
}
787800
continue;
@@ -880,12 +893,12 @@ int read_config(const char *path, int type)
880893
}
881894
fclose(fp);
882895

883-
if ((booth_conf->site_count % 2) == 0) {
896+
if (((*conf)->site_count % 2) == 0) {
884897
log_warn("Odd number of nodes is strongly recommended!");
885898
}
886899

887900
/* Default: make config name match config filename. */
888-
if (!booth_conf->name[0]) {
901+
if (!(*conf)->name[0]) {
889902
cp = strrchr(path, '/');
890903
cp = cp ? cp+1 : (char *)path;
891904
cp2 = strrchr(cp, '.');
@@ -895,8 +908,8 @@ int read_config(const char *path, int type)
895908
log_error("booth config file name too long");
896909
goto out;
897910
}
898-
strncpy(booth_conf->name, cp, cp2-cp);
899-
*(booth_conf->name+(cp2-cp)) = '\0';
911+
strncpy((*conf)->name, cp, cp2-cp);
912+
*((*conf)->name+(cp2-cp)) = '\0';
900913
}
901914

902915
if (!postproc_ticket(current_tk)) {
@@ -916,8 +929,8 @@ int read_config(const char *path, int type)
916929
log_error("%s in config file line %d",
917930
error, lineno);
918931

919-
free(booth_conf);
920-
booth_conf = NULL;
932+
free(*conf);
933+
*conf = NULL;
921934
return -1;
922935
}
923936

src/config.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,17 @@ extern struct booth_config *booth_conf;
327327

328328
#define is_auth_req() (booth_conf->authkey[0] != '\0')
329329

330-
331-
int read_config(const char *path, int type);
330+
/**
331+
* @internal
332+
* Parse booth configuration file and store as structured data
333+
*
334+
* @param[in,out] conf config object to free-alloc cycle & fill accordingly
335+
* @param[in] path where the configuration file is expected
336+
* @param[in] type role currently being acted as
337+
*
338+
* @return 0 or negative value (-1 or -errno) on error
339+
*/
340+
int read_config(struct booth_config **conf, const char *path, int type);
332341

333342
int check_config(int type);
334343

0 commit comments

Comments
 (0)