2013-11-27 16:19:10

by Andrei Emeltchenko

[permalink] [raw]
Subject: [PATCH 1/5] android: Avoid memory leak warnings for event_loop

From: Andrei Emeltchenko <[email protected]>

Move creation of event_loop closer to g_main_loop_run. This avoids
calling g_main_loop_unref too many times in initialization error paths.
This is safe since g_main_loop_quit eval to NOOP if parameter == NULL.
---
android/main.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/android/main.c b/android/main.c
index bfd2a87..830eef2 100644
--- a/android/main.c
+++ b/android/main.c
@@ -559,7 +559,6 @@ int main(int argc, char *argv[])
exit(EXIT_SUCCESS);
}

- event_loop = g_main_loop_new(NULL, FALSE);
signal = setup_signalfd();
if (!signal)
return EXIT_FAILURE;
@@ -584,6 +583,8 @@ int main(int argc, char *argv[])

DBG("Entering main loop");

+ event_loop = g_main_loop_new(NULL, FALSE);
+
g_main_loop_run(event_loop);

g_source_remove(signal);
--
1.8.3.2



2013-11-27 20:25:37

by Andrei Emeltchenko

[permalink] [raw]
Subject: Re: [PATCH 3/5] android/main: Remove signal source on exit

Hi,

On Wed, Nov 27, 2013 at 6:19 PM, Andrei Emeltchenko
<[email protected]> wrote:
> From: Andrei Emeltchenko <[email protected]>
>
> Remove signal source on exit and move check capability function in order
> to avoid extra check.
> ---
> android/main.c | 8 +++++---
> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/android/main.c b/android/main.c
> index dd5c622..e408c21 100644
> --- a/android/main.c
> +++ b/android/main.c
> @@ -536,6 +536,9 @@ int main(int argc, char *argv[])
> GError *err = NULL;
> guint signal;
>
> + if (!set_capabilities())
> + return EXIT_FAILURE;
> +

Please disregard this patch, debug needs to be initialized first.

> /* Core Service (ID=0) should always be considered registered */
> services[0] = true;
>
> @@ -565,18 +568,17 @@ int main(int argc, char *argv[])
>
> __btd_log_init("*", 0);
>
> - if (!set_capabilities())
> - return EXIT_FAILURE;
> -
> bluetooth_start_timeout = g_timeout_add_seconds(STARTUP_GRACE_SECONDS,
> quit_eventloop, NULL);
> if (bluetooth_start_timeout == 0) {
> error("Failed to init startup timeout");
> + g_source_remove(signal);
> return EXIT_FAILURE;
> }
>
> if (!bt_bluetooth_start(option_index, adapter_ready)) {
> g_source_remove(bluetooth_start_timeout);
> + g_source_remove(signal);
> return EXIT_FAILURE;
> }
>
> --
> 1.8.3.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html

2013-11-27 16:19:11

by Andrei Emeltchenko

[permalink] [raw]
Subject: [PATCH 2/5] android/main: Remove timeout source on exit

From: Andrei Emeltchenko <[email protected]>

This fixes memory leak types of warnings from some tools.
---
android/main.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/android/main.c b/android/main.c
index 830eef2..dd5c622 100644
--- a/android/main.c
+++ b/android/main.c
@@ -575,8 +575,10 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

- if (!bt_bluetooth_start(option_index, adapter_ready))
+ if (!bt_bluetooth_start(option_index, adapter_ready)) {
+ g_source_remove(bluetooth_start_timeout);
return EXIT_FAILURE;
+ }

/* Use params: mtu = 0, flags = 0 */
start_sdp_server(0, 0);
@@ -589,6 +591,9 @@ int main(int argc, char *argv[])

g_source_remove(signal);

+ if (bluetooth_start_timeout > 0)
+ g_source_remove(bluetooth_start_timeout);
+
cleanup_hal_connection();
stop_sdp_server();
bt_bluetooth_cleanup();
--
1.8.3.2


2013-11-27 16:19:13

by Andrei Emeltchenko

[permalink] [raw]
Subject: [PATCH 4/5] android/socket: Cleanup sockets on unregister

From: Andrei Emeltchenko <[email protected]>

