[mini] Always emit safepoints, except WASM
[mono-project.git] / mono / metadata / mono-config.c
blobb44c84556b5fea316102ec1f093e95ae6b25822d
1 /**
2 * \file
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)
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12 #include "config.h"
13 #include <glib.h>
14 #include <string.h>
16 #include "mono/metadata/assembly.h"
17 #include "mono/metadata/loader.h"
18 #include "mono/metadata/mono-config.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include "mono/metadata/object-internals.h"
21 #include "mono/utils/mono-logger-internals.h"
23 #if defined(TARGET_PS3)
24 #define CONFIG_OS "CellOS"
25 #elif defined(__linux__)
26 #define CONFIG_OS "linux"
27 #elif defined(__APPLE__)
28 #define CONFIG_OS "osx"
29 #elif defined(sun)
30 #define CONFIG_OS "solaris"
31 #elif defined(__FreeBSD__)
32 #define CONFIG_OS "freebsd"
33 #elif defined(__NetBSD__)
34 #define CONFIG_OS "netbsd"
35 #elif defined(__OpenBSD__)
36 #define CONFIG_OS "openbsd"
37 #elif defined(__WIN32__) || defined(TARGET_WIN32)
38 #define CONFIG_OS "windows"
39 #elif defined(_IBMR2)
40 #define CONFIG_OS "aix"
41 #elif defined(__hpux)
42 #define CONFIG_OS "hpux"
43 #elif defined(__HAIKU__)
44 #define CONFIG_OS "haiku"
45 #elif defined (TARGET_WASM)
46 #define CONFIG_OS "wasm"
47 #else
48 #warning Unknown operating system
49 #define CONFIG_OS "unknownOS"
50 #endif
52 #ifndef CONFIG_CPU
53 #if defined(__i386__) || defined(TARGET_X86)
54 #define CONFIG_CPU "x86"
55 #define CONFIG_WORDSIZE "32"
56 #elif defined(__x86_64__) || defined(TARGET_AMD64)
57 #define CONFIG_CPU "x86-64"
58 #define CONFIG_WORDSIZE "64"
59 #elif defined(sparc) || defined(__sparc__)
60 #define CONFIG_CPU "sparc"
61 #define CONFIG_WORDSIZE "32"
62 #elif defined(__ppc64__) || defined(__powerpc64__) || defined(_ARCH_64) || defined(TARGET_POWERPC)
63 #define CONFIG_WORDSIZE "64"
64 #ifdef __mono_ppc_ilp32__
65 # define CONFIG_CPU "ppc64ilp32"
66 #else
67 # define CONFIG_CPU "ppc64"
68 #endif
69 #elif defined(__ppc__) || defined(__powerpc__)
70 #define CONFIG_CPU "ppc"
71 #define CONFIG_WORDSIZE "32"
72 #elif defined(__s390x__)
73 #define CONFIG_CPU "s390x"
74 #define CONFIG_WORDSIZE "64"
75 #elif defined(__s390__)
76 #define CONFIG_CPU "s390"
77 #define CONFIG_WORDSIZE "32"
78 #elif defined(__arm__)
79 #define CONFIG_CPU "arm"
80 #define CONFIG_WORDSIZE "32"
81 #elif defined(__aarch64__)
82 #define CONFIG_CPU "armv8"
83 #define CONFIG_WORDSIZE "64"
84 #elif defined(mips) || defined(__mips) || defined(_mips)
85 #define CONFIG_CPU "mips"
86 #define CONFIG_WORDSIZE "32"
87 #elif defined (TARGET_RISCV32)
88 #define CONFIG_CPU "riscv32"
89 #define CONFIG_WORDSIZE "32"
90 #elif defined (TARGET_RISCV64)
91 #define CONFIG_CPU "riscv64"
92 #define CONFIG_WORDSIZE "64"
93 #elif defined(TARGET_WASM)
94 #define CONFIG_CPU "wasm"
95 #define CONFIG_WORDSIZE "32"
96 #else
97 #error Unknown CPU
98 #define CONFIG_CPU "unknownCPU"
99 #endif
100 #endif
102 static void mono_config_for_assembly_internal (MonoImage *assembly);
105 * mono_config_get_os:
107 * Returns the operating system that Mono is running on, as used for dllmap entries.
109 const char *
110 mono_config_get_os (void)
112 return CONFIG_OS;
116 * mono_config_get_cpu:
118 * Returns the architecture that Mono is running on, as used for dllmap entries.
120 const char *
121 mono_config_get_cpu (void)
123 return CONFIG_CPU;
127 * mono_config_get_wordsize:
129 * Returns the word size that Mono is running on, as used for dllmap entries.
131 const char *
132 mono_config_get_wordsize (void)
134 return CONFIG_WORDSIZE;
137 static void start_element (GMarkupParseContext *context,
138 const gchar *element_name,
139 const gchar **attribute_names,
140 const gchar **attribute_values,
141 gpointer user_data,
142 GError **gerror);
144 static void end_element (GMarkupParseContext *context,
145 const gchar *element_name,
146 gpointer user_data,
147 GError **gerror);
149 static void parse_text (GMarkupParseContext *context,
150 const gchar *text,
151 gsize text_len,
152 gpointer user_data,
153 GError **gerror);
155 static void passthrough (GMarkupParseContext *context,
156 const gchar *text,
157 gsize text_len,
158 gpointer user_data,
159 GError **gerror);
161 static void parse_error (GMarkupParseContext *context,
162 GError *gerror,
163 gpointer user_data);
165 static const GMarkupParser
166 mono_parser = {
167 start_element,
168 end_element,
169 parse_text,
170 passthrough,
171 parse_error
174 static GHashTable *config_handlers;
176 static char *mono_cfg_dir = NULL;
178 /* when this interface is stable, export it. */
179 typedef struct MonoParseHandler MonoParseHandler;
181 struct MonoParseHandler {
182 const char *element_name;
183 void*(*init) (MonoImage *assembly);
184 void (*start) (gpointer user_data, const gchar *name,
185 const gchar **attributes,
186 const gchar **values);
187 void (*text) (gpointer user_data, const char *text, gsize test_len);
188 void (*end) (gpointer user_data, const char *name);
189 void (*finish) (gpointer user_data);
192 typedef struct {
193 MonoAssemblyBindingInfo *info;
194 void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
195 void *user_data;
196 } ParserUserData;
198 typedef struct {
199 MonoParseHandler *current;
200 void *user_data;
201 MonoImage *assembly;
202 int inited;
203 } MonoConfigParseState;
205 static void start_element (GMarkupParseContext *context,
206 const gchar *element_name,
207 const gchar **attribute_names,
208 const gchar **attribute_values,
209 gpointer user_data,
210 GError **gerror)
212 MonoConfigParseState *state = (MonoConfigParseState *)user_data;
213 if (!state->current) {
214 state->current = (MonoParseHandler *)g_hash_table_lookup (config_handlers, element_name);
215 if (state->current && state->current->init)
216 state->user_data = state->current->init (state->assembly);
218 if (state->current && state->current->start)
219 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
222 static void end_element (GMarkupParseContext *context,
223 const gchar *element_name,
224 gpointer user_data,
225 GError **gerror)
227 MonoConfigParseState *state = (MonoConfigParseState *)user_data;
228 if (state->current) {
229 if (state->current->end)
230 state->current->end (state->user_data, element_name);
231 if (strcmp (state->current->element_name, element_name) == 0) {
232 if (state->current->finish)
233 state->current->finish (state->user_data);
234 state->current = NULL;
235 state->user_data = NULL;
240 static void parse_text (GMarkupParseContext *context,
241 const gchar *text,
242 gsize text_len,
243 gpointer user_data,
244 GError **gerror)
246 MonoConfigParseState *state = (MonoConfigParseState *)user_data;
247 if (state->current && state->current->text)
248 state->current->text (state->user_data, text, text_len);
251 static void passthrough (GMarkupParseContext *context,
252 const gchar *text,
253 gsize text_len,
254 gpointer user_data,
255 GError **gerror)
257 /* do nothing */
260 static void parse_error (GMarkupParseContext *context,
261 GError *gerror,
262 gpointer user_data)
264 MonoConfigParseState *state = (MonoConfigParseState *)user_data;
265 const gchar *msg;
266 const gchar *filename;
268 filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
269 msg = gerror && gerror->message ? gerror->message : "";
270 g_warning ("Error parsing %s: %s", filename, msg);
273 static int
274 arch_matches (const char* arch, const char *value)
276 char **splitted, **p;
277 int found = FALSE;
278 if (value [0] == '!')
279 return !arch_matches (arch, value + 1);
280 p = splitted = g_strsplit (value, ",", 0);
281 while (*p) {
282 if (strcmp (arch, *p) == 0) {
283 found = TRUE;
284 break;
286 p++;
288 g_strfreev (splitted);
289 return found;
292 typedef struct {
293 char *dll;
294 char *target;
295 int ignore;
296 MonoImage *assembly;
297 } DllInfo;
299 static void*
300 dllmap_init (MonoImage *assembly) {
301 DllInfo *info = g_new0 (DllInfo, 1);
302 info->assembly = assembly;
303 return info;
306 static void
307 dllmap_start (gpointer user_data,
308 const gchar *element_name,
309 const gchar **attribute_names,
310 const gchar **attribute_values)
312 int i;
313 DllInfo *info = (DllInfo *)user_data;
315 if (strcmp (element_name, "dllmap") == 0) {
316 g_free (info->dll);
317 g_free (info->target);
318 info->dll = info->target = NULL;
319 info->ignore = FALSE;
320 for (i = 0; attribute_names [i]; ++i) {
321 if (strcmp (attribute_names [i], "dll") == 0)
322 info->dll = g_strdup (attribute_values [i]);
323 else if (strcmp (attribute_names [i], "target") == 0){
324 const char* p = strstr (attribute_values [i], "$mono_libdir");
325 if (p != NULL){
326 char *libdir = mono_native_getrootdir ();
327 size_t libdir_len = strlen (libdir);
328 char *result;
330 result = (char *)g_malloc (libdir_len-strlen("$mono_libdir")+strlen(attribute_values[i])+1);
331 strncpy (result, attribute_values[i], p-attribute_values[i]);
332 strcpy (result+(p-attribute_values[i]), libdir);
333 g_free (libdir);
334 strcat (result, p+strlen("$mono_libdir"));
335 info->target = result;
336 } else
337 info->target = g_strdup (attribute_values [i]);
338 } else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
339 info->ignore = TRUE;
340 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
341 info->ignore = TRUE;
342 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
343 info->ignore = TRUE;
345 if (!info->ignore)
346 mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
347 } else if (strcmp (element_name, "dllentry") == 0) {
348 const char *name = NULL, *target = NULL, *dll = NULL;
349 int ignore = FALSE;
350 for (i = 0; attribute_names [i]; ++i) {
351 if (strcmp (attribute_names [i], "dll") == 0)
352 dll = attribute_values [i];
353 else if (strcmp (attribute_names [i], "target") == 0)
354 target = attribute_values [i];
355 else if (strcmp (attribute_names [i], "name") == 0)
356 name = attribute_values [i];
357 else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
358 ignore = TRUE;
359 else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
360 ignore = TRUE;
361 else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
362 ignore = TRUE;
364 if (!dll)
365 dll = info->dll;
366 if (!info->ignore && !ignore)
367 mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
371 static void
372 dllmap_finish (gpointer user_data)
374 DllInfo *info = (DllInfo *)user_data;
376 g_free (info->dll);
377 g_free (info->target);
378 g_free (info);
381 static const MonoParseHandler
382 dllmap_handler = {
383 "dllmap",
384 dllmap_init,
385 dllmap_start,
386 NULL, /* text */
387 NULL, /* end */
388 dllmap_finish
391 static void
392 legacyUEP_start (gpointer user_data,
393 const gchar *element_name,
394 const gchar **attribute_names,
395 const gchar **attribute_values) {
396 if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
397 (attribute_names [0] != NULL) &&
398 (strcmp (attribute_names [0], "enabled") == 0)) {
399 if ((strcmp (attribute_values [0], "1") == 0) ||
400 (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
401 mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
406 static const MonoParseHandler
407 legacyUEP_handler = {
408 "legacyUnhandledExceptionPolicy",
409 NULL, /* init */
410 legacyUEP_start,
411 NULL, /* text */
412 NULL, /* end */
413 NULL, /* finish */
416 static void
417 aot_cache_start (gpointer user_data,
418 const gchar *element_name,
419 const gchar **attribute_names,
420 const gchar **attribute_values)
422 int i;
423 MonoAotCacheConfig *config;
425 if (strcmp (element_name, "aotcache") != 0)
426 return;
428 config = mono_get_aot_cache_config ();
430 /* Per-app configuration */
431 for (i = 0; attribute_names [i]; ++i) {
432 if (!strcmp (attribute_names [i], "app")) {
433 config->apps = g_slist_prepend (config->apps, g_strdup (attribute_values [i]));
437 /* Global configuration */
438 for (i = 0; attribute_names [i]; ++i) {
439 if (!strcmp (attribute_names [i], "assemblies")) {
440 char **parts, **ptr;
441 char *part;
443 parts = g_strsplit (attribute_values [i], " ", -1);
444 for (ptr = parts; ptr && *ptr; ptr ++) {
445 part = *ptr;
446 config->assemblies = g_slist_prepend (config->assemblies, g_strdup (part));
448 g_strfreev (parts);
449 } else if (!strcmp (attribute_names [i], "options")) {
450 config->aot_options = g_strdup (attribute_values [i]);
455 static const MonoParseHandler
456 aot_cache_handler = {
457 "aotcache",
458 NULL, /* init */
459 aot_cache_start,
460 NULL, /* text */
461 NULL, /* end */
462 NULL, /* finish */
465 static int inited = 0;
467 static void
468 mono_config_init (void)
470 inited = 1;
471 config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
472 g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
473 g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
474 g_hash_table_insert (config_handlers, (gpointer) aot_cache_handler.element_name, (gpointer) &aot_cache_handler);
478 * mono_config_cleanup:
480 void
481 mono_config_cleanup (void)
483 if (config_handlers)
484 g_hash_table_destroy (config_handlers);
485 g_free (mono_cfg_dir);
488 /* FIXME: error handling */
490 static void
491 mono_config_parse_xml_with_context (MonoConfigParseState *state, const char *text, gsize len)
493 GMarkupParseContext *context;
495 if (!inited)
496 mono_config_init ();
498 context = g_markup_parse_context_new (&mono_parser, (GMarkupParseFlags)0, state, NULL);
499 if (g_markup_parse_context_parse (context, text, len, NULL)) {
500 g_markup_parse_context_end_parse (context, NULL);
502 g_markup_parse_context_free (context);
505 /* If assembly is NULL, parse in the global context */
506 static int
507 mono_config_parse_file_with_context (MonoConfigParseState *state, const char *filename)
509 gchar *text;
510 gsize len;
511 gint offset;
513 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
514 "Config attempting to parse: '%s'.", filename);
516 if (!g_file_get_contents (filename, &text, &len, NULL))
517 return 0;
519 offset = 0;
520 if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
521 offset = 3; /* Skip UTF-8 BOM */
522 if (state->user_data == NULL)
523 state->user_data = (gpointer) filename;
524 mono_config_parse_xml_with_context (state, text + offset, len - offset);
525 g_free (text);
526 return 1;
530 * mono_config_parse_memory:
531 * \param buffer a pointer to an string XML representation of the configuration
532 * Parses the configuration from a buffer
534 void
535 mono_config_parse_memory (const char *buffer)
537 MonoConfigParseState state = {NULL};
539 state.user_data = (gpointer) "<buffer>";
540 mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
543 static void
544 mono_config_parse_file (const char *filename)
546 MonoConfigParseState state = {NULL};
547 state.user_data = (gpointer) filename;
548 mono_config_parse_file_with_context (&state, filename);
552 * use the equivalent lookup code from the GAC when available.
553 * Depending on state, this should give something like:
554 * aname/version-pubtoken/
555 * aname/version/
556 * aname
558 static char*
559 get_assembly_filename (MonoImage *image, int state)
561 switch (state) {
562 case 0:
563 return g_strdup (mono_image_get_name (image));
564 default:
565 return NULL;
569 typedef struct _BundledConfig BundledConfig;
571 struct _BundledConfig {
572 BundledConfig *next;
573 const char* aname;
574 const char* config_xml;
577 static BundledConfig *bundled_configs = NULL;
579 static const char *bundled_machine_config = NULL;
582 * mono_register_config_for_assembly:
584 void
585 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
587 BundledConfig *bconfig;
589 bconfig = g_new0 (BundledConfig, 1);
590 bconfig->aname = assembly_name;
591 bconfig->config_xml = config_xml;
592 bconfig->next = bundled_configs;
593 bundled_configs = bconfig;
597 * mono_config_string_for_assembly_file:
599 const char *
600 mono_config_string_for_assembly_file (const char *filename)
602 BundledConfig *bconfig;
604 for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
605 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
606 return bconfig->config_xml;
608 return NULL;
612 * mono_config_for_assembly:
614 void
615 mono_config_for_assembly (MonoImage *assembly)
617 MONO_ENTER_GC_UNSAFE;
618 mono_config_for_assembly_internal (assembly);
619 MONO_EXIT_GC_UNSAFE;
622 void
623 mono_config_for_assembly_internal (MonoImage *assembly)
625 MONO_REQ_GC_UNSAFE_MODE;
627 MonoConfigParseState state = {NULL};
628 int got_it = 0, i;
629 char *aname, *cfg, *cfg_name;
630 const char *bundled_config;
632 state.assembly = assembly;
634 bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
635 if (bundled_config) {
636 state.user_data = (gpointer) "<bundled>";
637 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
640 cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
641 mono_config_parse_file_with_context (&state, cfg_name);
642 g_free (cfg_name);
644 cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
646 for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
647 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
648 got_it += mono_config_parse_file_with_context (&state, cfg);
649 g_free (cfg);
651 #ifdef TARGET_WIN32
652 const char *home = g_get_home_dir ();
653 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
654 got_it += mono_config_parse_file_with_context (&state, cfg);
655 g_free (cfg);
656 #endif
657 g_free (aname);
658 if (got_it)
659 break;
661 g_free (cfg_name);
665 * mono_config_parse:
666 * \param filename the filename to load the configuration variables from.
667 * Pass a NULL filename to parse the default config files
668 * (or the file in the \c MONO_CONFIG env var).
670 void
671 mono_config_parse (const char *filename) {
672 const char *home;
673 char *mono_cfg;
674 #ifndef TARGET_WIN32
675 char *user_cfg;
676 #endif
678 if (filename) {
679 mono_config_parse_file (filename);
680 return;
683 // FIXME: leak, do we store any references to home
684 char *env_home = g_getenv ("MONO_CONFIG");
685 if (env_home) {
686 mono_config_parse_file (env_home);
687 return;
690 mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
691 mono_config_parse_file (mono_cfg);
692 g_free (mono_cfg);
694 #if !defined(TARGET_WIN32)
695 home = g_get_home_dir ();
696 user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
697 mono_config_parse_file (user_cfg);
698 g_free (user_cfg);
699 #endif
703 * mono_set_config_dir:
704 * Invoked during startup
706 void
707 mono_set_config_dir (const char *dir)
709 /* If this environment variable is set, overrides the directory computed */
710 char *env_mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
711 if (env_mono_cfg_dir == NULL && dir != NULL)
712 env_mono_cfg_dir = g_strdup (dir);
714 mono_cfg_dir = env_mono_cfg_dir;
718 * mono_get_config_dir:
720 const char*
721 mono_get_config_dir (void)
723 if (mono_cfg_dir == NULL)
724 mono_set_dirs (NULL, NULL);
726 return mono_cfg_dir;
730 * mono_register_machine_config:
732 void
733 mono_register_machine_config (const char *config_xml)
735 bundled_machine_config = config_xml;
739 * mono_get_machine_config:
741 const char *
742 mono_get_machine_config (void)
744 return bundled_machine_config;
747 static void
748 assembly_binding_end (gpointer user_data, const char *element_name)
750 ParserUserData *pud = (ParserUserData *)user_data;
752 if (!strcmp (element_name, "dependentAssembly")) {
753 if (pud->info_parsed && pud->info) {
754 pud->info_parsed (pud->info, pud->user_data);
755 g_free (pud->info->name);
756 g_free (pud->info->culture);
761 static void
762 publisher_policy_start (gpointer user_data,
763 const gchar *element_name,
764 const gchar **attribute_names,
765 const gchar **attribute_values)
767 ParserUserData *pud;
768 MonoAssemblyBindingInfo *info;
769 int n;
771 pud = (ParserUserData *)user_data;
772 info = pud->info;
773 if (!strcmp (element_name, "dependentAssembly")) {
774 info->name = NULL;
775 info->culture = NULL;
776 info->has_old_version_bottom = FALSE;
777 info->has_old_version_top = FALSE;
778 info->has_new_version = FALSE;
779 info->is_valid = FALSE;
780 memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
781 memset (&info->old_version_top, 0, sizeof (info->old_version_top));
782 memset (&info->new_version, 0, sizeof (info->new_version));
783 } else if (!strcmp (element_name, "assemblyIdentity")) {
784 for (n = 0; attribute_names [n]; n++) {
785 const gchar *attribute_name = attribute_names [n];
787 if (!strcmp (attribute_name, "name"))
788 info->name = g_strdup (attribute_values [n]);
789 else if (!strcmp (attribute_name, "publicKeyToken")) {
790 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
791 g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
792 } else if (!strcmp (attribute_name, "culture")) {
793 if (!strcmp (attribute_values [n], "neutral"))
794 info->culture = g_strdup ("");
795 else
796 info->culture = g_strdup (attribute_values [n]);
799 } else if (!strcmp (element_name, "bindingRedirect")) {
800 for (n = 0; attribute_names [n]; n++) {
801 const gchar *attribute_name = attribute_names [n];
803 if (!strcmp (attribute_name, "oldVersion")) {
804 gchar **numbers, **version, **versions;
805 gint major, minor, build, revision;
807 /* Invalid value */
808 if (!strcmp (attribute_values [n], ""))
809 return;
811 versions = g_strsplit (attribute_values [n], "-", 2);
812 version = g_strsplit (*versions, ".", 4);
814 /* We assign the values to gint vars to do the checks */
815 numbers = version;
816 major = *numbers ? atoi (*numbers++) : -1;
817 minor = *numbers ? atoi (*numbers++) : -1;
818 build = *numbers ? atoi (*numbers++) : -1;
819 revision = *numbers ? atoi (*numbers) : -1;
820 g_strfreev (version);
821 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
822 g_strfreev (versions);
823 return;
826 info->old_version_bottom.major = major;
827 info->old_version_bottom.minor = minor;
828 info->old_version_bottom.build = build;
829 info->old_version_bottom.revision = revision;
830 info->has_old_version_bottom = TRUE;
832 if (!*(versions + 1)) {
833 g_strfreev (versions);
834 continue;
837 numbers = version = g_strsplit (*(versions + 1), ".", 4);
838 major = *numbers ? atoi (*numbers++) : -1;
839 minor = *numbers ? atoi (*numbers++) : -1;
840 build = *numbers ? atoi (*numbers++) : -1;
841 revision = *numbers ? atoi (*numbers) : 1;
842 g_strfreev (version);
843 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
844 g_strfreev (versions);
845 return;
848 info->old_version_top.major = major;
849 info->old_version_top.minor = minor;
850 info->old_version_top.build = build;
851 info->old_version_top.revision = revision;
852 info->has_old_version_top = TRUE;
854 g_strfreev (versions);
855 } else if (!strcmp (attribute_name, "newVersion")) {
856 gchar **numbers, **version;
858 /* Invalid value */
859 if (!strcmp (attribute_values [n], ""))
860 return;
862 numbers = version = g_strsplit (attribute_values [n], ".", 4);
863 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
864 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
865 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
866 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
867 info->has_new_version = TRUE;
868 g_strfreev (version);
874 static MonoParseHandler
875 publisher_policy_parser = {
876 "", /* We don't need to use declare an xml element */
877 NULL,
878 publisher_policy_start,
879 NULL,
880 NULL,
881 NULL
884 void
885 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
887 ParserUserData user_data = {
888 info,
889 NULL,
890 NULL
892 MonoConfigParseState state = {
893 &publisher_policy_parser, /* MonoParseHandler */
894 &user_data, /* user_data */
895 NULL, /* MonoImage (we don't need it right now)*/
896 TRUE /* We are already inited */
899 mono_config_parse_file_with_context (&state, filename);
902 static MonoParseHandler
903 config_assemblybinding_parser = {
904 "", /* We don't need to use declare an xml element */
905 NULL,
906 publisher_policy_start,
907 NULL,
908 assembly_binding_end,
909 NULL
912 void
913 mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
915 MonoAssemblyBindingInfo info;
916 ParserUserData pud;
917 MonoConfigParseState state;
919 info.major = amajor;
920 info.minor = aminor;
922 pud.info = &info;
923 pud.info_parsed = infocb;
924 pud.user_data = user_data;
926 state.current = &config_assemblybinding_parser; /* MonoParseHandler */
927 state.user_data = &pud;
928 state.assembly = NULL; /* MonoImage (we don't need it right now)*/
929 state.inited = TRUE; /* We are already inited */
931 mono_config_parse_file_with_context (&state, filename);
934 static mono_bool mono_server_mode = FALSE;
937 * mono_config_set_server_mode:
939 void
940 mono_config_set_server_mode (mono_bool server_mode)
942 mono_server_mode = server_mode;
946 * mono_config_is_server_mode:
948 mono_bool
949 mono_config_is_server_mode (void)
951 return mono_server_mode;