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