tizen-platform-wrapper: fix resource leak
[platform/core/system/tizen-platform-wrapper.git] / src / init.c
1 /*
2  * Copyright (C) 2013-2014 Intel Corporation.
3  * 
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.
8  *
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.
13  *
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
17  *
18  * Authors:
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>
22  *
23  */
24 #define _GNU_SOURCE
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <pthread.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 #include <stdarg.h>
40 #include <alloca.h>
41 #include <pwd.h>
42 #include <assert.h>
43
44 #ifndef NOT_MULTI_THREAD_SAFE
45 #include <pthread.h>
46 #endif
47
48 #ifndef CONFIGPATH
49 #define CONFIGPATH "/etc/tizen-platform.conf"
50 #endif
51
52 #include "tzplatform_variables.h"
53 #include "tzplatform_config.h"
54 #include "parser.h"
55 #include "heap.h"
56 #include "buffer.h"
57 #include "foreign.h"
58 #include "scratch.h"
59 #include "passwd.h"
60 #include "context.h"
61 #include "hashing.h"
62 #include "init.h"
63
64 #define _HAS_IDS_   (  _FOREIGN_HAS_(UID)  \
65                     || _FOREIGN_HAS_(EUID) \
66                     || _FOREIGN_HAS_(GID)  )
67
68 #define _HAS_PWS_   (  _FOREIGN_HAS_(HOME)  \
69                     || _FOREIGN_HAS_(USER)  \
70                     || _FOREIGN_HAS_(EHOME) \
71                     || _FOREIGN_HAS_(EUSER) )
72
73 /* local and static variables */
74 static const char metafilepath[] = CONFIGPATH;
75 static const char emptystring[] = "";
76
77 /* structure for reading config files */
78 struct reading {
79     int errcount;
80     struct tzplatform_context *context;
81     size_t dynvars[_FOREIGN_COUNT_];
82     size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
83 };
84
85 /* write the error message */
86 static void writerror( const char *format, ...)
87 {
88     va_list ap;
89     fprintf( stderr, "tzplatform_config ERROR: ");
90     va_start(ap, format);
91     vfprintf(stderr, format, ap);
92     va_end(ap);
93     fprintf( stderr, "\n");
94 }
95
96 #if _HAS_IDS_
97 /* fill the foreign variables for ids */
98 static void foreignid( struct reading *reading)
99 {
100     int n;
101     char buffer[50];
102
103 #if _FOREIGN_HAS_(UID)
104     /* set the uid */
105     if (reading->dynvars[UID] == HNULL) {
106         n = snprintf( buffer, sizeof buffer, "%d", (int)get_uid(reading->context));
107         if (0 < n && n < (int)(sizeof buffer))
108             reading->dynvars[UID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
109     }
110 #endif
111
112 #if _FOREIGN_HAS_(EUID)
113     /* set the euid */
114     if (reading->dynvars[EUID] == HNULL) {
115         n = snprintf( buffer, sizeof buffer, "%d", (int)get_euid(reading->context));
116         if (0 < n && n < (int)(sizeof buffer))
117             reading->dynvars[EUID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
118     }
119 #endif
120
121 #if _FOREIGN_HAS_(GID)
122     /* set the gid */
123     if (reading->dynvars[GID] == HNULL) {
124         n = snprintf( buffer, sizeof buffer, "%d", (int)get_gid(reading->context));
125         if (0 < n && n < (int)(sizeof buffer))
126             reading->dynvars[GID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
127     }
128 #endif
129 }
130 #endif
131
132 #if _HAS_PWS_
133 /* fill the foreign variables for home and user */
134 static void foreignpw( struct reading *reading)
135 {
136     int n = 0;
137     struct pwget *array[3];
138 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
139     struct pwget uid;
140     char suid[50];
141 #endif
142 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
143     struct pwget euid;
144     char seuid[50];
145 #endif
146
147 #if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
148     if (
149 #if _FOREIGN_HAS_(HOME)
150         reading->dynvars[HOME] == HNULL
151 #endif
152 #if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
153         ||
154 #endif
155 #if _FOREIGN_HAS_(USER)
156         reading->dynvars[USER] == HNULL
157 #endif
158     ) {
159         snprintf( suid, sizeof suid, "%u", (unsigned)get_uid(reading->context));
160         uid.id = suid;
161         array[n++] = &uid;
162     }
163     else {
164         uid.set = 0;
165     }
166 #endif
167
168 #if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
169     if (
170 #if _FOREIGN_HAS_(EHOME)
171         reading->dynvars[EHOME] == HNULL
172 #endif
173 #if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
174         ||
175 #endif
176 #if _FOREIGN_HAS_(EUSER)
177         reading->dynvars[EUSER] == HNULL
178 #endif
179     ) {
180         snprintf( seuid, sizeof seuid, "%u", (unsigned)get_euid(reading->context));
181         euid.id = seuid;
182         array[n++] = &euid;
183     }
184     else {
185         euid.set = 0;
186     }
187 #endif
188
189     if (n) {
190         array[n] = NULL;
191         if (pw_get( &reading->context->heap, array) == 0) {
192 #if _FOREIGN_HAS_(HOME)
193             if (uid.set)
194                 reading->dynvars[HOME] = uid.home;
195 #endif
196 #if _FOREIGN_HAS_(USER)
197             if (uid.set)
198                 reading->dynvars[USER] = uid.user;
199 #endif
200 #if _FOREIGN_HAS_(EHOME)
201             if (euid.set)
202                 reading->dynvars[EHOME] = euid.home;
203 #endif
204 #if _FOREIGN_HAS_(EUSER)
205             if (euid.set)
206                 reading->dynvars[EUSER] = euid.user;
207 #endif
208         }
209     }
210 }
211 #endif
212
213 /* get the foreign variable */
214 static const char *foreignvar( struct reading *reading, 
215                                             const char *name, size_t length)
216 {
217     enum fkey key = foreign( name, length);
218     size_t offset;
219
220     switch (key) {
221 #if _HAS_PWS_
222 #if _FOREIGN_HAS_(HOME)
223     case HOME:
224 #endif
225 #if _FOREIGN_HAS_(USER)
226     case USER:
227 #endif
228 #if _FOREIGN_HAS_(EHOME)
229     case EHOME:
230 #endif
231 #if _FOREIGN_HAS_(EUSER)
232     case EUSER:
233 #endif
234         foreignpw( reading);
235         break;
236 #endif
237  
238 #if _HAS_IDS_
239 #if _FOREIGN_HAS_(UID)
240     case UID:
241 #endif
242 #if _FOREIGN_HAS_(GID)
243     case GID:
244 #endif
245 #if _FOREIGN_HAS_(EUID)
246     case EUID:
247 #endif
248         foreignid( reading);
249         break;
250 #endif
251
252     default:
253         return NULL;
254     }
255     offset = reading->dynvars[key];
256     return offset==HNULL ? NULL : heap_address( &reading->context->heap, offset);
257 }
258
259 /* callback for parsing errors */
260 static int errcb( struct parsing *parsing, 
261             size_t position, const char *message)
262 {
263     struct parsinfo info;
264     struct reading *reading = parsing->data;
265
266     /* count the error */
267     reading->errcount++;
268
269     /* print the error */
270     parse_utf8_info( parsing, &info, position);
271     writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
272
273     /* continue to parse */
274     return 0;
275 }
276
277 /* callback for solving variables */
278 static const char *getcb( struct parsing *parsing, 
279             const char *key, size_t length,
280             size_t begin_pos, size_t end_pos)
281 {
282     struct parsinfo info;
283     const char *result;
284     size_t offset;
285     struct reading *reading = parsing->data;
286     int id;
287
288     /* try to find a tzplatform variable */
289     id = hashid(key, length);
290     if (id >= 0) {
291         /* found: try to use it */
292         offset = reading->offsets[id];
293         if (offset != HNULL)
294             result = heap_address( &reading->context->heap, offset);
295         else 
296             result = NULL;
297     }
298     else {
299         /* that is a foreign variable */
300         result = foreignvar( reading, key, length);
301     }
302
303     /* emit the error and then return */
304     if (result == NULL) {
305         reading->errcount++;
306         parse_utf8_info( parsing, &info, begin_pos);
307         writerror( "undefined value for %.*s (file %s line %d)",
308             length, key, metafilepath, info.lino);
309         result = emptystring; /* avoid error of the parser */
310     }
311     return result;
312 }
313
314 /* callback to define variables */
315 static int putcb( struct parsing *parsing, 
316             const char *key, size_t key_length, 
317             const char *value, size_t value_length,
318             size_t begin_pos, size_t end_pos)
319 {
320     struct parsinfo info;
321     size_t offset;
322     char *string;
323     struct reading *reading = parsing->data;
324     int id;
325
326     /* try to find a tzplatform variable */
327     id = hashid( key, key_length);
328     if (id >= 0) {
329         /* check that the variable isn't already defined */
330         offset = reading->offsets[id];
331         if (offset != HNULL) {
332             reading->errcount++;
333             parse_utf8_info( parsing, &info, begin_pos);
334             writerror( "redefinition of variable %.*s (file %s line %d)",
335                     key_length, key, metafilepath, info.lino);
336         }
337
338         /* allocate the variable value */
339         offset = heap_alloc( &reading->context->heap, value_length+1);
340         if (offset == HNULL) {
341             /* error of allocation */
342             reading->errcount++;
343             writerror( "out of memory");
344         }
345         else {
346             /* record the variable value */
347             reading->offsets[id] = offset;
348             string = heap_address( &reading->context->heap, offset);
349             memcpy( string, value, value_length);
350             string[value_length] = 0;
351         }
352     }
353     else {
354         /* forbidden variable */
355         parse_utf8_info( parsing, &info, begin_pos);
356         writerror( "forbidden variable name %.*s (file %s line %d)",
357             key_length, key, metafilepath, info.lino);
358         
359     }
360
361     /* continue to parse */
362     return 0;
363 }
364
365 /* initialize the environment */
366 inline void initialize(struct tzplatform_context *context)
367 {
368     struct buffer buffer;
369     struct parsing parsing;
370     struct reading reading;
371     size_t offset;
372     int i, result;
373
374     /* clear the variables */
375     reading.errcount = 0;
376     reading.context = context;
377     for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
378         reading.dynvars[i] = HNULL;
379     }
380     for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
381         context->values[i] = NULL;
382         reading.offsets[i] = HNULL;
383     }
384
385     /* read the configuration file */
386     result = buffer_create( &buffer, metafilepath);
387     if (result != 0) {
388         writerror( "can't read file %s",metafilepath);
389         context->state = ERROR;
390         return;
391     }
392
393     /* create the heap */
394     result = heap_create( &context->heap, 1);
395     if (result != 0) {
396         buffer_destroy( &buffer);
397         writerror( "out of memory");
398         context->state = ERROR;
399         return;
400     }
401
402     /* read the file */
403     parsing.buffer = buffer.buffer;
404     parsing.length = buffer.length;
405     parsing.maximum_data_size = 0;
406     parsing.should_escape = 0;
407     parsing.data = &reading;
408     parsing.get = getcb;
409     parsing.put = putcb;
410     parsing.error = errcb;
411     result = parse_utf8_config( &parsing);
412     buffer_destroy( &buffer);
413     if (result != 0 || reading.errcount != 0) {
414         writerror( "%d errors while parsing file %s",
415                                             reading.errcount, metafilepath);
416     }
417
418     /* set the variables */
419     heap_read_only( &context->heap);
420     for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
421         offset = reading.offsets[i];
422         if (offset != HNULL)
423             context->values[i] = heap_address( &context->heap, offset);
424         else
425             writerror( "the variable %s isn't defined in file %s",
426                 keyname(i), metafilepath);
427             /* TODO undefined variable */;
428     }
429     context->state = VALID;
430 }
431