fix some more valgrind errors
authorTero Marttila <terom@fixme.fi>
Wed, 01 Apr 2009 19:31:11 +0300
changeset 109 bfe9b9a8fe5b
parent 108 50ff7ac8a725
child 110 43e9a7984955
fix some more valgrind errors
src/irc_log.c
src/lua_objs.c
src/nexus.c
src/nexus.h
src/signals.c
src/sock_tcp.c
test/valgrind-suppressions
--- a/src/irc_log.c	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/irc_log.c	Wed Apr 01 19:31:11 2009 +0300
@@ -53,14 +53,21 @@
 
 /**
  * The irc_log_ctx has completed stopping all the channels, and should now destroy itself
+ *
  */
 static void irc_log_stopped (struct irc_log_ctx *ctx)
 {
+    log_debug("destroying the irc_log_ctx");
+
     // schedule an evsql_destroy
     if (evsql_destroy_next(ctx->db))
         log_fatal("evsql_destroy_next failed");
     
+    // hands-off on the db
+    ctx->db = NULL;
+
     // free ourself
+    // XXX: wrong, we need to implement a irc_log_destroy that's called by module?
     free(ctx);
 }
 
@@ -71,6 +78,8 @@
 {
     struct irc_log_ctx *ctx = chan_ctx->ctx;
 
+    log_debug("destroying the irc_log_chan (chan=%s)", irc_chan_name(chan_ctx->chan));
+
     // destroy the channel
     irc_log_chan_destroy(chan_ctx);
 
--- a/src/lua_objs.c	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/lua_objs.c	Wed Apr 01 19:31:11 2009 +0300
@@ -583,8 +583,23 @@
     return 0;
 }
 
+static int lua_nexus_load_config (lua_State *L)
+{
+    struct lua_nexus *lua_nexus = lua_obj_get_obj(L, __func__, "spbot.nexus");
+    struct error_info err;
+
+    const char *path = luaL_checkstring(L, 2);
+    
+    // just load it
+    if (nexus_load_config(lua_nexus->nexus, path, &err))
+        return luaL_error(L, "nexus_load_config(%s): %s", path, error_msg(&err));
+
+    return 0;
+}
+
 static const struct luaL_Reg lua_nexus_methods[] = {
     {   "shutdown",     &lua_nexus_shutdown         },
+    {   "load_config",  &lua_nexus_load_config      },
     {   NULL,           NULL                        }
 };
 
--- a/src/nexus.c	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/nexus.c	Wed Apr 01 19:31:11 2009 +0300
@@ -301,6 +301,11 @@
     return ERROR_CODE(err);
 }
 
+err_t nexus_load_config (struct nexus *nexus, const char *path, struct error_info *err)
+{
+    return lua_config_load(nexus->lua, path, err);
+}
+
 /**
  * Load the lua config file from \a path
  */
@@ -308,7 +313,7 @@
 {
     log_info("loading lua config from %s", path);
     
-    return lua_config_load(nexus->lua, path, err);
+    return nexus_load_config(nexus, path, err);
 }
 
 /**
@@ -409,8 +414,18 @@
     nexus->shutdown = true;
 }
 
+void nexus_crash (struct nexus *nexus)
+{
+    // force-quit the event loop
+    event_base_loopbreak(nexus->ev_base);
+}
+
 void nexus_destroy (struct nexus *nexus)
 {
+    // destroy the console
+    if (nexus->lua_console)
+        lua_console_destroy(nexus->lua_console);
+
     // destroy the lua state
     if (nexus->lua)
         nexus_lua_destroy(nexus->lua);
@@ -419,16 +434,15 @@
     if (nexus->modules)
         modules_destroy(nexus->modules);
 
-    nexus->modules = NULL;
-
     // destroy the irc client
     if (nexus->client)
         irc_client_destroy(nexus->client);
 
-    nexus->client = NULL;
+    // remove the signal handlers
+    if (nexus->signals)
+        signals_free(nexus->signals);
 
-    // then force-quit the event loop
-    event_base_loopbreak(nexus->ev_base);
+    // ok, nexus is now dead
 }
 
 static void on_sigint (evutil_socket_t sig, short what, void *arg)
@@ -505,7 +519,11 @@
         }
     }
 
-    // ok, no cleanup
-    return 0;
+    // cleanup
+    nexus_destroy(nexus);
+    nexus = NULL;
+
+    // ok
+    return EXIT_SUCCESS;
 }
 
--- a/src/nexus.h	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/nexus.h	Wed Apr 01 19:31:11 2009 +0300
@@ -42,6 +42,11 @@
 };
 
 /**
+ * Load a config file into the nexus from the given path
+ */
+err_t nexus_load_config (struct nexus *nexus, const char *path, struct error_info *err);
+
+/**
  * Shut down the nexus cleanly. It /should/ be safe to call this several times, but it won't do anything.
  *
  * Once everything has shut down nicely (which it should, unless there's something buggy), the event loop should exit,
@@ -50,8 +55,12 @@
 void nexus_shutdown (struct nexus *nexus);
 
 /**
- * Destroy the nexus in an ugly fashion. This will attempt to release as many resources as possible, and quit the event
- * loop.
+ * Shut down the nexus fast. This will stop the event loop right away, which should lead to nexus_destroy being called.
+ */
+void nexus_crash (struct nexus *nexus);
+
+/**
+ * Destroy the nexus directly. This is intended to be used once the event loop as exited.
  */
 void nexus_destroy (struct nexus *nexus);
 
