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/utils/mono-logger.h"
20 #if defined(__linux__)
21 #define CONFIG_OS "linux"
22 #elif defined(__APPLE__)
23 #define CONFIG_OS "osx"
25 #define CONFIG_OS "solaris"
26 #elif defined(__FreeBSD__)
27 #define CONFIG_OS "freebsd"
28 #elif defined(__NetBSD__)
29 #define CONFIG_OS "netbsd"
30 #elif defined(__OpenBSD__)
31 #define CONFIG_OS "openbsd"
32 #elif defined(__WIN32__)
33 #define CONFIG_OS "windows"
35 #define CONFIG_OS "aix"
37 #define CONFIG_OS "hpux"
39 #warning Unknown operating system
40 #define CONFIG_OS "unknownOS"
44 #define CONFIG_CPU "x86"
45 #elif defined(__x86_64__)
46 #define CONFIG_CPU "x86-64"
47 #elif defined(sparc) || defined(__sparc__)
48 #define CONFIG_CPU "sparc"
49 #elif defined(__ppc__) || defined(__powerpc__)
50 #define CONFIG_CPU "ppc"
51 #elif defined(__s390x__)
52 #define CONFIG_CPU "s390x"
53 #elif defined(__s390__)
54 #define CONFIG_CPU "s390"
55 #elif defined(__arm__)
56 #define CONFIG_CPU "arm"
57 #elif defined(__ia64__)
58 #define CONFIG_CPU "ia64"
59 #elif defined(__alpha__)
60 #define CONFIG_CPU "alpha"
61 #elif defined(hppa) || defined(__hppa__)
62 #define CONFIG_CPU "hppa"
63 #elif defined(mips) || defined(__mips) || defined(_mips)
64 #define CONFIG_CPU "mips"
67 #define CONFIG_CPU "unknownCPU"
70 static void start_element (GMarkupParseContext *context,
71 const gchar *element_name,
72 const gchar **attribute_names,
73 const gchar **attribute_values,
77 static void end_element (GMarkupParseContext *context,
78 const gchar *element_name,
82 static void parse_text (GMarkupParseContext *context,
88 static void passthrough (GMarkupParseContext *context,
94 static void parse_error (GMarkupParseContext *context,
98 static const GMarkupParser
107 static GHashTable *config_handlers;
109 /* when this interface is stable, export it. */
110 typedef struct MonoParseHandler MonoParseHandler;
112 struct MonoParseHandler {
113 const char *element_name;
114 void*(*init) (MonoImage *assembly);
115 void (*start) (gpointer user_data, const gchar *name,
116 const gchar **attributes,
117 const gchar **values);
118 void (*text) (gpointer user_data, const char *text, gsize test_len);
119 void (*end) (gpointer user_data, const char *name);
120 void (*finish) (gpointer user_data);
124 MonoParseHandler *current;
130 static void start_element (GMarkupParseContext *context,
131 const gchar *element_name,
132 const gchar **attribute_names,
133 const gchar **attribute_values,
137 ParseState *state = user_data;
138 if (!state->current) {
139 state->current = g_hash_table_lookup (config_handlers, element_name);
140 if (state->current && state->current->init)
141 state->user_data = state->current->init (state->assembly);
143 if (state->current && state->current->start)
144 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
147 static void end_element (GMarkupParseContext *context,
148 const gchar *element_name,
152 ParseState *state = user_data;
153 if (state->current) {
154 if (state->current->end)
155 state->current->end (state->user_data, element_name);
156 if (strcmp (state->current->element_name, element_name) == 0) {
157 if (state->current->finish)
158 state->current->finish (state->user_data);
159 state->current = NULL;
160 state->user_data = NULL;
165 static void parse_text (GMarkupParseContext *context,
171 ParseState *state = user_data;
172 if (state->current && state->current->text)
173 state->current->text (state->user_data, text, text_len);
176 static void passthrough (GMarkupParseContext *context,
185 static void parse_error (GMarkupParseContext *context,
192 arch_matches (const char* arch, const char *value)
194 char **splitted, **p;
196 if (value [0] == '!')
197 return !arch_matches (arch, value + 1);
198 p = splitted = g_strsplit (value, ",", 0);
200 if (strcmp (arch, *p) == 0) {
206 g_strfreev (splitted);
218 dllmap_init (MonoImage *assembly) {
219 DllInfo *info = g_new0 (DllInfo, 1);
220 info->assembly = assembly;
225 dllmap_start (gpointer user_data,
226 const gchar *element_name,
227 const gchar **attribute_names,
228 const gchar **attribute_values)
231 DllInfo *info = user_data;
233 if (strcmp (element_name, "dllmap") == 0) {
235 g_free (info->target);
236 info->dll = info->target = NULL;
237 info->ignore = FALSE;
238 for (i = 0; attribute_names [i]; ++i) {
239 if (strcmp (attribute_names [i], "dll") == 0)
240 info->dll = g_strdup (attribute_values [i]);
241 else if (strcmp (attribute_names [i], "target") == 0)
242 info->target = g_strdup (attribute_values [i]);
243 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
245 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
249 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
250 } else if (strcmp (element_name, "dllentry") == 0) {
251 const char *name = NULL, *target = NULL, *dll = NULL;
253 for (i = 0; attribute_names [i]; ++i) {
254 if (strcmp (attribute_names [i], "dll") == 0)
255 dll = attribute_values [i];
256 else if (strcmp (attribute_names [i], "target") == 0)
257 target = attribute_values [i];
258 else if (strcmp (attribute_names [i], "name") == 0)
259 name = attribute_values [i];
260 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
262 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
267 if (!info->ignore && !ignore)
268 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
273 dllmap_finish (gpointer user_data)
275 DllInfo *info = user_data;
278 g_free (info->target);
282 static const MonoParseHandler
292 static int inited = 0;
295 mono_config_init (void)
298 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
299 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
302 /* FIXME: error handling */
305 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
307 GMarkupParseContext *context;
312 context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
313 if (g_markup_parse_context_parse (context, text, len, NULL)) {
314 g_markup_parse_context_end_parse (context, NULL);
316 g_markup_parse_context_free (context);
319 /* If assembly is NULL, parse in the global context */
321 mono_config_parse_file_with_context (ParseState *state, const char *filename)
326 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
327 "Config attempting to parse: '%s'.", filename);
329 if (!g_file_get_contents (filename, &text, &len, NULL))
331 mono_config_parse_xml_with_context (state, text, len);
337 * mono_config_parse_memory:
338 * @buffer: a pointer to an string XML representation of the configuration
340 * Parses the configuration from a buffer
343 mono_config_parse_memory (const char *buffer)
345 ParseState state = {NULL};
347 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
351 mono_config_parse_file (const char *filename)
353 ParseState state = {NULL};
354 mono_config_parse_file_with_context (&state, filename);
358 * use the equivalent lookup code from the GAC when available.
359 * Depending on state, this should give something like:
360 * aname/version-pubtoken/
365 get_assembly_filename (MonoImage *image, int state)
369 return g_strdup (mono_image_get_name (image));
375 typedef struct _BundledConfig BundledConfig;
377 struct _BundledConfig {
380 const char* config_xml;
383 static BundledConfig *bundled_configs = NULL;
385 static const char *bundled_machine_config = NULL;
388 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
390 BundledConfig *bconfig;
392 bconfig = g_new0 (BundledConfig, 1);
393 bconfig->aname = assembly_name;
394 bconfig->config_xml = config_xml;
395 bconfig->next = bundled_configs;
396 bundled_configs = bconfig;
400 mono_config_string_for_assembly_file (const char *filename)
402 BundledConfig *bconfig;
404 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
405 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
406 return bconfig->config_xml;
412 mono_config_for_assembly (MonoImage *assembly)
414 ParseState state = {NULL};
416 char *aname, *cfg, *cfg_name;
417 const char *bundled_config;
420 state.assembly = assembly;
422 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
424 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
426 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
427 mono_config_parse_file_with_context (&state, cfg_name);
430 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
432 home = g_get_home_dir ();
434 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
435 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
436 got_it += mono_config_parse_file_with_context (&state, cfg);
439 #ifndef PLATFORM_WIN32
440 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
441 got_it += mono_config_parse_file_with_context (&state, cfg);
453 * @filename: the filename to load the configuration variables from.
455 * Pass a NULL filename to parse the default config files
456 * (or the file in the MONO_CONFIG env var).
459 mono_config_parse (const char *filename) {
462 #ifndef PLATFORM_WIN32
467 mono_config_parse_file (filename);
471 home = g_getenv ("MONO_CONFIG");
473 mono_config_parse_file (home);
477 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
478 mono_config_parse_file (mono_cfg);
481 #ifndef PLATFORM_WIN32
482 home = g_get_home_dir ();
483 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
484 mono_config_parse_file (user_cfg);
489 static const char *mono_cfg_dir = NULL;
491 /* Invoked during startup */
493 mono_set_config_dir (const char *dir)
495 /* If this variable is set, overrides the directory computed */
496 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
497 if (mono_cfg_dir == NULL)
498 mono_cfg_dir = g_strdup (dir);
502 mono_get_config_dir (void)
504 if (mono_cfg_dir == NULL)
505 mono_set_dirs (NULL, NULL);
511 mono_register_machine_config (const char *config_xml)
513 bundled_machine_config = config_xml;
517 mono_get_machine_config (void)
519 return bundled_machine_config;
523 publisher_policy_start (gpointer user_data,
524 const gchar *element_name,
525 const gchar **attribute_names,
526 const gchar **attribute_values)
528 MonoAssemblyBindingInfo *info;
532 if (!strcmp (element_name, "assemblyIdentity")) {
533 for (n = 0; attribute_names [n]; n++) {
534 const gchar *attribute_name = attribute_names [n];
536 if (!strcmp (attribute_name, "name"))
537 info->name = g_strdup (attribute_values [n]);
538 else if (!strcmp (attribute_name, "publicKeyToken")) {
539 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
540 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
541 } else if (!strcmp (attribute_name, "culture")) {
542 if (!strcmp (attribute_values [n], "neutral"))
543 info->culture = g_strdup ("");
545 info->culture = g_strdup (attribute_values [n]);
548 } else if (!strcmp (element_name, "bindingRedirect")) {
549 for (n = 0; attribute_names [n]; n++) {
550 const gchar *attribute_name = attribute_names [n];
552 if (!strcmp (attribute_name, "oldVersion")) {
553 gchar **numbers, **version, **versions;
554 gint major, minor, build, revision;
557 if (!strcmp (attribute_values [n], ""))
560 versions = g_strsplit (attribute_values [n], "-", 2);
561 version = g_strsplit (*versions, ".", 4);
563 /* We assign the values to gint vars to do the checks */
565 major = *numbers ? atoi (*numbers++) : -1;
566 minor = *numbers ? atoi (*numbers++) : -1;
567 build = *numbers ? atoi (*numbers++) : -1;
568 revision = *numbers ? atoi (*numbers) : -1;
569 g_strfreev (version);
570 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
571 g_strfreev (versions);
575 info->old_version_bottom.major = major;
576 info->old_version_bottom.minor = minor;
577 info->old_version_bottom.build = build;
578 info->old_version_bottom.revision = revision;
579 info->has_old_version_bottom = TRUE;
581 if (!*(versions + 1)) {
582 g_strfreev (versions);
586 numbers = version = g_strsplit (*(versions + 1), ".", 4);
587 major = *numbers ? atoi (*numbers++) : -1;
588 minor = *numbers ? atoi (*numbers++) : -1;
589 build = *numbers ? atoi (*numbers++) : -1;
590 revision = *numbers ? atoi (*numbers) : 1;
591 g_strfreev (version);
592 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
593 g_strfreev (versions);
597 info->old_version_top.major = major;
598 info->old_version_top.minor = minor;
599 info->old_version_top.build = build;
600 info->old_version_top.revision = revision;
601 info->has_old_version_top = TRUE;
603 g_strfreev (versions);
604 } else if (!strcmp (attribute_name, "newVersion")) {
605 gchar **numbers, **version;
608 if (!strcmp (attribute_values [n], ""))
611 numbers = version = g_strsplit (attribute_values [n], ".", 4);
612 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
613 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
614 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
615 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
616 info->has_new_version = TRUE;
617 g_strfreev (version);
623 static MonoParseHandler
624 publisher_policy_parser = {
625 "", /* We don't need to use declare an xml element */
627 publisher_policy_start,
634 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
637 &publisher_policy_parser, /* MonoParseHandler */
638 info, /* user_data */
639 NULL, /* MonoImage (we don't need it right now)*/
640 TRUE /* We are already inited */
643 mono_config_parse_file_with_context (&state, filename);