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"
48 #include "shared-api.h"
50 /* the global context */
51 static struct tzplatform_context global_context = {
52 #ifndef NOT_MULTI_THREAD_SAFE
53 .mutex = PTHREAD_MUTEX_INITIALIZER,
56 .user = _USER_NOT_SET_
59 /* the signup of names */
62 /* validate the signup */
63 static void validate_signup(char signup[33])
65 if (memcmp(signup+1, tizen_platform_config_signup+1, 32)) {
66 syslog(LOG_CRIT, "Bad signup of the client of tizen-platform-config");
72 /* check the signup */
73 static inline void check_signup(char signup[33])
76 validate_signup(signup);
79 /* locks the context */
80 static inline void lock(struct tzplatform_context *context)
82 #ifndef NOT_MULTI_THREAD_SAFE
83 pthread_mutex_lock(&context->mutex);
87 /* unlock the context */
88 static inline void unlock(struct tzplatform_context *context)
90 #ifndef NOT_MULTI_THREAD_SAFE
91 pthread_mutex_unlock(&context->mutex);
95 static inline const char *get_lock(int id, struct tzplatform_context *context)
99 if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
102 if (context->state == RESET)
105 return context->state == ERROR ? NULL : context->values[id];
108 /*************** PUBLIC API begins here **************/
110 int tzplatform_context_create(struct tzplatform_context **result)
112 struct tzplatform_context *context;
114 context = malloc(sizeof * context);
119 context->state = RESET;
120 context->user = _USER_NOT_SET_;
121 #ifndef NOT_MULTI_THREAD_SAFE
122 pthread_mutex_init(&context->mutex, NULL);
127 void tzplatform_context_destroy(struct tzplatform_context *context)
129 if (context->state == VALID)
130 heap_destroy(&context->heap);
131 context->state = ERROR;
135 void tzplatform_reset()
137 tzplatform_context_reset(&global_context);
140 void tzplatform_context_reset(struct tzplatform_context *context)
143 if (context->state != RESET) {
144 if (context->state == VALID)
145 heap_destroy(&context->heap);
146 context->state = RESET;
151 uid_t tzplatform_get_user(char signup[33])
153 return tzplatform_context_get_user(&global_context);
156 uid_t tzplatform_context_get_user(struct tzplatform_context *context)
161 result = get_uid(context);
167 void tzplatform_reset_user()
169 tzplatform_context_reset_user(&global_context);
172 void tzplatform_context_reset_user(struct tzplatform_context *context)
174 tzplatform_context_set_user(context, _USER_NOT_SET_);
177 int tzplatform_set_user(uid_t uid)
179 return tzplatform_context_set_user(&global_context, uid);
182 int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
185 if (context->user != uid) {
186 if (uid != _USER_NOT_SET_ && !pw_has_uid(context, uid)) {
191 if (context->state == VALID)
192 heap_destroy(&context->heap);
193 context->state = RESET;
202 /*************** PUBLIC INTERNAL API begins here **************/
204 const char* _getname_tzplatform_(int id, char signup[33])
206 check_signup(signup);
207 return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
210 int _getid_tzplatform_(const char *name, char signup[33])
212 check_signup(signup);
213 return hashid(name, strlen(name));
216 const char* _getenv_tzplatform_(int id, char signup[33])
218 return _context_getenv_tzplatform_(id, signup, &global_context);
221 const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
223 const char *array[2];
226 check_signup(signup);
227 result = get_lock(id, context);
228 if (result != NULL) {
231 result = scratchcat(0, array);
237 int _getenv_int_tzplatform_(int id, char signup[33])
239 return _context_getenv_int_tzplatform_(id, signup, &global_context);
242 int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
247 check_signup(signup);
248 value = get_lock(id, context);
249 result = (value == NULL) ? -1 : atoi(value);
254 const char* _mkstr_tzplatform_(int id, const char * str, char signup[33])
256 return _context_mkstr_tzplatform_(id, str, signup, &global_context);
259 const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context)
261 const char *array[3];
264 check_signup(signup);
265 result = get_lock(id, context);
266 if (result != NULL) {
270 result = scratchcat(0, array);
276 const char* _mkpath_tzplatform_(int id, const char * path, char signup[33])
278 return _context_mkpath_tzplatform_(id, path, signup, &global_context);
281 const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context)
283 const char *array[3];
286 check_signup(signup);
287 result = get_lock(id, context);
288 if (result != NULL) {
292 result = scratchcat(1, array);
298 const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33])
300 return _context_mkpath3_tzplatform_(id, path, path2, signup, &global_context);
303 const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context)
305 const char *array[4];
308 check_signup(signup);
309 result = get_lock(id, context);
310 if (result != NULL) {
315 result = scratchcat(1, array);
321 const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33])
323 return _context_mkpath4_tzplatform_(id, path, path2, path3, signup, &global_context);
326 const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context)
328 const char *array[5];
331 check_signup(signup);
332 result = get_lock(id, context);
333 if (result != NULL) {
339 result = scratchcat(1, array);
345 uid_t _getuid_tzplatform_(int id, char signup[33])
347 return _context_getuid_tzplatform_(id, signup, &global_context);
350 uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
355 check_signup(signup);
357 value = get_lock(id, context);
359 pw_get_uid(context, value, &result);
365 gid_t _getgid_tzplatform_(int id, char signup[33])
367 return _context_getgid_tzplatform_(id, signup, &global_context);
370 gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
375 check_signup(signup);
377 value = get_lock(id, context);
379 pw_get_gid(context, value, &result);