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)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "mono/metadata/assembly.h"
17 #include "mono/metadata/loader.h"
18 #include "mono/metadata/mono-config.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include "mono/metadata/object-internals.h"
21 #include "mono/utils/mono-logger-internals.h"
23 #if defined(TARGET_PS3)
24 #define CONFIG_OS "CellOS"
25 #elif defined(__linux__)
26 #define CONFIG_OS "linux"
27 #elif defined(__APPLE__)
28 #define CONFIG_OS "osx"
30 #define CONFIG_OS "solaris"
31 #elif defined(__FreeBSD__)
32 #define CONFIG_OS "freebsd"
33 #elif defined(__NetBSD__)
34 #define CONFIG_OS "netbsd"
35 #elif defined(__OpenBSD__)
36 #define CONFIG_OS "openbsd"
37 #elif defined(__WIN32__) || defined(TARGET_WIN32)
38 #define CONFIG_OS "windows"
40 #define CONFIG_OS "aix"
42 #define CONFIG_OS "hpux"
43 #elif defined(__HAIKU__)
44 #define CONFIG_OS "haiku"
46 #warning Unknown operating system
47 #define CONFIG_OS "unknownOS"
51 #if defined(__i386__) || defined(TARGET_X86)
52 #define CONFIG_CPU "x86"
53 #define CONFIG_WORDSIZE "32"
54 #elif defined(__x86_64__) || defined(TARGET_AMD64)
55 #define CONFIG_CPU "x86-64"
56 #define CONFIG_WORDSIZE "64"
57 #elif defined(sparc) || defined(__sparc__)
58 #define CONFIG_CPU "sparc"
59 #define CONFIG_WORDSIZE "32"
60 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
61 #define CONFIG_WORDSIZE "64"
62 #ifdef __mono_ppc_ilp32__
63 # define CONFIG_CPU "ppc64ilp32"
65 # define CONFIG_CPU "ppc64"
67 #elif defined(__ppc__) || defined(__powerpc__)
68 #define CONFIG_CPU "ppc"
69 #define CONFIG_WORDSIZE "32"
70 #elif defined(__s390x__)
71 #define CONFIG_CPU "s390x"
72 #define CONFIG_WORDSIZE "64"
73 #elif defined(__s390__)
74 #define CONFIG_CPU "s390"
75 #define CONFIG_WORDSIZE "32"
76 #elif defined(__arm__)
77 #define CONFIG_CPU "arm"
78 #define CONFIG_WORDSIZE "32"
79 #elif defined(__aarch64__)
80 #define CONFIG_CPU "armv8"
81 #define CONFIG_WORDSIZE "64"
82 #elif defined(mips) || defined(__mips) || defined(_mips)
83 #define CONFIG_CPU "mips"
84 #define CONFIG_WORDSIZE "32"
87 #define CONFIG_CPU "unknownCPU"
94 * Returns the operating system that Mono is running on, as used for dllmap entries.
97 mono_config_get_os (void)
103 * mono_config_get_cpu:
105 * Returns the architecture that Mono is running on, as used for dllmap entries.
108 mono_config_get_cpu (void)
114 * mono_config_get_wordsize:
116 * Returns the word size that Mono is running on, as used for dllmap entries.
119 mono_config_get_wordsize (void)
121 return CONFIG_WORDSIZE;
124 static void start_element (GMarkupParseContext *context,
125 const gchar *element_name,
126 const gchar **attribute_names,
127 const gchar **attribute_values,
131 static void end_element (GMarkupParseContext *context,
132 const gchar *element_name,
136 static void parse_text (GMarkupParseContext *context,
142 static void passthrough (GMarkupParseContext *context,
148 static void parse_error (GMarkupParseContext *context,
152 static const GMarkupParser
161 static GHashTable *config_handlers;
163 static char *mono_cfg_dir = NULL;
165 /* when this interface is stable, export it. */
166 typedef struct MonoParseHandler MonoParseHandler;
168 struct MonoParseHandler {
169 const char *element_name;
170 void*(*init) (MonoImage *assembly);
171 void (*start) (gpointer user_data, const gchar *name,
172 const gchar **attributes,
173 const gchar **values);
174 void (*text) (gpointer user_data, const char *text, gsize test_len);
175 void (*end) (gpointer user_data, const char *name);
176 void (*finish) (gpointer user_data);
180 MonoAssemblyBindingInfo *info;
181 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
186 MonoParseHandler *current;
192 static void start_element (GMarkupParseContext *context,
193 const gchar *element_name,
194 const gchar **attribute_names,
195 const gchar **attribute_values,
199 ParseState *state = (ParseState *)user_data;
200 if (!state->current) {
201 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
202 if (state->current && state->current->init)
203 state->user_data = state->current->init (state->assembly);
205 if (state->current && state->current->start)
206 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
209 static void end_element (GMarkupParseContext *context,
210 const gchar *element_name,
214 ParseState *state = (ParseState *)user_data;
215 if (state->current) {
216 if (state->current->end)
217 state->current->end (state->user_data, element_name);
218 if (strcmp (state->current->element_name, element_name) == 0) {
219 if (state->current->finish)
220 state->current->finish (state->user_data);
221 state->current = NULL;
222 state->user_data = NULL;
227 static void parse_text (GMarkupParseContext *context,
233 ParseState *state = (ParseState *)user_data;
234 if (state->current && state->current->text)
235 state->current->text (state->user_data, text, text_len);
238 static void passthrough (GMarkupParseContext *context,
247 static void parse_error (GMarkupParseContext *context,
251 ParseState *state = (ParseState *)user_data;
253 const gchar *filename;
255 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
256 msg = error && error->message ? error->message : "";
257 g_warning ("Error parsing %s: %s", filename, msg);
261 arch_matches (const char* arch, const char *value)
263 char **splitted, **p;
265 if (value [0] == '!')
266 return !arch_matches (arch, value + 1);
267 p = splitted = g_strsplit (value, ",", 0);
269 if (strcmp (arch, *p) == 0) {
275 g_strfreev (splitted);
287 dllmap_init (MonoImage *assembly) {
288 DllInfo *info = g_new0 (DllInfo, 1);
289 info->assembly = assembly;
294 dllmap_start (gpointer user_data,
295 const gchar *element_name,
296 const gchar **attribute_names,
297 const gchar **attribute_values)
300 DllInfo *info = (DllInfo *)user_data;
302 if (strcmp (element_name, "dllmap") == 0) {
304 g_free (info->target);
305 info->dll = info->target = NULL;
306 info->ignore = FALSE;
307 for (i = 0; attribute_names [i]; ++i) {
308 if (strcmp (attribute_names [i], "dll") == 0)
309 info->dll = g_strdup (attribute_values [i]);
310 else if (strcmp (attribute_names [i], "target") == 0){
311 char *p = strstr (attribute_values [i], "$mono_libdir");
313 char *libdir = mono_native_getrootdir ();
314 size_t libdir_len = strlen (libdir);
317 result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
318 strncpy (result, attribute_values[i], p-attribute_values[i]);
319 strcpy (result+(p-attribute_values[i]), libdir);
321 strcat (result, p+strlen("$mono_libdir"));
322 info->target = result;
324 info->target = g_strdup (attribute_values [i]);
325 } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
327 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
329 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
333 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
334 } else if (strcmp (element_name, "dllentry") == 0) {
335 const char *name = NULL, *target = NULL, *dll = NULL;
337 for (i = 0; attribute_names [i]; ++i) {
338 if (strcmp (attribute_names [i], "dll") == 0)
339 dll = attribute_values [i];
340 else if (strcmp (attribute_names [i], "target") == 0)
341 target = attribute_values [i];
342 else if (strcmp (attribute_names [i], "name") == 0)
343 name = attribute_values [i];
344 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
346 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
348 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
353 if (!info->ignore && !ignore)
354 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
359 dllmap_finish (gpointer user_data)
361 DllInfo *info = (DllInfo *)user_data;
364 g_free (info->target);
368 static const MonoParseHandler
379 legacyUEP_start (gpointer user_data,
380 const gchar *element_name,
381 const gchar **attribute_names,
382 const gchar **attribute_values) {
383 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
384 (attribute_names [0] != NULL) &&
385 (strcmp (attribute_names [0], "enabled") == 0)) {
386 if ((strcmp (attribute_values [0], "1") == 0) ||
387 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
388 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
393 static const MonoParseHandler
394 legacyUEP_handler = {
395 "legacyUnhandledExceptionPolicy",
404 aot_cache_start (gpointer user_data,
405 const gchar *element_name,
406 const gchar **attribute_names,
407 const gchar **attribute_values)
410 MonoAotCacheConfig *config;
412 if (strcmp (element_name, "aotcache") != 0)
415 config = mono_get_aot_cache_config ();
417 /* Per-app configuration */
418 for (i = 0; attribute_names [i]; ++i) {
419 if (!strcmp (attribute_names [i], "app")) {
420 config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
424 /* Global configuration */
425 for (i = 0; attribute_names [i]; ++i) {
426 if (!strcmp (attribute_names [i], "assemblies")) {
430 parts = g_strsplit (attribute_values [i], " ", -1);
431 for (ptr = parts; ptr && *ptr; ptr ++) {
433 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
436 } else if (!strcmp (attribute_names [i], "options")) {
437 config->aot_options = g_strdup (attribute_values [i]);
442 static const MonoParseHandler
443 aot_cache_handler = {
452 static int inited = 0;
455 mono_config_init (void)
458 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
459 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
460 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
461 g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
465 * mono_config_cleanup:
468 mono_config_cleanup (void)
471 g_hash_table_destroy (config_handlers);
472 g_free (mono_cfg_dir);
475 /* FIXME: error handling */
478 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
480 GMarkupParseContext *context;
485 context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
486 if (g_markup_parse_context_parse (context, text, len, NULL)) {
487 g_markup_parse_context_end_parse (context, NULL);
489 g_markup_parse_context_free (context);
492 /* If assembly is NULL, parse in the global context */
494 mono_config_parse_file_with_context (ParseState *state, const char *filename)
500 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
501 "Config attempting to parse: '%s'.", filename);
503 if (!g_file_get_contents (filename, &text, &len, NULL))
507 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
508 offset = 3; /* Skip UTF-8 BOM */
509 if (state->user_data == NULL)
510 state->user_data = (gpointer) filename;
511 mono_config_parse_xml_with_context (state, text + offset, len - offset);
517 * mono_config_parse_memory:
518 * \param buffer a pointer to an string XML representation of the configuration
519 * Parses the configuration from a buffer
522 mono_config_parse_memory (const char *buffer)
524 ParseState state = {NULL};
526 state.user_data = (gpointer) "<buffer>";
527 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
531 mono_config_parse_file (const char *filename)
533 ParseState state = {NULL};
534 state.user_data = (gpointer) filename;
535 mono_config_parse_file_with_context (&state, filename);
539 * use the equivalent lookup code from the GAC when available.
540 * Depending on state, this should give something like:
541 * aname/version-pubtoken/
546 get_assembly_filename (MonoImage *image, int state)
550 return g_strdup (mono_image_get_name (image));
556 typedef struct _BundledConfig BundledConfig;
558 struct _BundledConfig {
561 const char* config_xml;
564 static BundledConfig *bundled_configs = NULL;
566 static const char *bundled_machine_config = NULL;
569 * mono_register_config_for_assembly:
572 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
574 BundledConfig *bconfig;
576 bconfig = g_new0 (BundledConfig, 1);
577 bconfig->aname = assembly_name;
578 bconfig->config_xml = config_xml;
579 bconfig->next = bundled_configs;
580 bundled_configs = bconfig;
584 * mono_config_string_for_assembly_file:
587 mono_config_string_for_assembly_file (const char *filename)
589 BundledConfig *bconfig;
591 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
592 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
593 return bconfig->config_xml;
599 * mono_config_for_assembly:
602 mono_config_for_assembly (MonoImage *assembly)
604 ParseState state = {NULL};
606 char *aname, *cfg, *cfg_name;
607 const char *bundled_config;
609 state.assembly = assembly;
611 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
612 if (bundled_config) {
613 state.user_data = (gpointer) "<bundled>";
614 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
617 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
618 mono_config_parse_file_with_context (&state, cfg_name);
621 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
623 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
624 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
625 got_it += mono_config_parse_file_with_context (&state, cfg);
629 const char *home = g_get_home_dir ();
630 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
631 got_it += mono_config_parse_file_with_context (&state, cfg);
643 * \param filename the filename to load the configuration variables from.
644 * Pass a NULL filename to parse the default config files
645 * (or the file in the \c MONO_CONFIG env var).
648 mono_config_parse (const char *filename) {
656 mono_config_parse_file (filename);
660 // FIXME: leak, do we store any references to home
661 char *env_home = g_getenv ("MONO_CONFIG");
663 mono_config_parse_file (env_home);
667 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
668 mono_config_parse_file (mono_cfg);
671 #if !defined(TARGET_WIN32) && !defined(__native_client__)
672 home = g_get_home_dir ();
673 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
674 mono_config_parse_file (user_cfg);
680 * mono_set_config_dir:
681 * Invoked during startup
684 mono_set_config_dir (const char *dir)
686 /* If this environment variable is set, overrides the directory computed */
687 char *env_mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
688 if (env_mono_cfg_dir == NULL && dir != NULL)
689 env_mono_cfg_dir = strdup (dir);
691 mono_cfg_dir = env_mono_cfg_dir;
695 * mono_get_config_dir:
698 mono_get_config_dir (void)
700 if (mono_cfg_dir == NULL)
701 mono_set_dirs (NULL, NULL);
707 * mono_register_machine_config:
710 mono_register_machine_config (const char *config_xml)
712 bundled_machine_config = config_xml;
716 * mono_get_machine_config:
719 mono_get_machine_config (void)
721 return bundled_machine_config;
725 assembly_binding_end (gpointer user_data, const char *element_name)
727 ParserUserData *pud = (ParserUserData *)user_data;
729 if (!strcmp (element_name, "dependentAssembly")) {
730 if (pud->info_parsed && pud->info) {
731 pud->info_parsed (pud->info, pud->user_data);
732 g_free (pud->info->name);
733 g_free (pud->info->culture);
739 publisher_policy_start (gpointer user_data,
740 const gchar *element_name,
741 const gchar **attribute_names,
742 const gchar **attribute_values)
745 MonoAssemblyBindingInfo *info;
748 pud = (ParserUserData *)user_data;
750 if (!strcmp (element_name, "dependentAssembly")) {
752 info->culture = NULL;
753 info->has_old_version_bottom = FALSE;
754 info->has_old_version_top = FALSE;
755 info->has_new_version = FALSE;
756 info->is_valid = FALSE;
757 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
758 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
759 memset (&info->new_version, 0, sizeof (info->new_version));
760 } else if (!strcmp (element_name, "assemblyIdentity")) {
761 for (n = 0; attribute_names [n]; n++) {
762 const gchar *attribute_name = attribute_names [n];
764 if (!strcmp (attribute_name, "name"))
765 info->name = g_strdup (attribute_values [n]);
766 else if (!strcmp (attribute_name, "publicKeyToken")) {
767 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
768 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
769 } else if (!strcmp (attribute_name, "culture")) {
770 if (!strcmp (attribute_values [n], "neutral"))
771 info->culture = g_strdup ("");
773 info->culture = g_strdup (attribute_values [n]);
776 } else if (!strcmp (element_name, "bindingRedirect")) {
777 for (n = 0; attribute_names [n]; n++) {
778 const gchar *attribute_name = attribute_names [n];
780 if (!strcmp (attribute_name, "oldVersion")) {
781 gchar **numbers, **version, **versions;
782 gint major, minor, build, revision;
785 if (!strcmp (attribute_values [n], ""))
788 versions = g_strsplit (attribute_values [n], "-", 2);
789 version = g_strsplit (*versions, ".", 4);
791 /* We assign the values to gint vars to do the checks */
793 major = *numbers ? atoi (*numbers++) : -1;
794 minor = *numbers ? atoi (*numbers++) : -1;
795 build = *numbers ? atoi (*numbers++) : -1;
796 revision = *numbers ? atoi (*numbers) : -1;
797 g_strfreev (version);
798 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
799 g_strfreev (versions);
803 info->old_version_bottom.major = major;
804 info->old_version_bottom.minor = minor;
805 info->old_version_bottom.build = build;
806 info->old_version_bottom.revision = revision;
807 info->has_old_version_bottom = TRUE;
809 if (!*(versions + 1)) {
810 g_strfreev (versions);
814 numbers = version = g_strsplit (*(versions + 1), ".", 4);
815 major = *numbers ? atoi (*numbers++) : -1;
816 minor = *numbers ? atoi (*numbers++) : -1;
817 build = *numbers ? atoi (*numbers++) : -1;
818 revision = *numbers ? atoi (*numbers) : 1;
819 g_strfreev (version);
820 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
821 g_strfreev (versions);
825 info->old_version_top.major = major;
826 info->old_version_top.minor = minor;
827 info->old_version_top.build = build;
828 info->old_version_top.revision = revision;
829 info->has_old_version_top = TRUE;
831 g_strfreev (versions);
832 } else if (!strcmp (attribute_name, "newVersion")) {
833 gchar **numbers, **version;
836 if (!strcmp (attribute_values [n], ""))
839 numbers = version = g_strsplit (attribute_values [n], ".", 4);
840 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
841 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
842 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
843 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
844 info->has_new_version = TRUE;
845 g_strfreev (version);
851 static MonoParseHandler
852 publisher_policy_parser = {
853 "", /* We don't need to use declare an xml element */
855 publisher_policy_start,
862 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
864 ParserUserData user_data = {
870 &publisher_policy_parser, /* MonoParseHandler */
871 &user_data, /* user_data */
872 NULL, /* MonoImage (we don't need it right now)*/
873 TRUE /* We are already inited */
876 mono_config_parse_file_with_context (&state, filename);
879 static MonoParseHandler
880 config_assemblybinding_parser = {
881 "", /* We don't need to use declare an xml element */
883 publisher_policy_start,
885 assembly_binding_end,
890 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
892 MonoAssemblyBindingInfo info;
900 pud.info_parsed = infocb;
901 pud.user_data = user_data;
903 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
904 state.user_data = &pud;
905 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
906 state.inited = TRUE; /* We are already inited */
908 mono_config_parse_file_with_context (&state, filename);
911 static mono_bool mono_server_mode = FALSE;
914 * mono_config_set_server_mode:
917 mono_config_set_server_mode (mono_bool server_mode)
919 mono_server_mode = server_mode;
923 * mono_config_is_server_mode:
926 mono_config_is_server_mode (void)
928 return mono_server_mode;