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-internal.h"
22 #if defined(TARGET_PS3)
23 #define CONFIG_OS "CellOS"
24 #elif defined(__linux__)
25 #define CONFIG_OS "linux"
26 #elif defined(__APPLE__)
27 #define CONFIG_OS "osx"
29 #define CONFIG_OS "solaris"
30 #elif defined(__FreeBSD__)
31 #define CONFIG_OS "freebsd"
32 #elif defined(__NetBSD__)
33 #define CONFIG_OS "netbsd"
34 #elif defined(__OpenBSD__)
35 #define CONFIG_OS "openbsd"
36 #elif defined(__WIN32__)
37 #define CONFIG_OS "windows"
39 #define CONFIG_OS "aix"
41 #define CONFIG_OS "hpux"
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__) || defined(TARGET_POWERPC)
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 MonoAssemblyBindingInfo
*info
;
149 void (*info_parsed
)(MonoAssemblyBindingInfo
*info
, void *user_data
);
154 MonoParseHandler
*current
;
160 static void start_element (GMarkupParseContext
*context
,
161 const gchar
*element_name
,
162 const gchar
**attribute_names
,
163 const gchar
**attribute_values
,
167 ParseState
*state
= user_data
;
168 if (!state
->current
) {
169 state
->current
= g_hash_table_lookup (config_handlers
, element_name
);
170 if (state
->current
&& state
->current
->init
)
171 state
->user_data
= state
->current
->init (state
->assembly
);
173 if (state
->current
&& state
->current
->start
)
174 state
->current
->start (state
->user_data
, element_name
, attribute_names
, attribute_values
);
177 static void end_element (GMarkupParseContext
*context
,
178 const gchar
*element_name
,
182 ParseState
*state
= user_data
;
183 if (state
->current
) {
184 if (state
->current
->end
)
185 state
->current
->end (state
->user_data
, element_name
);
186 if (strcmp (state
->current
->element_name
, element_name
) == 0) {
187 if (state
->current
->finish
)
188 state
->current
->finish (state
->user_data
);
189 state
->current
= NULL
;
190 state
->user_data
= NULL
;
195 static void parse_text (GMarkupParseContext
*context
,
201 ParseState
*state
= user_data
;
202 if (state
->current
&& state
->current
->text
)
203 state
->current
->text (state
->user_data
, text
, text_len
);
206 static void passthrough (GMarkupParseContext
*context
,
215 static void parse_error (GMarkupParseContext
*context
,
219 ParseState
*state
= user_data
;
221 const gchar
*filename
;
223 filename
= state
&& state
->user_data
? (gchar
*) state
->user_data
: "<unknown>";
224 msg
= error
&& error
->message
? error
->message
: "";
225 g_warning ("Error parsing %s: %s", filename
, msg
);
229 arch_matches (const char* arch
, const char *value
)
231 char **splitted
, **p
;
233 if (value
[0] == '!')
234 return !arch_matches (arch
, value
+ 1);
235 p
= splitted
= g_strsplit (value
, ",", 0);
237 if (strcmp (arch
, *p
) == 0) {
243 g_strfreev (splitted
);
255 dllmap_init (MonoImage
*assembly
) {
256 DllInfo
*info
= g_new0 (DllInfo
, 1);
257 info
->assembly
= assembly
;
262 dllmap_start (gpointer user_data
,
263 const gchar
*element_name
,
264 const gchar
**attribute_names
,
265 const gchar
**attribute_values
)
268 DllInfo
*info
= user_data
;
270 if (strcmp (element_name
, "dllmap") == 0) {
272 g_free (info
->target
);
273 info
->dll
= info
->target
= NULL
;
274 info
->ignore
= FALSE
;
275 for (i
= 0; attribute_names
[i
]; ++i
) {
276 if (strcmp (attribute_names
[i
], "dll") == 0)
277 info
->dll
= g_strdup (attribute_values
[i
]);
278 else if (strcmp (attribute_names
[i
], "target") == 0)
279 info
->target
= g_strdup (attribute_values
[i
]);
280 else if (strcmp (attribute_names
[i
], "os") == 0 && !arch_matches (CONFIG_OS
, attribute_values
[i
]))
282 else if (strcmp (attribute_names
[i
], "cpu") == 0 && !arch_matches (CONFIG_CPU
, attribute_values
[i
]))
284 else if (strcmp (attribute_names
[i
], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE
, attribute_values
[i
]))
288 mono_dllmap_insert (info
->assembly
, info
->dll
, NULL
, info
->target
, NULL
);
289 } else if (strcmp (element_name
, "dllentry") == 0) {
290 const char *name
= NULL
, *target
= NULL
, *dll
= NULL
;
292 for (i
= 0; attribute_names
[i
]; ++i
) {
293 if (strcmp (attribute_names
[i
], "dll") == 0)
294 dll
= attribute_values
[i
];
295 else if (strcmp (attribute_names
[i
], "target") == 0)
296 target
= attribute_values
[i
];
297 else if (strcmp (attribute_names
[i
], "name") == 0)
298 name
= attribute_values
[i
];
299 else if (strcmp (attribute_names
[i
], "os") == 0 && !arch_matches (CONFIG_OS
, attribute_values
[i
]))
301 else if (strcmp (attribute_names
[i
], "cpu") == 0 && !arch_matches (CONFIG_CPU
, attribute_values
[i
]))
303 else if (strcmp (attribute_names
[i
], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE
, attribute_values
[i
]))
308 if (!info
->ignore
&& !ignore
)
309 mono_dllmap_insert (info
->assembly
, info
->dll
, name
, dll
, target
);
314 dllmap_finish (gpointer user_data
)
316 DllInfo
*info
= user_data
;
319 g_free (info
->target
);
323 static const MonoParseHandler
334 legacyUEP_start (gpointer user_data
,
335 const gchar
*element_name
,
336 const gchar
**attribute_names
,
337 const gchar
**attribute_values
) {
338 if ((strcmp (element_name
, "legacyUnhandledExceptionPolicy") == 0) &&
339 (attribute_names
[0] != NULL
) &&
340 (strcmp (attribute_names
[0], "enabled") == 0)) {
341 if ((strcmp (attribute_values
[0], "1") == 0) ||
342 (g_ascii_strcasecmp (attribute_values
[0], "true") == 0)) {
343 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY
);
348 static const MonoParseHandler
349 legacyUEP_handler
= {
350 "legacyUnhandledExceptionPolicy",
358 static int inited
= 0;
361 mono_config_init (void)
364 config_handlers
= g_hash_table_new (g_str_hash
, g_str_equal
);
365 g_hash_table_insert (config_handlers
, (gpointer
) dllmap_handler
.element_name
, (gpointer
) &dllmap_handler
);
366 g_hash_table_insert (config_handlers
, (gpointer
) legacyUEP_handler
.element_name
, (gpointer
) &legacyUEP_handler
);
369 /* FIXME: error handling */
372 mono_config_parse_xml_with_context (ParseState
*state
, const char *text
, gsize len
)
374 GMarkupParseContext
*context
;
379 context
= g_markup_parse_context_new (&mono_parser
, 0, state
, NULL
);
380 if (g_markup_parse_context_parse (context
, text
, len
, NULL
)) {
381 g_markup_parse_context_end_parse (context
, NULL
);
383 g_markup_parse_context_free (context
);
386 /* If assembly is NULL, parse in the global context */
388 mono_config_parse_file_with_context (ParseState
*state
, const char *filename
)
394 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_CONFIG
,
395 "Config attempting to parse: '%s'.", filename
);
397 if (!g_file_get_contents (filename
, &text
, &len
, NULL
))
402 if (len
> 3 && text
[0] == '\xef' && text
[1] == (gchar
) '\xbb' && text
[2] == '\xbf')
403 offset
= 3; /* Skip UTF-8 BOM */
404 if (state
->user_data
== NULL
)
405 state
->user_data
= (gpointer
) filename
;
406 mono_config_parse_xml_with_context (state
, text
+ offset
, len
- offset
);
412 * mono_config_parse_memory:
413 * @buffer: a pointer to an string XML representation of the configuration
415 * Parses the configuration from a buffer
418 mono_config_parse_memory (const char *buffer
)
420 ParseState state
= {NULL
};
422 state
.user_data
= (gpointer
) "<buffer>";
423 mono_config_parse_xml_with_context (&state
, buffer
, strlen (buffer
));
427 mono_config_parse_file (const char *filename
)
429 ParseState state
= {NULL
};
430 state
.user_data
= (gpointer
) filename
;
431 mono_config_parse_file_with_context (&state
, filename
);
435 * use the equivalent lookup code from the GAC when available.
436 * Depending on state, this should give something like:
437 * aname/version-pubtoken/
442 get_assembly_filename (MonoImage
*image
, int state
)
446 return g_strdup (mono_image_get_name (image
));
452 typedef struct _BundledConfig BundledConfig
;
454 struct _BundledConfig
{
457 const char* config_xml
;
460 static BundledConfig
*bundled_configs
= NULL
;
462 static const char *bundled_machine_config
= NULL
;
465 mono_register_config_for_assembly (const char* assembly_name
, const char* config_xml
)
467 BundledConfig
*bconfig
;
469 bconfig
= g_new0 (BundledConfig
, 1);
470 bconfig
->aname
= assembly_name
;
471 bconfig
->config_xml
= config_xml
;
472 bconfig
->next
= bundled_configs
;
473 bundled_configs
= bconfig
;
477 mono_config_string_for_assembly_file (const char *filename
)
479 BundledConfig
*bconfig
;
481 for (bconfig
= bundled_configs
; bconfig
; bconfig
= bconfig
->next
) {
482 if (bconfig
->aname
&& strcmp (bconfig
->aname
, filename
) == 0)
483 return bconfig
->config_xml
;
489 mono_config_for_assembly (MonoImage
*assembly
)
491 ParseState state
= {NULL
};
493 char *aname
, *cfg
, *cfg_name
;
494 const char *bundled_config
;
497 state
.assembly
= assembly
;
499 bundled_config
= mono_config_string_for_assembly_file (assembly
->module_name
);
500 if (bundled_config
) {
501 state
.user_data
= (gpointer
) "<bundled>";
502 mono_config_parse_xml_with_context (&state
, bundled_config
, strlen (bundled_config
));
505 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_filename (assembly
));
506 mono_config_parse_file_with_context (&state
, cfg_name
);
509 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_name (assembly
));
511 home
= g_get_home_dir ();
513 for (i
= 0; (aname
= get_assembly_filename (assembly
, i
)) != NULL
; ++i
) {
514 cfg
= g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname
, cfg_name
, NULL
);
515 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
519 cfg
= g_build_filename (home
, ".mono", "assemblies", aname
, cfg_name
, NULL
);
520 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
532 * @filename: the filename to load the configuration variables from.
534 * Pass a NULL filename to parse the default config files
535 * (or the file in the MONO_CONFIG env var).
538 mono_config_parse (const char *filename
) {
546 mono_config_parse_file (filename
);
550 home
= g_getenv ("MONO_CONFIG");
552 mono_config_parse_file (home
);
556 mono_cfg
= g_build_filename (mono_get_config_dir (), "mono", "config", NULL
);
557 mono_config_parse_file (mono_cfg
);
561 home
= g_get_home_dir ();
562 user_cfg
= g_strconcat (home
, G_DIR_SEPARATOR_S
, ".mono/config", NULL
);
563 mono_config_parse_file (user_cfg
);
568 static const char *mono_cfg_dir
= NULL
;
570 /* Invoked during startup */
572 mono_set_config_dir (const char *dir
)
574 /* If this variable is set, overrides the directory computed */
575 mono_cfg_dir
= g_getenv ("MONO_CFG_DIR");
576 if (mono_cfg_dir
== NULL
)
577 mono_cfg_dir
= g_strdup (dir
);
581 mono_get_config_dir (void)
583 if (mono_cfg_dir
== NULL
)
584 mono_set_dirs (NULL
, NULL
);
590 mono_register_machine_config (const char *config_xml
)
592 bundled_machine_config
= config_xml
;
596 mono_get_machine_config (void)
598 return bundled_machine_config
;
602 assembly_binding_end (gpointer user_data
, const char *element_name
)
604 ParserUserData
*pud
= user_data
;
606 if (!strcmp (element_name
, "dependentAssembly")) {
607 if (pud
->info_parsed
&& pud
->info
) {
608 pud
->info_parsed (pud
->info
, pud
->user_data
);
609 g_free (pud
->info
->name
);
610 g_free (pud
->info
->culture
);
616 publisher_policy_start (gpointer user_data
,
617 const gchar
*element_name
,
618 const gchar
**attribute_names
,
619 const gchar
**attribute_values
)
622 MonoAssemblyBindingInfo
*info
;
627 if (!strcmp (element_name
, "dependentAssembly")) {
629 info
->culture
= NULL
;
630 info
->has_old_version_bottom
= FALSE
;
631 info
->has_old_version_top
= FALSE
;
632 info
->has_new_version
= FALSE
;
633 info
->is_valid
= FALSE
;
634 memset (&info
->old_version_bottom
, 0, sizeof (info
->old_version_bottom
));
635 memset (&info
->old_version_top
, 0, sizeof (info
->old_version_top
));
636 memset (&info
->new_version
, 0, sizeof (info
->new_version
));
637 } if (!strcmp (element_name
, "assemblyIdentity")) {
638 for (n
= 0; attribute_names
[n
]; n
++) {
639 const gchar
*attribute_name
= attribute_names
[n
];
641 if (!strcmp (attribute_name
, "name"))
642 info
->name
= g_strdup (attribute_values
[n
]);
643 else if (!strcmp (attribute_name
, "publicKeyToken")) {
644 if (strlen (attribute_values
[n
]) == MONO_PUBLIC_KEY_TOKEN_LENGTH
- 1)
645 g_strlcpy ((char *) info
->public_key_token
, attribute_values
[n
], MONO_PUBLIC_KEY_TOKEN_LENGTH
);
646 } else if (!strcmp (attribute_name
, "culture")) {
647 if (!strcmp (attribute_values
[n
], "neutral"))
648 info
->culture
= g_strdup ("");
650 info
->culture
= g_strdup (attribute_values
[n
]);
653 } else if (!strcmp (element_name
, "bindingRedirect")) {
654 for (n
= 0; attribute_names
[n
]; n
++) {
655 const gchar
*attribute_name
= attribute_names
[n
];
657 if (!strcmp (attribute_name
, "oldVersion")) {
658 gchar
**numbers
, **version
, **versions
;
659 gint major
, minor
, build
, revision
;
662 if (!strcmp (attribute_values
[n
], ""))
665 versions
= g_strsplit (attribute_values
[n
], "-", 2);
666 version
= g_strsplit (*versions
, ".", 4);
668 /* We assign the values to gint vars to do the checks */
670 major
= *numbers
? atoi (*numbers
++) : -1;
671 minor
= *numbers
? atoi (*numbers
++) : -1;
672 build
= *numbers
? atoi (*numbers
++) : -1;
673 revision
= *numbers
? atoi (*numbers
) : -1;
674 g_strfreev (version
);
675 if (major
< 0 || minor
< 0 || build
< 0 || revision
< 0) {
676 g_strfreev (versions
);
680 info
->old_version_bottom
.major
= major
;
681 info
->old_version_bottom
.minor
= minor
;
682 info
->old_version_bottom
.build
= build
;
683 info
->old_version_bottom
.revision
= revision
;
684 info
->has_old_version_bottom
= TRUE
;
686 if (!*(versions
+ 1)) {
687 g_strfreev (versions
);
691 numbers
= version
= g_strsplit (*(versions
+ 1), ".", 4);
692 major
= *numbers
? atoi (*numbers
++) : -1;
693 minor
= *numbers
? atoi (*numbers
++) : -1;
694 build
= *numbers
? atoi (*numbers
++) : -1;
695 revision
= *numbers
? atoi (*numbers
) : 1;
696 g_strfreev (version
);
697 if (major
< 0 || minor
< 0 || build
< 0 || revision
< 0) {
698 g_strfreev (versions
);
702 info
->old_version_top
.major
= major
;
703 info
->old_version_top
.minor
= minor
;
704 info
->old_version_top
.build
= build
;
705 info
->old_version_top
.revision
= revision
;
706 info
->has_old_version_top
= TRUE
;
708 g_strfreev (versions
);
709 } else if (!strcmp (attribute_name
, "newVersion")) {
710 gchar
**numbers
, **version
;
713 if (!strcmp (attribute_values
[n
], ""))
716 numbers
= version
= g_strsplit (attribute_values
[n
], ".", 4);
717 info
->new_version
.major
= *numbers
? atoi (*numbers
++) : -1;
718 info
->new_version
.minor
= *numbers
? atoi (*numbers
++) : -1;
719 info
->new_version
.build
= *numbers
? atoi (*numbers
++) : -1;
720 info
->new_version
.revision
= *numbers
? atoi (*numbers
) : -1;
721 info
->has_new_version
= TRUE
;
722 g_strfreev (version
);
728 static MonoParseHandler
729 publisher_policy_parser
= {
730 "", /* We don't need to use declare an xml element */
732 publisher_policy_start
,
739 mono_config_parse_publisher_policy (const gchar
*filename
, MonoAssemblyBindingInfo
*info
)
741 ParserUserData user_data
= {
747 &publisher_policy_parser
, /* MonoParseHandler */
748 &user_data
, /* user_data */
749 NULL
, /* MonoImage (we don't need it right now)*/
750 TRUE
/* We are already inited */
753 mono_config_parse_file_with_context (&state
, filename
);
756 static MonoParseHandler
757 config_assemblybinding_parser
= {
758 "", /* We don't need to use declare an xml element */
760 publisher_policy_start
,
762 assembly_binding_end
,
767 mono_config_parse_assembly_bindings (const char *filename
, int amajor
, int aminor
, void *user_data
, void (*infocb
)(MonoAssemblyBindingInfo
*info
, void *user_data
))
769 MonoAssemblyBindingInfo info
;
777 pud
.info_parsed
= infocb
;
778 pud
.user_data
= user_data
;
780 state
.current
= &config_assemblybinding_parser
; /* MonoParseHandler */
781 state
.user_data
= &pud
;
782 state
.assembly
= NULL
; /* MonoImage (we don't need it right now)*/
783 state
.inited
= TRUE
; /* We are already inited */
785 mono_config_parse_file_with_context (&state
, filename
);