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__) || defined(TARGET_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"
50 #if defined(__i386__) || defined(TARGET_X86)
51 #define CONFIG_CPU "x86"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__x86_64__) || defined(TARGET_AMD64)
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(mips) || defined(__mips) || defined(_mips)
82 #define CONFIG_CPU "mips"
83 #define CONFIG_WORDSIZE "32"
86 #define CONFIG_CPU "unknownCPU"
90 static void start_element (GMarkupParseContext *context,
91 const gchar *element_name,
92 const gchar **attribute_names,
93 const gchar **attribute_values,
97 static void end_element (GMarkupParseContext *context,
98 const gchar *element_name,
102 static void parse_text (GMarkupParseContext *context,
108 static void passthrough (GMarkupParseContext *context,
114 static void parse_error (GMarkupParseContext *context,
118 static const GMarkupParser
127 static GHashTable *config_handlers;
129 static const char *mono_cfg_dir = NULL;
130 static char *mono_cfg_dir_allocated = NULL;
132 /* when this interface is stable, export it. */
133 typedef struct MonoParseHandler MonoParseHandler;
135 struct MonoParseHandler {
136 const char *element_name;
137 void*(*init) (MonoImage *assembly);
138 void (*start) (gpointer user_data, const gchar *name,
139 const gchar **attributes,
140 const gchar **values);
141 void (*text) (gpointer user_data, const char *text, gsize test_len);
142 void (*end) (gpointer user_data, const char *name);
143 void (*finish) (gpointer user_data);
147 MonoAssemblyBindingInfo *info;
148 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
153 MonoParseHandler *current;
159 static void start_element (GMarkupParseContext *context,
160 const gchar *element_name,
161 const gchar **attribute_names,
162 const gchar **attribute_values,
166 ParseState *state = user_data;
167 if (!state->current) {
168 state->current = g_hash_table_lookup (config_handlers, element_name);
169 if (state->current && state->current->init)
170 state->user_data = state->current->init (state->assembly);
172 if (state->current && state->current->start)
173 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
176 static void end_element (GMarkupParseContext *context,
177 const gchar *element_name,
181 ParseState *state = user_data;
182 if (state->current) {
183 if (state->current->end)
184 state->current->end (state->user_data, element_name);
185 if (strcmp (state->current->element_name, element_name) == 0) {
186 if (state->current->finish)
187 state->current->finish (state->user_data);
188 state->current = NULL;
189 state->user_data = NULL;
194 static void parse_text (GMarkupParseContext *context,
200 ParseState *state = user_data;
201 if (state->current && state->current->text)
202 state->current->text (state->user_data, text, text_len);
205 static void passthrough (GMarkupParseContext *context,
214 static void parse_error (GMarkupParseContext *context,
218 ParseState *state = user_data;
220 const gchar *filename;
222 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
223 msg = error && error->message ? error->message : "";
224 g_warning ("Error parsing %s: %s", filename, msg);
228 arch_matches (const char* arch, const char *value)
230 char **splitted, **p;
232 if (value [0] == '!')
233 return !arch_matches (arch, value + 1);
234 p = splitted = g_strsplit (value, ",", 0);
236 if (strcmp (arch, *p) == 0) {
242 g_strfreev (splitted);
254 dllmap_init (MonoImage *assembly) {
255 DllInfo *info = g_new0 (DllInfo, 1);
256 info->assembly = assembly;
261 dllmap_start (gpointer user_data,
262 const gchar *element_name,
263 const gchar **attribute_names,
264 const gchar **attribute_values)
267 DllInfo *info = user_data;
269 if (strcmp (element_name, "dllmap") == 0) {
271 g_free (info->target);
272 info->dll = info->target = NULL;
273 info->ignore = FALSE;
274 for (i = 0; attribute_names [i]; ++i) {
275 if (strcmp (attribute_names [i], "dll") == 0)
276 info->dll = g_strdup (attribute_values [i]);
277 else if (strcmp (attribute_names [i], "target") == 0)
278 info->target = g_strdup (attribute_values [i]);
279 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
281 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
283 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
287 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
288 } else if (strcmp (element_name, "dllentry") == 0) {
289 const char *name = NULL, *target = NULL, *dll = NULL;
291 for (i = 0; attribute_names [i]; ++i) {
292 if (strcmp (attribute_names [i], "dll") == 0)
293 dll = attribute_values [i];
294 else if (strcmp (attribute_names [i], "target") == 0)
295 target = attribute_values [i];
296 else if (strcmp (attribute_names [i], "name") == 0)
297 name = attribute_values [i];
298 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
300 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
302 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
307 if (!info->ignore && !ignore)
308 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
313 dllmap_finish (gpointer user_data)
315 DllInfo *info = user_data;
318 g_free (info->target);
322 static const MonoParseHandler
333 legacyUEP_start (gpointer user_data,
334 const gchar *element_name,
335 const gchar **attribute_names,
336 const gchar **attribute_values) {
337 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
338 (attribute_names [0] != NULL) &&
339 (strcmp (attribute_names [0], "enabled") == 0)) {
340 if ((strcmp (attribute_values [0], "1") == 0) ||
341 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
342 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
347 static const MonoParseHandler
348 legacyUEP_handler = {
349 "legacyUnhandledExceptionPolicy",
357 static int inited = 0;
360 mono_config_init (void)
363 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
364 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
365 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
369 mono_config_cleanup (void)
372 g_hash_table_destroy (config_handlers);
373 g_free (mono_cfg_dir_allocated);
376 /* FIXME: error handling */
379 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
381 GMarkupParseContext *context;
386 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
387 if (g_markup_parse_context_parse (context, text, len, NULL)) {
388 g_markup_parse_context_end_parse (context, NULL);
390 g_markup_parse_context_free (context);
393 /* If assembly is NULL, parse in the global context */
395 mono_config_parse_file_with_context (ParseState *state, const char *filename)
401 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
402 "Config attempting to parse: '%s'.", filename);
404 if (!g_file_get_contents (filename, &text, &len, NULL))
409 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
410 offset = 3; /* Skip UTF-8 BOM */
411 if (state->user_data == NULL)
412 state->user_data = (gpointer) filename;
413 mono_config_parse_xml_with_context (state, text + offset, len - offset);
419 * mono_config_parse_memory:
420 * @buffer: a pointer to an string XML representation of the configuration
422 * Parses the configuration from a buffer
425 mono_config_parse_memory (const char *buffer)
427 ParseState state = {NULL};
429 state.user_data = (gpointer) "<buffer>";
430 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
434 mono_config_parse_file (const char *filename)
436 ParseState state = {NULL};
437 state.user_data = (gpointer) filename;
438 mono_config_parse_file_with_context (&state, filename);
442 * use the equivalent lookup code from the GAC when available.
443 * Depending on state, this should give something like:
444 * aname/version-pubtoken/
449 get_assembly_filename (MonoImage *image, int state)
453 return g_strdup (mono_image_get_name (image));
459 typedef struct _BundledConfig BundledConfig;
461 struct _BundledConfig {
464 const char* config_xml;
467 static BundledConfig *bundled_configs = NULL;
469 static const char *bundled_machine_config = NULL;
472 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
474 BundledConfig *bconfig;
476 bconfig = g_new0 (BundledConfig, 1);
477 bconfig->aname = assembly_name;
478 bconfig->config_xml = config_xml;
479 bconfig->next = bundled_configs;
480 bundled_configs = bconfig;
484 mono_config_string_for_assembly_file (const char *filename)
486 BundledConfig *bconfig;
488 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
489 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
490 return bconfig->config_xml;
496 mono_config_for_assembly (MonoImage *assembly)
498 ParseState state = {NULL};
500 char *aname, *cfg, *cfg_name;
501 const char *bundled_config;
504 state.assembly = assembly;
506 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
507 if (bundled_config) {
508 state.user_data = (gpointer) "<bundled>";
509 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
512 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
513 mono_config_parse_file_with_context (&state, cfg_name);
516 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
518 home = g_get_home_dir ();
520 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
521 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
522 got_it += mono_config_parse_file_with_context (&state, cfg);
526 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
527 got_it += mono_config_parse_file_with_context (&state, cfg);
539 * @filename: the filename to load the configuration variables from.
541 * Pass a NULL filename to parse the default config files
542 * (or the file in the MONO_CONFIG env var).
545 mono_config_parse (const char *filename) {
553 mono_config_parse_file (filename);
557 home = g_getenv ("MONO_CONFIG");
559 mono_config_parse_file (home);
563 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
564 mono_config_parse_file (mono_cfg);
567 #if !defined(TARGET_WIN32) && !defined(__native_client__)
568 home = g_get_home_dir ();
569 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
570 mono_config_parse_file (user_cfg);
575 /* Invoked during startup */
577 mono_set_config_dir (const char *dir)
579 /* If this variable is set, overrides the directory computed */
580 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
581 if (mono_cfg_dir == NULL)
582 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
586 mono_get_config_dir (void)
588 if (mono_cfg_dir == NULL)
589 mono_set_dirs (NULL, NULL);
595 mono_register_machine_config (const char *config_xml)
597 bundled_machine_config = config_xml;
601 mono_get_machine_config (void)
603 return bundled_machine_config;
607 assembly_binding_end (gpointer user_data, const char *element_name)
609 ParserUserData *pud = user_data;
611 if (!strcmp (element_name, "dependentAssembly")) {
612 if (pud->info_parsed && pud->info) {
613 pud->info_parsed (pud->info, pud->user_data);
614 g_free (pud->info->name);
615 g_free (pud->info->culture);
621 publisher_policy_start (gpointer user_data,
622 const gchar *element_name,
623 const gchar **attribute_names,
624 const gchar **attribute_values)
627 MonoAssemblyBindingInfo *info;
632 if (!strcmp (element_name, "dependentAssembly")) {
634 info->culture = NULL;
635 info->has_old_version_bottom = FALSE;
636 info->has_old_version_top = FALSE;
637 info->has_new_version = FALSE;
638 info->is_valid = FALSE;
639 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
640 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
641 memset (&info->new_version, 0, sizeof (info->new_version));
642 } if (!strcmp (element_name, "assemblyIdentity")) {
643 for (n = 0; attribute_names [n]; n++) {
644 const gchar *attribute_name = attribute_names [n];
646 if (!strcmp (attribute_name, "name"))
647 info->name = g_strdup (attribute_values [n]);
648 else if (!strcmp (attribute_name, "publicKeyToken")) {
649 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
650 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
651 } else if (!strcmp (attribute_name, "culture")) {
652 if (!strcmp (attribute_values [n], "neutral"))
653 info->culture = g_strdup ("");
655 info->culture = g_strdup (attribute_values [n]);
658 } else if (!strcmp (element_name, "bindingRedirect")) {
659 for (n = 0; attribute_names [n]; n++) {
660 const gchar *attribute_name = attribute_names [n];
662 if (!strcmp (attribute_name, "oldVersion")) {
663 gchar **numbers, **version, **versions;
664 gint major, minor, build, revision;
667 if (!strcmp (attribute_values [n], ""))
670 versions = g_strsplit (attribute_values [n], "-", 2);
671 version = g_strsplit (*versions, ".", 4);
673 /* We assign the values to gint vars to do the checks */
675 major = *numbers ? atoi (*numbers++) : -1;
676 minor = *numbers ? atoi (*numbers++) : -1;
677 build = *numbers ? atoi (*numbers++) : -1;
678 revision = *numbers ? atoi (*numbers) : -1;
679 g_strfreev (version);
680 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
681 g_strfreev (versions);
685 info->old_version_bottom.major = major;
686 info->old_version_bottom.minor = minor;
687 info->old_version_bottom.build = build;
688 info->old_version_bottom.revision = revision;
689 info->has_old_version_bottom = TRUE;
691 if (!*(versions + 1)) {
692 g_strfreev (versions);
696 numbers = version = g_strsplit (*(versions + 1), ".", 4);
697 major = *numbers ? atoi (*numbers++) : -1;
698 minor = *numbers ? atoi (*numbers++) : -1;
699 build = *numbers ? atoi (*numbers++) : -1;
700 revision = *numbers ? atoi (*numbers) : 1;
701 g_strfreev (version);
702 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
703 g_strfreev (versions);
707 info->old_version_top.major = major;
708 info->old_version_top.minor = minor;
709 info->old_version_top.build = build;
710 info->old_version_top.revision = revision;
711 info->has_old_version_top = TRUE;
713 g_strfreev (versions);
714 } else if (!strcmp (attribute_name, "newVersion")) {
715 gchar **numbers, **version;
718 if (!strcmp (attribute_values [n], ""))
721 numbers = version = g_strsplit (attribute_values [n], ".", 4);
722 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
723 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
724 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
725 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
726 info->has_new_version = TRUE;
727 g_strfreev (version);
733 static MonoParseHandler
734 publisher_policy_parser = {
735 "", /* We don't need to use declare an xml element */
737 publisher_policy_start,
744 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
746 ParserUserData user_data = {
752 &publisher_policy_parser, /* MonoParseHandler */
753 &user_data, /* user_data */
754 NULL, /* MonoImage (we don't need it right now)*/
755 TRUE /* We are already inited */
758 mono_config_parse_file_with_context (&state, filename);
761 static MonoParseHandler
762 config_assemblybinding_parser = {
763 "", /* We don't need to use declare an xml element */
765 publisher_policy_start,
767 assembly_binding_end,
772 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
774 MonoAssemblyBindingInfo info;
782 pud.info_parsed = infocb;
783 pud.user_data = user_data;
785 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
786 state.user_data = &pud;
787 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
788 state.inited = TRUE; /* We are already inited */
790 mono_config_parse_file_with_context (&state, filename);
793 static mono_bool mono_server_mode = FALSE;
796 mono_config_set_server_mode (mono_bool server_mode)
798 mono_server_mode = server_mode;
802 mono_config_is_server_mode (void)
804 return mono_server_mode;