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"
94 static void start_element (GMarkupParseContext *context,
95 const gchar *element_name,
96 const gchar **attribute_names,
97 const gchar **attribute_values,
101 static void end_element (GMarkupParseContext *context,
102 const gchar *element_name,
106 static void parse_text (GMarkupParseContext *context,
112 static void passthrough (GMarkupParseContext *context,
118 static void parse_error (GMarkupParseContext *context,
122 static const GMarkupParser
131 static GHashTable *config_handlers;
133 static const char *mono_cfg_dir = NULL;
134 static char *mono_cfg_dir_allocated = NULL;
136 /* when this interface is stable, export it. */
137 typedef struct MonoParseHandler MonoParseHandler;
139 struct MonoParseHandler {
140 const char *element_name;
141 void*(*init) (MonoImage *assembly);
142 void (*start) (gpointer user_data, const gchar *name,
143 const gchar **attributes,
144 const gchar **values);
145 void (*text) (gpointer user_data, const char *text, gsize test_len);
146 void (*end) (gpointer user_data, const char *name);
147 void (*finish) (gpointer user_data);
151 MonoAssemblyBindingInfo *info;
152 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
157 MonoParseHandler *current;
163 static void start_element (GMarkupParseContext *context,
164 const gchar *element_name,
165 const gchar **attribute_names,
166 const gchar **attribute_values,
170 ParseState *state = (ParseState *)user_data;
171 if (!state->current) {
172 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
173 if (state->current && state->current->init)
174 state->user_data = state->current->init (state->assembly);
176 if (state->current && state->current->start)
177 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
180 static void end_element (GMarkupParseContext *context,
181 const gchar *element_name,
185 ParseState *state = (ParseState *)user_data;
186 if (state->current) {
187 if (state->current->end)
188 state->current->end (state->user_data, element_name);
189 if (strcmp (state->current->element_name, element_name) == 0) {
190 if (state->current->finish)
191 state->current->finish (state->user_data);
192 state->current = NULL;
193 state->user_data = NULL;
198 static void parse_text (GMarkupParseContext *context,
204 ParseState *state = (ParseState *)user_data;
205 if (state->current && state->current->text)
206 state->current->text (state->user_data, text, text_len);
209 static void passthrough (GMarkupParseContext *context,
218 static void parse_error (GMarkupParseContext *context,
222 ParseState *state = (ParseState *)user_data;
224 const gchar *filename;
226 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
227 msg = error && error->message ? error->message : "";
228 g_warning ("Error parsing %s: %s", filename, msg);
232 arch_matches (const char* arch, const char *value)
234 char **splitted, **p;
236 if (value [0] == '!')
237 return !arch_matches (arch, value + 1);
238 p = splitted = g_strsplit (value, ",", 0);
240 if (strcmp (arch, *p) == 0) {
246 g_strfreev (splitted);
258 dllmap_init (MonoImage *assembly) {
259 DllInfo *info = g_new0 (DllInfo, 1);
260 info->assembly = assembly;
265 dllmap_start (gpointer user_data,
266 const gchar *element_name,
267 const gchar **attribute_names,
268 const gchar **attribute_values)
271 DllInfo *info = (DllInfo *)user_data;
273 if (strcmp (element_name, "dllmap") == 0) {
275 g_free (info->target);
276 info->dll = info->target = NULL;
277 info->ignore = FALSE;
278 for (i = 0; attribute_names [i]; ++i) {
279 if (strcmp (attribute_names [i], "dll") == 0)
280 info->dll = g_strdup (attribute_values [i]);
281 else if (strcmp (attribute_names [i], "target") == 0){
282 char *p = strstr (attribute_values [i], "$mono_libdir");
284 const char *libdir = mono_assembly_getrootdir ();
285 size_t libdir_len = strlen (libdir);
288 result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
289 strncpy (result, attribute_values[i], p-attribute_values[i]);
290 strcpy (result+(p-attribute_values[i]), libdir);
291 strcat (result, p+strlen("$mono_libdir"));
292 info->target = result;
294 info->target = g_strdup (attribute_values [i]);
295 } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
297 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
299 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
303 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
304 } else if (strcmp (element_name, "dllentry") == 0) {
305 const char *name = NULL, *target = NULL, *dll = NULL;
307 for (i = 0; attribute_names [i]; ++i) {
308 if (strcmp (attribute_names [i], "dll") == 0)
309 dll = attribute_values [i];
310 else if (strcmp (attribute_names [i], "target") == 0)
311 target = attribute_values [i];
312 else if (strcmp (attribute_names [i], "name") == 0)
313 name = attribute_values [i];
314 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
316 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
318 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
323 if (!info->ignore && !ignore)
324 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
329 dllmap_finish (gpointer user_data)
331 DllInfo *info = (DllInfo *)user_data;
334 g_free (info->target);
338 static const MonoParseHandler
349 legacyUEP_start (gpointer user_data,
350 const gchar *element_name,
351 const gchar **attribute_names,
352 const gchar **attribute_values) {
353 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
354 (attribute_names [0] != NULL) &&
355 (strcmp (attribute_names [0], "enabled") == 0)) {
356 if ((strcmp (attribute_values [0], "1") == 0) ||
357 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
358 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
363 static const MonoParseHandler
364 legacyUEP_handler = {
365 "legacyUnhandledExceptionPolicy",
374 aot_cache_start (gpointer user_data,
375 const gchar *element_name,
376 const gchar **attribute_names,
377 const gchar **attribute_values)
380 MonoAotCacheConfig *config;
382 if (strcmp (element_name, "aotcache") != 0)
385 config = mono_get_aot_cache_config ();
387 /* Per-app configuration */
388 for (i = 0; attribute_names [i]; ++i) {
389 if (!strcmp (attribute_names [i], "app")) {
390 config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
394 /* Global configuration */
395 for (i = 0; attribute_names [i]; ++i) {
396 if (!strcmp (attribute_names [i], "assemblies")) {
400 parts = g_strsplit (attribute_values [i], " ", -1);
401 for (ptr = parts; ptr && *ptr; ptr ++) {
403 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
406 } else if (!strcmp (attribute_names [i], "options")) {
407 config->aot_options = g_strdup (attribute_values [i]);
412 static const MonoParseHandler
413 aot_cache_handler = {
422 static int inited = 0;
425 mono_config_init (void)
428 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
429 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
430 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
431 g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
435 mono_config_cleanup (void)
438 g_hash_table_destroy (config_handlers);
439 g_free (mono_cfg_dir_allocated);
442 /* FIXME: error handling */
445 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
447 GMarkupParseContext *context;
452 context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
453 if (g_markup_parse_context_parse (context, text, len, NULL)) {
454 g_markup_parse_context_end_parse (context, NULL);
456 g_markup_parse_context_free (context);
459 /* If assembly is NULL, parse in the global context */
461 mono_config_parse_file_with_context (ParseState *state, const char *filename)
467 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
468 "Config attempting to parse: '%s'.", filename);
470 if (!g_file_get_contents (filename, &text, &len, NULL))
474 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
475 offset = 3; /* Skip UTF-8 BOM */
476 if (state->user_data == NULL)
477 state->user_data = (gpointer) filename;
478 mono_config_parse_xml_with_context (state, text + offset, len - offset);
484 * mono_config_parse_memory:
485 * @buffer: a pointer to an string XML representation of the configuration
487 * Parses the configuration from a buffer
490 mono_config_parse_memory (const char *buffer)
492 ParseState state = {NULL};
494 state.user_data = (gpointer) "<buffer>";
495 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
499 mono_config_parse_file (const char *filename)
501 ParseState state = {NULL};
502 state.user_data = (gpointer) filename;
503 mono_config_parse_file_with_context (&state, filename);
507 * use the equivalent lookup code from the GAC when available.
508 * Depending on state, this should give something like:
509 * aname/version-pubtoken/
514 get_assembly_filename (MonoImage *image, int state)
518 return g_strdup (mono_image_get_name (image));
524 typedef struct _BundledConfig BundledConfig;
526 struct _BundledConfig {
529 const char* config_xml;
532 static BundledConfig *bundled_configs = NULL;
534 static const char *bundled_machine_config = NULL;
537 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
539 BundledConfig *bconfig;
541 bconfig = g_new0 (BundledConfig, 1);
542 bconfig->aname = assembly_name;
543 bconfig->config_xml = config_xml;
544 bconfig->next = bundled_configs;
545 bundled_configs = bconfig;
549 mono_config_string_for_assembly_file (const char *filename)
551 BundledConfig *bconfig;
553 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
554 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
555 return bconfig->config_xml;
561 mono_config_for_assembly (MonoImage *assembly)
563 ParseState state = {NULL};
565 char *aname, *cfg, *cfg_name;
566 const char *bundled_config;
568 state.assembly = assembly;
570 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
571 if (bundled_config) {
572 state.user_data = (gpointer) "<bundled>";
573 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
576 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
577 mono_config_parse_file_with_context (&state, cfg_name);
580 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
582 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
583 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
584 got_it += mono_config_parse_file_with_context (&state, cfg);
588 const char *home = g_get_home_dir ();
589 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
590 got_it += mono_config_parse_file_with_context (&state, cfg);
602 * @filename: the filename to load the configuration variables from.
604 * Pass a NULL filename to parse the default config files
605 * (or the file in the MONO_CONFIG env var).
608 mono_config_parse (const char *filename) {
616 mono_config_parse_file (filename);
620 home = g_getenv ("MONO_CONFIG");
622 mono_config_parse_file (home);
626 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
627 mono_config_parse_file (mono_cfg);
630 #if !defined(TARGET_WIN32) && !defined(__native_client__)
631 home = g_get_home_dir ();
632 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
633 mono_config_parse_file (user_cfg);
638 /* Invoked during startup */
640 mono_set_config_dir (const char *dir)
642 /* If this variable is set, overrides the directory computed */
643 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
644 if (mono_cfg_dir == NULL)
645 mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
649 mono_get_config_dir (void)
651 if (mono_cfg_dir == NULL)
652 mono_set_dirs (NULL, NULL);
658 mono_register_machine_config (const char *config_xml)
660 bundled_machine_config = config_xml;
664 mono_get_machine_config (void)
666 return bundled_machine_config;
670 assembly_binding_end (gpointer user_data, const char *element_name)
672 ParserUserData *pud = (ParserUserData *)user_data;
674 if (!strcmp (element_name, "dependentAssembly")) {
675 if (pud->info_parsed && pud->info) {
676 pud->info_parsed (pud->info, pud->user_data);
677 g_free (pud->info->name);
678 g_free (pud->info->culture);
684 publisher_policy_start (gpointer user_data,
685 const gchar *element_name,
686 const gchar **attribute_names,
687 const gchar **attribute_values)
690 MonoAssemblyBindingInfo *info;
693 pud = (ParserUserData *)user_data;
695 if (!strcmp (element_name, "dependentAssembly")) {
697 info->culture = NULL;
698 info->has_old_version_bottom = FALSE;
699 info->has_old_version_top = FALSE;
700 info->has_new_version = FALSE;
701 info->is_valid = FALSE;
702 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
703 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
704 memset (&info->new_version, 0, sizeof (info->new_version));
705 } else if (!strcmp (element_name, "assemblyIdentity")) {
706 for (n = 0; attribute_names [n]; n++) {
707 const gchar *attribute_name = attribute_names [n];
709 if (!strcmp (attribute_name, "name"))
710 info->name = g_strdup (attribute_values [n]);
711 else if (!strcmp (attribute_name, "publicKeyToken")) {
712 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
713 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
714 } else if (!strcmp (attribute_name, "culture")) {
715 if (!strcmp (attribute_values [n], "neutral"))
716 info->culture = g_strdup ("");
718 info->culture = g_strdup (attribute_values [n]);
721 } else if (!strcmp (element_name, "bindingRedirect")) {
722 for (n = 0; attribute_names [n]; n++) {
723 const gchar *attribute_name = attribute_names [n];
725 if (!strcmp (attribute_name, "oldVersion")) {
726 gchar **numbers, **version, **versions;
727 gint major, minor, build, revision;
730 if (!strcmp (attribute_values [n], ""))
733 versions = g_strsplit (attribute_values [n], "-", 2);
734 version = g_strsplit (*versions, ".", 4);
736 /* We assign the values to gint vars to do the checks */
738 major = *numbers ? atoi (*numbers++) : -1;
739 minor = *numbers ? atoi (*numbers++) : -1;
740 build = *numbers ? atoi (*numbers++) : -1;
741 revision = *numbers ? atoi (*numbers) : -1;
742 g_strfreev (version);
743 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
744 g_strfreev (versions);
748 info->old_version_bottom.major = major;
749 info->old_version_bottom.minor = minor;
750 info->old_version_bottom.build = build;
751 info->old_version_bottom.revision = revision;
752 info->has_old_version_bottom = TRUE;
754 if (!*(versions + 1)) {
755 g_strfreev (versions);
759 numbers = version = g_strsplit (*(versions + 1), ".", 4);
760 major = *numbers ? atoi (*numbers++) : -1;
761 minor = *numbers ? atoi (*numbers++) : -1;
762 build = *numbers ? atoi (*numbers++) : -1;
763 revision = *numbers ? atoi (*numbers) : 1;
764 g_strfreev (version);
765 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
766 g_strfreev (versions);
770 info->old_version_top.major = major;
771 info->old_version_top.minor = minor;
772 info->old_version_top.build = build;
773 info->old_version_top.revision = revision;
774 info->has_old_version_top = TRUE;
776 g_strfreev (versions);
777 } else if (!strcmp (attribute_name, "newVersion")) {
778 gchar **numbers, **version;
781 if (!strcmp (attribute_values [n], ""))
784 numbers = version = g_strsplit (attribute_values [n], ".", 4);
785 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
786 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
787 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
788 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
789 info->has_new_version = TRUE;
790 g_strfreev (version);
796 static MonoParseHandler
797 publisher_policy_parser = {
798 "", /* We don't need to use declare an xml element */
800 publisher_policy_start,
807 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
809 ParserUserData user_data = {
815 &publisher_policy_parser, /* MonoParseHandler */
816 &user_data, /* user_data */
817 NULL, /* MonoImage (we don't need it right now)*/
818 TRUE /* We are already inited */
821 mono_config_parse_file_with_context (&state, filename);
824 static MonoParseHandler
825 config_assemblybinding_parser = {
826 "", /* We don't need to use declare an xml element */
828 publisher_policy_start,
830 assembly_binding_end,
835 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
837 MonoAssemblyBindingInfo info;
845 pud.info_parsed = infocb;
846 pud.user_data = user_data;
848 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
849 state.user_data = &pud;
850 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
851 state.inited = TRUE; /* We are already inited */
853 mono_config_parse_file_with_context (&state, filename);
856 static mono_bool mono_server_mode = FALSE;
859 mono_config_set_server_mode (mono_bool server_mode)
861 mono_server_mode = server_mode;
865 mono_config_is_server_mode (void)
867 return mono_server_mode;