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;
386 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
388 BundledConfig *bconfig;
390 bconfig = g_new0 (BundledConfig, 1);
391 bconfig->aname = assembly_name;
392 bconfig->config_xml = config_xml;
393 bconfig->next = bundled_configs;
394 bundled_configs = bconfig;
398 mono_config_for_assembly (MonoImage *assembly)
400 ParseState state = {NULL};
402 char *aname, *cfg, *cfg_name;
404 BundledConfig *bconfig;
406 state.assembly = assembly;
408 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
409 if (bconfig->aname && strcmp (bconfig->aname, assembly->module_name) == 0)
410 mono_config_parse_xml_with_context (&state, bconfig->config_xml, strlen (bconfig->config_xml));
413 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
414 mono_config_parse_file_with_context (&state, cfg_name);
417 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
419 home = g_get_home_dir ();
421 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
422 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
423 got_it += mono_config_parse_file_with_context (&state, cfg);
426 #ifndef PLATFORM_WIN32
427 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
428 got_it += mono_config_parse_file_with_context (&state, cfg);
440 * @filename: the filename to load the configuration variables from.
442 * Pass a NULL filename to parse the default config files
443 * (or the file in the MONO_CONFIG env var).
446 mono_config_parse (const char *filename) {
449 #ifndef PLATFORM_WIN32
454 mono_config_parse_file (filename);
458 home = g_getenv ("MONO_CONFIG");
460 mono_config_parse_file (home);
464 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
465 mono_config_parse_file (mono_cfg);
468 #ifndef PLATFORM_WIN32
469 home = g_get_home_dir ();
470 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
471 mono_config_parse_file (user_cfg);
476 static const char *mono_cfg_dir = NULL;
478 /* Invoked during startup */
480 mono_set_config_dir (const char *dir)
482 /* If this variable is set, overrides the directory computed */
483 mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
484 if (mono_cfg_dir == NULL)
485 mono_cfg_dir = g_strdup (dir);
489 mono_get_config_dir (void)
491 if (mono_cfg_dir == NULL)
492 mono_set_dirs (NULL, NULL);
498 publisher_policy_start (gpointer user_data,
499 const gchar *element_name,
500 const gchar **attribute_names,
501 const gchar **attribute_values)
503 MonoAssemblyBindingInfo *info;
507 if (!strcmp (element_name, "assemblyIdentity")) {
508 for (n = 0; attribute_names [n]; n++) {
509 const gchar *attribute_name = attribute_names [n];
511 if (!strcmp (attribute_name, "name"))
512 info->name = g_strdup (attribute_values [n]);
513 else if (!strcmp (attribute_name, "publicKeyToken")) {
514 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
515 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
516 } else if (!strcmp (attribute_name, "culture")) {
517 if (!strcmp (attribute_values [n], "neutral"))
518 info->culture = g_strdup ("");
520 info->culture = g_strdup (attribute_values [n]);
523 } else if (!strcmp (element_name, "bindingRedirect")) {
524 for (n = 0; attribute_names [n]; n++) {
525 const gchar *attribute_name = attribute_names [n];
527 if (!strcmp (attribute_name, "oldVersion")) {
528 gchar **numbers, **version, **versions;
529 gint major, minor, build, revision;
532 if (!strcmp (attribute_values [n], ""))
535 versions = g_strsplit (attribute_values [n], "-", 2);
536 version = g_strsplit (*versions, ".", 4);
538 /* We assign the values to gint vars to do the checks */
540 major = *numbers ? atoi (*numbers++) : -1;
541 minor = *numbers ? atoi (*numbers++) : -1;
542 build = *numbers ? atoi (*numbers++) : -1;
543 revision = *numbers ? atoi (*numbers) : -1;
544 g_strfreev (version);
545 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
546 g_strfreev (versions);
550 info->old_version_bottom.major = major;
551 info->old_version_bottom.minor = minor;
552 info->old_version_bottom.build = build;
553 info->old_version_bottom.revision = revision;
554 info->has_old_version_bottom = TRUE;
556 if (!*(versions + 1)) {
557 g_strfreev (versions);
561 numbers = version = g_strsplit (*(versions + 1), ".", 4);
562 major = *numbers ? atoi (*numbers++) : -1;
563 minor = *numbers ? atoi (*numbers++) : -1;
564 build = *numbers ? atoi (*numbers++) : -1;
565 revision = *numbers ? atoi (*numbers) : 1;
566 g_strfreev (version);
567 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
568 g_strfreev (versions);
572 info->old_version_top.major = major;
573 info->old_version_top.minor = minor;
574 info->old_version_top.build = build;
575 info->old_version_top.revision = revision;
576 info->has_old_version_top = TRUE;
578 g_strfreev (versions);
579 } else if (!strcmp (attribute_name, "newVersion")) {
580 gchar **numbers, **version;
583 if (!strcmp (attribute_values [n], ""))
586 numbers = version = g_strsplit (attribute_values [n], ".", 4);
587 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
588 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
589 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
590 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
591 info->has_new_version = TRUE;
592 g_strfreev (version);
598 static MonoParseHandler
599 publisher_policy_parser = {
600 "", /* We don't need to use declare an xml element */
602 publisher_policy_start,
609 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
612 &publisher_policy_parser, /* MonoParseHandler */
613 info, /* user_data */
614 NULL, /* MonoImage (we don't need it right now)*/
615 TRUE /* We are already inited */
618 mono_config_parse_file_with_context (&state, filename);