2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
33 #include "tzplatform_variables.h"
34 #include "tzplatform_config.h"
36 #include "shared-api.h"
40 #define TZ_UID_START 5000
41 #define TZ_UID_MAX 128
42 #define uid2idx(x) ((x > TZ_UID_START) ? (x - TZ_UID_START) : x)
43 static struct tzplatform_context *_context[TZ_UID_MAX] = {0};
45 int init_internal_context(uid_t uid)
49 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
51 if (uid <= TZ_UID_START)
55 assert(idx < TZ_UID_MAX);
57 if (_context[idx]) return 0;
59 pthread_mutex_lock(&mutex);
61 ret = tzplatform_context_create(&_context[idx]);
63 pthread_mutex_unlock(&mutex);
67 ret = tzplatform_context_set_user(_context[idx], uid);
69 tzplatform_context_destroy(_context[idx]);
72 pthread_mutex_unlock(&mutex);
76 pthread_mutex_unlock(&mutex);
81 int tzplatform_getcount()
83 return _TZPLATFORM_VARIABLES_COUNT_;
86 const char* tzplatform_getname(enum tzplatform_variable id)
88 return _getname_tzplatform_(id, tizen_platform_config_signup);
91 enum tzplatform_variable tzplatform_getid(const char *name)
93 return _getid_tzplatform_(name, tizen_platform_config_signup);
96 const char* tzplatform_getenv(enum tzplatform_variable id)
98 return _getenv_tzplatform_(id, tizen_platform_config_signup);
101 const char* tzplatform_uid_getenv(uid_t uid, enum tzplatform_variable id)
103 int ret = init_internal_context(uid);
107 return _context_getenv_tzplatform_(id, tizen_platform_config_signup, _context[uid2idx(uid)]);
110 const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
112 return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
115 int tzplatform_getenv_int(enum tzplatform_variable id)
117 return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
120 int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
122 return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
125 const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str)
127 return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
130 const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
132 return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
135 const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path)
137 return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
140 const char* tzplatform_uid_mkpath(uid_t uid, enum tzplatform_variable id, const char *path)
142 int ret = init_internal_context(uid);
146 return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, _context[uid2idx(uid)]);
149 const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
151 return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
154 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
156 return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
159 const char* tzplatform_uid_mkpath3(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2)
161 int ret = init_internal_context(uid);
165 return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, _context[uid2idx(uid)]);
167 const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
169 return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
172 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
174 return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
177 const char* tzplatform_uid_mkpath4(uid_t uid, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
179 int ret = init_internal_context(uid);
183 return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, _context[uid2idx(uid)]);
186 const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
188 return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
191 uid_t tzplatform_getuid(enum tzplatform_variable id)
193 return _getuid_tzplatform_(id, tizen_platform_config_signup);
196 uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
198 return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
201 gid_t tzplatform_getgid(enum tzplatform_variable id)
203 return _getgid_tzplatform_(id, tizen_platform_config_signup);
206 gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
208 return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
211 int tzplatform_has_system_group(uid_t uid)
213 return _has_system_group_static_(uid);
223 struct tzplatform_context *context;
224 enum tzplatform_variable id;
231 while (i != tzplatform_getcount()) {
232 id = (enum tzplatform_variable)i;
233 name = tzplatform_getname(id);
234 value = tzplatform_getenv(id);
235 xid = (int)tzplatform_getid(name);
236 printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
240 printf("------------------------\n");
241 i = tzplatform_context_create(&context);
243 printf("error while creating context %d\n", i);
248 i = tzplatform_context_set_user(context, uid);
250 printf("error %d while switching to user %d\n", i, (int)uid);
254 while (i != tzplatform_getcount()) {
255 id = (enum tzplatform_variable)i;
256 name = tzplatform_getname(id);
257 value = tzplatform_context_getenv(context, id);
258 xid = (int)tzplatform_getid(name);
259 printf("%d=%d\t%s=%s\n", i, xid, name, value ? value : "<null>");
262 tzplatform_context_destroy(context);