New test.
[mono.git] / mono / metadata / mono-config.c
1 /*
2  * mono-config.c
3  *
4  * Runtime and assembly configuration file support routines.
5  *
6  * Author: Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  */
10 #include "config.h"
11 #include <glib.h>
12 #include <string.h>
13
14 #include "mono/metadata/assembly.h"
15 #include "mono/metadata/loader.h"
16 #include "mono/metadata/mono-config.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/utils/mono-logger.h"
19
20 #if defined(__linux__)
21 #define CONFIG_OS "linux"
22 #elif defined(__APPLE__)
23 #define CONFIG_OS "osx"
24 #elif defined(sun)
25 #define CONFIG_OS "solaris"
26 #elif defined(__FreeBSD__)
27 #define CONFIG_OS "freebsd"
28 #elif defined(__NetBSD__)
29 #define CONFIG_OS "netbsd"
30 #elif defined(__OpenBSD__)
31 #define CONFIG_OS "openbsd"
32 #elif defined(__WIN32__)
33 #define CONFIG_OS "windows"
34 #elif defined(_IBMR2)
35 #define CONFIG_OS "aix"
36 #elif defined(__hpux)
37 #define CONFIG_OS "hpux"
38 #else
39 #warning Unknown operating system
40 #define CONFIG_OS "unknownOS"
41 #endif
42
43 #if defined(__i386__)
44 #define CONFIG_CPU "x86"
45 #elif defined(__x86_64__)
46 #define CONFIG_CPU "x86-64"
47 #elif defined(sparc) || defined(__sparc__)
48 #define CONFIG_CPU "sparc"
49 #elif defined(__ppc__) || defined(__powerpc__)
50 #define CONFIG_CPU "ppc"
51 #elif defined(__s390x__)
52 #define CONFIG_CPU "s390x"
53 #elif defined(__s390__)
54 #define CONFIG_CPU "s390"
55 #elif defined(__arm__)
56 #define CONFIG_CPU "arm"
57 #elif defined(__ia64__)
58 #define CONFIG_CPU "ia64"
59 #elif defined(__alpha__)
60 #define CONFIG_CPU "alpha"
61 #elif defined(hppa) || defined(__hppa__)
62 #define CONFIG_CPU "hppa"
63 #elif defined(mips) || defined(__mips) || defined(_mips)
64 #define CONFIG_CPU "mips"
65 #else
66 #warning Unknown CPU
67 #define CONFIG_CPU "unknownCPU"
68 #endif
69
70 static void start_element (GMarkupParseContext *context, 
71                            const gchar         *element_name,
72                            const gchar        **attribute_names,
73                            const gchar        **attribute_values,
74                            gpointer             user_data,
75                            GError             **error);
76
77 static void end_element   (GMarkupParseContext *context,
78                            const gchar         *element_name,
79                            gpointer             user_data,
80                            GError             **error);
81
82 static void parse_text    (GMarkupParseContext *context,
83                            const gchar         *text,
84                            gsize                text_len,
85                            gpointer             user_data,
86                            GError             **error);
87
88 static void passthrough   (GMarkupParseContext *context,
89                            const gchar         *text,
90                            gsize                text_len,
91                            gpointer             user_data,
92                            GError             **error);
93
94 static void parse_error   (GMarkupParseContext *context,
95                            GError              *error,
96                            gpointer             user_data);
97
98 static const GMarkupParser 
99 mono_parser = {
100         start_element,
101         end_element,
102         parse_text,
103         passthrough,
104         parse_error
105 };
106
107 static GHashTable *config_handlers;
108
109 /* when this interface is stable, export it. */
110 typedef struct MonoParseHandler MonoParseHandler;
111
112 struct MonoParseHandler {
113         const char *element_name;
114         void*(*init)   (MonoImage *assembly);
115         void (*start)  (gpointer user_data, const gchar *name,
116                         const gchar **attributes,
117                         const gchar **values);
118         void (*text)   (gpointer user_data, const char *text, gsize test_len);
119         void (*end)    (gpointer user_data, const char *name);
120         void (*finish) (gpointer user_data);
121 };
122
123 typedef struct {
124         MonoParseHandler *current;
125         void *user_data;
126         MonoImage *assembly;
127         int inited;
128 } ParseState;
129
130 static void start_element (GMarkupParseContext *context, 
131                            const gchar         *element_name,
132                            const gchar        **attribute_names,
133                            const gchar        **attribute_values,
134                            gpointer             user_data,
135                            GError             **error)
136 {
137         ParseState *state = user_data;
138         if (!state->current) {
139                 state->current = g_hash_table_lookup (config_handlers, element_name);
140                 if (state->current && state->current->init)
141                         state->user_data = state->current->init (state->assembly);
142         }
143         if (state->current && state->current->start)
144                 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
145 }
146
147 static void end_element   (GMarkupParseContext *context,
148                            const gchar         *element_name,
149                            gpointer             user_data,
150                            GError             **error)
151 {
152         ParseState *state = user_data;
153         if (state->current) {
154                 if (state->current->end)
155                         state->current->end (state->user_data, element_name);
156                 if (strcmp (state->current->element_name, element_name) == 0) {
157                         if (state->current->finish)
158                                 state->current->finish (state->user_data);
159                         state->current = NULL;
160                         state->user_data = NULL;
161                 }
162         }
163 }
164
165 static void parse_text    (GMarkupParseContext *context,
166                            const gchar         *text,
167                            gsize                text_len,
168                            gpointer             user_data,
169                            GError             **error)
170 {
171         ParseState *state = user_data;
172         if (state->current && state->current->text)
173                 state->current->text (state->user_data, text, text_len);
174 }
175
176 static void passthrough   (GMarkupParseContext *context,
177                            const gchar         *text,
178                            gsize                text_len,
179                            gpointer             user_data,
180                            GError             **error)
181 {
182         /* do nothing */
183 }
184
185 static void parse_error   (GMarkupParseContext *context,
186                            GError              *error,
187                            gpointer             user_data)
188 {
189 }
190
191 static int
192 arch_matches (const char* arch, const char *value)
193 {
194         char **splitted, **p;
195         int found = FALSE;
196         if (value [0] == '!')
197                 return !arch_matches (arch, value + 1);
198         p = splitted = g_strsplit (value, ",", 0);
199         while (*p) {
200                 if (strcmp (arch, *p) == 0) {
201                         found = TRUE;
202                         break;
203                 }
204                 p++;
205         }
206         g_strfreev (splitted);
207         return found;
208 }
209
210 typedef struct {
211         char *dll;
212         char *target;
213         int ignore;
214         MonoImage *assembly;
215 } DllInfo;
216
217 static void*
218 dllmap_init (MonoImage *assembly) {
219         DllInfo *info = g_new0 (DllInfo, 1);
220         info->assembly = assembly;
221         return info;
222 }
223
224 static void
225 dllmap_start (gpointer user_data, 
226               const gchar         *element_name,
227               const gchar        **attribute_names,
228               const gchar        **attribute_values)
229 {
230         int i;
231         DllInfo *info = user_data;
232         
233         if (strcmp (element_name, "dllmap") == 0) {
234                 g_free (info->dll);
235                 g_free (info->target);
236                 info->dll = info->target = NULL;
237                 info->ignore = FALSE;
238                 for (i = 0; attribute_names [i]; ++i) {
239                         if (strcmp (attribute_names [i], "dll") == 0)
240                                 info->dll = g_strdup (attribute_values [i]);
241                         else if (strcmp (attribute_names [i], "target") == 0)
242                                 info->target = g_strdup (attribute_values [i]);
243                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
244                                 info->ignore = TRUE;
245                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
246                                 info->ignore = TRUE;
247                 }
248                 if (!info->ignore)
249                         mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
250         } else if (strcmp (element_name, "dllentry") == 0) {
251                 const char *name = NULL, *target = NULL, *dll = NULL;
252                 int ignore = FALSE;
253                 for (i = 0; attribute_names [i]; ++i) {
254                         if (strcmp (attribute_names [i], "dll") == 0)
255                                 dll = attribute_values [i];
256                         else if (strcmp (attribute_names [i], "target") == 0)
257                                 target = attribute_values [i];
258                         else if (strcmp (attribute_names [i], "name") == 0)
259                                 name = attribute_values [i];
260                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
261                                 ignore = TRUE;
262                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
263                                 ignore = TRUE;
264                 }
265                 if (!dll)
266                         dll = info->dll;
267                 if (!info->ignore && !ignore)
268                         mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
269         }
270 }
271
272 static void
273 dllmap_finish (gpointer user_data)
274 {
275         DllInfo *info = user_data;
276
277         g_free (info->dll);
278         g_free (info->target);
279         g_free (info);
280 }
281
282 static const MonoParseHandler
283 dllmap_handler = {
284         "dllmap",
285         dllmap_init,
286         dllmap_start,
287         NULL, /* text */
288         NULL, /* end */
289         dllmap_finish
290 };
291
292 static int inited = 0;
293
294 static void
295 mono_config_init (void)
296 {
297         inited = 1;
298         config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
299         g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
300 }
301
302 /* FIXME: error handling */
303
304 static void
305 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
306 {
307         GMarkupParseContext *context;
308
309         if (!inited)
310                 mono_config_init ();
311
312         context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
313         if (g_markup_parse_context_parse (context, text, len, NULL)) {
314                 g_markup_parse_context_end_parse (context, NULL);
315         }
316         g_markup_parse_context_free (context);
317 }
318
319 /* If assembly is NULL, parse in the global context */
320 static int
321 mono_config_parse_file_with_context (ParseState *state, const char *filename)
322 {
323         char *text;
324         gsize len;
325
326         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
327                         "Config attempting to parse: '%s'.", filename);
328
329         if (!g_file_get_contents (filename, &text, &len, NULL))
330                 return 0;
331         mono_config_parse_xml_with_context (state, text, len);
332         g_free (text);
333         return 1;
334 }
335
336 /**
337  * mono_config_parse_memory:
338  * @buffer: a pointer to an string XML representation of the configuration
339  *
340  * Parses the configuration from a buffer
341  */
342 void
343 mono_config_parse_memory (const char *buffer)
344 {
345         ParseState state = {NULL};
346         
347         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
348 }
349
350 static void
351 mono_config_parse_file (const char *filename)
352 {
353         ParseState state = {NULL};
354         mono_config_parse_file_with_context (&state, filename);
355 }
356
357 /* 
358  * use the equivalent lookup code from the GAC when available.
359  * Depending on state, this should give something like:
360  *      aname/version-pubtoken/
361  *      aname/version/
362  *      aname
363  */
364 static char*
365 get_assembly_filename (MonoImage *image, int state)
366 {
367         switch (state) {
368         case 0:
369                 return g_strdup (mono_image_get_name (image));
370         default:
371                 return NULL;
372         }
373 }
374
375 typedef struct _BundledConfig BundledConfig;
376
377 struct _BundledConfig {
378         BundledConfig *next;
379         const char* aname;
380         const char* config_xml;
381 };
382
383 static BundledConfig *bundled_configs = NULL;
384
385 void
386 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
387 {
388         BundledConfig *bconfig;
389
390         bconfig = g_new0 (BundledConfig, 1);
391         bconfig->aname = assembly_name;
392         bconfig->config_xml = config_xml;
393         bconfig->next = bundled_configs;
394         bundled_configs = bconfig;
395 }
396
397 void 
398 mono_config_for_assembly (MonoImage *assembly)
399 {
400         ParseState state = {NULL};
401         int got_it = 0, i;
402         char *aname, *cfg, *cfg_name;
403         const char *home;
404         BundledConfig *bconfig;
405         
406         state.assembly = assembly;
407
408         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
409                 if (bconfig->aname && strcmp (bconfig->aname, assembly->module_name) == 0)
410                         mono_config_parse_xml_with_context (&state, bconfig->config_xml, strlen (bconfig->config_xml));
411         }
412
413         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
414         mono_config_parse_file_with_context (&state, cfg_name);
415         g_free (cfg_name);
416
417         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
418
419         home = g_get_home_dir ();
420
421         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
422                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
423                 got_it += mono_config_parse_file_with_context (&state, cfg);
424                 g_free (cfg);
425
426 #ifndef PLATFORM_WIN32
427                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
428                 got_it += mono_config_parse_file_with_context (&state, cfg);
429                 g_free (cfg);
430 #endif
431                 g_free (aname);
432                 if (got_it)
433                         break;
434         }
435         g_free (cfg_name);
436 }
437
438 /**
439  * mono_config_parse:
440  * @filename: the filename to load the configuration variables from.
441  *
442  * Pass a NULL filename to parse the default config files
443  * (or the file in the MONO_CONFIG env var).
444  */
445 void
446 mono_config_parse (const char *filename) {
447         const char *home;
448         char *mono_cfg;
449 #ifndef PLATFORM_WIN32
450         char *user_cfg;
451 #endif
452
453         if (filename) {
454                 mono_config_parse_file (filename);
455                 return;
456         }
457
458         home = g_getenv ("MONO_CONFIG");
459         if (home) {
460                 mono_config_parse_file (home);
461                 return;
462         }
463
464         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
465         mono_config_parse_file (mono_cfg);
466         g_free (mono_cfg);
467
468 #ifndef PLATFORM_WIN32
469         home = g_get_home_dir ();
470         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
471         mono_config_parse_file (user_cfg);
472         g_free (user_cfg);
473 #endif
474 }
475
476 static const char *mono_cfg_dir = NULL;
477
478 /* Invoked during startup */
479 void
480 mono_set_config_dir (const char *dir)
481 {
482         /* If this variable is set, overrides the directory computed */
483         mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
484         if (mono_cfg_dir == NULL)
485                 mono_cfg_dir = g_strdup (dir);
486 }
487
488 const char* 
489 mono_get_config_dir (void)
490 {
491         if (mono_cfg_dir == NULL)
492                 mono_set_dirs (NULL, NULL);
493
494         return mono_cfg_dir;
495 }
496
497 static void
498 publisher_policy_start (gpointer user_data,
499                 const gchar *element_name,
500                 const gchar **attribute_names,
501                 const gchar **attribute_values)
502 {
503         MonoAssemblyBindingInfo *info;
504         int n;
505
506         info = user_data;
507         if (!strcmp (element_name, "assemblyIdentity")) {
508                 for (n = 0; attribute_names [n]; n++) {
509                         const gchar *attribute_name = attribute_names [n];
510                         
511                         if (!strcmp (attribute_name, "name"))
512                                 info->name = g_strdup (attribute_values [n]);
513                         else if (!strcmp (attribute_name, "publicKeyToken")) {
514                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
515                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
516                         } else if (!strcmp (attribute_name, "culture")) {
517                                 if (!strcmp (attribute_values [n], "neutral"))
518                                         info->culture = g_strdup ("");
519                                 else
520                                         info->culture = g_strdup (attribute_values [n]);
521                         }
522                 }
523         } else if (!strcmp (element_name, "bindingRedirect")) {
524                 for (n = 0; attribute_names [n]; n++) {
525                         const gchar *attribute_name = attribute_names [n];
526
527                         if (!strcmp (attribute_name, "oldVersion")) {
528                                 gchar **numbers, **version, **versions;
529                                 gint major, minor, build, revision;
530
531                                 /* Invalid value */
532                                 if (!strcmp (attribute_values [n], ""))
533                                         return;
534                                 
535                                 versions = g_strsplit (attribute_values [n], "-", 2);
536                                 version = g_strsplit (*versions, ".", 4);
537
538                                 /* We assign the values to gint vars to do the checks */
539                                 numbers = version;
540                                 major = *numbers ? atoi (*numbers++) : -1;
541                                 minor = *numbers ? atoi (*numbers++) : -1;
542                                 build = *numbers ? atoi (*numbers++) : -1;
543                                 revision = *numbers ? atoi (*numbers) : -1;
544                                 g_strfreev (version);
545                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
546                                         g_strfreev (versions);
547                                         return;
548                                 }
549
550                                 info->old_version_bottom.major = major;
551                                 info->old_version_bottom.minor = minor;
552                                 info->old_version_bottom.build = build;
553                                 info->old_version_bottom.revision = revision;
554                                 info->has_old_version_bottom = TRUE;
555
556                                 if (!*(versions + 1)) {
557                                         g_strfreev (versions);
558                                         continue;
559                                 }
560                                 
561                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
562                                 major = *numbers ? atoi (*numbers++) : -1;
563                                 minor = *numbers ? atoi (*numbers++) : -1;
564                                 build = *numbers ? atoi (*numbers++) : -1;
565                                 revision = *numbers ? atoi (*numbers) : 1;
566                                 g_strfreev (version);
567                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
568                                         g_strfreev (versions);
569                                         return;
570                                 }
571
572                                 info->old_version_top.major = major;
573                                 info->old_version_top.minor = minor;
574                                 info->old_version_top.build = build;
575                                 info->old_version_top.revision = revision;
576                                 info->has_old_version_top = TRUE;
577
578                                 g_strfreev (versions);
579                         } else if (!strcmp (attribute_name, "newVersion")) {
580                                 gchar **numbers, **version;
581
582                                 /* Invalid value */
583                                 if (!strcmp (attribute_values [n], ""))
584                                         return;
585
586                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
587                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
588                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
589                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
590                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
591                                 info->has_new_version = TRUE;
592                                 g_strfreev (version);
593                         }
594                 }
595         }
596 }
597
598 static MonoParseHandler
599 publisher_policy_parser = {
600         "", /* We don't need to use declare an xml element */
601         NULL,
602         publisher_policy_start,
603         NULL,
604         NULL,
605         NULL
606 };
607
608 void
609 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
610 {
611         ParseState state = {
612                 &publisher_policy_parser, /* MonoParseHandler */
613                 info, /* user_data */
614                 NULL, /* MonoImage (we don't need it right now)*/
615                 TRUE /* We are already inited */
616         };
617         
618         mono_config_parse_file_with_context (&state, filename);
619 }
620