From 4a8a50efc8207839ed1596076b67f16c3596214b Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 27 Dec 2010 00:27:38 +0000 Subject: better error handling at tcp listen module git-svn-id: https://svn.spreadspace.org/gcsd/trunk@83 ac14a137-c7f1-4531-abe0-07747231d213 --- src/l_tcp.c | 26 ++++++++++++++------------ src/modules/tcp_listen.lua | 7 ++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/l_tcp.c b/src/l_tcp.c index da452d4..158a8ba 100644 --- a/src/l_tcp.c +++ b/src/l_tcp.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -85,14 +86,14 @@ static int init_listener(tcp_endpoint_t* end) { int fd = socket(end->addr_.ss_family, SOCK_STREAM, 0); if(fd < 0) { - log_printf(ERROR, "tcp: Error on opening socket: %s", strerror(errno)); + log_printf(ERROR, "tcp: error on opening socket: %s", strerror(errno)); return -1; } int on = 1; int ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if(ret) { - log_printf(ERROR, "tcp: Error on setsockopt(): %s", strerror(errno)); + log_printf(ERROR, "tcp: error on setsockopt(): %s", strerror(errno)); return -1; } if(end->addr_.ss_family == AF_INET6) { @@ -103,7 +104,7 @@ static int init_listener(tcp_endpoint_t* end) char* ls = tcp_endpoint_to_string(end); ret = bind(fd, (struct sockaddr *)&(end->addr_), end->len_); if(ret) { - log_printf(ERROR, "tcp: Error on bind(%s): %s", ls ? ls:"", strerror(errno)); + log_printf(ERROR, "tcp: error on bind(%s): %s", ls ? ls:"", strerror(errno)); if(ls) free(ls); return -1; } @@ -111,7 +112,7 @@ static int init_listener(tcp_endpoint_t* end) ret = listen(fd, 0); if(ret) { - log_printf(ERROR, "tcp: Error on listen(): %s", strerror(errno)); + log_printf(ERROR, "tcp: error on listen(): %s", strerror(errno)); if(ls) free(ls); return -1; } @@ -138,14 +139,14 @@ static int l_tcp_server(lua_State *L) } int errcode = getaddrinfo(addr, port, &hints, &res); - // TODO: better error handling (no lua error) - if(errcode != 0) - luaL_error(L, "tcp: resolver error: %s", gai_strerror(errcode)); - if(!res) - luaL_error(L, "tcp: no address found!"); + if(errcode != 0 || !res) { + log_printf(ERROR, "tcp: resolver error: %s", errcode ? gai_strerror(errcode):"no address found!"); + lua_pushnil(L); + lua_pushstring(L, "error at tcp-server initialization"); + return 2; + } lua_newtable(L); - int idx = 1; struct addrinfo* r = res; while(r) { @@ -161,8 +162,9 @@ static int l_tcp_server(lua_State *L) int fd = init_listener(end); if(fd < 0) { freeaddrinfo(res); - // TODO: better error handling (no lua error) - luaL_error(L, "tcp: Error at server init"); + lua_pushnil(L); + lua_pushstring(L, "error at tcp-server initialization"); + return 2; } lua_pushliteral(L, "fd"); diff --git a/src/modules/tcp_listen.lua b/src/modules/tcp_listen.lua index 77a7e26..32c3de5 100644 --- a/src/modules/tcp_listen.lua +++ b/src/modules/tcp_listen.lua @@ -48,7 +48,12 @@ function tcp_listen:new(config, runtype) inst.name = config.name end - inst.listeners = tcp.server(config.addr, config.port, config.resolv_type) + local lst, err = tcp.server(config.addr, config.port, config.resolv_type) + if(not lst) then + return nil + end + + inst.listeners = lst for _, l in ipairs(inst.listeners) do log.printf(log.NOTICE, "%s: listening on %s", inst.name, tcp.endtostring(l.local_end)) -- cgit v1.2.3