* nih/main.h (NihMainLoopFunc): Add delete member.
* nih/main.c (nih_main_loop): Don't run the callback for any function
marked for deletion, instead call nih_free on it.
(nih_main_loop_add_func): Initialise delete to FALSE.
* nih/dbus.c (nih_dbus_release_callback): Add function to mark a
loop function as deleted.
(nih_dbus_setup): and use it as the free function instead of nih_free.
* nih/tests/test_dbus.c (test_connect, test_bus, test_setup): Check
that the loop functions are marked for deletion instead of freed.

* nih/nih_dbus_tool.py (Output.sourceFile): Include limits.h
diff --git a/ChangeLog b/ChangeLog
index 7f09001..7f89d80 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2008-08-12  Casey Dahlin  <cdahlin@redhat.com>
+
+	* nih/main.h (NihMainLoopFunc): Add delete member.
+	* nih/main.c (nih_main_loop): Don't run the callback for any function
+	marked for deletion, instead call nih_free on it.
+	(nih_main_loop_add_func): Initialise delete to FALSE.
+	* nih/dbus.c (nih_dbus_release_callback): Add function to mark a
+	loop function as deleted.
+	(nih_dbus_setup): and use it as the free function instead of nih_free.
+	* nih/tests/test_dbus.c (test_connect, test_bus, test_setup): Check
+	that the loop functions are marked for deletion instead of freed.
+
+	* nih/nih_dbus_tool.py (Output.sourceFile): Include limits.h
+
 2008-07-02  Scott James Remnant  <scott@netsplit.com>
 
 	* nih/Makefile.am (test_com_netsplit_Nih_Test_object_SOURCES)
diff --git a/nih/dbus.c b/nih/dbus.c
index b720470..814caf4 100644
--- a/nih/dbus.c
+++ b/nih/dbus.c
@@ -295,13 +295,13 @@
 /**
  * nih_dbus_release_callback:
  * @func: main loop callback function
- * 
+ *
  * Mark a dbus callback in the main loop deleted.
  **/
 static void
 nih_dbus_release_callback (NihMainLoopFunc *func)
 {
-	func->marked_deleted = TRUE;
+	func->delete = TRUE;
 }
 
 /**
@@ -347,9 +347,8 @@
 	if (! loop)
 		return -1;
 
-	if (! dbus_connection_set_data
-			(conn, main_loop_slot, loop,
-			 (DBusFreeFunction)nih_dbus_release_callback)) {
+	if (! dbus_connection_set_data (conn, main_loop_slot, loop,
+					(DBusFreeFunction)nih_dbus_release_callback)) {
 		nih_free (loop);
 		return -1;
 	}
diff --git a/nih/main.c b/nih/main.c
index 7bdfb1e..a010e2f 100644
--- a/nih/main.c
+++ b/nih/main.c
@@ -617,7 +617,7 @@
 		NIH_LIST_FOREACH_SAFE (nih_main_loop_functions, iter) {
 			NihMainLoopFunc *func = (NihMainLoopFunc *)iter;
 
-			if (! func->marked_deleted) {
+			if (! func->delete) {
 				func->callback (func->data, func);
 			} else {
 				nih_free (iter);
@@ -705,7 +705,8 @@
 
 	func->callback = callback;
 	func->data = data;
-	func->marked_deleted = FALSE;
+
+	func->delete = FALSE;
 
 	nih_list_add (nih_main_loop_functions, &func->entry);
 
diff --git a/nih/main.h b/nih/main.h
index cd5e7ad..f827ce9 100644
--- a/nih/main.h
+++ b/nih/main.h
@@ -44,7 +44,7 @@
  * @entry: list header,
  * @callback: function called,
  * @data: pointer passed to @callback.
- * @marked_deleted: the function should not be run and is ready to be deleted.
+ * @delete: the function should be freed instead of run.
  *
  * This structure contains information about a function that should be
  * called once in each main loop iteration.
@@ -57,7 +57,8 @@
 
 	NihMainLoopCb  callback;
 	void          *data;
-	int            marked_deleted;
+
+	int            delete;
 };
 
 
diff --git a/nih/nih_dbus_tool.py b/nih/nih_dbus_tool.py
index c3df5c4..e998578 100644
--- a/nih/nih_dbus_tool.py
+++ b/nih/nih_dbus_tool.py
@@ -1302,7 +1302,7 @@
 dbus_error_init (&error);
 
 /* Send the reply, appending it to the outgoing queue and blocking. */
