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(__ia64__)
83 #define CONFIG_CPU "ia64"
84 #define CONFIG_WORDSIZE "64"
85 #elif defined(mips) || defined(__mips) || defined(_mips)
86 #define CONFIG_CPU "mips"
87 #define CONFIG_WORDSIZE "32"
90 #define CONFIG_CPU "unknownCPU"
97 * Returns the operating system that Mono is running on, as used for dllmap entries.
100 mono_config_get_os (void)
106 * mono_config_get_cpu:
108 * Returns the architecture that Mono is running on, as used for dllmap entries.
111 mono_config_get_cpu (void)
117 * mono_config_get_wordsize:
119 * Returns the word size that Mono is running on, as used for dllmap entries.
122 mono_config_get_wordsize (void)
124 return CONFIG_WORDSIZE;
127 static void start_element (GMarkupParseContext *context,
128 const gchar *element_name,
129 const gchar **attribute_names,
130 const gchar **attribute_values,
134 static void end_element (GMarkupParseContext *context,
135 const gchar *element_name,
139 static void parse_text (GMarkupParseContext *context,
145 static void passthrough (GMarkupParseContext *context,
151 static void parse_error (GMarkupParseContext *context,
155 static const GMarkupParser
164 static GHashTable *config_handlers;
166 static const char *mono_cfg_dir = NULL;
167 static char *mono_cfg_dir_allocated = NULL;
169 /* when this interface is stable, export it. */
170 typedef struct MonoParseHandler MonoParseHandler;
172 struct MonoParseHandler {
173 const char *element_name;
174 void*(*init) (MonoImage *assembly);
175 void (*start) (gpointer user_data, const gchar *name,
176 const gchar **attributes,
177 const gchar **values);
178 void (*text) (gpointer user_data, const char *text, gsize test_len);
179 void (*end) (gpointer user_data, const char *name);
180 void (*finish) (gpointer user_data);
184 MonoAssemblyBindingInfo *info;
185 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
190 MonoParseHandler *current;
196 static void start_element (GMarkupParseContext *context,
197 const gchar *element_name,
198 const gchar **attribute_names,
199 const gchar **attribute_values,
203 ParseState *state = (ParseState *)user_data;
204 if (!state->current) {
205 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
206 if (state->current && state->current->init)
207 state->user_data = state->current->init (state->assembly);
209 if (state->current && state->current->start)
210 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
213 static void end_element (GMarkupParseContext *context,
214 const gchar *element_name,
218 ParseState *state = (ParseState *)user_data;
219 if (state->current) {
220 if (state->current->end)
221 state->current->end (state->user_data, element_name);
222 if (strcmp (state->current->element_name, element_name) == 0) {
223 if (state->current->finish)
224 state->current->finish (state->user_data);
225 state->current = NULL;
226 state->user_data = NULL;
231 static void parse_text (GMarkupParseContext *context,
237 ParseState *state = (ParseState *)user_data;
238 if (state->current && state->current->text)
239 state->current->text (state->user_data, text, text_len);
242 static void passthrough (GMarkupParseContext *context,
251 static void parse_error (GMarkupParseContext *context,
255 ParseState *state = (ParseState *)user_data;
257 const gchar *filename;
259 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
260 msg = error && error->message ? error->message : "";
261 g_warning ("Error parsing %s: %s", filename, msg);
265 arch_matches (const char* arch, const char *value)
267 char **splitted, **p;
269 if (value [0] == '!')
270 return !arch_matches (arch, value + 1);
271 p = splitted = g_strsplit (value, ",", 0);
273 if (strcmp (arch, *p) == 0) {
279 g_strfreev (splitted);
291 dllmap_init (MonoImage *assembly) {
292 DllInfo *info = g_new0 (DllInfo, 1);
293 info->assembly = assembly;
298 dllmap_start (gpointer user_data,
299 const gchar *element_name,
300 const gchar **attribute_names,
301 const gchar **attribute_values)
304 DllInfo *info = (DllInfo *)user_data;
306 if (strcmp (element_name, "dllmap") == 0) {
308 g_free (info->target);
309 info->dll = info->target = NULL;
310 info->ignore = FALSE;
311 for (i = 0; attribute_names [i]; ++i) {
312 if (strcmp (attribute_names [i], "dll") == 0)
313 info->dll = g_strdup (attribute_values [i]);
314 else if (strcmp (attribute_names [i], "target") == 0){
315 char *p = strstr (attribute_values [i], "$mono_libdir");
317 char *libdir = mono_native_getrootdir ();
318 size_t libdir_len = strlen (libdir);
321 result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
322 strncpy (result, attribute_values[i], p-attribute_values[i]);
323 strcpy (result+(p-attribute_values[i]), libdir);
325 strcat (result, p+strlen("$mono_libdir"));
326 info->target = result;
328 info->target = g_strdup (attribute_values [i]);
329 } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
331 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
333 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
337 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
338 } else if (strcmp (element_name, "dllentry") == 0) {
339 const char *name = NULL, *target = NULL, *dll = NULL;
341 for (i = 0; attribute_names [i]; ++i) {
342 if (strcmp (attribute_names [i], "dll") == 0)
343 dll = attribute_values [i];
344 else if (strcmp (attribute_names [i], "target") == 0)
345 target = attribute_values [i];
346 else if (strcmp (attribute_names [i], "name") == 0)
347 name = attribute_values [i];
348 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
350 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
352 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
357 if (!info->ignore && !ignore)
358 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
363 dllmap_finish (gpointer user_data)
365 DllInfo *info = (DllInfo *)user_data;
368 g_free (info->target);
372 static const MonoParseHandler
383 legacyUEP_start (gpointer user_data,
384 const gchar *element_name,
385 const gchar **attribute_names,
386 const gchar **attribute_values) {
387 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
388 (attribute_names [0] != NULL) &&
389 (strcmp (attribute_names [0], "enabled") == 0)) {
390 if ((strcmp (attribute_values [0], "1") == 0) ||
391 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
392 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
397 static const MonoParseHandler
398 legacyUEP_handler = {
399 "legacyUnhandledExceptionPolicy",
408 aot_cache_start (gpointer user_data,
409 const gchar *element_name,
410 const gchar **attribute_names,
411 const gchar **attribute_values)
414 MonoAotCacheConfig *config;
416 if (strcmp (element_name, "aotcache") != 0)
419 config = mono_get_aot_cache_config ();
421 /* Per-app configuration */
422 for (i = 0; attribute_names [i]; ++i) {
423 if (!strcmp (attribute_names [i], "app")) {
424 config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
428 /* Global configuration */
429 for (i = 0; attribute_names [i]; ++i) {
430 if (!strcmp (attribute_names [i], "assemblies")) {
434 parts = g_strsplit (attribute_values [i], " ", -1);
435 for (ptr = parts; ptr && *ptr; ptr ++) {
437 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
440 } else if (!strcmp (attribute_names [i], "options")) {
441 config->aot_options = g_strdup (attribute_values [i]);
446 static const MonoParseHandler
447 aot_cache_handler = {
456 static int inited = 0;
459 mono_config_init (void)
462 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
463 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
464 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
465 g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
469 mono_config_cleanup (void)
472 g_hash_table_destroy (config_handlers);
473 g_free (mono_cfg_dir_allocated);
476 /* FIXME: error handling */
479 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
481 GMarkupParseContext *context;
486 context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
487 if (g_markup_parse_context_parse (context, text, len, NULL)) {
488 g_markup_parse_context_end_parse (context, NULL);
490 g_markup_parse_context_free (context);
493 /* If assembly is NULL, parse in the global context */
495 mono_config_parse_file_with_context (ParseState *state, const char *filename)
501 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
502 "Config attempting to parse: '%s'.", filename);
504 if (!g_file_get_contents (filename, &text, &len, NULL))
508 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
509 offset = 3; /* Skip UTF-8 BOM */
510 if (state->user_data == NULL)
511 state->user_data = (gpointer) filename;
512 mono_config_parse_xml_with_context (state, text + offset, len - offset);
518 * mono_config_parse_memory:
519 * @buffer: a pointer to an string XML representation of the configuration
521 * Parses the configuration from a buffer
524 mono_config_parse_memory (const char *buffer)
526 ParseState state = {NULL};
528 state.user_data = (gpointer) "<buffer>";
529 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
533 mono_config_parse_file (const char *filename)
535 ParseState state = {NULL};
536 state.user_data = (gpointer) filename;
537 mono_config_parse_file_with_context (&state, filename);
541 * use the equivalent lookup code from the GAC when available.
542 * Depending on state, this should give something like:
543 * aname/version-pubtoken/
548 get_assembly_filename (MonoImage *image, int state)
552 return g_strdup (mono_image_get_name (image));
558 typedef struct _BundledConfig BundledConfig;
560 struct _BundledConfig {
563 const char* config_xml;
566 static BundledConfig *bundled_configs = NULL;
568 static const char *bundled_machine_config = NULL;
571 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
573 BundledConfig *bconfig;
575 bconfig = g_new0 (BundledConfig, 1);
576 bconfig->aname = assembly_name;
577 bconfig->config_xml = config_xml;
578 bconfig->next = bundled_configs;
579 bundled_configs = bconfig;
583 mono_config_string_for_assembly_file (const char *filename)
585 BundledConfig *bconfig;
587 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
588 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
589 return bconfig->config_xml;
595 mono_config_for_assembly (MonoImage *assembly)
597 ParseState state = {NULL};
599 char *aname, *cfg, *cfg_name;
600 const char *bundled_config;
602 state.assembly = assembly;
604 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
605 if (bundled_config) {
606 state.user_data = (gpointer) "<bundled>";
607 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
610 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
611 mono_config_parse_file_with_context (&state, cfg_name);
614 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
616 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
617 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
618 got_it += mono_config_parse_file_with_context (&state, cfg);
622 const char *home = g_get_home_dir ();
623 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
624 got_it += mono_config_parse_file_with_context (&state, cfg);
636 * @filename: the filename to load the configuration variables from.
638 * Pass a NULL filename to parse the default config files
639 * (or the file in the MONO_CONFIG env var).
642 mono_config_parse (const char *filename) {
650 mono_config_parse_file (filename);
654 home = g_getenv ("MONO_CONFIG");
656 mono_config_parse_file (home);
660 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
661 mono_config_parse_file (mono_cfg);
664 #if !defined(TARGET_WIN32) && !defined(__native_client__)
665 home = g_get_home_dir ();
666 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
667 mono_config_parse_file (user_cfg);
672 /* Invoked during startup */
674 mono_set_config_dir (const char *dir)
676 /* If this variable is set, overrides the directory computed */
677 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
678 if (mono_cfg_dir == NULL)
679 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
683 mono_get_config_dir (void)
685 if (mono_cfg_dir == NULL)
686 mono_set_dirs (NULL, NULL);
692 mono_register_machine_config (const char *config_xml)
694 bundled_machine_config = config_xml;
698 mono_get_machine_config (void)
700 return bundled_machine_config;
704 assembly_binding_end (gpointer user_data, const char *element_name)
706 ParserUserData *pud = (ParserUserData *)user_data;
708 if (!strcmp (element_name, "dependentAssembly")) {
709 if (pud->info_parsed && pud->info) {
710 pud->info_parsed (pud->info, pud->user_data);
711 g_free (pud->info->name);
712 g_free (pud->info->culture);
718 publisher_policy_start (gpointer user_data,
719 const gchar *element_name,
720 const gchar **attribute_names,
721 const gchar **attribute_values)
724 MonoAssemblyBindingInfo *info;
727 pud = (ParserUserData *)user_data;
729 if (!strcmp (element_name, "dependentAssembly")) {
731 info->culture = NULL;
732 info->has_old_version_bottom = FALSE;
733 info->has_old_version_top = FALSE;
734 info->has_new_version = FALSE;
735 info->is_valid = FALSE;
736 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
737 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
738 memset (&info->new_version, 0, sizeof (info->new_version));
739 } else if (!strcmp (element_name, "assemblyIdentity")) {
740 for (n = 0; attribute_names [n]; n++) {
741 const gchar *attribute_name = attribute_names [n];
743 if (!strcmp (attribute_name, "name"))
744 info->name = g_strdup (attribute_values [n]);
745 else if (!strcmp (attribute_name, "publicKeyToken")) {
746 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
747 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
748 } else if (!strcmp (attribute_name, "culture")) {
749 if (!strcmp (attribute_values [n], "neutral"))
750 info->culture = g_strdup ("");
752 info->culture = g_strdup (attribute_values [n]);
755 } else if (!strcmp (element_name, "bindingRedirect")) {
756 for (n = 0; attribute_names [n]; n++) {
757 const gchar *attribute_name = attribute_names [n];
759 if (!strcmp (attribute_name, "oldVersion")) {
760 gchar **numbers, **version, **versions;
761 gint major, minor, build, revision;
764 if (!strcmp (attribute_values [n], ""))
767 versions = g_strsplit (attribute_values [n], "-", 2);
768 version = g_strsplit (*versions, ".", 4);
770 /* We assign the values to gint vars to do the checks */
772 major = *numbers ? atoi (*numbers++) : -1;
773 minor = *numbers ? atoi (*numbers++) : -1;
774 build = *numbers ? atoi (*numbers++) : -1;
775 revision = *numbers ? atoi (*numbers) : -1;
776 g_strfreev (version);
777 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
778 g_strfreev (versions);
782 info->old_version_bottom.major = major;
783 info->old_version_bottom.minor = minor;
784 info->old_version_bottom.build = build;
785 info->old_version_bottom.revision = revision;
786 info->has_old_version_bottom = TRUE;
788 if (!*(versions + 1)) {
789 g_strfreev (versions);
793 numbers = version = g_strsplit (*(versions + 1), ".", 4);
794 major = *numbers ? atoi (*numbers++) : -1;
795 minor = *numbers ? atoi (*numbers++) : -1;
796 build = *numbers ? atoi (*numbers++) : -1;
797 revision = *numbers ? atoi (*numbers) : 1;
798 g_strfreev (version);
799 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
800 g_strfreev (versions);
804 info->old_version_top.major = major;
805 info->old_version_top.minor = minor;
806 info->old_version_top.build = build;
807 info->old_version_top.revision = revision;
808 info->has_old_version_top = TRUE;
810 g_strfreev (versions);
811 } else if (!strcmp (attribute_name, "newVersion")) {
812 gchar **numbers, **version;
815 if (!strcmp (attribute_values [n], ""))
818 numbers = version = g_strsplit (attribute_values [n], ".", 4);
819 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
820 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
821 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
822 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
823 info->has_new_version = TRUE;
824 g_strfreev (version);
830 static MonoParseHandler
831 publisher_policy_parser = {
832 "", /* We don't need to use declare an xml element */
834 publisher_policy_start,
841 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
843 ParserUserData user_data = {
849 &publisher_policy_parser, /* MonoParseHandler */
850 &user_data, /* user_data */
851 NULL, /* MonoImage (we don't need it right now)*/
852 TRUE /* We are already inited */
855 mono_config_parse_file_with_context (&state, filename);
858 static MonoParseHandler
859 config_assemblybinding_parser = {
860 "", /* We don't need to use declare an xml element */
862 publisher_policy_start,
864 assembly_binding_end,
869 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
871 MonoAssemblyBindingInfo info;
879 pud.info_parsed = infocb;
880 pud.user_data = user_data;
882 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
883 state.user_data = &pud;
884 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
885 state.inited = TRUE; /* We are already inited */
887 mono_config_parse_file_with_context (&state, filename);
890 static mono_bool mono_server_mode = FALSE;
893 mono_config_set_server_mode (mono_bool server_mode)
895 mono_server_mode = server_mode;
899 mono_config_is_server_mode (void)
901 return mono_server_mode;