4 * Runtime and assembly configuration file support routines.
6 * Author: Paolo Molaro (lupus@ximian.com)
8 * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
15 #include "mono/metadata/assembly.h"
16 #include "mono/metadata/loader.h"
17 #include "mono/metadata/mono-config.h"
18 #include "mono/metadata/metadata-internals.h"
19 #include "mono/metadata/object-internals.h"
20 #include "mono/utils/mono-logger-internal.h"
22 #if defined(TARGET_PS3)
23 #define CONFIG_OS "CellOS"
24 #elif defined(__linux__)
25 #define CONFIG_OS "linux"
26 #elif defined(__APPLE__)
27 #define CONFIG_OS "osx"
29 #define CONFIG_OS "solaris"
30 #elif defined(__FreeBSD__)
31 #define CONFIG_OS "freebsd"
32 #elif defined(__NetBSD__)
33 #define CONFIG_OS "netbsd"
34 #elif defined(__OpenBSD__)
35 #define CONFIG_OS "openbsd"
36 #elif defined(__WIN32__)
37 #define CONFIG_OS "windows"
39 #define CONFIG_OS "aix"
41 #define CONFIG_OS "hpux"
42 #elif defined(__HAIKU__)
43 #define CONFIG_OS "haiku"
45 #warning Unknown operating system
46 #define CONFIG_OS "unknownOS"
51 #define CONFIG_CPU "x86"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__x86_64__)
54 #define CONFIG_CPU "x86-64"
55 #define CONFIG_WORDSIZE "64"
56 #elif defined(sparc) || defined(__sparc__)
57 #define CONFIG_CPU "sparc"
58 #define CONFIG_WORDSIZE "32"
59 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
60 #define CONFIG_WORDSIZE "64"
61 #ifdef __mono_ppc_ilp32__
62 # define CONFIG_CPU "ppc64ilp32"
64 # define CONFIG_CPU "ppc64"
66 #elif defined(__ppc__) || defined(__powerpc__)
67 #define CONFIG_CPU "ppc"
68 #define CONFIG_WORDSIZE "32"
69 #elif defined(__s390x__)
70 #define CONFIG_CPU "s390x"
71 #define CONFIG_WORDSIZE "64"
72 #elif defined(__s390__)
73 #define CONFIG_CPU "s390"
74 #define CONFIG_WORDSIZE "32"
75 #elif defined(__arm__)
76 #define CONFIG_CPU "arm"
77 #define CONFIG_WORDSIZE "32"
78 #elif defined(__ia64__)
79 #define CONFIG_CPU "ia64"
80 #define CONFIG_WORDSIZE "64"
81 #elif defined(__alpha__)
82 #define CONFIG_CPU "alpha"
83 #define CONFIG_WORDSIZE "64"
84 #elif defined(hppa) || defined(__hppa__)
85 #define CONFIG_CPU "hppa"
86 #define CONFIG_WORDSIZE "32"
87 #elif defined(mips) || defined(__mips) || defined(_mips)
88 #define CONFIG_CPU "mips"
89 #define CONFIG_WORDSIZE "32"
92 #define CONFIG_CPU "unknownCPU"
96 static void start_element (GMarkupParseContext *context,
97 const gchar *element_name,
98 const gchar **attribute_names,
99 const gchar **attribute_values,
103 static void end_element (GMarkupParseContext *context,
104 const gchar *element_name,
108 static void parse_text (GMarkupParseContext *context,
114 static void passthrough (GMarkupParseContext *context,
120 static void parse_error (GMarkupParseContext *context,
124 static const GMarkupParser
133 static GHashTable *config_handlers;
135 static const char *mono_cfg_dir = NULL;
136 static char *mono_cfg_dir_allocated = NULL;
138 /* when this interface is stable, export it. */
139 typedef struct MonoParseHandler MonoParseHandler;
141 struct MonoParseHandler {
142 const char *element_name;
143 void*(*init) (MonoImage *assembly);
144 void (*start) (gpointer user_data, const gchar *name,
145 const gchar **attributes,
146 const gchar **values);
147 void (*text) (gpointer user_data, const char *text, gsize test_len);
148 void (*end) (gpointer user_data, const char *name);
149 void (*finish) (gpointer user_data);
153 MonoAssemblyBindingInfo *info;
154 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
159 MonoParseHandler *current;
165 static void start_element (GMarkupParseContext *context,
166 const gchar *element_name,
167 const gchar **attribute_names,
168 const gchar **attribute_values,
172 ParseState *state = user_data;
173 if (!state->current) {
174 state->current = g_hash_table_lookup (config_handlers, element_name);
175 if (state->current && state->current->init)
176 state->user_data = state->current->init (state->assembly);
178 if (state->current && state->current->start)
179 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
182 static void end_element (GMarkupParseContext *context,
183 const gchar *element_name,
187 ParseState *state = user_data;
188 if (state->current) {
189 if (state->current->end)
190 state->current->end (state->user_data, element_name);
191 if (strcmp (state->current->element_name, element_name) == 0) {
192 if (state->current->finish)
193 state->current->finish (state->user_data);
194 state->current = NULL;
195 state->user_data = NULL;
200 static void parse_text (GMarkupParseContext *context,
206 ParseState *state = user_data;
207 if (state->current && state->current->text)
208 state->current->text (state->user_data, text, text_len);
211 static void passthrough (GMarkupParseContext *context,
220 static void parse_error (GMarkupParseContext *context,
224 ParseState *state = user_data;
226 const gchar *filename;
228 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
229 msg = error && error->message ? error->message : "";
230 g_warning ("Error parsing %s: %s", filename, msg);
234 arch_matches (const char* arch, const char *value)
236 char **splitted, **p;
238 if (value [0] == '!')
239 return !arch_matches (arch, value + 1);
240 p = splitted = g_strsplit (value, ",", 0);
242 if (strcmp (arch, *p) == 0) {
248 g_strfreev (splitted);
260 dllmap_init (MonoImage *assembly) {
261 DllInfo *info = g_new0 (DllInfo, 1);
262 info->assembly = assembly;
267 dllmap_start (gpointer user_data,
268 const gchar *element_name,
269 const gchar **attribute_names,
270 const gchar **attribute_values)
273 DllInfo *info = user_data;
275 if (strcmp (element_name, "dllmap") == 0) {
277 g_free (info->target);
278 info->dll = info->target = NULL;
279 info->ignore = FALSE;
280 for (i = 0; attribute_names [i]; ++i) {
281 if (strcmp (attribute_names [i], "dll") == 0)
282 info->dll = g_strdup (attribute_values [i]);
283 else if (strcmp (attribute_names [i], "target") == 0)
284 info->target = g_strdup (attribute_values [i]);
285 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
287 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
289 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
293 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
294 } else if (strcmp (element_name, "dllentry") == 0) {
295 const char *name = NULL, *target = NULL, *dll = NULL;
297 for (i = 0; attribute_names [i]; ++i) {
298 if (strcmp (attribute_names [i], "dll") == 0)
299 dll = attribute_values [i];
300 else if (strcmp (attribute_names [i], "target") == 0)
301 target = attribute_values [i];
302 else if (strcmp (attribute_names [i], "name") == 0)
303 name = attribute_values [i];
304 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
306 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
308 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
313 if (!info->ignore && !ignore)
314 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
319 dllmap_finish (gpointer user_data)
321 DllInfo *info = user_data;
324 g_free (info->target);
328 static const MonoParseHandler
339 legacyUEP_start (gpointer user_data,
340 const gchar *element_name,
341 const gchar **attribute_names,
342 const gchar **attribute_values) {
343 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
344 (attribute_names [0] != NULL) &&
345 (strcmp (attribute_names [0], "enabled") == 0)) {
346 if ((strcmp (attribute_values [0], "1") == 0) ||
347 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
348 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
353 static const MonoParseHandler
354 legacyUEP_handler = {
355 "legacyUnhandledExceptionPolicy",
363 static int inited = 0;
366 mono_config_init (void)
369 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
370 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
371 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
375 mono_config_cleanup (void)
377 g_hash_table_destroy (config_handlers);
378 g_free (mono_cfg_dir_allocated);
381 /* FIXME: error handling */
384 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
386 GMarkupParseContext *context;
391 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
392 if (g_markup_parse_context_parse (context, text, len, NULL)) {
393 g_markup_parse_context_end_parse (context, NULL);
395 g_markup_parse_context_free (context);
398 /* If assembly is NULL, parse in the global context */
400 mono_config_parse_file_with_context (ParseState *state, const char *filename)
406 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
407 "Config attempting to parse: '%s'.", filename);
409 if (!g_file_get_contents (filename, &text, &len, NULL))
414 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
415 offset = 3; /* Skip UTF-8 BOM */
416 if (state->user_data == NULL)
417 state->user_data = (gpointer) filename;
418 mono_config_parse_xml_with_context (state, text + offset, len - offset);
424 * mono_config_parse_memory:
425 * @buffer: a pointer to an string XML representation of the configuration
427 * Parses the configuration from a buffer
430 mono_config_parse_memory (const char *buffer)
432 ParseState state = {NULL};
434 state.user_data = (gpointer) "<buffer>";
435 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
439 mono_config_parse_file (const char *filename)
441 ParseState state = {NULL};
442 state.user_data = (gpointer) filename;
443 mono_config_parse_file_with_context (&state, filename);
447 * use the equivalent lookup code from the GAC when available.
448 * Depending on state, this should give something like:
449 * aname/version-pubtoken/
454 get_assembly_filename (MonoImage *image, int state)
458 return g_strdup (mono_image_get_name (image));
464 typedef struct _BundledConfig BundledConfig;
466 struct _BundledConfig {
469 const char* config_xml;
472 static BundledConfig *bundled_configs = NULL;
474 static const char *bundled_machine_config = NULL;
477 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
479 BundledConfig *bconfig;
481 bconfig = g_new0 (BundledConfig, 1);
482 bconfig->aname = assembly_name;
483 bconfig->config_xml = config_xml;
484 bconfig->next = bundled_configs;
485 bundled_configs = bconfig;
489 mono_config_string_for_assembly_file (const char *filename)
491 BundledConfig *bconfig;
493 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
494 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
495 return bconfig->config_xml;
501 mono_config_for_assembly (MonoImage *assembly)
503 ParseState state = {NULL};
505 char *aname, *cfg, *cfg_name;
506 const char *bundled_config;
509 state.assembly = assembly;
511 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
512 if (bundled_config) {
513 state.user_data = (gpointer) "<bundled>";
514 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
517 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
518 mono_config_parse_file_with_context (&state, cfg_name);
521 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
523 home = g_get_home_dir ();
525 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
526 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
527 got_it += mono_config_parse_file_with_context (&state, cfg);
531 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
532 got_it += mono_config_parse_file_with_context (&state, cfg);
544 * @filename: the filename to load the configuration variables from.
546 * Pass a NULL filename to parse the default config files
547 * (or the file in the MONO_CONFIG env var).
550 mono_config_parse (const char *filename) {
558 mono_config_parse_file (filename);
562 home = g_getenv ("MONO_CONFIG");
564 mono_config_parse_file (home);
568 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
569 mono_config_parse_file (mono_cfg);
573 home = g_get_home_dir ();
574 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
575 mono_config_parse_file (user_cfg);
580 /* Invoked during startup */
582 mono_set_config_dir (const char *dir)
584 /* If this variable is set, overrides the directory computed */
585 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
586 if (mono_cfg_dir == NULL)
587 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
591 mono_get_config_dir (void)
593 if (mono_cfg_dir == NULL)
594 mono_set_dirs (NULL, NULL);
600 mono_register_machine_config (const char *config_xml)
602 bundled_machine_config = config_xml;
606 mono_get_machine_config (void)
608 return bundled_machine_config;
612 assembly_binding_end (gpointer user_data, const char *element_name)
614 ParserUserData *pud = user_data;
616 if (!strcmp (element_name, "dependentAssembly")) {
617 if (pud->info_parsed && pud->info) {
618 pud->info_parsed (pud->info, pud->user_data);
619 g_free (pud->info->name);
620 g_free (pud->info->culture);
626 publisher_policy_start (gpointer user_data,
627 const gchar *element_name,
628 const gchar **attribute_names,
629 const gchar **attribute_values)
632 MonoAssemblyBindingInfo *info;
637 if (!strcmp (element_name, "dependentAssembly")) {
639 info->culture = NULL;
640 info->has_old_version_bottom = FALSE;
641 info->has_old_version_top = FALSE;
642 info->has_new_version = FALSE;
643 info->is_valid = FALSE;
644 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
645 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
646 memset (&info->new_version, 0, sizeof (info->new_version));
647 } if (!strcmp (element_name, "assemblyIdentity")) {
648 for (n = 0; attribute_names [n]; n++) {
649 const gchar *attribute_name = attribute_names [n];
651 if (!strcmp (attribute_name, "name"))
652 info->name = g_strdup (attribute_values [n]);
653 else if (!strcmp (attribute_name, "publicKeyToken")) {
654 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
655 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
656 } else if (!strcmp (attribute_name, "culture")) {
657 if (!strcmp (attribute_values [n], "neutral"))
658 info->culture = g_strdup ("");
660 info->culture = g_strdup (attribute_values [n]);
663 } else if (!strcmp (element_name, "bindingRedirect")) {
664 for (n = 0; attribute_names [n]; n++) {
665 const gchar *attribute_name = attribute_names [n];
667 if (!strcmp (attribute_name, "oldVersion")) {
668 gchar **numbers, **version, **versions;
669 gint major, minor, build, revision;
672 if (!strcmp (attribute_values [n], ""))
675 versions = g_strsplit (attribute_values [n], "-", 2);
676 version = g_strsplit (*versions, ".", 4);
678 /* We assign the values to gint vars to do the checks */
680 major = *numbers ? atoi (*numbers++) : -1;
681 minor = *numbers ? atoi (*numbers++) : -1;
682 build = *numbers ? atoi (*numbers++) : -1;
683 revision = *numbers ? atoi (*numbers) : -1;
684 g_strfreev (version);
685 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
686 g_strfreev (versions);
690 info->old_version_bottom.major = major;
691 info->old_version_bottom.minor = minor;
692 info->old_version_bottom.build = build;
693 info->old_version_bottom.revision = revision;
694 info->has_old_version_bottom = TRUE;
696 if (!*(versions + 1)) {
697 g_strfreev (versions);
701 numbers = version = g_strsplit (*(versions + 1), ".", 4);
702 major = *numbers ? atoi (*numbers++) : -1;
703 minor = *numbers ? atoi (*numbers++) : -1;
704 build = *numbers ? atoi (*numbers++) : -1;
705 revision = *numbers ? atoi (*numbers) : 1;
706 g_strfreev (version);
707 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
708 g_strfreev (versions);
712 info->old_version_top.major = major;
713 info->old_version_top.minor = minor;
714 info->old_version_top.build = build;
715 info->old_version_top.revision = revision;
716 info->has_old_version_top = TRUE;
718 g_strfreev (versions);
719 } else if (!strcmp (attribute_name, "newVersion")) {
720 gchar **numbers, **version;
723 if (!strcmp (attribute_values [n], ""))
726 numbers = version = g_strsplit (attribute_values [n], ".", 4);
727 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
728 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
729 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
730 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
731 info->has_new_version = TRUE;
732 g_strfreev (version);
738 static MonoParseHandler
739 publisher_policy_parser = {
740 "", /* We don't need to use declare an xml element */
742 publisher_policy_start,
749 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
751 ParserUserData user_data = {
757 &publisher_policy_parser, /* MonoParseHandler */
758 &user_data, /* user_data */
759 NULL, /* MonoImage (we don't need it right now)*/
760 TRUE /* We are already inited */
763 mono_config_parse_file_with_context (&state, filename);
766 static MonoParseHandler
767 config_assemblybinding_parser = {
768 "", /* We don't need to use declare an xml element */
770 publisher_policy_start,
772 assembly_binding_end,
777 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
779 MonoAssemblyBindingInfo info;
787 pud.info_parsed = infocb;
788 pud.user_data = user_data;
790 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
791 state.user_data = &pud;
792 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
793 state.inited = TRUE; /* We are already inited */
795 mono_config_parse_file_with_context (&state, filename);