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"
45 #elif defined (TARGET_WASM)
46 #define CONFIG_OS "wasm"
48 #warning Unknown operating system
49 #define CONFIG_OS "unknownOS"
53 #if defined(__i386__) || defined(TARGET_X86)
54 #define CONFIG_CPU "x86"
55 #define CONFIG_WORDSIZE "32"
56 #elif defined(__x86_64__) || defined(TARGET_AMD64)
57 #define CONFIG_CPU "x86-64"
58 #define CONFIG_WORDSIZE "64"
59 #elif defined(sparc) || defined(__sparc__)
60 #define CONFIG_CPU "sparc"
61 #define CONFIG_WORDSIZE "32"
62 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
63 #define CONFIG_WORDSIZE "64"
64 #ifdef __mono_ppc_ilp32__
65 # define CONFIG_CPU "ppc64ilp32"
67 # define CONFIG_CPU "ppc64"
69 #elif defined(__ppc__) || defined(__powerpc__)
70 #define CONFIG_CPU "ppc"
71 #define CONFIG_WORDSIZE "32"
72 #elif defined(__s390x__)
73 #define CONFIG_CPU "s390x"
74 #define CONFIG_WORDSIZE "64"
75 #elif defined(__s390__)
76 #define CONFIG_CPU "s390"
77 #define CONFIG_WORDSIZE "32"
78 #elif defined(__arm__)
79 #define CONFIG_CPU "arm"
80 #define CONFIG_WORDSIZE "32"
81 #elif defined(__aarch64__)
82 #define CONFIG_CPU "armv8"
83 #define CONFIG_WORDSIZE "64"
84 #elif defined(mips) || defined(__mips) || defined(_mips)
85 #define CONFIG_CPU "mips"
86 #define CONFIG_WORDSIZE "32"
87 #elif defined(TARGET_WASM)
88 #define CONFIG_CPU "wasm"
89 #define CONFIG_WORDSIZE "32"
92 #define CONFIG_CPU "unknownCPU"
99 * Returns the operating system that Mono is running on, as used for dllmap entries.
102 mono_config_get_os (void)
108 * mono_config_get_cpu:
110 * Returns the architecture that Mono is running on, as used for dllmap entries.
113 mono_config_get_cpu (void)
119 * mono_config_get_wordsize:
121 * Returns the word size that Mono is running on, as used for dllmap entries.
124 mono_config_get_wordsize (void)
126 return CONFIG_WORDSIZE;
129 static void start_element (GMarkupParseContext *context,
130 const gchar *element_name,
131 const gchar **attribute_names,
132 const gchar **attribute_values,
136 static void end_element (GMarkupParseContext *context,
137 const gchar *element_name,
141 static void parse_text (GMarkupParseContext *context,
147 static void passthrough (GMarkupParseContext *context,
153 static void parse_error (GMarkupParseContext *context,
157 static const GMarkupParser
166 static GHashTable *config_handlers;
168 static char *mono_cfg_dir = NULL;
170 /* when this interface is stable, export it. */
171 typedef struct MonoParseHandler MonoParseHandler;
173 struct MonoParseHandler {
174 const char *element_name;
175 void*(*init) (MonoImage *assembly);
176 void (*start) (gpointer user_data, const gchar *name,
177 const gchar **attributes,
178 const gchar **values);
179 void (*text) (gpointer user_data, const char *text, gsize test_len);
180 void (*end) (gpointer user_data, const char *name);
181 void (*finish) (gpointer user_data);
185 MonoAssemblyBindingInfo *info;
186 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
191 MonoParseHandler *current;
197 static void start_element (GMarkupParseContext *context,
198 const gchar *element_name,
199 const gchar **attribute_names,
200 const gchar **attribute_values,
204 ParseState *state = (ParseState *)user_data;
205 if (!state->current) {
206 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
207 if (state->current && state->current->init)
208 state->user_data = state->current->init (state->assembly);
210 if (state->current && state->current->start)
211 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
214 static void end_element (GMarkupParseContext *context,
215 const gchar *element_name,
219 ParseState *state = (ParseState *)user_data;
220 if (state->current) {
221 if (state->current->end)
222 state->current->end (state->user_data, element_name);
223 if (strcmp (state->current->element_name, element_name) == 0) {
224 if (state->current->finish)
225 state->current->finish (state->user_data);
226 state->current = NULL;
227 state->user_data = NULL;
232 static void parse_text (GMarkupParseContext *context,
238 ParseState *state = (ParseState *)user_data;
239 if (state->current && state->current->text)
240 state->current->text (state->user_data, text, text_len);
243 static void passthrough (GMarkupParseContext *context,
252 static void parse_error (GMarkupParseContext *context,
256 ParseState *state = (ParseState *)user_data;
258 const gchar *filename;
260 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
261 msg = error && error->message ? error->message : "";
262 g_warning ("Error parsing %s: %s", filename, msg);
266 arch_matches (const char* arch, const char *value)
268 char **splitted, **p;
270 if (value [0] == '!')
271 return !arch_matches (arch, value + 1);
272 p = splitted = g_strsplit (value, ",", 0);
274 if (strcmp (arch, *p) == 0) {
280 g_strfreev (splitted);
292 dllmap_init (MonoImage *assembly) {
293 DllInfo *info = g_new0 (DllInfo, 1);
294 info->assembly = assembly;
299 dllmap_start (gpointer user_data,
300 const gchar *element_name,
301 const gchar **attribute_names,
302 const gchar **attribute_values)
305 DllInfo *info = (DllInfo *)user_data;
307 if (strcmp (element_name, "dllmap") == 0) {
309 g_free (info->target);
310 info->dll = info->target = NULL;
311 info->ignore = FALSE;
312 for (i = 0; attribute_names [i]; ++i) {
313 if (strcmp (attribute_names [i], "dll") == 0)
314 info->dll = g_strdup (attribute_values [i]);
315 else if (strcmp (attribute_names [i], "target") == 0){
316 char *p = strstr (attribute_values [i], "$mono_libdir");
318 char *libdir = mono_native_getrootdir ();
319 size_t libdir_len = strlen (libdir);
322 result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
323 strncpy (result, attribute_values[i], p-attribute_values[i]);
324 strcpy (result+(p-attribute_values[i]), libdir);
326 strcat (result, p+strlen("$mono_libdir"));
327 info->target = result;
329 info->target = g_strdup (attribute_values [i]);
330 } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
332 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
334 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
338 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
339 } else if (strcmp (element_name, "dllentry") == 0) {
340 const char *name = NULL, *target = NULL, *dll = NULL;
342 for (i = 0; attribute_names [i]; ++i) {
343 if (strcmp (attribute_names [i], "dll") == 0)
344 dll = attribute_values [i];
345 else if (strcmp (attribute_names [i], "target") == 0)
346 target = attribute_values [i];
347 else if (strcmp (attribute_names [i], "name") == 0)
348 name = attribute_values [i];
349 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
351 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
353 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
358 if (!info->ignore && !ignore)
359 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
364 dllmap_finish (gpointer user_data)
366 DllInfo *info = (DllInfo *)user_data;
369 g_free (info->target);
373 static const MonoParseHandler
384 legacyUEP_start (gpointer user_data,
385 const gchar *element_name,
386 const gchar **attribute_names,
387 const gchar **attribute_values) {
388 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
389 (attribute_names [0] != NULL) &&
390 (strcmp (attribute_names [0], "enabled") == 0)) {
391 if ((strcmp (attribute_values [0], "1") == 0) ||
392 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
393 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
398 static const MonoParseHandler
399 legacyUEP_handler = {
400 "legacyUnhandledExceptionPolicy",
409 aot_cache_start (gpointer user_data,
410 const gchar *element_name,
411 const gchar **attribute_names,
412 const gchar **attribute_values)
415 MonoAotCacheConfig *config;
417 if (strcmp (element_name, "aotcache") != 0)
420 config = mono_get_aot_cache_config ();
422 /* Per-app configuration */
423 for (i = 0; attribute_names [i]; ++i) {
424 if (!strcmp (attribute_names [i], "app")) {
425 config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
429 /* Global configuration */
430 for (i = 0; attribute_names [i]; ++i) {
431 if (!strcmp (attribute_names [i], "assemblies")) {
435 parts = g_strsplit (attribute_values [i], " ", -1);
436 for (ptr = parts; ptr && *ptr; ptr ++) {
438 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
441 } else if (!strcmp (attribute_names [i], "options")) {
442 config->aot_options = g_strdup (attribute_values [i]);
447 static const MonoParseHandler
448 aot_cache_handler = {
457 static int inited = 0;
460 mono_config_init (void)
463 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
464 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
465 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
466 g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
470 * mono_config_cleanup:
473 mono_config_cleanup (void)
476 g_hash_table_destroy (config_handlers);
477 g_free (mono_cfg_dir);
480 /* FIXME: error handling */
483 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
485 GMarkupParseContext *context;
490 context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
491 if (g_markup_parse_context_parse (context, text, len, NULL)) {
492 g_markup_parse_context_end_parse (context, NULL);
494 g_markup_parse_context_free (context);
497 /* If assembly is NULL, parse in the global context */
499 mono_config_parse_file_with_context (ParseState *state, const char *filename)
505 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
506 "Config attempting to parse: '%s'.", filename);
508 if (!g_file_get_contents (filename, &text, &len, NULL))
512 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
513 offset = 3; /* Skip UTF-8 BOM */
514 if (state->user_data == NULL)
515 state->user_data = (gpointer) filename;
516 mono_config_parse_xml_with_context (state, text + offset, len - offset);
522 * mono_config_parse_memory:
523 * \param buffer a pointer to an string XML representation of the configuration
524 * Parses the configuration from a buffer
527 mono_config_parse_memory (const char *buffer)
529 ParseState state = {NULL};
531 state.user_data = (gpointer) "<buffer>";
532 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
536 mono_config_parse_file (const char *filename)
538 ParseState state = {NULL};
539 state.user_data = (gpointer) filename;
540 mono_config_parse_file_with_context (&state, filename);
544 * use the equivalent lookup code from the GAC when available.
545 * Depending on state, this should give something like:
546 * aname/version-pubtoken/
551 get_assembly_filename (MonoImage *image, int state)
555 return g_strdup (mono_image_get_name (image));
561 typedef struct _BundledConfig BundledConfig;
563 struct _BundledConfig {
566 const char* config_xml;
569 static BundledConfig *bundled_configs = NULL;
571 static const char *bundled_machine_config = NULL;
574 * mono_register_config_for_assembly:
577 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
579 BundledConfig *bconfig;
581 bconfig = g_new0 (BundledConfig, 1);
582 bconfig->aname = assembly_name;
583 bconfig->config_xml = config_xml;
584 bconfig->next = bundled_configs;
585 bundled_configs = bconfig;
589 * mono_config_string_for_assembly_file:
592 mono_config_string_for_assembly_file (const char *filename)
594 BundledConfig *bconfig;
596 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
597 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
598 return bconfig->config_xml;
604 * mono_config_for_assembly:
607 mono_config_for_assembly (MonoImage *assembly)
609 ParseState state = {NULL};
611 char *aname, *cfg, *cfg_name;
612 const char *bundled_config;
614 state.assembly = assembly;
616 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
617 if (bundled_config) {
618 state.user_data = (gpointer) "<bundled>";
619 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
622 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
623 mono_config_parse_file_with_context (&state, cfg_name);
626 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
628 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
629 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
630 got_it += mono_config_parse_file_with_context (&state, cfg);
634 const char *home = g_get_home_dir ();
635 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
636 got_it += mono_config_parse_file_with_context (&state, cfg);
648 * \param filename the filename to load the configuration variables from.
649 * Pass a NULL filename to parse the default config files
650 * (or the file in the \c MONO_CONFIG env var).
653 mono_config_parse (const char *filename) {
661 mono_config_parse_file (filename);
665 // FIXME: leak, do we store any references to home
666 char *env_home = g_getenv ("MONO_CONFIG");
668 mono_config_parse_file (env_home);
672 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
673 mono_config_parse_file (mono_cfg);
676 #if !defined(TARGET_WIN32)
677 home = g_get_home_dir ();
678 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
679 mono_config_parse_file (user_cfg);
685 * mono_set_config_dir:
686 * Invoked during startup
689 mono_set_config_dir (const char *dir)
691 /* If this environment variable is set, overrides the directory computed */
692 char *env_mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
693 if (env_mono_cfg_dir == NULL && dir != NULL)
694 env_mono_cfg_dir = g_strdup (dir);
696 mono_cfg_dir = env_mono_cfg_dir;
700 * mono_get_config_dir:
703 mono_get_config_dir (void)
705 if (mono_cfg_dir == NULL)
706 mono_set_dirs (NULL, NULL);
712 * mono_register_machine_config:
715 mono_register_machine_config (const char *config_xml)
717 bundled_machine_config = config_xml;
721 * mono_get_machine_config:
724 mono_get_machine_config (void)
726 return bundled_machine_config;
730 assembly_binding_end (gpointer user_data, const char *element_name)
732 ParserUserData *pud = (ParserUserData *)user_data;
734 if (!strcmp (element_name, "dependentAssembly")) {
735 if (pud->info_parsed && pud->info) {
736 pud->info_parsed (pud->info, pud->user_data);
737 g_free (pud->info->name);
738 g_free (pud->info->culture);
744 publisher_policy_start (gpointer user_data,
745 const gchar *element_name,
746 const gchar **attribute_names,
747 const gchar **attribute_values)
750 MonoAssemblyBindingInfo *info;
753 pud = (ParserUserData *)user_data;
755 if (!strcmp (element_name, "dependentAssembly")) {
757 info->culture = NULL;
758 info->has_old_version_bottom = FALSE;
759 info->has_old_version_top = FALSE;
760 info->has_new_version = FALSE;
761 info->is_valid = FALSE;
762 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
763 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
764 memset (&info->new_version, 0, sizeof (info->new_version));
765 } else if (!strcmp (element_name, "assemblyIdentity")) {
766 for (n = 0; attribute_names [n]; n++) {
767 const gchar *attribute_name = attribute_names [n];
769 if (!strcmp (attribute_name, "name"))
770 info->name = g_strdup (attribute_values [n]);
771 else if (!strcmp (attribute_name, "publicKeyToken")) {
772 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
773 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
774 } else if (!strcmp (attribute_name, "culture")) {
775 if (!strcmp (attribute_values [n], "neutral"))
776 info->culture = g_strdup ("");
778 info->culture = g_strdup (attribute_values [n]);
781 } else if (!strcmp (element_name, "bindingRedirect")) {
782 for (n = 0; attribute_names [n]; n++) {
783 const gchar *attribute_name = attribute_names [n];
785 if (!strcmp (attribute_name, "oldVersion")) {
786 gchar **numbers, **version, **versions;
787 gint major, minor, build, revision;
790 if (!strcmp (attribute_values [n], ""))
793 versions = g_strsplit (attribute_values [n], "-", 2);
794 version = g_strsplit (*versions, ".", 4);
796 /* We assign the values to gint vars to do the checks */
798 major = *numbers ? atoi (*numbers++) : -1;
799 minor = *numbers ? atoi (*numbers++) : -1;
800 build = *numbers ? atoi (*numbers++) : -1;
801 revision = *numbers ? atoi (*numbers) : -1;
802 g_strfreev (version);
803 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
804 g_strfreev (versions);
808 info->old_version_bottom.major = major;
809 info->old_version_bottom.minor = minor;
810 info->old_version_bottom.build = build;
811 info->old_version_bottom.revision = revision;
812 info->has_old_version_bottom = TRUE;
814 if (!*(versions + 1)) {
815 g_strfreev (versions);
819 numbers = version = g_strsplit (*(versions + 1), ".", 4);
820 major = *numbers ? atoi (*numbers++) : -1;
821 minor = *numbers ? atoi (*numbers++) : -1;
822 build = *numbers ? atoi (*numbers++) : -1;
823 revision = *numbers ? atoi (*numbers) : 1;
824 g_strfreev (version);
825 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
826 g_strfreev (versions);
830 info->old_version_top.major = major;
831 info->old_version_top.minor = minor;
832 info->old_version_top.build = build;
833 info->old_version_top.revision = revision;
834 info->has_old_version_top = TRUE;
836 g_strfreev (versions);
837 } else if (!strcmp (attribute_name, "newVersion")) {
838 gchar **numbers, **version;
841 if (!strcmp (attribute_values [n], ""))
844 numbers = version = g_strsplit (attribute_values [n], ".", 4);
845 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
846 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
847 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
848 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
849 info->has_new_version = TRUE;
850 g_strfreev (version);
856 static MonoParseHandler
857 publisher_policy_parser = {
858 "", /* We don't need to use declare an xml element */
860 publisher_policy_start,
867 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
869 ParserUserData user_data = {
875 &publisher_policy_parser, /* MonoParseHandler */
876 &user_data, /* user_data */
877 NULL, /* MonoImage (we don't need it right now)*/
878 TRUE /* We are already inited */
881 mono_config_parse_file_with_context (&state, filename);
884 static MonoParseHandler
885 config_assemblybinding_parser = {
886 "", /* We don't need to use declare an xml element */
888 publisher_policy_start,
890 assembly_binding_end,
895 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
897 MonoAssemblyBindingInfo info;
905 pud.info_parsed = infocb;
906 pud.user_data = user_data;
908 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
909 state.user_data = &pud;
910 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
911 state.inited = TRUE; /* We are already inited */
913 mono_config_parse_file_with_context (&state, filename);
916 static mono_bool mono_server_mode = FALSE;
919 * mono_config_set_server_mode:
922 mono_config_set_server_mode (mono_bool server_mode)
924 mono_server_mode = server_mode;
928 * mono_config_is_server_mode:
931 mono_config_is_server_mode (void)
933 return mono_server_mode;