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>
36 #ifndef NOT_MULTI_THREAD_SAFE
40 #include "tzplatform_variables.h"
41 #include "tzplatform_config.h"
49 #include "shared-api.h"
52 /* the global context */
53 static struct tzplatform_context global_context = {
54 #ifndef NOT_MULTI_THREAD_SAFE
55 .mutex = PTHREAD_MUTEX_INITIALIZER,
58 .user = _USER_NOT_SET_
62 * Tizen 4.0: signup validation is deprecated
63 * version matching will be conducted by numbering of so version
66 /* locks the context */
67 inline static void lock(struct tzplatform_context *context)
69 #ifndef NOT_MULTI_THREAD_SAFE
70 pthread_mutex_lock( &context->mutex);
74 /* unlock the context */
75 inline static void unlock(struct tzplatform_context *context)
77 #ifndef NOT_MULTI_THREAD_SAFE
78 pthread_mutex_unlock( &context->mutex);
82 static inline const char *get_lock(int id, struct tzplatform_context *context)
86 if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
89 if (context->state == RESET)
92 return context->state == ERROR ? NULL : context->values[id];
95 /*************** PUBLIC API begins here **************/
97 int tzplatform_context_create(struct tzplatform_context **result)
99 struct tzplatform_context *context;
101 context = malloc( sizeof * context);
106 context->state = RESET;
107 context->user = _USER_NOT_SET_;
108 #ifndef NOT_MULTI_THREAD_SAFE
109 pthread_mutex_init( &context->mutex, NULL);
114 void tzplatform_context_destroy(struct tzplatform_context *context)
116 if (context->state == VALID)
117 heap_destroy( &context->heap);
118 context->state = ERROR;
122 void tzplatform_reset()
124 tzplatform_context_reset(&global_context);
127 void tzplatform_context_reset(struct tzplatform_context *context)
130 if (context->state != RESET) {
131 if (context->state == VALID)
132 heap_destroy( &context->heap);
133 context->state = RESET;
138 uid_t tzplatform_get_user(void)
140 return tzplatform_context_get_user( &global_context);
143 uid_t tzplatform_context_get_user(struct tzplatform_context *context)
148 result = get_uid( context);
154 void tzplatform_reset_user()
156 tzplatform_context_reset_user( &global_context);
159 void tzplatform_context_reset_user(struct tzplatform_context *context)
161 tzplatform_context_set_user( context, _USER_NOT_SET_);
164 int tzplatform_set_user(uid_t uid)
166 return tzplatform_context_set_user( &global_context, uid);
169 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
172 if (context->user != uid) {
173 if (uid != _USER_NOT_SET_ && !pw_has_uid( uid)) {
178 if (context->state == VALID)
179 heap_destroy( &context->heap);
180 context->state = RESET;
189 /*************** PUBLIC INTERNAL API begins here **************/
191 const char* _getname_tzplatform_(int id)
193 return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
196 int _getid_tzplatform_(const char *name)
198 return hashid(name, strlen(name));
201 const char* _getenv_tzplatform_(int id)
203 return _context_getenv_tzplatform_(id, &global_context);
206 const char* _context_getenv_tzplatform_(int id, struct tzplatform_context *context)
208 const char *array[2];
211 result = get_lock(id, context);
212 if (result != NULL) {
215 result = scratchcat( 0, array);
221 int _getenv_int_tzplatform_(int id)
223 return _context_getenv_int_tzplatform_(id, &global_context);
226 int _context_getenv_int_tzplatform_(int id, struct tzplatform_context *context)
231 value = get_lock(id, context);
232 result = value==NULL ? -1 : atoi(value);
237 const char* _mkstr_tzplatform_(int id, const char * str)
239 return _context_mkstr_tzplatform_(id, str, &global_context);
242 const char* _context_mkstr_tzplatform_(int id, const char *str, struct tzplatform_context *context)
244 const char *array[3];
247 result = get_lock(id, context);
248 if (result != NULL) {
252 result = scratchcat( 0, array);
258 const char* _mkpath_tzplatform_(int id, const char * path)
260 return _context_mkpath_tzplatform_(id, path, &global_context);
263 const char* _context_mkpath_tzplatform_(int id, const char *path, struct tzplatform_context *context)
265 const char *array[3];
268 result = get_lock(id, context);
269 if (result != NULL) {
273 result = scratchcat( 1, array);
279 const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2)
281 return _context_mkpath3_tzplatform_( id, path, path2, &global_context);
284 const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, struct tzplatform_context *context)
286 const char *array[4];
289 result = get_lock(id, context);
290 if (result != NULL) {
295 result = scratchcat( 1, array);
301 const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3)
303 return _context_mkpath4_tzplatform_( id, path, path2, path3, &global_context);
306 const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, struct tzplatform_context *context)
308 const char *array[5];
311 result = get_lock(id, context);
312 if (result != NULL) {
318 result = scratchcat( 1, array);
324 uid_t _getuid_tzplatform_(int id)
326 return _context_getuid_tzplatform_( id, &global_context);
329 uid_t _context_getuid_tzplatform_(int id, struct tzplatform_context *context)
335 value = get_lock(id, context);
337 pw_get_uid( value, &result);
343 gid_t _getgid_tzplatform_(int id)
345 return _context_getgid_tzplatform_( id, &global_context);
348 gid_t _context_getgid_tzplatform_(int id, struct tzplatform_context *context)
354 value = get_lock(id, context);
356 pw_get_gid( value, &result);