From d3a8e1616e8457fab99a3022a1a1b1092fb896ea Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 12 Aug 2022 15:57:04 +0900 Subject: [PATCH] Fix fd leak When IO error occurs, AUL has to close the file descriptor and release the io job resource of aul worker. Change-Id: Ic612531f3a2fa53efa9fe55556b770369731769c Signed-off-by: Hwankyu Jhun --- src/aul_launch.c | 6 +++++- src/aul_worker.c | 28 ++++++++++++++++++++++++++++ src/aul_worker.h | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/aul_launch.c b/src/aul_launch.c index 78120d0..18dbe8a 100644 --- a/src/aul_launch.c +++ b/src/aul_launch.c @@ -113,6 +113,9 @@ static void __destroy_client_channel(gpointer data) g_rec_mutex_unlock(&channel->mutex); g_rec_mutex_clear(&channel->mutex); + if (channel->fd > -1) + close(channel->fd); + free(channel); } @@ -545,9 +548,10 @@ static bool __received_event_cb(int fd, int condition, void *user_data) if (condition & (AUL_IO_HUP | AUL_IO_ERR | AUL_IO_NVAL)) { _E("IO error occurred. condition(%d), fd(%d)", condition, fd); + aul_worker_remove_io_job(worker, fd); __remove_client_channel(channel); __destroy_client_channel(channel); - return false; + return true; } g_rec_mutex_lock(&channel->mutex); diff --git a/src/aul_worker.c b/src/aul_worker.c index 3593bb2..6a9137c 100644 --- a/src/aul_worker.c +++ b/src/aul_worker.c @@ -286,6 +286,34 @@ int aul_worker_add_io_job(aul_worker_h handle, const char *job_name, return AUL_R_OK; } +void aul_worker_remove_io_job(aul_worker_h handle, int fd) +{ + struct aul_worker_s *worker = handle; + struct job_s *job; + GList *iter; + + if (worker == NULL || fd < 0) { + _E("Invalid parameter"); + return; + } + + g_mutex_lock(&worker->mutex); + iter = worker->jobs; + while (iter != NULL) { + job = iter->data; + iter = g_list_next(iter); + if (job->channel == NULL) + continue; + + if (g_io_channel_unix_get_fd(job->channel) == fd) { + worker->jobs = g_list_remove(worker->jobs, job); + __destroy_job(job); + break; + } + } + g_mutex_unlock(&worker->mutex); +} + void aul_worker_destroy(aul_worker_h handle) { struct aul_worker_s *worker = (struct aul_worker_s *)handle; diff --git a/src/aul_worker.h b/src/aul_worker.h index c4a1c10..afdd79a 100644 --- a/src/aul_worker.h +++ b/src/aul_worker.h @@ -40,6 +40,8 @@ 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); +void aul_worker_remove_io_job(aul_worker_h handle, int fd); + int aul_worker_add_anr_timer(aul_worker_h handle, int cmd); int aul_worker_remove_anr_timer(aul_worker_h handle); -- 2.7.4