Skip to content

Commit 2246488

Browse files
committed
Refactor: get rid of "struct command_line" as a global variable
At that occasion, make "struct booth_config" contain "path_to_self" as it is handy to have it there and not to rely on presence of the former struct. Signed-off-by: Jan Pokorný <[email protected]>
1 parent 389c5bb commit 2246488

File tree

7 files changed

+205
-172
lines changed

7 files changed

+205
-172
lines changed

src/attr.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ void print_geostore_usage(void)
6262
* ticket, attr name, attr value
6363
*/
6464

65-
int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)
65+
int test_attr_reply(struct command_line *cl, cmd_result_t reply_code)
6666
{
6767
int rv = 0;
6868
const char *op_str = "";
6969

70-
switch (cmd) {
70+
switch (cl->type) {
7171
case ATTR_SET: op_str = "set"; break;
7272
case ATTR_GET: op_str = "get"; break;
7373
case ATTR_LIST: op_str = "list"; break;
@@ -86,7 +86,7 @@ int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)
8686

8787
case RLT_SYNC_SUCC:
8888
case RLT_SUCCESS:
89-
if (cmd == ATTR_SET)
89+
if (cl->type == ATTR_SET)
9090
log_info("%s succeeded!", op_str);
9191
rv = 0;
9292
break;
@@ -98,13 +98,13 @@ int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd)
9898

9999
case RLT_INVALID_ARG:
100100
log_error("ticket \"%s\" does not exist",
101-
cl.attr_msg.attr.tkt_id);
101+
cl->attr_msg.attr.tkt_id);
102102
rv = 1;
103103
break;
104104

105105
case RLT_NO_SUCH_ATTR:
106106
log_error("attribute \"%s\" not set",
107-
cl.attr_msg.attr.name);
107+
cl->attr_msg.attr.name);
108108
rv = 1;
109109
break;
110110

@@ -149,7 +149,7 @@ static int read_server_reply(
149149
return rv;
150150
}
151151

152-
int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
152+
int do_attr_command(struct command_line *cl, struct booth_config *conf_ptr)
153153
{
154154
struct booth_site *site = NULL;
155155
struct boothc_header *header;
@@ -158,12 +158,13 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
158158
char *msg = NULL;
159159

160160
assert(conf_ptr != NULL && conf_ptr->transport != NULL);
161+
assert(cl != NULL);
161162

162-
if (!*cl.site)
163+
if (*cl->site == '\0')
163164
site = local;
164165
else {
165-
if (!find_site_by_name(conf_ptr, cl.site, &site, 1)) {
166-
log_error("Site \"%s\" not configured.", cl.site);
166+
if (!find_site_by_name(conf_ptr, cl->site, &site, 1)) {
167+
log_error("Site \"%s\" not configured.", cl->site);
167168
goto out_close;
168169
}
169170
}
@@ -172,21 +173,22 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
172173
if (site == local) {
173174
log_error("We're just an arbitrator, no attributes here.");
174175
} else {
175-
log_error("%s is just an arbitrator, no attributes there.", cl.site);
176+
log_error("%s is just an arbitrator, no attributes there.",
177+
cl->site);
176178
}
177179
goto out_close;
178180
}
179181

180182
tpt = *conf_ptr->transport + TCP;
181183

182-
init_header(conf_ptr, &cl.attr_msg.header, cmd, 0, cl.options, 0, 0,
183-
sizeof(cl.attr_msg));
184+
init_header(conf_ptr, &cl->attr_msg.header, cl->type, 0, cl->options,
185+
0, 0, sizeof(cl->attr_msg));
184186

185187
rv = tpt->open(site);
186188
if (rv < 0)
187189
goto out_close;
188190

189-
rv = tpt->send(conf_ptr, site, &cl.attr_msg, sendmsglen(&cl.attr_msg));
191+
rv = tpt->send(conf_ptr, site, &cl->attr_msg, sendmsglen(&cl->attr_msg));
190192
if (rv < 0)
191193
goto out_close;
192194

@@ -201,7 +203,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
201203
header = (struct boothc_header *)msg;
202204
if (rv < 0) {
203205
if (rv == -1)
204-
(void)test_attr_reply(ntohl(header->result), cmd);
206+
(void) test_attr_reply(cl, ntohl(header->result));
205207
goto out_close;
206208
}
207209
len = ntohl(header->length);
@@ -217,7 +219,7 @@ int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd)
217219
rv = -1;
218220
goto out_close;
219221
}
220-
rv = test_attr_reply(ntohl(header->result), cmd);
222+
rv = test_attr_reply(cl, ntohl(header->result));
221223

