Revise dbus error handling codes to be more defensive 03/147303/2 accepted/tizen/4.0/unified/20170906.144000 submit/tizen_4.0/20170906.054904
authorSangchul Lee <sc11.lee@samsung.com>
Mon, 4 Sep 2017 01:41:15 +0000 (10:41 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Mon, 4 Sep 2017 03:01:18 +0000 (12:01 +0900)
[Version] 0.4.22
[Issue Type] Enhancement

Change-Id: I08d99c364881d9252c12c7f607a9be07e8d1a780
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
packaging/capi-media-sound-manager.spec
src/sound_manager_private.c

index aa97b0c8a83e86411c3321e6a112c6d041bf0951..b623556b3a225e6c1eabc78a28e77ae5263d2ada 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-sound-manager
 Summary:    Sound Manager library
-Version:    0.4.21
+Version:    0.4.22
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 9d483197ebb9a8531c9add28f17483572a273ab1..12ed9f66c62dcf828fa7ae179a71b7a5a8dca7f5 100644 (file)
@@ -329,6 +329,23 @@ static int _convert_stream_type_to_interrupt_reason(const char *stream_type, sou
        return MM_ERROR_NONE;
 }
 
+static int __get_dbus_connection(GDBusConnection **conn)
+{
+       GError *err = NULL;
+
+       *conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+       if (!(*conn) || err) {
+               if (err)
+                       LOGE("g_bus_get_sync() error (%s)", err->message);
+               else
+                       LOGE("g_bus_get_sync() error");
+               g_error_free(err);
+               return MM_ERROR_SOUND_INTERNAL;
+       }
+
+       return MM_ERROR_NONE;
+}
+
 int _convert_sound_type(sound_type_e sound_type, const char **volume_type)
 {
        SM_NULL_ARG_CHECK_FOR_PRIV(volume_type);
@@ -708,12 +725,9 @@ int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info)
        assert(stream_type);
        assert(info);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn && err) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
+
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
                                                        PA_STREAM_MANAGER_OBJECT_PATH,
@@ -725,8 +739,8 @@ int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info)
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result && err) {
-               LOGE("g_dbus_connection_call_sync() for GET_STREAM_INFO error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for GET_STREAM_INFO error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -818,7 +832,6 @@ int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info)
        }
        g_variant_unref(item);
        g_variant_unref(child);
-       g_variant_unref(result);
 
        if (info->priority == -1) {
                LOGE("could not find the info of stream type(%s)", stream_type);
@@ -826,6 +839,7 @@ int _get_stream_conf_info(const char *stream_type, stream_conf_info_s *info)
        }
 
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -844,12 +858,8 @@ int _set_manual_route_info(unsigned int index, manual_route_info_s *info)
 
        assert(info);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        LOGI("stream info index(%u)", index);
 
@@ -885,8 +895,8 @@ int _set_manual_route_info(unsigned int index, manual_route_info_s *info)
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for SET_STREAM_ROUTE_DEVICES error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for SET_STREAM_ROUTE_DEVICES error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -899,8 +909,8 @@ int _set_manual_route_info(unsigned int index, manual_route_info_s *info)
        else
                info->is_set = true;
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        if (builder_for_in_devices)
                g_variant_builder_unref(builder_for_in_devices);
        if (builder_for_out_devices)
@@ -920,12 +930,8 @@ int _set_route_option(unsigned int index, const char *name, int value)
 
        assert(name);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn && err) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        LOGI("[OPTION] %s(%d)", name, value);
 
