Skip to content

Commit e9a7547

Browse files
committed
Merge pull request #25 from maralla/bind-config
fix bind config
2 parents 116ddc4 + 3a4f6fa commit e9a7547

File tree

9 files changed

+95
-46
lines changed

9 files changed

+95
-46
lines changed

src/corvus.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
static struct context *contexts;
2323

24-
static void config_init()
24+
void config_init()
2525
{
2626
memset(config.cluster, 0, CLUSTER_NAME_SIZE + 1);
2727
strncpy(config.cluster, "default", CLUSTER_NAME_SIZE);
@@ -40,17 +40,18 @@ static void config_init()
4040
config.metric_interval = 10;
4141
}
4242

43-
static int config_add(char *name, char *value)
43+
int config_add(char *name, char *value)
4444
{
4545
int val;
4646
if (strcmp(name, "cluster") == 0) {
4747
if (strlen(value) <= 0) return 0;
4848
strncpy(config.cluster, value, CLUSTER_NAME_SIZE);
4949
} else if (strcmp(name, "bind") == 0) {
50-
config.bind = atoi(value);
51-
if (config.bind > 0xFFFF) return -1;
50+
if (socket_parse_port(value, &config.bind) == CORVUS_ERR) {
51+
return CORVUS_ERR;
52+
}
5253
} else if (strcmp(name, "syslog") == 0) {
53-
config.syslog = atoi(value);
54+
config.syslog = atoi(value) ? 1 : 0;
5455
} else if (strcmp(name, "thread") == 0) {
5556
config.thread = atoi(value);
5657
if (config.thread <= 0) config.thread = 4;
@@ -106,7 +107,7 @@ static int config_add(char *name, char *value)
106107
return 0;
107108
}
108109

109-
static int read_conf(const char *filename)
110+
int read_conf(const char *filename)
110111
{
111112
FILE *fp = fopen(filename, "r");
112113
if (fp == NULL) {
@@ -137,7 +138,7 @@ static int read_conf(const char *filename)
137138
return 0;
138139
}
139140

140-
static void quit()
141+
void quit()
141142
{
142143
if (config.stats) stats_kill();
143144

@@ -155,7 +156,7 @@ static void quit()
155156
}
156157
}
157158

158-
static void log_traceback()
159+
void log_traceback()
159160
{
160161
void *stack[64];
161162
char **symbols;
@@ -185,7 +186,7 @@ static void log_traceback()
185186
exit(EXIT_FAILURE);
186187
}
187188

188-
static void sig_handler(int sig)
189+
void sig_handler(int sig)
189190
{
190191
switch (sig) {
191192
case SIGINT:
@@ -198,7 +199,7 @@ static void sig_handler(int sig)
198199
}
199200
}
200201

201-
static void setup_signal()
202+
void setup_signal()
202203
{
203204
struct sigaction act;
204205
sigemptyset(&act.sa_mask);
@@ -224,18 +225,15 @@ struct context *get_contexts()
224225
return contexts;
225226
}
226227

227-
void context_init(struct context *ctx, bool syslog, int log_level)
228+
void context_init(struct context *ctx)
228229
{
229230
memset(ctx, 0, sizeof(struct context));
230231

231-
ctx->syslog = syslog;
232-
ctx->log_level = log_level;
233232
dict_init(&ctx->server_table);
234233
ctx->started = false;
235234
ctx->role = THREAD_UNKNOWN;
236235
ctx->state = CTX_UNKNOWN;
237236
mbuf_init(ctx);
238-
log_init(ctx);
239237

240238
STAILQ_INIT(&ctx->free_cmdq);
241239
TAILQ_INIT(&ctx->servers);
@@ -375,7 +373,7 @@ int main(int argc, const char *argv[])
375373

376374
contexts = malloc(sizeof(struct context) * (config.thread + 1));
377375
for (i = 0; i <= config.thread; i++) {
378-
context_init(&contexts[i], config.syslog, config.loglevel);
376+
context_init(&contexts[i]);
379377
contexts[i].role = THREAD_UNKNOWN;
380378
}
381379

src/corvus.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ struct context {
5757
struct connection proxy;
5858
struct connection timer;
5959

60-
/* logging */
61-
bool syslog;
62-
int log_level;
63-
6460
/* connection pool */
6561
struct dict server_table;
6662
struct conn_tqh conns;
@@ -88,7 +84,7 @@ struct {
8884
struct node_conf node;
8985
int thread;
9086
int loglevel;
91-
int syslog;
87+
bool syslog;
9288
char statsd_addr[DSN_MAX + 1];
9389
int metric_interval;
9490
int stats;

src/logging.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010

1111
static const char *LEVEL_MAP[] = {"DEBUG", "INFO", "WARN", "ERROR"};
1212
static const int SYSLOG_LEVEL_MAP[] = {LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR};
13-
static bool enable_syslog = false;
14-
static int log_level = INFO;
15-
16-
void log_init(struct context *ctx)
17-
{
18-
if (ctx->syslog) {
19-
enable_syslog = true;
20-
} else {
21-
enable_syslog = false;
22-
}
23-
24-
if (ctx->log_level != -1) {
25-
log_level = ctx->log_level;
26-
}
27-
}
2813

2914
void logger(const char *file, int line, int level, const char *fmt, ...)
3015
{
@@ -33,15 +18,15 @@ void logger(const char *file, int line, int level, const char *fmt, ...)
3318
char timestamp[64];
3419
struct timeval now;
3520

36-
if (level < log_level) return;
21+
if (level < config.loglevel) return;
3722

3823
pid_t thread_id = (pid_t)syscall(SYS_gettid);
3924

4025
va_start(ap, fmt);
4126
vsnprintf(msg, sizeof(msg), fmt, ap);
4227
va_end(ap);
4328

44-
if (enable_syslog) {
29+
if (config.syslog) {
4530
syslog(SYSLOG_LEVEL_MAP[level], "[%d] %s", (int)thread_id, msg);
4631
} else {
4732
gettimeofday(&now, NULL);

src/socket.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,26 @@ void socket_address_init(struct address *addr, char *host, int len, int port)
318318
addr->port = port;
319319
}
320320

321+
int socket_parse_port(char *ptr, uint16_t *res)
322+
{
323+
char *end;
324+
int port = strtol(ptr, &end, 0);
325+
if (*end != '\0' || port > 0xFFFF || port <= 0) return CORVUS_ERR;
326+
*res = port;
327+
return CORVUS_OK;
328+
}
329+
321330
int socket_parse_addr(char *addr, struct address *address)
322331
{
323-
unsigned long port;
324-
char *colon, *end;
332+
uint16_t port;
333+
char *colon;
325334

326335
colon = strchr(addr, ':');
327336
if (colon == NULL) return CORVUS_ERR;
328337

329-
port = strtoul(colon + 1, &end, 0);
330-
if (*end != '\0' || end == colon + 1) return CORVUS_ERR;
331-
if (port > 0xFFFF) return CORVUS_ERR;
338+
if (socket_parse_port(colon + 1, &port) == CORVUS_ERR) {
339+
return CORVUS_ERR;
340+
}
332341

333342
socket_address_init(address, addr, colon - addr, port);
334343
return port;

src/socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void socket_address_init(struct address *addr, char *host, int len, int port);
3030
int socket_set_nonblocking(int fd);
3131
int socket_set_tcpnodelay(int fd);
3232
int socket_set_timeout(int fd, int timeout);
33+
int socket_parse_port(char *ptr, uint16_t *res);
3334
int socket_parse_addr(char *addr, struct address *address);
3435
void socket_get_key(struct address *addr, char *dst);
3536

tests/corvus_test.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern TEST_CASE(test_dict);
5959
extern TEST_CASE(test_socket);
6060
extern TEST_CASE(test_client);
6161
extern TEST_CASE(test_timer);
62+
extern TEST_CASE(test_config);
6263

6364
int main(int argc, const char *argv[])
6465
{
@@ -67,9 +68,13 @@ int main(int argc, const char *argv[])
6768
return EXIT_FAILURE;
6869
}
6970

71+
config.syslog = 0;
72+
config.loglevel = ERROR;
73+
config.bufsize = 16384;
74+
7075
struct node_conf conf = {NULL, 0};
7176
struct context ctx;
72-
context_init(&ctx, 0, ERROR);
77+
context_init(&ctx);
7378
memcpy(&config.node, &conf, sizeof(config.node));
7479
slot_init_updater(&ctx);
7580

@@ -82,6 +87,7 @@ int main(int argc, const char *argv[])
8287
RUN_CASE(test_socket);
8388
RUN_CASE(test_client);
8489
RUN_CASE(test_timer);
90+
RUN_CASE(test_config);
8591

8692
usleep(10000);
8793
slot_create_job(SLOT_UPDATER_QUIT);

tests/test.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
do { \
3535
if (filter(#test_func, manager.test_func_filter)) { \
3636
struct context ctx; \
37-
context_init(&ctx, 0, ERROR); \
38-
config.bufsize = 16384; \
37+
context_init(&ctx); \
3938
event_init(&ctx.loop, 1024); \
4039
enum test_result res = test_func(&ctx); \
4140
context_free(&ctx); \
@@ -196,7 +195,7 @@ static void post_case()
196195

197196

198197
struct context;
199-
extern void context_init(struct context *ctx, bool syslog, int log_level);
198+
extern void context_init(struct context *ctx);
200199
extern void context_free(struct context *ctx);
201200

202201
#endif

tests/test_config.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "test.h"
2+
3+
extern int config_add(char *name, char *value);
4+
5+
TEST(test_config_bind) {
6+
char n[] = "bind";
7+
8+
ASSERT(config_add(n, "123456") == -1);
9+
ASSERT(config_add(n, "123asf") == -1);
10+
ASSERT(config_add(n, "-1243") == -1);
11+
ASSERT(config_add(n, "") == -1);
12+
ASSERT(config_add(n, "abc") == -1);
13+
ASSERT(config_add(n, "2345") == 0);
14+
ASSERT(config.bind == 2345);
15+
16+
PASS(NULL);
17+
}
18+
19+
TEST(test_config_syslog) {
20+
char n[] = "syslog";
21+
22+
ASSERT(config_add(n, "1") == 0);
23+
ASSERT(config.syslog == 1);
24+
ASSERT(config_add(n, "4") == 0);
25+
ASSERT(config.syslog == 1);
26+
ASSERT(config_add(n, "0") == 0);
27+
ASSERT(config.syslog == 0);
28+
29+
PASS(NULL);
30+
}
31+
32+
TEST_CASE(test_config) {
33+
RUN_TEST(test_config_bind);
34+
RUN_TEST(test_config_syslog);
35+
}

tests/test_socket.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ TEST(test_socket_address_init) {
1212
PASS(NULL);
1313
}
1414

15+
TEST(test_parse_port) {
16+
uint16_t port;
17+
18+
ASSERT(socket_parse_port("abcd", &port) == -1);
19+
ASSERT(socket_parse_port("123455", &port) == -1);
20+
ASSERT(socket_parse_port("-234", &port) == -1);
21+
ASSERT(socket_parse_port("12345", &port) == 0);
22+
ASSERT(port == 12345);
23+
24+
ASSERT(socket_parse_port("", &port) == -1);
25+
26+
char *d6 = ":";
27+
ASSERT(socket_parse_port(d6 + 1, &port) == -1);
28+
29+
ASSERT(socket_parse_port("12345a", &port) == -1);
30+
31+
PASS(NULL);
32+
}
33+
1534
TEST(test_socket_parse_addr) {
1635
struct address address;
1736
memset(&address, 0, sizeof(address));
@@ -47,6 +66,7 @@ TEST(test_socket_parse_addr_wrong) {
4766

4867
TEST_CASE(test_socket) {
4968
RUN_TEST(test_socket_address_init);
69+
RUN_TEST(test_parse_port);
5070
RUN_TEST(test_socket_parse_addr);
5171
RUN_TEST(test_socket_parse_addr_wrong);
5272
}

0 commit comments

Comments
 (0)