modified to init global instance once 04/110804/4
authorsanghyeok.oh <sanghyeok.oh@samsung.com>
Wed, 18 Jan 2017 06:03:44 +0000 (15:03 +0900)
committersanghyeok.oh <sanghyeok.oh@samsung.com>
Thu, 19 Jan 2017 05:00:30 +0000 (14:00 +0900)
Change-Id: Ib5092c6bee952b7a51f7da90d9d65414a0f10697
Signed-off-by: sanghyeok.oh <sanghyeok.oh@samsung.com>
src/libdbuspolicy1.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index b37b1ac..b2a3248
@@ -69,8 +69,10 @@ struct kconn {
        int fd;
        uint64_t id;
        char *pool;
-       int counter;
-} g_conn[2];
+} g_conn[2] = {
+       {.pool = MAP_FAILED},
+       {.pool = MAP_FAILED}
+};
 
 struct udesc {
        dbus_uid_t uid;
@@ -180,13 +182,13 @@ static int bus_path_resolve(const char *bus_path, char *resolved_path, unsigned
 }
 
 static bool init_once_done = false;
+static bool init_once[2] = {false, false};
 
 DBUSPOLICY1_EXPORT void* dbuspolicy1_init(const char *bus_path)
 {
        unsigned int bus_type = -1;
        char resolved_path[PATH_MAX] = { 0 };
        int rp, rs;
-       bool rb;
 
        _Static_assert(SYSTEM_BUS == 0, "SYSTEM_BUS not 0");
        _Static_assert(SESSION_BUS == 1, "SESSION_BUS not 0");
@@ -197,15 +199,12 @@ DBUSPOLICY1_EXPORT void* dbuspolicy1_init(const char *bus_path)
        if (bus_type)
                bus_type = SESSION_BUS;
 
-       rb = false;
        pthread_mutex_lock(&g_mutex);
        if (!init_once_done) {
                init_once_done = true;
-               rb = dbuspolicy_init_once();
+               dbuspolicy_init_once();
        }
-       if (rb)
-               goto err_close;
-       if (g_conn[bus_type].counter == 0) {
+       if (!init_once[bus_type]) {
                if ((g_conn[bus_type].fd = kdbus_open_bus(resolved_path)) < 0)
                        goto err;
 
@@ -220,15 +219,18 @@ DBUSPOLICY1_EXPORT void* dbuspolicy1_init(const char *bus_path)
 
                if (rp < 0 && rs < 0) /* when both negative */
                        goto err_close;
+
+               init_once[bus_type] = true;
        }
 
-       g_conn[bus_type].counter++;
        pthread_mutex_unlock(&g_mutex);
        __internal_init_flush_logs();
 
        return &g_conn[bus_type];
 
 err_close:
+       if (g_conn[bus_type].pool != MAP_FAILED)
+               munmap(g_conn[bus_type].pool, KDBUS_POOL_SIZE);
        close(g_conn[bus_type].fd);
 err:
        pthread_mutex_unlock(&g_mutex);
@@ -237,18 +239,7 @@ err:
 
 DBUSPOLICY1_EXPORT void dbuspolicy1_free(void* configuration)
 {
-    if (configuration) {
-               struct kconn* k = (struct kconn*)configuration;
-               pthread_mutex_lock(&g_mutex);
-
-               --k->counter;
-               if (k->counter <= 0) {
-                       munmap(k->pool, KDBUS_POOL_SIZE);
-                       close(k->fd);
-               }
-
-               pthread_mutex_unlock(&g_mutex);
-       }
+       configuration = configuration;
 }
 
 #ifdef LIBDBUSPOLICY_TESTS_API