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.h"
22 #if defined(__linux__)
23 #define CONFIG_OS "linux"
24 #elif defined(__APPLE__)
25 #define CONFIG_OS "osx"
27 #define CONFIG_OS "solaris"
28 #elif defined(__FreeBSD__)
29 #define CONFIG_OS "freebsd"
30 #elif defined(__NetBSD__)
31 #define CONFIG_OS "netbsd"
32 #elif defined(__OpenBSD__)
33 #define CONFIG_OS "openbsd"
34 #elif defined(__WIN32__)
35 #define CONFIG_OS "windows"
37 #define CONFIG_OS "aix"
39 #define CONFIG_OS "hpux"
40 #elif defined(SN_TARGET_PS3)
41 #define CONFIG_OS "CellOS"
43 #warning Unknown operating system
44 #define CONFIG_OS "unknownOS"
49 #define CONFIG_CPU "x86"
50 #define CONFIG_WORDSIZE "32"
51 #elif defined(__x86_64__)
52 #define CONFIG_CPU "x86-64"
53 #define CONFIG_WORDSIZE "64"
54 #elif defined(sparc) || defined(__sparc__)
55 #define CONFIG_CPU "sparc"
56 #define CONFIG_WORDSIZE "32"
57 #elif defined(__ppc64__) || defined(__powerpc64__)
58 #define CONFIG_WORDSIZE "64"
59 #ifdef __mono_ppc_ilp32__
60 # define CONFIG_CPU "ppc64ilp32"
62 # define CONFIG_CPU "ppc64"
64 #elif defined(__ppc__) || defined(__powerpc__)
65 #define CONFIG_CPU "ppc"
66 #define CONFIG_WORDSIZE "32"
67 #elif defined(__s390x__)
68 #define CONFIG_CPU "s390x"
69 #define CONFIG_WORDSIZE "64"
70 #elif defined(__s390__)
71 #define CONFIG_CPU "s390"
72 #define CONFIG_WORDSIZE "32"
73 #elif defined(__arm__)
74 #define CONFIG_CPU "arm"
75 #define CONFIG_WORDSIZE "32"
76 #elif defined(__ia64__)
77 #define CONFIG_CPU "ia64"
78 #define CONFIG_WORDSIZE "64"
79 #elif defined(__alpha__)
80 #define CONFIG_CPU "alpha"
81 #define CONFIG_WORDSIZE "64"
82 #elif defined(hppa) || defined(__hppa__)
83 #define CONFIG_CPU "hppa"
84 #define CONFIG_WORDSIZE "32"
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 /* when this interface is stable, export it. */
134 typedef struct MonoParseHandler MonoParseHandler;
136 struct MonoParseHandler {
137 const char *element_name;
138 void*(*init) (MonoImage *assembly);
139 void (*start) (gpointer user_data, const gchar *name,
140 const gchar **attributes,
141 const gchar **values);
142 void (*text) (gpointer user_data, const char *text, gsize test_len);
143 void (*end) (gpointer user_data, const char *name);
144 void (*finish) (gpointer user_data);
148 MonoParseHandler *current;
154 static void start_element (GMarkupParseContext *context,
155 const gchar *element_name,
156 const gchar **attribute_names,
157 const gchar **attribute_values,
161 ParseState *state = user_data;
162 if (!state->current) {
163 state->current = g_hash_table_lookup (config_handlers, element_name);
164 if (state->current && state->current->init)
165 state->user_data = state->current->init (state->assembly);
167 if (state->current && state->current->start)
168 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
171 static void end_element (GMarkupParseContext *context,
172 const gchar *element_name,
176 ParseState *state = user_data;
177 if (state->current) {
178 if (state->current->end)
179 state->current->end (state->user_data, element_name);
180 if (strcmp (state->current->element_name, element_name) == 0) {
181 if (state->current->finish)
182 state->current->finish (state->user_data);
183 state->current = NULL;
184 state->user_data = NULL;
189 static void parse_text (GMarkupParseContext *context,
195 ParseState *state = user_data;
196 if (state->current && state->current->text)
197 state->current->text (state->user_data, text, text_len);
200 static void passthrough (GMarkupParseContext *context,
209 static void parse_error (GMarkupParseContext *context,
216 arch_matches (const char* arch, const char *value)
218 char **splitted, **p;
220 if (value [0] == '!')
221 return !arch_matches (arch, value + 1);
222 p = splitted = g_strsplit (value, ",", 0);
224 if (strcmp (arch, *p) == 0) {
230 g_strfreev (splitted);
242 dllmap_init (MonoImage *assembly) {
243 DllInfo *info = g_new0 (DllInfo, 1);
244 info->assembly = assembly;
249 dllmap_start (gpointer user_data,
250 const gchar *element_name,
251 const gchar **attribute_names,
252 const gchar **attribute_values)
255 DllInfo *info = user_data;
257 if (strcmp (element_name, "dllmap") == 0) {
259 g_free (info->target);
260 info->dll = info->target = NULL;
261 info->ignore = FALSE;
262 for (i = 0; attribute_names [i]; ++i) {
263 if (strcmp (attribute_names [i], "dll") == 0)
264 info->dll = g_strdup (attribute_values [i]);
265 else if (strcmp (attribute_names [i], "target") == 0)
266 info->target = g_strdup (attribute_values [i]);
267 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
269 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
271 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
275 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
276 } else if (strcmp (element_name, "dllentry") == 0) {
277 const char *name = NULL, *target = NULL, *dll = NULL;
279 for (i = 0; attribute_names [i]; ++i) {
280 if (strcmp (attribute_names [i], "dll") == 0)
281 dll = attribute_values [i];
282 else if (strcmp (attribute_names [i], "target") == 0)
283 target = attribute_values [i];
284 else if (strcmp (attribute_names [i], "name") == 0)
285 name = attribute_values [i];
286 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
288 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
290 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
295 if (!info->ignore && !ignore)
296 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
301 dllmap_finish (gpointer user_data)
303 DllInfo *info = user_data;
306 g_free (info->target);
310 static const MonoParseHandler
321 legacyUEP_start (gpointer user_data,
322 const gchar *element_name,
323 const gchar **attribute_names,
324 const gchar **attribute_values) {
325 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
326 (attribute_names [0] != NULL) &&
327 (strcmp (attribute_names [0], "enabled") == 0)) {
328 if ((strcmp (attribute_values [0], "1") == 0) ||
329 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
330 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
335 static const MonoParseHandler
336 legacyUEP_handler = {
337 "legacyUnhandledExceptionPolicy",
345 static int inited = 0;
348 mono_config_init (void)
351 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
352 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
353 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
356 /* FIXME: error handling */
359 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
361 GMarkupParseContext *context;
366 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
367 if (g_markup_parse_context_parse (context, text, len, NULL)) {
368 g_markup_parse_context_end_parse (context, NULL);
370 g_markup_parse_context_free (context);
373 /* If assembly is NULL, parse in the global context */
375 mono_config_parse_file_with_context (ParseState *state, const char *filename)
380 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
381 "Config attempting to parse: '%s'.", filename);
383 if (!g_file_get_contents (filename, &text, &len, NULL))
385 mono_config_parse_xml_with_context (state, text, len);
391 * mono_config_parse_memory:
392 * @buffer: a pointer to an string XML representation of the configuration
394 * Parses the configuration from a buffer
397 mono_config_parse_memory (const char *buffer)
399 ParseState state = {NULL};
401 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
405 mono_config_parse_file (const char *filename)
407 ParseState state = {NULL};
408 mono_config_parse_file_with_context (&state, filename);
412 * use the equivalent lookup code from the GAC when available.
413 * Depending on state, this should give something like:
414 * aname/version-pubtoken/
419 get_assembly_filename (MonoImage *image, int state)
423 return g_strdup (mono_image_get_name (image));
429 typedef struct _BundledConfig BundledConfig;
431 struct _BundledConfig {
434 const char* config_xml;
437 static BundledConfig *bundled_configs = NULL;
439 static const char *bundled_machine_config = NULL;
442 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
444 BundledConfig *bconfig;
446 bconfig = g_new0 (BundledConfig, 1);
447 bconfig->aname = assembly_name;
448 bconfig->config_xml = config_xml;
449 bconfig->next = bundled_configs;
450 bundled_configs = bconfig;
454 mono_config_string_for_assembly_file (const char *filename)
456 BundledConfig *bconfig;
458 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
459 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
460 return bconfig->config_xml;
466 mono_config_for_assembly (MonoImage *assembly)
468 ParseState state = {NULL};
470 char *aname, *cfg, *cfg_name;
471 const char *bundled_config;
474 state.assembly = assembly;
476 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
478 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
480 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
481 mono_config_parse_file_with_context (&state, cfg_name);
484 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
486 home = g_get_home_dir ();
488 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
489 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
490 got_it += mono_config_parse_file_with_context (&state, cfg);
493 #ifndef PLATFORM_WIN32
494 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
495 got_it += mono_config_parse_file_with_context (&state, cfg);
507 * @filename: the filename to load the configuration variables from.
509 * Pass a NULL filename to parse the default config files
510 * (or the file in the MONO_CONFIG env var).
513 mono_config_parse (const char *filename) {
516 #ifndef PLATFORM_WIN32
521 mono_config_parse_file (filename);
525 home = g_getenv ("MONO_CONFIG");
527 mono_config_parse_file (home);
531 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
532 mono_config_parse_file (mono_cfg);
535 #ifndef PLATFORM_WIN32
536 home = g_get_home_dir ();
537 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
538 mono_config_parse_file (user_cfg);
543 static const char *mono_cfg_dir = NULL;
545 /* Invoked during startup */
547 mono_set_config_dir (const char *dir)
549 /* If this variable is set, overrides the directory computed */
550 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
551 if (mono_cfg_dir == NULL)
552 mono_cfg_dir = g_strdup (dir);
556 mono_get_config_dir (void)
558 if (mono_cfg_dir == NULL)
559 mono_set_dirs (NULL, NULL);
565 mono_register_machine_config (const char *config_xml)
567 bundled_machine_config = config_xml;
571 mono_get_machine_config (void)
573 return bundled_machine_config;
577 publisher_policy_start (gpointer user_data,
578 const gchar *element_name,
579 const gchar **attribute_names,
580 const gchar **attribute_values)
582 MonoAssemblyBindingInfo *info;
586 if (!strcmp (element_name, "assemblyIdentity")) {
587 for (n = 0; attribute_names [n]; n++) {
588 const gchar *attribute_name = attribute_names [n];
590 if (!strcmp (attribute_name, "name"))
591 info->name = g_strdup (attribute_values [n]);
592 else if (!strcmp (attribute_name, "publicKeyToken")) {
593 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
594 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
595 } else if (!strcmp (attribute_name, "culture")) {
596 if (!strcmp (attribute_values [n], "neutral"))
597 info->culture = g_strdup ("");
599 info->culture = g_strdup (attribute_values [n]);
602 } else if (!strcmp (element_name, "bindingRedirect")) {
603 for (n = 0; attribute_names [n]; n++) {
604 const gchar *attribute_name = attribute_names [n];
606 if (!strcmp (attribute_name, "oldVersion")) {
607 gchar **numbers, **version, **versions;
608 gint major, minor, build, revision;
611 if (!strcmp (attribute_values [n], ""))
614 versions = g_strsplit (attribute_values [n], "-", 2);
615 version = g_strsplit (*versions, ".", 4);
617 /* We assign the values to gint vars to do the checks */
619 major = *numbers ? atoi (*numbers++) : -1;
620 minor = *numbers ? atoi (*numbers++) : -1;
621 build = *numbers ? atoi (*numbers++) : -1;
622 revision = *numbers ? atoi (*numbers) : -1;
623 g_strfreev (version);
624 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
625 g_strfreev (versions);
629 info->old_version_bottom.major = major;
630 info->old_version_bottom.minor = minor;
631 info->old_version_bottom.build = build;
632 info->old_version_bottom.revision = revision;
633 info->has_old_version_bottom = TRUE;
635 if (!*(versions + 1)) {
636 g_strfreev (versions);
640 numbers = version = g_strsplit (*(versions + 1), ".", 4);
641 major = *numbers ? atoi (*numbers++) : -1;
642 minor = *numbers ? atoi (*numbers++) : -1;
643 build = *numbers ? atoi (*numbers++) : -1;
644 revision = *numbers ? atoi (*numbers) : 1;
645 g_strfreev (version);
646 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
647 g_strfreev (versions);
651 info->old_version_top.major = major;
652 info->old_version_top.minor = minor;
653 info->old_version_top.build = build;
654 info->old_version_top.revision = revision;
655 info->has_old_version_top = TRUE;
657 g_strfreev (versions);
658 } else if (!strcmp (attribute_name, "newVersion")) {
659 gchar **numbers, **version;
662 if (!strcmp (attribute_values [n], ""))
665 numbers = version = g_strsplit (attribute_values [n], ".", 4);
666 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
667 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
668 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
669 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
670 info->has_new_version = TRUE;
671 g_strfreev (version);
677 static MonoParseHandler
678 publisher_policy_parser = {
679 "", /* We don't need to use declare an xml element */
681 publisher_policy_start,
688 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
691 &publisher_policy_parser, /* MonoParseHandler */
692 info, /* user_data */
693 NULL, /* MonoImage (we don't need it right now)*/
694 TRUE /* We are already inited */
697 mono_config_parse_file_with_context (&state, filename);