222224
out_close:
223225
if (tpt && site)

src/attr.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,28 @@
3030
#include <glib.h>
3131

3232
void print_geostore_usage(void);
33-
int test_attr_reply(cmd_result_t reply_code, cmd_request_t cmd);
33+
34+
/**
35+
* @internal
36+
* Late handling of the response towards the client
37+
*
38+
* @param[in] cl parsed command line form
39+
* @param[in] reply_code what the inner handling returns
40+
*
41+
* @return 0 on success, -1 on failure, 1 when "cannot serve"
42+
*/
43+
int test_attr_reply(struct command_line *cl, cmd_result_t reply_code);
3444

3545
/**
3646
* @internal
3747
* Carry out a geo-atribute related command
3848
*
49+
* @param[in] cl parsed command line structure
3950
* @param[inout] conf_ptr config object to refer to
40-
* @param[in] cmd what to perform
4151
*
4252
* @return 0 or negative value (-1 or -errno) on error
4353
*/
44-
int do_attr_command(struct booth_config *conf_ptr, cmd_request_t cmd);
54+
int do_attr_command(struct command_line *cl, struct booth_config *conf_ptr);
4555

4656
/**
4757
* @internal

src/booth.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,20 @@ int client_add(int fd, const struct booth_transport *tpt,
361361
void (*deadfn)(int ci));
362362

363363
int find_client_by_fd(int fd);
364-
void safe_copy(char *dest, char *value, size_t buflen, const char *description);
364+
365+
/**
366+
* @internal
367+
* Like strncpy, but with explicit protection and better diagnostics
368+
*
369+
* @param[out] dest where to copy the string to
370+
* @param[in] value where to copy the string from
371+
* @param[in] buflen nmaximum size of #dest (incl. trailing '\0', or sizeof)
372+
* @param[in] description how to refer to the target as
373+
*
374+
* @return number of clients tracked (incl. this one)
375+
*/
376+
void safe_copy(char *dest, const char *value, size_t buflen,
377+
const char *description);
365378

366379
/**
367380
* @internal
@@ -395,8 +408,6 @@ struct command_line {
395408
struct boothc_ticket_msg msg;
396409
struct boothc_attr_msg attr_msg;
397410
};
398-
extern struct command_line cl;
399-
400411

401412

402413
/* http://gcc.gnu.org/onlinedocs/gcc/Typeof.html */

src/config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,10 @@ int read_config(struct booth_config **conf_pptr,
875875
goto out;
876876
}
877877

878+
safe_copy((*conf_pptr)->path_to_self, path,
879+
sizeof((*conf_pptr)->path_to_self),
880+
"path to config file itself");
881+
878882
poll_timeout = min(POLL_TIMEOUT, min_timeout/10);
879883
if (!poll_timeout)
880884
poll_timeout = POLL_TIMEOUT;

src/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ struct booth_config {
325325
int ticket_allocated;
326326
struct ticket_config *ticket;
327327

328+
char path_to_self[BOOTH_PATH_LEN];
329+
328330
const booth_transport_table_t *transport;
329331
};
330332

src/handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int set_booth_env(struct booth_config *conf_ptr,
4747
rv = setenv("BOOTH_TICKET", tk->name, 1) ||
4848
setenv("BOOTH_LOCAL", local->addr_string, 1) ||
4949
setenv("BOOTH_CONF_NAME", conf_ptr->name, 1) ||
50-
setenv("BOOTH_CONF_PATH", cl.configfile, 1) ||
50+
setenv("BOOTH_CONF_PATH", conf_ptr->path_to_self, 1) ||
5151
setenv("BOOTH_TICKET_EXPIRES", expires, 1);
5252

5353
if (rv) {

0 commit comments

Comments
 (0)