--- a/src/signals.c	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/signals.c	Wed Apr 01 19:31:11 2009 +0300
@@ -129,8 +129,7 @@
     
     // free all events
     for (i = 0; i < signals->sig_count; i++) {
-        if (evsignal_del(signals->sig_list[i].ev))
-            log_warn("evsignal_del failed");
+        event_free(signals->sig_list[i].ev);
     }
     
     // free the info itself
--- a/src/sock_tcp.c	Wed Apr 01 18:30:47 2009 +0300
+++ b/src/sock_tcp.c	Wed Apr 01 19:31:11 2009 +0300
@@ -181,13 +181,13 @@
 void sock_tcp_deinit_ev (struct sock_tcp *sock)
 {
     if (sock->ev_read) {
-        event_del(sock->ev_read);
+        event_free(sock->ev_read);
 
         sock->ev_read = NULL;
     }
 
     if (sock->ev_write) {
-        event_del(sock->ev_write);
+        event_free(sock->ev_write);
         
         sock->ev_write = NULL;
     }
@@ -282,6 +282,10 @@
     // XXX: timeouts
     (void) what;
 
+    // init params
+    optval = 0;
+    optlen = sizeof(optval);
+
     // read error code
     if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &optval, &optlen))
         JUMP_SET_ERROR_ERRNO(&err, ERR_GETSOCKOPT);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/valgrind-suppressions	Wed Apr 01 19:31:11 2009 +0300
@@ -0,0 +1,127 @@
+# seems to be a common libc bug:
+#   https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/213825
+{
+    sock_tcp_connect_async-getaddrinfo-1
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libc-2.7.so
+    obj:/lib/ld-2.7.so
+    fun:__libc_dlopen_mode
+    fun:__nss_lookup_function
+    obj:/lib/libc-2.7.so
+    fun:getaddrinfo
+    fun:sock_tcp_connect_async_begin
+    fun:sock_tcp_connect_async
+}
+
+{
+    sock_tcp_connect_async-getaddrinfo-2
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libc-2.7.so
+    obj:/lib/ld-2.7.so
+    fun:__libc_dlopen_mode
+    fun:__nss_lookup_function
+    obj:/lib/libc-2.7.so
+    fun:getaddrinfo
+    fun:sock_tcp_connect_async_begin
+    fun:sock_tcp_connect_async
+}
+
+# I'm not so sure about this one, but dlopen() seems to do 8-byte reads() at locations 4 bytes from the end of the
+# path, so I /suspect/ it's a 64-bit/32-bit bug of some kind?
+{
+    module_load-dlopen-1
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    fun:dlopen
+    fun:module_load
+}
+
+{
+    module_load-dlopen-2
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    fun:dlopen
+    fun:module_load
+}
+
+{
+    module_load-dlopen-3
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    fun:dlopen
+    fun:module_load
+}
+
+{
+    module_load-dlopen-4
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    fun:dlopen
+    fun:module_load
+}
+
+{
+    module_load-dlopen-5
+    Memcheck:Addr8
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    obj:/lib/ld-2.7.so
+    obj:/lib/libdl-2.7.so
+    fun:dlopen
+    fun:module_load
+}
+