Skip to content

Commit 31fc33f

Browse files
committed
Refactor: get rid of any use of booth_conf global var except for main.c
And except for said single use intermingled with another global mentioned in the previous commit: transport() of inline-fn.h. Signed-off-by: Jan Pokorný <[email protected]>
1 parent a7e8db8 commit 31fc33f

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

src/handler.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@
3535
#include "booth.h"
3636
#include "handler.h"
3737

38-
static int set_booth_env(struct ticket_config *tk)
38+
static int set_booth_env(struct booth_config *conf_ptr,
39+
struct ticket_config *tk)
3940
{
4041
int rv;
4142
char expires[16];
4243

44+
assert(conf_ptr != NULL);
45+
4346
sprintf(expires, "%" PRId64, (int64_t)wall_ts(&tk->term_expires));
4447
rv = setenv("BOOTH_TICKET", tk->name, 1) ||
4548
setenv("BOOTH_LOCAL", local->addr_string, 1) ||
46-
setenv("BOOTH_CONF_NAME", booth_conf->name, 1) ||
49+
setenv("BOOTH_CONF_NAME", conf_ptr->name, 1) ||
4750
setenv("BOOTH_CONF_PATH", cl.configfile, 1) ||
4851
setenv("BOOTH_TICKET_EXPIRES", expires, 1);
4952

@@ -65,9 +68,10 @@ closefiles(void)
6568
}
6669

6770
static void
68-
run_ext_prog(struct ticket_config *tk, char *prog)
71+
run_ext_prog(struct booth_config *conf_ptr, struct ticket_config *tk,
72+
char *prog)
6973
{
70-
if (set_booth_env(tk)) {
74+
if (set_booth_env(conf_ptr, tk)) {
7175
_exit(1);
7276
}
7377
closefiles(); /* don't leak open files */
@@ -125,15 +129,15 @@ int tk_test_exit_status(struct ticket_config *tk)
125129
return rv;
126130
}
127131

128-
void wait_child(int sig)
132+
void wait_child(struct booth_config *conf_ptr)
129133
{
130134
int i, status;
131135
struct ticket_config *tk;
132136

133137
/* use waitpid(2) and not wait(2) in order not to interfere
134138
* with popen(2)/pclose(2) and system(2) used in pacemaker.c
135139
*/
136-
FOREACH_TICKET(booth_conf, i, tk) {
140+
FOREACH_TICKET(conf_ptr, i, tk) {
137141
if (tk_test.path && tk_test.pid > 0 &&
138142
(tk_test.progstate == EXTPROG_RUNNING ||
139143
tk_test.progstate == EXTPROG_IGNORE) &&
@@ -187,7 +191,7 @@ void ignore_ext_test(struct ticket_config *tk)
187191
}
188192

189193
static void
190-
process_ext_dir(struct ticket_config *tk)
194+
process_ext_dir(struct booth_config *conf_ptr, struct ticket_config *tk)
191195
{
192196
char prog[FILENAME_MAX+1];
193197
int rv, n_progs, i, status;
@@ -220,7 +224,7 @@ process_ext_dir(struct ticket_config *tk)
220224
log_error("fork: %s", strerror(errno));
221225
_exit(1);
222226
case 0: /* child */
223-
run_ext_prog(tk, prog);
227+
run_ext_prog(conf_ptr, tk, prog);
224228
break; /* run_ext_prog effectively noreturn */
225229
default: /* parent */
226230
while (waitpid(curr_pid, &status, 0) != curr_pid)
@@ -241,7 +245,7 @@ process_ext_dir(struct ticket_config *tk)
241245
* RUNCMD_ERR: executing program failed (or some other failure)
242246
* RUNCMD_MORE: program forked, results later
243247
*/
244-
int run_handler(struct ticket_config *tk)
248+
int run_handler(struct booth_config *conf_ptr, struct ticket_config *tk)
245249
{
246250
int rv = 0;
247251
pid_t pid;
@@ -262,9 +266,9 @@ int run_handler(struct ticket_config *tk)
262266
return RUNCMD_ERR;
263267
case 0: /* child */
264268
if (tk_test.is_dir) {
265-
process_ext_dir(tk);
269+
process_ext_dir(conf_ptr, tk);
266270
} else {
267-
run_ext_prog(tk, tk_test.path);
271+
run_ext_prog(conf_ptr, tk, tk_test.path);
268272
}
269273
default: /* parent */
270274
tk_test.pid = pid;

src/handler.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,30 @@ enum {
2424
RUNCMD_MORE = -2,
2525
};
2626

27-
int run_handler(struct ticket_config *tk);
27+
/**
28+
* @internal
29+
* First stage of incoming datagram handling (authentication)
30+
*
31+
* @param[inout] conf_ptr config object to refer to
32+
* @param[in] tk ticket at hand
33+
*
34+
* @return 0, #RUNCMD_ERR, #RUNCMD_MORE
35+
*/
36+
int run_handler(struct booth_config *conf_ptr, struct ticket_config *tk);
37+
2838
int tk_test_exit_status(struct ticket_config *tk);
2939
void ignore_ext_test(struct ticket_config *tk);
3040
int is_ext_prog_running(struct ticket_config *tk);
3141
void ext_prog_timeout(struct ticket_config *tk);
32-
void wait_child(int sig);
42+
43+
/**
44+
* @internal
45+
* SIGCHLD handling so as to mark the handler-at-a-ticket finalization
46+
*
47+
* @param[inout] conf_ptr config object to refer to
48+
* @param[in] tk ticket at hand
49+
*/
50+
void wait_child(struct booth_config *conf_ptr);
3351

3452
#define set_progstate(tk, newst) do { \
3553
if (!(newst)) tk_log_debug("progstate reset"); \

src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,11 @@ static void sig_exit_handler(int sig)
14201420
exit(0);
14211421
}
14221422

1423+
static void wait_child_adaptor(int sig)
1424+
{
1425+
wait_child(booth_conf);
1426+
}
1427+
14231428
static int do_server(struct booth_config **conf_pptr, int type)
14241429
{
14251430
int rv = -1;
@@ -1490,7 +1495,7 @@ static int do_server(struct booth_config **conf_pptr, int type)
14901495
}
14911496
#endif
14921497

1493-
signal(SIGCHLD, (__sighandler_t)wait_child);
1498+
signal(SIGCHLD, (__sighandler_t) wait_child_adaptor);
14941499
rv = loop(lock_fd);
14951500

14961501
return rv;

src/ticket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static int do_ext_prog(struct booth_config *conf_ptr,
274274

275275
switch(tk_test.progstate) {
276276
case EXTPROG_IDLE:
277-
rv = run_handler(tk);
277+
rv = run_handler(conf_ptr, tk);
278278
if (rv == RUNCMD_ERR) {
279279
tk_log_warn("couldn't run external test, not allowed to acquire ticket");
280280
ext_prog_failed(conf_ptr, tk, start_election);

0 commit comments

Comments
 (0)