Add UID-based APIs:
[platform/core/system/tizen-platform-config.git] / src / global-api.c
index dd8ab83..d122b5b 100644 (file)
 #endif
 
 #include <unistd.h>
+#include <assert.h>
+#include <pthread.h>
 #include "tzplatform_variables.h"
 #include "tzplatform_config.h"
 
 #include "shared-api.h"
 #include "isadmin.h"
-
 #include "signup.inc"
 
+#define TZ_UID_START   5000
+#define TZ_UID_MAX     128
+#define uid2idx(x)     ((x > TZ_UID_START) ? (x - TZ_UID_START) : x)
+static struct tzplatform_context *_context[TZ_UID_MAX] = {0};
+
+int init_internal_context(uid_t uid)
+{
+       int ret;
+       int idx;
+       static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+       if (uid <= TZ_UID_START)
+               assert(uid == 0);
+
+       idx = uid2idx(uid);
+       assert(idx < TZ_UID_MAX);
+
+       if (_context[idx]) return 0;
+
+       pthread_mutex_lock(&mutex);
+
+       ret = tzplatform_context_create(&_context[idx]);
+       if (ret < 0) {
+               pthread_mutex_unlock(&mutex);
+               return ret;
+       }
+
+       ret = tzplatform_context_set_user(_context[idx], uid);
+       if (ret < 0) {
+               tzplatform_context_destroy(_context[idx]);
+               _context[idx] = NULL;
+
+               pthread_mutex_unlock(&mutex);
+               return ret;
+       }
+
+       pthread_mutex_unlock(&mutex);
+
+       return 0;
+}
+
 int tzplatform_getcount()
 {
        return _TZPLATFORM_VARIABLES_COUNT_;
@@ -56,6 +98,15 @@ const char* tzplatform_getenv(enum tzplatform_variable id)
        return _getenv_tzplatform_(id, tizen_platform_config_signup);
 }
 
+const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id)
+{
+       int ret = init_internal_context(uid);
+       if (ret < 0)
+               return NULL;
+
+       return _context_getenv_tzplatform_(id, tizen_platform_config_signup, _context[uid2idx(uid)]);
+}
+
 const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
 {
        return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
@@ -71,7 +122,7 @@ int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzpla
        return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
 }
 
-const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
+const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str)
 {
        return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
 }
@@ -81,11 +132,20 @@ const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tz
        return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
 }
 
-const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
+const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path)
 {
        return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
 }
 
+const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path)
+{
+       int ret = init_internal_context(uid);
+       if (ret < 0)
+               return NULL;
+
+       return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, _context[uid2idx(uid)]);
+}
+
 const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
 {
        return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
@@ -96,6 +156,14 @@ const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, c
        return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
 }
 
+const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2)
+{
+       int ret = init_internal_context(uid);
+       if (ret < 0)
+               return NULL;
+
+       return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, _context[uid2idx(uid)]);
+}
 const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
 {
        return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
@@ -106,6 +174,15 @@ const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, c
        return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
 }
 
+const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
+{
+       int ret = init_internal_context(uid);
+       if (ret < 0)
+               return NULL;
+
+       return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, _context[uid2idx(uid)]);
+}
+
 const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
 {
        return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
@@ -187,5 +264,3 @@ int main()
        return 0;
 }
 #endif
-
-