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"
42 #elif defined(__HAIKU__)
43 #define CONFIG_OS "haiku"
45 #warning Unknown operating system
46 #define CONFIG_OS "unknownOS"
51 #define CONFIG_CPU "x86"
52 #define CONFIG_WORDSIZE "32"
53 #elif defined(__x86_64__)
54 #define CONFIG_CPU "x86-64"
55 #define CONFIG_WORDSIZE "64"
56 #elif defined(sparc) || defined(__sparc__)
57 #define CONFIG_CPU "sparc"
58 #define CONFIG_WORDSIZE "32"
59 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
60 #define CONFIG_WORDSIZE "64"
61 #ifdef __mono_ppc_ilp32__
62 # define CONFIG_CPU "ppc64ilp32"
64 # define CONFIG_CPU "ppc64"
66 #elif defined(__ppc__) || defined(__powerpc__)
67 #define CONFIG_CPU "ppc"
68 #define CONFIG_WORDSIZE "32"
69 #elif defined(__s390x__)
70 #define CONFIG_CPU "s390x"
71 #define CONFIG_WORDSIZE "64"
72 #elif defined(__s390__)
73 #define CONFIG_CPU "s390"
74 #define CONFIG_WORDSIZE "32"
75 #elif defined(__arm__)
76 #define CONFIG_CPU "arm"
77 #define CONFIG_WORDSIZE "32"
78 #elif defined(__ia64__)
79 #define CONFIG_CPU "ia64"
80 #define CONFIG_WORDSIZE "64"
81 #elif defined(__alpha__)
82 #define CONFIG_CPU "alpha"
83 #define CONFIG_WORDSIZE "64"
84 #elif defined(hppa) || defined(__hppa__)
85 #define CONFIG_CPU "hppa"
86 #define CONFIG_WORDSIZE "32"
87 #elif defined(mips) || defined(__mips) || defined(_mips)
88 #define CONFIG_CPU "mips"
89 #define CONFIG_WORDSIZE "32"
92 #define CONFIG_CPU "unknownCPU"
96 static void start_element (GMarkupParseContext
*context
,
97 const gchar
*element_name
,
98 const gchar
**attribute_names
,
99 const gchar
**attribute_values
,
103 static void end_element (GMarkupParseContext
*context
,
104 const gchar
*element_name
,
108 static void parse_text (GMarkupParseContext
*context
,
114 static void passthrough (GMarkupParseContext
*context
,
120 static void parse_error (GMarkupParseContext
*context
,
124 static const GMarkupParser
133 static GHashTable
*config_handlers
;
135 static const char *mono_cfg_dir
= NULL
;
136 static char *mono_cfg_dir_allocated
= NULL
;
138 /* when this interface is stable, export it. */
139 typedef struct MonoParseHandler MonoParseHandler
;
141 struct MonoParseHandler
{
142 const char *element_name
;
143 void*(*init
) (MonoImage
*assembly
);
144 void (*start
) (gpointer user_data
, const gchar
*name
,
145 const gchar
**attributes
,
146 const gchar
**values
);
147 void (*text
) (gpointer user_data
, const char *text
, gsize test_len
);
148 void (*end
) (gpointer user_data
, const char *name
);
149 void (*finish
) (gpointer user_data
);
153 MonoAssemblyBindingInfo
*info
;
154 void (*info_parsed
)(MonoAssemblyBindingInfo
*info
, void *user_data
);
159 MonoParseHandler
*current
;
165 static void start_element (GMarkupParseContext
*context
,
166 const gchar
*element_name
,
167 const gchar
**attribute_names
,
168 const gchar
**attribute_values
,
172 ParseState
*state
= user_data
;
173 if (!state
->current
) {
174 state
->current
= g_hash_table_lookup (config_handlers
, element_name
);
175 if (state
->current
&& state
->current
->init
)
176 state
->user_data
= state
->current
->init (state
->assembly
);
178 if (state
->current
&& state
->current
->start
)
179 state
->current
->start (state
->user_data
, element_name
, attribute_names
, attribute_values
);
182 static void end_element (GMarkupParseContext
*context
,
183 const gchar
*element_name
,
187 ParseState
*state
= user_data
;
188 if (state
->current
) {
189 if (state
->current
->end
)
190 state
->current
->end (state
->user_data
, element_name
);
191 if (strcmp (state
->current
->element_name
, element_name
) == 0) {
192 if (state
->current
->finish
)
193 state
->current
->finish (state
->user_data
);
194 state
->current
= NULL
;
195 state
->user_data
= NULL
;
200 static void parse_text (GMarkupParseContext
*context
,
206 ParseState
*state
= user_data
;
207 if (state
->current
&& state
->current
->text
)
208 state
->current
->text (state
->user_data
, text
, text_len
);
211 static void passthrough (GMarkupParseContext
*context
,
220 static void parse_error (GMarkupParseContext
*context
,
224 ParseState
*state
= user_data
;
226 const gchar
*filename
;
228 filename
= state
&& state
->user_data
? (gchar
*) state
->user_data
: "<unknown>";
229 msg
= error
&& error
->message
? error
->message
: "";
230 g_warning ("Error parsing %s: %s", filename
, msg
);
234 arch_matches (const char* arch
, const char *value
)
236 char **splitted
, **p
;
238 if (value
[0] == '!')
239 return !arch_matches (arch
, value
+ 1);
240 p
= splitted
= g_strsplit (value
, ",", 0);
242 if (strcmp (arch
, *p
) == 0) {
248 g_strfreev (splitted
);
260 dllmap_init (MonoImage
*assembly
) {
261 DllInfo
*info
= g_new0 (DllInfo
, 1);
262 info
->assembly
= assembly
;
267 dllmap_start (gpointer user_data
,
268 const gchar
*element_name
,
269 const gchar
**attribute_names
,
270 const gchar
**attribute_values
)
273 DllInfo
*info
= user_data
;
275 if (strcmp (element_name
, "dllmap") == 0) {
277 g_free (info
->target
);
278 info
->dll
= info
->target
= NULL
;
279 info
->ignore
= FALSE
;
280 for (i
= 0; attribute_names
[i
]; ++i
) {
281 if (strcmp (attribute_names
[i
], "dll") == 0)
282 info
->dll
= g_strdup (attribute_values
[i
]);
283 else if (strcmp (attribute_names
[i
], "target") == 0)
284 info
->target
= g_strdup (attribute_values
[i
]);
285 else if (strcmp (attribute_names
[i
], "os") == 0 && !arch_matches (CONFIG_OS
, attribute_values
[i
]))
287 else if (strcmp (attribute_names
[i
], "cpu") == 0 && !arch_matches (CONFIG_CPU
, attribute_values
[i
]))
289 else if (strcmp (attribute_names
[i
], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE
, attribute_values
[i
]))
293 mono_dllmap_insert (info
->assembly
, info
->dll
, NULL
, info
->target
, NULL
);
294 } else if (strcmp (element_name
, "dllentry") == 0) {
295 const char *name
= NULL
, *target
= NULL
, *dll
= NULL
;
297 for (i
= 0; attribute_names
[i
]; ++i
) {
298 if (strcmp (attribute_names
[i
], "dll") == 0)
299 dll
= attribute_values
[i
];
300 else if (strcmp (attribute_names
[i
], "target") == 0)
301 target
= attribute_values
[i
];
302 else if (strcmp (attribute_names
[i
], "name") == 0)
303 name
= attribute_values
[i
];
304 else if (strcmp (attribute_names
[i
], "os") == 0 && !arch_matches (CONFIG_OS
, attribute_values
[i
]))
306 else if (strcmp (attribute_names
[i
], "cpu") == 0 && !arch_matches (CONFIG_CPU
, attribute_values
[i
]))
308 else if (strcmp (attribute_names
[i
], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE
, attribute_values
[i
]))
313 if (!info
->ignore
&& !ignore
)
314 mono_dllmap_insert (info
->assembly
, info
->dll
, name
, dll
, target
);
319 dllmap_finish (gpointer user_data
)
321 DllInfo
*info
= user_data
;
324 g_free (info
->target
);
328 static const MonoParseHandler
339 legacyUEP_start (gpointer user_data
,
340 const gchar
*element_name
,
341 const gchar
**attribute_names
,
342 const gchar
**attribute_values
) {
343 if ((strcmp (element_name
, "legacyUnhandledExceptionPolicy") == 0) &&
344 (attribute_names
[0] != NULL
) &&
345 (strcmp (attribute_names
[0], "enabled") == 0)) {
346 if ((strcmp (attribute_values
[0], "1") == 0) ||
347 (g_ascii_strcasecmp (attribute_values
[0], "true") == 0)) {
348 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY
);
353 static const MonoParseHandler
354 legacyUEP_handler
= {
355 "legacyUnhandledExceptionPolicy",
363 static int inited
= 0;
366 mono_config_init (void)
369 config_handlers
= g_hash_table_new (g_str_hash
, g_str_equal
);
370 g_hash_table_insert (config_handlers
, (gpointer
) dllmap_handler
.element_name
, (gpointer
) &dllmap_handler
);
371 g_hash_table_insert (config_handlers
, (gpointer
) legacyUEP_handler
.element_name
, (gpointer
) &legacyUEP_handler
);
375 mono_config_cleanup (void)
378 g_hash_table_destroy (config_handlers
);
379 g_free (mono_cfg_dir_allocated
);
382 /* FIXME: error handling */
385 mono_config_parse_xml_with_context (ParseState
*state
, const char *text
, gsize len
)
387 GMarkupParseContext
*context
;
392 context
= g_markup_parse_context_new (&mono_parser
, 0, state
, NULL
);
393 if (g_markup_parse_context_parse (context
, text
, len
, NULL
)) {
394 g_markup_parse_context_end_parse (context
, NULL
);
396 g_markup_parse_context_free (context
);
399 /* If assembly is NULL, parse in the global context */
401 mono_config_parse_file_with_context (ParseState
*state
, const char *filename
)
407 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_CONFIG
,
408 "Config attempting to parse: '%s'.", filename
);
410 if (!g_file_get_contents (filename
, &text
, &len
, NULL
))
415 if (len
> 3 && text
[0] == '\xef' && text
[1] == (gchar
) '\xbb' && text
[2] == '\xbf')
416 offset
= 3; /* Skip UTF-8 BOM */
417 if (state
->user_data
== NULL
)
418 state
->user_data
= (gpointer
) filename
;
419 mono_config_parse_xml_with_context (state
, text
+ offset
, len
- offset
);
425 * mono_config_parse_memory:
426 * @buffer: a pointer to an string XML representation of the configuration
428 * Parses the configuration from a buffer
431 mono_config_parse_memory (const char *buffer
)
433 ParseState state
= {NULL
};
435 state
.user_data
= (gpointer
) "<buffer>";
436 mono_config_parse_xml_with_context (&state
, buffer
, strlen (buffer
));
440 mono_config_parse_file (const char *filename
)
442 ParseState state
= {NULL
};
443 state
.user_data
= (gpointer
) filename
;
444 mono_config_parse_file_with_context (&state
, filename
);
448 * use the equivalent lookup code from the GAC when available.
449 * Depending on state, this should give something like:
450 * aname/version-pubtoken/
455 get_assembly_filename (MonoImage
*image
, int state
)
459 return g_strdup (mono_image_get_name (image
));
465 typedef struct _BundledConfig BundledConfig
;
467 struct _BundledConfig
{
470 const char* config_xml
;
473 static BundledConfig
*bundled_configs
= NULL
;
475 static const char *bundled_machine_config
= NULL
;
478 mono_register_config_for_assembly (const char* assembly_name
, const char* config_xml
)
480 BundledConfig
*bconfig
;
482 bconfig
= g_new0 (BundledConfig
, 1);
483 bconfig
->aname
= assembly_name
;
484 bconfig
->config_xml
= config_xml
;
485 bconfig
->next
= bundled_configs
;
486 bundled_configs
= bconfig
;
490 mono_config_string_for_assembly_file (const char *filename
)
492 BundledConfig
*bconfig
;
494 for (bconfig
= bundled_configs
; bconfig
; bconfig
= bconfig
->next
) {
495 if (bconfig
->aname
&& strcmp (bconfig
->aname
, filename
) == 0)
496 return bconfig
->config_xml
;
502 mono_config_for_assembly (MonoImage
*assembly
)
504 ParseState state
= {NULL
};
506 char *aname
, *cfg
, *cfg_name
;
507 const char *bundled_config
;
510 state
.assembly
= assembly
;
512 bundled_config
= mono_config_string_for_assembly_file (assembly
->module_name
);
513 if (bundled_config
) {
514 state
.user_data
= (gpointer
) "<bundled>";
515 mono_config_parse_xml_with_context (&state
, bundled_config
, strlen (bundled_config
));
518 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_filename (assembly
));
519 mono_config_parse_file_with_context (&state
, cfg_name
);
522 cfg_name
= g_strdup_printf ("%s.config", mono_image_get_name (assembly
));
524 home
= g_get_home_dir ();
526 for (i
= 0; (aname
= get_assembly_filename (assembly
, i
)) != NULL
; ++i
) {
527 cfg
= g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname
, cfg_name
, NULL
);
528 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
532 cfg
= g_build_filename (home
, ".mono", "assemblies", aname
, cfg_name
, NULL
);
533 got_it
+= mono_config_parse_file_with_context (&state
, cfg
);
545 * @filename: the filename to load the configuration variables from.
547 * Pass a NULL filename to parse the default config files
548 * (or the file in the MONO_CONFIG env var).
551 mono_config_parse (const char *filename
) {
559 mono_config_parse_file (filename
);
563 home
= g_getenv ("MONO_CONFIG");
565 mono_config_parse_file (home
);
569 mono_cfg
= g_build_filename (mono_get_config_dir (), "mono", "config", NULL
);
570 mono_config_parse_file (mono_cfg
);
574 home
= g_get_home_dir ();
575 user_cfg
= g_strconcat (home
, G_DIR_SEPARATOR_S
, ".mono/config", NULL
);
576 mono_config_parse_file (user_cfg
);
581 /* Invoked during startup */
583 mono_set_config_dir (const char *dir
)
585 /* If this variable is set, overrides the directory computed */
586 mono_cfg_dir
= g_getenv ("MONO_CFG_DIR");
587 if (mono_cfg_dir
== NULL
)
588 mono_cfg_dir
= mono_cfg_dir_allocated
= g_strdup (dir
);
592 mono_get_config_dir (void)
594 if (mono_cfg_dir
== NULL
)
595 mono_set_dirs (NULL
, NULL
);
601 mono_register_machine_config (const char *config_xml
)
603 bundled_machine_config
= config_xml
;
607 mono_get_machine_config (void)
609 return bundled_machine_config
;
613 assembly_binding_end (gpointer user_data
, const char *element_name
)
615 ParserUserData
*pud
= user_data
;
617 if (!strcmp (element_name
, "dependentAssembly")) {
618 if (pud
->info_parsed
&& pud
->info
) {
619 pud
->info_parsed (pud
->info
, pud
->user_data
);
620 g_free (pud
->info
->name
);
621 g_free (pud
->info
->culture
);
627 publisher_policy_start (gpointer user_data
,
628 const gchar
*element_name
,
629 const gchar
**attribute_names
,
630 const gchar
**attribute_values
)
633 MonoAssemblyBindingInfo
*info
;
638 if (!strcmp (element_name
, "dependentAssembly")) {
640 info
->culture
= NULL
;
641 info
->has_old_version_bottom
= FALSE
;
642 info
->has_old_version_top
= FALSE
;
643 info
->has_new_version
= FALSE
;
644 info
->is_valid
= FALSE
;
645 memset (&info
->old_version_bottom
, 0, sizeof (info
->old_version_bottom
));
646 memset (&info
->old_version_top
, 0, sizeof (info
->old_version_top
));
647 memset (&info
->new_version
, 0, sizeof (info
->new_version
));
648 } if (!strcmp (element_name
, "assemblyIdentity")) {
649 for (n
= 0; attribute_names
[n
]; n
++) {
650 const gchar
*attribute_name
= attribute_names
[n
];
652 if (!strcmp (attribute_name
, "name"))
653 info
->name
= g_strdup (attribute_values
[n
]);
654 else if (!strcmp (attribute_name
, "publicKeyToken")) {
655 if (strlen (attribute_values
[n
]) == MONO_PUBLIC_KEY_TOKEN_LENGTH
- 1)
656 g_strlcpy ((char *) info
->public_key_token
, attribute_values
[n
], MONO_PUBLIC_KEY_TOKEN_LENGTH
);
657 } else if (!strcmp (attribute_name
, "culture")) {
658 if (!strcmp (attribute_values
[n
], "neutral"))
659 info
->culture
= g_strdup ("");
661 info
->culture
= g_strdup (attribute_values
[n
]);
664 } else if (!strcmp (element_name
, "bindingRedirect")) {
665 for (n
= 0; attribute_names
[n
]; n
++) {
666 const gchar
*attribute_name
= attribute_names
[n
];
668 if (!strcmp (attribute_name
, "oldVersion")) {
669 gchar
**numbers
, **version
, **versions
;
670 gint major
, minor
, build
, revision
;
673 if (!strcmp (attribute_values
[n
], ""))
676 versions
= g_strsplit (attribute_values
[n
], "-", 2);
677 version
= g_strsplit (*versions
, ".", 4);
679 /* We assign the values to gint vars to do the checks */
681 major
= *numbers
? atoi (*numbers
++) : -1;
682 minor
= *numbers
? atoi (*numbers
++) : -1;
683 build
= *numbers
? atoi (*numbers
++) : -1;
684 revision
= *numbers
? atoi (*numbers
) : -1;
685 g_strfreev (version
);
686 if (major
< 0 || minor
< 0 || build
< 0 || revision
< 0) {
687 g_strfreev (versions
);
691 info
->old_version_bottom
.major
= major
;
692 info
->old_version_bottom
.minor
= minor
;
693 info
->old_version_bottom
.build
= build
;
694 info
->old_version_bottom
.revision
= revision
;
695 info
->has_old_version_bottom
= TRUE
;
697 if (!*(versions
+ 1)) {
698 g_strfreev (versions
);
702 numbers
= version
= g_strsplit (*(versions
+ 1), ".", 4);
703 major
= *numbers
? atoi (*numbers
++) : -1;
704 minor
= *numbers
? atoi (*numbers
++) : -1;
705 build
= *numbers
? atoi (*numbers
++) : -1;
706 revision
= *numbers
? atoi (*numbers
) : 1;
707 g_strfreev (version
);
708 if (major
< 0 || minor
< 0 || build
< 0 || revision
< 0) {
709 g_strfreev (versions
);
713 info
->old_version_top
.major
= major
;
714 info
->old_version_top
.minor
= minor
;
715 info
->old_version_top
.build
= build
;
716 info
->old_version_top
.revision
= revision
;
717 info
->has_old_version_top
= TRUE
;
719 g_strfreev (versions
);
720 } else if (!strcmp (attribute_name
, "newVersion")) {
721 gchar
**numbers
, **version
;
724 if (!strcmp (attribute_values
[n
], ""))
727 numbers
= version
= g_strsplit (attribute_values
[n
], ".", 4);
728 info
->new_version
.major
= *numbers
? atoi (*numbers
++) : -1;
729 info
->new_version
.minor
= *numbers
? atoi (*numbers
++) : -1;
730 info
->new_version
.build
= *numbers
? atoi (*numbers
++) : -1;
731 info
->new_version
.revision
= *numbers
? atoi (*numbers
) : -1;
732 info
->has_new_version
= TRUE
;
733 g_strfreev (version
);
739 static MonoParseHandler
740 publisher_policy_parser
= {
741 "", /* We don't need to use declare an xml element */
743 publisher_policy_start
,
750 mono_config_parse_publisher_policy (const gchar
*filename
, MonoAssemblyBindingInfo
*info
)
752 ParserUserData user_data
= {
758 &publisher_policy_parser
, /* MonoParseHandler */
759 &user_data
, /* user_data */
760 NULL
, /* MonoImage (we don't need it right now)*/
761 TRUE
/* We are already inited */
764 mono_config_parse_file_with_context (&state
, filename
);
767 static MonoParseHandler
768 config_assemblybinding_parser
= {
769 "", /* We don't need to use declare an xml element */
771 publisher_policy_start
,
773 assembly_binding_end
,
778 mono_config_parse_assembly_bindings (const char *filename
, int amajor
, int aminor
, void *user_data
, void (*infocb
)(MonoAssemblyBindingInfo
*info
, void *user_data
))
780 MonoAssemblyBindingInfo info
;
788 pud
.info_parsed
= infocb
;
789 pud
.user_data
= user_data
;
791 state
.current
= &config_assemblybinding_parser
; /* MonoParseHandler */
792 state
.user_data
= &pud
;
793 state
.assembly
= NULL
; /* MonoImage (we don't need it right now)*/
794 state
.inited
= TRUE
; /* We are already inited */
796 mono_config_parse_file_with_context (&state
, filename
);