4 * Runtime and assembly configuration file support routines.
6 * Author: Paolo Molaro (lupus@ximian.com)
8 * (C) 2002 Ximian, Inc.
14 #include "mono/metadata/assembly.h"
15 #include "mono/metadata/loader.h"
16 #include "mono/metadata/mono-config.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/metadata/object-internals.h"
19 #include "mono/utils/mono-logger.h"
21 #if defined(__linux__)
22 #define CONFIG_OS "linux"
23 #elif defined(__APPLE__)
24 #define CONFIG_OS "osx"
26 #define CONFIG_OS "solaris"
27 #elif defined(__FreeBSD__)
28 #define CONFIG_OS "freebsd"
29 #elif defined(__NetBSD__)
30 #define CONFIG_OS "netbsd"
31 #elif defined(__OpenBSD__)
32 #define CONFIG_OS "openbsd"
33 #elif defined(__WIN32__)
34 #define CONFIG_OS "windows"
36 #define CONFIG_OS "aix"
38 #define CONFIG_OS "hpux"
40 #warning Unknown operating system
41 #define CONFIG_OS "unknownOS"
45 #define CONFIG_CPU "x86"
46 #define CONFIG_WORDSIZE "32"
47 #elif defined(__x86_64__)
48 #define CONFIG_CPU "x86-64"
49 #define CONFIG_WORDSIZE "64"
50 #elif defined(sparc) || defined(__sparc__)
51 #define CONFIG_CPU "sparc"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__ppc64__) || defined(__powerpc64__)
54 #define CONFIG_CPU "ppc64"
55 #define CONFIG_WORDSIZE "64"
56 #elif defined(__ppc__) || defined(__powerpc__)
57 #define CONFIG_CPU "ppc"
58 #define CONFIG_WORDSIZE "32"
59 #elif defined(__s390x__)
60 #define CONFIG_CPU "s390x"
61 #define CONFIG_WORDSIZE "64"
62 #elif defined(__s390__)
63 #define CONFIG_CPU "s390"
64 #define CONFIG_WORDSIZE "32"
65 #elif defined(__arm__)
66 #define CONFIG_CPU "arm"
67 #define CONFIG_WORDSIZE "32"
68 #elif defined(__ia64__)
69 #define CONFIG_CPU "ia64"
70 #define CONFIG_WORDSIZE "64"
71 #elif defined(__alpha__)
72 #define CONFIG_CPU "alpha"
73 #define CONFIG_WORDSIZE "64"
74 #elif defined(hppa) || defined(__hppa__)
75 #define CONFIG_CPU "hppa"
76 #define CONFIG_WORDSIZE "32"
77 #elif defined(mips) || defined(__mips) || defined(_mips)
78 #define CONFIG_CPU "mips"
79 #define CONFIG_WORDSIZE "32"
82 #define CONFIG_CPU "unknownCPU"
85 static void start_element (GMarkupParseContext *context,
86 const gchar *element_name,
87 const gchar **attribute_names,
88 const gchar **attribute_values,
92 static void end_element (GMarkupParseContext *context,
93 const gchar *element_name,
97 static void parse_text (GMarkupParseContext *context,
103 static void passthrough (GMarkupParseContext *context,
109 static void parse_error (GMarkupParseContext *context,
113 static const GMarkupParser
122 static GHashTable *config_handlers;
124 /* when this interface is stable, export it. */
125 typedef struct MonoParseHandler MonoParseHandler;
127 struct MonoParseHandler {
128 const char *element_name;
129 void*(*init) (MonoImage *assembly);
130 void (*start) (gpointer user_data, const gchar *name,
131 const gchar **attributes,
132 const gchar **values);
133 void (*text) (gpointer user_data, const char *text, gsize test_len);
134 void (*end) (gpointer user_data, const char *name);
135 void (*finish) (gpointer user_data);
139 MonoParseHandler *current;
145 static void start_element (GMarkupParseContext *context,
146 const gchar *element_name,
147 const gchar **attribute_names,
148 const gchar **attribute_values,
152 ParseState *state = user_data;
153 if (!state->current) {
154 state->current = g_hash_table_lookup (config_handlers, element_name);
155 if (state->current && state->current->init)
156 state->user_data = state->current->init (state->assembly);
158 if (state->current && state->current->start)
159 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
162 static void end_element (GMarkupParseContext *context,
163 const gchar *element_name,
167 ParseState *state = user_data;
168 if (state->current) {
169 if (state->current->end)
170 state->current->end (state->user_data, element_name);
171 if (strcmp (state->current->element_name, element_name) == 0) {
172 if (state->current->finish)
173 state->current->finish (state->user_data);
174 state->current = NULL;
175 state->user_data = NULL;
180 static void parse_text (GMarkupParseContext *context,
186 ParseState *state = user_data;
187 if (state->current && state->current->text)
188 state->current->text (state->user_data, text, text_len);
191 static void passthrough (GMarkupParseContext *context,
200 static void parse_error (GMarkupParseContext *context,
207 arch_matches (const char* arch, const char *value)
209 char **splitted, **p;
211 if (value [0] == '!')
212 return !arch_matches (arch, value + 1);
213 p = splitted = g_strsplit (value, ",", 0);
215 if (strcmp (arch, *p) == 0) {
221 g_strfreev (splitted);
233 dllmap_init (MonoImage *assembly) {
234 DllInfo *info = g_new0 (DllInfo, 1);
235 info->assembly = assembly;
240 dllmap_start (gpointer user_data,
241 const gchar *element_name,
242 const gchar **attribute_names,
243 const gchar **attribute_values)
246 DllInfo *info = user_data;
248 if (strcmp (element_name, "dllmap") == 0) {
250 g_free (info->target);
251 info->dll = info->target = NULL;
252 info->ignore = FALSE;
253 for (i = 0; attribute_names [i]; ++i) {
254 if (strcmp (attribute_names [i], "dll") == 0)
255 info->dll = g_strdup (attribute_values [i]);
256 else if (strcmp (attribute_names [i], "target") == 0)
257 info->target = g_strdup (attribute_values [i]);
258 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
260 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
262 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
266 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
267 } else if (strcmp (element_name, "dllentry") == 0) {
268 const char *name = NULL, *target = NULL, *dll = NULL;
270 for (i = 0; attribute_names [i]; ++i) {
271 if (strcmp (attribute_names [i], "dll") == 0)
272 dll = attribute_values [i];
273 else if (strcmp (attribute_names [i], "target") == 0)
274 target = attribute_values [i];
275 else if (strcmp (attribute_names [i], "name") == 0)
276 name = attribute_values [i];
277 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
279 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
281 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
286 if (!info->ignore && !ignore)
287 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
292 dllmap_finish (gpointer user_data)
294 DllInfo *info = user_data;
297 g_free (info->target);
301 static const MonoParseHandler
312 legacyUEP_start (gpointer user_data,
313 const gchar *element_name,
314 const gchar **attribute_names,
315 const gchar **attribute_values) {
316 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
317 (attribute_names [0] != NULL) &&
318 (strcmp (attribute_names [0], "enabled") == 0)) {
319 if ((strcmp (attribute_values [0], "1") == 0) ||
320 (g_strcasecmp (attribute_values [0], "true") == 0)) {
321 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
326 static const MonoParseHandler
327 legacyUEP_handler = {
328 "legacyUnhandledExceptionPolicy",
336 static int inited = 0;
339 mono_config_init (void)
342 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
343 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
344 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
347 /* FIXME: error handling */
350 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
352 GMarkupParseContext *context;
357 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
358 if (g_markup_parse_context_parse (context, text, len, NULL)) {
359 g_markup_parse_context_end_parse (context, NULL);
361 g_markup_parse_context_free (context);
364 /* If assembly is NULL, parse in the global context */
366 mono_config_parse_file_with_context (ParseState *state, const char *filename)
371 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
372 "Config attempting to parse: '%s'.", filename);
374 if (!g_file_get_contents (filename, &text, &len, NULL))
376 mono_config_parse_xml_with_context (state, text, len);
382 * mono_config_parse_memory:
383 * @buffer: a pointer to an string XML representation of the configuration
385 * Parses the configuration from a buffer
388 mono_config_parse_memory (const char *buffer)
390 ParseState state = {NULL};
392 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
396 mono_config_parse_file (const char *filename)
398 ParseState state = {NULL};
399 mono_config_parse_file_with_context (&state, filename);
403 * use the equivalent lookup code from the GAC when available.
404 * Depending on state, this should give something like:
405 * aname/version-pubtoken/
410 get_assembly_filename (MonoImage *image, int state)
414 return g_strdup (mono_image_get_name (image));
420 typedef struct _BundledConfig BundledConfig;
422 struct _BundledConfig {
425 const char* config_xml;
428 static BundledConfig *bundled_configs = NULL;
430 static const char *bundled_machine_config = NULL;
433 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
435 BundledConfig *bconfig;
437 bconfig = g_new0 (BundledConfig, 1);
438 bconfig->aname = assembly_name;
439 bconfig->config_xml = config_xml;
440 bconfig->next = bundled_configs;
441 bundled_configs = bconfig;
445 mono_config_string_for_assembly_file (const char *filename)
447 BundledConfig *bconfig;
449 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
450 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
451 return bconfig->config_xml;
457 mono_config_for_assembly (MonoImage *assembly)
459 ParseState state = {NULL};
461 char *aname, *cfg, *cfg_name;
462 const char *bundled_config;
465 state.assembly = assembly;
467 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
469 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
471 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
472 mono_config_parse_file_with_context (&state, cfg_name);
475 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
477 home = g_get_home_dir ();
479 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
480 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
481 got_it += mono_config_parse_file_with_context (&state, cfg);
484 #ifndef PLATFORM_WIN32
485 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
486 got_it += mono_config_parse_file_with_context (&state, cfg);
498 * @filename: the filename to load the configuration variables from.
500 * Pass a NULL filename to parse the default config files
501 * (or the file in the MONO_CONFIG env var).
504 mono_config_parse (const char *filename) {
507 #ifndef PLATFORM_WIN32
512 mono_config_parse_file (filename);
516 home = g_getenv ("MONO_CONFIG");
518 mono_config_parse_file (home);
522 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
523 mono_config_parse_file (mono_cfg);
526 #ifndef PLATFORM_WIN32
527 home = g_get_home_dir ();
528 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
529 mono_config_parse_file (user_cfg);
534 static const char *mono_cfg_dir = NULL;
536 /* Invoked during startup */
538 mono_set_config_dir (const char *dir)
540 /* If this variable is set, overrides the directory computed */
541 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
542 if (mono_cfg_dir == NULL)
543 mono_cfg_dir = g_strdup (dir);
547 mono_get_config_dir (void)
549 if (mono_cfg_dir == NULL)
550 mono_set_dirs (NULL, NULL);
556 mono_register_machine_config (const char *config_xml)
558 bundled_machine_config = config_xml;
562 mono_get_machine_config (void)
564 return bundled_machine_config;
568 publisher_policy_start (gpointer user_data,
569 const gchar *element_name,
570 const gchar **attribute_names,
571 const gchar **attribute_values)
573 MonoAssemblyBindingInfo *info;
577 if (!strcmp (element_name, "assemblyIdentity")) {
578 for (n = 0; attribute_names [n]; n++) {
579 const gchar *attribute_name = attribute_names [n];
581 if (!strcmp (attribute_name, "name"))
582 info->name = g_strdup (attribute_values [n]);
583 else if (!strcmp (attribute_name, "publicKeyToken")) {
584 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
585 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
586 } else if (!strcmp (attribute_name, "culture")) {
587 if (!strcmp (attribute_values [n], "neutral"))
588 info->culture = g_strdup ("");
590 info->culture = g_strdup (attribute_values [n]);
593 } else if (!strcmp (element_name, "bindingRedirect")) {
594 for (n = 0; attribute_names [n]; n++) {
595 const gchar *attribute_name = attribute_names [n];
597 if (!strcmp (attribute_name, "oldVersion")) {
598 gchar **numbers, **version, **versions;
599 gint major, minor, build, revision;
602 if (!strcmp (attribute_values [n], ""))
605 versions = g_strsplit (attribute_values [n], "-", 2);
606 version = g_strsplit (*versions, ".", 4);
608 /* We assign the values to gint vars to do the checks */
610 major = *numbers ? atoi (*numbers++) : -1;
611 minor = *numbers ? atoi (*numbers++) : -1;
612 build = *numbers ? atoi (*numbers++) : -1;
613 revision = *numbers ? atoi (*numbers) : -1;
614 g_strfreev (version);
615 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
616 g_strfreev (versions);
620 info->old_version_bottom.major = major;
621 info->old_version_bottom.minor = minor;
622 info->old_version_bottom.build = build;
623 info->old_version_bottom.revision = revision;
624 info->has_old_version_bottom = TRUE;
626 if (!*(versions + 1)) {
627 g_strfreev (versions);
631 numbers = version = g_strsplit (*(versions + 1), ".", 4);
632 major = *numbers ? atoi (*numbers++) : -1;
633 minor = *numbers ? atoi (*numbers++) : -1;
634 build = *numbers ? atoi (*numbers++) : -1;
635 revision = *numbers ? atoi (*numbers) : 1;
636 g_strfreev (version);
637 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
638 g_strfreev (versions);
642 info->old_version_top.major = major;
643 info->old_version_top.minor = minor;
644 info->old_version_top.build = build;
645 info->old_version_top.revision = revision;
646 info->has_old_version_top = TRUE;
648 g_strfreev (versions);
649 } else if (!strcmp (attribute_name, "newVersion")) {
650 gchar **numbers, **version;
653 if (!strcmp (attribute_values [n], ""))
656 numbers = version = g_strsplit (attribute_values [n], ".", 4);
657 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
658 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
659 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
660 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
661 info->has_new_version = TRUE;
662 g_strfreev (version);
668 static MonoParseHandler
669 publisher_policy_parser = {
670 "", /* We don't need to use declare an xml element */
672 publisher_policy_start,
679 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
682 &publisher_policy_parser, /* MonoParseHandler */
683 info, /* user_data */
684 NULL, /* MonoImage (we don't need it right now)*/
685 TRUE /* We are already inited */
688 mono_config_parse_file_with_context (&state, filename);