@@ -940,8 +946,8 @@ int _set_route_option(unsigned int index, const char *name, int value)
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result && err) {
-               LOGE("g_dbus_connection_call_sync() for SET_STREAM_ROUTE_OPTION error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for SET_STREAM_ROUTE_OPTION error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -954,8 +960,8 @@ int _set_route_option(unsigned int index, const char *name, int value)
        else if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
                ret = MM_ERROR_SOUND_INTERNAL;
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -973,12 +979,8 @@ int _get_volume_max_level(const char *direction, const char *volume_type, unsign
        assert(volume_type);
        assert(max_level);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -991,8 +993,8 @@ int _get_volume_max_level(const char *direction, const char *volume_type, unsign
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for GET_VOLUME_MAX_LEVEL error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for GET_VOLUME_MAX_LEVEL error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1003,8 +1005,8 @@ int _get_volume_max_level(const char *direction, const char *volume_type, unsign
        if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
                ret = MM_ERROR_SOUND_INTERNAL;
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -1022,12 +1024,8 @@ int _get_volume_level(const char *direction, const char *volume_type, unsigned i
        assert(volume_type);
        assert(level);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1040,8 +1038,8 @@ int _get_volume_level(const char *direction, const char *volume_type, unsigned i
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for GET_VOLUME_LEVEL error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for GET_VOLUME_LEVEL error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1052,8 +1050,8 @@ int _get_volume_level(const char *direction, const char *volume_type, unsigned i
        if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
                ret = MM_ERROR_SOUND_INTERNAL;
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -1072,12 +1070,8 @@ int _set_volume_level(const char *direction, const char *volume_type, unsigned i
        assert(direction);
        assert(volume_type);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1090,8 +1084,8 @@ int _set_volume_level(const char *direction, const char *volume_type, unsigned i
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for SET_VOLUME_LEVEL error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for SET_VOLUME_LEVEL error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1107,8 +1101,9 @@ int _set_volume_level(const char *direction, const char *volume_type, unsigned i
                if ((vret = vconf_set_int(volume_path, (int)level)))
                        LOGE("vconf_set_int(%s) failed..ret[%d]\n", volume_path, vret);
        }
-       g_variant_unref(result);
+
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -1126,12 +1121,8 @@ int _get_current_volume_type(const char *direction, char **volume_type)
        assert(direction);
        assert(volume_type);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1144,8 +1135,8 @@ int _get_current_volume_type(const char *direction, char **volume_type)
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for GET_CURRENT_VOLUME_TYPE error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for GET_CURRENT_VOLUME_TYPE error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1156,13 +1147,14 @@ int _get_current_volume_type(const char *direction, char **volume_type)
        if (!strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret))) {
                ret = MM_ERROR_NONE;
                *volume_type = strdup(dbus_volume_type);
-       } else if (!strncmp("STREAM_MANAGER_RETURN_ERROR_NO_STREAM", dbus_ret, strlen(dbus_ret)))
+       } else if (!strncmp("STREAM_MANAGER_RETURN_ERROR_NO_STREAM", dbus_ret, strlen(dbus_ret))) {
                ret = MM_ERROR_SOUND_VOLUME_NO_INSTANCE;
-       else
+       } else {
                ret = MM_ERROR_SOUND_INTERNAL;
+       }
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -1180,12 +1172,8 @@ int _get_current_media_routing_path(const char *direction, sound_device_type_e *
        assert(direction);
        assert(device_type);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1198,8 +1186,8 @@ int _get_current_media_routing_path(const char *direction, sound_device_type_e *
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for GET_CURRENT_MEDIA_ROUTING_PATH error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for GET_CURRENT_MEDIA_ROUTING_PATH error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1218,8 +1206,8 @@ int _get_current_media_routing_path(const char *direction, sound_device_type_e *
                        ret = MM_ERROR_SOUND_INTERNAL;
        }
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;
@@ -1232,12 +1220,8 @@ void _update_focus_status(unsigned int index, unsigned int acquired_focus_status
        GError *err = NULL;
        const gchar *dbus_ret = NULL;
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
+       if (__get_dbus_connection(&conn))
                return;
-       }
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1250,8 +1234,8 @@ void _update_focus_status(unsigned int index, unsigned int acquired_focus_status
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for UPDATE_FOCUS_STATUS error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for UPDATE_FOCUS_STATUS error (%s)", err ? err->message : NULL);
                g_error_free(err);
                goto LEAVE;
        }
@@ -1260,8 +1244,8 @@ void _update_focus_status(unsigned int index, unsigned int acquired_focus_status
        if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
                LOGE("failed to UPDATE_FOCUS_STATUS error (%s)", dbus_ret);
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return;
@@ -1278,12 +1262,8 @@ static int _check_media_stream_exist_by_pid(unsigned int pid, const char *direct
        assert(exist);
        assert(direction);
 
-       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
-       if (!conn) {
-               LOGE("g_bus_get_sync() error (%s)", err->message);
-               g_error_free(err);
-               return MM_ERROR_SOUND_INTERNAL;
-       }
+       if ((ret = __get_dbus_connection(&conn)))
+               return ret;
 
        result = g_dbus_connection_call_sync(conn,
                                                        PA_BUS_NAME,
@@ -1296,8 +1276,8 @@ static int _check_media_stream_exist_by_pid(unsigned int pid, const char *direct
                                                        2000,
                                                        NULL,
                                                        &err);
-       if (!result) {
-               LOGE("g_dbus_connection_call_sync() for CHECK_STREAM_EXIST_BY_PID error (%s)", err->message);
+       if (!result || err) {
+               LOGE("g_dbus_connection_call_sync() for CHECK_STREAM_EXIST_BY_PID error (%s)", err ? err->message : NULL);
                g_error_free(err);
                ret = MM_ERROR_SOUND_INTERNAL;
                goto LEAVE;
@@ -1312,8 +1292,8 @@ static int _check_media_stream_exist_by_pid(unsigned int pid, const char *direct
        else
                ret = MM_ERROR_SOUND_INTERNAL;
 
-       g_variant_unref(result);
 LEAVE:
+       g_variant_unref(result);
        g_object_unref(conn);
 
        return ret;