Add UID-based APIs: 82/160982/6
authorHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 21 Nov 2017 04:14:48 +0000 (13:14 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 22 Nov 2017 02:25:12 +0000 (11:25 +0900)
const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id);
const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path);
const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2);
const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2, const char *path3);

Change-Id: I5f81ce6ed46bb97021b174486bef6c236edb61b2
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/global-api.c
src/tzplatform_config.h

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
-
-
index add6c12..96cb1d8 100644 (file)
@@ -31,7 +31,9 @@ extern "C" {
 #include <sys/types.h>
 #include <tzplatform_variables.h>
 
-/*------------------------------ COMMON API (no context) ---------------*/
+//=================================================================================
+//======================== COMMON APIs (No context) ===============================
+//=================================================================================
 
 /*
  Return the count of variables.
@@ -54,7 +56,9 @@ const char* tzplatform_getname(enum tzplatform_variable id);
 extern
 enum tzplatform_variable tzplatform_getid(const char *name);
 
-/*------------------------------ GLOBAL API (default global context) ----*/
+//=================================================================================
+//====================== GLOBAL APIs (default global context) =====================
+//=================================================================================
 
 /*
  Enforces the removal of the previously evaluated tizen platform variables.
@@ -159,7 +163,7 @@ const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path);
 */
 extern
 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char *path,
-                                                                                                                       const char *path2);
+                               const char *path2);
 
 /*
  Return the string resulting of the path-concatenation of string value of the
@@ -180,7 +184,7 @@ const char* tzplatform_mkpath3(enum tzplatform_variable id, const char *path,
 */
 extern
 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char *path,
-                                                                               const char *path2, const char *path3);
+                               const char *path2, const char *path3);
 
 /*
  Return the uid for a given user name, stored in variable <id>
@@ -210,7 +214,35 @@ uid_t tzplatform_getuid(enum tzplatform_variable id);
 extern
 gid_t tzplatform_getgid(enum tzplatform_variable id);
 
-/*------------------------------ CONTEXTUAL API --------------------------*/
+//=================================================================================
+//============================ UID-based APIs =====================================
+//=================================================================================
+
+/*
+ In the following APIs, a 'uid' parameter is added to the form of GLOBAL APIs.
+
+ How-to-use is the same with GLOBAL APIs, but contexts are internally created and set for each uid.
+
+ uid should be one of root(0), owner(5001), and so on.
+*/
+
+extern
+const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id);
+
+extern
+const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path);
+
+extern
+const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path,
+                               const char *path2);
+
+extern
+const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path,
+                               const char *path2, const char *path3);
+
+//=================================================================================
+//============================ CONTEXTUAL APIs ====================================
+//=================================================================================
 
 struct tzplatform_context;
 
@@ -265,13 +297,15 @@ void tzplatform_context_reset_user(struct tzplatform_context *context);
  Can return NULL in case of internal error or when 'id' isn't defined.
 */
 extern
-const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id);
+const char* tzplatform_context_getenv(struct tzplatform_context *context,
+                                       enum tzplatform_variable id);
 
 /*
  Return the integer value of the tizen plaform variable 'id'.
 */
 extern
-int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id);
+int tzplatform_context_getenv_int(struct tzplatform_context *context,
+                                       enum tzplatform_variable id);
 
 /*
  Return the string resulting of the concatenation of string value of the
@@ -289,7 +323,8 @@ int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzpla
        will return "/opt/home-yes"
 */
 extern
-const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str);
+const char* tzplatform_context_mkstr(struct tzplatform_context *context,
+                                       enum tzplatform_variable id, const char *str);
 
 /*
  Return the string resulting of the path-concatenation of string value of the
@@ -309,7 +344,8 @@ const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tz
        will return "/opt/home/yes"
 */
 extern
-const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path);
+const char* tzplatform_context_mkpath(struct tzplatform_context *context,
+                                       enum tzplatform_variable id, const char *path);
 
 /*
  Return the string resulting of the path-concatenation of string value of the
@@ -329,8 +365,9 @@ const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum t
        will return "/opt/home/yes/no"
 */
 extern
-const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                                                                                                       const char *path2);
+const char* tzplatform_context_mkpath3(struct tzplatform_context *context,
+                                       enum tzplatform_variable id, const char *path,
+                                       const char *path2);
 
 /*
  Return the string resulting of the path-concatenation of string value of the
@@ -350,8 +387,9 @@ const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum
        will return "/opt/home/yes/no/maybe"
 */
 extern
-const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                                                               const char *path2, const char *path3);
+const char* tzplatform_context_mkpath4(struct tzplatform_context *context,
+                                       enum tzplatform_variable id, const char *path,
+                                       const char *path2, const char *path3);
 
 /*
  Return the uid for a given user name, stored in variable <id>