Fix double close issue 73/287273/2
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 26 Jan 2023 06:08:56 +0000 (06:08 +0000)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 26 Jan 2023 06:49:58 +0000 (06:49 +0000)
When calling the g_io_channel_unref() function, the socket fd is closed
by the glib. The AUL library has a problem that is a double close issue.
This patch is to fix the issue.

+------------------------------------------------------------------------------+
| [__AUL_WORKER__] close(188)                                                  |
| close backtrace() returned 9 addesses                                        |
| [ 0] close /lib/libinterceptor.so(close+0x4d) [0xb60da72a]                   |
| [ 1] ? /lib/libglib-2.0.so.0(+0x430c00ea) [0xb0a620ea]                       |
| [ 2] g_io_channel_shutdown /lib/libglib-2.0.so.0(g_io_channel_shutdown+0x75) |
|      [0xb0aa49f2]                                                            |
| [ 3] ? /lib/libaul.so.0(+0x43c0df00) [0xae66ff00]                            |
| [ 4] g_list_foreach /lib/libglib-2.0.so.0(g_list_foreach+0x13) [0xb0a99080]  |
| [ 5] g_list_free_full /lib/libglib-2.0.so.0(g_list_free_full+0x9)            |
|      [0xb0a9dc0e]                                                            |
| [ 6] ? /lib/libaul.so.0(+0x43c0e284) [0xae670284]                            |
| [ 7] ? /lib/libglib-2.0.so.0(+0x430dbc42) [0xb0a7dc42]                       |
| [ 8] ? /lib/libpthread.so.0(+0x430174c4) [0xb5d044c4]                        |
+------------------------------------------------------------------------------+
| [__MAIN__] close(188)                                                        |
| close backtrace() returned 13 addesses                                       |
| [ 0] close /lib/libinterceptor.so(close+0x4d) [0xb60da72a]                   |
| [ 1] aul_sock_destroy_server /lib/libaul.so.0(aul_sock_destroy_server+0x2e3) |
|      [0xae67302c]                                                            |
| [ 2] aul_finalize /lib/libaul.so.0(aul_finalize+0x15) [0xae673196]           |
| [ 3] _ZN9tizen_cpp11AppCoreBase11OnTerminateEv                               |
|      /lib/libapp-core-cpp.so.1(_ZN9tizen_cpp11AppCoreBase11OnTerminateEv+0x5)|
| [0xae22d046]                                                           |
| [__MAIN__] EBADF occurs. fd(188)                                             |
+------------------------------------------------------------------------------+

Change-Id: Id465eefae87653d2acb706875732ec176ce95bb1
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_launch.c
src/aul_worker.c
src/aul_worker.h

index 528a1c8..675b964 100644 (file)
@@ -619,13 +619,12 @@ static bool __connected_event_cb(int fd, int condition, void *user_data)
        if (clifd < 0)
                return true;
 
-       ret = aul_worker_add_io_job(worker, "client", clifd, cond,
-                               __received_event_cb, worker);
+       ret = aul_worker_add_io_job(worker, "client", clifd, cond, true,
+                       __received_event_cb, worker);
        if (ret < 0) {
                _E("Failed to add io job. error(%d)", ret);
                __remove_client_channel(channel);
                __destroy_client_channel(channel);
-               close(clifd);
                return true;
        }
 
@@ -712,7 +711,7 @@ static int __initialize_context(void)
        }
 
        ret = aul_worker_add_io_job(__context.worker, "server", fd, AUL_IO_IN,
-                       __connected_event_cb, __context.worker);
+                       false, __connected_event_cb, __context.worker);
        if (ret < 0) {
                __finalize_context();
                return ret;
index ee6f497..8c58feb 100644 (file)
@@ -90,17 +90,9 @@ static void __destroy_job(gpointer data)
        struct job_s *job = (struct job_s *)data;
        GSource *source;
        GMainContext *context;
-       GError *error = NULL;
-
-       if (job->channel) {
-               g_io_channel_shutdown(job->channel, TRUE, &error);
-               if (error) {
-                       _E("g_io_channel_shutdown() is failed. error(%s)",
-                                       error->message);
-                       g_error_free(error);
-               }
+
+       if (job->channel)
                g_io_channel_unref(job->channel);
-       }
 
        if (job->tag) {
                context = g_main_context_get_thread_default();
@@ -237,7 +229,8 @@ static gboolean __io_job_handler(GIOChannel *io, GIOCondition condition,
 }
 
 int aul_worker_add_io_job(aul_worker_h handle, const char *job_name,
-               int fd, int condition, aul_worker_io_job_cb callback,
+               int fd, int condition, bool do_close,
+               aul_worker_io_job_cb callback,
                void *user_data)
 {
        GIOCondition cond = __convert_aul_io_condition(condition);
@@ -254,9 +247,13 @@ int aul_worker_add_io_job(aul_worker_h handle, const char *job_name,
        channel = g_io_channel_unix_new(fd);
        if (!channel) {
                _E("Failed to create GIOChannel");
+               if (do_close)
+                       close(fd);
+
                return AUL_R_ENOMEM;
        }
 
+       g_io_channel_set_close_on_unref(channel, do_close);
        source = g_io_create_watch(channel, cond);
        if (!source) {
                _E("Failed to create GSource");
index 5f8ff2f..32db6fd 100644 (file)
@@ -37,8 +37,8 @@ aul_worker_h aul_worker_create(const char *name);
 void aul_worker_destroy(aul_worker_h handle);
 
 int aul_worker_add_io_job(aul_worker_h handle, const char *job_name,
-               int fd, int condition, aul_worker_io_job_cb callback,
-               void *user_data);
+               int fd, int condition, bool do_close,
+               aul_worker_io_job_cb callback, void *user_data);
 
 void aul_worker_remove_io_job(aul_worker_h handle, int fd);