summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-16 15:54:33 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-16 15:54:33 +0200
commit202fdce48fd8fb04778bd3c7e30a036a68b32ba2 (patch)
tree80045d42e0248acbb5c22e46d79d40c49160357a
parent7b2240a7a9387068f8da0f9bfeac90ec27034878 (diff)
Convert cfgdir and proxy to use debugging
-rw-r--r--cfgdir.c76
-rw-r--r--config.c4
-rw-r--r--config.h3
-rw-r--r--main.c3
-rw-r--r--main.h1
-rw-r--r--proxy.c34
6 files changed, 66 insertions, 55 deletions
diff --git a/cfgdir.c b/cfgdir.c
index 3d5c2f7..54a2e6e 100644
--- a/cfgdir.c
+++ b/cfgdir.c
@@ -105,14 +105,16 @@ scfg_parse(struct cfg *cfg, struct server *scfg)
while (true) {
int key;
+ const char *keyname;
union cfg_value value;
- if (!config_parse_line(cfg, &pos, scfg_key_map, &key, &value))
+ if (!config_parse_line(cfg, &pos, scfg_key_map, &key, &keyname, &value))
break;
if (key == SCFG_KEY_INVALID)
break;
- printf("Got a key-value pair: %i = something\n", key);
+
+ debug(DBG_CFG, "%s: key %s\n", scfg->name, keyname);
switch (key) {
@@ -230,12 +232,12 @@ scfg_read_cb(struct cfg *cfg, struct uring_task *task, int res)
return;
if (res <= 0) {
- fprintf(stderr, "%s: error reading config file for %s: %s\n",
- __func__, scfg->name, strerror(-res));
+ error("error reading config file for %s: %s\n",
+ scfg->name, strerror(-res));
server_delete(cfg, scfg);
}
- printf("Asked to parse server cfg %s (bytes %i)\n", scfg->name, res);
+ debug(DBG_CFG, "%s: parsing cfg (%i bytes)\n", scfg->name, res);
uring_task_close_fd(cfg, &scfg->task);
scfg_parse(cfg, scfg);
server_commit(cfg, scfg);
@@ -246,17 +248,18 @@ scfg_open_cb(struct cfg *cfg, struct uring_task *task, int res)
{
struct server *scfg = container_of(task, struct server, task);
- if (task->dead)
+ if (task->dead) {
+ debug(DBG_CFG, "task dead\n");
return;
+ }
if (res < 0) {
- fprintf(stderr, "%s: open(%s) failed: %s\n",
- __func__, scfg->name, strerror(-res));
+ error("open(%s) failed: %s\n", scfg->name, strerror(-res));
server_delete(cfg, scfg);
return;
}
- printf("Asked to read server cfg %s (fd %i)\n", scfg->name, res);
+ debug(DBG_CFG, "reading server cfg %s (fd %i)\n", scfg->name, res);
uring_task_set_fd(&scfg->task, res);
uring_tbuf_read_until_eof(cfg, &scfg->task, scfg_read_cb);
}
@@ -289,7 +292,8 @@ inotify_free(struct uring_task *task)
struct inotify_ev *iev = container_of(task, struct inotify_ev, task);
struct cfg *cfg = container_of(task->parent, struct cfg, task);
- fprintf(stderr, "%s called\n", __func__);
+ debug(DBG_CFG, "called\n");
+
if (!iev || !cfg)
die("iev or cfg is NULL!?");
@@ -300,43 +304,43 @@ inotify_free(struct uring_task *task)
static void
inotify_event_dump(const struct inotify_event *event)
{
- printf("Event:\n");
- printf(" * WD : %i\n", event->wd);
- printf(" * Cookie : %" PRIu32 "\n", event->cookie);
- printf(" * Length : %" PRIu32 "\n", event->len);
- printf(" * Name : %s\n", event->name);
- printf(" * Mask : %" PRIu32 "\n", event->mask);
+ debug(DBG_CFG, "inotify event:\n");
+ debug(DBG_CFG, " * WD : %i\n", event->wd);
+ debug(DBG_CFG, " * Cookie : %" PRIu32 "\n", event->cookie);
+ debug(DBG_CFG, " * Length : %" PRIu32 "\n", event->len);
+ debug(DBG_CFG, " * Name : %s\n", event->name);
+ debug(DBG_CFG, " * Mask : %" PRIu32 "\n", event->mask);
if (event->mask & IN_ACCESS)
- printf("\tIN_ACCESS\n");
+ debug(DBG_CFG, "\tIN_ACCESS\n");
else if(event->mask & IN_MODIFY)
- printf("\tIN_MODIFY\n");
+ debug(DBG_CFG, "\tIN_MODIFY\n");
else if(event->mask & IN_ATTRIB)
- printf("\tIN_ATTRIB\n");
+ debug(DBG_CFG, "\tIN_ATTRIB\n");
else if(event->mask & IN_CLOSE_WRITE)
- printf("\tIN_CLOSE_WRITE\n");
+ debug(DBG_CFG, "\tIN_CLOSE_WRITE\n");
else if(event->mask & IN_CLOSE_NOWRITE)
- printf("\tIN_CLOSE_NOWRITE\n");
+ debug(DBG_CFG, "\tIN_CLOSE_NOWRITE\n");
else if(event->mask & IN_OPEN)
- printf("\tIN_OPEN\n");
+ debug(DBG_CFG, "\tIN_OPEN\n");
else if(event->mask & IN_MOVED_FROM)
- printf("\tIN_MOVED_FROM\n");
+ debug(DBG_CFG, "\tIN_MOVED_FROM\n");
else if(event->mask & IN_MOVED_TO)
- printf("\tIN_MOVED_TO\n");
+ debug(DBG_CFG, "\tIN_MOVED_TO\n");
else if(event->mask & IN_CREATE)
- printf("\tIN_CREATE\n");
+ debug(DBG_CFG, "\tIN_CREATE\n");
else if(event->mask & IN_DELETE)
- printf("\tIN_DELETE\n");
+ debug(DBG_CFG, "\tIN_DELETE\n");
else if(event->mask & IN_DELETE_SELF)
- printf("\tIN_DELETE_SELF\n");
+ debug(DBG_CFG, "\tIN_DELETE_SELF\n");
else if(event->mask & IN_MOVE_SELF)
- printf("\tIN_MOVE_SELF\n");
+ debug(DBG_CFG, "\tIN_MOVE_SELF\n");
else if(event->mask & IN_UNMOUNT)
- printf("\tIN_UNMOUNT\n");
+ debug(DBG_CFG, "\tIN_UNMOUNT\n");
else if(event->mask & IN_Q_OVERFLOW)
- printf("\tIN_Q_OVERFLOW\n");
+ debug(DBG_CFG, "\tIN_Q_OVERFLOW\n");
else if(event->mask & IN_IGNORED)
- printf("\tIN_IGNORED\n");
- printf("\n");
+ debug(DBG_CFG, "\tIN_IGNORED\n");
+ debug(DBG_CFG, "\n");
}
static void
@@ -378,10 +382,10 @@ inotify_cb(struct cfg *cfg, struct uring_task *task, int res)
server_delete_by_name(cfg, event->name);
else if (event->mask & (IN_MOVED_TO | IN_CREATE | IN_CLOSE_WRITE)) {
scfg = server_new(cfg, event->name);
- fprintf(stderr, "%s: calling openat(%s)\n", __func__, scfg->name);
+ verbose("New server config file detected: %s\n", scfg->name);
uring_openat(cfg, &scfg->task, scfg->name, scfg_open_cb);
} else
- error("inotify: weird, unknown event: 0x%08x\n", event->mask);
+ error("inotify: unknown event: 0x%08x\n", event->mask);
}
uring_read(cfg, &iev->task, iev->buf, sizeof(iev->buf), inotify_cb);
@@ -397,11 +401,11 @@ void
cfgdir_delete(struct cfg *cfg)
{
if (!cfg->iev) {
- fprintf(stderr, "%s called with no iev!\n", __func__);
+ error("missing arguments\n");
return;
}
- fprintf(stderr, "%s called, closing fd %i\n", __func__, cfg->iev->task.fd);
+ debug(DBG_CFG, "closing fd %i\n", cfg->iev->task.fd);
uring_task_destroy(cfg, &cfg->iev->task);
cfg->iev = NULL;
}
diff --git a/config.c b/config.c
index 4ccd3c3..47d0572 100644
--- a/config.c
+++ b/config.c
@@ -258,7 +258,7 @@ out:
/* Returns true if theres data left to parse in buf */
bool
config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap,
- int *rkey, union cfg_value *rvalue)
+ int *rkey, const char **rkeyname, union cfg_value *rvalue)
{
char *line, *tmp, *key;
int i;
@@ -346,12 +346,14 @@ config_parse_line(struct cfg *cfg, char **buf, struct cfg_key_value_map *kvmap,
}
*rkey = kvmap[i].key_value;
+ *rkeyname = kvmap[i].key_name;
return true;
}
out:
fprintf(stderr, "Invalid line\n");
*rkey = 0;
+ *rkeyname = NULL;
return true;
}
diff --git a/config.h b/config.h
index 30be385..7a92c1c 100644
--- a/config.h
+++ b/config.h
@@ -22,7 +22,8 @@ union cfg_value {
bool config_parse_line(struct cfg *cfg, char **buf,
struct cfg_key_value_map *kvmap,
- int *key, union cfg_value *value);
+ int *rkey, const char **rkeyname,
+ union cfg_value *rvalue);
bool config_parse_header(struct cfg *cfg, const char *title, char **buf);
diff --git a/main.c b/main.c
index ad99684..a457caa 100644
--- a/main.c
+++ b/main.c
@@ -104,6 +104,9 @@ const struct {
.name = "server",
.val = DBG_SRV
},{
+ .name = "proxy",
+ .val = DBG_PROXY
+ },{
.name = NULL,
.val = 0
}
diff --git a/main.h b/main.h
index 4d73f2b..a4bf7da 100644
--- a/main.h
+++ b/main.h
@@ -23,6 +23,7 @@ enum debug_category {
DBG_SIG = (0x1 << 7),
DBG_UR = (0x1 << 8),
DBG_SRV = (0x1 << 9),
+ DBG_PROXY = (0x1 << 10),
};
static inline bool
diff --git a/proxy.c b/proxy.c
index 125c408..91b7c02 100644
--- a/proxy.c
+++ b/proxy.c
@@ -74,12 +74,12 @@ proxy_free(struct uring_task *task)
format_bytes(cts, sizeof(cts), proxy->client_bytes);
format_bytes(stc, sizeof(stc), proxy->server_bytes);
- fprintf(stderr, "%s: proxy connection %s -> %s closed "
- "(CtS: %s, StC: %s), duration %s\n",
- proxy->scfg->name,
- proxy->client_conn.remotestr,
- proxy->server_conn.remotestr,
- cts, stc, duration);
+ info("%s: proxy connection %s -> %s closed "
+ "(CtS: %s, StC: %s), duration %s\n",
+ proxy->scfg->name,
+ proxy->client_conn.remotestr,
+ proxy->server_conn.remotestr,
+ cts, stc, duration);
}
list_del(&proxy->list);
@@ -91,7 +91,7 @@ proxy_client_free(struct uring_task *task)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
- fprintf(stderr, "%s: %s client connection closed\n", __func__, proxy->scfg->name);
+ debug(DBG_PROXY, "%s: client connection closed\n", proxy->scfg->name);
}
static void
@@ -99,13 +99,13 @@ proxy_server_free(struct uring_task *task)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
- fprintf(stderr, "%s: %s server connection closed\n", __func__, proxy->scfg->name);
+ debug(DBG_PROXY, "%s: server connection closed\n", proxy->scfg->name);
}
void
proxy_delete(struct cfg *cfg, struct server_proxy *proxy)
{
- fprintf(stderr, "%s: shutting down proxy 0x%p\n", __func__, proxy);
+ debug(DBG_PROXY, "%s: shutting down proxy %p\n", proxy->scfg->name, proxy);
uring_task_destroy(cfg, &proxy->servertask);
uring_task_destroy(cfg, &proxy->clienttask);
@@ -119,8 +119,8 @@ proxy_client_data_out(struct cfg *cfg, struct uring_task *task, int res)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
- fprintf(stderr, "%s: result was %i\n", __func__, res);
if (res <= 0) {
+ debug(DBG_PROXY, "%s: result was %i\n", proxy->scfg->name, res);
uring_task_close_fd(cfg, task);
proxy_delete(cfg, proxy);
return;
@@ -136,8 +136,8 @@ proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
- fprintf(stderr, "%s: result was %i\n", __func__, res);
if (res <= 0) {
+ debug(DBG_PROXY, "%s: result was %i\n", proxy->scfg->name, res);
uring_task_close_fd(cfg, task);
proxy_delete(cfg, proxy);
return;
@@ -154,8 +154,8 @@ proxy_server_data_out(struct cfg *cfg, struct uring_task *task, int res)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
- fprintf(stderr, "%s: result was %i\n", __func__, res);
if (res <= 0) {
+ debug(DBG_PROXY, "%s: result was %i\n", proxy->scfg->name, res);
uring_task_close_fd(cfg, task);
proxy_delete(cfg, proxy);
return;
@@ -171,8 +171,8 @@ proxy_server_data_in(struct cfg *cfg, struct uring_task *task, int res)
{
struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
- fprintf(stderr, "%s: result was %i\n", __func__, res);
if (res <= 0) {
+ debug(DBG_PROXY, "%s: result was %i\n", proxy->scfg->name, res);
uring_task_close_fd(cfg, task);
proxy_delete(cfg, proxy);
return;
@@ -188,14 +188,14 @@ proxy_connected_cb(struct cfg *cfg, struct connection *conn, bool connected)
struct server_proxy *proxy = container_of(conn, struct server_proxy, server_conn);
if (!connected) {
- fprintf(stderr, "%s: proxy connection to remote server failed\n",
- proxy->scfg->name);
+ error("%s: proxy connection to remote server failed\n",
+ proxy->scfg->name);
proxy_delete(cfg, proxy);
return;
}
proxy->sfd = proxy->servertask.fd;
- fprintf(stderr, "%s: proxy connection %s -> %s opened\n",
+ verbose("%s: proxy connection %s -> %s opened\n",
proxy->scfg->name,
proxy->client_conn.remotestr,
proxy->server_conn.remotestr);
@@ -212,7 +212,7 @@ proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, in
proxy = zmalloc(sizeof(*proxy));
if (!proxy) {
- perror("malloc");
+ error("malloc: %m");
return NULL;
}