This cleans up rfsock structures closing all sockets and making general cleanup
for servers and for connections. This will be called form socket unregister.
---
android/socket.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)

diff --git a/android/socket.c b/android/socket.c
index 1fb154d..d55db54 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -943,7 +943,27 @@ bool bt_socket_register(int sk, const bdaddr_t *addr)
return true;
}

+static void free_connection(gpointer data, gpointer user_data)
+{
+ struct rfcomm_sock *rfsock = data;
+
+ connections = g_list_remove(connections, rfsock);
+ cleanup_rfsock(rfsock);
+}
+
+static void free_server(gpointer data, gpointer user_data)
+{
+ struct rfcomm_sock *rfsock = data;
+
+ servers = g_list_remove(servers, rfsock);
+ cleanup_rfsock(rfsock);
+}
+
void bt_socket_unregister(void)
{
DBG("");
+
+ g_list_foreach(connections, free_connection, NULL);
+
+ g_list_foreach(servers, free_server, NULL);
}
--
1.8.3.2


2013-11-27 16:19:14

by Andrei Emeltchenko

[permalink] [raw]
Subject: [PATCH 5/5] android/socket: Fix rfsock lists

From: Andrei Emeltchenko <[email protected]>

This fixes several places where rfsock structure were not removed
from the list due to connection errors.
---
android/socket.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/android/socket.c b/android/socket.c
index d55db54..3f07dc6 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -625,8 +625,6 @@ static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
return;
}

- connections = g_list_append(connections, rfsock_acc);
-
DBG("rfsock: fd %d real_sock %d chan %u sock %d",
rfsock->fd, rfsock->real_sock, rfsock->channel,
sock_acc);
@@ -636,6 +634,8 @@ static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
return;
}

+ connections = g_list_append(connections, rfsock_acc);
+
/* Handle events from Android */
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
io_stack = g_io_channel_unix_new(rfsock_acc->fd);
@@ -700,7 +700,6 @@ static int handle_listen(void *buf)
}

rfsock->real_sock = g_io_channel_unix_get_fd(io);
- servers = g_list_append(servers, rfsock);

/* TODO: Add server watch */
g_io_channel_set_close_on_unref(io, TRUE);
@@ -717,6 +716,8 @@ static int handle_listen(void *buf)

rfsock->service_handle = sdp_service_register(profile, cmd->name);

+ servers = g_list_append(servers, rfsock);
+
return hal_fd;
}

@@ -787,6 +788,7 @@ static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)

return;
fail:
+ connections = g_list_remove(connections, rfsock);
cleanup_rfsock(rfsock);
}

@@ -865,6 +867,7 @@ static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)

return;
fail:
+ connections = g_list_remove(connections, rfsock);
cleanup_rfsock(rfsock);
}

--
1.8.3.2


2013-11-27 16:19:12

by Andrei Emeltchenko

[permalink] [raw]
Subject: [PATCH 3/5] android/main: Remove signal source on exit

From: Andrei Emeltchenko <[email protected]>

Remove signal source on exit and move check capability function in order
to avoid extra check.
---
android/main.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/android/main.c b/android/main.c
index dd5c622..e408c21 100644
--- a/android/main.c
+++ b/android/main.c
@@ -536,6 +536,9 @@ int main(int argc, char *argv[])
GError *err = NULL;
guint signal;

+ if (!set_capabilities())
+ return EXIT_FAILURE;
+
/* Core Service (ID=0) should always be considered registered */
services[0] = true;

@@ -565,18 +568,17 @@ int main(int argc, char *argv[])

__btd_log_init("*", 0);

- if (!set_capabilities())
- return EXIT_FAILURE;
-
bluetooth_start_timeout = g_timeout_add_seconds(STARTUP_GRACE_SECONDS,
quit_eventloop, NULL);
if (bluetooth_start_timeout == 0) {
error("Failed to init startup timeout");
+ g_source_remove(signal);
return EXIT_FAILURE;
}

if (!bt_bluetooth_start(option_index, adapter_ready)) {
g_source_remove(bluetooth_start_timeout);
+ g_source_remove(signal);
return EXIT_FAILURE;
}

--
1.8.3.2