[SQE][LibFuzzer] Fix opened file descriptors cleanup in libmm-sound
[platform/core/multimedia/libmm-sound.git] / mm_sound_device.c
index 457c7c2..fc72e58 100644 (file)
@@ -30,7 +30,8 @@
 
 #define VOLUME_TYPE_LEN 64
 
-mm_sound_device_list_t *g_device_list;
+mm_sound_device_list_t g_device_list;
+pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int _check_for_valid_mask (int flags)
 {
@@ -224,26 +225,25 @@ int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDevic
                return ret;
        }
 
-       /* free previously allocated list */
-       if (g_device_list != NULL)
-               __free_device_list(g_device_list);
+       pthread_mutex_lock(&g_thread_mutex);
 
-       if (!(g_device_list = g_malloc0(sizeof(mm_sound_device_list_t)))) {
-               debug_error("[Client] Allocate device list failed");
-               return MM_ERROR_SOUND_INTERNAL;
+       if (g_device_list.list != NULL) {
+               g_list_free_full(g_device_list.list, g_free);
+               g_device_list.list = NULL;
        }
 
-       g_device_list->is_new_device_list = true;
+       g_device_list.is_new_device_list = true;
 
-       ret = mm_sound_client_get_current_connected_device_list(flags, g_device_list);
+       ret = mm_sound_client_get_current_connected_device_list(flags, &g_device_list);
        if (ret < 0) {
                debug_error("Could not get current connected device list, ret = %x\n", ret);
-               g_free(g_device_list);
-               g_device_list = NULL;
+               g_device_list.list = NULL;
        } else {
-               *device_list = g_device_list;
+               *device_list = &g_device_list;
        }
 
+       pthread_mutex_unlock(&g_thread_mutex);
+
        return ret;
 }
 
@@ -287,6 +287,35 @@ int mm_sound_free_device_list(MMSoundDeviceList_t device_list)
 }
 
 EXPORT_API
+int mm_sound_free_device(MMSoundDevice_t device_h)
+{
+       if (device_h == NULL)
+               return MM_ERROR_INVALID_ARGUMENT;
+
+       g_free(device_h);
+
+       return MM_ERROR_NONE;
+}
+
+EXPORT_API
+int mm_sound_get_device_by_id(int device_id, MMSoundDevice_t *device_h)
+{
+       int ret = MM_ERROR_NONE;
+       mm_sound_device_t *device = NULL;
+
+       if (device_id < 1 || device_h == NULL)
+               return MM_ERROR_INVALID_ARGUMENT;
+
+       ret = mm_sound_client_get_device_by_id(device_id, &device);
+       if (ret < 0)
+               debug_error("Could not get device by id, ret = %x\n", ret);
+       else
+               *device_h = device;
+
+       return ret;
+}
+
+EXPORT_API
 int mm_sound_get_next_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
 {
        int ret = MM_ERROR_NONE;
@@ -442,5 +471,3 @@ int mm_sound_is_stream_on_device(int stream_id, MMSoundDevice_t device_h, bool *
 
        return ret;
 }
-
-