-reply = dbus_connection_send_with_reply_and_block (proxy->conn, message, INT_MAX, &error);
+reply = dbus_connection_send_with_reply_and_block (proxy->conn, message, -1, &error);
 if (! reply) {
 	dbus_message_unref (message);
 
diff --git a/nih/tests/test_dbus.c b/nih/tests/test_dbus.c
index fd37e38..568944a 100644
--- a/nih/tests/test_dbus.c
+++ b/nih/tests/test_dbus.c
@@ -212,6 +212,11 @@
 	TEST_TRUE (disconnected);
 	TEST_EQ_P (last_connection, conn);
 
+	TEST_NOT_FREE (loop_func);
+	TEST_TRUE (loop_func->delete);
+
+	nih_free (loop_func);
+
 
 	/* Check that by using a GUID we can reuse connections to the same
 	 * server, the second call to connect just returns the same
@@ -294,6 +299,11 @@
 	TEST_TRUE (disconnected);
 	TEST_EQ_P (last_connection, conn);
 
+	TEST_NOT_FREE (loop_func);
+	TEST_TRUE (loop_func->delete);
+
+	nih_free (loop_func);
+
 
 	/* Check that we can create a new connection to a listening dbus
 	 * server, it should return a hooked up object but if the server
@@ -324,6 +334,14 @@
 	TEST_TRUE (disconnected);
 	TEST_EQ_P (last_connection, conn);
 
+	/* Reap deleted */
+	NIH_LIST_FOREACH_SAFE (nih_main_loop_functions, iter) {
+		NihMainLoopFunc *func = (NihMainLoopFunc *)iter;
+
+		if (func->delete)
+			nih_free (func);
+	}
+
 
 	/* Check that if we create a new connection to a non-listening
 	 * address, no object is returned.
@@ -352,7 +370,7 @@
 	DBusServer      *server;
 	DBusConnection  *conn, *last_conn;
 	NihIoWatch      *io_watch;
-	NihMainLoopFunc *loop_func;
+	NihMainLoopFunc *loop_func = NULL;
 	NihError        *err;
 	pid_t            pid1, pid2;
 	int              fd, wait_fd, status;
@@ -395,10 +413,18 @@
 
 	TEST_EQ_P (loop_func->data, conn);
 
+	TEST_FREE_TAG (loop_func);
+
 	dbus_connection_unref (conn);
 system_bus:
 	dbus_shutdown ();
 
+	if (loop_func) {
+		TEST_NOT_FREE (loop_func);
+		TEST_TRUE (loop_func->delete);
+		nih_free (loop_func);
+	}
+
 
 	/* Check that we can create a connection to the D-Bus system bus,
 	 * the returned object should be hooked up to the main loop.
@@ -419,6 +445,7 @@
 	TEST_EQ (io_watch->fd, fd);
 	TEST_NE_P (io_watch->data, NULL);
 
+#if 0
 	/* Reap deleted */
 	NIH_LIST_FOREACH_SAFE (nih_main_loop_functions, iter) {
 		NihMainLoopFunc *fn = (NihMainLoopFunc *)iter;
@@ -426,6 +453,7 @@
 		if (fn->marked_deleted)
 			nih_free (fn);
 	}
+#endif
 
 	/* Should be a single main loop function. */
 	TEST_LIST_NOT_EMPTY (nih_main_loop_functions);
@@ -434,9 +462,15 @@
 
 	TEST_EQ_P (loop_func->data, conn);
 
+	TEST_FREE_TAG (loop_func);
+
 	dbus_connection_unref (conn);
 	dbus_shutdown ();
 
+	TEST_NOT_FREE (loop_func);
+	TEST_TRUE (loop_func->delete);
+	nih_free (loop_func);
+
 
 	/* Check that we can share connections to a bus. */
 	TEST_FEATURE ("with shared bus connection");
@@ -455,6 +489,7 @@
 	TEST_EQ (io_watch->fd, fd);
 	TEST_NE_P (io_watch->data, NULL);
 
+#if 0
 	/* Reap deleted */
 	NIH_LIST_FOREACH_SAFE (nih_main_loop_functions, iter) {
 		NihMainLoopFunc *fn = (NihMainLoopFunc *)iter;
@@ -462,6 +497,7 @@
 		if (fn->marked_deleted)
 			nih_free (fn);
 	}
+#endif
 
 	/* Should be a single main loop function. */
 	TEST_LIST_NOT_EMPTY (nih_main_loop_functions);
@@ -487,6 +523,7 @@
 
 	/* Should be the same main loop function. */
 	TEST_NOT_FREE (loop_func);
+	TEST_FALSE (loop_func->delete);
 	TEST_LIST_NOT_EMPTY (nih_main_loop_functions);
 	TEST_EQ_P (nih_main_loop_functions->next, &loop_func->entry);
 	TEST_EQ_P (loop_func->entry.next, nih_main_loop_functions);
@@ -496,6 +533,10 @@
 	dbus_connection_unref (last_conn);
 	dbus_shutdown ();
 
+	TEST_NOT_FREE (loop_func);
+	TEST_TRUE (loop_func->delete);
+	nih_free (loop_func);
+
 
 	/* Check that if the bus disconnects before registration, NULL
 	 * is returned along with an error.  Stock dbus tends to bail out
@@ -608,6 +649,7 @@
 	TEST_EQ (io_watch->fd, fd);
 	TEST_NE_P (io_watch->data, NULL);
 
+#if 0
 	/* Reap deleted */
 	NIH_LIST_FOREACH_SAFE (nih_main_loop_functions, iter) {
 		NihMainLoopFunc *fn = (NihMainLoopFunc *)iter;
@@ -615,6 +657,7 @@
 		if (fn->marked_deleted)
 			nih_free (fn);
 	}
+#endif
 
 	/* Should be a single main loop function. */
 	TEST_LIST_NOT_EMPTY (nih_main_loop_functions);
@@ -645,6 +688,7 @@
 
 	/* Should be the same main loop function. */
 	TEST_NOT_FREE (loop_func);
+	TEST_FALSE (loop_func->delete);
 	TEST_LIST_NOT_EMPTY (nih_main_loop_functions);
 	TEST_EQ_P (nih_main_loop_functions->next, &loop_func->entry);
 	TEST_EQ_P (loop_func->entry.next, nih_main_loop_functions);
@@ -652,6 +696,10 @@
 
 	dbus_connection_unref (conn);
 	dbus_shutdown ();
+
+	TEST_NOT_FREE (loop_func);
+	TEST_TRUE (loop_func->delete);
+	nih_free (loop_func);
 }