2 * Copyright (C) 2013 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>
35 #include <sys/types.h>
43 #ifndef NOT_MULTI_THREAD_SAFE
45 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
46 static inline void lock() { pthread_mutex_lock(&mutex); }
47 static inline void unlock() { pthread_mutex_unlock(&mutex); }
49 static inline void lock() { }
50 static inline void unlock() { }
54 #define CONFIGPATH "/etc/tizen-platform.conf"
57 #include "tzplatform_variables.h"
58 #include "tzplatform_config.h"
66 #define _HAS_IDS_ ( _FOREIGN_HAS_(UID) \
67 || _FOREIGN_HAS_(EUID) \
68 || _FOREIGN_HAS_(GID) )
70 #define _HAS_PWS_ ( _FOREIGN_HAS_(HOME) \
71 || _FOREIGN_HAS_(USER) \
72 || _FOREIGN_HAS_(EHOME) \
73 || _FOREIGN_HAS_(EUSER) )
75 /* structure for reading config files */
78 size_t dynvars[_FOREIGN_COUNT_];
79 size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
82 /* local and static variables */
83 static const char metafilepath[] = CONFIGPATH;
84 static const char emptystring[] = "";
85 static struct heap heap;
86 static enum { RESET=0, ERROR, VALID } state = RESET;
87 static const char *var_values[_TZPLATFORM_VARIABLES_COUNT_];
88 static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_];
92 /* write the error message */
93 static void writerror( const char *format, ...)
96 fprintf( stderr, "tzplatform_config ERROR: ");
98 vfprintf(stderr, format, ap);
100 fprintf( stderr, "\n");
104 /* fill the foreign variables for ids */
105 static void foreignid( struct reading *reading)
110 #if _FOREIGN_HAS_(UID)
112 if (reading->dynvars[UID] == HNULL) {
113 n = snprintf( buffer, sizeof buffer, "%d", (int)getuid());
114 if (0 < n && n < (int)(sizeof buffer))
115 reading->dynvars[UID] = heap_strndup( &heap, buffer, (size_t)n);
119 #if _FOREIGN_HAS_(EUID)
121 if (reading->dynvars[EUID] == HNULL) {
122 n = snprintf( buffer, sizeof buffer, "%d", (int)geteuid());
123 if (0 < n && n < (int)(sizeof buffer))
124 reading->dynvars[EUID] = heap_strndup( &heap, buffer, (size_t)n);
128 #if _FOREIGN_HAS_(GID)
130 if (reading->dynvars[GID] == HNULL) {
131 n = snprintf( buffer, sizeof buffer, "%d", (int)getgid());
132 if (0 < n && n < (int)(sizeof buffer))
133 reading->dynvars[GID] = heap_strndup( &heap, buffer, (size_t)n);
140 /* fill the foreign variables for home and user */
141 static void foreignpw( struct reading *reading)
144 struct pwget *array[3];
145 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
149 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
154 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
156 #if _FOREIGN_HAS_(HOME)
157 reading->dynvars[HOME] == HNULL
159 #if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
162 #if _FOREIGN_HAS_(USER)
163 reading->dynvars[USER] == HNULL
166 snprintf( suid, sizeof suid, "%u", (unsigned)getuid());
175 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
177 #if _FOREIGN_HAS_(EHOME)
178 reading->dynvars[EHOME] == HNULL
180 #if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
183 #if _FOREIGN_HAS_(EUSER)
184 reading->dynvars[EUSER] == HNULL
187 snprintf( seuid, sizeof seuid, "%u", (unsigned)geteuid());
198 if (pw_get( &heap, array) == 0) {
199 #if _FOREIGN_HAS_(HOME)
201 reading->dynvars[HOME] = uid.home;
203 #if _FOREIGN_HAS_(USER)
205 reading->dynvars[USER] = uid.user;
207 #if _FOREIGN_HAS_(EHOME)
209 reading->dynvars[EHOME] = euid.home;
211 #if _FOREIGN_HAS_(EUSER)
213 reading->dynvars[EUSER] = euid.user;
220 /* get the foreign variable */
221 static const char *foreignvar( struct reading *reading,
222 const char *name, size_t length)
224 enum fkey key = foreign( name, length);
229 #if _FOREIGN_HAS_(HOME)
232 #if _FOREIGN_HAS_(USER)
235 #if _FOREIGN_HAS_(EHOME)
238 #if _FOREIGN_HAS_(EUSER)
246 #if _FOREIGN_HAS_(UID)
249 #if _FOREIGN_HAS_(GID)
252 #if _FOREIGN_HAS_(EUID)
262 offset = reading->dynvars[key];
263 return offset==HNULL ? NULL : heap_address( &heap, offset);
266 /* callback for parsing errors */
267 static int errcb( struct parsing *parsing,
268 size_t position, const char *message)
270 struct parsinfo info;
271 struct reading *reading = parsing->data;
273 /* count the error */
276 /* print the error */
277 parse_utf8_info( parsing, &info, position);
278 writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
280 /* continue to parse */
284 /* callback for solving variables */
285 static const char *getcb( struct parsing *parsing,
286 const char *key, size_t length,
287 size_t begin_pos, size_t end_pos)
289 struct parsinfo info;
291 const struct varassoc *vara;
293 struct reading *reading = parsing->data;
295 /* try to find a tzplatform variable */
296 vara = hashvar( key, length);
298 /* found: try to use it */
299 offset = reading->offsets[(int)vara->id];
301 result = heap_address( &heap, offset);
306 /* that is a foreign variable */
307 result = foreignvar( reading, key, length);
310 /* emit the error and then return */
311 if (result == NULL) {
313 parse_utf8_info( parsing, &info, begin_pos);
314 writerror( "undefined value for %.*s (file %s line %d)",
315 length, key, metafilepath, info.lino);
316 result = emptystring; /* avoid error of the parser */
321 /* callback to define variables */
322 static int putcb( struct parsing *parsing,
323 const char *key, size_t key_length,
324 const char *value, size_t value_length,
325 size_t begin_pos, size_t end_pos)
327 struct parsinfo info;
328 const struct varassoc *vara;
331 struct reading *reading = parsing->data;
333 /* try to find a tzplatform variable */
334 vara = hashvar( key, key_length);
336 /* check that the variable isn't already defined */
337 offset = reading->offsets[(int)vara->id];
338 if (offset != HNULL) {
340 parse_utf8_info( parsing, &info, begin_pos);
341 writerror( "redefinition of variable %.*s (file %s line %d)",
342 key_length, key, metafilepath, info.lino);
345 /* allocate the variable value */
346 offset = heap_alloc( &heap, value_length+1);
347 if (offset == HNULL) {
348 /* error of allocation */
350 writerror( "out of memory");
353 /* record the variable value */
354 reading->offsets[(int)vara->id] = offset;
355 string = heap_address( &heap, offset);
356 memcpy( string, value, value_length);
357 string[value_length] = 0;
361 /* forbidden variable */
362 parse_utf8_info( parsing, &info, begin_pos);
363 writerror( "forbidden variable name %.*s (file %s line %d)",
364 key_length, key, metafilepath, info.lino);
368 /* continue to parse */
372 /* initialize the environment */
373 static void initialize()
375 struct buffer buffer;
376 struct parsing parsing;
377 struct reading reading;
381 /* clear the variables */
382 reading.errcount = 0;
383 for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
384 reading.dynvars[i] = HNULL;
386 for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
387 var_values[i] = NULL;
388 reading.offsets[i] = HNULL;
391 /* read the configuration file */
392 result = buffer_create( &buffer, metafilepath);
394 writerror( "can't read file %s",metafilepath);
399 /* create the heap */
400 result = heap_create( &heap, 1);
402 buffer_destroy( &buffer);
403 writerror( "out of memory");
409 parsing.buffer = buffer.buffer;
410 parsing.length = buffer.length;
411 parsing.maximum_data_size = 0;
412 parsing.should_escape = 0;
413 parsing.data = &reading;
416 parsing.error = errcb;
417 result = parse_utf8_config( &parsing);
418 buffer_destroy( &buffer);
419 if (result != 0 || reading.errcount != 0) {
420 writerror( "%d errors while parsing file %s",
421 reading.errcount, metafilepath);
424 /* set the variables */
425 heap_read_only( &heap);
426 for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
427 offset = reading.offsets[i];
429 var_values[i] = heap_address( &heap, offset);
431 writerror( "the variable %s isn't defined in file %s",
432 tzplatform_getname((enum tzplatform_variable)i), metafilepath);
433 /* TODO undefined variable */;
438 static const char *get_lock(enum tzplatform_variable id)
445 if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
446 /*error("invalid id"); TODO*/
452 result = var_values[offset];
457 /* reset the state of the environement */
458 void tzplatform_reset()
461 if (state != RESET) {
463 heap_destroy( &heap);
469 int tzplatform_getcount()
471 return (int)_TZPLATFORM_VARIABLES_COUNT_;
474 const char* tzplatform_getname(enum tzplatform_variable id)
476 const struct varassoc *iter, *end;
481 if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
482 /*error("invalid id"); TODO*/
488 end = iter + (sizeof namassoc / sizeof namassoc[0]);
489 while (iter != end) {
490 if (iter->offset >= 0)
491 var_names[(int)iter->id] = varpool + iter->offset;
495 result = var_names[offset];
500 enum tzplatform_variable tzplatform_getid(const char *name)
502 const struct varassoc *vara = hashvar( name, strlen(name));
503 return vara ? vara->id : _TZPLATFORM_VARIABLES_INVALID_;
506 const char* tzplatform_getenv(enum tzplatform_variable id)
508 const char *result = get_lock( id);
513 int tzplatform_getenv_int(enum tzplatform_variable id)
515 const char *value = get_lock( id);
516 int result = value==NULL ? -1 : atoi(value);
521 const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
523 const char *array[3];
524 const char *result = get_lock( id);
525 if (result != NULL) {
529 result = scratchcat( 0, array);
535 const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
537 const char *array[3];
538 const char *result = get_lock( id);
539 if (result != NULL) {
543 result = scratchcat( 1, array);
549 const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path,
552 const char *array[4];
553 const char *result = get_lock( id);
554 if (result != NULL) {
559 result = scratchcat( 1, array);
565 const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path,
566 const char* path2, const char *path3)
568 const char *array[5];
569 const char *result = get_lock( id);
570 if (result != NULL) {
576 result = scratchcat( 1, array);
582 uid_t tzplatform_getuid(enum tzplatform_variable id)
584 uid_t result = (uid_t)-1;
585 const char *value = get_lock( id);
587 pw_get_uid( value, &result);
593 gid_t tzplatform_getgid(enum tzplatform_variable id)
595 gid_t result = (uid_t)-1;
596 const char *value = get_lock( id);
598 pw_get_gid( value, &result);
607 while(i != (int)_TZPLATFORM_VARIABLES_COUNT_) {
608 enum tzplatform_variable id = (enum tzplatform_variable)i;
609 const char*name = tzplatform_getname(id);
610 const char*value = tzplatform_getenv(id);
611 int xid = (int)tzplatform_getid(name);
612 printf("%d=%d\t%s=%s\n",i,xid,name,value);