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_
61 /* the signup of names */
64 /* validate the signup */
65 static void validate_signup(char signup[33])
67 if (memcmp(signup+1, tizen_platform_config_signup+1, 32)) {
68 syslog(LOG_CRIT, "Bad signup of the client of tizen-platform-config");
74 /* check the signup */
75 static inline void check_signup(char signup[33])
78 validate_signup(signup);
81 /* locks the context */
82 inline static void lock(struct tzplatform_context *context)
84 #ifndef NOT_MULTI_THREAD_SAFE
85 pthread_mutex_lock( &context->mutex);
89 /* unlock the context */
90 inline static void unlock(struct tzplatform_context *context)
92 #ifndef NOT_MULTI_THREAD_SAFE
93 pthread_mutex_unlock( &context->mutex);
97 static inline const char *get_lock(int id, struct tzplatform_context *context)
101 if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
104 if (context->state == RESET)
105 initialize( context);
107 return context->state == ERROR ? NULL : context->values[id];
110 /*************** PUBLIC API begins here **************/
112 int tzplatform_context_create(struct tzplatform_context **result)
114 struct tzplatform_context *context;
116 context = malloc( sizeof * context);
121 context->state = RESET;
122 context->user = _USER_NOT_SET_;
123 #ifndef NOT_MULTI_THREAD_SAFE
124 pthread_mutex_init( &context->mutex, NULL);
129 void tzplatform_context_destroy(struct tzplatform_context *context)
131 if (context->state == VALID)
132 heap_destroy( &context->heap);
133 context->state = ERROR;
137 void tzplatform_reset()
139 tzplatform_context_reset(&global_context);
142 void tzplatform_context_reset(struct tzplatform_context *context)
145 if (context->state != RESET) {
146 if (context->state == VALID)
147 heap_destroy( &context->heap);
148 context->state = RESET;
153 uid_t tzplatform_get_user(char signup[33])
155 return tzplatform_context_get_user( &global_context);
158 uid_t tzplatform_context_get_user(struct tzplatform_context *context)
163 result = get_uid( context);
169 void tzplatform_reset_user()
171 tzplatform_context_reset_user( &global_context);
174 void tzplatform_context_reset_user(struct tzplatform_context *context)
176 tzplatform_context_set_user( context, _USER_NOT_SET_);
179 int tzplatform_set_user(uid_t uid)
181 return tzplatform_context_set_user( &global_context, uid);
184 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
187 if (context->user != uid) {
188 if (uid != _USER_NOT_SET_ && !pw_has_uid( uid)) {
193 if (context->state == VALID)
194 heap_destroy( &context->heap);
195 context->state = RESET;
204 /*************** PUBLIC INTERNAL API begins here **************/
206 const char* _getname_tzplatform_(int id, char signup[33])
208 check_signup(signup);
209 return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
212 int _getid_tzplatform_(const char *name, char signup[33])
214 check_signup(signup);
215 return hashid(name, strlen(name));
218 const char* _getenv_tzplatform_(int id, char signup[33])
220 return _context_getenv_tzplatform_(id, signup, &global_context);
223 const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
225 const char *array[2];
228 check_signup(signup);
229 result = get_lock(id, context);
230 if (result != NULL) {
233 result = scratchcat( 0, array);
239 int _getenv_int_tzplatform_(int id, char signup[33])
241 return _context_getenv_int_tzplatform_(id, signup, &global_context);
244 int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
249 check_signup(signup);
250 value = get_lock(id, context);
251 result = value==NULL ? -1 : atoi(value);
256 const char* _mkstr_tzplatform_(int id, const char * str, char signup[33])
258 return _context_mkstr_tzplatform_(id, str, signup, &global_context);
261 const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context)
263 const char *array[3];
266 check_signup(signup);
267 result = get_lock(id, context);
268 if (result != NULL) {
272 result = scratchcat( 0, array);
278 const char* _mkpath_tzplatform_(int id, const char * path, char signup[33])
280 return _context_mkpath_tzplatform_(id, path, signup, &global_context);
283 const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context)
285 const char *array[3];
288 check_signup(signup);
289 result = get_lock(id, context);
290 if (result != NULL) {
294 result = scratchcat( 1, array);
300 const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33])
302 return _context_mkpath3_tzplatform_( id, path, path2, signup, &global_context);
305 const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context)
307 const char *array[4];
310 check_signup(signup);
311 result = get_lock(id, context);
312 if (result != NULL) {
317 result = scratchcat( 1, array);
323 const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33])
325 return _context_mkpath4_tzplatform_( id, path, path2, path3, signup, &global_context);
328 const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context)
330 const char *array[5];
333 check_signup(signup);
334 result = get_lock(id, context);
335 if (result != NULL) {
341 result = scratchcat( 1, array);
347 uid_t _getuid_tzplatform_(int id, char signup[33])
349 return _context_getuid_tzplatform_( id, signup, &global_context);
352 uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
357 check_signup(signup);
359 value = get_lock(id, context);
361 pw_get_uid( value, &result);
367 gid_t _getgid_tzplatform_(int id, char signup[33])
369 return _context_getgid_tzplatform_( id, signup, &global_context);
372 gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
377 check_signup(signup);
379 value = get_lock(id, context);
381 pw_get_gid( value, &result);